artdaq  v2_03_00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
EventStore.hh
1 #ifndef artdaq_DAQrate_EventStore_hh
2 #define artdaq_DAQrate_EventStore_hh
3 
4 #include "artdaq/DAQdata/Globals.hh" // Before trace.h gets included in ConcurrentQueue (from GlobalQueue)
5 #include "artdaq-core/Data/RawEvent.hh"
6 #include "artdaq-core/Core/GlobalQueue.hh"
7 #include "artdaq-utilities/Plugins/MetricManager.hh"
8 #include <fhiclcpp/ParameterSet.h>
9 
10 #include <map>
11 #include <memory>
12 #include <chrono>
13 //#include <thread>
14 #include <future>
15 #include <stdint.h>
16 #include <arpa/inet.h>
17 #include <netinet/in.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 
21 namespace artdaq
22 {
23 
48  class EventStore
49  {
50  public:
55  typedef int (ART_CMDLINE_FCN)(int, char**);
56 
61  typedef int (ART_CFGSTRING_FCN)(const std::string&);
62 
63  typedef RawEvent::run_id_t run_id_t;
64  typedef RawEvent::subrun_id_t subrun_id_t;
65  typedef Fragment::sequence_id_t sequence_id_t;
66  typedef std::map<sequence_id_t, RawEvent_ptr> EventMap;
67 
68  static const std::string EVENT_RATE_STAT_KEY;
69  static const std::string INCOMPLETE_EVENT_STAT_KEY;
70 
74  enum class EventStoreInsertResult : int
75  {
77  SUCCESS,
80  };
81 
85  EventStore() = delete;
86 
90  EventStore(EventStore const&) = delete;
91 
96  EventStore& operator=(EventStore const&) = delete;
97 
129  EventStore(const fhicl::ParameterSet& pset,
130  size_t num_fragments_per_event, run_id_t run,
131  size_t event_queue_depth, size_t max_incomplete_events);
132 
144  EventStore(const fhicl::ParameterSet& pset,
145  size_t num_fragments_per_event, run_id_t run,
146  int argc, char* argv[],
147  ART_CMDLINE_FCN* reader);
148 
159  EventStore(const fhicl::ParameterSet& pset,
160  size_t num_fragments_per_event, run_id_t run,
161  const std::string& configString,
162  ART_CFGSTRING_FCN* reader);
163 
167  virtual ~EventStore();
168 
183  void insert(FragmentPtr pfrag,
184  bool printWarningWhenFragmentIsDropped = true);
185 
201  EventStoreInsertResult insert(FragmentPtr pfrag, FragmentPtr& rejectedFragment);
202 
213  bool endOfData(int& readerReturnValue);
214 
226  void setSeqIDModulus(unsigned int seqIDModulus);
227 
233  bool flushData();
234 
239  void startRun(run_id_t runID);
240 
244  void startSubrun();
245 
250  subrun_id_t subrunID() const { return subrun_id_; }
251 
256  bool endRun();
257 
262  bool endSubrun();
263 
267  void sendMetrics();
268 
273  size_t incompleteEventCount() const { return events_.size(); }
274 
275  private:
276  // id_ is the unique identifier of this object; MPI programs will
277  // use the MPI rank to fill in this value.
278  size_t const num_fragments_per_event_;
279  size_t const max_queue_size_;
280  size_t const max_incomplete_count_;
281  run_id_t run_id_;
282  subrun_id_t subrun_id_;
283  EventMap events_;
284  RawEventQueue& queue_;
285  std::chrono::steady_clock::time_point reader_thread_launch_time_;
286  std::future<int> reader_thread_;
287 
288  bool send_requests_;
289  std::mutex request_mutex_;
290  std::map<Fragment::sequence_id_t, Fragment::timestamp_t> active_requests_;
291  int request_port_;
292  size_t request_delay_;
293  int request_socket_;
294  struct sockaddr_in request_addr_;
295  std::string multicast_out_addr_;
296 
297  unsigned int seqIDModulus_;
298  sequence_id_t lastFlushedSeqID_;
299  sequence_id_t highestSeqIDSeen_;
300 
301  artdaq::detail::seconds const enq_timeout_;
302  size_t enq_check_count_;
303  bool const printSummaryStats_;
304 
305  int incomplete_event_report_interval_ms_;
306  std::chrono::steady_clock::time_point last_incomplete_event_report_time_;
307 
308  bool send_routing_tokens_;
309  int token_port_;
310  int token_socket_;
311  struct sockaddr_in token_addr_;
312  std::string token_address_;
313 
314  int art_thread_wait_ms_;
315 
316  private:
317  void initStatistics_();
318 
319  void reportStatistics_();
320 
321  void setup_requests_(std::string trigger_addr);
322 
323  void send_request_();
324 
325  void do_send_request_();
326 
327  void setup_tokens_();
328 
329  void send_routing_token_(int nSlots);
330  };
331 }
332 #endif /* artdaq_DAQrate_EventStore_hh */
void insert(FragmentPtr pfrag, bool printWarningWhenFragmentIsDropped=true)
Give ownership of the Fragment to the EventStore.
Definition: EventStore.cc:94
static const std::string EVENT_RATE_STAT_KEY
Key for the Event Rate MonitoredQuantity.
Definition: EventStore.hh:68
bool endOfData(int &readerReturnValue)
Indicate that the end of input has been reached to the art thread.
Definition: EventStore.cc:247
EventStore & operator=(EventStore const &)=delete
Copy Assignment operator is deleted.
EventStoreInsertResult
This enumeration contains possible status codes of insertion attempts.
Definition: EventStore.hh:74
void startRun(run_id_t runID)
Start a Run.
Definition: EventStore.cc:305
Fragment::sequence_id_t sequence_id_t
Copy Fragment::sequence_id_t into local scope.
Definition: EventStore.hh:65
EventStore()=delete
Default Constructor is deleted.
virtual ~EventStore()
EventStore Destructor.
Definition: EventStore.cc:81
The Fragment was successfully inserted.
size_t incompleteEventCount() const
Get the number of events currently being built in the EventStore.
Definition: EventStore.hh:273
std::map< sequence_id_t, RawEvent_ptr > EventMap
An EventMap is a map of RawEvent_ptr objects, keyed by sequence ID.
Definition: EventStore.hh:66
void startSubrun()
Start a new Subrun, incrementing the subrun number.
Definition: EventStore.cc:344
RawEvent::subrun_id_t subrun_id_t
Copy RawEvent::subrun_id_t into local scope.
Definition: EventStore.hh:64
The EventStore class collects Fragment objects, until it receives a complete event, at which point the event is handed over to the art thread.
Definition: EventStore.hh:48
int( ART_CMDLINE_FCN)(int, char **)
An art function that accepts standard C main arguments.
Definition: EventStore.hh:55
static const std::string INCOMPLETE_EVENT_STAT_KEY
Key for the Incomplete Events MonitoredQuantity.
Definition: EventStore.hh:69
The EventStore is full, but the Fragment was accepted as it is for an already-open event...
subrun_id_t subrunID() const
Get the current subrun number.
Definition: EventStore.hh:250
void setSeqIDModulus(unsigned int seqIDModulus)
Set the parameter that will be used to determine which sequence IDs get grouped together into events...
Definition: EventStore.cc:262
void sendMetrics()
Send metrics to the MetricManager, if one has been instantiated in the application.
Definition: EventStore.cc:633
int( ART_CFGSTRING_FCN)(const std::string &)
An art function that accepts a fhicl::ParameterSet as a string.
Definition: EventStore.hh:61
bool flushData()
Push any incomplete events onto the queue.
Definition: EventStore.cc:267
RawEvent::run_id_t run_id_t
Copy RawEvent::run_id_t into local scope.
Definition: EventStore.hh:63
The EventStore is full, and the Fragment was rejected.
The Fragment was rejected, because the RawEventQueue is full.
bool endRun()
Send an EndOfRunFragment to the art thread.
Definition: EventStore.cc:354
bool endSubrun()
Send an EndOfSubRunFragment to the art thread.
Definition: EventStore.cc:370