00001
00002
00003 #include "parser/CalibMicrorocParser.hh"
00004 #include "parser/MicrorocSCReader.hh"
00005 #include "parser/AcquisitionParser.hh"
00006
00007 #include "event/Event.hh"
00008
00009 #include "geometry/Detector.hh"
00010 #include "geometry/Dif.hh"
00011 #include "geometry/Board.hh"
00012 #include "geometry/Chip.hh"
00013 #include "geometry/MicrorocChip.hh"
00014 #include "geometry/Chamber.hh"
00015
00016 #include "tools/MicroException.hh"
00017 #include "tools/Toolbox.hh"
00018 #include "tools/SteerDesc.hh"
00019 #include "tools/Log.hh"
00020
00021 #include <cstdio>
00022 #include <iostream>
00023 #include <iomanip>
00024 #include <fstream>
00025 #include <string>
00026 #include "math.h"
00027
00028 using namespace std;
00029
00030
00031
00032 #define bigTolittle Toolbox::bigTolittle
00033
00034 using namespace std;
00035
00036
00037
00038
00039
00040
00041
00042
00043 static unsigned int grayToBinary2(unsigned int gray) {
00044
00045 unsigned int bin = ((gray >> 31) & 1) << 31;
00046 for (int bit = 30; bit >= 0; --bit)
00047 bin += ( ((gray >> bit) & 1) ^ ((bin >> (bit + 1)) & 1) ) << bit;
00048
00049 return(bin);
00050 }
00051
00052
00053
00054
00055 CalibMicrorocParser::CalibMicrorocParser(Run& aRun, FILE *aFile, ui32 _firstEventId,bool _testMicroroc)
00056 : AcquisitionParser(aRun, aFile, _firstEventId), scReader(NULL)
00057 {
00058
00059 testMicroroc = _testMicroroc;
00060 run.setCalibrationRun(true);
00061 run.setName("Microroc calibration Run");
00062
00063 reset();
00064 }
00065
00066
00067 CalibMicrorocParser::~CalibMicrorocParser()
00068 {
00069 FILE_LOG(logDEBUG1)<< "----CalibMicrorocParser destructeur"<< endl;
00070 }
00071
00072
00073 void CalibMicrorocParser::reset()
00074 {
00075 init = false;
00076 newCalibration = true;
00077 newDif = true;
00078 newHR = true;
00079 FILE_LOG(logDEBUG) << " -------- reset force newHR to true " << endl;
00080 difId = -1;
00081 dac[0] = -1;
00082 dac[1] = -1;
00083 dac[2] = -1;
00084 shaper[0] = -1;
00085 shaper[1] = -1;
00086 difId = -1;
00087 charge = 0.;
00088
00089 for ( int i = 0; i < MOTIFNB; i++)
00090 {
00091 motif[i] = 0;
00092 }
00093
00094
00095 }
00096
00097 void CalibMicrorocParser::setChargeValue(string line)
00098 {
00099 ui64 result = 0;
00100 string mot;
00101 int equalpos = 0;
00102
00103 equalpos = line.find('=') ;
00104 if ( equalpos != string::npos )
00105 {
00106 mot = line.substr(equalpos + 2, line.size());
00107 }
00108
00109 string sep = ".";
00110 equalpos = mot.find(',') ;
00111 if ( equalpos != string::npos )
00112 {
00113 mot.replace(equalpos,1,sep);
00114 }
00115
00116 charge = atof(mot.c_str());
00117 FILE_LOG(logDEBUG) << "CHARGE["<< charge << "]" << endl;
00118 run.setInjectedCharge(charge);
00119
00120 }
00121
00122 void CalibMicrorocParser::setMotifValue(string line)
00123 {
00124 ui64 result = 0;
00125 string mot;
00126 int equalpos = 0;
00127
00128 equalpos = line.find('=') ;
00129 if ( equalpos != string::npos )
00130 {
00131 mot = line.substr(equalpos + 2, line.size());
00132
00133
00134
00135
00136 for(int i=MOTIFNB-1; i>=0 ; --i)
00137 {
00138 motif[i] = false;
00139 if ( mot[i] == '1')
00140 {
00141 motif[i] = true;
00142 }
00143 }
00144 }
00145
00146 }
00147
00148
00149
00150
00151 int CalibMicrorocParser::getDacValue(string line)
00152 {
00153 int result = -1;
00154 string value;
00155 int equalpos = 0;
00156
00157 equalpos = line.find('=') ;
00158 if ( equalpos != string::npos )
00159 {
00160 value = line.substr(equalpos + 2, line.size());
00161 result = atoi(value.c_str());
00162 }
00163
00164 return result;
00165
00166 }
00167
00168
00169 void CalibMicrorocParser::initParsing(FILE* inputFile)
00170 {
00171
00172 string line;
00173 size_t found;
00174
00175 do
00176 {
00177 if ( getLine ( inputFile, line) == EOF ) { return ;}
00178 found = line.find("charge (fC)" );
00179 if (found!=string::npos)
00180 {
00181 setChargeValue(line);
00182 }
00183 line.clear();
00184 }
00185 while (found==string::npos);
00186 do
00187 {
00188 if ( getLine ( inputFile, line) == EOF ) { return ;}
00189 found = line.find("Chips en calibration" );
00190 line.clear();
00191 }
00192 while (found==string::npos);
00193
00194
00195 if ( getLine ( inputFile, line) == EOF ) { return ;}
00196 found = line.find(getCtestString() );
00197 if (found!=string::npos)
00198 {
00199 setMotifValue(line);
00200 }
00201 else {
00202 found = line.find(CalibMicrorocParser::getCtestString() );
00203 if (found!=string::npos)
00204 {
00205 setMotifValue(line);
00206 }
00207 }
00208
00209
00210
00211 do
00212 {
00213 if ( getLine ( inputFile, line) == EOF ) { return ;}
00214 found = line.find("donnees Slow-Control" );
00215 line.clear();
00216 }
00217 while (found==string::npos);
00218
00219 if ( getLine ( inputFile, line) == EOF ) { return ;}
00220
00221 line = line.substr(0, line.size());
00222
00223
00224 scReader = new MicrorocSCReader(run,line,difId);
00225
00226 }
00227
00228
00229 bool CalibMicrorocParser::checkNextLine(FILE* inputFile, string target )
00230 {
00231 size_t found;
00232 string line;
00233
00234
00235 int curPos = ftell(inputFile);
00236
00237 if ( getLine ( inputFile, line) == EOF ) { return false ;}
00238
00239 fseek(inputFile,curPos,SEEK_SET);
00240 found = line.find(target );
00241 FILE_LOG(logDEBUG) << "------- read line[" << line << endl;
00242
00243 if (found!=string::npos)
00244 {
00245 return true;
00246 }
00247 return false;
00248
00249 }
00250
00251
00252 int CalibMicrorocParser::getNextEvent(Event& event)
00253 {
00254 unsigned int nbHits = 0;
00255
00256
00257 if ( ! init)
00258 {
00259 initParsing(inputFile);
00260 init = true;
00261 }
00262
00263 string line;
00264
00265 if ( newCalibration)
00266 {
00267 FILE_LOG(logDEBUG1) << "New calibration !" << endl;
00268 size_t found;
00269
00270 do
00271 {
00272 int result = getLine ( inputFile, line);
00273 if (result == EOF)
00274 {
00275 FILE_LOG(logDEBUG1) << "End of file !" << endl;
00276 reset();
00277 FILE_LOG(logDEBUG1) << "return LAST_FILE_EVENT:[" << LAST_FILE_EVENT << endl;
00278 return LAST_FILE_EVENT;
00279 }
00280
00281
00282 found=line.find("DAC_2");
00283 }
00284 while (found==string::npos);
00285
00286 dac[0] = -1;
00287 dac[1] = -1;
00288 dac[2] = getDacValue(line);
00289
00290
00291 if ( getLine ( inputFile, line) != EOF )
00292 {
00293 if ( line.find("DAC_1")!=string::npos) { dac[1] = getDacValue(line); }
00294 else { return EVENT_ERROR; }
00295 }
00296
00297
00298 if ( getLine ( inputFile, line) != EOF )
00299 {
00300 if ( line.find("DAC_0")!=string::npos) { dac[0] = getDacValue(line); }
00301 else { return EVENT_ERROR; }
00302 }
00303 FILE_LOG(logDEBUG) << "dac_2[" << dac[2] << "]" << endl;
00304 FILE_LOG(logDEBUG) << "dac_1[" << dac[1] << "]" << endl;
00305 FILE_LOG(logDEBUG) << "dac_0[" << dac[0] << "]" << endl;
00306
00307 const Detector &detector = run.getDetector();
00308 try {
00309 ChamberMap_t chambers = detector.getChambers();
00310 for (ChamberMap_t::iterator chamberIt = chambers.begin(); chamberIt != chambers.end(); chamberIt++) {
00311 Chamber& chamber = *chamberIt->second;
00312 DifMap_t difs = chamber.getDifs();
00313 for (DifMap_t::iterator difIt = difs.begin(); difIt != difs.end(); difIt++) {
00314 Dif& dif = *difIt->second;
00315 BoardMap_t boards = dif.getBoards();
00316 for (BoardMap_t::iterator boardIt = boards.begin(); boardIt != boards.end(); boardIt++) {
00317 Board& b = *boardIt->second;
00318 ChipMap_t chips = b.getChips();
00319 for (ChipMap_t::iterator chipIt = chips.begin(); chipIt != chips.end(); chipIt++) {
00320 MicrorocChip &chip = dynamic_cast<MicrorocChip &> (*chipIt->second);
00321 chip.setThresholdDac_0(dac[0]);
00322 chip.setThresholdDac_1(dac[1]);
00323 chip.setThresholdDac_2(dac[2]);
00324
00325
00326
00327
00328 }
00329 }
00330 }
00331 }
00332 }
00333 catch (MicroException &e) { cout << e.getMessage() << endl; }
00334
00335 newCalibration = false;
00336 return NEW_CONFIG;
00337 }
00338 if ( newDif)
00339 {
00340 FILE_LOG(logDEBUG1) << "New Dif !" << endl;
00341
00342 int space = 0;
00343 int b0Pos = 6;
00344 bool find = false;
00345 do
00346 {
00347 b0Pos = ftell(inputFile);
00348
00349 FILE_LOG(logDEBUG) << "inputFile [" << inputFile << "]" << endl;
00350 FILE_LOG(logDEBUG) << "b0Pos [" << b0Pos << "]" << endl;
00351 if (getLine ( inputFile, line) == EOF) {
00352 return(LAST_FILE_EVENT);
00353 }
00354
00355
00356
00357 if (checkNextLine(inputFile,"DAC_") )
00358 {
00359 newCalibration = true;
00360 FILE_LOG(logWARNING) << "No data for DAC values:[" << dac[0] << "," << dac[1] << "," << dac[2] << "]" << endl;
00361 return getNextEvent( event);
00362 }
00363
00364 int b0find = line.find("B0",18);
00365 FILE_LOG(logDEBUG) << "b0find [" << b0find << "]" << endl;
00366 int b4pos = 0;
00367 do
00368 {
00369 b4pos = line.find("B4",b4pos+1);
00370 FILE_LOG(logDEBUG) << "b4pos [" << b4pos << "]" << endl;
00371 FILE_LOG(logDEBUG) << "b4pos - b0find [" << b4pos - b0find << "]" << endl;
00372
00373 ui16 gap = 48;
00374
00375 if ( testMicroroc == true ) { gap = 46 ;}
00376
00377 if ( b4pos - b0find == gap )
00378 {
00379 find = true;
00380 }
00381 }
00382 while (b4pos != string::npos && find == false );
00383 }
00384 while ( !find );
00385
00386 b0Pos = b0Pos + 18 + 2 ;
00387 FILE_LOG(logDEBUG) << " b0Pos = b0Pos + 18 + 2 [" << b0Pos << "]" << endl;
00388 fseek(inputFile, b0Pos, SEEK_SET);
00389
00390
00391
00392
00393 this->setComputeCrcFlag(true);
00394
00395 FILE_LOG(logDEBUG) << "crc->compute(0xB0)[ "<< crc <<"]" << endl;
00396 crc->compute(0xB0);
00397
00398
00399 difId = getData(inputFile, 1);
00400 if ( scReader ) { scReader->parseSC(difId); }
00401 FILE_LOG(logDEBUG) << "!!! difId[0x"<< hex << difId << "]" << endl;
00402
00403
00404
00405 ui16 skip = 22;
00406 if ( testMicroroc == true ) { skip = 21 ;}
00407 for ( int i = 0 ; i < skip ; i++) {
00408 int foo = getData(inputFile,1);
00409
00410 }
00411
00412
00413
00414 int dataHeader = getData(inputFile, 1);
00415 FILE_LOG(logDEBUG1) << (hex) << " dataHeader[" << dataHeader << "]" << endl;
00416
00417 if (dataHeader == 0xB4)
00418 {
00419 FILE_LOG(logDEBUG1) << " data header B4 found" << endl;
00420 }
00421 else
00422 {
00423 FILE_LOG(logERROR) << " parsing file error. data header error : " << hex << dataHeader << dec << endl;
00424 return EVENT_ERROR;
00425 }
00426
00427 newDif = false;
00428 }
00429
00430
00431
00432 if ( newHR)
00433 {
00434 FILE_LOG(logDEBUG) << "New MR !" << endl;
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 newHR = false;
00445 }
00446
00447
00448
00449
00450
00451
00452 int chipId = -1;
00453
00454 int foo = getData(inputFile , 1);
00455 if ( foo == 0xA3)
00456 {
00457 FILE_LOG(logDEBUG) << " -------- TAG A3 force newHR to true " << endl;
00458 newHR = true;
00459 foo = getData(inputFile , 1);
00460 if ( foo == 0xA0 )
00461 {
00462 newDif = true;
00463 FILE_LOG(logDEBUG1) << " -------- Tag A0 derriere A3 " << endl;
00464
00465
00466 this->setComputeCrcFlag(false);
00467 unsigned short CRC_check = getData(inputFile , 2 );
00468 FILE_LOG(logDEBUG1) << hex << " Compute CRC[" << crc->getCrc() << "]" << endl;
00469 FILE_LOG(logDEBUG1) << hex << " Received CRC[" << CRC_check << "]" << endl;
00470 if ( CRC_check != crc->getCrc() )
00471 {
00472 FILE_LOG(logERROR) << " CRC Error " << endl;
00473 exit(-1);
00474 }
00475
00476 try {
00477 foo = getData(inputFile, 1 );
00478 }
00479 catch ( MicroException &e)
00480 {
00481 foo = 0;
00482 }
00483 if ( foo != 0xB0)
00484 {
00485
00486 if ( getLine ( inputFile, line) == EOF ) { return LAST_FILE_EVENT ;}
00487 if (checkNextLine(inputFile,"acquisition") )
00488 {
00489 newDif = true;
00490 }
00491 else
00492 {
00493 newCalibration = true;
00494 FILE_LOG(logDEBUG1) << " -------- Pas de B0 derrire le CRC donc newCalibration = true " << endl;
00495 }
00496 }
00497 else
00498 {
00499 FILE_LOG(logDEBUG1) << " -------- B0 derrire le CRC donc Dif " << endl;
00500 }
00501 }
00502 return getNextEvent( event);
00503 }
00504 else
00505 {
00506 fseek ( inputFile, -2, SEEK_CUR);
00507 chipId = getData(inputFile, 1, false);
00508
00509
00510
00511 int bcId = getData(inputFile, 3);
00512 FILE_LOG(logDEBUG1) << (hex) << " bcid[" << bcId << "]" << endl;
00513
00514
00515
00516
00517 bool difFound = false;
00518 Detector& detector = run.getDetector();
00519 { const ChamberMap_t &chambers = detector.getChambers();
00520 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00521 try {
00522 Chamber& chamber = *(it->second);
00523 const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00524 }
00525 catch (...) {
00526
00527 continue;
00528 }
00529
00530
00531 difFound = true;
00532 break;
00533 }
00534 if (!difFound) {
00535 FILE_LOG(logERROR) << " No dif found with Id 0x" << hex << difId << dec << endl;
00536 return EVENT_ERROR;
00537 }
00538 }
00539
00540
00541
00542
00543 int canal[16]={0};
00544 for (int chNum = 48; chNum >= 0; chNum -= 16) {
00545 unsigned int data4;
00546 try {
00547 data4 = getData(inputFile, 4);
00548 }
00549 catch ( MicroException &e)
00550 {
00551 continue;
00552 }
00553
00554
00555 int index = 0;
00556 for ( index = 0; index <= 15 ; index++)
00557 {
00558
00559 canal[index] = (data4 >> ((15 - index )* 2 )) & 0x03;
00560
00561
00562
00563 {
00564
00565 {
00566 if ( newHit(event, detector, canal[index], index + chNum , chipId, difId) != 1 )
00567 {
00568
00569 try
00570 {
00571 foo = getData(inputFile, 1) ;
00572 while ( foo != 0xA3 )
00573 {
00574 foo = getData(inputFile, 1) ;
00575 }
00576 }
00577 catch ( MicroException &e)
00578 {
00579 FILE_LOG(logINFO) << "End of line 0xA3 not Found !!!" << endl;
00580 newDif = true;
00581
00582
00583
00584 }
00585 }
00586 else
00587 {
00588 nbHits++;
00589 }}
00590 }
00591 }
00592 }
00593
00594 event.setId(++lastEventId);
00595 FILE_LOG(logDEBUG1) << " STORE EVENT " << lastEventId << ", " << nbHits << " hits" << endl;
00596 return EVENT_CORRECT;
00597 }
00598
00599 }
00600
00601
00602
00603 int CalibMicrorocParser::getLine ( FILE*file, string &line )
00604 {
00605 line.clear();
00606 int result = fgetc(file ) ;
00607 while ( result != EOF )
00608 {
00609 line += result;
00610 if ( result == '\n' )
00611 {
00612 line.resize (line.size()-1);
00613 return 0;
00614 }
00615 result = fgetc(file ) ;
00616 }
00617 return EOF;
00618 }
00619
00620
00621
00622 unsigned int CalibMicrorocParser::checkData(FILE* inputFile, const int nBytes) {
00623
00624 int curPos = ftell(inputFile);
00625 unsigned int result = getData(inputFile, nBytes);
00626 fseek(inputFile,curPos,SEEK_SET);
00627 return result;
00628 }
00629
00630
00631
00632 unsigned int CalibMicrorocParser::getData(FILE* inputFile, const int nBytes, bool computeCRC) {
00633 unsigned int result = 0;
00634 for (int byte = 0; byte < nBytes; ++byte) {
00635 int data = 0;
00636 if (fscanf(inputFile, "%2x", &data) != 1) {
00637 throw MicroException("get data: fcanf error");
00638
00639
00640 }
00641 result = (result << 8) + data;
00642 if ( computeCrc && computeCRC) {
00643 crc->compute(data) ;
00644 }
00645
00646 }
00647 return(result);
00648 }
00649
00650
00651
00652
00653 unsigned int CalibMicrorocParser::getDecimalData(FILE* inputFile, const int nBytes) {
00654 unsigned int result = 0;
00655 for (int byte = 0; byte < nBytes; ++byte) {
00656 int data = 0;
00657 if (fscanf(inputFile, "%2d", &data) != 1) {
00658 return(0);
00659 }
00660 result = (result << 8) + data;
00661 }
00662 return(result);
00663 }
00664
00665
00666
00667
00668 int CalibMicrorocParser::newHit(Event& event, const Detector &detector, int data, const int chNum, const int chipId, const int difId)
00669 {
00670
00671 if ( data != 0 )
00672 {
00673 const ChamberMap_t &chambers = detector.getChambers();
00674 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00675 try {
00676 Chamber& chamber = *(it->second);
00677 const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00678
00679
00680
00681
00682 try {
00683 const Channel& channel = chamber.getChannelById(chNum, chipId, difId);
00684 i16 analogValue = 0;
00685
00686 ChannelHit *hit = new ChannelHit(channel, data, analogValue);
00687 event.insertHit(chamber, hit);
00688 FILE_LOG(logDEBUG1) << " new Hit : ch " << chNum << ", chip " << chipId << ", dif " << difId
00689 << " Data=" << data << " , x:" << channel.getX() << ", y:" << channel.getY() << endl;
00690 return(1);
00691 }
00692 catch (MicroException &e) {
00693 FILE_LOG(logERROR) << "ch " << chNum << ", chip " << chipId << ", dif " << difId << " : " << e.getMessage() << endl;
00694 return(0);
00695 }
00696 }
00697 catch (...) {
00698 continue;
00699 }
00700
00701 }
00702
00703 return(0);
00704 }
00705 else
00706 {
00707 return 1;
00708 }
00709 }
00710
00711
00712
00713
00714
00715 string CalibMicrorocParser::binaire(unsigned int nombre)
00716 {
00717 string res = "";
00718 unsigned int val = nombre;
00719
00720 while (val != 0) {
00721
00722
00723 if (val & 1 == 1) {
00724 res = string("1") + res;
00725 }
00726 else {
00727
00728 res = string("0") + res;
00729 }
00730
00731 val = val >> 1;
00732 }
00733 return res;
00734 }
00735
00736
00737