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 < 0x30000
10 #include "art/Persistency/Provenance/MasterProductRegistry.h"
11 #include "art/Persistency/Provenance/ProductMetaData.h"
13 #if ART_HEX_VERSION >= 0x30200
14 #include "art_root_io/setup.h"
16 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
18 #include "canvas/Persistency/Common/EDProduct.h"
19 #include "canvas/Persistency/Common/Wrapper.h"
20 #include "canvas/Persistency/Provenance/BranchDescription.h"
21 #include "canvas/Persistency/Provenance/BranchKey.h"
22 #include "canvas/Persistency/Provenance/History.h"
23 #include "canvas/Persistency/Provenance/ParentageRegistry.h"
24 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
25 #include "canvas/Persistency/Provenance/ProcessHistory.h"
26 #include "canvas/Persistency/Provenance/ProcessHistoryID.h"
27 #include "canvas/Persistency/Provenance/ProductList.h"
28 #include "canvas/Persistency/Provenance/ProductProvenance.h"
29 #include "canvas/Persistency/Provenance/ProductTables.h"
30 #include "canvas/Utilities/DebugMacros.h"
32 #include "fhiclcpp/ParameterSet.h"
33 #include "fhiclcpp/ParameterSetID.h"
34 #include "fhiclcpp/ParameterSetRegistry.h"
35 #include "fhiclcpp/make_ParameterSet.h"
37 #include <TBufferFile.h>
41 #include "artdaq-core/Data/detail/ParentageMap.hh"
42 #include "artdaq-core/Utilities/ExceptionHandler.hh"
43 #include "artdaq/ArtModules/InputUtilities.hh"
44 #include "artdaq/DAQdata/Globals.hh"
55 #if ART_HEX_VERSION < 0x30000
59 #define HISTORY_PTR_T std::shared_ptr<art::History>
61 #define EVENT_ID eventID
62 #define SUBRUN_ID subRunID
64 #define HISTORY_PTR_T std::unique_ptr<art::History>
112 ArtdaqInput(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper, art::SourceHelper
const& pm);
123 void readFile(
const std::string&, art::FileBlock*& fb);
140 bool readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
141 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
144 void readAndConstructPrincipal(std::unique_ptr<TBufferFile>&,
unsigned long, art::RunPrincipal*
const,
145 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
146 art::EventPrincipal*&);
149 void readDataProducts(std::unique_ptr<TBufferFile>&, T*&);
151 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
152 std::unique_ptr<const ProductProvenance>&&);
154 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
155 std::unique_ptr<const ProductProvenance>&&);
157 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
158 std::unique_ptr<const ProductProvenance>&&);
161 bool shutdownMsgReceived_;
162 bool outputFileCloseNeeded_;
163 art::SourceHelper
const& pm_;
164 U communicationWrapper_;
165 ProductList* productList_;
166 HISTORY_PTR_T history_to_use_;
171 art::SourceHelper
const& pm)
172 : shutdownMsgReceived_(false), outputFileCloseNeeded_(false), pm_(pm), communicationWrapper_(ps), productList_()
174 artdaq::configureMessageFacility(
"artdaqart");
176 #if ART_HEX_VERSION >= 0x30200
181 volatile bool loop =
true;
194 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::ArtdaqInput("
195 <<
"const fhicl::ParameterSet& ps, "
196 <<
"art::ProductRegistryHelper& helper, "
197 <<
"const art::SourceHelper& pm)";
199 TLOG_ARB(5,
"ArtdaqInput") <<
"Going to receive init message";
200 std::unique_ptr<TBufferFile> msg(
nullptr);
201 communicationWrapper_.receiveInitMessage(msg);
202 TLOG_ARB(5,
"ArtdaqInput") <<
"Init message received";
206 throw art::Exception(art::errors::DataCorruption) <<
"ArtdaqInput: Could not receive init message!";
210 unsigned long dummy = 0;
211 msg->ReadULong(dummy);
216 unsigned long ps_cnt = 0;
217 msg->ReadULong(ps_cnt);
218 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set count: " << ps_cnt;
219 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: reading parameter sets ...";
220 for (
unsigned long I = 0; I < ps_cnt; ++I)
222 std::string pset_str =
"";
223 msg->ReadStdString(pset_str);
225 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set: " << pset_str;
227 fhicl::ParameterSet pset;
228 fhicl::make_ParameterSet(pset_str, pset);
231 fhicl::ParameterSetRegistry::put(pset);
233 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: finished reading parameter sets.";
238 productList_ = ReadObjectAny<art::ProductList>(
239 msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInput::ArtdaqInput");
240 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Product list sz=" << productList_->size();
243 for (
auto I = productList_->begin(), E= productList_->end();I != E;++I)
245 TLOG_ARB(50,
"ArtdaqInput") <<
"Branch key: class: '" << I->first.friendlyClassName_ <<
"' modlbl: '"
246 << I->first.moduleLabel_ <<
"' instnm: '" << I->first.productInstanceName_ <<
"' procnm: '"
247 << I->first.processName_ <<
"', branch description name: " << I->second.wrappedName()
248 <<
", TClass = " << (
void*)TClass::GetClass(I->second.wrappedName().c_str());
253 #if ART_HEX_VERSION < 0x30000
254 helper.productList(productList_);
256 helper.productList(std::unique_ptr<art::ProductList>(productList_));
258 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: got product list";
260 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ProcessHistory";
261 art::ProcessHistoryMap* phm = ReadObjectAny<art::ProcessHistoryMap>(
262 msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInput::ArtdaqInput");
265 ProcessHistoryRegistry::put(*phm);
266 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInput's ProcessHistoryRegistry");
271 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ParentageMap";
272 ParentageMap* parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInput::ArtdaqInput");
273 ParentageRegistry::put(*parentageMap);
278 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading History";
279 history_to_use_.reset(ReadObjectAny<History>(msg,
"art::History",
"ArtdaqInput::ArtdaqInput"));
280 if (!history_to_use_->processHistoryID().isValid())
282 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: History from init message is INVALID!";
288 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::ArtdaqInput("
289 <<
"const fhicl::ParameterSet& ps, "
290 <<
"art::ProductRegistryHelper& helper, "
291 <<
"const art::SourceHelper& pm)";
301 TLOG_ARB(6,
"ArtdaqInput") <<
"Begin/End: ArtdaqInput::closeCurrentFile()";
307 TLOG_ARB(7,
"ArtdaqInput") <<
"Begin: ArtdaqInput::"
308 "readFile(const std::string& name, art::FileBlock*& fb)";
309 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInput2013"),
"nothing");
310 TLOG_ARB(7,
"ArtdaqInput") <<
"End: ArtdaqInput::"
311 "readFile(const std::string& name, art::FileBlock*& fb)";
317 TLOG_ARB(8,
"ArtdaqInput") <<
"Begin: ArtdaqInput::hasMoreData()";
318 if (shutdownMsgReceived_)
320 TLOG_ARB(8,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
321 "returning false on shutdownMsgReceived_.";
322 TLOG_ARB(8,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()";
325 TLOG_ARB(8,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
326 "returning true on not shutdownMsgReceived_.";
327 TLOG_ARB(8,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()";
333 art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
334 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
335 art::EventPrincipal*& outE)
340 std::unique_ptr<art::RunAuxiliary> run_aux;
341 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
342 std::unique_ptr<art::EventAuxiliary> event_aux;
344 HISTORY_PTR_T history_from_event;
351 if (msg_type_code == 2)
354 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
355 <<
"processing EndRun message ...";
357 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
360 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
361 <<
"making flush RunPrincipal ...";
362 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
364 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
365 <<
"making flush SubRunPrincipal ...";
366 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
368 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
369 <<
"making flush EventPrincipal ...";
370 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
372 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
373 <<
"finished processing EndRun message.";
375 else if (msg_type_code == 3)
378 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
379 <<
"processing EndSubRun message ...";
382 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
385 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
386 <<
"making flush RunPrincipal ...";
387 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
404 art::Timestamp currentTime = time(0);
407 #if ART_HEX_VERSION < 0x30000
408 inR->setEndTime(currentTime);
410 inR->endTime(currentTime);
415 #if ART_HEX_VERSION < 0x30000
416 inSR->setEndTime(currentTime);
418 inSR->endTime(currentTime);
422 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
423 <<
"making flush SubRunPrincipal ...";
424 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
426 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
427 <<
"making flush EventPrincipal ...";
428 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
430 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
431 <<
"finished processing EndSubRun message.";
433 else if (msg_type_code == 4)
436 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
437 <<
"processing Event message ...";
439 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
443 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
447 ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
449 history_from_event.reset(ReadObjectAny<art::History>(msg,
"art::History",
"ArtdaqInput::readAndConstructPrincipal"));
453 if (!history_from_event->processHistoryID().isValid())
455 throw art::Exception(art::errors::Unknown)
456 <<
"readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
459 else if (!history_to_use_->processHistoryID().isValid())
461 history_to_use_.swap(history_from_event);
464 else if (!art::ProcessHistoryRegistry::get().count(history_to_use_->processHistoryID()) &&
465 art::ProcessHistoryRegistry::get().count(history_from_event->processHistoryID()))
467 history_to_use_.swap(history_from_event);
470 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
471 <<
"inR: " << (
void*)inR <<
" run/expected "
472 << (inR ? std::to_string(inR->run()) :
"invalid") <<
"/" << event_aux->run();
473 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
474 <<
"inSR: " << (
void*)inSR <<
" run/expected "
475 << (inSR ? std::to_string(inSR->run()) :
"invalid") <<
"/" << event_aux->run()
476 <<
", subrun/expected " << (inSR ? std::to_string(inSR->subRun()) :
"invalid") <<
"/"
477 << event_aux->subRun();
478 if ((inR ==
nullptr) || !inR->RUN_ID().isValid() || (inR->run() != event_aux->run()))
482 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: making RunPrincipal ...";
483 outR = pm_.makeRunPrincipal(*run_aux.get());
485 art::SubRunID subrun_check(event_aux->run(), event_aux->subRun());
486 if (inSR == 0 || subrun_check != inSR->SUBRUN_ID())
490 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
491 <<
"making SubRunPrincipal ...";
492 outSR = pm_.makeSubRunPrincipal(*subrun_aux.get());
494 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: making EventPrincipal ...";
495 auto historyPtr = HISTORY_PTR_T(
new History(*(history_to_use_.get())));
496 if (!art::ProcessHistoryRegistry::get().count(history_to_use_->processHistoryID()))
498 TLOG_WARNING(
"ArtdaqInput") <<
"Stored history is not in ProcessHistoryRegistry, this event may have issues!";
500 outE = pm_.makeEventPrincipal(*event_aux.get(), std::move(historyPtr));
502 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
503 <<
"finished processing Event message.";
511 unsigned long prd_cnt = 0;
513 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: reading data product count ...";
514 msg->ReadULong(prd_cnt);
515 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: product count: " << prd_cnt;
520 for (
unsigned long I = 0; I < prd_cnt; ++I)
522 std::unique_ptr<BranchKey> bk;
524 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading branch key.";
525 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInput::readDataProducts"));
529 TLOG_ARB(13,
"ArtdaqInput") <<
"readDataProducts: got product class: '" << bk->friendlyClassName_ <<
"' modlbl: '"
530 << bk->moduleLabel_ <<
"' instnm: '" << bk->productInstanceName_ <<
"' procnm: '"
533 ProductList::const_iterator iter;
535 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: looking up product ...";
536 iter = productList_->find(*bk);
537 if (iter == productList_->end())
539 throw art::Exception(art::errors::ProductNotFound)
540 <<
"No product is registered for\n"
541 <<
" process name: '" << bk->processName_ <<
"'\n"
542 <<
" module label: '" << bk->moduleLabel_ <<
"'\n"
543 <<
" product friendly class name: '" << bk->friendlyClassName_ <<
"'\n"
544 <<
" product instance name: '" << bk->productInstanceName_ <<
"'\n";
549 const BranchDescription& bd = iter->second;
550 std::unique_ptr<EDProduct> prd;
552 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading product with wrapped name: " << bd.wrappedName()
553 <<
", TClass = " << (
void*)TClass::GetClass(bd.wrappedName().c_str());
560 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
561 auto pp =
reinterpret_cast<EDProduct*
>(p);
563 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: After ReadObjectAny(prd): p=" << p <<
", EDProduct::isPresent: " << pp->isPresent();
567 std::unique_ptr<const ProductProvenance> prdprov;
569 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading product provenance.";
570 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInput::readDataProducts"));
574 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: inserting product: class: '" << bd.friendlyClassName()
575 <<
"' modlbl: '" << bd.moduleLabel() <<
"' instnm: '" << bd.productInstanceName()
576 <<
"' procnm: '" << bd.processName() <<
"' id: '" << bd.productID() <<
"'";
577 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
584 const BranchDescription& bd,
585 std::unique_ptr<const ProductProvenance>&& prdprov)
587 #if ART_HEX_VERSION < 0x30000
588 rp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forRun(rp->RUN_ID()));
590 rp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forRun(rp->RUN_ID())));
596 const BranchDescription& bd,
597 std::unique_ptr<const ProductProvenance>&& prdprov)
599 #if ART_HEX_VERSION < 0x30000
600 srp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forSubRun(srp->SUBRUN_ID()));
602 srp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forSubRun(srp->SUBRUN_ID())));
608 const BranchDescription& bd,
609 std::unique_ptr<const ProductProvenance>&& prdprov)
611 TLOG_ARB(14,
"ArtdaqInput") <<
"EventPrincipal size before put: " << ep->size();
612 #if ART_HEX_VERSION < 0x30000
613 ep->put(std::move(prd), bd, std::move(prdprov));
615 ep->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::invalid()));
617 TLOG_ARB(14,
"ArtdaqInput") <<
"EventPrincipal size after put: " << ep->size();
622 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE)
624 TLOG_ARB(15,
"ArtdaqInput") <<
"Begin: ArtdaqInput::readNext";
625 if (outputFileCloseNeeded_)
627 outputFileCloseNeeded_ =
false;
630 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
631 <<
"returning false on outputFileCloseNeeded_";
632 TLOG_ARB(15,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
636 std::unique_ptr<TBufferFile> msg;
637 communicationWrapper_.receiveMessage(msg);
641 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext got an empty message";
642 shutdownMsgReceived_ =
true;
649 unsigned long msg_type_code = 0;
651 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
652 <<
"getting message type code ...";
653 msg->ReadULong(msg_type_code);
654 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
655 <<
"message type: " << msg_type_code;
657 if (msg_type_code == 5)
660 shutdownMsgReceived_ =
true;
661 TLOG_ARB(16,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
662 <<
"returning false on Shutdown message.";
663 TLOG_ARB(16,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
667 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR, outSR, outE);
671 if (msg_type_code == 2)
675 readDataProducts(msg, outR);
677 TLOG_ARB(17,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
678 <<
"returning false on EndRun message.";
679 TLOG_ARB(17,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
682 else if (msg_type_code == 3)
693 if (inR != 0 && inSR != 0 && outR != 0 && outSR != 0)
695 if (inR->RUN_ID().isFlush() && inSR->SUBRUN_ID().isFlush() && outR->RUN_ID().isFlush() &&
696 outSR->SUBRUN_ID().isFlush())
700 outputFileCloseNeeded_ =
true;
705 readDataProducts(msg, outSR);
708 outputFileCloseNeeded_ =
true;
709 TLOG_ARB(18,
"ArtdaqInput") <<
"readNext: returning true on EndSubRun message.";
710 TLOG_ARB(18,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
713 else if (msg_type_code == 4)
716 readDataProducts(msg, outE);
717 TLOG_ARB(19,
"ArtdaqInput") <<
"readNext: returning true on Event message.";
718 TLOG_ARB(19,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
723 TLOG_ARB(20,
"ArtdaqInput") <<
"readNext: returning false on unknown msg_type_code!";
724 TLOG_ARB(20,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
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.