artdaq_demo_hdf5  v1_01_05
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  ModuleContext const mc{moduleDescription()};
125  ProcessTag const processTag{"", mc.moduleDescription().processName()};
126 
127  result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
128 
129 
130  for (auto const& result_handle : result_handles)
131  {
132  auto const raw_event_handle = RawEventHandle(result_handle);
133 
134  if (raw_event_handle.isValid() && !raw_event_handle.product()->empty())
135  {
136  TLOG(10) << "raw_event_handle labels: branchName:" << raw_event_handle.provenance()->branchName();
137  TLOG(10) << "raw_event_handle labels: friendlyClassName:" << raw_event_handle.provenance()->friendlyClassName();
138  TLOG(10) << "raw_event_handle labels: inputTag:" << raw_event_handle.provenance()->inputTag();
139  TLOG(10) << "raw_event_handle labels: moduleLabel:" << raw_event_handle.provenance()->moduleLabel();
140  TLOG(10) << "raw_event_handle labels: processName:" << raw_event_handle.provenance()->processName();
141  sequence_id = (*raw_event_handle).front().sequenceID();
142 
143  TLOG(5) << "write: Writing to dataset";
144  ntuple_->insertMany(*raw_event_handle);
145  }
146  }
147  }
148  TLOG(5) << "write: Retrieving Event Header";
149  {
150  auto result_handles = std::vector<art::GroupQueryResult>();
151  auto const& wrapped = art::WrappedTypeID::make<artdaq::detail::RawEventHeader>();
152 
153  ModuleContext const mc{moduleDescription()};
154  ProcessTag const processTag{"", mc.moduleDescription().processName()};
155 
156  result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
157 
158 
159  for (auto const& result_handle : result_handles)
160  {
161  auto const raw_event_header_handle = RawEventHeaderHandle(result_handle);
162 
163  if (raw_event_header_handle.isValid())
164  {
165  auto const& header = *raw_event_header_handle;
166 
167  auto evt_sequence_id = header.sequence_id;
168  TLOG(TLVL_TRACE) << "HDFFileOutput::write header seq=" << evt_sequence_id;
169 
170  ntuple_->insertHeader(header);
171 
172  hdr_found = true;
173  TLOG(5) << "HDFFileOutput::write header seq=" << evt_sequence_id << " done errno=" << errno;
174  }
175  }
176  }
177  if (!hdr_found)
178  {
179  TLOG(5) << "write: Header not found, autogenerating";
180  artdaq::detail::RawEventHeader hdr(ep.run(), ep.subRun(), ep.event(), sequence_id, 0);
181  hdr.is_complete = true;
182 
183  ntuple_->insertHeader(hdr);
184  }
185 
186  fstats_.recordEvent(ep.eventID());
187 
188  TLOG(TLVL_TRACE) << "End: HDFFileOUtput::write(EventPrincipal& ep)";
189 }
190 
191 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.