mu2e_artdaq_core  v1_02_05
 All Classes Functions
ArtFragment.hh
1 #ifndef mu2e_artdaq_core_Overlays_ArtFragment_hh
2 #define mu2e_artdaq_core_Overlays_ArtFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 #include "cetlib/exception.h"
6 
7 #include <iostream>
8 
9 #include <ostream>
10 #include <vector>
11 
12 // Implementation of "ArtFragment", an artdaq::Fragment overlay class
13 
14 // The "packing factor": How many DataBlocks are stored in each ArtFragment
15 #define DATA_BLOCKS_PER_MU2E_FRAGMENT 2500
16 
17 namespace mu2e {
18  class ArtFragment;
19 
20  // Let the "<<" operator dump the ArtFragment's data to stdout
21  std::ostream & operator << (std::ostream &, ArtFragment const &);
22 }
23 
25 public:
26 
27  typedef uint16_t adc_t;
28 
29  ArtFragment(artdaq::Fragment const & f ) :
30  artdaq_Fragment_(f)
31  {
32 
33  // Initialize the index array
34  block_count_ = 0;
35  size_t curIndex = 0;
36  const adc_t* curPos = dataBegin();
37  if(dataBegin()!=dataEnd()) {
38  while(curPos<dataEnd()) {
39  block_count_++;
40  // The first position in the index array corresponds to the second DataBlock (the first always has index 0)
41  index[block_count_-1] = curIndex + *(curPos);
42  curIndex += *(curPos);
43  curPos = curPos + *(curPos) / 2; // Increment curPos by the number of 16 bit values in the current DataBlock
44  }
45  }
46  // assert(block_count_ < BLOCK_COUNT_MAXIMUM, "Block count exceeds maximum");
47  }
48 
49  // Start of the DTC packets, returned as a pointer to the packet type
50  adc_t const * dataBegin() const {
51  return reinterpret_cast<mu2e::ArtFragment::adc_t const *>(&*artdaq_Fragment_.dataBegin());
52  }
53 
54  adc_t const * dataEnd() const {
55  return reinterpret_cast<mu2e::ArtFragment::adc_t const *>(&*artdaq_Fragment_.dataEnd());
56  }
57 
58  // const getter functions for the data in the header
59  size_t block_count() const { return block_count_; }
60 
61  size_t byte_count() const {
62  return index[block_count_-1];
63  }
64 
65  // Convert index in number of DataBlocks to index in bytes
66  size_t blockIndexBytes(size_t offset) const {
67  if(offset>=block_count()) { return 0; }
68  else if(offset==0) { return 0;}
69  return index[ offset-1 ];
70  }
71 
72  size_t blockEndBytes(size_t offset) const {
73  if(offset>=block_count()) { return byte_count(); }
74  return index[ offset ];
75  }
76 
77 
78  // Return size of block at given DataBlock index
79  size_t blockSizeBytes(size_t offset) const {
80  if(offset > block_count()-1) { return 0; }
81  else if(offset==0) { return index[ offset ]; }
82  return index[ offset ] - index[ offset-1 ];
83  }
84 
85  // Return pointer to beginning of DataBlock at given byte index
86  adc_t const * dataAtBytes(size_t offset) const {
87  return dataBegin() + (offset/2);
88  }
89 
90  // Return pointer to beginning of DataBlock at given DataBlock index
91  adc_t const * dataAtBlockIndex(size_t offset) const {
92  return dataAtBytes(blockIndexBytes(offset));
93  }
94 
95  void printPacketAtByte(size_t byteIdx) const {
96  std::cout << "\t\t" << "Packet Bits (128): " << std::endl ;
97  for(int adcIdx = 0; adcIdx<8; adcIdx++) {
98  std::cout << "\t";
99  for(int offset = 15; offset>=0; offset--) {
100  if( ( (*((adc_t const *)(dataAtBytes(byteIdx)+adcIdx))) & (1<<offset) ) != 0) {
101  std::cout << "1";
102  } else {
103  std::cout << "0";
104  }
105  if(offset==7) {
106  std::cout << " ";
107  } else if(offset==0) {
108  std::cout << std::endl;
109  }
110  }
111  }
112  std::cout << std::endl;
113  return;
114  }
115 
116 private:
117  artdaq::Fragment const & artdaq_Fragment_;
118  size_t block_count_;
119  size_t index[DATA_BLOCKS_PER_MU2E_FRAGMENT];
120 };
121 
122 #endif /* mu2e_artdaq_Overlays_ArtFragment_hh */