otsdaq  v2_05_02_indev
UDPFragment.hh
1 #ifndef artdaq_ots_Overlays_UDPFragment_hh
2 #define artdaq_ots_Overlays_UDPFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 #include "cetlib_except/exception.h"
6 
7 #include <ostream>
8 #include <vector>
9 
10 // Implementation of "UDPFragment", an artdaq::Fragment overlay class
11 
12 namespace ots
13 {
14 class UDPFragment;
15 
16 // Let the "<<" operator dump the UDPFragment's data to stdout
17 std::ostream& operator<<(std::ostream&, UDPFragment const&);
18 } // namespace ots
19 
21 {
22  public:
23  // The "Metadata" struct is used to store info primarily related to
24  // the upstream hardware environment from where the fragment came
25 
26  // "data_t" is a typedef of the fundamental unit of data the
27  // metadata structure thinks of itself as consisting of; it can give
28  // its size via the static "size_words" variable (
29  // UDPFragment::Metadata::size_words )
30 
31  struct Metadata
32  {
33  typedef uint64_t data_t;
34 
35  data_t port : 16;
36  data_t address : 32;
37  data_t unused : 16;
38 
39  static size_t const size_words = 1ull; // Units of Metadata::data_t
40  };
41 
42  static_assert(sizeof(Metadata) == Metadata::size_words * sizeof(Metadata::data_t), "UDPFragment::Metadata size changed");
43 
44  // The "Header" struct contains "metadata" specific to the fragment
45  // which is not hardware-related
46 
47  // Header::data_t -- not to be confused with Metadata::data_t ! --
48  // describes the standard size of a data type not just for the
49  // header data, but ALSO the physics data beyond it; the size of the
50  // header in units of Header::data_t is given by "size_words", and
51  // the size of the fragment beyond the header in units of
52  // Header::data_t is given by "event_size"
53 
54  // Notice only the first 28 bits of the first 32-bit unsigned
55  // integer in the Header is used to hold the event_size ; this means
56  // that you can't represent a fragment larger than 2**28 units of
57  // data_t, or 1,073,741,824 bytes
58 
59  struct Header
60  {
61  typedef uint32_t data_t;
62 
63  typedef uint32_t event_size_t;
64  typedef uint32_t data_type_t;
65 
66  event_size_t event_size : 28;
67  event_size_t type : 4;
68 
69  static size_t const size_words = 1ul; // Units of Header::data_t
70  };
71 
72  static_assert(sizeof(Header) == Header::size_words * sizeof(Header::data_t), "UDPFragment::Header size changed");
73 
74  // The constructor simply sets its const private member "artdaq_Fragment_"
75  // to refer to the artdaq::Fragment object
76 
77  UDPFragment(artdaq::Fragment const& f) : artdaq_Fragment_(f) {}
78 
79  // const getter functions for the data in the header
80 
81  Header::event_size_t hdr_event_size() const { return header_()->event_size; }
82  Header::data_type_t hdr_data_type() const { return header_()->type; }
83  static constexpr size_t hdr_size_words() { return Header::size_words; }
84 
85  // UDP Data Word Count
86  size_t udp_data_words() const { return (hdr_event_size() - hdr_size_words()) * bytes_per_word_(); }
87 
88  // Start of the UDP data, returned as a pointer
89  uint8_t const* dataBegin() const { return reinterpret_cast<uint8_t const*>(header_() + 1); }
90 
91  // End of the UDP data, returned as a pointer
92  uint8_t const* dataEnd() const { return dataBegin() + udp_data_words(); }
93 
94  protected:
95  // Functions to translate between byte size and the size of
96  // this fragment overlay's concept of a unit of data (i.e.,
97  // Header::data_t).
98 
99  static constexpr size_t bytes_per_word_() { return sizeof(Header::data_t) / sizeof(uint8_t); }
100 
101  // header_() simply takes the address of the start of this overlay's
102  // data (i.e., where the UDPFragment::Header object begins) and
103  // casts it as a pointer to UDPFragment::Header
104 
105  Header const* header_() const { return reinterpret_cast<UDPFragment::Header const*>(artdaq_Fragment_.dataBeginBytes()); }
106 
107  private:
108  artdaq::Fragment const& artdaq_Fragment_;
109 };
110 
111 #endif /* artdaq_ots_Overlays_UDPFragment_hh */