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 <errno.h>
00025
00026 #include "parser/Hardroc2XdaqReader.hh"
00027 #include "parser/AcquisitionParser.hh"
00028
00029 #include "event/Event.hh"
00030
00031 #include "geometry/Chamber.hh"
00032 #include "geometry/Dif.hh"
00033 #include "geometry/Hardroc2Chip.hh"
00034 #include "geometry/Detector.hh"
00035
00036 #include "tools/Toolbox.hh"
00037 #include "tools/SteerDesc.hh"
00038 #include "tools/MicroException.hh"
00039 #include "tools/Log.hh"
00040
00041 #include <iostream>
00042 #include <iomanip>
00043
00044 using namespace std;
00045
00046
00047 Hardroc2XdaqReader::Hardroc2XdaqReader(Run& aRun, FILE *aFile, ui32 lastEventId) : Hardroc2Reader(aRun, aFile, lastEventId)
00048 {
00049 FILE_LOG(logDEBUG1) << "--Hardroc2XdaqReader constructor()"<< endl;
00050 reset();
00051 }
00052
00053
00054 Hardroc2XdaqReader::~Hardroc2XdaqReader()
00055 {
00056 FILE_LOG(logDEBUG1)<< "----Hardroc2XdaqReader destructor"<< endl;
00057 }
00058
00059
00060
00061 int Hardroc2XdaqReader::getScData() {
00062 unsigned int data;
00063 display(+1);
00064 FILE_LOG(logDEBUG) << " Slow Control data header B1 found" << endl;
00065
00066 int trop = 0;
00067 while ( getData(1) != 0xA1)
00068 {
00069 display();
00070 trop++;
00071 }
00072 display();
00073 FILE_LOG(logDEBUG) << "Il y a un [" << dec << trop << "] bytes depuis B1 0xA1 " << endl;
00074 data = 0xA1;
00075
00076
00077 display(-1);
00078 if (data != 0xA1)
00079 FILE_LOG(logERROR) << " parsing file error : Slow control trailer 0xA1 not found [" << hex << data << dec << "]" << endl;
00080
00081 return(1);
00082 }
00083
00084
00085
00086 int Hardroc2XdaqReader::getAcqData(Event& event) {
00087 unsigned int nbHits = 0;
00088
00089 display(+1);
00090
00091 this->setComputeCrcFlag(true);
00092 crc->compute(0xB0);
00093
00094
00095 FILE_LOG(logDEBUG) << " global header B0 found" << endl;
00096
00097
00098
00099 bcId_Abs = 0;
00100 bcId_Dif = 0;
00101
00102 difId = getData(1);
00103 difTriggerCounter = getData(4);
00104 acqTriggerCounter = getData(4);
00105 globalTriggerCounter = getData(4);
00106 ui64 bcIdHigh = getData(2);
00107 ui64 bcIdLow = getData(4);
00108 bcId_Abs = (bcIdHigh << 32) + bcIdLow;
00109 bcId_Dif = getData(3);
00110
00111 bcId_AbsOrg = getbcIdAbsOrg(difId, bcId_Abs);
00112 FILE_LOG(logDEBUG1) << " bcId_AbsOrg [" << bcId_AbsOrg << "[" << endl;
00113 display();
00114
00115
00116 i64 bcTime = ((bcId_Abs - bcId_AbsOrg) * bcPeriod) / 1000;
00117
00118 i64 readTime = globalTime + bcTime;
00119
00120 FILE_LOG(logDEBUG) << " START EVENT ------------- time " << readTime << " ms" << endl;
00121 FILE_LOG(logDEBUG) << " bc time " << bcTime << " ms" << endl;
00122 FILE_LOG(logDEBUG) << " DIF Id 0x" << hex << (int)difId << dec << endl;
00123 FILE_LOG(logDEBUG) << " DIF trigger counter " << difTriggerCounter << endl;
00124 FILE_LOG(logDEBUG) << " ACQ trigger counter " << acqTriggerCounter << endl;
00125 FILE_LOG(logDEBUG) << " total trigger counter " << globalTriggerCounter << endl;
00126 FILE_LOG(logDEBUG) << " absolute bcId " << bcId_Abs - bcId_AbsOrg << endl;
00127
00128
00129 bool difFound = false;
00130 Detector &detector = run.getDetector();
00131 { const ChamberMap_t &chambers = detector.getChambers();
00132 for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00133 try {
00134 Chamber& chamber = *(it->second);
00135 const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00136 }
00137 catch (...) {
00138
00139 continue;
00140 }
00141
00142
00143 difFound = true;
00144 break;
00145 }
00146 if (!difFound) {
00147 FILE_LOG(logERROR) << " No dif found with Id 0x" << hex << difId << dec << endl;
00148 }
00149 }
00150
00151 for (bool finish = false; !finish; ) {
00152 int delimiter = 0;
00153
00154 display();
00155 for (; delimiter != 0xA0 && delimiter != 0xB4; ) {
00156
00157 delimiter = getData(1);
00158 if (fileError == EOF) {
00159 delimiter = 0;
00160 break;
00161 }
00162 if (delimiter == 0xA3)
00163 FILE_LOG(logERROR) << " frame trailer A3 found -- should not" << endl;
00164 }
00165 if (delimiter == 0x00) {
00166 FILE_LOG(logERROR) << " end of file -- should not" << endl;
00167 break;
00168 }
00169 if (delimiter == 0xA0) {
00170 this->setComputeCrcFlag(false);
00171 unsigned short CRC_check = getData(2);
00172 FILE_LOG(logDEBUG) << hex << " Compute CRC[" << crc->getCrc() << "]" << endl;
00173 FILE_LOG(logDEBUG) << hex << " Received CRC[" << CRC_check << "]" << endl;
00174 if ( CRC_check != crc->getCrc() )
00175 {
00176 FILE_LOG(logERROR) << " CRC Error for event id " << lastEventId
00177 << " Compute CRC[" << crc->getCrc()
00178 << "] Received CRC[" << CRC_check << "]" << endl ;
00179 event.setCrcIsCorrect(false);
00180 event.setValidFlag(CRC_ERROR);
00181 }
00182 display(-1);
00183 FILE_LOG(logDEBUG) << " global trailer A0 found" << endl;
00184 break;
00185 }
00186 display(+1);
00187 FILE_LOG(logDEBUG) << " frame header B4 found" << endl;
00188
00189 int chipId = -1;
00190 for (; ; ) {
00191 chipId = getData(1);
00192 if (fileError == EOF) {
00193 finish = true;
00194 FILE_LOG(logINFO) << " end of file found -- should not" << endl;
00195 break;
00196 }
00197 if (chipId == 0x00) {
00198
00199 continue;
00200 }
00201 else if (chipId == 0xA3) {
00202 display(-1);
00203 FILE_LOG(logDEBUG) << " frame trailer A3 found" << endl;
00204 break;
00205 }
00206 else if (chipId == 0xA0) {
00207 finish = true;
00208 display(-1);
00209 FILE_LOG(logERROR) << " global trailer A0 found -- should not" << endl;
00210 int CRC_check = getData(1);
00211 break;
00212 }
00213 else if (chipId == 0xC3) {
00214 display(-1);
00215 FILE_LOG(logERROR) << " frame trailer C3 found - error occured during data transfer" << endl;
00216 break;
00217 }
00218
00219
00220 bcId_Hit = grayToBinary(getData(3));
00221
00222 eventTime = readTime + (((bcId_Dif - bcId_Hit) * bcPeriod) / 1000);
00223
00224
00225 if (bcId_Hit <= 1) {
00226 FILE_LOG(logDEBUG) << " skip data (bcIdHit = " << (int)bcId_Hit << ")" << endl;
00227 SkippedBcId++;
00228
00229 for (int chNum = 48; chNum >= 0; chNum -= 16) {
00230 unsigned int data4 = getData(4);
00231 }
00232 }
00233 else {
00234
00235 if (difFound) {
00236 FILE_LOG(logDEBUG) << " chip Id 0x" << hex << (int)chipId << dec << endl;
00237 FILE_LOG(logDEBUG) << " bcId Hardroc " << (int)bcId_Hit << endl;
00238 FILE_LOG(logDEBUG) << " event time " << eventTime << endl;
00239 }
00240
00241
00242
00243 for (int chNum = 48; chNum >= 0; chNum -= 16) {
00244 unsigned int data4 = getData(4);
00245
00246 if (difFound) {
00247 for (int index = 0; index < 16; index++) {
00248
00249 int trigger = (data4 >> ((15 - index) * 2)) & 0x03;
00250
00251 if (trigger) {
00252 nbHits += newHit(event, detector, trigger, index + chNum, chipId, difId);
00253 }
00254 }
00255 }
00256 }
00257 }
00258 display();
00259 }
00260
00261 }
00262 display(-1);
00263
00264 if (nbHits) {
00265
00266 event.setTimeStamp(globalTime + (bcId_AbsOrg * bcPeriod) / 1000);
00267
00268 event.setId(++lastEventId);
00269 FILE_LOG(logDEBUG1) << " STORE EVENT " << lastEventId << ", " << nbHits << " hits" << endl;
00270 }
00271 else
00272 FILE_LOG(logDEBUG) << " EMPTY EVENT" << endl;
00273
00274
00275 if (SkippedBcId) {
00276 FILE_LOG(logWARNING) << " skipped " << SkippedBcId << " data because of bad bcId" << endl;
00277 SkippedBcId = 0;
00278 }
00279
00280 return(nbHits);
00281 }
00282
00283
00284
00285
00286 int Hardroc2XdaqReader::getNextEvent(Event& event) {
00287
00288 Level(0);
00289
00290 FILE_LOG(logDEBUG) << "DEBUG A1"<< endl;
00291 ui16 dataFormat = run.getDataFormat();
00292 if (dataFormat == 0) {
00293 FILE_LOG(logDEBUG) << "DEBUG A2"<< endl;
00294
00295 bcIdDifAbsOrgMap.clear();
00296
00297 dataFormat = getData(1);
00298 if (fileError == EOF) {
00299 FILE_LOG(logERROR) << "parsing file[" << inputFile << "] - error " << errno << ":" << strerror(errno) << endl;
00300 reset();
00301 return(LAST_FILE_EVENT);
00302 }
00303 display(+1);
00304 FILE_LOG(logINFO) << " HARDROC2 DIF VHDL data format : " << dataFormat << endl;
00305
00306 if (dataFormat != 7 ) {
00307
00308 FILE_LOG(logERROR) << " HARDROC2 DIF VHDL data format " << dataFormat << " is not implemented" << endl;
00309 reset();
00310 return(LAST_FILE_EVENT);
00311 }
00312 run.setDataFormat(dataFormat);
00313
00314
00315 FILE_LOG(logDEBUG) << run.getDetector() << endl;
00316 display(-1);
00317 return NEW_CONFIG;
00318 }
00319
00320
00321
00322 for (unsigned int nbHits = 0; !nbHits; ) {
00323
00324
00325 unsigned int globalHeader = getData(1);
00326 if (fileError == EOF) {
00327 FILE_LOG(logINFO) << " END OF DATA FILE" << endl;
00328 reset();
00329 return(LAST_FILE_EVENT);
00330 }
00331 FILE_LOG(logDEBUG) << "globalHeader:["<< hex << globalHeader << "]" << endl;
00332 if (globalHeader == 0xB1) {
00333
00334 getScData();
00335 }
00336 else if (globalHeader == 0xB0) {
00337
00338 nbHits = getAcqData(event);
00339 }
00340 else {
00341 display();
00342 FILE_LOG(logERROR) << " parsing file error. global header error : " << hex << globalHeader << dec << endl;
00343 }
00344 }
00345
00346 return EVENT_CORRECT;
00347 }