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