artdaq  v3_07_02
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  virtual ~BinaryNetOutput();
60 
61 private:
62  void beginJob() override;
63 
64  void endJob() override;
65 
66  void write(EventPrincipal&) override;
67 
68  void writeRun(RunPrincipal&) override{};
69  void writeSubRun(SubRunPrincipal&) override{};
70 
71  void initialize_MPI_();
72 
73  void deinitialize_MPI_();
74 
75  bool readParameterSet_(fhicl::ParameterSet const& pset);
76 
77 private:
78  ParameterSet data_pset_;
79  std::string name_ = "BinaryNetOutput";
80  int rt_priority_ = 0;
81  std::unique_ptr<artdaq::DataSenderManager> sender_ptr_ = {nullptr};
82 };
83 
85  : OutputModule(ps)
86 {
87  TLOG(TLVL_DEBUG) << "Begin: BinaryNetOutput::BinaryNetOutput(ParameterSet const& ps)\n";
88  readParameterSet_(ps);
89  TLOG(TLVL_DEBUG) << "End: BinaryNetOutput::BinaryNetOutput(ParameterSet const& ps)\n";
90 }
91 
92 art::BinaryNetOutput::~BinaryNetOutput() { TLOG(TLVL_DEBUG) << "Begin/End: BinaryNetOutput::~BinaryNetOutput()\n"; }
93 
94 void art::BinaryNetOutput::beginJob()
95 {
96  TLOG(TLVL_DEBUG) << "Begin: BinaryNetOutput::beginJob()\n";
97  initialize_MPI_();
98  TLOG(TLVL_DEBUG) << "End: BinaryNetOutput::beginJob()\n";
99 }
100 
101 void art::BinaryNetOutput::endJob()
102 {
103  TLOG(TLVL_DEBUG) << "Begin: BinaryNetOutput::endJob()\n";
104  deinitialize_MPI_();
105  TLOG(TLVL_DEBUG) << "End: BinaryNetOutput::endJob()\n";
106 }
107 
108 void art::BinaryNetOutput::initialize_MPI_()
109 {
110  if (rt_priority_ > 0)
111  {
112 #pragma GCC diagnostic push
113 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
114  sched_param s_param = {};
115  s_param.sched_priority = rt_priority_;
116  int status = pthread_setschedparam(pthread_self(), SCHED_RR, &s_param);
117  if (status != 0)
118  {
119  TLOG(TLVL_ERROR) << name_ << "Failed to set realtime priority to " << rt_priority_
120  << ", return code = " << status;
121  }
122 #pragma GCC diagnostic pop
123  }
124 
125  sender_ptr_ = std::make_unique<artdaq::DataSenderManager>(data_pset_);
126  assert(sender_ptr_);
127 }
128 
129 void art::BinaryNetOutput::deinitialize_MPI_() { sender_ptr_.reset(nullptr); }
130 
131 bool art::BinaryNetOutput::readParameterSet_(fhicl::ParameterSet const& pset)
132 {
133  TLOG(TLVL_DEBUG) << name_ << "BinaryNetOutput::readParameterSet_ method called with "
134  << "ParameterSet = \"" << pset.to_string() << "\".";
135 
136  // determine the data sending parameters
137  data_pset_ = pset;
138  name_ = pset.get<std::string>("module_name", "BinaryNetOutput");
139  rt_priority_ = pset.get<int>("rt_priority", 0);
140 
141  TLOG(TLVL_TRACE) << "BinaryNetOutput::readParameterSet()";
142 
143  return true;
144 }
145 
146 void art::BinaryNetOutput::write(EventPrincipal& ep)
147 {
148  assert(sender_ptr_);
149 
150  using RawEvent = artdaq::Fragments;
151  ;
152  using RawEvents = std::vector<RawEvent>;
153  using RawEventHandle = art::Handle<RawEvent>;
154  using RawEventHandles = std::vector<RawEventHandle>;
155 
156  auto result_handles = std::vector<art::GroupQueryResult>();
157 
158  auto const& wrapped = art::WrappedTypeID::make<RawEvent>();
159 #if ART_HEX_VERSION >= 0x30000
160  ModuleContext const mc{moduleDescription()};
161  ProcessTag const processTag{"", mc.moduleDescription().processName()};
162 
163  result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
164 #else
165  result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
166 #endif
167 
168  artdaq::Fragment::sequence_id_t sequence_id = 0;
169 
170  for (auto const& result_handle : result_handles)
171  {
172  auto const raw_event_handle = RawEventHandle(result_handle);
173 
174  if (!raw_event_handle.isValid()) continue;
175 
176  for (auto const& fragment : *raw_event_handle)
177  {
178  auto fragment_copy = fragment;
179  auto fragid_id = fragment_copy.fragmentID();
180  sequence_id = fragment_copy.sequenceID();
181  TLOG(TLVL_DEBUG) << "BinaryNetOutput::write seq=" << sequence_id << " frag=" << fragid_id << " start";
182  sender_ptr_->sendFragment(std::move(fragment_copy));
183  TLOG(TLVL_DEBUG) << "BinaryNetOutput::write seq=" << sequence_id << " frag=" << fragid_id << " done";
184  }
185  }
186 
187  // Events are unique in art, so this will be the only send with this sequence ID!
188  // ELF 1/23/2020: Only remove routing entry AFTER all Fragments have been sent!
189  sender_ptr_->RemoveRoutingTableEntry(sequence_id);
190 
191  return;
192 }
193 
194 DEFINE_ART_MODULE(art::BinaryNetOutput)
BinaryNetOutput(ParameterSet const &ps)
BinaryNetOutput Constructor.
An art::OutputModule which sends Fragments using DataSenderManager. This module produces output ident...
virtual ~BinaryNetOutput()
BinaryNetOutput Destructor.