artdaq_core_demo  v1_07_01
ToyFragment.hh
1 #ifndef artdaq_demo_Overlays_ToyFragment_hh
2 #define artdaq_demo_Overlays_ToyFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 
6 #include <ostream>
7 
8 namespace demo {
9 class ToyFragment;
10 
12 std::ostream& operator<<(std::ostream&, ToyFragment const&);
13 } // namespace demo
14 
26 {
27 public:
44  struct Metadata
45  {
46  // "data_t" is a typedef of the fundamental unit of data the
47  // metadata structure thinks of itself as consisting of; it can give
48  // its size via the static "size_words" variable (
49  // ToyFragment::Metadata::size_words )
50 
51  typedef uint64_t data_t;
52 
55  data_t unused : 40;
56 
57  static size_t const size_words = 1ul;
58  };
59 
60  static_assert(sizeof(Metadata) == Metadata::size_words * sizeof(Metadata::data_t),
61  "ToyFragment::Metadata size changed");
62 
68  typedef uint16_t adc_t;
69 
76  struct Header
77  {
78  // Header::data_t -- not to be confused with Metadata::data_t ! --
79  // describes the standard size of a data type in the header.
80  // In this example, it is ALSO used to describe the size of the physics data
81  // beyond the header. This is not a general requirement for Overlay classes;
82  // it is simply the choice that was made for this example.
83  // The size of the header in units of Header::data_t is given by "size_words",
84  // and the size of the fragment beyond the header in units of
85  // Header::data_t is given by "event_size". Again, this is simply an
86  // artifact of this example. A real-life hardware module may pack its data
87  // differently, and any "size" fields in that real-life data could include
88  // the size of any header information provided by the hardware.
89 
90  typedef uint32_t data_t;
91 
92  typedef uint32_t event_size_t;
93  typedef uint32_t trigger_number_t;
94 
97 
99 
100  static size_t const size_words = 2ul;
101  };
102 
103  static_assert(sizeof(Header) == Header::size_words * sizeof(Header::data_t), "ToyFragment::Header size changed");
104 
111  explicit ToyFragment(artdaq::Fragment const& f)
112  : artdaq_Fragment_(f) {}
113 
124 
129  uint8_t hdr_distribution_type() const { return static_cast<uint8_t>(header_()->distribution_type); }
130 
135  static constexpr size_t hdr_size_words() { return Header::size_words; }
136 
141  size_t total_adc_values() const { return (hdr_event_size() - hdr_size_words()) * adcs_per_word_(); }
142 
148  adc_t adc_value(uint32_t index)
149  {
150  // Simple way to handle index out of bounds - better ways are surely possible
151  if (index >= total_adc_values())
152  {
153  return 0xffff;
154  }
155  return dataBeginADCs()[index]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
156  }
157 
162  adc_t const* dataBeginADCs() const
163  {
164  return reinterpret_cast<adc_t const*>(header_() + 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-pointer-arithmetic)
165  }
166 
171  adc_t const* dataEndADCs() const
172  {
173  return dataBeginADCs() + total_adc_values(); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
174  }
175 
176  // Functions to check if any ADC values are corrupt
187  adc_t const* findBadADC(int daq_adc_bits) const
188  {
189  return std::find_if(dataBeginADCs(), dataEndADCs(), [&](adc_t const adc) -> bool { return (adc >> daq_adc_bits); });
190  }
191 
197  bool fastVerify(int daq_adc_bits) const { return (findBadADC(daq_adc_bits) == dataEndADCs()); };
198 
204  void checkADCData(int daq_adc_bits) const;
205 
211  static size_t adc_range(int daq_adc_bits) { return (1ul << daq_adc_bits) - 1; }
212 
213 protected:
218  static constexpr size_t adcs_per_word_() { return sizeof(Header::data_t) / sizeof(adc_t); }
219 
228  Header const* header_() const
229  {
230  return reinterpret_cast<ToyFragment::Header const*>(artdaq_Fragment_.dataBeginBytes()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
231  }
232 
233 private:
234  artdaq::Fragment const& artdaq_Fragment_;
235 };
236 
237 #endif /* artdaq_demo_Overlays_ToyFragment_hh */
uint32_t event_size_t
Type used for the event size.
Definition: ToyFragment.hh:92
ToyFragment(artdaq::Fragment const &f)
The ToyFragment constructor.
Definition: ToyFragment.hh:111
event_size_t distribution_type
Which distribution_type was used to generate the Fragment data.
Definition: ToyFragment.hh:96
static size_t const size_words
Size of the ToyFragment::Metadata struct, in units of Metadata::data_t.
Definition: ToyFragment.hh:57
uint64_t data_t
Fundamental unit of data representation within the ToyFragment::Metadata.
Definition: ToyFragment.hh:51
std::ostream & operator<<(std::ostream &, AsciiFragment const &)
Dumps the AsciiFragment&#39;s data (text) to given stream.
Header::trigger_number_t hdr_trigger_number() const
Gets the trigger_number from the ToyFragment::Header.
Definition: ToyFragment.hh:123
data_t board_serial_number
Serial number of the simulated board.
Definition: ToyFragment.hh:53
adc_t const * findBadADC(int daq_adc_bits) const
Look through stored ADC values and determine if any are out-of-range.
Definition: ToyFragment.hh:187
bool fastVerify(int daq_adc_bits) const
Do any ADC values in the ToyFragment appear to be out-of-range?
Definition: ToyFragment.hh:197
adc_t adc_value(uint32_t index)
Retrieve the given ADC value from the Fragment.
Definition: ToyFragment.hh:148
Header const * header_() const
Get a pointer to the ToyFragment::Header object.
Definition: ToyFragment.hh:228
An artdaq::Fragment overlay class used for pedagogical purposes.
Definition: ToyFragment.hh:25
Header::event_size_t hdr_event_size() const
Gets the event_size from the ToyFragment::Header.
Definition: ToyFragment.hh:118
uint8_t hdr_distribution_type() const
Returns the distribution_type field from the ToyFragment::Header.
Definition: ToyFragment.hh:129
uint32_t data_t
Fundamental unit of data represenation within the ToyFragment::Header.
Definition: ToyFragment.hh:90
adc_t const * dataBeginADCs() const
Start of the ADC values, returned as a const pointer to the ADC type.
Definition: ToyFragment.hh:162
static size_t adc_range(int daq_adc_bits)
Get the largest ADC value possible for a given number of ADC bits.
Definition: ToyFragment.hh:211
static size_t const size_words
Size of the ToyFragment::Header class, in units of Header::data_t.
Definition: ToyFragment.hh:100
static constexpr size_t hdr_size_words()
Gets the size_words variable from the artdaq::Header.
Definition: ToyFragment.hh:135
The &quot;Header&quot; struct is used to interpret the header information that is created by the hardware and i...
Definition: ToyFragment.hh:76
The ToyFragment::Metadata struct holds data about the readout that may not be present in the primary ...
Definition: ToyFragment.hh:44
uint16_t adc_t
ToyFragment is intended to represent/interpret data that has an inherent size of 16 bits (unsigned)...
Definition: ToyFragment.hh:61
static constexpr size_t adcs_per_word_()
Translates between the size of an ADC and the size in Header::data_t words.
Definition: ToyFragment.hh:218
void checkADCData(int daq_adc_bits) const
Throw if any ADC value appears corrupt.
Definition: ToyFragment.cc:15
uint32_t trigger_number_t
Type used for the trigger number.
Definition: ToyFragment.hh:93
data_t num_adc_bits
Number of ADC bits this simulated board uses.
Definition: ToyFragment.hh:54
adc_t const * dataEndADCs() const
End of the ADC values, returned as a const pointer to the ADC type.
Definition: ToyFragment.hh:171
event_size_t event_size
The size of the event, in data_t words.
Definition: ToyFragment.hh:95
trigger_number_t trigger_number
The &quot;trigger number&quot; from the simulated hardware.
Definition: ToyFragment.hh:98
size_t total_adc_values() const
Get the number of ADC values describing data beyond the header.
Definition: ToyFragment.hh:141