mu2e_artdaq_core  v1_02_09
 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 }
21 
23  public:
24 
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), "DTCFragment::Metadata size changed");
56 
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  typedef uint8_t data_t;
70 
71  typedef uint64_t timestamp_t;
72  typedef uint32_t data_size_t;
73 
74  data_size_t event_size;
75 
76  timestamp_t timestamp : 48;
77  timestamp_t unused : 16;
78 
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 ) : artdaq_Fragment_(f) {}
89 
90  // const getter functions for the data in the header
91  Header::data_size_t hdr_packet_count() const { return header_()->event_size; }
92  Header::timestamp_t hdr_timestamp() const { return header_()->timestamp; }
93 
94  static constexpr size_t hdr_size_words() { return Header::size_words; }
95 
96  size_t dataSize() const {
97  return hdr_packet_count() * words_per_packet_();
98  }
99 
100  // Start of the DTC packets, returned as a pointer to the packet type
101  packet_t const * dataBegin() const {
102  return reinterpret_cast<packet_t const *>(header_() + 1);
103  }
104 
105  // End of the DTC packets, returned as a pointer to the packet type
106  packet_t const * dataEnd() const {
107  return dataBegin() + hdr_packet_count();
108  }
109 
110  protected:
111 
112  // Functions to translate between size (in bytes) of a DTC packet, size of
113  // this fragment overlay's concept of a unit of data (i.e.,
114  // Header::data_t) and size of an artdaq::Fragment's concept of a
115  // unit of data (the artdaq::Fragment::value_type).
116 
117  static constexpr size_t words_per_packet_() {
118  return sizeof(packet_t) / sizeof(Header::data_t);
119  }
120 
121  static constexpr size_t words_per_frag_word_() {
122  return sizeof(artdaq::Fragment::value_type) / sizeof(Header::data_t);
123  }
124 
125  // header_() simply takes the address of the start of this overlay's
126  // data (i.e., where the DTCFragment::Header object begins) and
127  // casts it as a pointer to DTCFragment::Header
128 
129  Header const * header_() const {
130  return reinterpret_cast<DTCFragment::Header const *>(&*artdaq_Fragment_.dataBegin());
131  }
132 
133 private:
134 
135  artdaq::Fragment const & artdaq_Fragment_;
136 };
137 
138 #endif /* mu2e_artdaq_Overlays_DTCFragment_hh */