00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <errno.h>
00013
00014 #include "parser/MicrorocXDaqReader.hh"
00015 #include "parser/MicrorocSCReader.hh"
00016 #include "parser/AcquisitionParser.hh"
00017
00018 #include "event/Event.hh"
00019
00020 #include "geometry/Chamber.hh"
00021 #include "geometry/Board.hh"
00022 #include "geometry/Dif.hh"
00023 #include "geometry/MicrorocChip.hh"
00024 #include "geometry/Detector.hh"
00025
00026 #include "tools/Toolbox.hh"
00027 #include "tools/SteerDesc.hh"
00028 #include "tools/Log.hh"
00029 #include "tools/MicroException.hh"
00030
00031
00032 #include "lcio.h"
00033 #include "IO/LCReader.h"
00034 #include "IMPL/LCTOOLS.h"
00035 #include "IMPL/LCGenericObjectImpl.h"
00036 #include "EVENT/LCCollection.h"
00037 #include "EVENT/LCRunHeader.h"
00038
00039 #include "geometry/MicrorocChannel.hh"
00040
00041 #include "ilcConfDb/IlcConfDb.hh"
00042
00043
00044 #include <iostream>
00045 #include <iomanip>
00046
00047
00048 #define NBBYTETOSKIP 94
00049
00050 using namespace std;
00051 using namespace lcio ;
00052
00053
00054
00055 MicrorocXDaqReader::MicrorocXDaqReader(Run& aRun, string aFileFullPath, ui32 lastEventId,bool _forceAnalog, set<unsigned int>*_difList) : MicrorocReader(aRun, NULL, lastEventId), nbChipPerLine(0), analogDataActivate(false), firstEvent(true), slowControlSet(false), difList(_difList) , hexTemperatureAsu1(0), hexTemperatureAsu2(0), hexTemperatureDif(0) , forceAnalog(_forceAnalog) , debugEventId(0)
00056
00057 {
00058 FILE_LOG(logDEBUG1) << "--MicrorocXDaqReader constructor()"<< endl;
00059
00060 lcReader = LCFactory::getInstance()->createLCReader() ;
00061 try {
00062 lcReader->open(aFileFullPath);
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 }
00075 catch( IOException& e) {
00076 cout << e.what() << endl ;
00077 exit(-1) ;
00078 }
00079
00080 if ( lcReader == NULL ) { exit(-1); }
00081
00082 for ( ui16 line = 0 ; line < NBLINE ; line++)
00083 {
00084 analogDataActivatedLine[line] = false;
00085 }
00086
00087 localReset();
00088
00089 }
00090
00091
00092 MicrorocXDaqReader::~MicrorocXDaqReader()
00093 {
00094 lcReader->close();
00095 delete lcReader;
00096 FILE_LOG(logDEBUG1)<< "----MicrorocXDaqReader destructor"<< endl;
00097 }
00098
00099
00100 void MicrorocXDaqReader::localReset()
00101 {
00102 reset();
00103 slowControlSet = false;
00104 rawdataList.clear();
00105 hexTemperatureAsu1 = 0;
00106 hexTemperatureAsu2 = 0;
00107 hexTemperatureDif = 0;
00108 }
00109
00110 int MicrorocXDaqReader::getScData()
00111 {
00112 return 0;
00113 }
00114
00115 int MicrorocXDaqReader::getNextEvent(Event& event)
00116 {
00117 Level(0);
00118 ui16 dataFormat = run.getDataFormat();
00119
00120 if (dataFormat == 0) {
00121
00122 bcIdDifAbsOrgMap.clear();
00123
00124 dataFormat = 1;
00125 run.setDataFormat(dataFormat);
00126
00127
00128
00129
00130
00131 return NEW_CONFIG;
00132 }
00133
00134
00135 bool eventFound = false;
00136 while (! eventFound)
00137 {
00138 LCEvent* lcioEvt(0) ;
00139 lcioEvt = lcReader->readNextEvent() ;
00140 if ( lcioEvt == NULL )
00141 {
00142 return LAST_FILE_EVENT;
00143 }
00144
00145
00146
00147
00148 if ( ! slowControlSet )
00149 {
00150
00151 if ( event.getRun().getId() == 0 )
00152 {
00153 run.setId(lcioEvt->getRunNumber());
00154 FILE_LOG(logINFO) << "Set run Id from lcioEvent information[" << run.getId() << "]" << endl;
00155 }
00156
00157 globalTime = (i64) ( lcioEvt->getTimeStamp()*1000 );
00158
00159 run.setDaqSoftware(0);
00160 run.setCccFirmware(0);
00161
00162 IlcConfDb *ilcconf = IlcConfDb::getInstance();
00163 if ( ilcconf != NULL )
00164 {
00165 MicrorocSCReader scReader(run);
00166 scReader.parseSCFromDb(*ilcconf);
00167 slowControlSet = true;
00168 }
00169
00170 }
00171 if ( firstEvent )
00172 {
00173 FILE_LOG(logINFO) << "firstEvent skip" << endl;
00174 firstEvent = false;
00175
00176 }
00177
00178
00179
00180
00181
00182
00183
00184 else
00185 {
00186
00187 std::vector<std::string >* vnames;
00188 try
00189 {
00190 vnames= (std::vector<std::string >*)lcioEvt->getCollectionNames();
00191
00192
00193
00194
00195
00196
00197
00198 for ( vector<std::string >::iterator it = vnames->begin(); it!= vnames->end(); it++)
00199 {
00200 if ((*it).compare("RU_XDAQ")==0)
00201 {
00202 EVENT::LCCollection* col= lcioEvt->getCollection(*it);
00203
00204
00205 for (int difRawDataIndex=0;difRawDataIndex<col->getNumberOfElements(); difRawDataIndex++)
00206 {
00207 IMPL::LCGenericObjectImpl* go= (IMPL::LCGenericObjectImpl*) col->getElementAt(difRawDataIndex);
00208 int rusize =go->getNInt();
00209
00210 if ( rusize == 56 ) continue;
00211
00212 int shift=-1;
00213 ui16 nbInt = go->getNInt();
00214
00215
00216
00217 unsigned int buf[go->getNInt()];
00218
00219 for (int k=0; k < (go->getNInt() );k++) { buf[k] = go->getIntVal(k); }
00220
00221
00222 unsigned char* bufChar = ( unsigned char* ) &(buf);
00223 ui16 rapport = (ui16) (sizeof(unsigned int ) / sizeof ( unsigned char) );
00224
00225 rawdataList.clear();
00226 for ( ui32 j = NBBYTETOSKIP ; j < go->getNInt()*rapport ; j++ )
00227 {
00228 rawdataList.push_back(*(bufChar+j));
00229 }
00230
00231 itRawdata = rawdataList.begin();
00232 unsigned int globalHeader = getData(1);
00233
00234 if (globalHeader == 0xB1) {
00235
00236 getScData();
00237 exit(-1);
00238 }
00239 else if (globalHeader == 0xC0) {
00240
00241 getAnalogData(event);
00242 }
00243 else if (globalHeader == 0xB0) {
00244
00245 this->analogData.clear();
00246 getAcqData(event,bufChar);
00247
00248 }
00249 else if (globalHeader == 0xBB) {
00250
00251 FILE_LOG(logDEBUG) << " 0xBB found " << hex << globalHeader << dec << endl;
00252 getAcqData(event,bufChar,true);
00253
00254 }
00255 else {
00256 FILE_LOG(logERROR) << " parsing file error. global header error : " << hex << globalHeader << dec << endl;
00257 event.setValidFlag(UNKNOW_NOT_VALID);
00258 }
00259 }
00260 eventFound = true ;
00261 event.setId(++lastEventId);
00262 }
00263 }
00264 }
00265 catch (IOException& e)
00266 {
00267 std::cout << e.what() << std::endl ;
00268 std::string s=e.what();s+=__PRETTY_FUNCTION__;
00269 exit(-1);
00270 }
00271 catch (...)
00272 {
00273 std::cout<<" unknow io exeption "<<std::endl;
00274 exit(-1);
00275 }
00276 unsigned int globalHeader = 0XB0;
00277 }
00278
00279
00280 }
00281
00282 return EVENT_CORRECT;
00283
00284
00285
00286
00287
00288 return 0;
00289 }
00290
00291 int MicrorocXDaqReader::getAcqData(Event& event,unsigned char *bufChar, bool temperatureFill)
00292 {
00293
00294 ui32 nbanalogHit = 0;
00295 unsigned int nbHits = 0;
00296
00297 display(+1);
00298
00299
00300 this->setComputeCrcFlag(true);
00301
00302
00303 if ( temperatureFill )
00304 {
00305 FILE_LOG(logDEBUG) << " global header BB found" << endl;
00306 FILE_LOG(logDEBUG) << "crc->compute(0xBB)[ "<< crc <<"]" << endl;
00307 crc->compute(0xBB);
00308 }
00309 else
00310 {
00311 FILE_LOG(logDEBUG) << " global header B0 found" << endl;
00312 FILE_LOG(logDEBUG) << "crc->compute(0xB0)[ "<< crc <<"]" << endl;
00313 crc->compute(0xB0);
00314 }
00315
00316
00317
00318
00319
00320 bcId_Abs = 0;
00321 bcId_Dif = 0;
00322
00323 difId = getData(1);
00324
00325 difTriggerCounter = getData(4);
00326 informationCounter = getData(4);
00327 globalTriggerCounter = getData(4);
00328
00329 ui64 bcIdHigh = getData(2);
00330 ui64 bcIdLow = getData(4);
00331 bcId_Abs = (bcIdHigh << 32) + bcIdLow;
00332
00333
00334 bcId_Dif = getData(3);
00335
00336 bcId_AbsOrg = getbcIdAbsOrg(difId, bcId_Abs);
00337 display();
00338
00339 ui16 lineInfo = getData(1);
00340 FILE_LOG(logDEBUG) << " lineInfo[0x" <<hex << lineInfo << "]" << endl;
00341 ui16 nbLine = (lineInfo & 0xF0 ) >> 4;
00342 FILE_LOG(logDEBUG) << " nbline[0x" <<hex << nbLine << "]" << endl;
00343 ui16 usedLine = lineInfo & 0x0F;
00344 FILE_LOG(logDEBUG) << " usedLine[0x" <<hex << usedLine << "]" << endl;
00345 display();
00346
00347
00348 if ( temperatureFill )
00349 {
00350 hexTemperatureAsu1 = getData(4);
00351 hexTemperatureAsu2 = getData(4);
00352 hexTemperatureDif = getData(1) & 0xFF;
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 i16 hex16tempAsu1 = hexTemperatureAsu1 & 0xFFFF;
00366 float tempAsu1 = (float) ((hex16tempAsu1 / 16 ) / 8 );
00367
00368 i16 hex16tempAsu2 = hexTemperatureAsu2 & 0xFFFF;
00369 float tempAsu2 = (float) ((hex16tempAsu2 / 16 ) / 8 );
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 float Vlsb = 20.0;
00394 float V_adc = hexTemperatureDif * Vlsb;
00395 float V_lm20 = ( (6450. - V_adc) / 3.3 ) ;
00396 float tempDif = (V_lm20 - 1860.5) / (-11.77);
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 event.setTemperature(difId, tempAsu1, tempAsu2 , tempDif);
00410
00411 }
00412
00413
00414 i64 bcTime = ((bcId_Abs - bcId_AbsOrg) * bcPeriod) / 1000;
00415
00416 i64 readTime = globalTime + bcTime;
00417
00418 FILE_LOG(logDEBUG) << " START EVENT ------------- time " << readTime << " ms" << endl;
00419 FILE_LOG(logDEBUG) << " bc time " << bcTime << " ms" << endl;
00420 FILE_LOG(logDEBUG) << " DIF Id 0x" << hex << (int)difId << dec << endl;
00421 FILE_LOG(logDEBUG) << " DIF trigger counter " << difTriggerCounter << endl;
00422 FILE_LOG(logDEBUG) << " Information counter " << informationCounter << endl;
00423 FILE_LOG(logDEBUG) << " total trigger counter " << globalTriggerCounter << endl;
00424 FILE_LOG(logDEBUG) << " absolute bcId " << bcId_Abs - bcId_AbsOrg << endl;
00425 FILE_LOG(logDEBUG) << " nb line " << dec <<nbLine << endl;
00426 FILE_LOG(logDEBUG) << " used line " << hex << usedLine << dec <<endl;
00427 event.setDifTriggerCounter(difTriggerCounter);
00428 event.setGlobalTriggerCounter(globalTriggerCounter);
00429
00430
00431 Detector &detector = run.getDetector();
00432
00433
00434
00435 bool difFound = false;
00436 { const ChamberMap_t &chambers = detector.getChambers();
00437 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00438 try {
00439 Chamber& chamber = *(it->second);
00440 const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00441 }
00442 catch (...) {
00443
00444 continue;
00445 }
00446
00447
00448 difFound = true;
00449 break;
00450 }
00451 if (detector.getMonitoringId() == difId)
00452 {
00453 difFound = true;
00454 }
00455 if (!difFound) {
00456 FILE_LOG(logERROR) << " No dif found with Id 0x" << hex << difId << ", " << dec << difId << " " <<endl;
00457 event.setValidFlag(UNKNOW_NOT_VALID);
00458
00459 }
00460 }
00461
00462 for (bool finish = false; !finish; ) {
00463 int delimiter = 0;
00464
00465 display();
00466 for (; delimiter != 0xA0 && delimiter != 0xB4 && delimiter != 0xC4; ) {
00467
00468 delimiter = getData(1);
00469 if (fileError == EOF) {
00470 delimiter = 0;
00471 break;
00472 }
00473 if (delimiter == 0xA3)
00474 {
00475 FILE_LOG(logERROR) << " frame trailer A3 found -- should not" << endl;
00476 event.setValidFlag(UNKNOW_NOT_VALID);
00477 }
00478 }
00479 if (delimiter == 0x00) {
00480 FILE_LOG(logERROR) << " end of file -- should not" << endl;
00481 event.setValidFlag(UNKNOW_NOT_VALID);
00482 break;
00483 }
00484 if (delimiter == 0xA0)
00485 {
00486 FILE_LOG(logDEBUG) << " frame trailer A0 found" << endl;
00487 this->setComputeCrcFlag(false);
00488 unsigned short CRC_check = getData(2);
00489 FILE_LOG(logDEBUG) << hex << " Compute CRC[" << crc->getCrc() << "]" << endl;
00490 FILE_LOG(logDEBUG) << hex << " Received CRC[" << CRC_check << "]" << endl;
00491 if ( CRC_check != crc->getCrc() )
00492 {
00493 FILE_LOG(logERROR) << " CRC Error for event id " << lastEventId
00494 << " Compute CRC[" << crc->getCrc()
00495 << "] Received CRC[" << CRC_check << "]" << endl ;
00496 event.setCrcIsCorrect(false);
00497 event.setValidFlag(CRC_ERROR);
00498 }
00499 display(-1);
00500 FILE_LOG(logDEBUG) << " global trailer A0 found" << endl;
00501 break;
00502 }
00503 if (delimiter == 0xC4)
00504 {
00505
00506 Dif& dif = run.getDetector().getDifById(difId);
00507 analogDataActivate = true;
00508 ui16 lineNum = 1;
00509 ui16 nbChipByBoard = dif.getBoards().begin()->second->getNbColumns();
00510 ui16 nbBoard = dif.getBoards().size();
00511 FILE_LOG(logDEBUG) << "nbBoard[" << nbBoard << "]" <<endl;
00512 FILE_LOG(logDEBUG) << "nbChipByBoard[" << nbChipByBoard << "]" <<endl;
00513
00514 for ( ui16 iLine = 0 ; iLine < nbLine ; iLine++)
00515 {
00516 if ( usedLine & lineNum )
00517 {
00518 ui16 nbChips = getData(1);
00519 FILE_LOG(logDEBUG) << endl << " Line " << lineNum << " nb Chip[" << dec << nbChips << "]" << endl;
00520
00521 for ( i32 col = nbBoard * nbChipByBoard -1 ; col >= 0; col--)
00522 {
00523 ui16 board = col/nbChipByBoard;
00524 FILE_LOG(logDEBUG)<< dec << "board [" << board << "] line[" << lineNum-1 << "] col[" << col << "]" << endl;
00525 Chip& chip = dif.getChipByRowCol(board, lineNum-1,col % 6);
00526 if( chip.getBypassed() == false )
00527 {
00528
00529 FILE_LOG(logDEBUG)<< dec << "chip Id[" << chip.getId() << "]" << endl;
00530 map<ui16,ui16> channelsValues;
00531 for ( ui16 iChannel = 0 ; iChannel < 64 ; iChannel++)
00532 {
00533 ui16 foo = getData(2);
00534 channelsValues[iChannel] = foo;
00535 FILE_LOG(logDEBUG) << "difId["<< dif.getId() << "] ,chipId["<< chip.getId() <<"] analogData line["<< line <<
00536 "], col number["<< col << "] , channel[" << iChannel << "= " << hex <<foo << "]" << dec << endl;
00537 }
00538 this->analogData[chip.getId()] = channelsValues;
00539 }
00540 }
00541 }
00542 lineNum++;
00543 }
00544
00545 }
00546 if ( forceAnalog )
00547 {
00548 for ( map<ui16,map<ui16,ui16> >::iterator iterAnal = analogData.begin(); iterAnal!= analogData.end(); iterAnal++)
00549 {
00550 map<ui16,ui16>& byChip = iterAnal->second;
00551 ui16 chipId = iterAnal->first;
00552
00553 for ( std::map<ui16,ui16>::iterator iterChip = byChip.begin(); iterChip != byChip.end();iterChip++)
00554 {
00555 ui16 analogValue = iterChip->second;
00556 ui16 channelId = iterChip->first;
00557 if ( difList == NULL || difList->find(difId) != difList->end() )
00558 {
00559 nbHits += newHit(event, detector, 0, analogValue,channelId , chipId, difId);
00560 nbanalogHit++;
00561 }
00562 }
00563 }
00564 }
00565
00566 if (delimiter == 0xB4)
00567 {
00568 display(+1);
00569 FILE_LOG(logDEBUG) << " frame header B4 found" << endl;
00570
00571
00572
00573 int chipId = -1;
00574 ui16 orderInChipMemory = 0;
00575 for (; ; )
00576 {
00577 chipId = getData(1);
00578
00579
00580 if (fileError == EOF) {
00581 finish = true;
00582 FILE_LOG(logINFO) << " end of file found -- should not" << endl;
00583 break;
00584 }
00585 if (chipId == 0x00) {
00586
00587 continue;
00588 }
00589 else if (chipId == 0xA3) {
00590 display(-1);
00591 FILE_LOG(logDEBUG) << " frame trailer A3 found" << endl;
00592 break;
00593 }
00594 else if (chipId == 0xA0) {
00595 finish = true;
00596 display(-1);
00597 break;
00598 }
00599 else if (chipId == 0xC3) {
00600 display(-1);
00601 FILE_LOG(logERROR) << " frame trailer C3 found - error occured during data transfer" << endl;
00602 event.setValidFlag(UNKNOW_NOT_VALID);
00603 break;
00604 }
00605
00606
00607 bcId_Hit = grayToBinary(getData(3));
00608 FILE_LOG(logDEBUG) << dec << "bcId_Hit[" << bcId_Hit << "]" << endl;
00609
00610 eventTime = readTime + (((bcId_Dif - bcId_Hit) * bcPeriod) / 1000);
00611
00612 orderInChipMemory++;
00613
00614 if (difFound)
00615 {
00616 for (int chNum = 48; chNum >= 0; chNum -= 16)
00617 {
00618 unsigned int data4 = getData(4);
00619 for (int index = 0; index < 16; index++)
00620 {
00621
00622 int trigger = (data4 >> ((15 - index) * 2)) & 0x03;
00623
00624 if (trigger)
00625 {
00626 if ( difList == NULL || difList->find(difId) != difList->end() )
00627 {
00628 if ( analogDataActivate && orderInChipMemory == 1 )
00629 {
00630 i16 analogValue = analogData[chipId][index + chNum];
00631 nbHits += newHit(event, detector, trigger, analogValue, index + chNum, chipId, difId,orderInChipMemory);
00632 }
00633 else
00634 {
00635 nbHits += newHit(event, detector, trigger, 0, index + chNum, chipId, difId,orderInChipMemory);
00636 }
00637 }
00638
00639
00640
00641 }
00642 }
00643 }
00644 }
00645 display();
00646 }
00647 }
00648 }
00649 display(-1);
00650
00651
00652
00653 event.setTimeStamp(globalTime + ((bcId_AbsOrg /1000000 * bcPeriod) ) );
00654 FILE_LOG(logDEBUG) << "Dif[" << dec << difId << "] Number of created hit for event EVENT " << lastEventId << ", " << nbHits << " total hits, within " << nbanalogHit << " analogic hits" << endl;
00655
00656
00657
00658 if (SkippedBcId) {
00659 FILE_LOG(logWARNING) << " skipped " << SkippedBcId << " data because of bad bcId" << endl;
00660 SkippedBcId = 0;
00661 }
00662
00663 return(nbHits);
00664
00665
00666 }
00667
00668
00669 int MicrorocXDaqReader::getAnalogData(Event& event)
00670 {
00671 return 0;
00672 }
00673
00674
00675 unsigned int MicrorocXDaqReader::getData(const unsigned int nBytes)
00676 {
00677 unsigned int result = 0;
00678
00679 fileError = 0;
00680 for (unsigned int byte = 0; byte < nBytes; ++byte)
00681 {
00682 unsigned int data = *itRawdata;
00683 itRawdata++;
00684 if ( computeCrc )
00685 {
00686
00687 {
00688 crc->compute(data) ;
00689 }
00690 }
00691 result = (result << 8) + data;
00692 }
00693
00694 putData(result, nBytes);
00695
00696 return result;
00697 }
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713