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