artdaq  v3_00_03
BinaryNetOutput_module.cc
1 #define TRACE_NAME "BinaryNetOutput"
2 
3 #include "art/Framework/Core/ModuleMacros.h"
4 #include "art/Framework/Core/OutputModule.h"
5 #include "art/Framework/Principal/EventPrincipal.h"
6 #include "art/Framework/Principal/RunPrincipal.h"
7 #include "art/Framework/Principal/Selector.h"
8 #include "art/Framework/Principal/SubRunPrincipal.h"
9 #include "art/Framework/Principal/Handle.h"
10 #include "art/Persistency/Common/GroupQueryResult.h"
11 #include "canvas/Utilities/DebugMacros.h"
12 #include "canvas/Utilities/Exception.h"
13 #include "fhiclcpp/ParameterSet.h"
14 
15 #include "artdaq/DAQrate/DataSenderManager.hh"
16 #include "artdaq/DAQdata/Globals.hh"
17 #include "artdaq-core/Data/Fragment.hh"
18 
19 #include <iomanip>
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24 #include <memory>
25 #include <unistd.h>
26 
27 namespace art
28 {
29  class BinaryNetOutput;
30 }
31 
33 using fhicl::ParameterSet;
34 
40 class art::BinaryNetOutput final: public OutputModule
41 {
42 public:
55  explicit BinaryNetOutput(ParameterSet const& ps);
56 
60  virtual ~BinaryNetOutput();
61 
62 private:
63  void beginJob() override;
64 
65  void endJob() override;
66 
67  void write(EventPrincipal&) override;
68 
69  void writeRun(RunPrincipal&) override {};
70  void writeSubRun(SubRunPrincipal&) override {};
71 
72  void initialize_MPI_();
73 
74  void deinitialize_MPI_();
75 
76  bool readParameterSet_(fhicl::ParameterSet const& pset);
77 
78 private:
79  ParameterSet data_pset_;
80  std::string name_ = "BinaryNetOutput";
81  int rt_priority_ = 0;
82  std::unique_ptr<artdaq::DataSenderManager> sender_ptr_ = {nullptr};
83 };
84 
86 BinaryNetOutput(ParameterSet const& ps)
87  : OutputModule(ps)
88 {
89  FDEBUG(1) << "Begin: BinaryNetOutput::BinaryNetOutput(ParameterSet const& ps)\n";
90  readParameterSet_(ps);
91  FDEBUG(1) << "End: BinaryNetOutput::BinaryNetOutput(ParameterSet const& ps)\n";
92 }
93 
96 {
97  FDEBUG(1) << "Begin/End: BinaryNetOutput::~BinaryNetOutput()\n";
98 }
99 
100 void
101 art::BinaryNetOutput::
102 beginJob()
103 {
104  FDEBUG(1) << "Begin: BinaryNetOutput::beginJob()\n";
105  initialize_MPI_();
106  FDEBUG(1) << "End: BinaryNetOutput::beginJob()\n";
107 }
108 
109 void
110 art::BinaryNetOutput::
111 endJob()
112 {
113  FDEBUG(1) << "Begin: BinaryNetOutput::endJob()\n";
114  deinitialize_MPI_();
115  FDEBUG(1) << "End: BinaryNetOutput::endJob()\n";
116 }
117 
118 
119 void
120 art::BinaryNetOutput::
121 initialize_MPI_()
122 {
123  if (rt_priority_ > 0)
124  {
125 #pragma GCC diagnostic push
126 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
127  sched_param s_param = {};
128  s_param.sched_priority = rt_priority_;
129  int status = pthread_setschedparam(pthread_self(), SCHED_RR, &s_param);
130  if (status != 0)
131  {
132  TLOG_ERROR(name_)
133  << "Failed to set realtime priority to " << rt_priority_
134  << ", return code = " << status << TLOG_ENDL;
135  }
136 #pragma GCC diagnostic pop
137  }
138 
139  sender_ptr_ = std::make_unique<artdaq::DataSenderManager>(data_pset_);
140  assert(sender_ptr_);
141 }
142 
143 void
144 art::BinaryNetOutput::
145 deinitialize_MPI_()
146 {
147  sender_ptr_.reset(nullptr);
148 }
149 
150 bool
151 art::BinaryNetOutput::
152 readParameterSet_(fhicl::ParameterSet const& pset)
153 {
154  TLOG_DEBUG(name_) << "BinaryNetOutput::readParameterSet_ method called with "
155  << "ParameterSet = \"" << pset.to_string()
156  << "\"." << TLOG_ENDL;
157 
158  // determine the data sending parameters
159  data_pset_ = pset;
160  name_ = pset.get<std::string>("module_name", "BinaryNetOutput");
161  rt_priority_ = pset.get<int>("rt_priority", 0);
162 
163  TRACE(4, "BinaryNetOutput::readParameterSet()");
164 
165  return true;
166 }
167 
168 void
169 art::BinaryNetOutput::
170 write(EventPrincipal& ep)
171 {
172  assert(sender_ptr_);
173 
174  using RawEvent = artdaq::Fragments;;
175  using RawEvents = std::vector<RawEvent>;
176  using RawEventHandle = art::Handle<RawEvent>;
177  using RawEventHandles = std::vector<RawEventHandle>;
178 
179  auto result_handles = std::vector<art::GroupQueryResult>();
180 
181 #if ART_HEX_VERSION < 0x20906
182  ep.getManyByType(art::TypeID(typeid(RawEvent)), result_handles);
183 #else
184  auto const& wrapped = art::WrappedTypeID::make<RawEvent>();
185  result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
186 #endif
187 
188  for (auto const& result_handle : result_handles)
189  {
190  auto const raw_event_handle = RawEventHandle(result_handle);
191 
192  if (!raw_event_handle.isValid())
193  continue;
194 
195  for (auto const& fragment : *raw_event_handle)
196  {
197  auto fragment_copy = fragment;
198  auto fragid_id = fragment_copy.fragmentID();
199  auto sequence_id = fragment_copy.sequenceID();
200  TRACE(1, "BinaryNetOutput::write seq=%lu frag=%i start", sequence_id, fragid_id);
201  sender_ptr_->sendFragment(std::move(fragment_copy));
202  TRACE(2, "BinaryNetOutput::write seq=%lu frag=%i done", sequence_id, fragid_id);
203  }
204  }
205 
206  return;
207 }
208 
209 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.