SSPFragmentWriter.hh
Go to the documentation of this file.
1 #ifndef artdaq_dune_Overlays_SSPFragmentWriter_hh
2 #define artdaq_dune_Overlays_SSPFragmentWriter_hh
3 
4 ////////////////////////////////////////////////////////////////////////
5 // SSPFragmentWriter
6 //
7 // Class derived from SSPFragment 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 // SSPFragment, 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 SSPFragmentWriter;
24 }
25 
26 
28 public:
29 
30 
31  SSPFragmentWriter(artdaq::Fragment & f);
32 
33  // These functions form overload sets with const functions from
34  // dune::SSPFragment
35 
36  adc_t * dataBegin();
37  adc_t * dataEnd();
38 
39  // We'll need to hide the const version of header in SSPFragment 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 // SSPFragment::Metadata object, otherwise it throws
65 
68 
69  if ( ! f.hasMetadata() || f.dataSizeBytes() > 0 ) {
70  throw cet::exception("Error in SSPFragmentWriter: Raw artdaq::Fragment object does not appear to consist of (and only of) its own header + the SSPFragment::Metadata object");
71  }
72 
73  // Allocate space for the header
74  artdaq_Fragment_.resizeBytes( sizeof(Header) );
75 }
76 
77 
79  // Make sure there's data past the SSPFragment header
80  assert(artdaq_Fragment_.dataSizeBytes() >= sizeof(Header) + sizeof(artdaq::Fragment::value_type) );
81  return reinterpret_cast<adc_t *>(header_() + 1);
82 }
83 
85  return dataBegin() + total_adc_values();
86 }
87 
88 
89 inline void dune::SSPFragmentWriter::resize(size_t nAdcs) {
90 
91  artdaq_Fragment_.resizeBytes(sizeof(Header::data_t) * calc_event_size_words_(nAdcs) );
93 }
94 
96  return adcs_to_words_(nAdcs) + hdr_size_words();
97 }
98 
99 inline size_t dune::SSPFragmentWriter::adcs_to_words_(size_t nAdcs) {
100  auto mod(nAdcs % adcs_per_word_());
101  return (mod == 0) ?
102  nAdcs / adcs_per_word_() :
103  nAdcs / adcs_per_word_() + 1;
104 }
105 
106 #endif /* artdaq_dune_Overlays_SSPFragmentWriter_hh */
size_t calc_event_size_words_(size_t nAdcs)
artdaq::Fragment & artdaq_Fragment_
size_t total_adc_values() const
Definition: SSPFragment.hh:92
void set_hdr_run_number(Header::run_number_t run_number)
void resize(size_t nAdcs)
static constexpr size_t hdr_size_words()
Definition: SSPFragment.hh:89
SSPFragmentWriter(artdaq::Fragment &f)
unsigned int adc_t
Definition: SSPFragment.hh:28
static constexpr size_t adcs_per_word_()
Definition: SSPFragment.hh:113
static size_t adcs_to_words_(size_t nAdcs)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33