otsdaq_mu2e  v2_04_02
DataGenFragment.hh
1 #ifndef artdaq_ots_Overlays_DataGenFragment_hh
2 #define artdaq_ots_Overlays_DataGenFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 #include "cetlib/exception.h"
6 
7 #include <ostream>
8 #include <vector>
9 
10 // Implementation of "DataGenFragment", an artdaq::Fragment overlay class
11 
12 namespace ots
13 {
14 class DataGenFragment;
15 
16 // Let the "<<" operator dump the DataGenFragment's data to stdout
17 std::ostream& operator<<(std::ostream&, DataGenFragment 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  // DataGenFragment::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),
43  "DataGenFragment::Metadata size changed");
44 
45  // The "Header" struct contains "metadata" specific to the fragment
46  // which is not hardware-related
47 
48  struct Header
49  {
50  typedef uint8_t data_t;
51 
52  data_t type;
53  data_t sequence;
54 
55  static size_t const size_words = 2ul; // Units of Header::data_t
56  };
57 
58  static_assert(sizeof(Header) == Header::size_words * sizeof(Header::data_t),
59  "DataGenFragment::Header size changed");
60 
61  struct DataBlob
62  {
63  double data[10];
64  };
65 
66  // The constructor simply sets its const private member "artdaq_Fragment_"
67  // to refer to the artdaq::Fragment object
68 
69  DataGenFragment(artdaq::Fragment const& f) : artdaq_Fragment_(f) {}
70 
71  // const getter functions for the data in the header
72 
73  // Start of the data, returned as a pointer
74  DataBlob const* dataBegin() const
75  {
76  return reinterpret_cast<DataBlob const*>(header_() + 1);
77  // return reinterpret_cast<DataBlob const *>(header_());
78  }
79  // 0 - type
80  // 1 - seq
81  // 2-9 - data
82 
83  // End of the data, returned as a pointer
84  DataBlob const* dataEnd() const
85  {
86  return dataBegin() + (artdaq_Fragment_.dataSize() / sizeof(DataBlob));
87  }
88 
89  protected:
90  // header_() simply takes the address of the start of this overlay's
91  // data (i.e., where the DataGenFragment::Header object begins) and
92  // casts it as a pointer to DataGenFragment::Header
93 
94  Header const* header_() const
95  {
96  return reinterpret_cast<DataGenFragment::Header const*>(
97  artdaq_Fragment_.dataBeginBytes());
98  }
99 
100  private:
101  artdaq::Fragment const& artdaq_Fragment_;
102 };
103 
104 #endif /* artdaq_ots_core_Overlays_DataGenFragment_hh */