1 #include "art/Framework/Core/FileBlock.h"
2 #include "art/Framework/Core/InputSourceMacros.h"
3 #include "art/Framework/Core/ProductRegistryHelper.h"
4 #include "art/Framework/IO/Sources/Source.h"
5 #include "art/Framework/IO/Sources/SourceHelper.h"
6 #include "art/Framework/IO/Sources/SourceTraits.h"
7 #include "art/Framework/Services/Registry/ServiceHandle.h"
9 #if ART_HEX_VERSION < 0x20703
10 # include "art/Persistency/Provenance/BranchIDListHelper.h"
12 #if ART_HEX_VERSION < 0x20900
13 #include "art/Persistency/Provenance/BranchIDListRegistry.h"
14 #include "canvas/Persistency/Provenance/BranchIDList.h"
16 #include "art/Persistency/Provenance/MasterProductRegistry.h"
17 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
18 #include "art/Persistency/Provenance/ProductMetaData.h"
20 #include "canvas/Persistency/Common/EDProduct.h"
21 #include "canvas/Persistency/Common/Wrapper.h"
22 #include "canvas/Persistency/Provenance/BranchDescription.h"
23 #include "canvas/Persistency/Provenance/BranchKey.h"
24 #include "canvas/Persistency/Provenance/History.h"
25 #include "canvas/Persistency/Provenance/ParentageRegistry.h"
26 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
27 #include "canvas/Persistency/Provenance/ProcessHistory.h"
28 #include "canvas/Persistency/Provenance/ProcessHistoryID.h"
29 #include "canvas/Persistency/Provenance/ProductList.h"
30 #include "canvas/Persistency/Provenance/ProductProvenance.h"
31 #include "canvas/Utilities/DebugMacros.h"
33 #include "fhiclcpp/make_ParameterSet.h"
34 #include "fhiclcpp/ParameterSet.h"
35 #include "fhiclcpp/ParameterSetID.h"
36 #include "fhiclcpp/ParameterSetRegistry.h"
40 #include <TBufferFile.h>
42 #include "artdaq/DAQdata/Globals.hh"
43 #include "artdaq/ArtModules/InputUtilities.hh"
44 #include "artdaq-core/Data/detail/ParentageMap.hh"
45 #include "artdaq-core/Utilities/ExceptionHandler.hh"
102 ArtdaqInput(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper,
103 art::SourceHelper
const& pm);
114 void readFile(
const std::string&, art::FileBlock*& fb);
131 bool readNext(art::RunPrincipal*
const inR,
132 art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
133 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
137 readAndConstructPrincipal(std::unique_ptr<TBufferFile>&,
139 art::RunPrincipal*
const,
140 art::SubRunPrincipal*
const,
142 art::SubRunPrincipal*&,
143 art::EventPrincipal*&);
146 void readDataProducts(std::unique_ptr<TBufferFile>&, T*&);
148 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
150 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
152 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
156 bool shutdownMsgReceived_;
157 bool outputFileCloseNeeded_;
158 art::SourceHelper
const& pm_;
159 U communicationWrapper_;
162 template <
typename U>
165 art::ProductRegistryHelper& helper,
166 art::SourceHelper
const& pm)
167 : shutdownMsgReceived_(false)
168 , outputFileCloseNeeded_(false)
170 , communicationWrapper_(ps)
172 artdaq::configureMessageFacility(
"artdaqart");
180 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::ArtdaqInput(" <<
181 "const fhicl::ParameterSet& ps, " <<
182 "art::ProductRegistryHelper& helper, " <<
183 "const art::SourceHelper& pm)" << TLOG_ENDL;
186 TLOG_ARB(5,
"ArtdaqInput") <<
"Going to receive init message" << TLOG_ENDL;
187 std::unique_ptr<TBufferFile> msg(
nullptr);
188 communicationWrapper_.receiveInitMessage(msg);
189 TLOG_ARB(5,
"ArtdaqInput") <<
"Init message received" << TLOG_ENDL;
193 throw art::Exception(art::errors::DataCorruption) <<
194 "ArtdaqInput: Could not receive init message!";
198 unsigned long dummy = 0;
199 msg->ReadULong(dummy);
204 unsigned long ps_cnt = 0;
205 msg->ReadULong(ps_cnt);
206 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set count: " << ps_cnt << TLOG_ENDL;
207 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: reading parameter sets ..." << TLOG_ENDL;
208 for (
unsigned long I = 0; I < ps_cnt; ++I)
210 std::string pset_str =
"";
211 msg->ReadStdString(pset_str);
213 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set: " << pset_str << TLOG_ENDL;
215 fhicl::ParameterSet pset;
216 fhicl::make_ParameterSet(pset_str, pset);
219 fhicl::ParameterSetRegistry::put(pset);
221 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: finished reading parameter sets." << TLOG_ENDL;
226 art::ProductList* productlist = ReadObjectAny<art::ProductList>(msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInput::ArtdaqInput");
227 helper.productList(productlist);
228 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: got product list" << TLOG_ENDL;
230 #if ART_HEX_VERSION < 0x20900
231 if (art::debugit() >= 1)
233 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: before BranchIDLists" << TLOG_ENDL;
235 #if ART_HEX_VERSION >= 0x20703
236 BranchIDLists
const * bil = &BranchIDListRegistry::instance().data();
238 BranchIDLists* bil = &BranchIDListRegistry::instance()->data();
240 int max_bli = bil->size();
241 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: max_bli: " << max_bli << TLOG_ENDL;
242 for (
int i = 0; i < max_bli; ++i)
244 int max_prdidx = (*bil)[i].size();
245 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: max_prdidx: " << max_prdidx << TLOG_ENDL;
246 for (
int j = 0; j < max_prdidx; ++j)
248 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput:"
255 <<
static_cast<unsigned long>((*bil)[i][j])
256 << std::dec << TLOG_ENDL;
264 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ProcessHistory" << TLOG_ENDL;
265 art::ProcessHistoryMap* phm = ReadObjectAny<art::ProcessHistoryMap>(msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInput::ArtdaqInput");
268 ProcessHistoryRegistry::put(*phm);
269 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInput's ProcessHistoryRegistry");
274 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ParentageMap" << TLOG_ENDL;
275 ParentageMap* parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInput::ArtdaqInput");
276 ParentageRegistry::put(*parentageMap);
281 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::ArtdaqInput(" <<
"const fhicl::ParameterSet& ps, " <<
"art::ProductRegistryHelper& helper, " <<
"const art::SourceHelper& pm)" << TLOG_ENDL;
284 template <
typename U>
288 template <
typename U>
293 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin/End: ArtdaqInput::closeCurrentFile()" << TLOG_ENDL;
296 template <
typename U>
301 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::"
302 "readFile(const std::string& name, art::FileBlock*& fb)" << TLOG_ENDL;
303 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInput2013"),
305 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::"
306 "readFile(const std::string& name, art::FileBlock*& fb)" << TLOG_ENDL;
309 template <
typename U>
314 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::hasMoreData()" << TLOG_ENDL;
315 if (shutdownMsgReceived_)
317 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
318 "returning false on shutdownMsgReceived_." << TLOG_ENDL;
319 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()" << TLOG_ENDL;
322 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
323 "returning true on not shutdownMsgReceived_." << TLOG_ENDL;
324 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()" << TLOG_ENDL;
328 template <
typename U>
332 unsigned long msg_type_code,
333 art::RunPrincipal*
const inR,
334 art::SubRunPrincipal*
const inSR,
335 art::RunPrincipal*& outR,
336 art::SubRunPrincipal*& outSR,
337 art::EventPrincipal*& outE)
342 std::unique_ptr<art::RunAuxiliary> run_aux;
343 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
344 std::unique_ptr<art::EventAuxiliary> event_aux;
345 std::shared_ptr<History> history;
347 if (msg_type_code == 2)
350 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing EndRun message ..." << TLOG_ENDL;
352 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
355 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush RunPrincipal ..." << TLOG_ENDL;
356 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
358 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush SubRunPrincipal ..." << TLOG_ENDL;
359 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
361 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush EventPrincipal ..." << TLOG_ENDL;
362 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
364 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
365 "finished processing EndRun message." << TLOG_ENDL;
367 else if (msg_type_code == 3)
370 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing EndSubRun message ..." << TLOG_ENDL;
372 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
375 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush RunPrincipal ..." << TLOG_ENDL;
376 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
393 art::Timestamp currentTime = time(0);
394 if (inR !=
nullptr) { inR->setEndTime(currentTime); }
395 if (inSR !=
nullptr) { inSR->setEndTime(currentTime); }
397 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush SubRunPrincipal ..." << TLOG_ENDL;
398 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
400 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush EventPrincipal ..." << TLOG_ENDL;
401 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
403 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"finished processing EndSubRun message." << TLOG_ENDL;
405 else if (msg_type_code == 4)
408 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing Event message ..." << TLOG_ENDL;
410 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
413 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
416 event_aux.reset(ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
418 history.reset(ReadObjectAny<art::History>(msg,
"art::History",
"ArtdaqInput::readAndConstructPrincipal"));
422 if (!history->processHistoryID().isValid())
424 throw art::Exception(art::errors::Unknown) <<
"readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
427 if ((inR ==
nullptr) || !inR->id().isValid() || (inR->run() != event_aux->run()))
431 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: making RunPrincipal ..." << TLOG_ENDL;
432 outR = pm_.makeRunPrincipal(*run_aux.get());
434 if ((inSR ==
nullptr) || !inSR->id().isValid() || (inSR->subRun() != event_aux->subRun()))
438 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making SubRunPrincipal ..." << TLOG_ENDL;
439 outSR = pm_.makeSubRunPrincipal(*subrun_aux.get());
441 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: making EventPrincipal ..." << TLOG_ENDL;
442 outE = pm_.makeEventPrincipal(*event_aux.get(), std::move(history));
444 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"finished processing Event message." << TLOG_ENDL;
448 template <
typename U>
454 unsigned long prd_cnt = 0;
456 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: reading data product count ..." << TLOG_ENDL;
457 msg->ReadULong(prd_cnt);
458 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: product count: " << prd_cnt << TLOG_ENDL;
463 const ProductList& productList = ProductMetaData::instance().productList();
465 for (
unsigned long I = 0; I < prd_cnt; ++I)
467 std::unique_ptr<BranchKey> bk;
470 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: Reading branch key." << TLOG_ENDL;
471 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInput::readDataProducts"));
474 if (art::debugit() >= 1)
476 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: got product class: '"
477 << bk->friendlyClassName_
481 << bk->productInstanceName_
483 << bk->processName_ << TLOG_ENDL;
485 ProductList::const_iterator iter;
487 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: looking up product ..." << TLOG_ENDL;
488 iter = productList.find(*bk);
489 if (iter == productList.end())
491 throw art::Exception(art::errors::ProductNotFound)
492 <<
"No product is registered for\n"
493 <<
" process name: '"
494 << bk->processName_ <<
"'\n"
495 <<
" module label: '"
496 << bk->moduleLabel_ <<
"'\n"
497 <<
" product friendly class name: '"
498 << bk->friendlyClassName_ <<
"'\n"
499 <<
" product instance name: '"
500 << bk->productInstanceName_ <<
"'\n";
505 const BranchDescription& bd = iter->second;
506 std::unique_ptr<EDProduct> prd;
508 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: Reading product with wrapped name: " << bd.wrappedName()
509 <<
", TClass = " << (
void*)TClass::GetClass(bd.wrappedName().c_str()) << TLOG_ENDL;
516 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
517 auto pp =
reinterpret_cast<EDProduct*
>(p);
519 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: After ReadObjectAny(prd): p=" << p
520 <<
", EDProduct::isPresent: " << pp->isPresent() << TLOG_ENDL;
524 std::unique_ptr<const ProductProvenance> prdprov;
526 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: Reading product provenance." << TLOG_ENDL;
527 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInput::readDataProducts"));
530 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: inserting product: class: '"
531 << bd.friendlyClassName()
535 << bd.productInstanceName()
537 << bd.processName() << TLOG_ENDL;
538 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
544 template <
typename U>
547 putInPrincipal(RunPrincipal*& rp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
549 rp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forRun(rp->id()));
552 template <
typename U>
555 putInPrincipal(SubRunPrincipal*& srp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
557 srp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forSubRun(srp->id()));
560 template <
typename U>
563 putInPrincipal(EventPrincipal*& ep, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
565 TLOG_ARB(6,
"ArtdaqInput") <<
"EventPrincipal size before put: " << ep->size();
566 ep->put(std::move(prd), bd, std::move(prdprov));
567 TLOG_ARB(6,
"ArtdaqInput") <<
"EventPrincipal size after put: " << ep->size();
571 template <
typename U>
574 readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
575 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
576 art::EventPrincipal*& outE)
578 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::readNext" << TLOG_ENDL;
579 if (outputFileCloseNeeded_)
581 outputFileCloseNeeded_ =
false;
584 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on outputFileCloseNeeded_" << TLOG_ENDL;
585 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
589 std::unique_ptr<TBufferFile> msg;
590 communicationWrapper_.receiveMessage(msg);
594 shutdownMsgReceived_ =
true;
601 unsigned long msg_type_code = 0;
603 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"getting message type code ..." << TLOG_ENDL;
604 msg->ReadULong(msg_type_code);
605 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"message type: " << msg_type_code << TLOG_ENDL;
607 if (msg_type_code == 5)
610 shutdownMsgReceived_ =
true;
611 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on Shutdown message." << TLOG_ENDL;
612 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
616 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR,
621 if (msg_type_code == 2)
625 readDataProducts(msg, outR);
627 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on EndRun message." << TLOG_ENDL;
628 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
631 else if (msg_type_code == 3)
642 if (inR != 0 && inSR != 0 && outR != 0 && outSR != 0)
644 if (inR->id().isFlush() && inSR->id().isFlush() &&
645 outR->id().isFlush() && outSR->id().isFlush())
649 outputFileCloseNeeded_ =
true;
654 readDataProducts(msg, outSR);
657 outputFileCloseNeeded_ =
true;
658 TLOG_ARB(5,
"ArtdaqInput") <<
"readNext: returning true on EndSubRun message." << TLOG_ENDL;
659 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
662 else if (msg_type_code == 4)
665 readDataProducts(msg, outE);
666 TLOG_ARB(5,
"ArtdaqInput") <<
"readNext: returning true on Event message." << TLOG_ENDL;
667 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
672 TLOG_ARB(5,
"ArtdaqInput") <<
"readNext: returning false on unknown msg_type_code!" << TLOG_ENDL;
673 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
void printProcessHistoryID(const std::string &label, const T &object)
Print the processHistoryID from the object.
void printProcessMap(const T &mappable, const std::string description)
Print data from a map-like class.