File indexing completed on 2024-04-06 12:10:37
0001 #include "DCCDataParser.h"
0002
0003
0004
0005
0006
0007 DCCTBDataParser::DCCTBDataParser(const std::vector<uint32_t> &parserParameters, bool parseInternalData, bool debug)
0008 : buffer_(nullptr), parseInternalData_(parseInternalData), debug_(debug), parameters(parserParameters) {
0009 mapper_ = new DCCTBDataMapper(this);
0010 resetErrorCounters();
0011 computeBlockSizes();
0012 }
0013
0014
0015
0016
0017
0018 void DCCTBDataParser::resetErrorCounters() {
0019
0020 errors_["DCC::BOE"] = 0;
0021 errors_["DCC::EOE"] = 0;
0022 errors_["DCC::EVENT LENGTH"] = 0;
0023 }
0024
0025
0026
0027
0028
0029 void DCCTBDataParser::computeBlockSizes() {
0030 uint32_t nTT = numbTTs();
0031 uint32_t tSamples = numbTriggerSamples();
0032 uint32_t nSr = numbSRF();
0033
0034 uint32_t tf(0), srf(0);
0035
0036 if ((nTT * tSamples) < 4 || (nTT * tSamples) % 4)
0037 tf = 1;
0038 else
0039 tf = 0;
0040
0041 if (srf < 16 || srf % 16)
0042 srf = 1;
0043 else
0044 srf = 0;
0045
0046
0047 tccBlockSize_ = 8 + ((nTT * tSamples) / 4) * 8 + tf * 8;
0048
0049
0050 srpBlockSize_ = 8 + (nSr / 16) * 8 + srf * 8;
0051 }
0052
0053
0054
0055
0056
0057 void DCCTBDataParser::parseFile(std::string fileName, bool singleEvent) {
0058 std::ifstream inputFile;
0059 inputFile.open(fileName.c_str());
0060
0061 resetErrorCounters();
0062
0063
0064
0065
0066
0067
0068 if (!inputFile.fail()) {
0069 std::string myWord;
0070 std::vector<std::string> dataVector;
0071
0072
0073 while (inputFile >> myWord) {
0074 dataVector.push_back(myWord);
0075 }
0076
0077 bufferSize_ = (dataVector.size()) * 4;
0078
0079 uint32_t *myData = new uint32_t[dataVector.size()];
0080 uint32_t *const myDataBeginning = myData;
0081
0082
0083 for (uint32_t i = 1; i <= dataVector.size(); i++, myData++) {
0084 sscanf((dataVector[i - 1]).c_str(), "%x", myData);
0085
0086
0087
0088 }
0089
0090 inputFile.close();
0091
0092 parseBuffer(myData, bufferSize_, singleEvent);
0093 delete[] myDataBeginning;
0094 } else {
0095 std::string errorMessage = std::string(" Error::Unable to open file :") + fileName;
0096 throw ECALTBParserException(errorMessage);
0097 }
0098 }
0099
0100
0101
0102
0103
0104 void DCCTBDataParser::parseBuffer(const uint32_t *buffer, uint32_t bufferSize, bool singleEvent) {
0105 resetErrorCounters();
0106
0107 buffer_ = buffer;
0108
0109
0110 processedEvent_ = 0;
0111 events_.clear();
0112 std::vector<DCCTBEventBlock *>::iterator it;
0113 for (it = dccEvents_.begin(); it != dccEvents_.end(); it++) {
0114 delete *it;
0115 }
0116 dccEvents_.clear();
0117 eventErrors_ = "";
0118
0119
0120
0121
0122
0123
0124 if (bufferSize % 8) {
0125 std::string fatalError;
0126 fatalError += "\n ======================================================================";
0127 fatalError += "\n Fatal error at event = " + getDecString(events_.size() + 1);
0128 fatalError += "\n Buffer Size of = " + getDecString(bufferSize) + "[bytes] is not divisible by 8 ... ";
0129 fatalError += "\n ======================================================================";
0130 throw ECALTBParserException(fatalError);
0131 }
0132 if (bufferSize < EMPTYEVENTSIZE) {
0133 std::string fatalError;
0134 fatalError += "\n ======================================================================";
0135 fatalError += "\n Fatal error at event = " + getDecString(events_.size() + 1);
0136 fatalError += "\n Buffer Size of = " + getDecString(bufferSize) + "[bytes] is less than an empty event ... ";
0137 fatalError += "\n ======================================================================";
0138 throw ECALTBParserException(fatalError);
0139 }
0140
0141 const uint32_t *myPointer = buffer_;
0142
0143
0144 uint32_t processedBytes(0), wordIndex(0), eventLength(0), errorMask(0);
0145
0146
0147 while (processedBytes + EMPTYEVENTSIZE <= bufferSize) {
0148
0149
0150
0151
0152
0153
0154
0155 uint32_t bytesToEnd = bufferSize - processedBytes;
0156 std::pair<uint32_t, uint32_t> eventD = checkEventLength(myPointer, bytesToEnd, singleEvent);
0157 eventLength = eventD.second;
0158 errorMask = eventD.first;
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 if (parseInternalData_) {
0172
0173 DCCTBEventBlock *myBlock =
0174 new DCCTBEventBlock(this, myPointer, eventLength * 8, eventLength * 2 - 1, wordIndex, 0);
0175
0176
0177 dccEvents_.push_back(myBlock);
0178 }
0179
0180
0181 std::pair<const uint32_t *, uint32_t> eventPointer(myPointer, eventLength);
0182 std::pair<uint32_t, std::pair<const uint32_t *, uint32_t> > eventPointerWithErrorMask(errorMask, eventPointer);
0183 events_.push_back(eventPointerWithErrorMask);
0184
0185
0186 processedEvent_++;
0187 processedBytes += eventLength * 8;
0188
0189
0190
0191 myPointer += eventLength * 2;
0192 wordIndex += eventLength * 2;
0193 }
0194 }
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206 std::pair<uint32_t, uint32_t> DCCTBDataParser::checkEventLength(const uint32_t *pointerToEvent,
0207 uint32_t bytesToEnd,
0208 bool singleEvent) {
0209 std::pair<uint32_t, uint32_t> result;
0210 uint32_t errorMask(0);
0211
0212
0213
0214 const uint32_t *boePointer = pointerToEvent + 1;
0215 if ((((*boePointer) >> BOEBEGIN) & BOEMASK) != BOE) {
0216 (errors_["DCC::BOE"])++;
0217 errorMask = 1;
0218 }
0219
0220
0221 const uint32_t *myPointer = pointerToEvent + 2;
0222 uint32_t eventLength = (*myPointer) & EVENTLENGTHMASK;
0223
0224
0225
0226 bool eoeError = false;
0227
0228
0229 if (singleEvent && eventLength != bytesToEnd / 8) {
0230 eventLength = bytesToEnd / 8;
0231 (errors_["DCC::EVENT LENGTH"])++;
0232 errorMask = errorMask | (1 << 1);
0233 }
0234
0235 else if (eventLength == 0 || eventLength > (bytesToEnd / 8) || eventLength < (EMPTYEVENTSIZE / 8)) {
0236
0237
0238
0239 std::string fatalError;
0240
0241 fatalError += "\n ======================================================================";
0242 fatalError += "\n Fatal error at event = " + getDecString(events_.size() + 1);
0243 fatalError += "\n Decoded event length = " + getDecString(eventLength);
0244 fatalError += "\n bytes to buffer end = " + getDecString(bytesToEnd);
0245 fatalError += "\n Unable to procead the data decoding ...";
0246
0247 if (eventLength > (bytesToEnd / 8)) {
0248 fatalError += " (eventLength > (bytesToEnd / 8)";
0249 } else {
0250 fatalError += "\n event length not big enough heaven to build an empty event ( 4x8 bytes)";
0251 }
0252
0253 fatalError += "\n ======================================================================";
0254
0255 throw ECALTBParserException(fatalError);
0256 }
0257
0258
0259
0260 const uint32_t *endOfEventPointer = pointerToEvent + eventLength * 2 - 1;
0261 if ((((*endOfEventPointer) >> EOEBEGIN & EOEMASK) != EOEMASK) && !eoeError) {
0262 (errors_["DCC::EOE"])++;
0263 errorMask = errorMask | (1 << 2);
0264 }
0265
0266
0267 result.first = errorMask;
0268 result.second = eventLength;
0269
0270 return result;
0271 }
0272
0273
0274
0275
0276
0277 std::string DCCTBDataParser::index(uint32_t position) {
0278 char indexBuffer[20];
0279 long unsigned int pos = position;
0280 snprintf(
0281 indexBuffer, sizeof(indexBuffer), "W[%08lu]", pos);
0282
0283 return std::string(indexBuffer);
0284 }
0285
0286
0287
0288
0289
0290 std::string DCCTBDataParser::getDecString(uint32_t dat) {
0291 char buffer[15];
0292 long unsigned int data = dat;
0293 snprintf(buffer, sizeof(buffer), "%lu", data);
0294
0295 return std::string(buffer);
0296 }
0297
0298
0299
0300
0301
0302 std::string DCCTBDataParser::getHexString(uint32_t data) {
0303 char buffer[15];
0304 snprintf(buffer, sizeof(buffer), "0x%08x", (uint16_t)(data));
0305
0306 return std::string(buffer);
0307 }
0308
0309
0310
0311
0312
0313 std::string DCCTBDataParser::getIndexedData(uint32_t position, uint32_t *pointer) {
0314 std::string ret;
0315
0316
0317
0318
0319
0320
0321
0322 ret = index(position) + getHexString(*pointer);
0323
0324 return ret;
0325 }
0326
0327
0328
0329
0330
0331 DCCTBDataParser::~DCCTBDataParser() {
0332
0333 std::vector<DCCTBEventBlock *>::iterator it;
0334 for (it = dccEvents_.begin(); it != dccEvents_.end(); it++) {
0335 delete *it;
0336 }
0337 dccEvents_.clear();
0338
0339 delete mapper_;
0340 }