CMS 3D CMS Logo

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