otsdaq_mu2e  v2_04_02
DataGenFragmentWriter.hh
1 #ifndef artdaq_ots_Overlays_DataGenFragmentWriter_hh
2 #define artdaq_ots_Overlays_DataGenFragmentWriter_hh
3 
5 // DataGenFragmentWriter
6 //
7 // Class derived from DataGenFragment which allows writes to the data (for
8 // simulation purposes). Note that for this reason it contains
9 // non-const members which hide the const members in its parent class,
10 // DataGenFragment, including its reference to the artdaq::Fragment
11 // object, artdaq_Fragment_, as well as its functions pointing to the
12 // beginning and end of ADC values in the fragment, dataBegin() and
13 // dataEnd()
14 //
16 
17 #include "artdaq-core/Data/Fragment.hh"
18 #include "otsdaq-demo/Overlays/DataGenFragment.hh"
19 
20 #include <iostream>
21 
22 namespace ots
23 {
24 class DataGenFragmentWriter;
25 }
26 
28 {
29  public:
30  DataGenFragmentWriter(artdaq::Fragment& f);
31 
32  // These functions form overload sets with const functions from
33  // ots::DataGenFragment
34 
35  DataBlob* dataBegin();
36  DataBlob* dataEnd();
37 
38  // We'll need to hide the const version of header in DataGenFragment in
39  // order to be able to perform writes
40 
41  Header* header_()
42  {
43  assert(artdaq_Fragment_.dataSizeBytes() >= sizeof(Header));
44  return reinterpret_cast<Header*>(artdaq_Fragment_.dataBeginBytes());
45  }
46 
47  void resize(size_t nBytes);
48 
49  int asksize() { return artdaq_Fragment_.dataSizeBytes(); }
50 
51  private:
52  // Note that this non-const reference hides the const reference in the base
53  // class
54  artdaq::Fragment& artdaq_Fragment_;
55 };
56 
57 // The constructor will expect the artdaq::Fragment object it's been
58 // passed to contain the artdaq::Fragment header + the
59 // DataGenFragment::Metadata object, otherwise it throws
60 
61 ots::DataGenFragmentWriter::DataGenFragmentWriter(artdaq::Fragment& f)
62  : DataGenFragment(f), artdaq_Fragment_(f)
63 {
64  if(!f.hasMetadata() || f.dataSizeBytes() > 0)
65  {
66  throw cet::exception(
67  "Error in DataGenFragmentWriter: Raw artdaq::Fragment "
68  "object does not appear to consist of (and only of) "
69  "its own header + the DataGenFragment::Metadata "
70  "object");
71  }
72 
73  // Allocate space for the header
74  artdaq_Fragment_.resizeBytes(sizeof(Header));
75 }
76 
77 inline ots::DataGenFragment::DataBlob* ots::DataGenFragmentWriter::dataBegin()
78 {
79  // Make sure there's data past the DataGenFragment header
80  assert(artdaq_Fragment_.dataSizeBytes() >=
81  sizeof(Header) + sizeof(artdaq::Fragment::value_type));
82  return reinterpret_cast<DataBlob*>(header_() + 1);
83 }
84 
85 inline ots::DataGenFragment::DataBlob* ots::DataGenFragmentWriter::dataEnd()
86 {
87  return dataBegin() + artdaq_Fragment_.dataSize() / sizeof(DataBlob);
88 }
89 
90 inline void ots::DataGenFragmentWriter::resize(size_t nBytes)
91 {
92  artdaq_Fragment_.resizeBytes(nBytes);
93 }
94 
95 #endif /* artdaq_demo_Overlays_DataGenFragmentWriter_hh */