RawEventV0.cpp
Go to the documentation of this file.
1 #include "DAQDataFormats/RawEventV0.h"
2 #include "DAQDataFormats/RawEventHeader.h"
3 #include "DAQDataFormats/RawEventTail.h"
4 #include "DAQDataFormats/RawDataBlock.h"
5 #include "DAQDataFormats/RawTrigger.h"
6 #include "DAQDataFormats/RawEvent.h"
7 
8 #include <boost/format.hpp>
9 /// It is requred to
10 /// 1. include DAQDataFormats/DataFormatFirst.inc
11 /// 2. define CURRENT_CLASS
12 /// 3. define CURRENT_CLASS_VERSION for version of the class
13 /// OR define LAST_CLASS_VERSION for the main structure of the class
14 /// 4. (optional) define CURRENT_FIELDS_SUB_NAMESPACE
15 /// 5. include DAQDataFormats/DataFormatLast.inc
16 /// before the declaration of the DataFormat version class in the header.
17 #include "DAQDataFormats/DataFormatFirst.inc"
18 #define CURRENT_CLASS RawEvent
19 #define CURRENT_CLASS_VERSION 0
20 #define CURRENT_FIELDS_SUB_NAMESPACE rawevent
21 #include "DAQDataFormats/DataFormatLast.inc"
22 
23 namespace daqdataformats{
24 namespace VERSION_NAMESPACE{
25 
26 //------------------------------------------------------------------------------
27 // Default constructor
29  RawDAQData(version, true, (sizeofdata_t) &RawEvent::getPredefinedSize)
30  ,ZEROOUT_FUNCTION(addDataBlock )
31  ,ZEROOUT_FUNCTION(addDataBlockWithSize )
33  ,ZEROOUT_FUNCTION(setTrigger1 )
34  ,ZEROOUT_FUNCTION(setTrigger2 )
35  ,ZEROOUT_FUNCTION(setFloatingDataBlockToBufferSource)
36  ,ZEROOUT_FUNCTION(nextEvent)
37  ,ZEROOUT_FUNCTION(setAppropriateMCFlag)
38  ,ZEROOUT_FUNCTION(getHeader )
39  ,ZEROOUT_FUNCTION(getTail )
40  ,ZEROOUT_FUNCTION(getTrigger )
41  ,ZEROOUT_FUNCTION(getFloatingDataBlock )
42  ,ZEROOUT_FUNCTION(getHeaderConst )
43  ,ZEROOUT_FUNCTION(getTailConst )
44  ,ZEROOUT_FUNCTION(getTriggerConst )
45  ,ZEROOUT_FUNCTION(getFloatingDataBlockConst )
46  ,ZEROOUT_FUNCTION(getHeaderMarkerLeft )
47  ,ZEROOUT_FUNCTION(getHeaderMarkerRight )
48  ,ZEROOUT_FUNCTION(getVersion )
49  ,ZEROOUT_FUNCTION(getDataLoggerID )
50  ,ZEROOUT_FUNCTION(getRunNumber )
51  ,ZEROOUT_FUNCTION(getRunType )
52  ,ZEROOUT_FUNCTION(getConfigIDX )
53  ,ZEROOUT_FUNCTION(getPartition )
54  ,ZEROOUT_FUNCTION(getSubrun )
55  ,ZEROOUT_FUNCTION(getEventNumberLo )
56  ,ZEROOUT_FUNCTION(getEventNumberHi )
57  ,ZEROOUT_FUNCTION(getEventNumber )
58  ,ZEROOUT_FUNCTION(getEventType )
59  ,ZEROOUT_FUNCTION(getEventSize )
60  ,ZEROOUT_FUNCTION(getDataBlockNumber )
61  ,ZEROOUT_FUNCTION(isEventIncomplete )
62  ,ZEROOUT_FUNCTION(getMonteCarloFlag )
63  ,ZEROOUT_FUNCTION(isCRCCalculationUsed )
64  ,ZEROOUT_FUNCTION(setTriggerPointer )
65  ,ZEROOUT_FUNCTION(setDataLoggerID )
66  ,ZEROOUT_FUNCTION(setRunNumber )
67  ,ZEROOUT_FUNCTION(setRunType )
68  ,ZEROOUT_FUNCTION(setConfigIDX )
69  ,ZEROOUT_FUNCTION(setPartition )
70  ,ZEROOUT_FUNCTION(setSubrun )
71  ,ZEROOUT_FUNCTION(setEventNumberLo )
72  ,ZEROOUT_FUNCTION(setEventNumberHi )
73  ,ZEROOUT_FUNCTION(setEventNumber )
74  ,ZEROOUT_FUNCTION(setEventType )
75  ,ZEROOUT_FUNCTION(setEventIncomplete )
76  ,ZEROOUT_FUNCTION(setMonteCarloFlag )
77  ,ZEROOUT_FUNCTION(setFloatingDataBlock )
78  ,_Header (NULL)
79  ,_Tail (NULL)
80  ,_Trigger (NULL)
81  ,_FloatingDataBlock (NULL)
82  ,_FloatingDataBlockNumber (-1)
83  ,_triggerVersion (rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)
84  ,_datablockVersion (rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)
85  ,_mcFlag (rawevent::MC_FLAG_UNKNOWN)
86  ,_mcFlagBool (false)
87  ,_tailSize (getTailSize(version))
88 { }
89 
90 //------------------------------------------------------------------------------
92  RawDAQData(copyin)
93  ,DEEPCOPY_FUNCTION(addDataBlock )
94  ,DEEPCOPY_FUNCTION(addDataBlockWithSize )
96  ,DEEPCOPY_FUNCTION(setTrigger1 )
98  ,DEEPCOPY_FUNCTION(setFloatingDataBlockToBufferSource)
100  ,DEEPCOPY_FUNCTION(setAppropriateMCFlag)
101  ,DEEPCOPY_FUNCTION(getHeader )
102  ,DEEPCOPY_FUNCTION(getTail )
103  ,DEEPCOPY_FUNCTION(getTrigger )
104  ,DEEPCOPY_FUNCTION(getFloatingDataBlock )
105  ,DEEPCOPY_FUNCTION(getHeaderConst )
106  ,DEEPCOPY_FUNCTION(getTailConst )
107  ,DEEPCOPY_FUNCTION(getTriggerConst )
108  ,DEEPCOPY_FUNCTION(getFloatingDataBlockConst )
109  ,DEEPCOPY_FUNCTION(getHeaderMarkerLeft )
110  ,DEEPCOPY_FUNCTION(getHeaderMarkerRight )
111  ,DEEPCOPY_FUNCTION(getVersion )
112  ,DEEPCOPY_FUNCTION(getDataLoggerID )
113  ,DEEPCOPY_FUNCTION(getRunNumber )
114  ,DEEPCOPY_FUNCTION(getRunType )
115  ,DEEPCOPY_FUNCTION(getConfigIDX )
116  ,DEEPCOPY_FUNCTION(getPartition )
117  ,DEEPCOPY_FUNCTION(getSubrun )
118  ,DEEPCOPY_FUNCTION(getEventNumberLo )
119  ,DEEPCOPY_FUNCTION(getEventNumberHi )
120  ,DEEPCOPY_FUNCTION(getEventNumber )
121  ,DEEPCOPY_FUNCTION(getEventType )
122  ,DEEPCOPY_FUNCTION(getEventSize )
123  ,DEEPCOPY_FUNCTION(getDataBlockNumber )
124  ,DEEPCOPY_FUNCTION(isEventIncomplete )
125  ,DEEPCOPY_FUNCTION(getMonteCarloFlag )
126  ,DEEPCOPY_FUNCTION(isCRCCalculationUsed )
127  ,DEEPCOPY_FUNCTION(setTriggerPointer )
128  ,DEEPCOPY_FUNCTION(setDataLoggerID )
129  ,DEEPCOPY_FUNCTION(setRunNumber )
130  ,DEEPCOPY_FUNCTION(setRunType )
131  ,DEEPCOPY_FUNCTION(setConfigIDX )
132  ,DEEPCOPY_FUNCTION(setPartition )
133  ,DEEPCOPY_FUNCTION(setSubrun )
134  ,DEEPCOPY_FUNCTION(setEventNumberLo )
135  ,DEEPCOPY_FUNCTION(setEventNumberHi )
136  ,DEEPCOPY_FUNCTION(setEventNumber )
137  ,DEEPCOPY_FUNCTION(setEventType )
138  ,DEEPCOPY_FUNCTION(setEventIncomplete )
139  ,DEEPCOPY_FUNCTION(setMonteCarloFlag )
140  ,DEEPCOPY_FUNCTION(setFloatingDataBlock )
141  ,_Header (NULL)
142  ,_Tail (NULL)
143  ,_Trigger (NULL)
144  ,_FloatingDataBlock (NULL)
148  ,_mcFlag (copyin._mcFlag)
149  ,_mcFlagBool (copyin._mcFlagBool)
150  ,_tailSize (copyin._tailSize)
151 { }
152 
153 //------------------------------------------------------------------------------
154 // Destructor
156  if(_Header) delete _Header;
158  if(_Tail) delete _Tail;
159  if(_Trigger) delete _Trigger;
160 }
161 
162 
163 //------------------------------------------------------------------------------
164 // Initialization of Data members
166 
167  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" Header size:"<<_Header->sizeofdata());
168 
169  /// Header + CRC
170  uint32_t size_of_data = _Header->sizeofdata() + 1;
171 
172  if(_Trigger){
173 
174  size_of_data += _Trigger->sizeofdata();
175 
176  if(_shouldBufferBeInternal){
177  resizeInternalBuffer(size_of_data);
178  /// Check pointers after each read or resize buffer
179  checkBufferInternalness();
180  }
181 
182  _Trigger->init();
183  }
184  else {
185 
186  if(_shouldBufferBeInternal){
187  resizeInternalBuffer(size_of_data);
188  /// Check pointers after each read or resize buffer
189  checkBufferInternalness();
190  }
191  }
192 
193  if(!_Tail) _Tail = new daqdataformats::RawEventTail(_Header->getTailVersion());
194 
195  _Header->init();
196  _Header->setEventSize(size_of_data);
197 
198  // Initialize the _FloatingDataBlockNumber
200 }
201 
202 //------------------------------------------------------------------------------
203 // Add a MicroSlice of a known size into the RawEvent changing the RawEvent Header appropriately
204 // The method should be identical to RawMicroSlice::readData() (except for not reading the MicroSlice Timing marker)
205 // If check_mc_flag = true, the method checks whether the DataBlock is MC generated
206 // and adds an appropriate flag to the Event Header.
207 bool RawEvent::addDataBlock(const void* datablock_buffer){
208  const daqdataformats::RawDataBlockHeader temp_datablock_header(datablock_buffer, _datablockVersion);
209  RETURN_FUNCTION(addDataBlockWithSize)(datablock_buffer, temp_datablock_header.getDataSize());
210 }
211 
212 //------------------------------------------------------------------------------
213 // Add a MicroSlice of a known size into the RawEvent changing the RawEvent Header appropriately
214 // The method should be identical to RawMicroSlice::readData() (except for not reading the MicroSlice Timing marker)
215 // If check_mc_flag = true, the method checks whether the DataBlock is MC generated
216 // and adds an appropriate flag to the Event Header.
217 bool RawEvent::addDataBlockWithSize(const void* datablock_buffer, const uint32_t datablock_size){
218 
219  const uint32_t current_size = EXECUTE_FUNCTION(sizeofdata)(); //remember current buffer position
220 
221  //PRINT_ON_DEBUG("DataBlock size="<<datablock_size)
222  //EXECUTE_ON_DEBUG(printHex(_Buffer, current_size);)
223 
224  //PRINT_ON_DEBUG("DataBlock size="<<datablock_size)
225 
226  //EXECUTE_ON_DEBUG(printHex(datablock_buffer, datablock_size);)
227 
228 
229  // Setup the RawEventHeader Header
230  // Advance the RawEvent DataBlock number and the size
231  _Header->addEventSize(datablock_size);
232  _Header->advanceDataBlockNumber();
233 
234  /// Add the DataBlock the RawEvent _Buffer starting from the last _Buffer size position - 1
235  /// -1 because we reserve one word to CRC
236  readDataGeneral(datablock_buffer, datablock_size, current_size - 1);
237 
238  /// Check pointers after each read or resize buffer
239  checkBufferInternalness();
240 
241  //PRINT_ON_DEBUG("Final size"<<sizeofdata())
242  //EXECUTE_ON_DEBUG(printHex(_Buffer, sizeofdata());)
243 
244  RETURN_FUNCTION(setAppropriateMCFlag)(datablock_buffer);
245 }
246 
247 //------------------------------------------------------------------------------
248 bool RawEvent::setAppropriateMCFlag(const void* datablock_buffer){
249 
250  // Check if the DataBlock is the MC generated
251  // If it is, set the appropriate MicroSlice MC flag
253  daqdataformats::RawDataBlock temp_datablock(datablock_buffer, _datablockVersion);
254  _mcFlagBool = temp_datablock.getMonteCarloFlag();
255  _mcFlag = 1;
256  _datablockVersion = temp_datablock.getVersion();
257  }
258 
259  return setMonteCarloFlag(_mcFlagBool);
260 }
261 
262 //------------------------------------------------------------------------------
263 // Usually just packs the Header leaving the rest of the _Buffer unchanged.
264 // Also, puts the Header in the tail as well. Puts CRC in the very end.
265 bool RawEvent::close(const bool produce_crc){
266 
267  const uint32_t old_event_size = EXECUTE_FUNCTION(sizeofdata)();
268 
269  /// Add the size of the tail
270  const uint32_t new_event_size = old_event_size + _Tail->sizeofdata();
271 
272  _Header->setEventSize(new_event_size);
273 
274  /// Resize the internal event buffer by adding Tail + CRC
275  if(_shouldBufferBeInternal){
276  resizeInternalBuffer(new_event_size);
277  /// Check pointers after each read or resize buffer
278  checkBufferInternalness();
279  }
280 
281  _Tail->setBufferSource(&RAWBUFF32[old_event_size - 1]);
282 
283  _Tail->init();
284  _Tail->setEventNumberLo(_Header->getEventNumberLo());
285  _Tail->setEventNumberHi(_Header->getEventNumberHi());
286  _Tail->setEventSize (_Header->getEventSize());
287 
288  /// Finally, add CRC in the very end of _Buffer
289  if(produce_crc){
290  _Header->setCRCCalculationUsed(true);
291  addCheckSum();
292  }
293 
294  return true;
295 }
296 
297 //------------------------------------------------------------------------------
298 //Getters
299 daqdataformats::RawEventTail* RawEvent::getTail() {
300  uint32_t* pointer = &RAWBUFF32[sizeofdata() - _Tail->sizeofdata() - 1];
301  _Tail->setBufferSource(pointer);
302  return _Tail;
303 }
304 daqdataformats::RawEventTail* RawEvent::getTailConst() const {
305  uint32_t* pointer = &RAWBUFF32[sizeofdata() - _Tail->sizeofdata() - 1];
306  _Tail->setBufferSource(pointer);
307  return _Tail;
308 }
309 
310 //------------------------------------------------------------------------------
311 daqdataformats::RawEventHeader* RawEvent::getHeader() const { return _Header; }
312 daqdataformats::RawTrigger* RawEvent::getTrigger() { return _Trigger; }
313 daqdataformats::RawDataBlock* RawEvent::getFloatingDataBlock() { return _FloatingDataBlock; }
314 
315 daqdataformats::RawEventHeader* RawEvent::getHeaderConst() const { return _Header; }
316 daqdataformats::RawTrigger* RawEvent::getTriggerConst() const { return _Trigger; }
317 daqdataformats::RawDataBlock* RawEvent::getFloatingDataBlockConst() const { return _FloatingDataBlock; }
318 
319 uint32_t RawEvent::getHeaderMarkerLeft() const { return _Header->getMarkerLeft();}
320 uint32_t RawEvent::getHeaderMarkerRight() const { return _Header->getMarkerRight();}
321 uint32_t RawEvent::getVersion() const { return _Header->getVersion();}
322 uint32_t RawEvent::getDataLoggerID() const { return _Header->getDataLoggerID();}
323 uint32_t RawEvent::getRunNumber() const { return _Header->getRunNumber();}
324 uint32_t RawEvent::getRunType() const { return _Header->getRunType();}
325 uint32_t RawEvent::getConfigIDX() const { return _Header->getConfigIDX();}
326 uint32_t RawEvent::getPartition() const { return _Header->getPartition();}
327 uint32_t RawEvent::getSubrun() const { return _Header->getSubrun();}
328 uint32_t RawEvent::getEventNumberLo() const { return _Header->getEventNumberLo();}
329 uint32_t RawEvent::getEventNumberHi() const { return _Header->getEventNumberHi();}
330 uint32_t RawEvent::getEventType() const { return _Header->getEventType();}
331 uint32_t RawEvent::getEventSize() const { return _Header->getEventSize();}
332 uint32_t RawEvent::getDataBlockNumber() const { return _Header->getDataBlockNumber();}
333 bool RawEvent::isEventIncomplete() const { return _Header->isEventIncomplete();}
334 bool RawEvent::getMonteCarloFlag() const { return _Header->getMonteCarloFlag();}
335 bool RawEvent::isCRCCalculationUsed() const { return _Header->isCRCCalculationUsed();}
336 uint64_t RawEvent::getEventNumber() const { return _Header->getEventNumber();}
337 
338 
339 //------------------------------------------------------------------------------
340 // Setters
341 // Different setters for the trigger
342 bool RawEvent::setTrigger1 (const daqdataformats::RawTrigger& trigger){ RETURN_FUNCTION(setTrigger2)(trigger.getBuffer());}
343 bool RawEvent::setTrigger2 (const void* pointer) {
344 
345  EXECUTE_FUNCTION(setTriggerPointer)(pointer);
346 
347  /// New Trigger, so resize the Event Size
348  _Header->setEventSize(_Header->sizeofdata() + _Trigger->sizeofdata() + 1);
349 
350  if(_shouldBufferBeInternal){
351  resizeInternalBuffer(EXECUTE_FUNCTION(sizeofdata)());
352  /// Check pointers after each read or resize buffer
353  checkBufferInternalness();
354  }
355 
356  /// read Trigger
357  readDataGeneral(pointer, _Trigger->sizeofdata(), _Header->sizeofdata());
358 
359  EXECUTE_FUNCTION(setTriggerPointer)(&(RAWBUFF32[_Header->sizeofdata()]));
360 
361  return true;
362 }
363 
364 //------------------------------------------------------------------------------
365 bool RawEvent::setTriggerPointer(const void* pointer){
366 
367  if(_Trigger){
368  _Trigger->setBufferSource(pointer);
369  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<"0");
370  }
371  else{
372  _triggerVersion = daqdataformats::RawTrigger::figureOutTheVersionStatic(pointer);
373  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<"1"<<"version = "<<_triggerVersion);
375  }
376 
377  return true;
378 }
379 
380 //------------------------------------------------------------------------------
381 bool RawEvent::setDataLoggerID (const uint32_t inp) {return _Header->setDataLoggerID (inp);}
382 bool RawEvent::setRunNumber (const uint32_t inp) {return _Header->setRunNumber (inp);}
383 bool RawEvent::setRunType (const uint32_t inp) {return _Header->setRunType (inp);}
384 bool RawEvent::setConfigIDX (const uint32_t inp) {return _Header->setConfigIDX (inp);}
385 bool RawEvent::setPartition (const uint32_t inp) {return _Header->setPartition (inp);}
386 bool RawEvent::setSubrun (const uint32_t inp) {return _Header->setSubrun (inp);}
387 bool RawEvent::setEventNumberLo (const uint32_t inp) {return _Header->setEventNumberLo (inp);}
388 bool RawEvent::setEventNumberHi (const uint32_t inp) {return _Header->setEventNumberHi (inp);}
389 bool RawEvent::setEventType (const uint32_t inp) {return _Header->setEventType (inp);}
390 bool RawEvent::setEventIncomplete(const bool tf) {return _Header->setEventIncomplete(tf);}
391 bool RawEvent::setMonteCarloFlag (const bool tf) {return _Header->setMonteCarloFlag(tf);}
392 bool RawEvent::setEventNumber (const uint64_t evnt){return _Header->setEventNumber(evnt);}
393 
394 //------------------------------------------------------------------------------
395 bool RawEvent::setFloatingDataBlockToBufferSource(const void* internal_buffer_pointer) const {
396  /// If it exists, just set its buffer
398  return _FloatingDataBlock->setBufferSource(internal_buffer_pointer);
399 
400  /// Otherwise, create new Floating MicroSlice pointing to the buffer
401  _FloatingDataBlock = new daqdataformats::RawDataBlock(internal_buffer_pointer, _datablockVersion);
402 
403  return true;
404 }
405 
406 //------------------------------------------------------------------------------
407 bool RawEvent::setFloatingDataBlock(const uint32_t idatablock) const {
408 
409  const int32_t idatablock_int = (int32_t)idatablock;
410 
411  /// If it's already the same, nothing needs to be done
412  if(idatablock_int == _FloatingDataBlockNumber)
413  return true;
414 
415  /// Pointer that we are going to use
416  uint32_t* pointer = NULL;
417 
418  /// If microslice number is greater than current floating Microslice number, then no need
419  /// to loop over all MicroSlices, just start from the current floating MicroSlice
420  if(idatablock_int > _FloatingDataBlockNumber && _FloatingDataBlockNumber > -1){
421  pointer = (uint32_t*)_FloatingDataBlock->getBuffer();
422  }
423  else{
424  /// Otherwise, start from scratch
425  /// First point to the first MicroSlice
426  pointer = ((uint32_t*)_Buffer) + _Header->sizeofdata() + _Trigger->sizeofdata();
427 
428 
429  /// Set floating MicroSlice to pointer
430  /// If it doesn't exist, create it
431  EXECUTE_FUNCTION(setFloatingDataBlockToBufferSource)(pointer);
433 
434  }// end of setting the first floating MicroSlice
435 
436 
437  /// Pointer to the end of the buffer for checks
438  const uint32_t* end_of_buffer_pointer = &RAWBUFF32[sizeofdata()];
439 
440  /// Start loop over microslices
441  for(; _FloatingDataBlockNumber < idatablock_int; ++_FloatingDataBlockNumber){
442  pointer += _FloatingDataBlock->sizeofdata();
443 
444  /// Check if the pointer is not valid
445  if (pointer >= end_of_buffer_pointer)
446  return false;
447 
448  _FloatingDataBlock->setBufferSource(pointer);
449  }// end of loop over microslices
450 
451  return true;
452 }
453 
454 //------------------------------------------------------------------------------
456 
457  // Advance event number
458  _Header->advanceEventNumber();
459  //reset DataBlock number
460  _Header->setDataBlockNumber(0);
461  //reset the size
462  _Header->setEventSize(_Trigger->sizeofdata() + _Header->sizeofdata() + 1);
463 
464  // reset the _FloatingDataBlockNumber
466 
467  return true;
468 
469 }
470 
471 //------------------------------------------------------------------------------
472 // Printing the RawEvent structure
473 void RawEvent::print(std::ostream& os) const {
474 
475  os<<boost::format("---RawEvent---------------------------------------------size = %u\n")
476  % sizeofdata();
477 
478  printBuffer(os);
479 
480  /// Copy of the event
481  daqdataformats::RawEvent event_copy(_Buffer, _version);
482 
483  event_copy.getHeader()->print(os);
484 
485  /// Print the Trigger Block
486  if(_Trigger){
487 
488  RawTrigger* tr = event_copy.getTrigger();
489  tr->print(os);
490 
491  const uint32_t n_datablocks = event_copy.getDataBlockNumber();
492  os<<"Number of datablocks="<<n_datablocks<<std::endl;
493 
494  /// Print all DataBlocks. Can only print them when there is a Trigger
495  for(uint32_t idatablock = 0; idatablock < n_datablocks; ++idatablock){
496  event_copy.setFloatingDataBlock(idatablock);
497  event_copy.getFloatingDataBlock()->print(os);
498  }
499 
500  }
501  else{
502  os<<"There is no trigger right now\n";
503  }
504  return;
505 
506  const uint32_t size_of_data = EXECUTE_FUNCTION(sizeofdata)();
507 
508  os<<"\nRAW:";
509  for(uint32_t i = 0; i < size_of_data; ++i){
510  printWord(i, false, os);
511  os<<" ";
512  }
513 
514  os<<"\nRAW:";
515  for(uint32_t i = 0; i < size_of_data; ++i){
516  os<<" ";
517  printWord(i, true, os);
518  os<<std::endl;
519  }
520 
521  os<<boost::format("---end of RawEvent---------------------------------------size = %u\n")
522  % size_of_data;
523 
524 }
525 
526 //------------------------------------------------------------------------------
527 // Size of the RawEvent _Buffer in words
528 uint32_t RawEvent::sizeofdata()const { return _Header->getEventSize(); }
529 
530 //------------------------------------------------------------------------------
531 // Read an entire DataBlock from datablock_buffer into _Buffer
532 void* RawEvent::readData(const void* datablock_buffer){
533 
534  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" Reading "<<_Header->sizeofdata());
535 
536  const uint32_t header_size = _Header->sizeofdata();
537 
538  if(_shouldBufferBeInternal){
539  resizeInternalBuffer(header_size + 1);
540  /// Check pointers after each read or resize buffer
541  checkBufferInternalness();
542  }
543 
544  /// Read the header
545  void* datablock_buffer_new = readDataGeneral(datablock_buffer, header_size, 0);
546 
547  if(_shouldBufferBeInternal){
548  resizeInternalBuffer(EXECUTE_FUNCTION(sizeofdata)());
549  /// Check pointers after each read or resize buffer
550  checkBufferInternalness();
551  }
552 
553  /// Read the rest of the event
554  datablock_buffer_new = readDataGeneral(datablock_buffer_new, EXECUTE_FUNCTION(sizeofdata)() - header_size, header_size);
555 
556  /// Check pointers after each read or resize buffer
557  checkBufferInternalness();
558 
559  /// Need to assign trigger if it's not constructed yet
560  if(!_Trigger){
561  EXECUTE_FUNCTION(setTriggerPointer)(&RAWBUFF32[header_size]);
562  }
563 
564  return datablock_buffer_new;
565 }
566 
567 
568 
569 //------------------------------------------------------------------------------
570 bool RawEvent::setBufferSource(const void* pointer){
571 
572  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<"version = "<<_version);
573 
574  _Buffer = (void*)pointer;
575 
576  _shouldBufferBeInternal = isInternalBuffer();
577 
578  /// If Header is already defined, then set its buffer source
579  /// Otherwise, need to construct it
580  if(_Header){
581  _Header->setBufferSource(pointer);
582 
583  /// Once we set the header, then try to set Trigger and then if we succed, set Tail
584  if(tryToSetTrigger())
585  tryToSetTail();
586 
587  return true;
588  }/// end of if there is Header
589 
590  /// So, the header is not constructed. Construct it now
591 
592  PRINT_ON_DEBUG("Setting Header "<<"version = "<<_version);
593  _Header = new daqdataformats::RawEventHeader(_Buffer, _version); ///< version of the EventHeader equals the version of the Event
594 
595  /// Once header is constructed, we can read size of data
596  _function_sizeofdata = (sizeofdata_t) &VERSION_NAMESPACE::CURRENT_CLASS::sizeofdata;
597 
598  /// Once we set the header, then try to set Trigger and then if we succed, set Tail
599  if(tryToSetTrigger())
600  tryToSetTail();
601 
602  return true;
603 }
604 
605 //------------------------------------------------------------------------------
606 // Attempt to set the trigger to the pointer
608  /// If header is not instanciated, no way to set the Trigger
609  if(!_Header) return false;
610 
611  /// If header is not set appropriately, no way we can se the trigger.
612  if(_Header->getMarkerLeft() != daqdataformats::raweventhdr::Marker_value[0]) return false;
613  if(_Header->getMarkerRight() != daqdataformats::raweventhdr::Marker_value[1]) return false;
614 
615  /// Can't do anything if version is unknown
616  if(isVersionUnknown()) return false;
617 
618  /// See if we can even read TriggerHeader in case the buffer is internal
619  /// som trigger header of version 0
621 
622  /// if the size is too small, can't even set the TriggerHeader,
623  /// much less trigger
624  if(EXECUTE_FUNCTION(sizeofdata)() - _Header->sizeofdata() < header_v0.sizeofdata())
625  return false;
626 
627  /// pointer to the trigger
628  const void* pointer = &RAWBUFF32[_Header->sizeofdata()];
629 
630  /// Set Trigger header to that pointer
631  header_v0.setBufferSource(pointer);
632 
633  /// Check Delimiter
634  /// If it's not valid, ca't set it
635  if(header_v0.getDelimiter() != daqdataformats::triggerheader::TriggerHeaderDelimiter)
636  return false;
637 
638  /// Finally, check if we can se the trigger
639  try{
640  const daqdataformats::RawTrigger trigger_try((version_t)header_v0.getVersion());
641 
642  /// if the size is too small, can't even set the TriggerHeader,
643  /// much less trigger
644  if(EXECUTE_FUNCTION(sizeofdata)() - _Header->sizeofdata() < trigger_try.sizeofdata())
645  return false;
646  }
647  catch(...){
648  return false;
649  }
650 
651  /// At this point, should be able to set Trigger
652 
653  RETURN_FUNCTION(setTriggerPointer)(pointer);
654 }
655 
656 //------------------------------------------------------------------------------
657 // Attempt to set the Event Tail to the pointer
659 
660  /// If header or triggeris not instanciated, no way to set the Tail
661  if(!_Header) return false;
662  if(!_Trigger) return false;
663 
664  /// If header is not set appropriately, no way we can se the trigger.
665  if(_Header->getMarkerLeft() != daqdataformats::raweventhdr::Marker_value[0]) return false;
666  if(_Header->getMarkerRight() != daqdataformats::raweventhdr::Marker_value[1]) return false;
667 
668  if(_Trigger->getTriggerHeader()->getDelimiter() != daqdataformats::triggerheader::TriggerHeaderDelimiter) return false;
669 
670  /// Can't do anything if version is unknown
671  if(isVersionUnknown()) return false;
672 
673  /// Get version of the tail from the header
674  const version_t tail_version = _Header->getTailVersion();
675 
676  /// Once header is of a known version, then everything should be fine as _tailVersion is fixed
677  try{
678  daqdataformats::RawEventTail tail_0(tail_version);
679 
680 
681  const uint32_t event_tail_size = tail_0.sizeofdata();
682 
683  /// if the size is too small, can't set Tail
684  if(EXECUTE_FUNCTION(sizeofdata)() - _Header->sizeofdata() - _Trigger->sizeofdata() < tail_0.sizeofdata())
685  return false;
686 
687  /// Pointer should point to the end of event minus tail size and crc
688  const void* pointer = &RAWBUFF32[sizeofdata() - event_tail_size - 1];
689 
690  /// trying to read markers
691  tail_0.setBufferSource(pointer);
692 
693  if(tail_0.getMarker() != daqdataformats::raweventtail::Marker_value[0]) return false;
694  if(tail_0.getMarkerLeft() != daqdataformats::raweventtail::Marker_value[1]) return false;
695  if(tail_0.getMarkerRight() != daqdataformats::raweventtail::Marker_value[2]) return false;
696 
697  /// At this point should be able to read tail
698 
699  /// If tail exists, just assign the buffer source
700  if(_Tail)
701  _Tail->setBufferSource(pointer);
702  else
703  _Tail = new daqdataformats::RawEventTail(pointer, tail_version);
704  }
705  catch(...){
706  return false;
707  }
708 
709 
710 
711 
712  return true;
713 }
714 
715 
716 }}
#define ZEROOUT_FUNCTION(FUNC)
Definition: FunctionBind.h:52
#define DEEPCOPY_FUNCTION(FUNC)
Definition: FunctionBind.h:40
#define EXECUTE_FUNCTION(FULLFUNCTIONNAME)
Execute and return the delegate.
Definition: Macros.h:54
#define sizeofdata_t
Definition: FunctionBind.h:30
Class to hold the data from the global trigger in the correct format.
Definition: RawTriggerV0.h:65
#define VERSION_NAMESPACE
version_t _triggerVersion
Floating DataBlock number.
Definition: RawEventV0.h:139
Timing fit.
daqdataformats::RawEventHeader * _Header
Definition: RawEventV0.h:133
Class to hold the data from the global trigger in the correct format.
Definition: RawTrigger.h:40
#define RETURN_FUNCTION(FULLFUNCTIONNAME)
Definition: Macros.h:57
int32_t _FloatingDataBlockNumber
Floating DataBlock. The count starts from 0.
Definition: RawEventV0.h:137
void print(std::ostream &os=std::cout) const
Definition: RawEventV0.cpp:473
IMPLEMENT_MAIN_STANDARD IMPLEMENT_MAIN_setBufferSource bool addDataBlock(const void *buffer)
Definition: RawEvent.h:61
uint32_t getTailSize(const version_t event_version)
Function to get the tail size from Event version.
Definition: RawEvent.h:36
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
daqdataformats::RawDataBlock * _FloatingDataBlock
Trigger.
Definition: RawEventV0.h:136
const XML_Char * version
Definition: expat.h:187
daqdataformats::RawTrigger * _Trigger
RawEventV0 Tail.
Definition: RawEventV0.h:135
bool tryToSetTrigger()
Attempt to set the trigger to the pointer.
Definition: RawEventV0.cpp:607
daqdataformats::RawEventTail * _Tail
RawEventV0 Header.
Definition: RawEventV0.h:134
#define RAWBUFF32
Definition: RawDAQData.h:16
int32_t version_t
Definition: RawDAQData.h:72
bool setBufferSource(const void *pointer)
Definition: RawEventV0.cpp:570
#define PRINT_ON_DEBUG(x)
Definition: RawDAQData.h:35
static const version_t DAQDATAFORMAT_UNKNOWN_VERSION
Definition: RawDAQData.h:81
Class to hold the data from the FEBs in correct formats.
bool tryToSetTail()
Attempt to set the Event Tail to the pointer.
Definition: RawEventV0.cpp:658
void print(std::ostream &os=std::cout) const
procfile close()