otsdaq  v2_05_02_indev
UDPFragmentWriter.hh
1 #ifndef artdaq_ots_Overlays_UDPFragmentWriter_hh
2 #define artdaq_ots_Overlays_UDPFragmentWriter_hh
3 
5 // UDPFragmentWriter
6 //
7 // Class derived from UDPFragment 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 // UDPFragment, 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 "artdaq-ots/Overlays/UDPFragment.hh"
19 
20 #include <iostream>
21 
22 namespace ots
23 {
24 class UDPFragmentWriter;
25 }
26 
28 {
29  public:
30  UDPFragmentWriter(artdaq::Fragment& f);
31 
32  // These functions form overload sets with const functions from
33  // ots::UDPFragment
34 
35  uint8_t* dataBegin();
36  uint8_t* dataEnd();
37 
38  // We'll need to hide the const version of header in UDPFragment 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 set_hdr_type(Header::data_type_t dataType) { header_()->type = dataType & 0xF; }
48 
49  void resize(size_t nBytes);
50 
51  private:
52  size_t calc_event_size_words_(size_t nBytes);
53 
54  static size_t bytes_to_words_(size_t nBytes);
55 
56  // Note that this non-const reference hides the const reference in the base class
57  artdaq::Fragment& artdaq_Fragment_;
58 };
59 
60 // The constructor will expect the artdaq::Fragment object it's been
61 // passed to contain the artdaq::Fragment header + the
62 // UDPFragment::Metadata object, otherwise it throws
63 
64 ots::UDPFragmentWriter::UDPFragmentWriter(artdaq::Fragment& f) : UDPFragment(f), artdaq_Fragment_(f)
65 {
66  if(!f.hasMetadata() || f.dataSizeBytes() > 0)
67  {
68  throw cet::exception(
69  "Error in UDPFragmentWriter: Raw artdaq::Fragment object does not appear to "
70  "consist of (and only of) its own header + the UDPFragment::Metadata object");
71  }
72 
73  // Allocate space for the header
74  artdaq_Fragment_.resizeBytes(sizeof(Header));
75 }
76 
77 inline uint8_t* ots::UDPFragmentWriter::dataBegin()
78 {
79  // Make sure there's data past the UDPFragment header
80  assert(artdaq_Fragment_.dataSizeBytes() >= sizeof(Header) + sizeof(artdaq::Fragment::value_type));
81  return reinterpret_cast<uint8_t*>(header_() + 1);
82 }
83 
84 inline uint8_t* ots::UDPFragmentWriter::dataEnd() { return dataBegin() + udp_data_words(); }
85 
86 inline void ots::UDPFragmentWriter::resize(size_t nBytes)
87 {
88  artdaq_Fragment_.resizeBytes(sizeof(Header::data_t) * calc_event_size_words_(nBytes));
89  header_()->event_size = calc_event_size_words_(nBytes);
90 }
91 
92 inline size_t ots::UDPFragmentWriter::calc_event_size_words_(size_t nBytes) { return bytes_to_words_(nBytes) + hdr_size_words(); }
93 
94 inline size_t ots::UDPFragmentWriter::bytes_to_words_(size_t nBytes)
95 {
96  auto mod(nBytes % bytes_per_word_());
97  return (mod == 0) ? nBytes / bytes_per_word_() : nBytes / bytes_per_word_() + 1;
98 }
99 
100 #endif /* artdaq_demo_Overlays_UDPFragmentWriter_hh */