artdaq_core  v3_06_10
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 <memory>
11 #include <ostream>
12 
13 namespace artdaq {
17 namespace detail {
18 
27 {
28  static constexpr uint8_t CURRENT_VERSION = 0;
29  typedef uint32_t run_id_t;
30  typedef uint32_t subrun_id_t;
31  typedef uint32_t event_id_t;
32  typedef uint64_t sequence_id_t;
33  typedef uint64_t timestamp_t;
34 
40  bool is_complete;
41  uint8_t version;
42 
48 
58  subrun_id_t subrun,
59  event_id_t event,
60  sequence_id_t seq,
61  timestamp_t ts)
62  : run_id(run)
63  , subrun_id(subrun)
64  , event_id(event)
65  , sequence_id(seq)
66  , timestamp(ts)
67  , is_complete(false)
69  {}
70 
75  void print(std::ostream& os) const;
76 };
77 
78 #if HIDE_FROM_ROOT
79 
85 inline std::ostream& operator<<(std::ostream& os, RawEventHeader const& evh)
86 {
87  evh.print(os);
88  return os;
89 }
90 
91 #endif
92 } // namespace detail
100 class RawEvent
101 {
102 public:
108 
117  RawEvent(run_id_t run, subrun_id_t subrun, event_id_t event, sequence_id_t seq, timestamp_t ts);
118 
123  explicit RawEvent(detail::RawEventHeader hdr);
124 
125 #if HIDE_FROM_ROOT
126 
135  void insertFragment(FragmentPtr&& pfrag);
136 
140  void markComplete();
141 
146  size_t numFragments() const;
147 
152  size_t wordCount() const;
153 
158  run_id_t runID() const;
159 
164  subrun_id_t subrunID() const;
165 
170  event_id_t eventID() const;
171 
176  sequence_id_t sequenceID() const;
177 
182  timestamp_t timestamp() const;
183 
188  bool isComplete() const;
189 
194  void print(std::ostream& os) const;
195 
204  std::unique_ptr<Fragments> releaseProduct();
205 
210  void fragmentTypes(std::vector<Fragment::type_t>& type_list);
211 
224  std::unique_ptr<Fragments> releaseProduct(Fragment::type_t type);
225 
226 #endif
227 
228 private:
229  detail::RawEventHeader header_;
230  FragmentPtrs fragments_;
231 };
232 
233 typedef std::shared_ptr<RawEvent> RawEvent_ptr;
234 
236  : header_(run, subrun, event, seq, ts)
237  , fragments_() {}
238 
240  : header_(hdr), fragments_()
241 {}
242 
243 #if HIDE_FROM_ROOT
245 {
246  if (pfrag == nullptr)
247  {
248  throw cet::exception("LogicError") // NOLINT(cert-err60-cpp)
249  << "Attempt to insert a null FragmentPtr into a RawEvent detected.\n";
250  }
251  fragments_.emplace_back(std::move(pfrag));
252 }
253 
254 inline void RawEvent::markComplete() { header_.is_complete = true; }
255 
256 inline size_t RawEvent::numFragments() const
257 {
258  return fragments_.size();
259 }
260 
261 inline size_t RawEvent::wordCount() const
262 {
263  size_t sum = 0;
264  for (auto const& frag : fragments_) { sum += frag->size(); }
265  return sum;
266 }
267 
268 inline RawEvent::run_id_t RawEvent::runID() const { return header_.run_id; }
269 inline RawEvent::subrun_id_t RawEvent::subrunID() const { return header_.subrun_id; }
270 inline RawEvent::event_id_t RawEvent::eventID() const { return header_.event_id; }
271 inline RawEvent::sequence_id_t RawEvent::sequenceID() const { return header_.sequence_id; }
272 inline RawEvent::timestamp_t RawEvent::timestamp() const { return header_.timestamp; }
273 inline bool RawEvent::isComplete() const { return header_.is_complete; }
274 
275 inline std::unique_ptr<Fragments> RawEvent::releaseProduct()
276 {
277  std::unique_ptr<Fragments> result(new Fragments);
278  result->reserve(fragments_.size());
279  // 03/08/2016 ELF: Moving to range-for for STL compatibility
280  //for (size_t i = 0, sz = fragments_.size(); i < sz; ++i) {
281  for (auto& i : fragments_)
282  {
283  result->emplace_back(std::move(*i));
284  }
285  // It seems more hygenic to clear fragments_ rather than to leave
286  // it full of unique_ptrs to Fragments that have been plundered by
287  // the move.
288  fragments_.clear();
289  return result;
290 }
291 
292 inline void RawEvent::fragmentTypes(std::vector<Fragment::type_t>& type_list)
293 {
294  // 03/08/2016 ELF: Moving to range-for for STL compatibility
295  //for (size_t i = 0, sz = fragments_.size(); i < sz; ++i) {
296  for (auto& i : fragments_)
297  {
298  auto fragType = i->type(); // fragments_[i]->type();
299  if (std::find(type_list.begin(), type_list.end(), fragType) == type_list.end())
300  {
301  type_list.push_back(fragType);
302  }
303  }
304  //std::sort(type_list.begin(), type_list.end());
305  //std::unique(type_list.begin(), type_list.end());
306 }
307 
308 inline std::unique_ptr<Fragments>
310 {
311  std::unique_ptr<Fragments> result(new Fragments);
312  auto iter = fragments_.begin();
313  do
314  {
315  if ((*iter)->type() == fragment_type)
316  {
317  result->push_back(std::move(*(*iter)));
318  iter = fragments_.erase(iter);
319  }
320  else
321  {
322  ++iter;
323  }
324  } while (iter != fragments_.end());
325  return result;
326 }
327 
334 inline std::ostream& operator<<(std::ostream& os, RawEvent const& ev)
335 {
336  ev.print(os);
337  return os;
338 }
339 #endif
340 } // namespace artdaq
341 
342 #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:275
detail::RawEventHeader::run_id_t run_id_t
Run numbers are 32 bits.
Definition: RawEvent.hh:103
event_id_t eventID() const
Retrieve the event number from the RawEventHeader.
Definition: RawEvent.hh:270
void fragmentTypes(std::vector< Fragment::type_t > &type_list)
Fills in a list of unique fragment types from this event.
Definition: RawEvent.hh:292
timestamp_t timestamp() const
Retrieve the timestamp from the RawEventHeader.
Definition: RawEvent.hh:272
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:269
static constexpr uint8_t CURRENT_VERSION
Current version of the RawEventHeader.
Definition: RawEvent.hh:28
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:57
uint32_t event_id_t
Event numbers are 32 bits.
Definition: RawEvent.hh:31
detail::RawEventHeader::subrun_id_t subrun_id_t
Subrun numbers are 32 bits.
Definition: RawEvent.hh:104
sequence_id_t sequenceID() const
Retrieve the sequence id from the RawEventHeader.
Definition: RawEvent.hh:271
bool isComplete() const
Retrieve the value of the complete flag from the RawEventHeader.
Definition: RawEvent.hh:273
detail::RawEventHeader::sequence_id_t sequence_id_t
Field size should be the same as the Fragment::sequence_id field.
Definition: RawEvent.hh:106
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:235
size_t numFragments() const
Return the number of fragments this RawEvent contains.
Definition: RawEvent.hh:256
event_id_t event_id
Event number should be either sequence ID or Timestamp of component Fragments.
Definition: RawEvent.hh:37
std::ostream & operator<<(std::ostream &os, RawEventHeader const &evh)
Prints the RawEventHeader to the given stream.
Definition: RawEvent.hh:85
detail::RawEventHeader::event_id_t event_id_t
Event numbers are 32 bits.
Definition: RawEvent.hh:105
uint32_t run_id_t
Run numbers are 32 bits.
Definition: RawEvent.hh:29
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:36
uint64_t timestamp_t
Field size should be the same as the Fragment::timestamp field.
Definition: RawEvent.hh:33
run_id_t runID() const
Retrieve the run number from the RawEventHeader.
Definition: RawEvent.hh:268
std::ostream & operator<<(std::ostream &os, Fragment const &f)
Prints the given Fragment to the stream.
Definition: Fragment.hh:1284
RawEventHeader()
Default constructor. Provided for ROOT compatibility.
Definition: RawEvent.hh:46
The header information used to identify key properties of the RawEvent object.
Definition: RawEvent.hh:26
std::shared_ptr< RawEvent > RawEvent_ptr
A shared_ptr to a RawEvent.
Definition: RawEvent.hh:233
uint64_t sequence_id_t
Field size should be the same as the Fragment::sequence_id field.
Definition: RawEvent.hh:32
detail::RawEventHeader::timestamp_t timestamp_t
Field size should be the same as the Fragment::timestamp field.
Definition: RawEvent.hh:107
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:244
run_id_t run_id
Fragments don&#39;t know about runs.
Definition: RawEvent.hh:35
size_t wordCount() const
Return the sum of the word counts of all fragments in this RawEvent.
Definition: RawEvent.hh:261
void markComplete()
Mark the event as complete.
Definition: RawEvent.hh:254
timestamp_t timestamp
The timestamp of the first Fragment received for this event.
Definition: RawEvent.hh:39
void print(std::ostream &os) const
Print a RawEventHeader to the given stream.
Definition: RawEvent.cc:5
uint32_t subrun_id_t
Subrun numbers are 32 bits.
Definition: RawEvent.hh:30
uint8_t version
Version number of the RawEventHeader.
Definition: RawEvent.hh:41
bool is_complete
Does the event contain the expected number of Fragment objects?
Definition: RawEvent.hh:40
RawEvent is the artdaq view of a generic event, containing a header and zero or more Fragments...
Definition: RawEvent.hh:100
sequence_id_t sequence_id
RawEvent sequence_id should be the same as its component Fragment sequence_ids.
Definition: RawEvent.hh:38