00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <errno.h>
00026
00027 #include "parser/Hardroc2LabviewReader.hh"
00028 #include "parser/AcquisitionParser.hh"
00029
00030 #include "event/Event.hh"
00031
00032 #include "geometry/Chamber.hh"
00033 #include "geometry/Dif.hh"
00034 #include "geometry/Hardroc2Chip.hh"
00035 #include "geometry/Detector.hh"
00036
00037 #include "tools/Toolbox.hh"
00038 #include "tools/SteerDesc.hh"
00039 #include "tools/Log.hh"
00040 #include "tools/MicroException.hh"
00041
00042 #include <iostream>
00043 #include <iomanip>
00044
00045 using namespace std;
00046
00047
00048 Hardroc2LabviewReader::Hardroc2LabviewReader(Run& aRun, FILE *aFile, ui32 lastEventId) : Hardroc2Reader(aRun, aFile, lastEventId)
00049 {
00050 FILE_LOG(logDEBUG1) << "--Hardroc2LabviewReader constructor()"<< endl;
00051 reset();
00052 }
00053
00054
00055 Hardroc2LabviewReader::~Hardroc2LabviewReader()
00056 {
00057 FILE_LOG(logDEBUG1)<< "----Hardroc2LabviewReader destructor"<< endl;
00058 }
00059
00060
00061
00062
00063
00064
00065
00066 int Hardroc2LabviewReader::getScData() {
00067 unsigned int data;
00068 display(+1);
00069 FILE_LOG(logDEBUG) << " Slow Control data header B1 found" << endl;
00070
00071
00072 int scHRNumber = 24;
00073
00074 difId=1;
00075 ui16 dataFormat = run.getDataFormat();
00076 if ( dataFormat == 1 || dataFormat == 10)
00077 {
00078 bool findA1 = false;
00079 difId = getData(1);
00080 display(+1);
00081 FILE_LOG(logDEBUG) << "--- difId[" << difId << "] config ---" << endl;
00082
00083 if ( dataFormat == 10)
00084 {
00085 Dif& dif = run.getDetector().getDifById(difId);
00086 dif.setDifFirmware(getData(2));
00087 display(+2);
00088 FILE_LOG(logINFO) << "-------- difFirmware[0x" << hex << dif.getDifFirmware() << "]" << endl;
00089
00090 dif.setDifConfig (getData(1));
00091 display(+1);
00092 FILE_LOG(logINFO) << "-------- difConfig[0x" << hex << dif.getDifConfig() << "]" << endl;
00093
00094 dif.setAsicType(getData(1));
00095 display(+1);
00096 FILE_LOG(logINFO) << "-------- asicType[0x" << dif.getAsicType() << "]" << endl;
00097
00098 dif.setAsu1Id(getData(1));
00099 display(+1);
00100 FILE_LOG(logINFO) << "-------- asu1Id[0x" << dif.getAsu1Id() << "]" << endl;
00101
00102 dif.setAsu2Id (getData(1));
00103 display(+1);
00104 FILE_LOG(logINFO) << "-------- asu2Id[0x" << dif.getAsu2Id() << "]" << endl;
00105 }
00106 scHRNumber = getData(1);
00107 FILE_LOG(logINFO) << "NB chip configuration[" << scHRNumber << "]" << endl;
00108
00109 for ( int nbChip = 0 ; (nbChip < scHRNumber) & findA1 == false ; nbChip++)
00110 {
00111 unsigned char *buf = initGetBits(1, false);
00112 if ( !buf)
00113 {
00114 FILE_LOG(logERROR) << " malloc error while getting SC buffer" << endl;
00115 break;
00116 }
00117
00118
00119 int foo = getBits(8);
00120 if ( foo == 0xa1)
00121 {
00122 findA1 = true;
00123 finishGetBits();
00124 FILE_LOG(logDEBUG1) << "Header A1 found" << endl;
00125 }
00126 else
00127 {
00128 unsigned char *buf = initGetBits(108, false);
00129
00130
00131 FILE_LOG(logDEBUG) << "------------------- Config chip [" << nbChip << "]" << endl;
00132
00133
00134 getBits(16);
00135
00136
00137 ui32 dac_2 = getBits(10);
00138 FILE_LOG(logDEBUG) << "dac_2[" << BinOut(dac_2,10) <<"]" << dec<< dac_2 << "]" << endl;
00139
00140 ui32 dac_1 = getBits(10);
00141 FILE_LOG(logDEBUG) << "dac_1[" << BinOut(dac_1,10) <<"]" << dec<< dac_1 << "]" << endl;
00142
00143 ui32 dac_0 = getBits(10);
00144 FILE_LOG(logDEBUG) << "dac_0[" << BinOut(dac_0,10) <<"]" << dec<< dac_0 << "]" << endl;
00145
00146 ui16 chipId = getBits(8);
00147
00148 ui16 result = 0;
00149 int mask = 1;
00150 for ( int bit = 0 ; bit <= 8; bit++ )
00151 {
00152 result = result + ((( chipId & mask ) >> (bit) ) << (7-bit) );
00153 mask = mask * 2;
00154 }
00155 chipId = result;
00156 FILE_LOG(logDEBUG) << "header chip id: [" << BinOut(chipId,8) <<"]=0x" << hex<< chipId << "]" << endl;
00157
00158
00159
00160
00161 finishGetBits();
00162
00163
00164
00165
00166 const Detector &detector = run.getDetector();
00167 try
00168 {
00169 Dif& dif = detector.getDifById(difId);
00170 try {
00171 Hardroc2Chip& chip = dynamic_cast<Hardroc2Chip &> (dif.getChipById(chipId));
00172 FILE_LOG(logDEBUG1) << " Hardroc2 slow control for chip " << chipId << " et Dif " << difId << " configuration set: " << endl;
00173 chip.setThresholdDac_0(dac_0);
00174 chip.setThresholdDac_1(dac_1);
00175 chip.setThresholdDac_1(dac_2);
00176 }
00177 catch ( ... )
00178 {
00179 }
00180 }
00181 catch ( MicroException e )
00182 {
00183 FILE_LOG(logINFO) << " Hardroc2 slow control for chip " << chipId << " et Dif " << difId << " configuration: " << e.getMessage() << endl;
00184
00185 }
00186 }
00187
00188 }
00189
00190 if ( ! findA1) {
00191 unsigned char *buf = initGetBits(1, false);
00192 if ( !buf)
00193 {
00194 FILE_LOG(logERROR) << " malloc error while getting SC buffer" << endl;
00195 exit ( -1);
00196 }
00197
00198 int foo = getBits(8);
00199 finishGetBits();
00200
00201 FILE_LOG(logDEBUG) << "Herader[" << foo <<"]" << endl;
00202 if ( foo != 0xa1)
00203 {
00204 FILE_LOG(logERROR) << " Header A1 not found" << endl;
00205 exit (-1);
00206 }
00207 FILE_LOG(logDEBUG1) << "Header A1 found" << endl;
00208 }
00209
00210 }
00211
00212 else {
00213 bool findA1 = false;
00214 while ( ! findA1 )
00215 {
00216 unsigned char *buf = initGetBits(1, false);
00217 if ( !buf)
00218 {
00219 FILE_LOG(logERROR) << " malloc error while getting SC buffer" << endl;
00220 exit ( -1);
00221 }
00222
00223 int foo = getBits(8);
00224 finishGetBits();
00225 if ( foo == 0xA1 )
00226 {
00227 findA1 = true;
00228 FILE_LOG(logDEBUG1) << "Header A1 found" << endl;
00229 }
00230 }
00231 }
00232
00233 return(1);
00234 }
00235
00236
00237
00238 int Hardroc2LabviewReader::getAcqData(Event& event) {
00239 unsigned int nbHits = 0;
00240
00241 display(+1);
00242 FILE_LOG(logDEBUG) << " global header B0 found" << endl;
00243
00244 this->setComputeCrcFlag(true);
00245 crc->compute(0xB0);
00246
00247
00248
00249 bcId_Abs = 0;
00250 bcId_Dif = 0;
00251
00252 difId = getData(1);
00253 difTriggerCounter = getData(4);
00254 acqTriggerCounter = getData(4);
00255 globalTriggerCounter = getData(4);
00256 ui64 bcIdHigh = getData(2);
00257 ui64 bcIdLow = getData(4);
00258 bcId_Abs = (bcIdHigh << 32) + bcIdLow;
00259 FILE_LOG(logDEBUG1) << "bcId_Abs[" <<bcId_Abs << endl;
00260 bcId_Dif = getData(3);
00261
00262 bcId_AbsOrg = getbcIdAbsOrg(difId, bcId_Abs);
00263 display();
00264
00265
00266 i64 bcTime = ((bcId_Abs - bcId_AbsOrg) * bcPeriod) / 1000;
00267
00268 i64 readTime = globalTime + bcTime;
00269
00270 FILE_LOG(logDEBUG) << " START EVENT ------------- time " << readTime << " ms" << endl;
00271 FILE_LOG(logDEBUG) << " bc time " << bcTime << " ms" << endl;
00272 FILE_LOG(logDEBUG) << " DIF Id 0x" << hex << (int)difId << dec << endl;
00273 FILE_LOG(logDEBUG) << " DIF trigger counter " << difTriggerCounter << endl;
00274 FILE_LOG(logDEBUG) << " ACQ trigger counter " << acqTriggerCounter << endl;
00275 FILE_LOG(logDEBUG) << " total trigger counter " << globalTriggerCounter << endl;
00276 FILE_LOG(logDEBUG) << " absolute bcId " << bcId_Abs - bcId_AbsOrg << endl;
00277 event.setDifTriggerCounter(difTriggerCounter);
00278 event.setGlobalTriggerCounter(globalTriggerCounter);
00279
00280
00281
00282 bool difFound = false;
00283 Detector &detector = run.getDetector();
00284 { const ChamberMap_t &chambers = detector.getChambers();
00285 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00286 try {
00287 Chamber& chamber = *(it->second);
00288 const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00289 }
00290 catch (...) {
00291
00292 continue;
00293 }
00294
00295
00296 difFound = true;
00297 break;
00298 }
00299 if (!difFound) {
00300 FILE_LOG(logERROR) << " No dif found with Id 0x" << hex << difId << dec << endl;
00301 }
00302 }
00303
00304 for (bool finish = false; !finish; ) {
00305 int delimiter = 0;
00306
00307 display();
00308 for (; delimiter != 0xA0 && delimiter != 0xB4; ) {
00309
00310 delimiter = getData(1);
00311 if (fileError == EOF) {
00312 delimiter = 0;
00313 break;
00314 }
00315 if (delimiter == 0xA3)
00316 FILE_LOG(logERROR) << " frame trailer A3 found -- should not" << endl;
00317 }
00318 if (delimiter == 0x00) {
00319 FILE_LOG(logERROR) << " end of file -- should not" << endl;
00320 break;
00321 }
00322 if (delimiter == 0xA0) {
00323 this->setComputeCrcFlag(false);
00324 unsigned short CRC_check = getData(2);
00325 FILE_LOG(logDEBUG) << hex << " Compute CRC[" << crc->getCrc() << "]" << endl;
00326 FILE_LOG(logDEBUG) << hex << " Received CRC[" << CRC_check << "]" << endl;
00327
00328 if ( CRC_check != crc->getCrc() )
00329 {
00330 FILE_LOG(logERROR) << " CRC Error " << endl;
00331 exit(-1);
00332 }
00333 display(-1);
00334 FILE_LOG(logDEBUG) << " global trailer A0 found" << endl;
00335 break;
00336 }
00337 display(+1);
00338 FILE_LOG(logDEBUG) << " frame header B4 found" << endl;
00339
00340 int chipId = -1;
00341 for (; ; ) {
00342 chipId = getData(1);
00343 if (fileError == EOF) {
00344 finish = true;
00345 FILE_LOG(logINFO) << " end of file found -- should not" << endl;
00346 break;
00347 }
00348 if (chipId == 0x00) {
00349
00350 continue;
00351 }
00352 else if (chipId == 0xA3) {
00353 display(-1);
00354 FILE_LOG(logDEBUG) << " frame trailer A3 found" << endl;
00355 break;
00356 }
00357 else if (chipId == 0xA0) {
00358 finish = true;
00359 display(-1);
00360 FILE_LOG(logERROR) << " global trailer A0 found -- should not" << endl;
00361 int CRC_check = getData(1);
00362 break;
00363 }
00364 else if (chipId == 0xC3) {
00365 display(-1);
00366 FILE_LOG(logERROR) << " frame trailer C3 found - error occured during data transfer" << endl;
00367 break;
00368 }
00369
00370
00371 bcId_Hit = grayToBinary(getData(3));
00372
00373 eventTime = readTime + (((bcId_Dif - bcId_Hit) * bcPeriod) / 1000);
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 if (difFound) {
00392 FILE_LOG(logDEBUG) << " chip Id 0x" << hex << (int)chipId << dec << endl;
00393 FILE_LOG(logDEBUG) << " bcId Hardroc " << (int)bcId_Hit << endl;
00394 FILE_LOG(logDEBUG) << " event time " << eventTime << endl;
00395
00396
00397
00398
00399 for (int chNum = 48; chNum >= 0; chNum -= 16) {
00400 unsigned int data4 = getData(4);
00401
00402 if (difFound) {
00403 for (int index = 0; index < 16; index++) {
00404
00405 int trigger = (data4 >> ((15 - index) * 2)) & 0x03;
00406
00407 if (trigger) {
00408 nbHits += newHit(event, detector, trigger, index + chNum, chipId, difId);
00409 }
00410 }
00411 }
00412 }
00413 }
00414 display();
00415 }
00416
00417 }
00418 display(-1);
00419
00420 if (true)
00421 {
00422
00423
00424 event.setTimeStamp(globalTime + ((bcId_AbsOrg /1000000 * bcPeriod) ) );
00425 event.setId(++lastEventId);
00426
00427 FILE_LOG(logDEBUG) << " STORE EVENT " << lastEventId << ", " << nbHits << " hits" << endl;
00428 }
00429 else
00430 FILE_LOG(logDEBUG) << " EMPTY EVENT" << endl;
00431
00432
00433 if (SkippedBcId) {
00434 FILE_LOG(logWARNING) << " skipped " << SkippedBcId << " data because of bad bcId" << endl;
00435 SkippedBcId = 0;
00436 }
00437
00438 return(nbHits);
00439 }
00440
00441
00442
00443
00444 int Hardroc2LabviewReader::getNextEvent(Event& event) {
00445
00446 Level(0);
00447
00448 ui16 dataFormat = run.getDataFormat();
00449
00450 if (dataFormat == 0) {
00451
00452 bcIdDifAbsOrgMap.clear();
00453
00454 dataFormat = getData(1);
00455 if ( dataFormat == 0x10 ) { dataFormat = 0xa; }
00456 run.setDataFormat(dataFormat);
00457
00458
00459 if (fileError == EOF) {
00460 FILE_LOG(logERROR) << "parsing file[" << inputFile << "] - error " << errno << ":" << strerror(errno) << endl;
00461 reset();
00462 return(LAST_FILE_EVENT);
00463 }
00464 display(+1);
00465 FILE_LOG(logINFO) << " HARDROC2 LABVIEW DATA FORMAT : " << dataFormat << endl;
00466
00467 if (dataFormat != 7 && dataFormat != 1 && dataFormat != 10) {
00468
00469 FILE_LOG(logERROR) << " HARDROC2 LABVIEW data format " << dataFormat << " is not implemented" << endl;
00470 reset();
00471 return(LAST_FILE_EVENT);
00472 }
00473
00474 if ( dataFormat == 10)
00475 {
00476 run.setDaqSoftware( getData(1));
00477 display(+1);
00478 FILE_LOG(logINFO) << " DAQ software version : " << run.getDaqSoftware() << endl;
00479
00480 run.setCccFirmware(getData(1));
00481 display(+1);
00482 FILE_LOG(logINFO) << " ccc firware : " << run.getCccFirmware() << endl;
00483 }
00484
00485 globalTime = getData(4) ;
00486 FILE_LOG(logDEBUG1) << "globalTime in second:[" << globalTime << "]" << endl;
00487 globalTime *= 1000;
00488 FILE_LOG(logDEBUG1) << "globalTime in millisecond:[" << globalTime << "]" << endl;
00489 FILE_LOG(logDEBUG) << " Global timestamp(hex) [" << hex << globalTime << "]" << endl;
00490
00491
00492
00493 if (fileError == EOF) {
00494 FILE_LOG(logDEBUG) << " END OF DATA FILE" << endl;
00495 reset();
00496 return(LAST_FILE_EVENT);
00497 }
00498 FILE_LOG(logDEBUG) << run.getDetector() << endl;
00499 display(-1);
00500 return NEW_CONFIG;
00501 }
00502
00503
00504
00505
00506 bool eventFound = false;
00507 while (! eventFound)
00508 {
00509
00510 unsigned int globalHeader = getData(1);
00511 if (fileError == EOF) {
00512 FILE_LOG(logDEBUG) << " END OF DATA FILE" << endl;
00513 reset();
00514 return(LAST_FILE_EVENT);
00515 }
00516 FILE_LOG(logDEBUG) << "globalHeader:["<< hex << globalHeader << "]" << endl;
00517 if (globalHeader == 0xB1) {
00518
00519 getScData();
00520 }
00521 else if (globalHeader == 0xB0) {
00522
00523
00524 getAcqData(event);
00525 eventFound = true;
00526 }
00527 else {
00528 display();
00529 FILE_LOG(logERROR) << " parsing file error. global header error : " << hex << globalHeader << dec << endl;
00530 }
00531 }
00532
00533
00534 return EVENT_CORRECT;
00535 }