mu2e_artdaq_core  v1_02_19
 All Classes Functions
DTCFragment.hh
1 #ifndef mu2e_artdaq_core_Overlays_DTCFragment_hh
2 #define mu2e_artdaq_core_Overlays_DTCFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 //#include "artdaq/DAQdata/features.hh"
6 #include "cetlib_except/exception.h"
7 
8 #include <ostream>
9 #include <vector>
10 
11 // Implementation of "DTCFragment", an artdaq::Fragment overlay class
12 // used for pedagogical purposes
13 
14 namespace mu2e {
15 class DTCFragment;
16 
17 typedef uint8_t packet_t[16];
18 // Let the "<<" operator dump the DTCFragment's data to stdout
19 std::ostream &operator<<(std::ostream &, DTCFragment const &);
20 } // namespace mu2e
21 
23 {
24 public:
25  // The DTCFragment represents its data through the adc_t type, which
26  // is a typedef of an 16-member uint8_t array.
27 
28  // typedef uint8_t packet_t[16];
29 
30  // The "Metadata" struct is used to store info primarily related to
31  // the upstream hardware environment from where the fragment came
32 
33  // "data_t" is a typedef of the fundamental unit of data the
34  // metadata structure thinks of itself as consisting of; it can give
35  // its size via the static "size_words" variable (
36  // DTCFragment::Metadata::size_words )
37 
38  struct Metadata
39  {
40  typedef uint8_t data_t;
41  typedef uint32_t run_number_t;
42 
43  data_t sim_mode : 4;
44  data_t unused : 4; // 4 + 4 = 8 bits
45 
46  data_t board_id;
47  data_t unused2;
48  data_t unused3;
49 
50  run_number_t run_number;
51 
52  static size_t const size_words = 8ul; // Units of Metadata::data_t
53  };
54 
55  static_assert(sizeof(Metadata) == Metadata::size_words * sizeof(Metadata::data_t),
56  "DTCFragment::Metadata size changed");
57 
58  // The "Header" struct contains "metadata" specific to the fragment
59  // which is not hardware-related
60 
61  // Header::data_t -- not to be confused with Metadata::data_t ! --
62  // describes the standard size of a data type not just for the
63  // header data, but ALSO the physics data beyond it; the size of the
64  // header in units of Header::data_t is given by "size_words", and
65  // the size of the fragment beyond the header in units of
66  // Header::data_t is given by "event_size"
67 
68  struct Header
69  {
70  typedef uint8_t data_t;
71 
72  typedef uint64_t timestamp_t;
73  typedef uint32_t data_size_t;
74 
75  data_size_t event_size;
76 
77  timestamp_t timestamp : 48;
78  timestamp_t unused : 16;
79 
80  static size_t const size_words = 16ul; // Units of Header::data_t
81  };
82 
83  static_assert(sizeof(Header) == Header::size_words * sizeof(Header::data_t), "DTCFragment::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  DTCFragment(artdaq::Fragment const &f)
89  : artdaq_Fragment_(f) {}
90 
91  // const getter functions for the data in the header
92  Header::data_size_t hdr_packet_count() const { return header_()->event_size; }
93  Header::timestamp_t hdr_timestamp() const { return header_()->timestamp; }
94 
95  static constexpr size_t hdr_size_words() { return Header::size_words; }
96 
97  size_t dataSize() const { return hdr_packet_count() * words_per_packet_(); }
98 
99  // Start of the DTC packets, returned as a pointer to the packet type
100  packet_t const *dataBegin() const { return reinterpret_cast<packet_t const *>(header_() + 1); }
101 
102  // End of the DTC packets, returned as a pointer to the packet type
103  packet_t const *dataEnd() const { return dataBegin() + hdr_packet_count(); }
104 
105 protected:
106  // Functions to translate between size (in bytes) of a DTC packet, size of
107  // this fragment overlay's concept of a unit of data (i.e.,
108  // Header::data_t) and size of an artdaq::Fragment's concept of a
109  // unit of data (the artdaq::Fragment::value_type).
110 
111  static constexpr size_t words_per_packet_() { return sizeof(packet_t) / sizeof(Header::data_t); }
112 
113  static constexpr size_t words_per_frag_word_()
114  {
115  return sizeof(artdaq::Fragment::value_type) / sizeof(Header::data_t);
116  }
117 
118  // header_() simply takes the address of the start of this overlay's
119  // data (i.e., where the DTCFragment::Header object begins) and
120  // casts it as a pointer to DTCFragment::Header
121 
122  Header const *header_() const
123  {
124  return reinterpret_cast<DTCFragment::Header const *>(&*artdaq_Fragment_.dataBegin());
125  }
126 
127 private:
128  artdaq::Fragment const &artdaq_Fragment_;
129 };
130 
131 #endif /* mu2e_artdaq_Overlays_DTCFragment_hh */