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