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