1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#include "DCCDataParser.h"
/*----------------------------------------------*/
/* DCCTBDataParser::DCCTBDataParser */
/* class constructor */
/*----------------------------------------------*/
DCCTBDataParser::DCCTBDataParser(const std::vector<uint32_t> &parserParameters, bool parseInternalData, bool debug)
: buffer_(nullptr), parseInternalData_(parseInternalData), debug_(debug), parameters(parserParameters) {
mapper_ = new DCCTBDataMapper(this); //build a new data mapper
resetErrorCounters(); //restart error counters
computeBlockSizes(); //calculate block sizes
}
/*----------------------------------------------*/
/* DCCTBDataParser::resetErrorCounters */
/* resets error counters */
/*----------------------------------------------*/
void DCCTBDataParser::resetErrorCounters() {
//set error counters to 0
errors_["DCC::BOE"] = 0; //begin of event (header B[60-63])
errors_["DCC::EOE"] = 0; //end of event (trailer B[60-63])
errors_["DCC::EVENT LENGTH"] = 0; //event length (trailer B[32-55])
}
/*----------------------------------------------*/
/* DCCTBDataParser::computeBlockSizes */
/* calculate the size of TCC and SR blocks */
/*----------------------------------------------*/
void DCCTBDataParser::computeBlockSizes() {
uint32_t nTT = numbTTs(); //gets the number of the trigger towers (default:68)
uint32_t tSamples = numbTriggerSamples(); //gets the number of trigger time samples (default: 1)
uint32_t nSr = numbSRF(); //gests the number of SR flags (default:68)
uint32_t tf(0), srf(0);
if ((nTT * tSamples) < 4 || (nTT * tSamples) % 4)
tf = 1; //test is there is no TTC primitives or if it's a multiple of 4?
else
tf = 0;
if (srf < 16 || srf % 16)
srf = 1; //??? by default srf=0 why do we make this test ?????
else
srf = 0;
//TTC block size: header (8 bytes) + 17 words with 4 trigger primitives (17*8bytes)
tccBlockSize_ = 8 + ((nTT * tSamples) / 4) * 8 + tf * 8;
//SR block size: header (8 bytes) + 4 words with 16 SR flags + 1 word with 4 SR flags (5*8bytes)
srpBlockSize_ = 8 + (nSr / 16) * 8 + srf * 8;
}
/*------------------------------------------------*/
/* DCCTBDataParser::parseFile */
/* reada data from file and parse it */
/*------------------------------------------------*/
void DCCTBDataParser::parseFile(std::string fileName, bool singleEvent) {
std::ifstream inputFile; //open file as input
inputFile.open(fileName.c_str());
resetErrorCounters(); //reset error counters
//for debug purposes
//std::cout << "Now in DCCTBDataParser::parseFile " << std::endl;
//if file opened correctly read data to a buffer and parse it
//else throw an exception
if (!inputFile.fail()) {
std::string myWord; //word read from line
std::vector<std::string> dataVector; //data vector
//until the end of file read each line as a string and add it to the data vector
while (inputFile >> myWord) {
dataVector.push_back(myWord);
}
bufferSize_ = (dataVector.size()) * 4; //buffer size in bytes (note:each char is an hex number)
uint32_t *myData = new uint32_t[dataVector.size()]; //allocate memory for a new data buffer
uint32_t *const myDataBeginning = myData; //pointer that stays at the beginning of the allocated memory
//fill buffer data with data from file lines
for (uint32_t i = 1; i <= dataVector.size(); i++, myData++) {
sscanf((dataVector[i - 1]).c_str(), "%x", myData);
//for debug purposes
//std::cout << std::endl << "Data position: " << dec << i << " val = " << getHexString(*myData);
}
inputFile.close(); //close file
parseBuffer(myData, bufferSize_, singleEvent); //parse data from the newly filled myData
delete[] myDataBeginning;
} else {
std::string errorMessage = std::string(" Error::Unable to open file :") + fileName;
throw ECALTBParserException(errorMessage);
}
}
/*----------------------------------------------------------*/
/* DCCTBDataParser::parseBuffer */
/* parse data from a buffer */
/*----------------------------------------------------------*/
void DCCTBDataParser::parseBuffer(const uint32_t *buffer, uint32_t bufferSize, bool singleEvent) {
resetErrorCounters(); //reset error counters
buffer_ = buffer; //set class buffer
//clear stored data
processedEvent_ = 0;
events_.clear();
std::vector<DCCTBEventBlock *>::iterator it;
for (it = dccEvents_.begin(); it != dccEvents_.end(); it++) {
delete *it;
}
dccEvents_.clear();
eventErrors_ = "";
//for debug purposes
//std::cout << std::endl << "Now in DCCTBDataParser::parseBuffer" << std::endl;
//std::cout << std::endl << "Buffer Size:" << dec << bufferSize << std::endl;
//check if we have a coherent buffer size
if (bufferSize % 8) {
std::string fatalError;
fatalError += "\n ======================================================================";
fatalError += "\n Fatal error at event = " + getDecString(events_.size() + 1);
fatalError += "\n Buffer Size of = " + getDecString(bufferSize) + "[bytes] is not divisible by 8 ... ";
fatalError += "\n ======================================================================";
throw ECALTBParserException(fatalError);
}
if (bufferSize < EMPTYEVENTSIZE) {
std::string fatalError;
fatalError += "\n ======================================================================";
fatalError += "\n Fatal error at event = " + getDecString(events_.size() + 1);
fatalError += "\n Buffer Size of = " + getDecString(bufferSize) + "[bytes] is less than an empty event ... ";
fatalError += "\n ======================================================================";
throw ECALTBParserException(fatalError);
}
const uint32_t *myPointer = buffer_;
// uint32_t processedBytes(0), wordIndex(0), lastEvIndex(0),eventSize(0), eventLength(0), errorMask(0);
uint32_t processedBytes(0), wordIndex(0), eventLength(0), errorMask(0);
//parse until there are no more events
while (processedBytes + EMPTYEVENTSIZE <= bufferSize) {
//for debug purposes
//std::cout << "-> processedBytes. = " << dec << processedBytes << std::endl;
//std::cout << " -> Processed Event index = " << dec << processedEvent_ << std::endl;
//std::cout << "-> First ev.word = 0x" << hex << (*myPointer) << std::endl;
//std::cout << "-> word index = " << dec << wordIndex << std::endl;
//check if Event Length is coherent /////////////////////////////////////////
uint32_t bytesToEnd = bufferSize - processedBytes;
std::pair<uint32_t, uint32_t> eventD = checkEventLength(myPointer, bytesToEnd, singleEvent);
eventLength = eventD.second;
errorMask = eventD.first;
//////////////////////////////////////////////////////////////////////////////
//for debug purposes
//std::cout <<" -> EventSizeBytes = " << dec << eventLength*8 << std::endl;
//for debug purposes debug
//std::cout<<std::endl;
//std::cout<<" out... Bytes To End.... = "<<dec<<bytesToEnd<<std::endl;
//std::cout<<" out... Processed Event = "<<dec<<processedEvent_<<std::endl;
//std::cout<<" out... Event Length = "<<dec<<eventLength<<std::endl;
//std::cout<<" out... LastWord = 0x"<<hex<<*(myPointer+eventLength*2-1)<<std::endl;
if (parseInternalData_) {
//build a new event block from buffer
DCCTBEventBlock *myBlock =
new DCCTBEventBlock(this, myPointer, eventLength * 8, eventLength * 2 - 1, wordIndex, 0);
//add event to dccEvents vector
dccEvents_.push_back(myBlock);
}
//build the event pointer with error mask and add it to the events vector
std::pair<const uint32_t *, uint32_t> eventPointer(myPointer, eventLength);
std::pair<uint32_t, std::pair<const uint32_t *, uint32_t> > eventPointerWithErrorMask(errorMask, eventPointer);
events_.push_back(eventPointerWithErrorMask);
//update processed buffer size
processedEvent_++;
processedBytes += eventLength * 8;
//std::cout << std::endl << "Processed Bytes = " << dec << processedBytes << std::endl;
//go to next event
myPointer += eventLength * 2;
wordIndex += eventLength * 2;
}
}
/*---------------------------------------------*/
/* DCCTBDataParser::checkEventLength */
/* check if event length is consistent with */
/* the words written in buffer */
/* returns a 3 bit error mask codified as: */
/* bit 1 - BOE error */
/* bit 2 - EVENT LENGTH error */
/* bit 3 - EOE Error */
/* and the event length */
/*---------------------------------------------*/
std::pair<uint32_t, uint32_t> DCCTBDataParser::checkEventLength(const uint32_t *pointerToEvent,
uint32_t bytesToEnd,
bool singleEvent) {
std::pair<uint32_t, uint32_t> result; //returns error mask and event length
uint32_t errorMask(0); //error mask to return
//check begin of event (BOE bits field)
//(Note: we have to add one to read the 2nd 32 bit word where BOE is written)
const uint32_t *boePointer = pointerToEvent + 1;
if ((((*boePointer) >> BOEBEGIN) & BOEMASK) != BOE) {
(errors_["DCC::BOE"])++;
errorMask = 1;
}
//get Event Length from buffer (Note: we have to add two to read the 3rd 32 bit word where EVENT LENGTH is written)
const uint32_t *myPointer = pointerToEvent + 2;
uint32_t eventLength = (*myPointer) & EVENTLENGTHMASK;
// std::cout << " Event Length(from decoding) = " << dec << eventLength << "... bytes to end... " << bytesToEnd << ", event numb : " << processedEvent_ << std::endl;
bool eoeError = false;
//check if event is empty but but EVENT LENGTH is not corresponding to it
if (singleEvent && eventLength != bytesToEnd / 8) {
eventLength = bytesToEnd / 8;
(errors_["DCC::EVENT LENGTH"])++;
errorMask = errorMask | (1 << 1);
}
//check if event length mismatches the number of words written as data
else if (eventLength == 0 || eventLength > (bytesToEnd / 8) || eventLength < (EMPTYEVENTSIZE / 8)) {
// How to handle bad event length in multiple event buffers
// First approach : Send an exception
// Second aproach : Try to find the EOE (To be done? If yes check dataDecoder tBeam implementation)
std::string fatalError;
fatalError += "\n ======================================================================";
fatalError += "\n Fatal error at event = " + getDecString(events_.size() + 1);
fatalError += "\n Decoded event length = " + getDecString(eventLength);
fatalError += "\n bytes to buffer end = " + getDecString(bytesToEnd);
fatalError += "\n Unable to procead the data decoding ...";
if (eventLength > (bytesToEnd / 8)) {
fatalError += " (eventLength > (bytesToEnd / 8)";
} else {
fatalError += "\n event length not big enough heaven to build an empty event ( 4x8 bytes)";
}
fatalError += "\n ======================================================================";
throw ECALTBParserException(fatalError);
}
//check end of event (EOE bits field)
//(Note: event length is multiplied by 2 because its written as 32 bit words and not 64 bit words)
const uint32_t *endOfEventPointer = pointerToEvent + eventLength * 2 - 1;
if ((((*endOfEventPointer) >> EOEBEGIN & EOEMASK) != EOEMASK) && !eoeError) {
(errors_["DCC::EOE"])++;
errorMask = errorMask | (1 << 2);
}
//build result to return
result.first = errorMask;
result.second = eventLength;
return result;
}
/*----------------------------------------------*/
/* DCCTBDataParser::index */
/* build an index string */
/*----------------------------------------------*/
std::string DCCTBDataParser::index(uint32_t position) {
char indexBuffer[20];
long unsigned int pos = position;
snprintf(
indexBuffer, sizeof(indexBuffer), "W[%08lu]", pos); //build an index string for display purposes, p.e. W[15]
return std::string(indexBuffer);
}
/*-----------------------------------------------*/
/* DCCTBDataParser::getDecString */
/* print decimal data to a string */
/*-----------------------------------------------*/
std::string DCCTBDataParser::getDecString(uint32_t dat) {
char buffer[15];
long unsigned int data = dat;
snprintf(buffer, sizeof(buffer), "%lu", data);
return std::string(buffer);
}
/*-------------------------------------------------*/
/* DCCTBDataParser::getHexString */
/* print data in hexadecimal base to a string */
/*-------------------------------------------------*/
std::string DCCTBDataParser::getHexString(uint32_t data) {
char buffer[15];
snprintf(buffer, sizeof(buffer), "0x%08x", (uint16_t)(data));
return std::string(buffer);
}
/*------------------------------------------------*/
/* DCCTBDataParser::getIndexedData */
/* build a string with index and data */
/*------------------------------------------------*/
std::string DCCTBDataParser::getIndexedData(uint32_t position, uint32_t *pointer) {
std::string ret;
//char indexBuffer[20];
//char dataBuffer[20];
//sprintf(indexBuffer,"W[%08u] = ",position);
//sprintf(dataBuffer,"0x%08x",*pointer);
//ret = std::string(indexBuffer)+std::string(dataBuffer);
ret = index(position) + getHexString(*pointer);
return ret;
}
/*-------------------------------------------------*/
/* DCCTBDataParser::~DCCTBDataParser */
/* destructor */
/*-------------------------------------------------*/
DCCTBDataParser::~DCCTBDataParser() {
// delete DCCTBEvents if any...
std::vector<DCCTBEventBlock *>::iterator it;
for (it = dccEvents_.begin(); it != dccEvents_.end(); it++) {
delete *it;
}
dccEvents_.clear();
delete mapper_;
}
|