artdaq  v3_09_01
BinaryNetOutput_module.cc
1 #define TRACE_NAME "BinaryNetOutput"
2 #include "artdaq/DAQdata/Globals.hh"
3 
4 #include "art/Framework/Core/ModuleMacros.h"
5 #include "art/Framework/Core/OutputModule.h"
6 #include "art/Framework/Principal/EventPrincipal.h"
7 #include "art/Framework/Principal/Handle.h"
8 #include "art/Framework/Principal/RunPrincipal.h"
9 #include "art/Framework/Principal/Selector.h"
10 #include "art/Framework/Principal/SubRunPrincipal.h"
11 #include "art/Persistency/Common/GroupQueryResult.h"
12 #include "canvas/Utilities/DebugMacros.h"
13 #include "canvas/Utilities/Exception.h"
14 #include "fhiclcpp/ParameterSet.h"
15 
16 #include "artdaq-core/Data/Fragment.hh"
17 #include "artdaq/DAQrate/DataSenderManager.hh"
18 
19 #include <unistd.h>
20 #include <iomanip>
21 #include <iostream>
22 #include <memory>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 
27 namespace art {
28 class BinaryNetOutput;
29 }
30 
32 using fhicl::ParameterSet;
33 
39 class art::BinaryNetOutput final : public OutputModule
40 {
41 public:
54  explicit BinaryNetOutput(ParameterSet const& ps);
55 
59  ~BinaryNetOutput() override;
60 
61 private:
62  BinaryNetOutput(BinaryNetOutput const&) = delete;
63  BinaryNetOutput(BinaryNetOutput&&) = delete;
64  BinaryNetOutput& operator=(BinaryNetOutput const&) = delete;
65  BinaryNetOutput& operator=(BinaryNetOutput&&) = delete;
66 
67  void beginJob() override;
68 
69  void endJob() override;
70 
71  void write(EventPrincipal& /*ep*/) override;
72 
73  void writeRun(RunPrincipal& /*r*/) override{};
74  void writeSubRun(SubRunPrincipal& /*sr*/) override{};
75 
76  void initialize_MPI_();
77 
78  void deinitialize_MPI_();
79 
80  bool readParameterSet_(fhicl::ParameterSet const& pset);
81 
82 private:
83  ParameterSet data_pset_;
84  std::string name_ = "BinaryNetOutput";
85  int rt_priority_ = 0;
86  std::unique_ptr<artdaq::DataSenderManager> sender_ptr_ = {nullptr};
87 };
88 
90  : OutputModule(ps)
91 {
92  TLOG(TLVL_DEBUG) << "Begin: BinaryNetOutput::BinaryNetOutput(ParameterSet const& ps)\n";
93  readParameterSet_(ps);
94  TLOG(TLVL_DEBUG) << "End: BinaryNetOutput::BinaryNetOutput(ParameterSet const& ps)\n";
95 }
96 
97 art::BinaryNetOutput::~BinaryNetOutput() { TLOG(TLVL_DEBUG) << "Begin/End: BinaryNetOutput::~BinaryNetOutput()\n"; }
98 
99 void art::BinaryNetOutput::beginJob()
100 {
101  TLOG(TLVL_DEBUG) << "Begin: BinaryNetOutput::beginJob()\n";
102  initialize_MPI_();
103  TLOG(TLVL_DEBUG) << "End: BinaryNetOutput::beginJob()\n";
104 }
105 
106 void art::BinaryNetOutput::endJob()
107 {
108  TLOG(TLVL_DEBUG) << "Begin: BinaryNetOutput::endJob()\n";
109  deinitialize_MPI_();
110  TLOG(TLVL_DEBUG) << "End: BinaryNetOutput::endJob()\n";
111 }
112 
113 void art::BinaryNetOutput::initialize_MPI_()
114 {
115  if (rt_priority_ > 0)
116  {
117 #pragma GCC diagnostic push
118 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
119  sched_param s_param = {};
120  s_param.sched_priority = rt_priority_;
121  int status = pthread_setschedparam(pthread_self(), SCHED_RR, &s_param);
122  if (status != 0)
123  {
124  TLOG(TLVL_ERROR) << name_ << "Failed to set realtime priority to " << rt_priority_
125  << ", return code = " << status;
126  }
127 #pragma GCC diagnostic pop
128  }
129 
130  sender_ptr_ = std::make_unique<artdaq::DataSenderManager>(data_pset_);
131  assert(sender_ptr_);
132 }
133 
134 void art::BinaryNetOutput::deinitialize_MPI_() { sender_ptr_.reset(nullptr); }
135 
136 bool art::BinaryNetOutput::readParameterSet_(fhicl::ParameterSet const& pset)
137 {
138  TLOG(TLVL_DEBUG) << name_ << "BinaryNetOutput::readParameterSet_ method called with "
139  << "ParameterSet = \"" << pset.to_string() << "\".";
140 
141  // determine the data sending parameters
142  data_pset_ = pset;
143  name_ = pset.get<std::string>("module_name", "BinaryNetOutput");
144  rt_priority_ = pset.get<int>("rt_priority", 0);
145 
146  TLOG(TLVL_TRACE) << "BinaryNetOutput::readParameterSet()";
147 
148  return true;
149 }
150 
151 void art::BinaryNetOutput::write(EventPrincipal& ep)
152 {
153  assert(sender_ptr_);
154 
155  using RawEvent = artdaq::Fragments;
156  ;
157  using RawEvents = std::vector<RawEvent>;
158  using RawEventHandle = art::Handle<RawEvent>;
159  using RawEventHandles = std::vector<RawEventHandle>;
160 
161  auto result_handles = std::vector<art::GroupQueryResult>();
162 
163  auto const& wrapped = art::WrappedTypeID::make<RawEvent>();
164 #if ART_HEX_VERSION >= 0x30000
165  ModuleContext const mc{moduleDescription()};
166  ProcessTag const processTag{"", mc.moduleDescription().processName()};
167 
168  result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
169 #else
170  result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
171 #endif
172 
173  artdaq::Fragment::sequence_id_t sequence_id = 0;
174 
175  for (auto const& result_handle : result_handles)
176  {
177  auto const raw_event_handle = RawEventHandle(result_handle);
178 
179  if (!raw_event_handle.isValid())
180  {
181  continue;
182  }
183 
184  for (auto const& fragment : *raw_event_handle)
185  {
186  auto fragment_copy = fragment;
187  auto fragid_id = fragment_copy.fragmentID();
188  sequence_id = fragment_copy.sequenceID();
189  TLOG(TLVL_DEBUG) << "BinaryNetOutput::write seq=" << sequence_id << " frag=" << fragid_id << " start";
190  sender_ptr_->sendFragment(std::move(fragment_copy));
191  TLOG(TLVL_DEBUG) << "BinaryNetOutput::write seq=" << sequence_id << " frag=" << fragid_id << " done";
192  }
193  }
194 
195  // Events are unique in art, so this will be the only send with this sequence ID!
196  // ELF 1/23/2020: Only remove routing entry AFTER all Fragments have been sent!
197  sender_ptr_->RemoveRoutingTableEntry(sequence_id);
198 }
199 
200 DEFINE_ART_MODULE(art::BinaryNetOutput)// NOLINT(performance-unnecessary-value-param)
BinaryNetOutput(ParameterSet const &ps)
BinaryNetOutput Constructor.
An art::OutputModule which sends Fragments using DataSenderManager. This module produces output ident...
~BinaryNetOutput() override
BinaryNetOutput Destructor.