artdaq_core  v3_06_07
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 
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)
71  {}
72 
77  void print(std::ostream& os) const;
78 };
79 
80 #if HIDE_FROM_ROOT
81 
87 inline std::ostream& operator<<(std::ostream& os, detail::RawEventHeader const& evh)
88 {
89  evh.print(os);
90  return os;
91 }
92 #endif
93 
101 class RawEvent
102 {
103 public:
109 
118  RawEvent(run_id_t run, subrun_id_t subrun, event_id_t event, sequence_id_t seq, timestamp_t ts);
119 
124  explicit RawEvent(detail::RawEventHeader hdr);
125 
126 #if HIDE_FROM_ROOT
127 
136  void insertFragment(FragmentPtr&& pfrag);
137 
141  void markComplete();
142 
147  size_t numFragments() const;
148 
153  size_t wordCount() const;
154 
159  run_id_t runID() const;
160 
165  subrun_id_t subrunID() const;
166 
171  event_id_t eventID() const;
172 
177  sequence_id_t sequenceID() const;
178 
183  timestamp_t timestamp() const;
184 
189  bool isComplete() const;
190 
195  void print(std::ostream& os) const;
196 
205  std::unique_ptr<Fragments> releaseProduct();
206 
211  void fragmentTypes(std::vector<Fragment::type_t>& type_list);
212 
225  std::unique_ptr<Fragments> releaseProduct(Fragment::type_t type);
226 
227 #endif
228 
229 private:
230  detail::RawEventHeader header_;
231  FragmentPtrs fragments_;
232 };
233 
234 typedef std::shared_ptr<RawEvent> RawEvent_ptr;
235 
237  : header_(run, subrun, event, seq, ts)
238  , fragments_() {}
239 
241  : header_(hdr), fragments_()
242 {}
243 
244 #if HIDE_FROM_ROOT
246 {
247  if (pfrag == nullptr)
248  {
249  throw cet::exception("LogicError") // NOLINT(cert-err60-cpp)
250  << "Attempt to insert a null FragmentPtr into a RawEvent detected.\n";
251  }
252  fragments_.emplace_back(std::move(pfrag));
253 }
254 
255 inline void RawEvent::markComplete() { header_.is_complete = true; }
256 
257 inline size_t RawEvent::numFragments() const
258 {
259  return fragments_.size();
260 }
261 
262 inline size_t RawEvent::wordCount() const
263 {
264  size_t sum = 0;
265  for (auto const& frag : fragments_) { sum += frag->size(); }
266  return sum;
267 }
268 
269 inline RawEvent::run_id_t RawEvent::runID() const { return header_.run_id; }
270 inline RawEvent::subrun_id_t RawEvent::subrunID() const { return header_.subrun_id; }
271 inline RawEvent::event_id_t RawEvent::eventID() const { return header_.event_id; }
272 inline RawEvent::sequence_id_t RawEvent::sequenceID() const { return header_.sequence_id; }
273 inline RawEvent::timestamp_t RawEvent::timestamp() const { return header_.timestamp; }
274 inline bool RawEvent::isComplete() const { return header_.is_complete; }
275 
276 inline std::unique_ptr<Fragments> RawEvent::releaseProduct()
277 {
278  std::unique_ptr<Fragments> result(new Fragments);
279  result->reserve(fragments_.size());
280  // 03/08/2016 ELF: Moving to range-for for STL compatibility
281  //for (size_t i = 0, sz = fragments_.size(); i < sz; ++i) {
282  for (auto& i : fragments_)
283  {
284  result->emplace_back(std::move(*i));
285  }
286  // It seems more hygenic to clear fragments_ rather than to leave
287  // it full of unique_ptrs to Fragments that have been plundered by
288  // the move.
289  fragments_.clear();
290  return result;
291 }
292 
293 inline void RawEvent::fragmentTypes(std::vector<Fragment::type_t>& type_list)
294 {
295  // 03/08/2016 ELF: Moving to range-for for STL compatibility
296  //for (size_t i = 0, sz = fragments_.size(); i < sz; ++i) {
297  for (auto& i : fragments_)
298  {
299  auto fragType = i->type(); // fragments_[i]->type();
300  if (std::find(type_list.begin(), type_list.end(), fragType) == type_list.end())
301  {
302  type_list.push_back(fragType);
303  }
304  }
305  //std::sort(type_list.begin(), type_list.end());
306  //std::unique(type_list.begin(), type_list.end());
307 }
308 
309 inline std::unique_ptr<Fragments>
311 {
312  std::unique_ptr<Fragments> result(new Fragments);
313  auto iter = fragments_.begin();
314  do
315  {
316  if ((*iter)->type() == fragment_type)
317  {
318  result->push_back(std::move(*(*iter)));
319  iter = fragments_.erase(iter);
320  }
321  else
322  {
323  ++iter;
324  }
325  } while (iter != fragments_.end());
326  return result;
327 }
328 
335 inline std::ostream& operator<<(std::ostream& os, RawEvent const& ev)
336 {
337  ev.print(os);
338  return os;
339 }
340 
341 #endif
342 } // namespace artdaq
343 
344 #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:276
detail::RawEventHeader::run_id_t run_id_t
Run numbers are 32 bits.
Definition: RawEvent.hh:104
event_id_t eventID() const
Retrieve the event number from the RawEventHeader.
Definition: RawEvent.hh:271
void fragmentTypes(std::vector< Fragment::type_t > &type_list)
Fills in a list of unique fragment types from this event.
Definition: RawEvent.hh:293
timestamp_t timestamp() const
Retrieve the timestamp from the RawEventHeader.
Definition: RawEvent.hh:273
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:270
static constexpr uint8_t CURRENT_VERSION
Current version of the RawEventHeader.
Definition: RawEvent.hh:30
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:105
sequence_id_t sequenceID() const
Retrieve the sequence id from the RawEventHeader.
Definition: RawEvent.hh:272
bool isComplete() const
Retrieve the value of the complete flag from the RawEventHeader.
Definition: RawEvent.hh:274
detail::RawEventHeader::sequence_id_t sequence_id_t
Field size should be the same as the Fragment::sequence_id field.
Definition: RawEvent.hh:107
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:236
size_t numFragments() const
Return the number of fragments this RawEvent contains.
Definition: RawEvent.hh:257
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:106
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:269
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:108
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:245
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:262
void markComplete()
Mark the event as complete.
Definition: RawEvent.hh:255
timestamp_t timestamp
The timestamp of the first Fragment received for this event.
Definition: RawEvent.hh:41
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:32
uint8_t version
Version number of the RawEventHeader.
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:101
sequence_id_t sequence_id
RawEvent sequence_id should be the same as its component Fragment sequence_ids.
Definition: RawEvent.hh:40