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"
35 using fhicl::ParameterSet;
62 void beginJob()
override;
64 void endJob()
override;
66 void write(EventPrincipal&)
override;
68 void writeRun(RunPrincipal&)
override{};
69 void writeSubRun(SubRunPrincipal&)
override{};
72 std::string name_ =
"HDFFileOutput";
73 art::FileStatsCollector fstats_;
75 std::unique_ptr<artdaq::hdf5::FragmentDataset> ntuple_;
80 , fstats_{name_, processName()}
82 TLOG(TLVL_DEBUG) <<
"Begin: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
83 ntuple_ = artdaq::hdf5::MakeDatasetPlugin(ps,
"dataset");
85 <<
"End: HDFFileOutput::HDFFileOutput(ParameterSet const& ps)\n";
90 void art::HDFFileOutput::beginJob()
92 TLOG(TLVL_DEBUG) <<
"Begin: HDFFileOutput::beginJob()\n";
93 TLOG(TLVL_DEBUG) <<
"End: HDFFileOutput::beginJob()\n";
96 void art::HDFFileOutput::endJob()
98 TLOG(TLVL_DEBUG) <<
"Begin: HDFFileOutput::endJob()\n";
99 TLOG(TLVL_DEBUG) <<
"End: HDFFileOutput::endJob()\n";
102 void art::HDFFileOutput::write(EventPrincipal& ep)
104 TLOG(TLVL_TRACE) <<
"Begin: HDFFileOutput::write(EventPrincipal& ep)";
105 using RawEvent = artdaq::Fragments;
106 using RawEvents = std::vector<RawEvent>;
107 using RawEventHandle = art::Handle<RawEvent>;
108 using RawEventHeaderHandle = art::Handle<artdaq::detail::RawEventHeader>;
110 auto hdr_found =
false;
111 auto sequence_id = artdaq::Fragment::InvalidSequenceID;
113 TLOG(5) <<
"write: Retrieving event Fragments";
115 auto result_handles = std::vector<art::GroupQueryResult>();
116 auto const& wrapped = art::WrappedTypeID::make<RawEvent>();
117 #if ART_HEX_VERSION >= 0x30000
118 ModuleContext
const mc{moduleDescription()};
119 ProcessTag
const processTag{
"", mc.moduleDescription().processName()};
121 result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
123 result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
126 for (
auto const& result_handle : result_handles)
128 auto const raw_event_handle = RawEventHandle(result_handle);
130 if (raw_event_handle.isValid() && raw_event_handle.product()->size() > 0)
132 TLOG(10) <<
"raw_event_handle labels: branchName:" << raw_event_handle.provenance()->branchName();
133 TLOG(10) <<
"raw_event_handle labels: friendlyClassName:" << raw_event_handle.provenance()->friendlyClassName();
134 TLOG(10) <<
"raw_event_handle labels: inputTag:" << raw_event_handle.provenance()->inputTag();
135 TLOG(10) <<
"raw_event_handle labels: moduleLabel:" << raw_event_handle.provenance()->moduleLabel();
136 TLOG(10) <<
"raw_event_handle labels: processName:" << raw_event_handle.provenance()->processName();
137 sequence_id = (*raw_event_handle).front().sequenceID();
139 TLOG(5) <<
"write: Writing to dataset";
140 ntuple_->insertMany(*raw_event_handle);
144 TLOG(5) <<
"write: Retrieving Event Header";
146 auto result_handles = std::vector<art::GroupQueryResult>();
147 auto const& wrapped = art::WrappedTypeID::make<artdaq::detail::RawEventHeader>();
148 #if ART_HEX_VERSION >= 0x30000
149 ModuleContext
const mc{moduleDescription()};
150 ProcessTag
const processTag{
"", mc.moduleDescription().processName()};
152 result_handles = ep.getMany(mc, wrapped, art::MatchAllSelector{}, processTag);
154 result_handles = ep.getMany(wrapped, art::MatchAllSelector{});
157 for (
auto const& result_handle : result_handles)
159 auto const raw_event_header_handle = RawEventHeaderHandle(result_handle);
161 if (raw_event_header_handle.isValid())
163 auto const& header = *raw_event_header_handle;
165 auto evt_sequence_id = header.sequence_id;
166 TLOG(TLVL_TRACE) <<
"HDFFileOutput::write header seq=" << evt_sequence_id;
168 ntuple_->insertHeader(header);
171 TLOG(5) <<
"HDFFileOutput::write header seq=" << evt_sequence_id <<
" done errno=" << errno;
177 TLOG(5) <<
"write: Header not found, autogenerating";
178 artdaq::detail::RawEventHeader hdr(ep.run(), ep.subRun(), ep.event(), sequence_id);
179 hdr.is_complete =
true;
181 ntuple_->insertHeader(hdr);
184 #if ART_HEX_VERSION < 0x30000
185 fstats_.recordEvent(ep.id());
187 fstats_.recordEvent(ep.eventID());
189 TLOG(TLVL_TRACE) <<
"End: HDFFileOUtput::write(EventPrincipal& ep)";
The HDFFileOutput module streams art Events to a binary file, bypassing ROOT.
virtual ~HDFFileOutput()
HDFFileOutput Destructor.
HDFFileOutput(ParameterSet const &ps)
HDFFileOutput Constructor.