otsdaq_demo  v2_05_02_indev
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 class
53  artdaq::Fragment& artdaq_Fragment_;
54 };
55 
56 // The constructor will expect the artdaq::Fragment object it's been
57 // passed to contain the artdaq::Fragment header + the
58 // DataGenFragment::Metadata object, otherwise it throws
59 
60 ots::DataGenFragmentWriter::DataGenFragmentWriter(artdaq::Fragment& f)
61  : DataGenFragment(f), artdaq_Fragment_(f)
62 {
63  if(!f.hasMetadata() || f.dataSizeBytes() > 0)
64  {
65  throw cet::exception(
66  "Error in DataGenFragmentWriter: Raw artdaq::Fragment object does not appear "
67  "to consist of (and only of) its own header + the DataGenFragment::Metadata "
68  "object");
69  }
70 
71  // Allocate space for the header
72  artdaq_Fragment_.resizeBytes(sizeof(Header));
73 }
74 
75 inline ots::DataGenFragment::DataBlob* ots::DataGenFragmentWriter::dataBegin()
76 {
77  // Make sure there's data past the DataGenFragment header
78  assert(artdaq_Fragment_.dataSizeBytes() >=
79  sizeof(Header) + sizeof(artdaq::Fragment::value_type));
80  return reinterpret_cast<DataBlob*>(header_() + 1);
81 }
82 
83 inline ots::DataGenFragment::DataBlob* ots::DataGenFragmentWriter::dataEnd()
84 {
85  return dataBegin() + artdaq_Fragment_.dataSize() / sizeof(DataBlob);
86 }
87 
88 inline void ots::DataGenFragmentWriter::resize(size_t nBytes)
89 {
90  artdaq_Fragment_.resizeBytes(nBytes);
91 }
92 
93 #endif /* artdaq_demo_Overlays_DataGenFragmentWriter_hh */