ToyFragmentWriter.hh
Go to the documentation of this file.
1 #ifndef dune_artdaq_Overlays_ToyFragmentWriter_hh
2 #define dune_artdaq_Overlays_ToyFragmentWriter_hh
3 
4 ////////////////////////////////////////////////////////////////////////
5 // ToyFragmentWriter
6 //
7 // Class derived from ToyFragment 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 // ToyFragment, 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 //
15 ////////////////////////////////////////////////////////////////////////
16 
17 #include "artdaq-core/Data/Fragment.hh"
19 
20 #include <iostream>
21 
22 namespace dune {
23  class ToyFragmentWriter;
24 }
25 
26 
28 public:
29 
30 
31  ToyFragmentWriter(artdaq::Fragment & f);
32 
33  // These functions form overload sets with const functions from
34  // dune::ToyFragment
35 
36  adc_t * dataBegin();
37  adc_t * dataEnd();
38 
39  // We'll need to hide the const version of header in ToyFragment in
40  // order to be able to perform writes
41 
43  assert(artdaq_Fragment_.dataSizeBytes() >= sizeof(Header) );
44  return reinterpret_cast<Header *>( artdaq_Fragment_.dataBeginBytes());
45  }
46 
48  header_()->run_number = run_number;
49  }
50 
51  void resize(size_t nAdcs);
52 
53 private:
54  size_t calc_event_size_words_(size_t nAdcs);
55 
56  static size_t adcs_to_words_(size_t nAdcs);
57 
58  // Note that this non-const reference hides the const reference in the base class
59  artdaq::Fragment & artdaq_Fragment_;
60 };
61 
62 // The constructor will expect the artdaq::Fragment object it's been
63 // passed to contain the artdaq::Fragment header + the
64 // ToyFragment::Metadata object, otherwise it throws
65 
68 
69  // If this assert doesn't hold, then can't call
70  // "words_to_frag_words_" below, translating between the
71  // ToyFragment's standard data type size and the
72  // artdaq::Fragment's data type size, on the Metadata object
73 
74  assert( sizeof(Metadata::data_t) == sizeof(Header::data_t) );
75 
76 
77  if (! artdaq_Fragment_.hasMetadata() ) {
78  throw cet::exception("Error in ToyFragmentWriter: passed artdaq::Fragment object doesn't contain metadata");
79  }
80 
81  if (artdaq_Fragment_.dataSizeBytes() > 0) {
82  throw cet::exception("Error in ToyFragmentWriter: passed artdaq::Fragment object already contains payload");
83  }
84 
85  // Allocate space for the header
86  artdaq_Fragment_.resizeBytes( sizeof(Header) );
87 }
88 
89 
91  assert(artdaq_Fragment_.dataSizeBytes() > sizeof(Header) );
92  return reinterpret_cast<adc_t *>(header_() + 1);
93 }
94 
96  return dataBegin() + total_adc_values();
97 }
98 
99 
100 inline void dune::ToyFragmentWriter::resize(size_t nAdcs) {
101  auto es(calc_event_size_words_(nAdcs));
102  artdaq_Fragment_.resizeBytes( sizeof(Header::data_t) * es );
103  header_()->event_size = es;
104 }
105 
107  return adcs_to_words_(nAdcs) + hdr_size_words();
108 }
109 
110 inline size_t dune::ToyFragmentWriter::adcs_to_words_(size_t nAdcs) {
111  auto mod(nAdcs % adcs_per_word_());
112  return (mod == 0) ?
113  nAdcs / adcs_per_word_() :
114  nAdcs / adcs_per_word_() + 1;
115 }
116 
117 #endif /* dune_artdaq_Overlays_ToyFragmentWriter_hh */
static constexpr size_t hdr_size_words()
Definition: ToyFragment.hh:94
static constexpr size_t adcs_per_word_()
Definition: ToyFragment.hh:144
size_t total_adc_values() const
Definition: ToyFragment.hh:97
void resize(size_t nAdcs)
static size_t adcs_to_words_(size_t nAdcs)
ToyFragmentWriter(artdaq::Fragment &f)
void set_hdr_run_number(Header::run_number_t run_number)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
size_t calc_event_size_words_(size_t nAdcs)
artdaq::Fragment & artdaq_Fragment_