artdaq_core  v3_06_01
RawEvent.hh
1 #ifndef artdaq_core_Data_RawEvent_hh
2 #define artdaq_core_Data_RawEvent_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 #include "artdaq-core/Data/dictionarycontrol.hh"
6 
7 #include "cetlib_except/exception.h"
8 
9 #include <algorithm>
10 #include <iosfwd>
11 #include <memory>
12 
13 namespace artdaq {
17 namespace detail {
18 struct RawEventHeader;
19 }
20 
29 {
30  static constexpr uint8_t CURRENT_VERSION = 0;
31  typedef uint32_t run_id_t;
32  typedef uint32_t subrun_id_t;
33  typedef uint32_t event_id_t;
34  typedef uint64_t sequence_id_t;
35  typedef uint64_t timestamp_t;
36 
42  bool is_complete;
43  uint8_t version;
44 
49  : run_id(0), subrun_id(0), event_id(0), sequence_id(0), timestamp(0), is_complete(false), version(CURRENT_VERSION) {}
50 
60  subrun_id_t subrun,
61  event_id_t event,
62  sequence_id_t seq,
63  timestamp_t ts)
64  : run_id(run)
65  , subrun_id(subrun)
66  , event_id(event)
67  , sequence_id(seq)
68  , timestamp(ts)
69  , is_complete(false)
70  , version(CURRENT_VERSION)
71  {}
72 
73  void print(std::ostream& os) const;
74 };
75 
76 #if HIDE_FROM_ROOT
77 
83 inline std::ostream& operator<<(std::ostream& os, detail::RawEventHeader const& evh)
84 {
85  evh.print(os);
86  return os;
87 }
88 #endif
89 
97 class RawEvent
98 {
99 public:
105 
114  RawEvent(run_id_t run, subrun_id_t subrun, event_id_t event, sequence_id_t seq, timestamp_t ts);
115 
120  explicit RawEvent(detail::RawEventHeader hdr);
121 
122 #if HIDE_FROM_ROOT
123 
132  void insertFragment(FragmentPtr&& pfrag);
133 
137  void markComplete();
138 
143  size_t numFragments() const;
144 
149  size_t wordCount() const;
150 
155  run_id_t runID() const;
156 
161  subrun_id_t subrunID() const;
162 
167  event_id_t eventID() const;
168 
173  sequence_id_t sequenceID() const;
174 
179  timestamp_t timestamp() const;
180 
185  bool isComplete() const;
186 
191  void print(std::ostream& os) const;
192 
201  std::unique_ptr<Fragments> releaseProduct();
202 
207  void fragmentTypes(std::vector<Fragment::type_t>& type_list);
208 
221  std::unique_ptr<Fragments> releaseProduct(Fragment::type_t type);
222 
223 #endif
224 
225 private:
226  detail::RawEventHeader header_;
227  FragmentPtrs fragments_;
228 };
229 
230 typedef std::shared_ptr<RawEvent> RawEvent_ptr;
231 
233  : header_(run, subrun, event, seq, ts)
234  , fragments_() {}
235 
237  : header_(hdr), fragments_()
238 {}
239 
240 #if HIDE_FROM_ROOT
242 {
243  if (pfrag == nullptr)
244  {
245  throw cet::exception("LogicError") // NOLINT(cert-err60-cpp)
246  << "Attempt to insert a null FragmentPtr into a RawEvent detected.\n";
247  }
248  fragments_.emplace_back(std::move(pfrag));
249 }
250 
251 inline void RawEvent::markComplete() { header_.is_complete = true; }
252 
253 inline size_t RawEvent::numFragments() const
254 {
255  return fragments_.size();
256 }
257 
258 inline size_t RawEvent::wordCount() const
259 {
260  size_t sum = 0;
261  for (auto const& frag : fragments_) { sum += frag->size(); }
262  return sum;
263 }
264 
265 inline RawEvent::run_id_t RawEvent::runID() const { return header_.run_id; }
266 inline RawEvent::subrun_id_t RawEvent::subrunID() const { return header_.subrun_id; }
267 inline RawEvent::event_id_t RawEvent::eventID() const { return header_.event_id; }
268 inline RawEvent::sequence_id_t RawEvent::sequenceID() const { return header_.sequence_id; }
269 inline RawEvent::timestamp_t RawEvent::timestamp() const { return header_.timestamp; }
270 inline bool RawEvent::isComplete() const { return header_.is_complete; }
271 
272 inline std::unique_ptr<Fragments> RawEvent::releaseProduct()
273 {
274  std::unique_ptr<Fragments> result(new Fragments);
275  result->reserve(fragments_.size());
276  // 03/08/2016 ELF: Moving to range-for for STL compatibility
277  //for (size_t i = 0, sz = fragments_.size(); i < sz; ++i) {
278  for (auto& i : fragments_)
279  {
280  result->emplace_back(std::move(*i));
281  }
282  // It seems more hygenic to clear fragments_ rather than to leave
283  // it full of unique_ptrs to Fragments that have been plundered by
284  // the move.
285  fragments_.clear();
286  return result;
287 }
288 
289 inline void RawEvent::fragmentTypes(std::vector<Fragment::type_t>& type_list)
290 {
291  // 03/08/2016 ELF: Moving to range-for for STL compatibility
292  //for (size_t i = 0, sz = fragments_.size(); i < sz; ++i) {
293  for (auto& i : fragments_)
294  {
295  auto fragType = i->type(); // fragments_[i]->type();
296  if (std::find(type_list.begin(), type_list.end(), fragType) == type_list.end())
297  {
298  type_list.push_back(fragType);
299  }
300  }
301  //std::sort(type_list.begin(), type_list.end());
302  //std::unique(type_list.begin(), type_list.end());
303 }
304 
305 inline std::unique_ptr<Fragments>
307 {
308  std::unique_ptr<Fragments> result(new Fragments);
309  auto iter = fragments_.begin();
310  do
311  {
312  if ((*iter)->type() == fragment_type)
313  {
314  result->push_back(std::move(*(*iter)));
315  iter = fragments_.erase(iter);
316  }
317  else
318  {
319  ++iter;
320  }
321  } while (iter != fragments_.end());
322  return result;
323 }
324 
331 inline std::ostream& operator<<(std::ostream& os, RawEvent const& ev)
332 {
333  ev.print(os);
334  return os;
335 }
336 
337 #endif
338 } // namespace artdaq
339 
340 #endif /* artdaq_core_Data_RawEvent_hh */
std::unique_ptr< Fragment > FragmentPtr
A std::unique_ptr to a Fragment object.
Definition: Fragment.hh:54
std::unique_ptr< Fragments > releaseProduct()
Release all the Fragments from this RawEvent.
Definition: RawEvent.hh:272
detail::RawEventHeader::run_id_t run_id_t
Run numbers are 32 bits.
Definition: RawEvent.hh:100
event_id_t eventID() const
Retrieve the event number from the RawEventHeader.
Definition: RawEvent.hh:267
void fragmentTypes(std::vector< Fragment::type_t > &type_list)
Fills in a list of unique fragment types from this event.
Definition: RawEvent.hh:289
timestamp_t timestamp() const
Retrieve the timestamp from the RawEventHeader.
Definition: RawEvent.hh:269
std::vector< Fragment > Fragments
A std::vector of Fragment objects.
Definition: Fragment.hh:42
subrun_id_t subrunID() const
Retrieve the subrun number from the RawEventHeader.
Definition: RawEvent.hh:266
RawEventHeader(run_id_t run, subrun_id_t subrun, event_id_t event, sequence_id_t seq, timestamp_t ts)
Constructs the RawEventHeader struct with the given parameters.
Definition: RawEvent.hh:59
uint32_t event_id_t
Event numbers are 32 bits.
Definition: RawEvent.hh:33
detail::RawEventHeader::subrun_id_t subrun_id_t
Subrun numbers are 32 bits.
Definition: RawEvent.hh:101
sequence_id_t sequenceID() const
Retrieve the sequence id from the RawEventHeader.
Definition: RawEvent.hh:268
bool isComplete() const
Retrieve the value of the complete flag from the RawEventHeader.
Definition: RawEvent.hh:270
detail::RawEventHeader::sequence_id_t sequence_id_t
Field size should be the same as the Fragment::sequence_id field.
Definition: RawEvent.hh:103
detail::RawFragmentHeader::type_t type_t
typedef for type_t from RawFragmentHeader
Definition: Fragment.hh:137
RawEvent(run_id_t run, subrun_id_t subrun, event_id_t event, sequence_id_t seq, timestamp_t ts)
Constructs a RawEvent with the given parameters.
Definition: RawEvent.hh:232
size_t numFragments() const
Return the number of fragments this RawEvent contains.
Definition: RawEvent.hh:253
event_id_t event_id
Event number should be either sequence ID or Timestamp of component Fragments.
Definition: RawEvent.hh:39
detail::RawEventHeader::event_id_t event_id_t
Event numbers are 32 bits.
Definition: RawEvent.hh:102
uint32_t run_id_t
Run numbers are 32 bits.
Definition: RawEvent.hh:31
void print(std::ostream &os) const
Print summary information about this RawEvent to the given stream.
Definition: RawEvent.cc:18
subrun_id_t subrun_id
Fragments don&#39;t know about subruns.
Definition: RawEvent.hh:38
uint64_t timestamp_t
Field size should be the same as the Fragment::timestamp field.
Definition: RawEvent.hh:35
run_id_t runID() const
Retrieve the run number from the RawEventHeader.
Definition: RawEvent.hh:265
std::ostream & operator<<(std::ostream &os, Fragment const &f)
Prints the given Fragment to the stream.
Definition: Fragment.hh:1284
std::shared_ptr< RawEvent > RawEvent_ptr
A smart pointer to a RawEvent object.
Definition: GlobalQueue.hh:12
RawEventHeader()
Default constructor. Provided for ROOT compatibility.
Definition: RawEvent.hh:48
The header information used to identify key properties of the RawEvent object.
Definition: RawEvent.hh:28
uint64_t sequence_id_t
Field size should be the same as the Fragment::sequence_id field.
Definition: RawEvent.hh:34
detail::RawEventHeader::timestamp_t timestamp_t
Field size should be the same as the Fragment::timestamp field.
Definition: RawEvent.hh:104
std::list< FragmentPtr > FragmentPtrs
A std::list of FragmentPtrs.
Definition: Fragment.hh:59
void insertFragment(FragmentPtr &&pfrag)
Insert the given (pointer to a) Fragment into this RawEvent.
Definition: RawEvent.hh:241
run_id_t run_id
Fragments don&#39;t know about runs.
Definition: RawEvent.hh:37
size_t wordCount() const
Return the sum of the word counts of all fragments in this RawEvent.
Definition: RawEvent.hh:258
void markComplete()
Mark the event as complete.
Definition: RawEvent.hh:251
timestamp_t timestamp
The timestamp of the first Fragment received for this event.
Definition: RawEvent.hh:41
uint32_t subrun_id_t
Subrun numbers are 32 bits.
Definition: RawEvent.hh:32
uint8_t version
Version number of the RawFragmentHeader.
Definition: RawEvent.hh:43
bool is_complete
Does the event contain the expected number of Fragment objects?
Definition: RawEvent.hh:42
RawEvent is the artdaq view of a generic event, containing a header and zero or more Fragments...
Definition: RawEvent.hh:97
sequence_id_t sequence_id
RawEvent sequence_id should be the same as its component Fragment sequence_ids.
Definition: RawEvent.hh:40