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, set<unsigned int>*_difList) : MicrorocReader(aRun, NULL, lastEventId), nbChipPerLine(0), analogDataActivate(false), firstEvent(true), slowControlSet(false), difList(_difList)
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 }
00106
00107 int MicrorocXDaqReader::getScData()
00108 {
00109 return 0;
00110 }
00111
00112 int MicrorocXDaqReader::getNextEvent(Event& event)
00113 {
00114 Level(0);
00115 ui16 dataFormat = run.getDataFormat();
00116
00117 if (dataFormat == 0) {
00118
00119 bcIdDifAbsOrgMap.clear();
00120
00121 dataFormat = 1;
00122 run.setDataFormat(dataFormat);
00123
00124
00125
00126
00127
00128 return NEW_CONFIG;
00129 }
00130
00131
00132 bool eventFound = false;
00133 while (! eventFound)
00134 {
00135 LCEvent* lcioEvt(0) ;
00136 lcioEvt = lcReader->readNextEvent() ;
00137 if ( lcioEvt == NULL )
00138 {
00139 return LAST_FILE_EVENT;
00140 }
00141
00142
00143
00144
00145 if ( ! slowControlSet )
00146 {
00147
00148 if ( event.getRun().getId() == 0 )
00149 {
00150 run.setId(lcioEvt->getRunNumber());
00151 FILE_LOG(logINFO) << "Set run Id from lcioEvent information[" << run.getId() << "]" << endl;
00152 }
00153 globalTime = lcioEvt->getTimeStamp();
00154
00155 run.setDaqSoftware(0);
00156 run.setCccFirmware(0);
00157
00158 IlcConfDb *ilcconf = IlcConfDb::getInstance();
00159 if ( ilcconf != NULL )
00160 {
00161 MicrorocSCReader scReader(run);
00162 scReader.parseSCFromDb(*ilcconf);
00163 slowControlSet = true;
00164 }
00165
00166 }
00167
00168 if ( firstEvent )
00169 {
00170 FILE_LOG(logINFO) << "firstEvent skip" << endl;
00171 firstEvent = false;
00172
00173 }
00174 else
00175 {
00176
00177 std::vector<std::string >* vnames;
00178 try
00179 {
00180 vnames= (std::vector<std::string >*)lcioEvt->getCollectionNames();
00181
00182
00183
00184
00185
00186
00187
00188 for ( vector<std::string >::iterator it = vnames->begin(); it!= vnames->end(); it++)
00189 {
00190 if ((*it).compare("RU_XDAQ")==0)
00191 {
00192 EVENT::LCCollection* col= lcioEvt->getCollection(*it);
00193
00194
00195 for (int difRawDataIndex=0;difRawDataIndex<col->getNumberOfElements(); difRawDataIndex++)
00196 {
00197 IMPL::LCGenericObjectImpl* go= (IMPL::LCGenericObjectImpl*) col->getElementAt(difRawDataIndex);
00198 int rusize =go->getNInt();
00199
00200 if ( rusize == 56 ) continue;
00201
00202 int shift=-1;
00203 ui16 nbInt = go->getNInt();
00204
00205
00206
00207 unsigned int buf[go->getNInt()];
00208
00209 for (int k=0; k < (go->getNInt() );k++) { buf[k] = go->getIntVal(k); }
00210
00211
00212 unsigned char* bufChar = ( unsigned char* ) &(buf);
00213 ui16 rapport = (ui16) (sizeof(unsigned int ) / sizeof ( unsigned char) );
00214
00215 rawdataList.clear();
00216 for ( ui32 j = NBBYTETOSKIP ; j < go->getNInt()*rapport ; j++ )
00217 {
00218 rawdataList.push_back(*(bufChar+j));
00219 }
00220
00221 itRawdata = rawdataList.begin();
00222 unsigned int globalHeader = getData(1);
00223
00224
00225 if (globalHeader == 0xB1) {
00226
00227 getScData();
00228 exit(-1);
00229 }
00230 else if (globalHeader == 0xC0) {
00231
00232 getAnalogData(event);
00233 }
00234 else if (globalHeader == 0xB0) {
00235
00236 getAcqData(event,bufChar);
00237
00238 }
00239 else {
00240 FILE_LOG(logERROR) << " parsing file error. global header error : " << hex << globalHeader << dec << endl;
00241 }
00242 }
00243 eventFound = true ;
00244 event.setId(++lastEventId);
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254 }
00255 }
00256 catch (IOException& e)
00257 {
00258 std::cout << e.what() << std::endl ;
00259 std::string s=e.what();s+=__PRETTY_FUNCTION__;
00260 exit(-1);
00261 }
00262 catch (...)
00263 {
00264 std::cout<<" unknow io exeption "<<std::endl;
00265 exit(-1);
00266 }
00267 unsigned int globalHeader = 0XB0;
00268 }
00269
00270
00271 }
00272
00273 return EVENT_CORRECT;
00274
00275
00276
00277
00278
00279 return 0;
00280 }
00281
00282 int MicrorocXDaqReader::getAcqData(Event& event,unsigned char *bufChar)
00283 {
00284 ui32 nbanalogHit = 0;
00285 unsigned int nbHits = 0;
00286
00287 display(+1);
00288 FILE_LOG(logDEBUG) << " global header B0 found" << endl;
00289
00290
00291 this->setComputeCrcFlag(true);
00292
00293 FILE_LOG(logDEBUG) << "crc->compute(0xB0)[ "<< crc <<"]" << endl;
00294
00295 crc->compute(0xB0);
00296
00297
00298
00299
00300
00301 bcId_Abs = 0;
00302 bcId_Dif = 0;
00303
00304 difId = getData(1);
00305
00306 difTriggerCounter = getData(4);
00307 usbBusyTrigger = getData(4);
00308 globalTriggerCounter = getData(4);
00309
00310 ui64 bcIdHigh = getData(2);
00311 ui64 bcIdLow = getData(4);
00312 bcId_Abs = (bcIdHigh << 32) + bcIdLow;
00313
00314
00315 bcId_Dif = getData(3);
00316
00317 bcId_AbsOrg = getbcIdAbsOrg(difId, bcId_Abs);
00318 display();
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 i64 bcTime = ((bcId_Abs - bcId_AbsOrg) * bcPeriod) / 1000;
00332
00333 i64 readTime = globalTime + bcTime;
00334
00335 FILE_LOG(logDEBUG) << " START EVENT ------------- time " << readTime << " ms" << endl;
00336 FILE_LOG(logDEBUG) << " bc time " << bcTime << " ms" << endl;
00337 FILE_LOG(logDEBUG) << " DIF Id 0x" << hex << (int)difId << dec << endl;
00338 FILE_LOG(logDEBUG) << " DIF trigger counter " << difTriggerCounter << endl;
00339 FILE_LOG(logDEBUG) << " USB Busy trigger counter " << usbBusyTrigger << endl;
00340 FILE_LOG(logDEBUG) << " total trigger counter " << globalTriggerCounter << endl;
00341 FILE_LOG(logDEBUG) << " absolute bcId " << bcId_Abs - bcId_AbsOrg << endl;
00342
00343
00344 event.setDifTriggerCounter(difTriggerCounter);
00345 event.setGlobalTriggerCounter(globalTriggerCounter);
00346
00347
00348 Detector &detector = run.getDetector();
00349
00350 if ( analogDataActivate )
00351 {
00352 for ( map<ui16,map<ui16,ui16> >::iterator iterAnal = analogData.begin(); iterAnal!= analogData.end(); iterAnal++)
00353 {
00354 map<ui16,ui16>& byChip = iterAnal->second;
00355 ui16 chipId = iterAnal->first;
00356
00357 for ( std::map<ui16,ui16>::iterator iterChip = byChip.begin(); iterChip != byChip.end();iterChip++)
00358 {
00359 ui16 analogValue = iterChip->second;
00360 ui16 channelId = iterChip->first;
00361 if ( difList == NULL || difList->find(difId) != difList->end() )
00362 {
00363 nbHits += newHit(event, detector, 0, analogValue,channelId , chipId, difId);
00364 nbanalogHit++;
00365 }
00366 }
00367 }
00368 }
00369
00370
00371 bool difFound = false;
00372 { const ChamberMap_t &chambers = detector.getChambers();
00373 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00374 try {
00375 Chamber& chamber = *(it->second);
00376 const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00377 }
00378 catch (...) {
00379
00380 continue;
00381 }
00382
00383
00384 difFound = true;
00385 break;
00386 }
00387 if (!difFound) {
00388 FILE_LOG(logERROR) << " No dif found with Id 0x" << hex << difId << ", " << dec << difId << " " <<endl;
00389 }
00390 }
00391
00392 for (bool finish = false; !finish; ) {
00393 int delimiter = 0;
00394
00395 display();
00396 for (; delimiter != 0xA0 && delimiter != 0xB4; ) {
00397
00398 delimiter = getData(1);
00399 if (fileError == EOF) {
00400 delimiter = 0;
00401 break;
00402 }
00403 if (delimiter == 0xA3)
00404 FILE_LOG(logERROR) << " frame trailer A3 found -- should not" << endl;
00405 }
00406 if (delimiter == 0x00) {
00407 FILE_LOG(logERROR) << " end of file -- should not" << endl;
00408 break;
00409 }
00410 if (delimiter == 0xA0) {
00411 FILE_LOG(logDEBUG) << " frame trailer A0 found" << endl;
00412 this->setComputeCrcFlag(false);
00413 unsigned short CRC_check = getData(2);
00414 FILE_LOG(logDEBUG) << hex << " Compute CRC[" << crc->getCrc() << "]" << endl;
00415 FILE_LOG(logDEBUG) << hex << " Received CRC[" << CRC_check << "]" << endl;
00416 if ( CRC_check != crc->getCrc() )
00417 {
00418 FILE_LOG(logERROR) << " CRC Error for event id " << lastEventId << endl;
00419 FILE_LOG(logERROR) << hex << " Compute CRC[" << crc->getCrc() << "]" << endl;
00420 FILE_LOG(logERROR) << hex << " Received CRC[" << CRC_check << "]" << endl;
00421 event.setCrcIsCorrect(false);
00422
00423 }
00424 else
00425 {
00426 event.setCrcIsCorrect(true);
00427 }
00428
00429 display(-1);
00430 FILE_LOG(logDEBUG) << " global trailer A0 found" << endl;
00431 break;
00432 }
00433 display(+1);
00434 FILE_LOG(logDEBUG) << " frame header B4 found" << endl;
00435
00436
00437 int chipId = -1;
00438 ui16 orderInChipMemory = 0;
00439 for (; ; )
00440 {
00441 chipId = getData(1);
00442 FILE_LOG(logDEBUG) << "8 bits read[" << chipId << "] [0x" << hex << chipId << "]" << dec << endl;
00443
00444 if (fileError == EOF) {
00445 finish = true;
00446 FILE_LOG(logINFO) << " end of file found -- should not" << endl;
00447 break;
00448 }
00449 if (chipId == 0x00) {
00450
00451 continue;
00452 }
00453 else if (chipId == 0xA3) {
00454 display(-1);
00455 FILE_LOG(logDEBUG) << " frame trailer A3 found" << endl;
00456 break;
00457 }
00458 else if (chipId == 0xA0) {
00459 finish = true;
00460 display(-1);
00461
00462
00463 break;
00464 }
00465 else if (chipId == 0xC3) {
00466 display(-1);
00467 FILE_LOG(logERROR) << " frame trailer C3 found - error occured during data transfer" << endl;
00468 break;
00469 }
00470
00471
00472 bcId_Hit = grayToBinary(getData(3));
00473
00474 eventTime = readTime + (((bcId_Dif - bcId_Hit) * bcPeriod) / 1000);
00475
00476 orderInChipMemory++;
00477
00478 if (difFound)
00479 {
00480 FILE_LOG(logDEBUG) << " chip Id 0x" << hex << (int)chipId << dec << endl;
00481 FILE_LOG(logDEBUG) << " bcId Microroc " << hex << (int)bcId_Hit << endl;
00482 FILE_LOG(logDEBUG) << " event time " << eventTime << endl;
00483
00484 for (int chNum = 48; chNum >= 0; chNum -= 16)
00485 {
00486 unsigned int data4 = getData(4);
00487 for (int index = 0; index < 16; index++)
00488 {
00489
00490 int trigger = (data4 >> ((15 - index) * 2)) & 0x03;
00491
00492 if (trigger)
00493 {
00494 if ( difList == NULL || difList->find(difId) != difList->end() )
00495 {
00496 if ( analogDataActivate && orderInChipMemory == 1 )
00497 {
00498 i16 analogValue = analogData[chipId][index + chNum];
00499 nbHits += newHit(event, detector, trigger, analogValue, index + chNum, chipId, difId,orderInChipMemory);
00500 }
00501 else
00502 {
00503 nbHits += newHit(event, detector, trigger, 0, index + chNum, chipId, difId,orderInChipMemory);
00504 }
00505 }
00506
00507
00508 FILE_LOG(logDEBUG) << "digital chipId[" << chipId << "] channelNum["<< index+chNum <<"]= " << trigger << endl;
00509 }
00510 }
00511 }
00512 }
00513 display();
00514 }
00515
00516 }
00517 display(-1);
00518
00519
00520
00521
00522
00523 event.setTimeStamp(globalTime + ((bcId_AbsOrg /1000000 * bcPeriod) ) );
00524
00525
00526 FILE_LOG(logINFO) << " Add Hit for event EVENT " << lastEventId << ", " << nbHits << " hits \r" << flush;
00527
00528
00529
00530
00531
00532
00533
00534
00535 if (SkippedBcId) {
00536 FILE_LOG(logWARNING) << " skipped " << SkippedBcId << " data because of bad bcId" << endl;
00537 SkippedBcId = 0;
00538 }
00539
00540 return(nbHits);
00541
00542
00543 }
00544
00545
00546 int MicrorocXDaqReader::getAnalogData(Event& event)
00547 {
00548 return 0;
00549 }
00550
00551
00552 unsigned int MicrorocXDaqReader::getData(const unsigned int nBytes)
00553 {
00554 unsigned int result = 0;
00555
00556 fileError = 0;
00557 for (unsigned int byte = 0; byte < nBytes; ++byte)
00558 {
00559 unsigned int data = *itRawdata;
00560 itRawdata++;
00561 if ( computeCrc )
00562 {
00563 crc->compute(data) ;
00564 }
00565 result = (result << 8) + data;
00566 }
00567
00568 putData(result, nBytes);
00569
00570 return result;
00571 }
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587