artdaq_demo_hdf5  v1_02_01
HDFFileOutput_module.cc
1 #define TRACE_NAME "HDFFileOutput"
2 
3 #include "artdaq-demo-hdf5/HDF5/MakeDatasetPlugin.hh"
4 
5 #include "art/Framework/Core/ModuleMacros.h"
6 #include "art/Framework/Core/OutputModule.h"
7 #include "art/Framework/IO/FileStatsCollector.h"
8 #include "art/Framework/IO/PostCloseFileRenamer.h"
9 #include "art/Framework/Principal/EventPrincipal.h"
10 #include "art/Framework/Principal/Handle.h"
11 #include "art/Framework/Principal/RunPrincipal.h"
12 #include "art/Framework/Principal/SubRunPrincipal.h"
13 #include "art/Persistency/Common/GroupQueryResult.h"
14 #include "canvas/Persistency/Common/Wrapper.h"
15 #include "canvas/Utilities/DebugMacros.h"
16 #include "canvas/Utilities/Exception.h"
17 #if ART_HEX_VERSION < 0x30901
18 #include "canvas/Utilities/WrappedTypeID.h"
19 #endif
20 #include "fhiclcpp/ParameterSet.h"
21 
22 #include "artdaq/ArtModules/ArtdaqFragmentNamingService.h"
23 #include "artdaq/DAQdata/Globals.hh"
24 
25 #include <unistd.h>
26 #include <cstdio>
27 #include <fstream>
28 #include <iomanip>
29 #include <iostream>
30 #include <memory>
31 #include <sstream>
32 #include <string>
33 #include <vector>
34 
35 namespace art {
36 class HDFFileOutput;
37 }
38 
39 using art::HDFFileOutput;
40 using fhicl::ParameterSet;
41 
45 class art::HDFFileOutput final : public OutputModule
46 {
47 public:
59  explicit HDFFileOutput(ParameterSet const& ps);
60 
64  ~HDFFileOutput() override;
65 
66 private:
67  HDFFileOutput(HDFFileOutput const&) = delete;
68  HDFFileOutput(HDFFileOutput&&) = delete;
69  HDFFileOutput& operator=(HDFFileOutput const&) = delete;
70  HDFFileOutput& operator=(HDFFileOutput&&) = delete;
71 
72  void beginJob() override;
73 
74  void endJob() override;
75 
76  void write(EventPrincipal& /*ep*/) override;
77 
78  void writeRun(RunPrincipal& /*r*/) override{};
79  void writeSubRun(SubRunPrincipal& /*sr*/) override{};
80 
81 private:
82  std::string name_ = "HDFFileOutput";
83  art::FileStatsCollector fstats_;
84 
85  std::unique_ptr<artdaq::hdf5::FragmentDataset> ntuple_;
86 };
87 
88 art::HDFFileOutput::HDFFileOutput(ParameterSet const& ps)
89  : OutputModule(ps)
90  , fstats_{name_, processName()}
91 {
92  TLOG(TLVL_DEBUG) << "Begin: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
93 
94  ntuple_ = artdaq::hdf5::MakeDatasetPlugin(ps, "dataset");
95  TLOG(TLVL_DEBUG)
96  << "End: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
97 }
98 
99 art::HDFFileOutput::~HDFFileOutput() { TLOG(TLVL_DEBUG) << "Begin/End: HDFFileOutput::~HDFFileOutput()\n"; }
100 
101 void art::HDFFileOutput::beginJob()
102 {
103  TLOG(TLVL_DEBUG) << "Begin: HDFFileOutput::beginJob()\n";
104  TLOG(TLVL_DEBUG) << "End: HDFFileOutput::beginJob()\n";
105 }
106 
107 void art::HDFFileOutput::endJob()
108 {
109  TLOG(TLVL_DEBUG) << "Begin: HDFFileOutput::endJob()\n";
110  TLOG(TLVL_DEBUG) << "End: HDFFileOutput::endJob()\n";
111 }
112 
113 void art::HDFFileOutput::write(EventPrincipal& ep)
114 {
115  TLOG(TLVL_TRACE) << "Begin: HDFFileOutput::write(EventPrincipal& ep)";
116  using RawEvent = artdaq::Fragments;
117  using RawEvents = std::vector<RawEvent>;
118  using RawEventHandle = art::Handle<RawEvent>;
119  using RawEventHeaderHandle = art::Handle<artdaq::detail::RawEventHeader>;
120 
121  auto hdr_found = false;
122  auto sequence_id = artdaq::Fragment::InvalidSequenceID;
123 
124  TLOG(5) << "write: Retrieving event Fragments";
125  {
126  auto result_handles = std::vector<art::GroupQueryResult>();
127  auto const& wrapped = art::WrappedTypeID::make<RawEvent>();
128  ModuleContext const mc{moduleDescription()};
129  ProcessTag const processTag{"", mc.moduleDescription().processName()};
130 
131  result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
132 
133  for (auto const& result_handle : result_handles)
134  {
135  auto const raw_event_handle = RawEventHandle(result_handle);
136 
137  if (raw_event_handle.isValid() && !raw_event_handle.product()->empty())
138  {
139  TLOG(10) << "raw_event_handle labels: branchName:" << raw_event_handle.provenance()->branchName();
140  TLOG(10) << "raw_event_handle labels: friendlyClassName:" << raw_event_handle.provenance()->friendlyClassName();
141  TLOG(10) << "raw_event_handle labels: inputTag:" << raw_event_handle.provenance()->inputTag();
142  TLOG(10) << "raw_event_handle labels: moduleLabel:" << raw_event_handle.provenance()->moduleLabel();
143  TLOG(10) << "raw_event_handle labels: processName:" << raw_event_handle.provenance()->processName();
144  sequence_id = (*raw_event_handle).front().sequenceID();
145 
146  TLOG(5) << "write: Writing to dataset";
147  ntuple_->insertMany(*raw_event_handle);
148  }
149  }
150  }
151  TLOG(5) << "write: Retrieving Event Header";
152  {
153  auto result_handles = std::vector<art::GroupQueryResult>();
154  auto const& wrapped = art::WrappedTypeID::make<artdaq::detail::RawEventHeader>();
155 
156  ModuleContext const mc{moduleDescription()};
157  ProcessTag const processTag{"", mc.moduleDescription().processName()};
158 
159  result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
160 
161  for (auto const& result_handle : result_handles)
162  {
163  auto const raw_event_header_handle = RawEventHeaderHandle(result_handle);
164 
165  if (raw_event_header_handle.isValid())
166  {
167  auto const& header = *raw_event_header_handle;
168 
169  auto evt_sequence_id = header.sequence_id;
170  TLOG(TLVL_TRACE) << "HDFFileOutput::write header seq=" << evt_sequence_id;
171 
172  ntuple_->insertHeader(header);
173 
174  hdr_found = true;
175  TLOG(5) << "HDFFileOutput::write header seq=" << evt_sequence_id << " done errno=" << errno;
176  }
177  }
178  }
179  if (!hdr_found)
180  {
181  TLOG(5) << "write: Header not found, autogenerating";
182  artdaq::detail::RawEventHeader hdr(ep.run(), ep.subRun(), ep.event(), sequence_id, 0);
183  hdr.is_complete = true;
184 
185  ntuple_->insertHeader(hdr);
186  }
187 
188  fstats_.recordEvent(ep.eventID());
189 
190  TLOG(TLVL_TRACE) << "End: HDFFileOUtput::write(EventPrincipal& ep)";
191 }
192 
193 DEFINE_ART_MODULE(art::HDFFileOutput) // NOLINT(performance-unnecessary-value-param)
The HDFFileOutput module streams art Events to a binary file, bypassing ROOT.
~HDFFileOutput() override
HDFFileOutput Destructor.
HDFFileOutput(ParameterSet const &ps)
HDFFileOutput Constructor.