1 #define TRACE_NAME "HDFFileOutput"
3 #include "artdaq-demo-hdf5/HDF5/MakeDatasetPlugin.hh"
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 #include "canvas/Utilities/WrappedTypeID.h"
18 #include "fhiclcpp/ParameterSet.h"
20 #include "artdaq/ArtModules/ArtdaqFragmentNamingService.h"
21 #include "artdaq/DAQdata/Globals.hh"
38 using fhicl::ParameterSet;
70 void beginJob()
override;
72 void endJob()
override;
74 void write(EventPrincipal& )
override;
76 void writeRun(RunPrincipal& )
override{};
77 void writeSubRun(SubRunPrincipal& )
override{};
80 std::string name_ =
"HDFFileOutput";
81 art::FileStatsCollector fstats_;
83 std::unique_ptr<artdaq::hdf5::FragmentDataset> ntuple_;
88 , fstats_{name_, processName()}
90 TLOG(TLVL_DEBUG) <<
"Begin: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
92 ntuple_ = artdaq::hdf5::MakeDatasetPlugin(ps,
"dataset");
94 <<
"End: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
99 void art::HDFFileOutput::beginJob()
101 TLOG(TLVL_DEBUG) <<
"Begin: HDFFileOutput::beginJob()\n";
102 TLOG(TLVL_DEBUG) <<
"End: HDFFileOutput::beginJob()\n";
105 void art::HDFFileOutput::endJob()
107 TLOG(TLVL_DEBUG) <<
"Begin: HDFFileOutput::endJob()\n";
108 TLOG(TLVL_DEBUG) <<
"End: HDFFileOutput::endJob()\n";
111 void art::HDFFileOutput::write(EventPrincipal& ep)
113 TLOG(TLVL_TRACE) <<
"Begin: HDFFileOutput::write(EventPrincipal& ep)";
114 using RawEvent = artdaq::Fragments;
115 using RawEvents = std::vector<RawEvent>;
116 using RawEventHandle = art::Handle<RawEvent>;
117 using RawEventHeaderHandle = art::Handle<artdaq::detail::RawEventHeader>;
119 auto hdr_found =
false;
120 auto sequence_id = artdaq::Fragment::InvalidSequenceID;
122 TLOG(5) <<
"write: Retrieving event Fragments";
124 auto result_handles = std::vector<art::GroupQueryResult>();
125 auto const& wrapped = art::WrappedTypeID::make<RawEvent>();
126 ModuleContext
const mc{moduleDescription()};
127 ProcessTag
const processTag{
"", mc.moduleDescription().processName()};
129 result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
131 for (
auto const& result_handle : result_handles)
133 auto const raw_event_handle = RawEventHandle(result_handle);
135 if (raw_event_handle.isValid() && !raw_event_handle.product()->empty())
137 TLOG(10) <<
"raw_event_handle labels: branchName:" << raw_event_handle.provenance()->branchName();
138 TLOG(10) <<
"raw_event_handle labels: friendlyClassName:" << raw_event_handle.provenance()->friendlyClassName();
139 TLOG(10) <<
"raw_event_handle labels: inputTag:" << raw_event_handle.provenance()->inputTag();
140 TLOG(10) <<
"raw_event_handle labels: moduleLabel:" << raw_event_handle.provenance()->moduleLabel();
141 TLOG(10) <<
"raw_event_handle labels: processName:" << raw_event_handle.provenance()->processName();
142 sequence_id = (*raw_event_handle).front().sequenceID();
144 TLOG(5) <<
"write: Writing to dataset";
145 ntuple_->insertMany(*raw_event_handle);
149 TLOG(5) <<
"write: Retrieving Event Header";
151 auto result_handles = std::vector<art::GroupQueryResult>();
152 auto const& wrapped = art::WrappedTypeID::make<artdaq::detail::RawEventHeader>();
154 ModuleContext
const mc{moduleDescription()};
155 ProcessTag
const processTag{
"", mc.moduleDescription().processName()};
157 result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
159 for (
auto const& result_handle : result_handles)
161 auto const raw_event_header_handle = RawEventHeaderHandle(result_handle);
163 if (raw_event_header_handle.isValid())
165 auto const& header = *raw_event_header_handle;
167 auto evt_sequence_id = header.sequence_id;
168 TLOG(TLVL_TRACE) <<
"HDFFileOutput::write header seq=" << evt_sequence_id;
170 ntuple_->insertHeader(header);
173 TLOG(5) <<
"HDFFileOutput::write header seq=" << evt_sequence_id <<
" done errno=" << errno;
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;
183 ntuple_->insertHeader(hdr);
186 fstats_.recordEvent(ep.eventID());
188 TLOG(TLVL_TRACE) <<
"End: HDFFileOUtput::write(EventPrincipal& ep)";
The HDFFileOutput module streams art Events to a binary file, bypassing ROOT.
~HDFFileOutput() override
HDFFileOutput Destructor.
HDFFileOutput(ParameterSet const &ps)
HDFFileOutput Constructor.