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