artdaq_demo_hdf5  v1_01_02
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/Utilities/DebugMacros.h"
15 #include "canvas/Utilities/Exception.h"
16 #include "fhiclcpp/ParameterSet.h"
17 
18 #include "artdaq/ArtModules/ArtdaqFragmentNamingService.h"
19 #include "artdaq/DAQdata/Globals.hh"
20 
21 #include <unistd.h>
22 #include <cstdio>
23 #include <fstream>
24 #include <iomanip>
25 #include <iostream>
26 #include <memory>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 
31 namespace art {
32 class HDFFileOutput;
33 }
34 
35 using art::HDFFileOutput;
36 using fhicl::ParameterSet;
37 
41 class art::HDFFileOutput final : public OutputModule
42 {
43 public:
55  explicit HDFFileOutput(ParameterSet const& ps);
56 
60  ~HDFFileOutput() override;
61 
62 private:
63  HDFFileOutput(HDFFileOutput const&) = delete;
64  HDFFileOutput(HDFFileOutput&&) = delete;
65  HDFFileOutput& operator=(HDFFileOutput const&) = delete;
66  HDFFileOutput& operator=(HDFFileOutput&&) = delete;
67 
68  void beginJob() override;
69 
70  void endJob() override;
71 
72  void write(EventPrincipal& /*ep*/) override;
73 
74  void writeRun(RunPrincipal& /*r*/) override{};
75  void writeSubRun(SubRunPrincipal& /*sr*/) override{};
76 
77 private:
78  std::string name_ = "HDFFileOutput";
79  art::FileStatsCollector fstats_;
80 
81  std::unique_ptr<artdaq::hdf5::FragmentDataset> ntuple_;
82 };
83 
84 art::HDFFileOutput::HDFFileOutput(ParameterSet const& ps)
85  : OutputModule(ps)
86  , fstats_{name_, processName()}
87 {
88  TLOG(TLVL_DEBUG) << "Begin: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
89 
90  ntuple_ = artdaq::hdf5::MakeDatasetPlugin(ps, "dataset");
91  TLOG(TLVL_DEBUG)
92  << "End: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
93 }
94 
95 art::HDFFileOutput::~HDFFileOutput() { TLOG(TLVL_DEBUG) << "Begin/End: HDFFileOutput::~HDFFileOutput()\n"; }
96 
97 void art::HDFFileOutput::beginJob()
98 {
99  TLOG(TLVL_DEBUG) << "Begin: HDFFileOutput::beginJob()\n";
100  TLOG(TLVL_DEBUG) << "End: HDFFileOutput::beginJob()\n";
101 }
102 
103 void art::HDFFileOutput::endJob()
104 {
105  TLOG(TLVL_DEBUG) << "Begin: HDFFileOutput::endJob()\n";
106  TLOG(TLVL_DEBUG) << "End: HDFFileOutput::endJob()\n";
107 }
108 
109 void art::HDFFileOutput::write(EventPrincipal& ep)
110 {
111  TLOG(TLVL_TRACE) << "Begin: HDFFileOutput::write(EventPrincipal& ep)";
112  using RawEvent = artdaq::Fragments;
113  using RawEvents = std::vector<RawEvent>;
114  using RawEventHandle = art::Handle<RawEvent>;
115  using RawEventHeaderHandle = art::Handle<artdaq::detail::RawEventHeader>;
116 
117  auto hdr_found = false;
118  auto sequence_id = artdaq::Fragment::InvalidSequenceID;
119 
120  TLOG(5) << "write: Retrieving event Fragments";
121  {
122  auto result_handles = std::vector<art::GroupQueryResult>();
123  auto const& wrapped = art::WrappedTypeID::make<RawEvent>();
124 #if ART_HEX_VERSION >= 0x30000
125  ModuleContext const mc{moduleDescription()};
126  ProcessTag const processTag{"", mc.moduleDescription().processName()};
127 
128  result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
129 #else
130  result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
131 #endif
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 #if ART_HEX_VERSION >= 0x30000
156  ModuleContext const mc{moduleDescription()};
157  ProcessTag const processTag{"", mc.moduleDescription().processName()};
158 
159  result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
160 #else
161  result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
162 #endif
163 
164  for (auto const& result_handle : result_handles)
165  {
166  auto const raw_event_header_handle = RawEventHeaderHandle(result_handle);
167 
168  if (raw_event_header_handle.isValid())
169  {
170  auto const& header = *raw_event_header_handle;
171 
172  auto evt_sequence_id = header.sequence_id;
173  TLOG(TLVL_TRACE) << "HDFFileOutput::write header seq=" << evt_sequence_id;
174 
175  ntuple_->insertHeader(header);
176 
177  hdr_found = true;
178  TLOG(5) << "HDFFileOutput::write header seq=" << evt_sequence_id << " done errno=" << errno;
179  }
180  }
181  }
182  if (!hdr_found)
183  {
184  TLOG(5) << "write: Header not found, autogenerating";
185  artdaq::detail::RawEventHeader hdr(ep.run(), ep.subRun(), ep.event(), sequence_id, 0);
186  hdr.is_complete = true;
187 
188  ntuple_->insertHeader(hdr);
189  }
190 
191 #if ART_HEX_VERSION < 0x30000
192  fstats_.recordEvent(ep.id());
193 #else
194  fstats_.recordEvent(ep.eventID());
195 #endif
196  TLOG(TLVL_TRACE) << "End: HDFFileOUtput::write(EventPrincipal& ep)";
197 }
198 
199 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.