00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <errno.h>
00024
00025 #include "parser/DiracReader.hh"
00026 #include "parser/AcquisitionParser.hh"
00027
00028 #include "event/Event.hh"
00029
00030 #include "geometry/Chamber.hh"
00031 #include "geometry/DiracChamber1.hh"
00032 #include "geometry/Detector.hh"
00033 #include "geometry/Dif.hh"
00034 #include "geometry/Board.hh"
00035 #include "geometry/Chip.hh"
00036 #include "geometry/DiracChip.hh"
00037
00038 #include "tools/Toolbox.hh"
00039 #include "tools/MicroException.hh"
00040 #include "tools/SteerDesc.hh"
00041 #include "tools/Log.hh"
00042
00043 #include <iostream>
00044 #include <iomanip>
00045 #include <string>
00046
00047 using namespace std;
00048
00049
00050
00051
00052 bool DiracChipConfig::read(unsigned char *buf) {
00053
00054 char dataBuf[256];
00055 char *dataStr = dataBuf;
00056
00057 for (int dataNum = 0; dataNum < 23; dataNum++) {
00058 unsigned int data = *buf++;
00059 dataStr += sprintf(dataStr, "%02X", data);
00060 }
00061 *dataStr = 0;
00062 dataStr = dataBuf;
00063 unsigned int data;
00064
00065
00066 sscanf(dataStr, "%1x", &data);
00067 dataStr++;
00068 gain = data & 0x03;
00069 analogData = (data & 0x08) != 0;
00070
00071
00072 sscanf(dataStr, "%2x", &data);
00073 dataStr += 2;
00074 bHiThreshold = data;
00075 sscanf(dataStr, "%2x", &data);
00076 dataStr += 2;
00077 bMidThreshold = data;
00078 sscanf(dataStr, "%2x", &data);
00079 dataStr += 2;
00080 bLoThreshold = data;
00081
00082 for (int bChNum = 63; bChNum >= 32; bChNum -= 4) {
00083
00084 sscanf(dataStr, "%2x", &data);
00085 dataStr += 2;
00086 unsigned char mask = 0x80;
00087 for (int chNum = 0; chNum <= 3; chNum++) {
00088 chConfig[bChNum - chNum].test = (data & mask) != 0;
00089 mask >>= 1;
00090 chConfig[bChNum - chNum].enable = (data & mask) != 0;
00091 mask >>= 1;
00092 }
00093 }
00094
00095 sscanf(dataStr, "%2x", &data);
00096 dataStr += 2;
00097 aHiThreshold = data;
00098 sscanf(dataStr, "%2x", &data);
00099 dataStr += 2;
00100 aMidThreshold = data;
00101 sscanf(dataStr, "%2x", &data);
00102 dataStr += 2;
00103 aLoThreshold = data;
00104
00105 for (int aChNum = 31; aChNum >= 0; aChNum -= 4) {
00106
00107 sscanf(dataStr, "%2x", &data);
00108 dataStr += 2;
00109 unsigned char mask = 0x80;
00110 for (int chNum = 0; chNum <= 3; chNum++) {
00111 chConfig[aChNum - chNum].test = (data & mask) != 0;
00112 mask >>= 1;
00113 chConfig[aChNum - chNum].enable = (data & mask) != 0;
00114 mask >>= 1;
00115 }
00116 }
00117
00118 FILE_LOG(logDEBUG1) << " DiracChipConfig read" << endl;
00119 return (true);
00120 };
00121
00122
00123
00124
00125 bool DiracDifConfig::read(unsigned char *buf) {
00126
00127 unsigned int data = *buf++;
00128 id = data;
00129
00130 data = *buf++;
00131 powerPulsing = (data & 0x40) != 0;
00132 analogReading = (data & 0x20) != 0;
00133 digitalReading = (data & 0x10) != 0;
00134 acqMode = data & 0x0F;
00135
00136 data = *buf++ << 8;
00137 data += *buf++;
00138 lowState = data & 0x03FF;
00139
00140 data = *buf++ << 8;
00141 data += *buf++;
00142 highState = data & 0x03FF;
00143 data = *buf++;
00144 afterState = data & 0x3F;
00145 data = *buf++;
00146 beforeState = data & 0x3F;
00147
00148 data = *buf++ << 8;
00149 data += *buf++;
00150 inverted = (data & 0x0800) != 0;
00151
00152 ctestEnable = (data & 0x0400) != 0;
00153 ctestPeriod = data & 0x03FF;
00154
00155 data = *buf++ << 8;
00156 data += *buf++;
00157 ctestDelay = data & 0x03FF;
00158
00159 data = *buf++ << 8;
00160 data += *buf++;
00161 ctestWidth = data & 0x03FF;
00162
00163 data = *buf++ << 8;
00164 data += *buf++;
00165 triggerDelay = data & 0x03FF;
00166
00167 data = *buf++ << 24;
00168 data += *buf++ << 16;
00169 data += *buf++ << 8;
00170 data += *buf++;
00171 charge = data;
00172
00173 bcPeriod = (lowState + highState) * 10;
00174 FILE_LOG(logDEBUG1) << " DIF #" << id << " : mode " << acqMode << ", BC period = " << bcPeriod << endl;
00175 };
00176
00177
00178
00179
00180 DiracTriggerInfo::DiracTriggerInfo() : bcId(0) {
00181 for (int chNum = 0; chNum < 64; chNum++)
00182 data[chNum] = 0;
00183 };
00184
00185
00186
00187 bool DiracTriggerInfo::read(unsigned char *buf) {
00188
00189
00190 bcId = 0;
00191 for (int chNum = 0; chNum < 64; chNum++)
00192 data[chNum] = 0;
00193
00194
00195 bcIdFpga = *buf++ << 8;
00196 bcIdFpga += *buf++;
00197
00198
00199 bcIdAbs = ((i64) (*buf++)) << 40;
00200 bcIdAbs += ((i64) (*buf++)) << 32;
00201 bcIdAbs += ((i64) (*buf++)) << 24;
00202 bcIdAbs += ((i64) (*buf++)) << 16;
00203 bcIdAbs += ((i64) (*buf++)) << 8;
00204 bcIdAbs += ((i64) (*buf++));
00205
00206
00207 unsigned int data = (*buf++ << 8);
00208 data += *buf++;
00209 reject = (data & 0x0001);
00210 pileUp = (data & 0x003E) >> 1;
00211 trigger = (data & 0xFFC0) >> 6;
00212
00213 FILE_LOG(logDEBUG) << " bcIdFPGA:" << hex << bcIdFpga
00214 << ", bcIdAbs:" << hex << bcIdAbs
00215 << ", data:" << hex << data
00216 << ", reject:" << hex << reject
00217 << ", pileUp:" << hex << pileUp
00218 << ", trigger:" << hex << trigger << endl;
00219
00220 FILE_LOG(logDEBUG1) << " DiracTriggerInfo read" << endl;
00221
00222 return true;
00223 };
00224
00225
00226
00227
00228 DiracReader::DiracReader(Run& aRun, FILE *aFile, ui32 _firstEventId) : DifReader(aRun, aFile, _firstEventId) {
00229 reset();
00230 }
00231
00232
00233
00234 DiracReader::~DiracReader() {
00235 }
00236
00237
00238
00239 void DiracReader::reset() {
00240 nbDifs = 0;
00241 CRC = 0;
00242 bcId_AbsOrg = -1;
00243 }
00244
00245
00246
00247
00248 int DiracReader::getConfigData() {
00249 int difId = getBinData(1);
00250
00251 for (int difNum = 0; difNum < difConfigs.size(); difNum++) {
00252 DiracDifConfig& difConfig = difConfigs[difNum];
00253 if (difConfig.id == difId) {
00254 difConfig.nbChips = getBinData(1);
00255
00256 display();
00257 FILE_LOG(logDEBUG1) << " DIF #" << difConfig.id << " : " << difConfig.nbChips << " chips" << endl;
00258
00259 for (int chipNum = 0; chipNum < difConfig.nbChips; chipNum++) {
00260 DiracChipConfig chipConfig;
00261
00262 unsigned char buf[24];
00263 getBinData(23, buf);
00264 display();
00265 chipConfig.read(buf);
00266
00267
00268
00269
00270 difConfig.chipConfigs.push_back(chipConfig);
00271
00272 const Detector &detector = run.getDetector();
00273 try
00274 {
00275 Dif& dif = detector.getDifById(difId);
00276 BoardMap_t boards = dif.getBoards();
00277 for (BoardMap_t::iterator boardIt = boards.begin(); boardIt != boards.end(); boardIt++) {
00278 Board& b = *boardIt->second;
00279 try
00280 {
00281 DiracChip& chip = dynamic_cast<DiracChip&> (b.getChipById(chipNum));
00282 chip.setBLoThreshold(chipConfig.bLoThreshold);
00283 chip.setBMidThreshold(chipConfig.bMidThreshold);
00284 chip.setBHiThreshold(chipConfig.bHiThreshold);
00285 chip.setALoThreshold(chipConfig.aLoThreshold);
00286 chip.setAMidThreshold(chipConfig.aMidThreshold);
00287 chip.setAHiThreshold(chipConfig.aHiThreshold);
00288 chip.setGain(chipConfig.gain);
00289 for (int chNum = 0; chNum < 64; chNum++) {
00290 Channel& ch = chip.getChannelById(chNum);
00291 ch.setEnable(chipConfig.chConfig[chNum].enable);
00292 ch.setStimulate(chipConfig.chConfig[chNum].test);
00293 }
00294 break;
00295 }
00296
00297 catch(...) {
00298 }
00299 }
00300 }
00301
00302 catch(...) {
00303 }
00304 }
00305
00306 if (difConfigs.size() > nbDifs) {
00307 FILE_LOG(logERROR) << "DIF config error" << endl;
00308 return (0);
00309 }
00310 return (1);
00311 }
00312 }
00313
00314 FILE_LOG(logERROR) << "DIF #" << difId << " not found" << endl;
00315 return (0);
00316 }
00317
00318
00319
00320 int DiracReader::getAcqData() {
00321 acqDifId = getBinData(1);
00322 unsigned int nbTotalTrigger = getBinData(4);
00323 unsigned int nbTrigger = getBinData(4);
00324 display();
00325
00326 FILE_LOG(logDEBUG) << " data for DIF #" << acqDifId << ", " << nbTotalTrigger << " total, " << nbTrigger << " triggers" << endl;
00327
00328
00329 for (int difNum = 0; difNum < difConfigs.size(); difNum++) {
00330 const DiracDifConfig& difConfig = difConfigs[difNum];
00331 if (difConfig.id == acqDifId) {
00332
00333 for (int trigInfoNum = 0; trigInfoNum < 8; trigInfoNum++) {
00334 unsigned char buf[11];
00335 getBinData(10, buf);
00336 display();
00337 trigInfos[trigInfoNum].read(buf);
00338 }
00339
00340
00341
00342
00343
00344
00345
00346 bcId_AbsOrg = getbcIdAbsOrg(acqDifId, trigInfos[0].bcIdAbs);
00347 FILE_LOG(logDEBUG) << " getbcIdAbsOrg(" << acqDifId << " , trigInfos[0].bcIdAbs) " << getbcIdAbsOrg(acqDifId, trigInfos[0].bcIdAbs) << endl;
00348
00349
00350 for (int chipNum = 0; chipNum < difConfig.nbChips; chipNum++) {
00351 unsigned int header = getBinData(1);
00352 if (header != 0xB4) {
00353 FILE_LOG(logERROR) << "parsing file error : header 0xB4 not found" << endl;
00354 return (0);
00355 }
00356 display(+1);
00357
00358
00359 for (int bitNum = 11; bitNum >= 0; bitNum--) {
00360 const unsigned int data = getBinData(1);
00361 unsigned int mask = 0x80;
00362 for (int trigNum = 0; trigNum < 8; trigNum++, mask >>= 1) {
00363 DiracTriggerInfo& trigInfo = trigInfos[trigNum];
00364 unsigned int bit = ((data & mask) != 0) ? 1 : 0;
00365 trigInfo.bcId |= bit << bitNum;
00366 }
00367 }
00368 display();
00369
00370
00371 for (int chNum = 63; chNum >= 0; chNum--) {
00372 for (int bitNum = 0; bitNum < 2; bitNum++) {
00373 const unsigned int data = getBinData(1);
00374 unsigned int mask = 0x80;
00375 for (int trigNum = 0; trigNum < 8; trigNum++, mask >>= 1) {
00376 DiracTriggerInfo& trigInfo = trigInfos[trigNum];
00377 unsigned int bit = ((data & mask) != 0) ? 1 : 0;
00378 trigInfo.data[chNum] |= bit << bitNum;
00379 }
00380 }
00381 if (chNum == 32)
00382 display();
00383 }
00384 display();
00385
00386 header = getBinData(1);
00387 display(-1);
00388 if (header != 0xA3) {
00389 FILE_LOG(logERROR) << "parsing file error : trailer 0xA3 not found" << endl;
00390 return (0);
00391 }
00392 }
00393
00394 return (1);
00395 }
00396 }
00397
00398 return (0);
00399 }
00400
00401
00402
00403 int DiracReader::storeHits(Event& event) {
00404 int nbHits = 0;
00405
00406 for (int difNum = 0; difNum < difConfigs.size(); difNum++) {
00407 const DiracDifConfig& difConfig = difConfigs[difNum];
00408 if (difConfig.id == acqDifId) {
00409 for (int chipNum = 0; chipNum < difConfig.nbChips; chipNum++) {
00410
00411 for (int trigNum = 0; trigNum < 8; trigNum++) {
00412 const DiracTriggerInfo& trigInfo = trigInfos[trigNum];
00413
00414 for (int chNum = 0; chNum < 64; chNum++) {
00415 if (trigInfo.data[chNum]) {
00416 if (newHit(event, difConfig, trigInfo, chipNum, chNum))
00417 nbHits++;
00418 }
00419 }
00420 }
00421 }
00422
00423 if (nbHits) {
00424
00425 event.setId(++lastEventId);
00426 FILE_LOG(logDEBUG1) << " STORE EVENT " << lastEventId << ", " << nbHits << " hits" << endl;
00427 } else
00428 FILE_LOG(logDEBUG) << " EMPTY EVENT" << endl;
00429 return (nbHits);
00430 }
00431 }
00432
00433 return (0);
00434 }
00435
00436
00437
00438 ChannelHit *DiracReader::newHit(Event& event, const DiracDifConfig& difConfig, const DiracTriggerInfo& trigInfo, const int chipNum, const int chNum) {
00439 const Detector &detector = run.getDetector();
00440 const ChamberMap_t &chambers = detector.getChambers();
00441 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00442 try
00443 {
00444 DiracChamber1& chamber = dynamic_cast<DiracChamber1&> (*(it->second));
00445 const BoardMap_t& boards = chamber.getBoardsByDifId(difConfig.id);
00446
00447 try
00448 {
00449 const Channel& channel = chamber.getChannelById(chNum, chipNum, difConfig.id);
00450 const int data = trigInfo.data[chNum];
00451 ChannelHit *hit = new ChannelHit(channel, data, 0);
00452
00453 i64 bcId_Abs;
00454 i64 bcId_Dif;
00455 i64 bcId_Hit;
00456 i64 bcId_Hit_Fine = trigInfos[0].trigger;
00457
00458
00459 bcId_Abs = trigInfos[0].bcIdAbs;
00460 bcId_Dif = trigInfos[0].bcIdFpga;
00461
00462
00463
00464
00465
00466
00467 bcId_Hit = trigInfo.bcId;
00468
00469 hit->setHardrocTime(globalTime, bcId_Abs, bcId_Dif, bcId_Hit,bcId_Hit_Fine);
00470 event.insertHit(chamber, hit);
00471 i64 timeStamp = (globalTime * 1000)
00472 + ((trigInfo.bcIdAbs - bcId_AbsOrg) / 5000000)
00473 + (((trigInfo.bcId - trigInfo.bcIdFpga) * difConfig.bcPeriod) / 1000);
00474 event.setTimeStamp(timeStamp);
00475 FILE_LOG(logDEBUG) << "bcId_Abs[" << bcId_Abs << "] - bcId_AbsOrg[" << bcId_AbsOrg << "] = [" << bcId_Abs - bcId_AbsOrg << "]" << endl;
00476 FILE_LOG(logDEBUG) << " new Hit : ch " << chNum << ", chip " << chipNum << ", dif " << difConfig.id
00477 << ", Time=" << timeStamp << "ms, bcId:abs=" << (bcId_Abs - bcId_AbsOrg) << ",dif=" << bcId_Dif << ",hit=" << bcId_Hit << " Data=" << data << endl;
00478 return (hit);
00479 }
00480
00481 catch(MicroException & e) {
00482 FILE_LOG(logERROR) << "ch " << chNum << ", chip " << chipNum << ", dif " << difConfig.id << " : " << e.getMessage() << endl;
00483 return (0);
00484 }
00485 }
00486
00487 catch(...) {
00488 continue;
00489 }
00490
00491 }
00492 FILE_LOG(logERROR) << "ch " << chNum << ", chip " << chipNum << ", dif " << difConfig.id << " : geometry element not found" << endl;
00493 return (0);
00494 }
00495
00496
00497
00498 void DiracReader::storeDifInfo(const int difId) {
00499 const Detector &detector = run.getDetector();
00500 try
00501 {
00502 Dif& dif = detector.getDifById(difId);
00503 for (int difNum = 0; difNum < difConfigs.size(); difNum++) {
00504 DiracDifConfig& difConfig = difConfigs[difNum];
00505 if (difConfig.id == difId) {
00506 dif.setAcqMode(difConfig.acqMode);
00507 dif.setLowRegister(difConfig.lowState);
00508 dif.setHighRegister(difConfig.highState);
00509 dif.setAfterRegister(difConfig.afterState);
00510 dif.setBeforeRegister(difConfig.beforeState);
00511 return;
00512 }
00513 }
00514 }
00515
00516 catch(...) {
00517 };
00518 };
00519
00520
00521
00522
00523
00524 int DiracReader::getNextEvent(Event& event) {
00525
00526 Level(0);
00527
00528 i16 dataFormat = run.getDataFormat();
00529 if (dataFormat == -1) {
00530
00531 try
00532 {
00533 bcIdDifAbsOrgMap.clear();
00534 }
00535
00536 catch(exception & e) {
00537 FILE_LOG(logERROR) << "bcIdDifAbsOrgMap.clear():" << e.what() << endl;
00538 }
00539
00540 dataFormat = getBinData(1);
00541 run.setDataFormat(dataFormat);
00542 if (fileError == EOF) {
00543 FILE_LOG(logDEBUG) << "end of file found" << endl;
00544 reset();
00545 return (LAST_FILE_EVENT);
00546 }
00547
00548 if (dataFormat != 1) {
00549
00550 FILE_LOG(logERROR) << "DIRAC data format " << dataFormat << " is not implemented" << endl;
00551 reset();
00552 return (LAST_FILE_EVENT);
00553 }
00554
00555 nbDifs = getBinData(1);
00556 display(+1);
00557 FILE_LOG(logDEBUG) << "DIRAC data format : " << dataFormat << ", nb Dif = " << nbDifs << endl;
00558
00559 for (int difNum = 0; difNum < nbDifs; difNum++) {
00560
00561 DiracDifConfig difConfig;
00562 unsigned char buf[21];
00563 getBinData(20, buf);
00564 display();
00565
00566 difConfig.read(buf);
00567 difConfigs.push_back(difConfig);
00568
00569
00570 storeDifInfo(difConfig.id);
00571
00572
00573 run.setInjectedCharge(difConfigs[0].charge);
00574 run.setWidthCtest(difConfigs[0].ctestWidth);
00575 run.setPeriodCtest(difConfigs[0].ctestPeriod);
00576 run.setDelayCtest(difConfigs[0].ctestDelay);
00577 run.setDelayTrigger(difConfigs[0].triggerDelay);
00578 }
00579 display(-1);
00580
00581
00582 globalTime = getBinData(4);
00583 display();
00584 FILE_LOG(logDEBUG1) << "global time : " << globalTime << endl;
00585 return NEW_CONFIG;
00586 }
00587
00588 for (unsigned int nbHits = 0; !nbHits;) {
00589 unsigned char header = getBinData(1);
00590 if (fileError == EOF) {
00591 FILE_LOG(logDEBUG) << " END OF DATA FILE" << endl;
00592 reset();
00593 return (LAST_FILE_EVENT);
00594 }
00595
00596 if (header == 0xB1) {
00597
00598 display(+1);
00599 getConfigData();
00600 if (getBinData(1) != 0xA1) {
00601 display(-1);
00602 FILE_LOG(logERROR) << " frame trailer A1 not found" << endl;
00603 break;
00604 }
00605 display(-1);
00606 } else if (header == 0xB0) {
00607
00608 display(+1);
00609 getAcqData();
00610 if (getBinData(1) != 0xA0) {
00611 display(-1);
00612 FILE_LOG(logERROR) << " frame trailer A0 not found" << endl;
00613 break;
00614 }
00615 display(-1);
00616 CRC = getBinData(2);
00617 display();
00618
00619
00620 nbHits = storeHits(event);
00621 } else {
00622 display();
00623 FILE_LOG(logERROR) << " expected header not found" << endl;
00624 }
00625
00626 if (fileError == EOF)
00627 break;
00628 }
00629
00630 return EVENT_CORRECT;
00631 }