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/MicrorocSCReader.hh"
00015 #include "parser/MicrorocLabviewReader.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 #include <iostream>
00032 #include <iomanip>
00033
00034 using namespace std;
00035
00036
00037 MicrorocLabviewReader::MicrorocLabviewReader(Run& aRun, FILE *aFile, ui32 lastEventId) : MicrorocReader(aRun, aFile, lastEventId), nbChipPerLine(0), analogDataActivate(false)
00038 {
00039 FILE_LOG(logDEBUG1) << "--MicrorocLabviewReader constructor()"<< endl;
00040 FILE_LOG(logDEBUG) << "cwMixrorocLabview ReaderDifReader Constructeur crc adr[" << crc << "]" << endl;
00041
00042 for ( ui16 line = 0 ; line < NBLINE ; line++)
00043 {
00044 analogDataActivatedLine[line] = false;
00045 }
00046
00047 reset();
00048
00049 }
00050
00051
00052 MicrorocLabviewReader::~MicrorocLabviewReader()
00053 {
00054 FILE_LOG(logDEBUG1)<< "----MicrorocLabviewReader destructor"<< endl;
00055 }
00056
00057
00058
00059 int MicrorocLabviewReader::getScData() {
00060 unsigned int data;
00061 display(+1);
00062 FILE_LOG(logDEBUG) << " Slow Control data header B1 found" << endl;
00063
00064
00065 int scHRNumber = 24;
00066
00067 difId=1;
00068 ui16 dataFormat = run.getDataFormat();
00069 if ( dataFormat == 11 || dataFormat == 12)
00070 {
00071
00072 nbChipPerLine = 12;
00073 bool findA1 = false;
00074 difId = getData(1);
00075 display(+1);
00076 FILE_LOG(logDEBUG) << "--- difId[" << difId << "] config ---" << endl;
00077
00078 Dif& dif = run.getDetector().getDifById(difId);
00079 FILE_LOG(logINFO) << "-------- dif Id[0x" << hex << dif.getId() << "]" << endl;
00080 dif.setDifFirmware(getData(2));
00081 display(+2);
00082 FILE_LOG(logINFO) << "-------- difFirmware[0x" << hex << dif.getDifFirmware() << "]" << endl;
00083
00084 dif.setDifConfig (getData(1));
00085 display(+1);
00086 FILE_LOG(logINFO) << "-------- difConfig[0x" << hex << dif.getDifConfig() << "]" << endl;
00087
00088 dif.setAsicType(getData(1));
00089 display(+1);
00090 FILE_LOG(logINFO) << "-------- asicType[0x" << dif.getAsicType() << "]" << endl;
00091 if ( dif.getAsicType() == 4 )
00092 {
00093 FILE_LOG(logINFO) << "------------------ [ Microroc ]" << endl;
00094 }
00095 dif.setConfigured(true);
00096
00097 if ( dataFormat == 12 )
00098 {
00099
00100 int foo = getData(1);
00101 if ( foo != 0x0 )
00102 {
00103 analogDataActivate = true;
00104 ui16 bitshift = 0x1;
00105 for ( ui16 line = 0 ; line < NBLINE; line++)
00106 {
00107 analogDataActivatedLine[line] = foo & bitshift;
00108 bitshift = bitshift * 2;
00109 FILE_LOG(logDEBUG) << "line [" << line << "] analog activation["<< analogDataActivatedLine[line] <<"]" << endl;
00110 }
00111 FILE_LOG(logINFO) << "-------- analog data enable. Mask to enable line [0x" <<hex << foo <<"]" << endl;
00112 }
00113 else
00114 {
00115 analogDataActivate = false;
00116 FILE_LOG(logINFO) << "-------- analogData disable" << endl;
00117 }
00118 }
00119
00120 dif.setAsu1Id(getData(1));
00121 FILE_LOG(logINFO) << "-------- ASU 1 Id[0x" << hex << dif.getAsu1Id() << "]" << endl;
00122 display(+1);
00123
00124
00125
00126
00127
00128
00129 ui16 boardColToCheck = 0;
00130 if ( dif.getAsu1Id() != 0)
00131 {
00132 if ( dif.getBoardByCol(boardColToCheck).getId() != dif.getAsu1Id() )
00133 {
00134 FILE_LOG(logINFO) << "Error data say ASU id = "<< dif.getAsu1Id() << " for ASU close to DIF ( left side ) while XML say ASU id = " << dif.getBoardByCol(boardColToCheck).getId() << endl;
00135 FILE_LOG(logINFO) << "Please consider modifying XML file" << endl;
00136 exit(-1);
00137 }
00138 boardColToCheck++;
00139 }
00140
00141
00142 dif.setAsu2Id (getData(1));
00143 FILE_LOG(logINFO) << "-------- ASU 2 Id[0x" <<hex << dif.getAsu2Id() << "]" << endl;
00144 display(+1);
00145 if ( dif.getAsu2Id() != 0)
00146 {
00147 if ( dif.getBoardByCol(boardColToCheck).getId() != dif.getAsu2Id() )
00148 {
00149 FILE_LOG(logINFO) << "Error data say ASU id = "<< dif.getAsu2Id() << " for ASU far away to DIF ( right side ) while XML say ASU id = " << dif.getBoardByCol(boardColToCheck).getId() << endl;
00150 FILE_LOG(logINFO) << "Please consider modifying XML file" << endl;
00151 exit(-1);
00152 }
00153 }
00154 scHRNumber = getData(1);
00155 FILE_LOG(logINFO) << "-------- Nombre de chip en Slow control[0x" << hex << scHRNumber << "]" << endl;
00156
00157
00158
00159 dif.setChipByPassed(true);
00160 for ( int nbChip = 0 ; (nbChip < scHRNumber) & findA1 == false ; nbChip++)
00161 {
00162 const unsigned char *buf = initGetBits(74, false);
00163 vector<int> sc;
00164 for ( ui16 i = 0; i < 74 ; i++)
00165 {
00166 int foo = getBits(8);
00167 sc.push_back(foo);
00168 }
00169 finishGetBits();
00170 MicrorocSCReader scReader(run,sc,difId);
00171 scReader.parseSC(difId);
00172
00173
00174
00175 }
00176 if ( ! findA1) {
00177 unsigned char *buf = initGetBits(1, false);
00178 if ( !buf)
00179 {
00180 FILE_LOG(logERROR) << " malloc error while getting SC buffer" << endl;
00181 exit ( -1);
00182 }
00183
00184 int foo = getBits(8);
00185 finishGetBits();
00186
00187 FILE_LOG(logDEBUG) << "Header[" <<hex << foo <<"]" << endl;
00188 if ( foo != 0xa1)
00189 {
00190 FILE_LOG(logERROR) << " Header A1 not found" << endl;
00191 exit (-1);
00192 }
00193 FILE_LOG(logDEBUG1) << "Header A1 found" << endl;
00194
00195 }
00196
00197 }
00198
00199 return(1);
00200 }
00201
00202
00203
00204 int MicrorocLabviewReader::getAnalogData(Event& event) {
00205 FILE_LOG(logDEBUG) << " global header C0 found" << endl;
00206 this->setComputeCrcFlag(true);
00207 crc->compute(0xC0);
00208
00209 ui16 nbchannel = 64;
00210 ui16 nbLine = 4;
00211 ui16 nbBitsPerLine = this->nbChipPerLine * nbchannel * 16;
00212 Dif& dif = run.getDetector().getDifById(difId);
00213 ui16 nbChipByBoard = dif.getBoards().begin()->second->getNbColumns();
00214 ui16 nbBoard = dif.getBoards().size();
00215 FILE_LOG(logDEBUG) << "nbBoard[" << nbBoard << "]" <<endl;
00216 FILE_LOG(logDEBUG) << "nbChipByBoard[" << nbChipByBoard << "]" <<endl;
00217
00218 this->analogData.clear();
00219 bool firstLine=true;
00220 for ( ui16 line = 0; line < nbLine ; line++)
00221 {
00222 if ( analogDataActivatedLine[line] == true )
00223 {
00224 FILE_LOG(logDEBUG) << " New Line[" << line << "]" << endl;
00225 if ( !firstLine )
00226 {
00227 int foo = getData(1);
00228 FILE_LOG(logDEBUG) << " foo C0["<< hex << foo << "]" << endl;
00229 if ( foo != 0xC0 )
00230 {
00231 FILE_LOG(logERROR) << "C0 header not found[0x" << hex << foo << "]" << endl;
00232 exit(-1);
00233 }
00234 }
00235 if( firstLine == true) { firstLine = false; }
00236
00237 for ( i32 col = nbBoard * nbChipByBoard -1 ; col >= 0; col--)
00238 {
00239 FILE_LOG(logDEBUG)<< hex << "col index[0x" << col << "]board col["<< col/nbChipByBoard << "] line["<< line <<"], col["<< col%nbChipByBoard <<"] "<<endl;
00240 Chip& chip = dif.getChipByRowCol(col/nbChipByBoard, line,col%nbChipByBoard);
00241 FILE_LOG(logDEBUG) << "------------chipId["<< chip.getId() << "] chip bypassed[" << chip.getBypassed() << "]" <<endl;
00242 if( chip.getBypassed() == false )
00243 {
00244 map<ui16,ui16> channelsValues;
00245 for ( ui16 channel = 0 ; channel < nbchannel ;channel++)
00246 {
00247 int foo = getData(2);
00248
00249 channelsValues[channel] = foo;
00250 FILE_LOG(logDEBUG) << "chipId["<< chip.getId() <<"] analogData line["<< line <<"], col number["<< col << "] , channel[" << channel << "= " << hex <<foo << dec << endl;
00251 }
00252 this->analogData[chip.getId()] = channelsValues;
00253 }
00254 }
00255
00256
00257 int foo = getData(1);
00258 if ( foo != 0xD0 )
00259 {
00260 FILE_LOG(logERROR) << "D0 header not found[0x" << hex << foo << "]" << endl;
00261 exit(-1);
00262 }
00263 }
00264 else
00265 {
00266 FILE_LOG(logDEBUG) << " line[ " << line +1<< "] is disable ( starting number at 1 )" << endl;
00267 }
00268 }
00269
00270
00271 this->setComputeCrcFlag(false);
00272 unsigned short CRC_check = getData(2);
00273 if ( CRC_check != crc->getCrc() )
00274 {
00275 FILE_LOG(logERROR) << endl << " CRC Error event Id: " << lastEventId << endl;
00276 FILE_LOG(logERROR) << hex << " Compute CRC[" << crc->getCrc() << "]" << endl;
00277 FILE_LOG(logERROR) << hex << " Received CRC[" << CRC_check << "]" << endl;
00278 event.setCrcIsCorrect(false);
00279 }
00280 else
00281 {
00282 event.setCrcIsCorrect(true);
00283 }
00284
00285 return 0;
00286 }
00287
00288
00289 int MicrorocLabviewReader::getAcqData(Event& event) {
00290 ui32 nbanalogHit = 0;
00291 unsigned int nbHits = 0;
00292
00293 display(+1);
00294 FILE_LOG(logDEBUG) << " global header B0 found" << endl;
00295
00296
00297 this->setComputeCrcFlag(true);
00298
00299 FILE_LOG(logDEBUG) << "crc->compute(0xB0)[ "<< crc <<"]" << endl;
00300
00301 crc->compute(0xB0);
00302
00303
00304
00305
00306
00307 bcId_Abs = 0;
00308 bcId_Dif = 0;
00309
00310 difId = getData(1);
00311
00312 difTriggerCounter = getData(4);
00313 usbBusyTrigger = getData(4);
00314 globalTriggerCounter = getData(4);
00315 ui64 bcIdHigh = getData(2);
00316 ui64 bcIdLow = getData(4);
00317 bcId_Abs = (bcIdHigh << 32) + bcIdLow;
00318 FILE_LOG(logDEBUG1) << "bcId_Abs[" <<bcId_Abs << endl;
00319 bcId_Dif = getData(3);
00320
00321 bcId_AbsOrg = getbcIdAbsOrg(difId, bcId_Abs);
00322
00323 ui16 lineInfo = getData(1);
00324 ui16 nbLine = (lineInfo & 0xF0 ) >> 4;
00325 ui16 usedLine = lineInfo & 0x0F;
00326
00327
00328 display();
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 FILE_LOG(logDEBUG) << " nb line " << dec <<nbLine << endl;
00343 FILE_LOG(logDEBUG) << " used line " << hex << usedLine << dec <<endl;
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
00362 nbHits += newHit(event, detector, 0, analogValue,channelId , chipId, difId);
00363 nbanalogHit++;
00364 }
00365 }
00366 }
00367
00368
00369 bool difFound = false;
00370 { const ChamberMap_t &chambers = detector.getChambers();
00371 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00372 try {
00373 Chamber& chamber = *(it->second);
00374 const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00375 }
00376 catch (...) {
00377
00378 continue;
00379 }
00380
00381
00382 difFound = true;
00383 break;
00384 }
00385 if (!difFound) {
00386 FILE_LOG(logERROR) << " No dif found with Id 0x" << hex << difId << dec << endl;
00387 }
00388 }
00389
00390 for (bool finish = false; !finish; ) {
00391 int delimiter = 0;
00392
00393 display();
00394 for (; delimiter != 0xA0 && delimiter != 0xB4; ) {
00395
00396 delimiter = getData(1);
00397 if (fileError == EOF) {
00398 delimiter = 0;
00399 break;
00400 }
00401 if (delimiter == 0xA3)
00402 FILE_LOG(logERROR) << " frame trailer A3 found -- should not" << endl;
00403 }
00404 if (delimiter == 0x00) {
00405 FILE_LOG(logERROR) << " end of file -- should not" << endl;
00406 break;
00407 }
00408 if (delimiter == 0xA0) {
00409 FILE_LOG(logDEBUG) << " frame trailer A0 found" << endl;
00410 this->setComputeCrcFlag(false);
00411 unsigned short CRC_check = getData(2);
00412 FILE_LOG(logDEBUG) << hex << " Compute CRC[" << crc->getCrc() << "]" << endl;
00413 FILE_LOG(logDEBUG) << hex << " Received CRC[" << CRC_check << "]" << endl;
00414 if ( CRC_check != crc->getCrc() )
00415 {
00416 FILE_LOG(logERROR) << endl << " CRC Error for event id " << lastEventId << endl;
00417 event.setCrcIsCorrect(false);
00418
00419 }
00420 else
00421 {
00422 event.setCrcIsCorrect(true);
00423 }
00424
00425
00426 display(-1);
00427 FILE_LOG(logDEBUG) << " global trailer A0 found" << endl;
00428 break;
00429 }
00430 display(+1);
00431 FILE_LOG(logDEBUG) << " frame header B4 found" << endl;
00432
00433
00434 int chipId = -1;
00435 ui16 orderInChipMemory = 0;
00436 for (; ; )
00437 {
00438 chipId = getData(1);
00439 FILE_LOG(logDEBUG) << "8 bits read[" << chipId << "] [0x" << hex << chipId << "]" << dec << endl;
00440
00441 if (fileError == EOF) {
00442 finish = true;
00443 FILE_LOG(logINFO) << " end of file found -- should not" << endl;
00444 break;
00445 }
00446 if (chipId == 0x00) {
00447
00448 continue;
00449 }
00450 else if (chipId == 0xA3) {
00451 display(-1);
00452 FILE_LOG(logDEBUG) << " frame trailer A3 found" << endl;
00453 break;
00454 }
00455 else if (chipId == 0xA0) {
00456 finish = true;
00457 display(-1);
00458 FILE_LOG(logERROR) << " global trailer A0 found -- should not" << endl;
00459 int CRC_check = getData(1);
00460 break;
00461 }
00462 else if (chipId == 0xC3) {
00463 display(-1);
00464 FILE_LOG(logERROR) << " frame trailer C3 found - error occured during data transfer" << endl;
00465 break;
00466 }
00467
00468
00469 bcId_Hit = grayToBinary(getData(3));
00470
00471 eventTime = readTime + (((bcId_Dif - bcId_Hit) * bcPeriod) / 1000);
00472
00473 orderInChipMemory++;
00474
00475 if (difFound)
00476 {
00477 FILE_LOG(logDEBUG) << " chip Id 0x" << hex << (int)chipId << dec << endl;
00478 FILE_LOG(logDEBUG) << " bcId Microroc " << hex << (int)bcId_Hit << endl;
00479 FILE_LOG(logDEBUG) << " event time " << eventTime << endl;
00480
00481 for (int chNum = 48; chNum >= 0; chNum -= 16)
00482 {
00483 unsigned int data4 = getData(4);
00484 for (int index = 0; index < 16; index++)
00485 {
00486
00487 int trigger = (data4 >> ((15 - index) * 2)) & 0x03;
00488
00489 if (trigger)
00490 {
00491
00492 if ( analogDataActivate && orderInChipMemory == 1 )
00493 {
00494
00495 i16 analogValue = analogData[chipId][index + chNum];
00496 nbHits += newHit(event, detector, trigger, analogValue, index + chNum, chipId, difId,orderInChipMemory);
00497 }
00498 else
00499 {
00500 nbHits += newHit(event, detector, trigger, 0, index + chNum, chipId, difId,orderInChipMemory);
00501 }
00502
00503
00504 FILE_LOG(logDEBUG) << "digital chipId[" << chipId << "] channelNum["<< index+chNum <<"]= " << trigger << endl;
00505 }
00506 }
00507 }
00508 }
00509 display();
00510 }
00511
00512 }
00513 display(-1);
00514
00515
00516
00517
00518
00519 event.setTimeStamp(globalTime + ((bcId_AbsOrg /1000000 * bcPeriod) ) );
00520 event.setId(++lastEventId);
00521 FILE_LOG(logDEBUG) << " STORE EVENT " << lastEventId << ", " << nbHits << " hits" << endl;
00522
00523
00524
00525
00526
00527
00528
00529 if (SkippedBcId) {
00530 FILE_LOG(logWARNING) << " skipped " << SkippedBcId << " data because of bad bcId" << endl;
00531 SkippedBcId = 0;
00532 }
00533
00534 return(nbHits);
00535 }
00536
00537
00538
00539
00540 int MicrorocLabviewReader::getNextEvent(Event& event) {
00541
00542 Level(0);
00543
00544 ui16 dataFormat = run.getDataFormat();
00545
00546 if (dataFormat == 0) {
00547
00548 bcIdDifAbsOrgMap.clear();
00549
00550 dataFormat = getData(1);
00551 FILE_LOG(logINFO) << " labview data format[" << hex << dataFormat << "] "<< endl;
00552 if ( dataFormat == 0x10 ) { dataFormat = 0xa; }
00553 run.setDataFormat(dataFormat);
00554
00555
00556 if (fileError == EOF) {
00557 FILE_LOG(logERROR) << "parsing file[" << inputFile << "] - error " << errno << ":" << strerror(errno) << endl;
00558 reset();
00559 return(LAST_FILE_EVENT);
00560 }
00561 display(+1);
00562 FILE_LOG(logINFO) << " Microroc LABVIEW DATA FORMAT : " << dataFormat << endl;
00563
00564 if (! ( dataFormat == 11 || dataFormat == 12) ){
00565
00566 FILE_LOG(logERROR) << " Microroc LABVIEW data format " << dataFormat << " is not implemented" << endl;
00567 reset();
00568 return(LAST_FILE_EVENT);
00569 }
00570
00571 if ( dataFormat == 11 || dataFormat == 12 )
00572 {
00573 run.setDaqSoftware( getData(1));
00574 display(+1);
00575 FILE_LOG(logINFO) << " DAQ software version : " << run.getDaqSoftware() << endl;
00576
00577 run.setCccFirmware(getData(1));
00578 display(+1);
00579 FILE_LOG(logINFO) << " ccc firware : " << run.getCccFirmware() << endl;
00580 }
00581
00582 globalTime = getData(4) ;
00583 FILE_LOG(logDEBUG) << "globalTime in second:[" << hex << globalTime << "]" << endl;
00584 globalTime *= 1000;
00585 FILE_LOG(logDEBUG1) << "globalTime in millisecond:[" << globalTime << "]" << endl;
00586 FILE_LOG(logDEBUG1) << " Global timestamp(hex) [" << hex << globalTime << "]" << endl;
00587
00588
00589
00590 if (fileError == EOF) {
00591 FILE_LOG(logDEBUG) << " END OF DATA FILE" << endl;
00592 reset();
00593 return(LAST_FILE_EVENT);
00594 }
00595
00596 display(-1);
00597 return NEW_CONFIG;
00598 }
00599
00600
00601
00602
00603 bool eventFound = false;
00604 while (! eventFound)
00605 {
00606
00607 unsigned int globalHeader = getData(1);
00608 if (fileError == EOF) {
00609 FILE_LOG(logDEBUG) << " END OF DATA FILE" << endl;
00610 reset();
00611 return(LAST_FILE_EVENT);
00612 }
00613 FILE_LOG(logDEBUG) << "globalHeader:["<< hex << globalHeader << "]" << endl;
00614 if (globalHeader == 0xB1) {
00615
00616 getScData();
00617 }
00618 else if (globalHeader == 0xC0) {
00619
00620 getAnalogData(event);
00621 }
00622 else if (globalHeader == 0xB0) {
00623
00624 getAcqData(event);
00625 eventFound = true;
00626 }
00627 else {
00628 display();
00629 FILE_LOG(logERROR) << " parsing file error. global header error : " << hex << globalHeader << dec << endl;
00630 }
00631 }
00632
00633
00634 return EVENT_CORRECT;
00635 }
00636