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