00001 #define TRACE_NAME "BinaryNetOutput"
00002
00003 #include "art/Framework/Core/ModuleMacros.h"
00004 #include "art/Framework/Core/OutputModule.h"
00005 #include "art/Framework/Principal/EventPrincipal.h"
00006 #include "art/Framework/Principal/RunPrincipal.h"
00007 #include "art/Framework/Principal/Selector.h"
00008 #include "art/Framework/Principal/SubRunPrincipal.h"
00009 #include "art/Framework/Principal/Handle.h"
00010 #include "art/Persistency/Common/GroupQueryResult.h"
00011 #include "canvas/Utilities/DebugMacros.h"
00012 #include "canvas/Utilities/Exception.h"
00013 #include "fhiclcpp/ParameterSet.h"
00014
00015 #include "artdaq/DAQrate/DataSenderManager.hh"
00016 #include "artdaq/DAQdata/Globals.hh"
00017 #include "artdaq-core/Data/Fragment.hh"
00018
00019 #include <iomanip>
00020 #include <iostream>
00021 #include <sstream>
00022 #include <string>
00023 #include <vector>
00024 #include <memory>
00025 #include <unistd.h>
00026
00027 namespace art
00028 {
00029 class BinaryNetOutput;
00030 }
00031
00032 using art::BinaryNetOutput;
00033 using fhicl::ParameterSet;
00034
00040 class art::BinaryNetOutput final: public OutputModule
00041 {
00042 public:
00055 explicit BinaryNetOutput(ParameterSet const& ps);
00056
00060 virtual ~BinaryNetOutput();
00061
00062 private:
00063 void beginJob() override;
00064
00065 void endJob() override;
00066
00067 void write(EventPrincipal&) override;
00068
00069 void writeRun(RunPrincipal&) override {};
00070 void writeSubRun(SubRunPrincipal&) override {};
00071
00072 void initialize_MPI_();
00073
00074 void deinitialize_MPI_();
00075
00076 bool readParameterSet_(fhicl::ParameterSet const& pset);
00077
00078 private:
00079 ParameterSet data_pset_;
00080 std::string name_ = "BinaryNetOutput";
00081 int rt_priority_ = 0;
00082 std::unique_ptr<artdaq::DataSenderManager> sender_ptr_ = {nullptr};
00083 };
00084
00085 art::BinaryNetOutput::
00086 BinaryNetOutput(ParameterSet const& ps)
00087 : OutputModule(ps)
00088 {
00089 FDEBUG(1) << "Begin: BinaryNetOutput::BinaryNetOutput(ParameterSet const& ps)\n";
00090 readParameterSet_(ps);
00091 FDEBUG(1) << "End: BinaryNetOutput::BinaryNetOutput(ParameterSet const& ps)\n";
00092 }
00093
00094 art::BinaryNetOutput::
00095 ~BinaryNetOutput()
00096 {
00097 FDEBUG(1) << "Begin/End: BinaryNetOutput::~BinaryNetOutput()\n";
00098 }
00099
00100 void
00101 art::BinaryNetOutput::
00102 beginJob()
00103 {
00104 FDEBUG(1) << "Begin: BinaryNetOutput::beginJob()\n";
00105 initialize_MPI_();
00106 FDEBUG(1) << "End: BinaryNetOutput::beginJob()\n";
00107 }
00108
00109 void
00110 art::BinaryNetOutput::
00111 endJob()
00112 {
00113 FDEBUG(1) << "Begin: BinaryNetOutput::endJob()\n";
00114 deinitialize_MPI_();
00115 FDEBUG(1) << "End: BinaryNetOutput::endJob()\n";
00116 }
00117
00118
00119 void
00120 art::BinaryNetOutput::
00121 initialize_MPI_()
00122 {
00123 if (rt_priority_ > 0)
00124 {
00125 #pragma GCC diagnostic push
00126 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
00127 sched_param s_param = {};
00128 s_param.sched_priority = rt_priority_;
00129 int status = pthread_setschedparam(pthread_self(), SCHED_RR, &s_param);
00130 if (status != 0)
00131 {
00132 TLOG_ERROR(name_)
00133 << "Failed to set realtime priority to " << rt_priority_
00134 << ", return code = " << status << TLOG_ENDL;
00135 }
00136 #pragma GCC diagnostic pop
00137 }
00138
00139 sender_ptr_ = std::make_unique<artdaq::DataSenderManager>(data_pset_);
00140 assert(sender_ptr_);
00141 }
00142
00143 void
00144 art::BinaryNetOutput::
00145 deinitialize_MPI_()
00146 {
00147 sender_ptr_.reset(nullptr);
00148 }
00149
00150 bool
00151 art::BinaryNetOutput::
00152 readParameterSet_(fhicl::ParameterSet const& pset)
00153 {
00154 TLOG_DEBUG(name_) << "BinaryNetOutput::readParameterSet_ method called with "
00155 << "ParameterSet = \"" << pset.to_string()
00156 << "\"." << TLOG_ENDL;
00157
00158
00159 data_pset_ = pset;
00160 name_ = pset.get<std::string>("module_name", "BinaryNetOutput");
00161 rt_priority_ = pset.get<int>("rt_priority", 0);
00162
00163 TRACE(4, "BinaryNetOutput::readParameterSet()");
00164
00165 return true;
00166 }
00167
00168 void
00169 art::BinaryNetOutput::
00170 write(EventPrincipal& ep)
00171 {
00172 assert(sender_ptr_);
00173
00174 using RawEvent = artdaq::Fragments;;
00175 using RawEvents = std::vector<RawEvent>;
00176 using RawEventHandle = art::Handle<RawEvent>;
00177 using RawEventHandles = std::vector<RawEventHandle>;
00178
00179 auto result_handles = std::vector<art::GroupQueryResult>();
00180
00181 #if ART_HEX_VERSION < 0x20906
00182 ep.getManyByType(art::TypeID(typeid(RawEvent)), result_handles);
00183 #else
00184 auto const& wrapped = art::WrappedTypeID::make<RawEvent>();
00185 result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
00186 #endif
00187
00188 for (auto const& result_handle : result_handles)
00189 {
00190 auto const raw_event_handle = RawEventHandle(result_handle);
00191
00192 if (!raw_event_handle.isValid())
00193 continue;
00194
00195 for (auto const& fragment : *raw_event_handle)
00196 {
00197 auto fragment_copy = fragment;
00198 auto fragid_id = fragment_copy.fragmentID();
00199 auto sequence_id = fragment_copy.sequenceID();
00200 TRACE(1, "BinaryNetOutput::write seq=%lu frag=%i start", sequence_id, fragid_id);
00201 sender_ptr_->sendFragment(std::move(fragment_copy));
00202 TRACE(2, "BinaryNetOutput::write seq=%lu frag=%i done", sequence_id, fragid_id);
00203 }
00204 }
00205
00206 return;
00207 }
00208
00209 DEFINE_ART_MODULE(art::BinaryNetOutput)