artdaq  v3_11_01
RequestSender_module.cc
1 // Class: RequestSender
3 // Module Type: analyzer
4 // File: RequestSender_module.cc
5 // Description: Sends artdaq requests for events
7 
8 #define TRACE_NAME "RequestSenderModule"
9 #include "artdaq/DAQdata/Globals.hh"
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 #if ART_HEX_VERSION < 0x30900
94  evt.getManyByType(fragmentHandles);
95 #else
96  fragmentHandles = evt.getMany<std::vector<artdaq::Fragment>>();
97 #endif
98 
99  artdaq::Fragment::sequence_id_t seq = 0;
100  artdaq::Fragment::timestamp_t timestamp = 0;
101 
102  for (auto& handle : fragmentHandles)
103  {
104  if (!handle.isValid()) continue;
105 
106  auto frag = handle->at(0);
107  seq = frag.sequenceID();
108  timestamp = frag.timestamp();
109 
110  if (seq != 0 && timestamp != 0) break;
111  }
112 
113  if (seq != 0 && timestamp != 0)
114  {
115  TLOG(TLVL_DEBUG + 5) << "Adding request for sequence ID " << seq << ", timestamp " << timestamp;
116  the_sender_.AddRequest(seq, timestamp);
117  active_requests_.insert(seq);
118  trim_active_requests();
119  }
120 }
121 
122 void artdaq::RequestSenderModule::beginRun(art::Run const& run)
123 {
124  the_sender_.SetRunNumber(run.run());
125  the_sender_.SetRequestMode(detail::RequestMessageMode::Normal);
126  clear_active_requests();
127 }
128 
130 {
131  the_sender_.SetRequestMode(detail::RequestMessageMode::EndOfRun);
132  the_sender_.SendRequest();
133  clear_active_requests();
134 }
135 
136 void artdaq::RequestSenderModule::clear_active_requests()
137 {
138  for (auto& req : active_requests_)
139  {
140  the_sender_.RemoveRequest(req);
141  }
142 }
143 
144 void artdaq::RequestSenderModule::trim_active_requests()
145 {
146  TLOG(TLVL_TRACE) << "Going to remove extra active requests";
147  while (active_requests_.size() > max_active_requests_)
148  {
149  TLOG(TLVL_DEBUG + 6) << "Removing request with sequence ID " << *active_requests_.begin();
150  the_sender_.RemoveRequest(*active_requests_.begin());
151  active_requests_.erase(active_requests_.begin());
152  }
153 }
154 
155 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.