mu2e_artdaq_core  v1_03_01
 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 "Header" struct contains "metadata" specific to the fragment
31  // which is not hardware-related
32 
33  // Header::data_t -- not to be confused with Metadata::data_t ! --
34  // describes the standard size of a data type not just for the
35  // header data, but ALSO the physics data beyond it; the size of the
36  // header in units of Header::data_t is given by "size_words", and
37  // the size of the fragment beyond the header in units of
38  // Header::data_t is given by "event_size"
39  struct Header
40  {
41  typedef uint8_t data_t;
42  typedef uint16_t data_size_t;
43  typedef uint64_t timestamp_t;
44 
45  // Word 0
46  data_size_t BlockByteCount;
47  // Word 1
48  data_t SubsystemID : 4;
49  data_t PacketType : 4;
50  data_t ROCID : 3;
51  data_t unused1 : 4;
52  data_t Valid : 1;
53  // Word 2
54  data_size_t PacketCount : 11;
55  data_size_t unused2 : 5;
56  // Word 3-5
57  data_size_t TimestampLow;
58  data_size_t TimestampMed;
59  data_size_t TimestampHigh;
60  // Word 6
61  data_t Status;
62  data_t FormatVersion;
63  // Word 7
64  data_t DTCID;
65  data_t EVBMode;
66 
67  static size_t const size_words = 16ul;
68  };
69 
70  static_assert(sizeof(Header) == Header::size_words * sizeof(Header::data_t), "DTCFragment::Header size changed");
71 
72  // The constructor simply sets its const private member "artdaq_Fragment_"
73  // to refer to the artdaq::Fragment object
74 
75  DTCFragment(artdaq::Fragment const &f)
76  : artdaq_Fragment_(f) {}
77 
78  // const getter functions for the data in the header
79  Header::data_size_t hdr_byte_count() const { return header_()->BlockByteCount; }
80  Header::data_t hdr_subsystem_id() const { return header_()->SubsystemID; }
81  Header::data_t hdr_packet_type() const { return header_()->PacketType; }
82  Header::data_t hdr_roc_id() const { return header_()->ROCID; }
83  bool hdr_is_valid() const { return header_()->Valid; }
84  Header::data_t hdr_status() const { return header_()->Status; }
85  Header::data_t hdr_data_format_version() const { return header_()->FormatVersion; }
86  Header::data_t hdr_dtc_id() const { return header_()->DTCID; }
87  Header::data_t hdr_evb_mode() const { return header_()->EVBMode; }
88  Header::data_size_t hdr_packet_count() const { return header_()->PacketCount; }
89  Header::timestamp_t hdr_timestamp() const
90  {
91  return static_cast<Header::timestamp_t>(header_()->TimestampLow)
92  + (static_cast<Header::timestamp_t>(header_()->TimestampMed) << 16)
93  + (static_cast<Header::timestamp_t>(header_()->TimestampHigh) << 32);
94  }
95 
96  static constexpr size_t hdr_size_words() { return Header::size_words; }
97 
98  size_t dataSize() const { return hdr_packet_count() * words_per_packet_(); }
99 
100  // Start of the DTC packets, returned as a pointer to the packet type
101  packet_t const *dataBegin() const { return reinterpret_cast<packet_t const *>(header_() + 1); }
102 
103  // End of the DTC packets, returned as a pointer to the packet type
104  packet_t const *dataEnd() const { return dataBegin() + hdr_packet_count(); }
105 
106 protected:
107  // Functions to translate between size (in bytes) of a DTC packet, size of
108  // this fragment overlay's concept of a unit of data (i.e.,
109  // Header::data_t) and size of an artdaq::Fragment's concept of a
110  // unit of data (the artdaq::Fragment::value_type).
111 
112  static constexpr size_t words_per_packet_() { return sizeof(packet_t) / sizeof(Header::data_t); }
113 
114  static constexpr size_t words_per_frag_word_()
115  {
116  return sizeof(artdaq::Fragment::value_type) / sizeof(Header::data_t);
117  }
118 
119  // header_() simply takes the address of the start of this overlay's
120  // data (i.e., where the DTCFragment::Header object begins) and
121  // casts it as a pointer to DTCFragment::Header
122 
123  Header const *header_() const
124  {
125  return reinterpret_cast<DTCFragment::Header const *>(&*artdaq_Fragment_.dataBegin());
126  }
127 
128 private:
129  artdaq::Fragment const &artdaq_Fragment_;
130 };
131 
132 #endif /* mu2e_artdaq_Overlays_DTCFragment_hh */