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/Utilities/DebugMacros.h"
15 #include "canvas/Utilities/Exception.h"
16 #include "fhiclcpp/ParameterSet.h"
18 #include "artdaq/DAQdata/Globals.hh"
19 #include "artdaq/ArtModules/ArtdaqFragmentNamingService.h"
36 using fhicl::ParameterSet;
68 void beginJob()
override;
70 void endJob()
override;
72 void write(EventPrincipal& )
override;
74 void writeRun(RunPrincipal& )
override{};
75 void writeSubRun(SubRunPrincipal& )
override{};
78 std::string name_ =
"HDFFileOutput";
79 art::FileStatsCollector fstats_;
81 std::unique_ptr<artdaq::hdf5::FragmentDataset> ntuple_;
86 , fstats_{name_, processName()}
88 TLOG(TLVL_DEBUG) <<
"Begin: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
90 ntuple_ = artdaq::hdf5::MakeDatasetPlugin(ps,
"dataset");
92 <<
"End: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
97 void art::HDFFileOutput::beginJob()
99 TLOG(TLVL_DEBUG) <<
"Begin: HDFFileOutput::beginJob()\n";
100 TLOG(TLVL_DEBUG) <<
"End: HDFFileOutput::beginJob()\n";
103 void art::HDFFileOutput::endJob()
105 TLOG(TLVL_DEBUG) <<
"Begin: HDFFileOutput::endJob()\n";
106 TLOG(TLVL_DEBUG) <<
"End: HDFFileOutput::endJob()\n";
109 void art::HDFFileOutput::write(EventPrincipal& ep)
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>;
117 auto hdr_found =
false;
118 auto sequence_id = artdaq::Fragment::InvalidSequenceID;
120 TLOG(5) <<
"write: Retrieving event Fragments";
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()};
128 result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
130 result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
133 for (
auto const& result_handle : result_handles)
135 auto const raw_event_handle = RawEventHandle(result_handle);
137 if (raw_event_handle.isValid() && !raw_event_handle.product()->empty())
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();
146 TLOG(5) <<
"write: Writing to dataset";
147 ntuple_->insertMany(*raw_event_handle);
151 TLOG(5) <<
"write: Retrieving Event Header";
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()};
159 result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
161 result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
164 for (
auto const& result_handle : result_handles)
166 auto const raw_event_header_handle = RawEventHeaderHandle(result_handle);
168 if (raw_event_header_handle.isValid())
170 auto const& header = *raw_event_header_handle;
172 auto evt_sequence_id = header.sequence_id;
173 TLOG(TLVL_TRACE) <<
"HDFFileOutput::write header seq=" << evt_sequence_id;
175 ntuple_->insertHeader(header);
178 TLOG(5) <<
"HDFFileOutput::write header seq=" << evt_sequence_id <<
" done errno=" << errno;
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;
188 ntuple_->insertHeader(hdr);
191 #if ART_HEX_VERSION < 0x30000
192 fstats_.recordEvent(ep.id());
194 fstats_.recordEvent(ep.eventID());
196 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.