mu2e_artdaq_core  v1_02_16
 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_except/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 } // namespace mu2e
23 
25 {
26 public:
27  typedef uint16_t adc_t;
28 
29  ArtFragment(artdaq::Fragment const &f)
30  : artdaq_Fragment_(f)
31  {
32  // Initialize the index array
33  block_count_ = 0;
34  size_t curIndex = 0;
35  const adc_t *curPos = dataBegin();
36  if (dataBegin() != dataEnd()) {
37  while (curPos < dataEnd()) {
38  block_count_++;
39  // The first position in the index array corresponds to the second DataBlock (the first always has index 0)
40  index[block_count_ - 1] = curIndex + *(curPos);
41  curIndex += *(curPos);
42  curPos = curPos + *(curPos) / 2; // Increment curPos by the number of 16 bit values in the current DataBlock
43  }
44  }
45  // assert(block_count_ < BLOCK_COUNT_MAXIMUM, "Block count exceeds maximum");
46  }
47 
48  // Start of the DTC packets, returned as a pointer to the packet type
49  adc_t const *dataBegin() const
50  {
51  return reinterpret_cast<mu2e::ArtFragment::adc_t const *>(&*artdaq_Fragment_.dataBegin());
52  }
53 
54  adc_t const *dataEnd() const
55  {
56  return reinterpret_cast<mu2e::ArtFragment::adc_t const *>(&*artdaq_Fragment_.dataEnd());
57  }
58 
59  // const getter functions for the data in the header
60  size_t block_count() const { return block_count_; }
61 
62  size_t byte_count() const { return index[block_count_ - 1]; }
63 
64  // Convert index in number of DataBlocks to index in bytes
65  size_t blockIndexBytes(size_t offset) const
66  {
67  if (offset >= block_count()) {
68  return 0;
69  }
70  else if (offset == 0)
71  {
72  return 0;
73  }
74  return index[offset - 1];
75  }
76 
77  size_t blockEndBytes(size_t offset) const
78  {
79  if (offset >= block_count()) {
80  return byte_count();
81  }
82  return index[offset];
83  }
84 
85  // Return size of block at given DataBlock index
86  size_t blockSizeBytes(size_t offset) const
87  {
88  if (offset > block_count() - 1) {
89  return 0;
90  }
91  else if (offset == 0)
92  {
93  return index[offset];
94  }
95  return index[offset] - index[offset - 1];
96  }
97 
98  // Return pointer to beginning of DataBlock at given byte index
99  adc_t const *dataAtBytes(size_t offset) const { return dataBegin() + (offset / 2); }
100 
101  // Return pointer to beginning of DataBlock at given DataBlock index
102  adc_t const *dataAtBlockIndex(size_t offset) const { return dataAtBytes(blockIndexBytes(offset)); }
103 
104  void printPacketAtByte(size_t byteIdx) const
105  {
106  std::cout << "\t\t"
107  << "Packet Bits (128): " << std::endl;
108  for (int adcIdx = 0; adcIdx < 8; adcIdx++) {
109  std::cout << "\t";
110  for (int offset = 15; offset >= 0; offset--) {
111  if (((*((adc_t const *)(dataAtBytes(byteIdx) + adcIdx))) & (1 << offset)) != 0) {
112  std::cout << "1";
113  }
114  else
115  {
116  std::cout << "0";
117  }
118  if (offset == 7) {
119  std::cout << " ";
120  }
121  else if (offset == 0)
122  {
123  std::cout << std::endl;
124  }
125  }
126  }
127  std::cout << std::endl;
128  return;
129  }
130 
131 private:
132  artdaq::Fragment const &artdaq_Fragment_;
133  size_t block_count_;
134  size_t index[DATA_BLOCKS_PER_MU2E_FRAGMENT];
135 };
136 
137 #endif /* mu2e_artdaq_Overlays_ArtFragment_hh */