CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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(std::vector<uint32_t> parserParameters, bool parseInternalData,bool debug):
10  buffer_(0),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 /*------------------------------------------------*/
61 void DCCTBDataParser::parseFile(std::string fileName, bool singleEvent){
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  if( buffer_ ){ delete [] buffer_; } //delete current vector if any
86  buffer_ = new uint32_t[dataVector.size()]; //allocate memory for new data buffer
87 
88  uint32_t *myData_ = (uint32_t *) buffer_;
89 
90  //fill buffer data with data from file lines
91  for(uint32_t i = 1; i <= dataVector.size() ; i++, myData_++ ){
92  sscanf((dataVector[i-1]).c_str(),"%x",(unsigned int *)myData_);
93 
94  //for debug purposes
95  //std::cout << std::endl << "Data position: " << dec << i << " val = " << getHexString(*myData_);
96  }
97 
98  inputFile.close(); //close file
99 
100  parseBuffer( buffer_,bufferSize_,singleEvent); //parse data from buffer
101 
102  }
103  else{
104  std::string errorMessage = std::string(" Error::Unable to open file :") + fileName;
105  throw ECALTBParserException(errorMessage);
106  }
107 }
108 
109 
110 /*----------------------------------------------------------*/
111 /* DCCTBDataParser::parseBuffer */
112 /* parse data from a buffer */
113 /*----------------------------------------------------------*/
114 void DCCTBDataParser::parseBuffer(uint32_t * buffer, uint32_t bufferSize, bool singleEvent){
115 
116  resetErrorCounters(); //reset error counters
117 
118  buffer_ = buffer; //set class buffer
119 
120  //clear stored data
121  processedEvent_ = 0;
122  events_.clear();
123  std::vector<DCCTBEventBlock *>::iterator it;
124  for( it = dccEvents_.begin(); it!=dccEvents_.end(); it++ ) { delete *it; }
125  dccEvents_.clear();
126  eventErrors_ = "";
127 
128  //for debug purposes
129  //std::cout << std::endl << "Now in DCCTBDataParser::parseBuffer" << std::endl;
130  //std::cout << std::endl << "Buffer Size:" << dec << bufferSize << std::endl;
131 
132  //check if we have a coherent buffer size
133  if( bufferSize%8 ){
134  std::string fatalError ;
135  fatalError += "\n ======================================================================";
136  fatalError += "\n Fatal error at event = " + getDecString(events_.size()+1);
137  fatalError += "\n Buffer Size of = "+ getDecString(bufferSize) + "[bytes] is not divisible by 8 ... ";
138  fatalError += "\n ======================================================================";
139  throw ECALTBParserException(fatalError);
140  }
141  if ( bufferSize < EMPTYEVENTSIZE ){
142  std::string fatalError ;
143  fatalError += "\n ======================================================================";
144  fatalError += "\n Fatal error at event = " + getDecString(events_.size()+1);
145  fatalError += "\n Buffer Size of = "+ getDecString(bufferSize) + "[bytes] is less than an empty event ... ";
146  fatalError += "\n ======================================================================";
147  throw ECALTBParserException(fatalError);
148  }
149 
150  uint32_t *myPointer = buffer_;
151 
152  // uint32_t processedBytes(0), wordIndex(0), lastEvIndex(0),eventSize(0), eventLength(0), errorMask(0);
153  uint32_t processedBytes(0), wordIndex(0), eventLength(0), errorMask(0);
154 
155  //parse until there are no more events
156  while( processedBytes + EMPTYEVENTSIZE <= bufferSize ){
157 
158  //for debug purposes
159  //std::cout << "-> processedBytes. = " << dec << processedBytes << std::endl;
160  //std::cout << " -> Processed Event index = " << dec << processedEvent_ << std::endl;
161  //std::cout << "-> First ev.word = 0x" << hex << (*myPointer) << std::endl;
162  //std::cout << "-> word index = " << dec << wordIndex << std::endl;
163 
164  //check if Event Length is coherent /////////////////////////////////////////
165  uint32_t bytesToEnd = bufferSize - processedBytes;
166  std::pair<uint32_t,uint32_t> eventD = checkEventLength(myPointer,bytesToEnd,singleEvent);
167  eventLength = eventD.second;
168  errorMask = eventD.first;
170 
171  //for debug purposes
172  //std::cout <<" -> EventSizeBytes = " << dec << eventLength*8 << std::endl;
173 
174 
175 
176  //for debug purposes debug
177  //std::cout<<std::endl;
178  //std::cout<<" out... Bytes To End.... = "<<dec<<bytesToEnd<<std::endl;
179  //std::cout<<" out... Processed Event = "<<dec<<processedEvent_<<std::endl;
180  //std::cout<<" out... Event Length = "<<dec<<eventLength<<std::endl;
181  //std::cout<<" out... LastWord = 0x"<<hex<<*(myPointer+eventLength*2-1)<<std::endl;
182 
183  if (parseInternalData_){
184  //build a new event block from buffer
185  DCCTBEventBlock *myBlock = new DCCTBEventBlock(this,myPointer,eventLength*8, eventLength*2 -1 ,wordIndex,0);
186 
187  //add event to dccEvents vector
188  dccEvents_.push_back(myBlock);
189  }
190 
191  //build the event pointer with error mask and add it to the events vector
192  std::pair<uint32_t *, uint32_t> eventPointer(myPointer,eventLength);
193  std::pair<uint32_t, std::pair<uint32_t*, uint32_t> > eventPointerWithErrorMask(errorMask,eventPointer);
194  events_.push_back(eventPointerWithErrorMask);
195 
196  //update processed buffer size
197  processedEvent_++;
198  processedBytes += eventLength*8;
199  //std::cout << std::endl << "Processed Bytes = " << dec << processedBytes << std::endl;
200 
201  //go to next event
202  myPointer += eventLength*2;
203  wordIndex += eventLength*2;
204  }
205 }
206 
207 
208 /*---------------------------------------------*/
209 /* DCCTBDataParser::checkEventLength */
210 /* check if event length is consistent with */
211 /* the words written in buffer */
212 /* returns a 3 bit error mask codified as: */
213 /* bit 1 - BOE error */
214 /* bit 2 - EVENT LENGTH error */
215 /* bit 3 - EOE Error */
216 /* and the event length */
217 /*---------------------------------------------*/
218 std::pair<uint32_t,uint32_t> DCCTBDataParser::checkEventLength(uint32_t *pointerToEvent, uint32_t bytesToEnd, bool singleEvent){
219 
220  std::pair<uint32_t,uint32_t> result; //returns error mask and event length
221  uint32_t errorMask(0); //error mask to return
222 
223  //check begin of event (BOE bits field)
224  //(Note: we have to add one to read the 2nd 32 bit word where BOE is written)
225  uint32_t *boePointer = pointerToEvent + 1;
226  if( ( ((*boePointer)>>BOEBEGIN)& BOEMASK ) != BOE ) {
227  (errors_["DCC::BOE"])++; errorMask = 1;
228  }
229 
230 
231  //get Event Length from buffer (Note: we have to add two to read the 3rd 32 bit word where EVENT LENGTH is written)
232  uint32_t * myPointer = pointerToEvent + 2;
233  uint32_t eventLength = (*myPointer)&EVENTLENGTHMASK;
234 
235  // std::cout << " Event Length(from decoding) = " << dec << eventLength << "... bytes to end... " << bytesToEnd << ", event numb : " << processedEvent_ << std::endl;
236 
237  bool eoeError = false;
238 
239  //check if event is empty but but EVENT LENGTH is not corresponding to it
240  if( singleEvent && eventLength != bytesToEnd/8 ){
241  eventLength = bytesToEnd/8;
242  (errors_["DCC::EVENT LENGTH"])++;
243  errorMask = errorMask | (1<<1);
244  }
245  //check if event length mismatches the number of words written as data
246  else if( eventLength == 0 || eventLength > (bytesToEnd / 8) || eventLength < (EMPTYEVENTSIZE/8) ){
247  // How to handle bad event length in multiple event buffers
248  // First approach : Send an exception
249  // Second aproach : Try to find the EOE (To be done? If yes check dataDecoder tBeam implementation)
250  std::string fatalError;
251 
252  fatalError +="\n ======================================================================";
253  fatalError +="\n Fatal error at event = " + getDecString(events_.size()+1);
254  fatalError +="\n Decoded event length = " + getDecString(eventLength);
255  fatalError +="\n bytes to buffer end = " + getDecString(bytesToEnd);
256  fatalError +="\n Unable to procead the data decoding ...";
257 
258  if(eventLength > (bytesToEnd / 8)){ fatalError +=" (eventLength > (bytesToEnd / 8)";}
259  else{ fatalError += "\n event length not big enough heaven to build an empty event ( 4x8 bytes)";}
260 
261  fatalError +="\n ======================================================================";
262 
263  throw ECALTBParserException(fatalError);
264  }
265 
266  //check end of event (EOE bits field)
267  //(Note: event length is multiplied by 2 because its written as 32 bit words and not 64 bit words)
268  uint32_t *endOfEventPointer = pointerToEvent + eventLength*2 -1;
269  if ( ( ((*endOfEventPointer) >> EOEBEGIN & EOEMASK ) != EOEMASK) && !eoeError ){
270  (errors_["DCC::EOE"])++;
271  errorMask = errorMask | (1<<2);
272  }
273 
274  //build result to return
275  result.first = errorMask;
276  result.second = eventLength;
277 
278  return result;
279 }
280 
281 
282 
283 /*----------------------------------------------*/
284 /* DCCTBDataParser::index */
285 /* build an index string */
286 /*----------------------------------------------*/
287 std::string DCCTBDataParser::index(uint32_t position){
288 
289  char indexBuffer[20];
290  long unsigned int pos = position;
291  sprintf(indexBuffer,"W[%08lu]",pos); //build an index string for display purposes, p.e. W[15]
292 
293  return std::string(indexBuffer);
294 }
295 
296 
297 /*-----------------------------------------------*/
298 /* DCCTBDataParser::getDecString */
299 /* print decimal data to a string */
300 /*-----------------------------------------------*/
301 std::string DCCTBDataParser::getDecString(uint32_t dat){
302 
303  char buffer[10];
304  long unsigned int data = dat;
305  sprintf(buffer,"%lu",data);
306 
307  return std::string(buffer);
308 }
309 
310 
311 /*-------------------------------------------------*/
312 /* DCCTBDataParser::getHexString */
313 /* print data in hexadecimal base to a string */
314 /*-------------------------------------------------*/
315 std::string DCCTBDataParser::getHexString(uint32_t data){
316 
317  char buffer[10];
318  sprintf(buffer,"0x%08x",(uint16_t)(data));
319 
320  return std::string(buffer);
321 }
322 
323 
324 /*------------------------------------------------*/
325 /* DCCTBDataParser::getIndexedData */
326 /* build a string with index and data */
327 /*------------------------------------------------*/
328 std::string DCCTBDataParser::getIndexedData(uint32_t position, uint32_t *pointer){
329  std::string ret;
330 
331  //char indexBuffer[20];
332  //char dataBuffer[20];
333  //sprintf(indexBuffer,"W[%08u] = ",position);
334  //sprintf(dataBuffer,"0x%08x",*pointer);
335  //ret = std::string(indexBuffer)+std::string(dataBuffer);
336 
337  ret = index(position) + getHexString(*pointer);
338 
339  return ret;
340 }
341 
342 
343 /*-------------------------------------------------*/
344 /* DCCTBDataParser::~DCCTBDataParser */
345 /* destructor */
346 /*-------------------------------------------------*/
348 
349  // delete DCCTBEvents if any...
350  std::vector<DCCTBEventBlock *>::iterator it;
351  for(it=dccEvents_.begin();it!=dccEvents_.end();it++){delete *it;}
352  dccEvents_.clear();
353 
354  delete mapper_;
355 }
std::vector< DCCTBEventBlock * > dccEvents_
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
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)
uint32_t tccBlockSize_
uint32_t numbTriggerSamples()
std::vector< std::pair< uint32_t, std::pair< uint32_t *, uint32_t > > > events_
#define nTT
Definition: TMEGeom.h:6
tuple result
Definition: query.py:137
uint32_t srpBlockSize_
void parseFile(std::string fileName, bool singleEvent=false)
std::pair< uint32_t, uint32_t > checkEventLength(uint32_t *pointerToEvent, uint32_t bytesToEnd, bool singleEvent=false)
uint32_t processedEvent_
uint32_t numbTTs()
DCCTBDataParser(std::vector< uint32_t > parserParameters, bool parseInternalData=true, bool debug=true)
Definition: DCCDataParser.cc:9
uint32_t numbSRF()
std::string getHexString(uint32_t data)
void parseBuffer(uint32_t *buffer, uint32_t bufferSize, bool singleEvent=false)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static int position[264][3]
Definition: ReadPGInfo.cc:509
std::string eventErrors_
DCCTBDataMapper * mapper_
uint32_t bufferSize_
#define debug
Definition: MEtoEDMFormat.h:34
std::string index(uint32_t position)
uint32_t * buffer_