RawDAQData.cpp
Go to the documentation of this file.
1 #include "DAQDataFormats/RawDAQData.h"
2 #include "DAQDataFormats/BitFields.h"
3 #include "DAQDataFormats/NOvACheckSum.h"
4 #include <DAQDataFormats/Macros.h> // most classes will use this
5 #include <DAQDataFormats/DataFormatException.h>
6 
7 #include <boost/lexical_cast.hpp>
8 #include <boost/format.hpp>
9 
10 namespace daqdataformats{
11 
12 //------------------------------------------------------------------------------
13 // Addition to the default constructor
15  _Buffer (NULL)
16  ,_shouldBufferBeInternal (false)
17  ,_version(rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)
18  ,_function_init (&RawDAQData::init)
19  ,_function_sizeofdata (NULL)
20  ,_function_print (&RawDAQData::printUnknownVersion)
21  ,_function_readData (&RawDAQData::readDataUnknownVersion)
22  ,_function_setBufferSource(&RawDAQData::setBufferSource)
23 { }
24 
25 //------------------------------------------------------------------------------
27  _Buffer (NULL)
29  ,_version (version)
30  ,_function_init (&RawDAQData::init)
31  ,_function_sizeofdata (NULL)
32  ,_function_print (&RawDAQData::printUnknownVersion)
33  ,_function_readData (&RawDAQData::readDataUnknownVersion)
34  ,_function_setBufferSource(&RawDAQData::setBufferSource)
35 { }
36 //------------------------------------------------------------------------------
38  _Buffer (NULL)
40  ,_version (version)
41  ,_function_init (&RawDAQData::init)
42  ,_function_sizeofdata (NULL)
43  ,_function_print (&RawDAQData::printUnknownVersion)
44  ,_function_readData (&RawDAQData::readDataUnknownVersion)
45  ,_function_setBufferSource(&RawDAQData::setBufferSourceUnknownVersion)
46 { }
47 //------------------------------------------------------------------------------
49  _Buffer (NULL)
51  ,_version (version)
52  ,_function_init (&RawDAQData::init)
53  ,_function_sizeofdata ((sizeofdata_t)sz)
54  ,_function_print (&RawDAQData::printUnknownVersion)
55  ,_function_readData (&RawDAQData::readDataGeneral)
56  ,_function_setBufferSource(&RawDAQData::setBufferSource)
57 { }
58 //------------------------------------------------------------------------------
59 RawDAQData::RawDAQData (const version_t version, bool, Sizeofdata_t(sz)):
60  _Buffer (NULL)
62  ,_version (version)
63  ,_function_init (&RawDAQData::init)
64  ,_function_sizeofdata ((sizeofdata_t)sz)
65  ,_function_print (&RawDAQData::printUnknownVersion)
66  ,_function_readData (&RawDAQData::readDataUnknownVersion)
67  ,_function_setBufferSource(&RawDAQData::setBufferSourceUnknownVersion)
68 { }
69 //------------------------------------------------------------------------------
70 RawDAQData::RawDAQData (Init_t(init_function_pointer [[gnu::unused]])):
71  _Buffer (NULL)
74  ,_function_init (&RawDAQData::init)
75  ,_function_sizeofdata (NULL)
76  ,_function_print (&RawDAQData::printUnknownVersion)
77  ,_function_readData (&RawDAQData::readDataUnknownVersion)
78  ,_function_setBufferSource(&RawDAQData::setBufferSource)
79 { }
80 
81 //------------------------------------------------------------------------------
82 RawDAQData::RawDAQData (const version_t version, Init_t(init_function_pointer)):
83  _Buffer (NULL)
85  ,_version (version)
86  ,_function_init ((init_t)init_function_pointer)
87  ,_function_sizeofdata (NULL)
88  ,_function_print (&RawDAQData::printUnknownVersion)
89  ,_function_readData (&RawDAQData::readDataUnknownVersion)
90  ,_function_setBufferSource(&RawDAQData::setBufferSource)
91 { }
92 //------------------------------------------------------------------------------
93 RawDAQData::RawDAQData (const version_t version,
94  Init_t(init_function_pointer),
95  Sizeofdata_t(sz)):
96  _Buffer (NULL)
98  ,_version (version)
99  ,_function_init ((init_t)init_function_pointer)
100  ,_function_sizeofdata ((sizeofdata_t)sz)
101  ,_function_print (&RawDAQData::printUnknownVersion)
102  ,_function_readData (&RawDAQData::readDataUnknownVersion)
103  ,_function_setBufferSource(&RawDAQData::setBufferSource)
104 { }
105 
106 //------------------------------------------------------------------------------
107 RawDAQData::RawDAQData (const version_t version,
108  Init_t(init_function_pointer),
109  Sizeofdata_t(sz),
110  SetBufferSource_t(bs)):
111  _Buffer (NULL)
112  ,_shouldBufferBeInternal (false)
113  ,_version (version)
114  ,_function_init ((init_t)init_function_pointer)
115  ,_function_sizeofdata ((sizeofdata_t)sz)
116  ,_function_print (&RawDAQData::printUnknownVersion)
117  ,_function_readData (&RawDAQData::readDataUnknownVersion)
118  ,_function_setBufferSource((setBufferSource_t)bs)
119 { }
120 
121 //------------------------------------------------------------------------------
122 RawDAQData::RawDAQData (const version_t version,
123  Init_t(init_function_pointer),
124  Sizeofdata_t(sz),
125  SetBufferSource_t(bs),
126  Print_t(pr),
127  ReadData_t(rd)):
128  _Buffer (NULL)
129  ,_shouldBufferBeInternal (false)
130  ,_version (version)
131  ,_function_init ((init_t)init_function_pointer)
132  ,_function_sizeofdata ((sizeofdata_t)sz)
133  ,_function_print ((print_t)pr)
134  ,_function_readData ((readData_t)rd)
135  ,_function_setBufferSource((setBufferSource_t)bs)
136 { }
137 
138 //------------------------------------------------------------------------------
140  _Buffer (NULL)
143  ,_version (copy_in._version)
144  ,_function_init (copy_in._function_init)
145  ,_function_sizeofdata (copy_in._function_sizeofdata)
146  ,_function_print (copy_in._function_print)
147  ,_function_readData (copy_in._function_readData)
148  ,_function_setBufferSource(copy_in._function_setBufferSource)
149 { }
150 //------------------------------------------------------------------------------
152 
153  /// If buffer is null, nothing to do
154  if(copy_in._Buffer == NULL) return;
155 
156  // Setup the main buffer pointer
157  // If the copy_in's buffer is external, point to that external memory
158  if(copy_in.isInternalBuffer()) {
160  }
161  else{
162  PRINT_ON_DEBUG("From RawDAQData::copy. Buffer is External. Using setBufferSource.");
164  }
165 }
166 
167 //------------------------------------------------------------------------------
168 // Addition to the default destructor
170 
171 //------------------------------------------------------------------------------
172 // General I/O Methods
173 // These methods rely on the _Buffer pointer to the general data
174 // buffer being set by the class, and there being an implimentation
175 // of the sizeofdata() method.
176 //
177 // These methods can be reimplimented by individual classes if their
178 // I/O is more complicated
179 // Method to write the data out to a file descriptor
180 //------------------------------------------------------------------------------
181 void* RawDAQData::readData(const void* buffer){
183 }
184 
185 // Method to read the data in from a raw buffer when size_of_data is known
187  return readDataGeneral(buffer, EXECUTE_FUNCTION(sizeofdata)(), 0);
188 }
189 //------------------------------------------------------------------------------
190 /// Adding stuff to buffer
191 void* RawDAQData::readDataGeneral(const void* buffer_void, const uint32_t size_of_data_to_read){
192 
193  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<"size of data "<<sizeofdata());
194  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<_InternalBuffer.size());
195 
196  return readDataGeneral(buffer_void, size_of_data_to_read, EXECUTE_FUNCTION(sizeofdata)());
197 }
198 
199 //------------------------------------------------------------------------------
200 void* RawDAQData::readDataGeneral(const void* buffer_void, const uint32_t size_of_data_to_read, const uint32_t buffer_start_position){
201 
202  PRINT_ON_DEBUG(__PRETTY_FUNCTION__);
203 
204  const uint32_t* buffer = (const uint32_t*)buffer_void;
205 
206  if(buffer == 0){return 0;}
207 
208 
209  if(isInternalBuffer()){
210  {
211  PRINT_ON_DEBUG ("Internal buffer");
212  }
213 
214  int32_t words_to_assign = int32_t(_InternalBuffer.size()) - int32_t(buffer_start_position);
215  {
216  PRINT_ON_DEBUG ("Size="<<_InternalBuffer.size()<<" buffer_start_pos="<<buffer_start_position<<" size_of_data_to_read="<<size_of_data_to_read);
217  PRINT_ON_DEBUG ("Words to assign1="<<words_to_assign);
218  EXECUTE_ON_DEBUG(if(words_to_assign < 0) {std::cout<<"FROM "<<__PRETTY_FUNCTION__<<"words_to_assign is negative("<<words_to_assign<<"). SOMETHING MUST BE WRONG\n";exit(1);});
219  }
220  /// If it's negative, then nothing to assign
221  if(words_to_assign > 0)
222  words_to_assign = std::min(int32_t(size_of_data_to_read), words_to_assign);
223  else
224  words_to_assign = 0;
225 
226  {
227  PRINT_ON_DEBUG ("Words to assign2="<<words_to_assign);
228  }
229  uint32_t i = 0;
230  ///At first, we are going to be assigning things
231  for(; i < uint32_t(words_to_assign); ++i ){
232  RAWBUFF32[buffer_start_position + i] = buffer[i];
233  }
234 
235  /// Push back the rest
236  for(; i < size_of_data_to_read; ++i ){
237  _InternalBuffer.push_back(buffer[i]);;
238  }
239  }
240  else {
241  /// Since, it's the external buffer, then we are going to assign
242  for(uint32_t i = 0; i < size_of_data_to_read; ++i ){
243  RAWBUFF32[buffer_start_position + i] = buffer[i];
244  }
245 
246  }// end if buffer is internal or external
247 
248  // Return the address of the buffer advanced by 1 word;
249  return (void*)(buffer + size_of_data_to_read);
250 }
251 
252 
253 
254 //------------------------------------------------------------------------------
256 
257  if(fd < 0) return rds_error;
258 
259  const uint32_t size_of_data = EXECUTE_FUNCTION(sizeofdata)();
260 
261  const ssize_t ss = read(fd, _Buffer, (size_t)size_of_data*sizeof(uint32_t));
262 
263  if (ss == -1 || ss == 0) return rds_error;
264 
265  if ((uint32_t)ss == size_of_data*sizeof(uint32_t)) return rds_complete;
266  else return rds_incomplete;
267 }
268 
269 
270 //------------------------------------------------------------------------------
271 bool RawDAQData::reserveInternalBuffer(const uint32_t size_of_data){
272 
273  /// We use vector::reserve instead of vector::resize,
274  /// because resize not only reserves things,
275  /// it initializes them to zero value.
276  /// We don't have to do that, because we may overwrite things.
277  if(_InternalBuffer.capacity() < size_of_data)
278  _InternalBuffer.reserve(size_of_data);
279 
280  return true;
281 }
282 
283 //------------------------------------------------------------------------------
284 bool RawDAQData::addToInternalBuffer(const uint32_t size_of_data){
285  for(uint32_t i = 0; i < size_of_data; ++i)
286  _InternalBuffer.push_back(0);
287 
288  return true;
289 }
290 
291 //------------------------------------------------------------------------------
292 bool RawDAQData::resizeInternalBuffer(const uint32_t size_of_data){
293 
294  if(_InternalBuffer.size() < size_of_data)
295  _InternalBuffer.resize(size_of_data);
296 
297  return true;
298 }
299 
300 //------------------------------------------------------------------------------
301 // Method to write the data out to a raw buffer
302 void* RawDAQData::writeData(const void* buffer) const {
303 
304  if(buffer == 0){return 0;}
305 
306  uint32_t* write_buffer = (uint32_t*)buffer;
307 
308  const uint32_t size_of_data = EXECUTE_FUNCTION(sizeofdata)();
309  /// Loop over words of the buffer
310  for(uint32_t i = 0; i < size_of_data; ++i){
311  *write_buffer = RAWBUFF32[i];
312  ++write_buffer;
313  } // endfor
314 
315  return write_buffer;
316 }// end of RawDAQData::writeData(void* buffer)
317 
318 //------------------------------------------------------------------------------
319 // Method to write the data out to a file descriptor
320 ssize_t RawDAQData::writeData(const int fd) const {
321  if(fd>0){
322  return write(fd, _Buffer, (size_t)EXECUTE_FUNCTION(sizeofdata)()*sizeof(uint32_t));
323  }else{
324  return -1;
325  }
326 }// end of RawDAQData::writeData(int fd)
327 
328 
329 //------------------------------------------------------------------------------
330 void RawDAQData::printBuffer(std::ostream& out_stream
331  ,const bool print_offset /* Print offset from the address of the _Buffer */
332  ,const bool print_ascii /* Print ASCII of the content of the Buffer */
333  ,const bool print_buffer_address /* Print Buffer Address */
334  ,const bool print_internal_buffer /* Print Internal Buffer Info */
335  ,const bool print_size_of_data /* Print Size Of Data */
336  ,const std::string separation_between_lines /* Separation symbol between lines */
337  ,const uint32_t cols /* Number of columns in a line */
338  ) const {
339 
340  // print Buffer address
341  if(print_buffer_address){
342 
343  uint32_t* buf = (uint32_t*)_Buffer;
344  out_stream<<"Buffer address is "<<buf<<std::endl;
345  }
346 
347  // print internal buffer info
348  if(print_internal_buffer){
349  if(_InternalBuffer.size() > 0){
350  /// If we havn't printed buffer address, need to print Internal Buffer address
351  if(!print_buffer_address || !isInternalBuffer())
352  out_stream<<"Internal Buffer address "<<&_InternalBuffer[0]<<". ";
353 
354  /// Print whether the buffer is internal/external
355  if(isInternalBuffer())
356  out_stream<<"Using Internal Buffer.";
357  else
358  out_stream<<"Using External Buffer.";
359 
360  /// print size
361  out_stream<<" The size of the internal buffer is "<<_InternalBuffer.size()<<std::endl;
362  }
363  else{
364  out_stream<<"Internal Buffer size is ZERO"<<std::endl;
365  }// end of checking if the size of internal buffer is nonzero
366 
367  }// end of printing internal buffer info
368 
369  /// Size of data
370  const uint32_t size_of_data = EXECUTE_FUNCTION(sizeofdata)();
371 
372  if(print_size_of_data)
373  out_stream<<"Size of data = "<<size_of_data<<std::endl;
374 
375  /// Current pointer to a word
376  uint32_t* data_buffer = (uint32_t*)_Buffer;
377 
378  /// This is only for ascii buffer printing
379  char* ascii_buffer = (char*)_Buffer;
380 
381  /// Loop over buffer
382  for(uint32_t i = 0; i < size_of_data; ++i){
383 
384  // At the start of the line write the address offset
385  if((print_offset && !(i % cols))){
386  out_stream<<boost::format("[Offset=%08x bytes] \t")%(uint32_t)((data_buffer-(uint32_t*)_Buffer)*sizeof(uint32_t));
387  }
388 
389  /// Write the data word in hex
390  out_stream<<boost::format("0x%08x\t")%*data_buffer;
391 
392  /// At the end of the line write the line in ASCII with suppression of
393  /// non printing characters
394  if((i + 1) % cols == 0 || i == size_of_data-1){
395 
396  /// printing the ascii for the word
397  if(print_ascii){
398 
399  uint32_t nsymbols = cols;
400 
401  if(i == size_of_data-1){
402  nsymbols = size_of_data % cols;
403  if(nsymbols == 0)
404  nsymbols = cols;
405  }
406 
407  if(size_of_data / cols){
408  for(uint32_t itab = 0; itab < cols - nsymbols; ++itab)
409  out_stream<<" ";
410  }
411 
412  out_stream<<"(ASCII=";
413 
414  /// Loop over 4 characters of the line
415  for(uint32_t j = 0; j < sizeof(uint32_t) * nsymbols; ++j){
416 
417  /// print the character
418  if( (*ascii_buffer < 32) || (*ascii_buffer > 126)){
419  out_stream<<".";
420  }else{
421  out_stream<<boost::format("%c") % (*ascii_buffer);
422  }// end of print the character
423 
424  ++ascii_buffer;
425  }// end of loop over charachters
426 
427  out_stream<<")";
428  }// end of printing ASCII
429 
430  /// At the end of the line, put separation symbol
431  out_stream<<separation_between_lines;
432  }// end of if the end of the line
433 
434  ++data_buffer;
435  }// end of loop over buffer
436 
437 }// end of RawDAQData::printBuffer()
438 
439 //------------------------------------------------------------------------------
440 void RawDAQData::printWord (const uint32_t iword, const bool is_bynary_printing, std::ostream& out_stream) const {
441 
442  const uint32_t rawbuf = RAWBUFF32[iword];
443  const uint32_t value = SWAB32(rawbuf);
444 
445  /// Check if we want to print in a bynary mode
446  if(is_bynary_printing){
447  printWordBinary(value, out_stream);
448  } else {
449  //Else print in hex mode
450  printWordHex(value, out_stream);
451  }// end of if(is_bynary_printing)
452 
453 }//end of void printWord
454 
455 //------------------------------------------------------------------------------
456 void RawDAQData::printWordBinary(const uint32_t value, std::ostream& out_stream) const {
457 
458  uint32_t value_copy = value;
459 
460  const int SHIFT = 8 * sizeof( unsigned ) - 1;
461  const unsigned MASK = 1 << SHIFT;
462 
463  for ( int i = 1; i <= SHIFT + 1; i++ ){
464  out_stream << ( value_copy & MASK ? '1' : '0' );
465  value_copy <<= 1;
466 
467  if ( i % 8 == 0 ) out_stream << ' ';
468  }
469  out_stream<<std::endl;
470 }
471 
472 //------------------------------------------------------------------------------
473 void RawDAQData::printWordHex (const uint32_t value, std::ostream& out_stream) const{
474  out_stream<<boost::format("0x%08x")% value;
475 }
476 
477 //------------------------------------------------------------------------------
478 void RawDAQData::printBinary (const void* buffer0, const uint32_t size, std::ostream& out_stream) const{
479 
480  const uint32_t* buffer = (uint32_t*)buffer0;
481  for(uint32_t iword = 0; iword < size; ++iword){
482 // out_stream<<"Address "<<&buffer[iword]<<" ";
483  printWordBinary(buffer[iword], out_stream);
484  }// end of loop over buffer
485 }
486 
487 //------------------------------------------------------------------------------
488 void RawDAQData::printHex (const void* buffer0, const uint32_t size, std::ostream& out_stream) const{
489 
490  const uint32_t* buffer = (uint32_t*)buffer0;
491  for(uint32_t iword = 0; iword < size; ++iword){
492 // out_stream<<"Address "<<&buffer[iword]<<" ";
493  printWordHex(buffer[iword], out_stream);
494  out_stream<<std::endl;
495  }// end of loop over buffer
496 }
497 
498 
499 //------------------------------------------------------------------------------
500 void RawDAQData::print(std::ostream& os) const {RETURN_FUNCTION(print)(os);}
501 
502 //------------------------------------------------------------------------------
503 void RawDAQData::print(std::string& output_string) const{
504 
505  /// Some temporary stream
506  std::ostringstream str;
507  /// perform standard print into the stream
509  /// Get the string out of the stream
510  output_string = str.str();
511 
512 }
513 
514 //------------------------------------------------------------------------------
515 // General printing of a DAQDataFormat member. Should be overloaded.
516 void RawDAQData::printGeneral(std::ostream& output_stream) const {
517 
518  const uint32_t size_of_data = EXECUTE_FUNCTION(sizeofdata)();
519 
520  output_stream<<"----------General printing of a DAQDataFormat member-----------size="<<size_of_data<<std::endl;
521  output_stream<<std::endl<<"nRAW:";
522  for(uint32_t i = 0; i < size_of_data; ++i){
523  output_stream<<" ";
524  printWord(i, false, output_stream);
525  }
526 
527  output_stream<<"\nRAW:";
528  for(uint32_t i = 0; i < size_of_data; ++i){
529  if(i > 0)output_stream<<" ";
530  printWord(i, true, output_stream);
531  output_stream<<std::endl;
532  }
533 }// end of RawDAQData::printGeneral()
534 
535 //------------------------------------------------------------------------------
536 // Calculate Check Sum (CRC) for _Buffer.
537 // skip_last_words = number of words from the end to skip.
538 // Usually use just 0 or 1. 0 - to calculate CRC of the whole _Buffer.
539 // 0 to caclulate the CRC for the whole _Buffer
540 // 1 is usually used to append CRC to the _Buffer block
541 uint32_t RawDAQData::calculateCheckSum(const uint32_t skip_last_words) const {
542 
544 
545  return CheckSum->processCRC(_Buffer, EXECUTE_FUNCTION(sizeofdata)() - skip_last_words);
546 }// end of calculateCheckSum
547 
548 //------------------------------------------------------------------------------
549 // Add the CRC into the end of fBuffer. CRC of all but the last words of the _Buffer
551 
552  /// Calculate CRC for the buffer, skipping the last word
553  uint32_t crc = calculateCheckSum(1);
554 
555  /// Do the swab
556  crc = SWAB32(crc);
557 
558  const uint32_t size_of_data = EXECUTE_FUNCTION(sizeofdata)();
559 
560  if(isInternalBuffer() && _InternalBuffer.size() < size_of_data){
561  _InternalBuffer.push_back(crc);
562  }
563  else {
564  /// Write the CRC at the last word of the Buffer
565  RAWBUFF32[size_of_data - 1] = crc;
566  }
567 
568  return true;
569 }// end of RawDAQData::addCheckSum()
570 
571 //------------------------------------------------------------------------------
572 // Clears all data in the buffer
574 
575  PRINT_ON_DEBUG(__PRETTY_FUNCTION__);
576 
577  const uint32_t size_of_data = EXECUTE_FUNCTION(sizeofdata)();
578 
579  /// If buffer is internal, need to use assign, because vector may not have been initialized
580  if(isInternalBuffer()){
581  _InternalBuffer.assign(size_of_data, 0);
582  }
583  else{
584  /// Loop over the buffer and zero it
585  /// The buffer could be either internal or external
586  for(uint32_t i = 0; i < size_of_data; ++i)
587  RAWBUFF32[i] = 0;
588  }
589 
590  PRINT_ON_DEBUG("end of"<<__PRETTY_FUNCTION__);
591 
592  return true;
593 } // end of clear
594 
595 //------------------------------------------------------------------------------
597  PRINT_ON_DEBUG(__PRETTY_FUNCTION__);
599  EXECUTE_ON_DEBUG(clear(); std::cout<<"End of "<<__PRETTY_FUNCTION__<<"\n"; return true);
600  return clear();
601 }// end of reset
602 
603 
604 //------------------------------------------------------------------------------
606  PRINT_ON_DEBUG(__PRETTY_FUNCTION__);
607 
609 } // end of reset _Buffer to point to InternalBuffer
610 
611 //------------------------------------------------------------------------------
613 
615  return resetBufferSource();
616 
617  return true;
618 }
619 
620 //------------------------------------------------------------------------------
621 // General routines for the default constructor of main class for fixed size data formats,
622 // like RawNanoSliceHeader, Timing marker etc. (they can be different from version to version though)
623 // But the difference is that we want to clear out the internal buffer for them,
624 // whereas for floating data formats don't need to have that, since the data may be overwritten anyways
627 }// end of defaultConstructorFixedSize(version_t)
628 
629 //------------------------------------------------------------------------------
630 // General routines for the default constructor of main class for fixed size data formats,
631 // like RawNanoSliceHeader, Timing marker etc. (they can be different from version to version though)
632 // But the difference is that we want to clear out the internal buffer for them,
633 // whereas for floating data formats don't need to have that, since the data may be overwritten anyways
634 void RawDAQData::defaultConstructorFixedSize(Init_t(init_function_pointer)){
636 }// end of defaultConstructorFixedSize(version_t, boost::function<void()>&)
637 
638 //------------------------------------------------------------------------------
639 // General routines for the default constructor of main class for floating size data formats,
640 // like RawNanoSlice, RawEvent etc.
641 // We don't need to initialize internal buffer
644 }// end of defaultConstructorFloatingSize(version_t)
645 
646 //------------------------------------------------------------------------------
647 // General routines for the default constructor of main class for floating size data formats,
648 // like RawNanoSlice, RawEvent etc.
649 // We don't need to initialize internal buffer
650 void RawDAQData::defaultConstructorFloatingSize(Init_t(init_function_pointer)){
652 }// end of defaultConstructorFloatingSize(version_t, boost::function<void()>&)
653 
654 //------------------------------------------------------------------------------
655 /// General routines for the default constructor of main class
657  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" 0 (Beginning)");
658 
659  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<". _version is set to "<<_version);
660 
661  /// Now, need to set all function pointers
662 
663  /// If version is unknown, then we can't do the following:
664  /// 1. initialize internal buffer because we don't know the size
665  /// 2. set buffer to the internal buffer, because internal buffer is 0
666  /// 3. initialize fields of the class, because we don't know which ones
667  /// Anyways, if we know the version, then go ahead and do all those steps
668  if(isVersionUnknown()){
669  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" 2 (Version is unknown)");
670  /// Since the version is unknown, we want to at least set the buffer to internal
671  /// Initialize the internal buffer to size 1
673  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" 3 (after resizing InternalBuffer, before assigning _Buffer pointer)");
674 
675  /// Set buffer to the internal buffer
676  /// Everything else is uninitialized
677  _Buffer = &(_InternalBuffer[0]);
678  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" 4 (after assigning _Buffer pointer)");
679  }
680  else {
681  /// Version is known
682  /// Set all Function pointers
684  {
685  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 5 (after setting Function pointers)");
686  PRINT_ON_DEBUG ("sizeofdata_function_pointer="<<_function_sizeofdata);
687  PRINT_ON_DEBUG ("init_function_pointer="<<_function_init);
688  EXECUTE_ON_DEBUG (uint32_t size0 = EXECUTE_FUNCTION(sizeofdata)());
689  PRINT_ON_DEBUG ("size after setting FunctionPointers = "<<size0);
690  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 6 (about to do resizeBuffer)");
692  PRINT_ON_DEBUG ("size = "<<size);
693  }
694 
695  /// Initialize internal buffer
696  /// We do know that it's internal buffer, because defaultConstructorGeneral is used for internal buffer only
698  {
699  PRINT_ON_DEBUG ("Internal Buffer size = "<<_InternalBuffer.size());
700  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 7 (about to call reset_buffer_source)");
701  }
702 
703  /// Set buffer to the internal buffer. May be clearing buffer as well
704  EXECUTE_GENERAL_FUNCTION(reset_buffer_source)();
705  {
706  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 8 (about to init)");
707  PRINT_ON_DEBUG ("Internal Buffer size = "<<_InternalBuffer.size());
709  EXECUTE_ON_DEBUG (if(EXECUTE_FUNCTION(sizeofdata)() != _InternalBuffer.size()) std::cout<<"SOMETHING MAY BE WRONG!!!\n");
710  PRINT_ON_DEBUG ("initing");
711  }
712 
713  /// Do initializations
715  {
716  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" 9 (about to exit)");
717  PRINT_ON_DEBUG("size of data:"<<EXECUTE_FUNCTION(sizeofdata)());
718  PRINT_ON_DEBUG("size of internal buffer:"<<_InternalBuffer.size());
719  }
720 
721  }// end of checking whether version is unknown
722 
723 
724  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" 10 (exiting)");
725 }// end of defaultConstructorGeneral(const ResetBufferSource_t)
726 
727 
728 //------------------------------------------------------------------------------
729 /// General routines for the default constructor of main class with a given init function pointer
730 void RawDAQData::defaultConstructorGeneral( Init_t (init_function_pointer)
731  , ResetBufferSource_t(reset_buffer_source)
732  )
733 {
734  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" 0 (Beginning)");
735  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<". _version is set to "<<_version);
736 
737  /// Now, need to set all function pointers
738  /// If version is unknown, then we can't do the following:
739  /// 1. initialize internal buffer because we don't know the size
740  /// 2. set buffer to the internal buffer, because internal buffer is 0
741  /// 3. initialize fields of the class, because we don't know which ones
742  /// Anyways, if we know the version, then go ahead and do all those steps
743  if(isVersionUnknown()){
744  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 2 (Version i unknown)");
745  /// Since the version is unknown, we want to at least set the buffer to internal
746  /// Initialize the internal buffer to size 1
748  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 3 (after resizing InternalBuffer, before assigning _Buffer pointer)");
749 
750  /// Set buffer to the internal buffer
751  /// Everything else is uninitialized
752  _Buffer = &(_InternalBuffer[0]);
753  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 4 (after assigning _Buffer pointer)");
754  }
755  else {
756  /// Version is known
757  /// Set all function pointers
759  {
760  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 5 (after setting Function pointers)");
761  EXECUTE_ON_DEBUG (uint32_t size0 = EXECUTE_FUNCTION(sizeofdata)());
762  PRINT_ON_DEBUG ("size after setting FunctionPointers = "<<size0);
763  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 6 (about to do resizeBuffer)");
765  PRINT_ON_DEBUG ("size = "<<size);
766  }
767 
768  /// Initialize internal buffer
769  /// We do know that it's internal buffer, because defaultConstructorGeneral is used for internal buffer only
771  {
772  PRINT_ON_DEBUG ("Internal Buffer size = "<<_InternalBuffer.size());
773  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 7 (about to call reset_buffer_source)");
774  }
775 
776  /// Set buffer to the internal buffer. May be clearing buffer as well
777  EXECUTE_GENERAL_FUNCTION(reset_buffer_source)();
778  {
779  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 8 (about to init)");
780  PRINT_ON_DEBUG ("Internal Buffer size = "<<_InternalBuffer.size());
782  EXECUTE_ON_DEBUG (if(EXECUTE_FUNCTION(sizeofdata)() != _InternalBuffer.size()) std::cout<<"SOMETHING MAY BE WRONG!!!\n");
783  PRINT_ON_DEBUG ("initing");
784  }
785 
786  /// Execute ::init() or whatever else is binded
787  EXECUTE_GENERAL_FUNCTION(init_function_pointer)();
788  {
789  PRINT_ON_DEBUG (__PRETTY_FUNCTION__<<" 9 (about to exit)");
790  PRINT_ON_DEBUG ("size of data:"<<EXECUTE_FUNCTION(sizeofdata)());
791  PRINT_ON_DEBUG ("size of internal buffer:"<<_InternalBuffer.size());
792  }
793 
794  }// end of checking whether version is unknown
795 
796  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" 10 (exiting)");
797 }// end of defaultConstructorGeneral(const Init_t, const ResetBufferSource_t)
798 
799 //------------------------------------------------------------------------------
800 /// Read data from the unknown version
801 /// Reading data to the format with unknown version can only happen when
802 /// the buffer is set to internal source, because if it was set to external
803 /// source, then we already figured out the version from it and wouldn't call this method
804 /// After reading the data, the DataFormat is fully initialized
806 
807  PRINT_ON_DEBUG(__PRETTY_FUNCTION__);
808  /// get the version
809  _version = figureOutTheVersion(buffer);
810 
811  /// Now that we know the version, initialize function pointers,
812  /// including readData
814 
815  /// Resize internal buffer
816  if(isInternalBuffer()){
818 
819  /// Reset buffer source, so that we can construct other things (like headers and such)
821  }// end of if isInternalBuffer
822 
823  /// Don't need to initialize the fields (use init()) because we are going to read new values from the buffer anyways...
824  /// Like now
826 }// end of readDataUnknownVersion
827 
828 //------------------------------------------------------------------------------
829 // Deep Copy Method
830 void RawDAQData::copy(const RawDAQData& copy_in){
831 
832  /// Get version
833  _version = copy_in._version;
834 
835  /// Copy the entire internal buffer
837 
838  /// Set function pointers
840 
841  lastOperationsInDeepCopy(copy_in);
842 }// end of copy(const RawDAQData& copy_in)
843 
844 //------------------------------------------------------------------------------
845 // Shallow Copy Method
846 void RawDAQData::copy(const void* external_buffer){
847  /// If input version is -1, then we don't really know what version it is and will try to read it
848  if(isVersionUnknown()){
849  PRINT_ON_DEBUG(__PRETTY_FUNCTION__ <<"version unknown");
850  /// figure out the version
851  _version = figureOutTheVersion(external_buffer);
852  }
853 
854  /// At this point version should be known
855  PRINT_ON_DEBUG(__PRETTY_FUNCTION__ <<"Picked up version "<<_version);
856  /// Now, need to define functions
858 
859  PRINT_ON_DEBUG(__PRETTY_FUNCTION__ << " 32 (about to do setBufferSource)");
860  EXECUTE_ON_DEBUG(if(!_function_setBufferSource)std::cout<<"sbf is not binded\n");
861  // Now set the pointer to the external buffer
862  EXECUTE_FUNCTION(setBufferSource)(external_buffer);
863  //setBufferSource(external_buffer);
864  PRINT_ON_DEBUG(__PRETTY_FUNCTION__ << " 4");
865 
866  EXECUTE_ON_DEBUG(if(_Buffer != external_buffer)
867  std::cout<<"Something is wrong in RawDAQData::copy";
868  else
869  std::cout<<"From RawDAQData::copy Buffer is set to "<<_Buffer<<"\n"
870  );
871 
872  // Don't need to initialize any other fields,
873  // because supposedly they should have been initialized before (by somebody else)
874 
875  /// Also, we don't initialize internal buffer, because the pointer is set to the external buffer
876 
877 }// end of copy(const void* external_buffer)
878 
879 //------------------------------------------------------------------------------
880 bool RawDAQData::setBufferSource(const void* pointer){
881  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" Setting to "<<pointer);
882  _Buffer = (void*)pointer;
883  return true;
884 }// end of setBufferSource
885 
886 //------------------------------------------------------------------------------
887 // Set the bufferSource and return pointer to the beginning of the next block
888 void* RawDAQData::setBufferSourceGeneral(RawDAQData& dataformat, const void* pointer){
889  dataformat.setBufferSource(pointer);
890  const uint32_t* next_block_pointer = ((uint32_t*)(pointer)) + dataformat.sizeofdata();
891  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" Size of data="<<dataformat.sizeofdata()<<". Next pointer will be"<<next_block_pointer);
892  return (void*)next_block_pointer;
893 }// end of setBufferSourceGeneral(RawDAQData& dataformat, const void* pointer)
894 
895 //------------------------------------------------------------------------------
897  /// First, figure out version
898  _version = figureOutTheVersion(pointer);
899 
900  /// Now, set Function pointers accordingly
902 
903  /// Now, execute setBuffer Source
905 }// end of setBufferSourceUnknownVersion(const void* pointer)
906 
907 //------------------------------------------------------------------------------
908 uint64_t RawDAQData::getUint64_t(const uint32_t low, const uint32_t high) const { return (uint64_t) high<<32 | low;}
909 //------------------------------------------------------------------------------
910 bool RawDAQData::getLoAndHi(const uint64_t input_data, uint32_t& out_low_word, uint32_t& out_hi_word) const {
911  out_low_word = input_data & 0xffffffff;
912  out_hi_word = input_data >> 32;
913  return true;
914 }// end of getLoAndHi
915 
916 /*
917 //------------------------------------------------------------------------------
918 uint64_t RawDAQData::getUint64_t( const DELEGATE_0PAR(RawDAQData, function_get_low_word, uint32_t)
919  , const DELEGATE_0PAR(RawDAQData, function_get_hi_word , uint32_t)
920  ) const{
921  //const DELEGATE_1PAR(RawDAQData, function_set_low_word, bool, uint32_t);
922  //(((const RawDAQData *)this)->*function_get_low_word)();
923 
924  //EXECUTE_GENERAL_FUNCTION(function_set_low_word)(1);
925  //(this->*function_set_low_word)(1);
926 
927  //EXECUTE_GENERAL_FUNCTION(function_get_low_word)();
928  uint32_t low = (uint32_t)EXECUTE_GENERAL_FUNCTION(function_get_low_word)();
929  //const uint32_t high = EXECUTE_GENERAL_FUNCTION(function_get_hi_word )();
930 
931  uint32_t low, high;
932  return getUint64_t(low, high);
933 }
934 */
935 
936 //------------------------------------------------------------------------------
937 bool RawDAQData::setUint64_t(const uint64_t value,
938  DELEGATE(RawDAQData, function_set_low_word, bool, uint32_t),
939  DELEGATE(RawDAQData, function_set_hi_word , bool, uint32_t)
940  ){
941  uint32_t low, high;
942  getLoAndHi(value, low, high);
943 
944  EXECUTE_GENERAL_FUNCTION(function_set_low_word)(low);
945  return EXECUTE_GENERAL_FUNCTION(function_set_hi_word)(high);
946 }// end of setUint64_t
947 
948 //------------------------------------------------------------------------------
949 // Generating exception as the version of DAQDataFormat is greater than the maximum allowed
951 
952  std::string msg = "The version of the DataFormat is too high! ";
953  msg += "The version is " + boost::lexical_cast<std::string>(_version)
954  + ". Whereas the maximum version allowed is " + boost::lexical_cast<std::string>(maximum_version);
955 
956  return msg;
957 }// end of generateExceptionForWrongVersion
958 
959 
960 //------------------------------------------------------------------------------
961 // Generating exception as the version of DAQDataFormat is greater than the maximum allowed
963 
964  /// The output message string
965  std::string output_message;
966 
967  if(function.size() > 0){
968  output_message = "\nFunction " + function + " ";
969  }
970  else{
971  output_message = "Operation ";
972  }
973 
974  output_message.append(" is Not allowed");
975  output_message.append(" for version " + boost::lexical_cast<std::string>(_version));
976  output_message.append(".");
977 
978  if(message.size() > 0)
979  output_message += " " + message;
980 
981  /// Generate the exception
982  throw daqdataformats::DataFormatException((const std::string)output_message);
983 
984  return false;
985 }
986 
987 
988 
989 //------------------------------------------------------------------------------
991 
992 //------------------------------------------------------------------------------
993 uint32_t calculateShiftFromAGivenMask (const uint32_t mask) {
994 
995  /// check that mask is not zero
996  if(mask == 0x0) return 0;
997 
998  /// find the number of first non-zero bit
999  uint32_t ibit = 0;
1000  for(; ibit < 32; ++ibit){
1001 
1002  /// check if the bit is non-zero
1003  if(mask & (0x1 << ibit)) return ibit;
1004 
1005  }// end of loop over bits
1006 
1007  return ibit;
1008 }// end of calculateShiftFromAGivenMask
1009 
1010 //------------------------------------------------------------------------------
1011 /// implement << operator
1012 std::ostream& operator << (std::ostream& os, const daqdataformats::RawDAQData& dataformat) {
1013  //dataformat.printFromFunctionPointer(os);
1014  dataformat.print(os);
1015  return os;
1016 }// end of << operator
1017 
1018 }// end of namespace daqdataformats
uint64_t getUint64_t(uint32_t low_word, uint32_t hi_word) const
Get uint64_t from low and hi word.
Definition: RawDAQData.cpp:908
#define EXECUTE_FUNCTION(FULLFUNCTIONNAME)
Execute and return the delegate.
Definition: Macros.h:54
void * readDataGeneral(const void *buffer)
General ReadData method when the sizeofdata is known.
Definition: RawDAQData.cpp:186
ReadData_t(_function_readData)
#define sizeofdata_t
Definition: FunctionBind.h:30
version_t _version
DataFormat Version.
Definition: RawDAQData.h:289
uint32_t processCRC(void *buffer, uint32_t size)
bool setBufferSource(const void *)
We are not going to overload these funtions, so no need to declare them virtual.
Definition: RawDAQData.cpp:880
Float_t x1[n_points_granero]
Definition: compare.C:5
bool isVersionUnknown() const
is current _version unknown?
Definition: RawDAQData.h:240
void * _Buffer
All data formats need to have _Buffer and a Data structure. _Buffer will be common.
Definition: RawDAQData.h:276
virtual bool addCheckSum()
Add the CRC into the end of fBuffer. CRC of all but the last words of the _Buffer.
Definition: RawDAQData.cpp:550
Float_t ss
Definition: plot.C:24
Print_t(_function_print)
void printWordBinary(const uint32_t word, std::ostream &os=std::cout) const
! Print the i-th word of _Buffer, is_bynary_printing: true = bynary, false = HEX
Definition: RawDAQData.cpp:456
virtual void copy(const RawDAQData &)
Deep copy from the DAQDataFormat.
Definition: RawDAQData.cpp:830
bool checkBufferInternalness()
If the buffer should be internal, check it&#39;s pointers and reassign them.
Definition: RawDAQData.cpp:612
bool getLoAndHi(uint64_t input_data, uint32_t &output_low_word, uint32_t &output_hi_word) const
Definition: RawDAQData.cpp:910
bool _shouldBufferBeInternal
Auxillary variable that holds whether the buffer should be external or internal.
Definition: RawDAQData.h:286
::xsd::cxx::tree::buffer< char > buffer
Definition: Database.h:179
void lastOperationsInDeepCopy(const RawDAQData &copy_in)
Need to perform these operations in Deep Copy constructor.
Definition: RawDAQData.cpp:151
#define RETURN_FUNCTION(FULLFUNCTIONNAME)
Definition: Macros.h:57
write
Run ND cosmics.
void * readDataUnknownVersion(const void *buffer)
Read data from the unknown version.
Definition: RawDAQData.cpp:805
void print(std::ostream &os=std::cout) const
Method for printing the content of the Buffer.
Definition: RawDAQData.cpp:500
static NOvACheckSum * getInstance()
Method to obtain the class instance.
RawDAQData()
Default Constructor.
Definition: RawDAQData.cpp:14
uint32_t calculateCheckSum(const uint32_t skip_last_words) const
Definition: RawDAQData.cpp:541
#define SWAB32(word)
Definition: BitFields.h:14
void * setBufferSourceGeneral(RawDAQData &, const void *)
Set BufferSource of the input DataFormat and return pointer to the position right after the end of it...
Definition: RawDAQData.cpp:888
Init_t(_function_init)
Some common function pointers used in each DAQDataFormats class.
void printWordHex(const uint32_t word, std::ostream &os=std::cout) const
Definition: RawDAQData.cpp:473
const XML_Char int const XML_Char * value
Definition: expat.h:331
const int cols[3]
virtual version_t figureOutTheVersion(const void *buffer) const =0
#define EXECUTE_GENERAL_FUNCTION(FULLFUNCTIONNAME)
Macro to execute the function given a function pointer.
Definition: FunctionBind.h:37
std::vector< uint32_t > _InternalBuffer
Definition: RawDAQData.h:283
bool isInternalBuffer() const
Definition: RawDAQData.h:141
Sizeofdata_t(_function_sizeofdata)
#define print_t
Definition: FunctionBind.h:31
void printBuffer(std::ostream &os=std::cout, const bool print_offset=true,const bool print_ascii=true,const bool print_buffer_address=false,const bool print_internal_buffer=false,const bool print_size_of_data=false,const std::string separation_between_lines="\n",const uint32_t cols=4) const
Definition: RawDAQData.cpp:330
bool setBufferSourceUnknownVersion(const void *)
Definition: RawDAQData.cpp:896
const double j
Definition: BetheBloch.cxx:29
std::string generateExceptionForWrongVersion(version_t maximum_version) const
Generating exception message as the version of DAQDataFormat is greater than the maximum allowed...
Definition: RawDAQData.cpp:950
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
#define ResetBufferSource_t(FUNC)
Definition: FunctionBind.h:26
#define resetBufferSource_t
Definition: FunctionBind.h:34
bool setUint64_t(uint64_t value, DELEGATE(RawDAQData, function_set_low_word, bool, uint32_t),)
Definition: RawDAQData.cpp:937
void * writeData(const void *buffer) const
Write Data to target memory location. Return is the address of the end of the write. NULL on error.
Definition: RawDAQData.cpp:302
virtual bool clear()
reset _Buffer to point to InternalBuffer
Definition: RawDAQData.cpp:573
OStream cout
Definition: OStream.cxx:6
const XML_Char * version
Definition: expat.h:187
#define setBufferSource_t
Definition: FunctionBind.h:33
bool addToInternalBuffer(const uint32_t size_of_data_to_add)
Add Zeros to internal buffer.
Definition: RawDAQData.cpp:284
uint32_t calculateShiftFromAGivenMask(const uint32_t mask)
Definition: RawDAQData.cpp:993
#define RAWBUFF32
Definition: RawDAQData.h:16
void * readData(const void *buffer)
Shallow copy constructor.
Definition: RawDAQData.cpp:181
#define readData_t
Definition: FunctionBind.h:32
void printHex(const void *buffer0, const uint32_t size, std::ostream &os=std::cout) const
Definition: RawDAQData.cpp:488
int32_t version_t
Definition: RawDAQData.h:72
void printWord(const uint32_t iword, const bool is_bynary_printing, std::ostream &os=std::cout) const
Print word from Buffer.
Definition: RawDAQData.cpp:440
exit(0)
void printGeneral(std::ostream &os=std::cout) const
Definition: RawDAQData.cpp:516
bool reserveInternalBuffer(const uint32_t size_of_data)
Reserve space for the internal buffer to some predetermined size.
Definition: RawDAQData.cpp:271
void printUnknownVersion(std::ostream &out_stream=std::cout) const
Definition: RawDAQData.h:213
SetBufferSource_t(_function_setBufferSource)
void defaultConstructorGeneral(resetBufferSource_t)
#define PRINT_ON_DEBUG(x)
Definition: RawDAQData.h:35
static const version_t DAQDATAFORMAT_UNKNOWN_VERSION
Definition: RawDAQData.h:81
void printBinary(const void *buffer0, const uint32_t size, std::ostream &os=std::cout) const
Definition: RawDAQData.cpp:478
Class to hold the data from the FEBs in correct formats.
T min(const caf::Proxy< T > &a, T b)
EXECUTE_ON_DEBUG(std::string _dataFormatClassName)
These variables are only for Debugging purposes.
#define init_t
Definition: FunctionBind.h:29
uint32_t sizeofdata() const
Sizeof method for the current data block. Returns the size of the data block in 32bit words...
Definition: RawDAQData.cpp:990
#define DELEGATE(CLASS, FUNC, OUTPUT,...)
Delegate (or function pointer)
Definition: FunctionBind.h:16
bool functionIsNotAllowed(const std::string function="", const std::string message="") const
One can call this function from another function, which usage is not allowed (for a particular versio...
Definition: RawDAQData.cpp:962
virtual bool setFunctionPointers()=0
std::ostream & operator<<(std::ostream &os, const daqdataformats::RawDAQData &dataformat)
operator << so that one can put print it into an output stream directly
bool resizeInternalBuffer(const uint32_t size_of_data)
Resize the internal buffer.
Definition: RawDAQData.cpp:292
enum BeamMode string