00001
00002
00003 #include "parser/CalibHR1Parser.hh"
00004 #include "parser/AcquisitionParser.hh"
00005
00006 #include "event/Event.hh"
00007 #include "geometry/Detector.hh"
00008 #include "geometry/Dif.hh"
00009 #include "geometry/Board.hh"
00010 #include "geometry/Chip.hh"
00011 #include "geometry/Hardroc1Chip.hh"
00012 #include "geometry/Chamber.hh"
00013
00014 #include "tools/MicroException.hh"
00015 #include "tools/Toolbox.hh"
00016 #include "tools/SteerDesc.hh"
00017 #include "tools/Log.hh"
00018
00019 #include <iostream>
00020 #include <iomanip>
00021 #include <fstream>
00022 #include <string>
00023 #include <cstdio>
00024 #include "math.h"
00025
00026 using namespace std;
00027
00028
00029
00030 #define bigTolittle Toolbox::bigTolittle
00031
00032 using namespace std;
00033
00034
00035
00036
00037
00038
00039
00040
00041 static unsigned int grayToBinary2(unsigned int gray) {
00042
00043 unsigned int bin = ((gray >> 31) & 1) << 31;
00044 for (int bit = 30; bit >= 0; --bit)
00045 bin += ( ((gray >> bit) & 1) ^ ((bin >> (bit + 1)) & 1) ) << bit;
00046
00047 return(bin);
00048 }
00049
00050
00051
00052 CalibHR1Parser::CalibHR1Parser(Run& aRun, FILE *aFile,ui32 _firstEventId)
00053 : AcquisitionParser(aRun, aFile,_firstEventId)
00054 {
00055 FILE_LOG(logDEBUG1) << "--CalibHR1Parser constructor()"<< endl;
00056
00057 run.setCalibrationRun(true);
00058
00059 reset();
00060 }
00061
00062
00063 CalibHR1Parser::~CalibHR1Parser()
00064 {
00065 FILE_LOG(logDEBUG1)<< "----CalibHR1Parser destructeur"<< endl;
00066 }
00067
00068
00069
00070 void CalibHR1Parser::reset()
00071 {
00072 init = false;
00073 newCalibration = true;
00074 newDif = true;
00075 newHR = true;
00076 difId = -1;
00077 dac[0] = -1;
00078 dac[1] = -1;
00079 dac[2] = -1;
00080 difId = -1;
00081
00082
00083
00084 }
00085
00086 void CalibHR1Parser::setMotifValue(string line)
00087 {
00088 ui64 result = 0;
00089 string mot;
00090 int equalpos = 0;
00091
00092 equalpos = line.find('=') ;
00093 if ( equalpos != string::npos )
00094 {
00095 mot = line.substr(equalpos + 2, line.size());
00096
00097
00098
00099 for(int i=MOTIFNB-1; i>=0 ; --i)
00100 {
00101
00102 motif[i] = false;
00103 if ( mot[i] == '1')
00104 {
00105 motif[i] = true;
00106 }
00107 }
00108 }
00109
00110 }
00111
00112
00113
00114
00115 int CalibHR1Parser::getDacValue(string line)
00116 {
00117 int result = -1;
00118 string value;
00119 int equalpos = 0;
00120
00121 equalpos = line.find('=') ;
00122 if ( equalpos != string::npos )
00123 {
00124 value = line.substr(equalpos + 2, line.size());
00125 result = atoi(value.c_str());
00126 }
00127
00128 return result;
00129
00130 }
00131
00132
00133 void CalibHR1Parser::initParsing(FILE* inputFile)
00134 {
00135
00136 string line;
00137 size_t found;
00138
00139
00140 do
00141 {
00142 if ( getLine ( inputFile, line) == EOF ) { return ;}
00143 found = line.find("nb acquisitions" );
00144
00145 line.clear();
00146 }
00147 while (found==string::npos);
00148
00149 getLine ( inputFile, line);
00150
00151
00152
00153 if ( getLine ( inputFile, line) == EOF ) { return ;}
00154 found = line.find("motif Ctest" );
00155 if (found!=string::npos)
00156 {
00157 setMotifValue(line);
00158 }
00159 }
00160
00161
00162 bool CalibHR1Parser::checkNextLine(FILE* inputFile, string target )
00163 {
00164 size_t found;
00165 string line;
00166
00167 int curPos = ftell(inputFile);
00168
00169 if ( getLine ( inputFile, line) == EOF ) { return false ;}
00170
00171 fseek(inputFile,curPos,SEEK_SET);
00172 found = line.find(target );
00173 if (found!=string::npos)
00174 {
00175 return true;
00176 }
00177 return false;
00178
00179 }
00180
00181
00182 int CalibHR1Parser::getNextEvent(Event& event)
00183 {
00184 unsigned int nbHits = 0;
00185
00186
00187 if ( ! init)
00188 {
00189 initParsing(inputFile);
00190 init = true;
00191 }
00192
00193 string line;
00194
00195 if ( newCalibration)
00196 {
00197 FILE_LOG(logDEBUG1) << "New calibration !" << endl;
00198 size_t found;
00199
00200 FILE_LOG(logDEBUG1) << "---------> DEBUG A" << endl;
00201 do
00202 {
00203 int result = getLine ( inputFile, line);
00204 if (result == EOF)
00205 {
00206 reset();
00207 return LAST_FILE_EVENT;
00208 }
00209
00210
00211 found=line.find("DAC_1");
00212 }
00213 while (found==string::npos);
00214
00215 dac[0] = -1;
00216 dac[1] = getDacValue(line);
00217
00218
00219 if ( getLine ( inputFile, line) != EOF )
00220 {
00221 if ( line.find("DAC_0")!=string::npos) { dac[0] = getDacValue(line); }
00222 else { return EVENT_ERROR; }
00223 }
00224
00225
00226
00227 run.setName("turlututu");
00228 const Detector &detector = run.getDetector();
00229 try {
00230 ChamberMap_t chambers = detector.getChambers();
00231 for (ChamberMap_t::iterator chamberIt = chambers.begin(); chamberIt != chambers.end(); chamberIt++) {
00232 Chamber& chamber = *chamberIt->second;
00233 DifMap_t difs = chamber.getDifs();
00234 for (DifMap_t::iterator difIt = difs.begin(); difIt != difs.end(); difIt++) {
00235 Dif& dif = *difIt->second;
00236 BoardMap_t boards = dif.getBoards();
00237 for (BoardMap_t::iterator boardIt = boards.begin(); boardIt != boards.end(); boardIt++) {
00238 Board& b = *boardIt->second;
00239 ChipMap_t chips = b.getChips();
00240 for (ChipMap_t::iterator chipIt = chips.begin(); chipIt != chips.end(); chipIt++) {
00241
00242 Hardroc1Chip &chip = dynamic_cast<Hardroc1Chip &> (*chipIt->second);
00243 chip.setThresholdDac_0(dac[0]);
00244 chip.setThresholdDac_1(dac[1]);
00245 for (int chNum = 0; chNum < 64; chNum++) {
00246 Channel& ch = chip.getChannelById(chNum);
00247 ch.setEnable(1);
00248 }
00249 }
00250 }
00251 }
00252 }
00253 }
00254 catch (MicroException &e) { cout << e.getMessage() << endl; }
00255
00256 newCalibration = false;
00257 return NEW_CONFIG;
00258 }
00259 if ( newDif)
00260 {
00261 FILE_LOG(logDEBUG1) << "New Dif !" << endl;
00262
00263 int space = 0;
00264 int b0Pos = 6;
00265 bool find = false;
00266 do
00267 {
00268 b0Pos = ftell(inputFile);
00269 if (getLine ( inputFile, line) == EOF) {
00270 return(LAST_FILE_EVENT);
00271 }
00272
00273
00274 if (checkNextLine(inputFile,"DAC_") )
00275 {
00276 newCalibration = true;
00277 FILE_LOG(logWARNING) << "No data for DAC values:[" << dac[0] << "," << dac[1] << "," << dac[2] << "]" << endl;
00278 return getNextEvent( event);
00279 }
00280
00281 int b0find = line.find("B0");
00282 int b4pos = 0;
00283 do
00284 {
00285 b4pos = line.find("B4",b4pos+1);
00286
00287 if ( b4pos - b0find == 46 )
00288 {
00289 find = true;
00290 }
00291 }
00292 while (b4pos != string::npos && find == false );
00293
00294 }
00295 while ( !find );
00296
00297 b0Pos = b0Pos + 18 + 2 ;
00298 fseek(inputFile, b0Pos, SEEK_SET);
00299
00300
00301
00302
00303
00304 difId = getData(inputFile, 1);
00305 FILE_LOG(logDEBUG1) << " difId[" << difId << "]" << endl;
00306
00307 fseek(inputFile, 42, SEEK_CUR);
00308
00309
00310
00311 int dataHeader = getData(inputFile, 1);
00312 FILE_LOG(logDEBUG1) << (hex) << " dataHeader[" << dataHeader << "]" << endl;
00313 if (dataHeader == 0xB4)
00314 {
00315 FILE_LOG(logDEBUG1) << " data header B4 found" << endl;
00316 }
00317 else
00318 {
00319 FILE_LOG(logERROR) << " parsing file error. data header error : " << hex << dataHeader << dec << endl;
00320 return EVENT_ERROR;
00321 }
00322
00323 newDif = false;
00324
00325
00326 }
00327 if ( newHR)
00328 {
00329 FILE_LOG(logDEBUG1) << "New HR !" << endl;
00330
00331 if ( checkData(inputFile, 1) == 0xA3)
00332 {
00333 FILE_LOG(logERROR) << "ERROR TAG A3 just after newHR for DAC values:[" << dac[0] << "," << dac[1] << "," << dac[2] << "]" << endl;
00334 return EVENT_ERROR;
00335 }
00336
00337 fseek ( inputFile, 40, SEEK_CUR);
00338 newHR = false;
00339 }
00340
00341
00342
00343
00344
00345
00346 int chipId = -1;
00347
00348 int foo = getData(inputFile , 1);
00349 if ( foo == 0xA3)
00350 {
00351 FILE_LOG(logDEBUG1) << " -------- TAG A3 " << endl;
00352 newHR = true;
00353 foo = getData(inputFile , 1);
00354 if ( foo == 0xA0 )
00355 {
00356 newDif = true;
00357 FILE_LOG(logDEBUG1) << " -------- Tag A0 derriere A3 " << endl;
00358 foo = getData(inputFile , 2 );
00359 foo = getData(inputFile, 1 );
00360 if ( foo != 0xB0)
00361 {
00362
00363 newCalibration = true;
00364 FILE_LOG(logDEBUG1) << " -------- Pas de B0 derrire le CRC donc newCalibration = true " << endl;
00365 }
00366 else {
00367 FILE_LOG(logDEBUG1) << " -------- B0 derrire le CRC donc Dif " << endl;
00368 }
00369
00370 }
00371 return getNextEvent( event);
00372 }
00373 else
00374 {
00375 fseek ( inputFile, -2, SEEK_CUR);
00376 chipId = getData(inputFile, 1);
00377
00378
00379 int bcId = getData(inputFile, 3);
00380 FILE_LOG(logDEBUG1) << (hex) << " bcid[" << bcId << "]" << endl;
00381
00382
00383
00384
00385 bool difFound = false;
00386 Detector& detector = run.getDetector();
00387 { const ChamberMap_t &chambers = detector.getChambers();
00388 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00389 try {
00390 Chamber& chamber = *(it->second);
00391 const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00392 }
00393 catch (...) {
00394
00395 continue;
00396 }
00397
00398
00399 difFound = true;
00400 break;
00401 }
00402 if (!difFound) {
00403 FILE_LOG(logERROR) << " No dif found with Id 0x" << hex << difId << dec << endl;
00404 return EVENT_ERROR;
00405 }
00406 }
00407
00408
00409
00410
00411 int canal[16]={0};
00412 for (int chNum = 48; chNum >= 0; chNum -= 16) {
00413 unsigned int data4 = getData(inputFile, 4);
00414
00415 int index = 0;
00416 for ( index = 0; index <= 15 ; index++)
00417 {
00418
00419 canal[index] = (data4 >> ((15 - index )* 2 )) & 0x03;
00420
00421 if ( motif[index+chNum])
00422 {
00423 newHit(event, detector, canal[index], index + chNum , chipId, difId);
00424 nbHits++;
00425 }
00426 }
00427 }
00428
00429 event.setId(++lastEventId);
00430 FILE_LOG(logDEBUG1) << " STORE EVENT " << lastEventId << ", " << nbHits << " hits" << endl;
00431 return EVENT_CORRECT;
00432 }
00433
00434 }
00435
00436
00437
00438 int CalibHR1Parser::getLine ( FILE*file, string &line )
00439 {
00440 line.clear();
00441 int result = fgetc(file ) ;
00442 while ( result != EOF )
00443 {
00444 line += result;
00445 if ( result == '\n' )
00446 {
00447 line.resize (line.size()-1);
00448 return 0;
00449 }
00450 result = fgetc(file ) ;
00451 }
00452 return EOF;
00453 }
00454
00455
00456
00457 unsigned int CalibHR1Parser::checkData(FILE* inputFile, const int nBytes) {
00458
00459 int curPos = ftell(inputFile);
00460 unsigned int result = getData(inputFile, nBytes);
00461 fseek(inputFile,curPos,SEEK_SET);
00462 return result;
00463 }
00464
00465
00466
00467 unsigned int CalibHR1Parser::getData(FILE* inputFile, const int nBytes) {
00468 unsigned int result = 0;
00469 for (int byte = 0; byte < nBytes; ++byte) {
00470 int data = 0;
00471 if (fscanf(inputFile, "%2x", &data) != 1) {
00472 return(0);
00473 }
00474 result = (result << 8) + data;
00475 }
00476 return(result);
00477 }
00478
00479
00480
00481
00482 unsigned int CalibHR1Parser::getDecimalData(FILE* inputFile, const int nBytes) {
00483 unsigned int result = 0;
00484 for (int byte = 0; byte < nBytes; ++byte) {
00485 int data = 0;
00486 if (fscanf(inputFile, "%2d", &data) != 1) {
00487 return(0);
00488 }
00489 result = (result << 8) + data;
00490 }
00491 return(result);
00492 }
00493
00494
00495
00496
00497 int CalibHR1Parser::newHit(Event& event, const Detector &detector, int data, const int chNum, const int chipId, const int difId)
00498 {
00499
00500
00501
00502
00503
00504
00505 const ChamberMap_t &chambers = detector.getChambers();
00506 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00507 try {
00508 Chamber& chamber = *(it->second);
00509 const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00510
00511
00512
00513
00514 try {
00515 const Channel& channel = chamber.getChannelById(chNum, chipId, difId);
00516 i16 analogValue = 0;
00517
00518 ChannelHit *hit = new ChannelHit(channel, data, analogValue);
00519
00520 event.insertHit(chamber, hit);
00521 FILE_LOG(logDEBUG1) << " new Hit : ch " << chNum << ", chip " << chipId << ", dif " << difId
00522 << " Data=" << data << " , x:" << channel.getX() << ", y:" << channel.getY() << endl;
00523 return(1);
00524 }
00525 catch (MicroException &e) {
00526 FILE_LOG(logERROR) << "ch " << chNum << ", chip " << chipId << ", dif " << difId << " : " << e.getMessage() << endl;
00527 return(0);
00528 }
00529 }
00530 catch (...) {
00531 continue;
00532 }
00533
00534 }
00535 return(0);
00536 }
00537
00538
00539
00540
00541
00542 string CalibHR1Parser::binaire(unsigned int nombre)
00543 {
00544 string res = "";
00545 unsigned int val = nombre;
00546
00547 while (val != 0) {
00548
00549
00550 if (val & 1 == 1) {
00551 res = string("1") + res;
00552 }
00553 else {
00554
00555 res = string("0") + res;
00556 }
00557
00558 val = val >> 1;
00559 }
00560 return res;
00561 }