ToyFragment.hh
Go to the documentation of this file.
1 #ifndef dune_artdaq_Overlays_ToyFragment_hh
2 #define dune_artdaq_Overlays_ToyFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 //#include "artdaq-core/Data/features.hh"
6 #include "cetlib_except/exception.h"
7 
8 #include <ostream>
9 #include <vector>
10 
11 // Implementation of "ToyFragment", an artdaq::Fragment overlay class
12 // used for pedagogical purposes
13 
14 namespace dune {
15  class ToyFragment;
16 
17  // Let the "<<" operator dump the ToyFragment's data to stdout
18  std::ostream & operator << (std::ostream &, ToyFragment const &);
19 }
20 
22  public:
23 
24  // The ToyFragment represents its data through the adc_t type, which
25  // is a typedef of an unsigned 16-bit integer. Note that since there
26  // are two types of ToyFragment ("TOY1" and "TOY2", declared in
27  // FragmentType.hh), the ADC type needs to be large enough to hold
28  // the ADC count with the highest number of bits.
29 
30  typedef uint16_t adc_t;
31 
32  // The "Metadata" struct is used to store info primarily related to
33  // the upstream hardware environment from where the fragment came
34 
35  // "data_t" is a typedef of the fundamental unit of data the
36  // metadata structure thinks of itself as consisting of; it can give
37  // its size via the static "size_words" variable (
38  // ToyFragment::Metadata::size_words )
39 
40  struct Metadata {
41 
42  typedef uint32_t data_t;
43 
44  data_t board_serial_number : 16;
45  data_t num_adc_bits : 8;
46  data_t unused : 8; // 16 + 8 + 8 == 32 bits
47 
48  static size_t const size_words = 1ul; // Units of Metadata::data_t
49  };
50 
51  static_assert (sizeof (Metadata) == Metadata::size_words * sizeof (Metadata::data_t), "ToyFragment::Metadata size changed");
52 
53 
54  // The "Header" struct contains "metadata" specific to the fragment
55  // which is not hardware-related
56 
57  // Header::data_t -- not to be confused with Metadata::data_t ! --
58  // describes the standard size of a data type not just for the
59  // header data, but ALSO the physics data beyond it; the size of the
60  // header in units of Header::data_t is given by "size_words", and
61  // the size of the fragment beyond the header in units of
62  // Header::data_t is given by "event_size"
63 
64  // Notice only the first 28 bits of the first 32-bit unsigned
65  // integer in the Header is used to hold the event_size ; this means
66  // that you can't represent a fragment larger than 2**28 units of
67  // data_t, or 1,073,741,824 bytes
68 
69  struct Header {
70  typedef uint32_t data_t;
71 
72  typedef uint32_t event_size_t;
73  typedef uint32_t run_number_t;
74 
75  event_size_t event_size : 28;
76  event_size_t unused_1 : 4;
77 
78  run_number_t run_number : 32;
79 
80  static size_t const size_words = 2ul; // Units of Header::data_t
81  };
82 
83  static_assert (sizeof (Header) == Header::size_words * sizeof (Header::data_t), "ToyFragment::Header size changed");
84 
85  // The constructor simply sets its const private member "artdaq_Fragment_"
86  // to refer to the artdaq::Fragment object
87 
88  ToyFragment(artdaq::Fragment const & f ) : artdaq_Fragment_(f) {}
89 
90  // const getter functions for the data in the header
91 
94  static constexpr size_t hdr_size_words() { return Header::size_words; }
95 
96  // The number of ADC values describing data beyond the header
97  size_t total_adc_values() const {
98  return (hdr_event_size() - hdr_size_words()) * adcs_per_word_();
99  }
100 
101  // Start of the ADC values, returned as a pointer to the ADC type
102  adc_t const * dataBegin() const {
103  return reinterpret_cast<adc_t const *>(header_() + 1);
104  }
105 
106  // End of the ADC values, returned as a pointer to the ADC type
107  adc_t const * dataEnd() const {
108  return dataBegin() + total_adc_values();
109  }
110 
111  // Functions to check if any ADC values are corrupt
112 
113  // findBadADC() checks to make sure that the ADC type (adc_t) variable
114  // holding the ADC value doesn't contain bits beyond the expected
115  // range, i.e., can't be evaluated to a larger value than the max
116  // permitted ADC value
117 
118  adc_t const * findBadADC(int daq_adc_bits) const {
119  return std::find_if(dataBegin(), dataEnd(),
120  [&](adc_t const adc) -> bool {
121  return (adc >> daq_adc_bits); });
122  }
123 
124  bool fastVerify(int daq_adc_bits) const {
125  return (findBadADC(daq_adc_bits) == dataEnd());
126  };
127 
128  // Defined in ToyFragment.cc, this throws if any ADC appears corrupt
129  void checkADCData(int daq_adc_bits) const;
130 
131 
132  // Largest ADC value possible
133  size_t adc_range(int daq_adc_bits) {
134  return (1ul << daq_adc_bits );
135  }
136 
137  protected:
138 
139  // Functions to translate between size (in bytes) of an ADC, size of
140  // this fragment overlay's concept of a unit of data (i.e.,
141  // Header::data_t) and size of an artdaq::Fragment's concept of a
142  // unit of data (the artdaq::Fragment::value_type).
143 
144  static constexpr size_t adcs_per_word_() {
145  return sizeof(Header::data_t) / sizeof(adc_t);
146  }
147 
148  // header_() simply takes the address of the start of this overlay's
149  // data (i.e., where the ToyFragment::Header object begins) and
150  // casts it as a pointer to ToyFragment::Header
151 
152  Header const * header_() const {
153  return reinterpret_cast<ToyFragment::Header const *>( artdaq_Fragment_.dataBeginBytes());
154  }
155 
156 private:
157 
158  artdaq::Fragment const & artdaq_Fragment_;
159 };
160 
161 #endif /* dune_artdaq_Overlays_ToyFragment_hh */
static constexpr size_t hdr_size_words()
Definition: ToyFragment.hh:94
std::ostream & operator<<(std::ostream &out, CTBFragment const &f)
Definition: CTBFragment.cc:6
adc_t const * findBadADC(int daq_adc_bits) const
Definition: ToyFragment.hh:118
ToyFragment(artdaq::Fragment const &f)
Definition: ToyFragment.hh:88
bool fastVerify(int daq_adc_bits) const
Definition: ToyFragment.hh:124
int16_t adc
Definition: CRTFragment.hh:202
static constexpr size_t adcs_per_word_()
Definition: ToyFragment.hh:144
size_t total_adc_values() const
Definition: ToyFragment.hh:97
adc_t const * dataEnd() const
Definition: ToyFragment.hh:107
Header::event_size_t hdr_event_size() const
Definition: ToyFragment.hh:92
artdaq::Fragment const & artdaq_Fragment_
Definition: ToyFragment.hh:158
adc_t const * dataBegin() const
Definition: ToyFragment.hh:102
size_t adc_range(int daq_adc_bits)
Definition: ToyFragment.hh:133
static size_t const size_words
Definition: ToyFragment.hh:48
Header const * header_() const
Definition: ToyFragment.hh:152
void checkADCData(int daq_adc_bits) const
Definition: ToyFragment.cc:17
Header::run_number_t hdr_run_number() const
Definition: ToyFragment.hh:93
static size_t const size_words
Definition: ToyFragment.hh:80