artdaq  v3_12_02
RequestSender_module.cc
1 // Class: RequestSender
3 // Module Type: analyzer
4 // File: RequestSender_module.cc
5 // Description: Sends artdaq requests for events
7 
8 #include "TRACE/tracemf.h"
9 #define TRACE_NAME "RequestSenderModule"
10 
11 #include "art/Framework/Core/EDAnalyzer.h"
12 #include "art/Framework/Core/ModuleMacros.h"
13 #include "art/Framework/Principal/Event.h"
14 #include "art/Framework/Principal/Handle.h"
15 #include "art/Framework/Principal/Run.h"
16 
17 #include "artdaq-core/Data/ContainerFragment.hh"
18 #include "artdaq-core/Data/Fragment.hh"
19 #include "artdaq/DAQrate/detail/RequestSender.hh"
20 
21 #include <set>
22 
23 namespace artdaq {
24 class RequestSenderModule;
25 }
26 
30 class artdaq::RequestSenderModule : public art::EDAnalyzer
31 {
32 public:
41  explicit RequestSenderModule(fhicl::ParameterSet const& pset);
45  ~RequestSenderModule() override;
46 
51  void analyze(art::Event const& evt) override;
52 
57  void beginRun(art::Run const& run) override;
62  void endRun(art::Run const& run) override;
63 
64 private:
67  RequestSenderModule& operator=(RequestSenderModule const&) = delete;
68  RequestSenderModule& operator=(RequestSenderModule&&) = delete;
69 
70  RequestSender the_sender_;
71  std::set<artdaq::Fragment::sequence_id_t> active_requests_;
72  size_t max_active_requests_;
73 
74  void clear_active_requests();
75  void trim_active_requests();
76 };
77 
78 artdaq::RequestSenderModule::RequestSenderModule(fhicl::ParameterSet const& pset)
79  : EDAnalyzer(pset)
80  , the_sender_(pset)
81  , max_active_requests_(pset.get<size_t>("request_list_max_size", 1000))
82 {
83 }
84 
86 {
87 }
88 
89 void artdaq::RequestSenderModule::analyze(art::Event const& evt)
90 {
91  // get all the artdaq fragment collections in the event.
92  std::vector<art::Handle<std::vector<artdaq::Fragment>>> fragmentHandles;
93  fragmentHandles = evt.getMany<std::vector<artdaq::Fragment>>();
94 
95  artdaq::Fragment::sequence_id_t seq = 0;
96  artdaq::Fragment::timestamp_t timestamp = 0;
97 
98  for (auto& handle : fragmentHandles)
99  {
100  if (!handle.isValid()) continue;
101 
102  auto frag = handle->at(0);
103  seq = frag.sequenceID();
104  timestamp = frag.timestamp();
105 
106  if (seq != 0 && timestamp != 0) break;
107  }
108 
109  if (seq != 0 && timestamp != 0)
110  {
111  TLOG(TLVL_DEBUG + 35) << "Adding request for sequence ID " << seq << ", timestamp " << timestamp;
112  the_sender_.AddRequest(seq, timestamp);
113  active_requests_.insert(seq);
114  trim_active_requests();
115  }
116 }
117 
118 void artdaq::RequestSenderModule::beginRun(art::Run const& run)
119 {
120  the_sender_.SetRunNumber(run.run());
121  the_sender_.SetRequestMode(detail::RequestMessageMode::Normal);
122  clear_active_requests();
123 }
124 
126 {
127  the_sender_.SetRequestMode(detail::RequestMessageMode::EndOfRun);
128  the_sender_.SendRequest();
129  clear_active_requests();
130 }
131 
132 void artdaq::RequestSenderModule::clear_active_requests()
133 {
134  for (auto& req : active_requests_)
135  {
136  the_sender_.RemoveRequest(req);
137  }
138 }
139 
140 void artdaq::RequestSenderModule::trim_active_requests()
141 {
142  TLOG(TLVL_DEBUG + 33) << "Going to remove extra active requests";
143  while (active_requests_.size() > max_active_requests_)
144  {
145  TLOG(TLVL_DEBUG + 36) << "Removing request with sequence ID " << *active_requests_.begin();
146  the_sender_.RemoveRequest(*active_requests_.begin());
147  active_requests_.erase(active_requests_.begin());
148  }
149 }
150 
151 DEFINE_ART_MODULE(artdaq::RequestSenderModule) // NOLINT(performance-unnecessary-value-param)
void endRun(art::Run const &run) override
PErform end Run actions.
End of Run mode (Used to end request processing on receiver)
The RequestSender contains methods used to send data requests and Routing tokens. ...
RequestSenderModule(fhicl::ParameterSet const &pset)
RequestSender Constructor.
An art::EDAnalyzer module which sends requests for events (e.g. for the Mu2e CRV system) ...
void analyze(art::Event const &evt) override
Analyze each event, using the configured mode bitmask.
~RequestSenderModule() override
Virtual Destructor. Shuts down MetricManager if one is present.
void beginRun(art::Run const &run) override
Perform begin Run actions.