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 #include "art/Persistency/Provenance/BranchIDListRegistry.h"
13 #include "art/Persistency/Provenance/MasterProductRegistry.h"
14 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
15 #include "art/Persistency/Provenance/ProductMetaData.h"
17 #include "canvas/Persistency/Common/EDProduct.h"
18 #include "canvas/Persistency/Common/Wrapper.h"
19 #include "canvas/Persistency/Provenance/BranchDescription.h"
20 #include "canvas/Persistency/Provenance/BranchIDList.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/Utilities/DebugMacros.h"
31 #include "fhiclcpp/make_ParameterSet.h"
32 #include "fhiclcpp/ParameterSet.h"
33 #include "fhiclcpp/ParameterSetID.h"
34 #include "fhiclcpp/ParameterSetRegistry.h"
38 #include <TBufferFile.h>
40 #include "artdaq/ArtModules/InputUtilities.hh"
41 #include "artdaq/ArtModules/detail/ParentageMap.hh"
42 #include "artdaq-core/Utilities/ExceptionHandler.hh"
99 ArtdaqInput(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper,
100 const art::SourceHelper& pm);
111 void readFile(
const std::string&, art::FileBlock*& fb);
128 bool readNext(art::RunPrincipal*
const inR,
129 art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
130 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
134 readAndConstructPrincipal(std::unique_ptr<TBufferFile>&,
136 art::RunPrincipal*
const,
137 art::SubRunPrincipal*
const,
139 art::SubRunPrincipal*&,
140 art::EventPrincipal*&);
143 void readDataProducts(std::unique_ptr<TBufferFile>&, T*&);
145 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
147 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
149 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
153 bool shutdownMsgReceived_;
154 bool outputFileCloseNeeded_;
155 const art::SourceHelper& pm_;
156 U communicationWrapper_;
159 template <
typename U>
162 art::ProductRegistryHelper& helper,
163 const art::SourceHelper& pm)
164 : shutdownMsgReceived_(false)
165 , outputFileCloseNeeded_(false)
167 , communicationWrapper_(ps)
175 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::ArtdaqInput(" \
176 "const fhicl::ParameterSet& ps, " \
177 "art::ProductRegistryHelper& helper, " \
178 "const art::SourceHelper& pm)" << TLOG_ENDL;
181 std::unique_ptr<TBufferFile> msg(
nullptr);
182 communicationWrapper_.receiveMessage(msg);
186 throw art::Exception(art::errors::DataCorruption) <<
187 "ArtdaqInput: Could not receive message!";
191 unsigned long dummy = 0;
192 msg->ReadULong(dummy);
197 unsigned long ps_cnt = 0;
198 msg->ReadULong(ps_cnt);
199 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set count: " << ps_cnt << TLOG_ENDL;
200 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: reading parameter sets ..." << TLOG_ENDL;
201 for (
unsigned long I = 0; I < ps_cnt; ++I)
203 std::string pset_str =
"";
204 msg->ReadStdString(pset_str);
206 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set: " << pset_str << TLOG_ENDL;
208 fhicl::ParameterSet pset;
209 fhicl::make_ParameterSet(pset_str, pset);
212 fhicl::ParameterSetRegistry::put(pset);
214 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: finished reading parameter sets." << TLOG_ENDL;
219 art::ProductList* productlist = ReadObjectAny<art::ProductList>(msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInput::ArtdaqInput");
220 helper.productList(productlist);
222 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: got product list" << TLOG_ENDL;
223 if (art::debugit() >= 1)
225 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: before BranchIDLists" << TLOG_ENDL;
227 #if ART_HEX_VERSION >= 0x20703
228 BranchIDLists
const * bil = &BranchIDListRegistry::instance().data();
230 BranchIDLists* bil = &BranchIDListRegistry::instance()->data();
232 int max_bli = bil->size();
233 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: max_bli: " << max_bli << TLOG_ENDL;
234 for (
int i = 0; i < max_bli; ++i)
236 int max_prdidx = (*bil)[i].size();
237 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: max_prdidx: "
238 << max_prdidx << TLOG_ENDL;
239 for (
int j = 0; j < max_prdidx; ++j)
241 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput:"
248 <<
static_cast<unsigned long>((*bil)[i][j])
249 << std::dec << TLOG_ENDL;
254 art::ProcessHistoryMap* phm = ReadObjectAny<art::ProcessHistoryMap>(msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInput::ArtdaqInput");
258 ProcessHistoryRegistry::put(*phm);
260 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInput's ProcessHistoryRegistry");
265 art::ParentageMap* parentageMap
266 = ReadObjectAny<ParentageMap>(msg,
268 "ArtdaqInput::ArtdaqInput");
269 ParentageRegistry::put(*parentageMap);
270 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: got ParentageMap" << TLOG_ENDL;
271 ParentageRegistry::put(*parentageMap);
276 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::ArtdaqInput("
277 "const fhicl::ParameterSet& ps, "
278 "art::ProductRegistryHelper& helper, "
279 "const art::SourceHelper& pm)" << TLOG_ENDL;
282 template <
typename U>
286 template <
typename U>
291 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin/End: ArtdaqInput::closeCurrentFile()" << TLOG_ENDL;
294 template <
typename U>
299 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::"
300 "readFile(const std::string& name, art::FileBlock*& fb)" << TLOG_ENDL;
301 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInput2013"),
303 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::"
304 "readFile(const std::string& name, art::FileBlock*& fb)" << TLOG_ENDL;
307 template <
typename U>
312 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::hasMoreData()" << TLOG_ENDL;
313 if (shutdownMsgReceived_)
315 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
316 "returning false on shutdownMsgReceived_." << TLOG_ENDL;
317 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()" << TLOG_ENDL;
320 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
321 "returning true on not shutdownMsgReceived_." << TLOG_ENDL;
322 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()" << TLOG_ENDL;
326 template <
typename U>
330 unsigned long msg_type_code,
331 art::RunPrincipal*
const inR,
332 art::SubRunPrincipal*
const inSR,
333 art::RunPrincipal*& outR,
334 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;
343 std::shared_ptr<History> history;
345 if (msg_type_code == 2)
348 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
349 "processing EndRun message ..." << TLOG_ENDL;
351 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
352 "ArtdaqInput::readAndConstructPrincipal"));
355 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
356 "making flush RunPrincipal ..." << TLOG_ENDL;
357 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
359 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
360 "making flush SubRunPrincipal ..." << TLOG_ENDL;
361 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(),
362 run_aux->beginTime());
364 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
365 "making flush EventPrincipal ..." << TLOG_ENDL;
366 outE = pm_.makeEventPrincipal(EventID::flushEvent(),
367 run_aux->endTime(),
true,
368 EventAuxiliary::Any);
370 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
371 "finished processing EndRun message." << TLOG_ENDL;
373 else if (msg_type_code == 3)
376 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
377 "processing EndSubRun message ..." << TLOG_ENDL;
379 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
380 "ArtdaqInput::readAndConstructPrincipal"));
383 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
384 "making flush RunPrincipal ..." << TLOG_ENDL;
385 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
402 art::Timestamp currentTime = time(0);
403 if (inR !=
nullptr) { inR->setEndTime(currentTime); }
404 if (inSR !=
nullptr) { inSR->setEndTime(currentTime); }
406 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
407 "making flush SubRunPrincipal ..." << TLOG_ENDL;
408 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(),
409 subrun_aux->beginTime());
411 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
412 "making flush EventPrincipal ..." << TLOG_ENDL;
413 outE = pm_.makeEventPrincipal(EventID::flushEvent(),
414 subrun_aux->endTime(),
true,
415 EventAuxiliary::Any);
417 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
418 "finished processing EndSubRun message." << TLOG_ENDL;
420 else if (msg_type_code == 4)
423 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
424 "processing Event message ..." << TLOG_ENDL;
426 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
427 "ArtdaqInput::readAndConstructPrincipal"));
430 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
431 "ArtdaqInput::readAndConstructPrincipal"));
434 event_aux.reset(ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
435 "ArtdaqInput::readAndConstructPrincipal"));
437 history.reset(ReadObjectAny<art::History>(msg,
"art::History",
438 "ArtdaqInput::readAndConstructPrincipal"));
442 if (!history->processHistoryID().isValid())
444 throw art::Exception(art::errors::Unknown) <<
445 "readAndConstructPrincipal: processHistoryID of history in "
446 "Event message is invalid!";
449 if ((inR ==
nullptr) || !inR->id().isValid() ||
450 (inR->run() != event_aux->run()))
454 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: making RunPrincipal ..." << TLOG_ENDL;
455 outR = pm_.makeRunPrincipal(*run_aux.get());
457 if ((inSR ==
nullptr) || !inSR->id().isValid() ||
458 (inSR->subRun() != event_aux->subRun()))
462 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
463 "making SubRunPrincipal ..." << TLOG_ENDL;
464 outSR = pm_.makeSubRunPrincipal(*subrun_aux.get());
466 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: making EventPrincipal ..." << TLOG_ENDL;
467 outE = pm_.makeEventPrincipal(*event_aux.get(), std::move(history));
469 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
470 "finished processing Event message." << TLOG_ENDL;
474 template <
typename U>
480 unsigned long prd_cnt = 0;
482 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: reading data product count ..." << TLOG_ENDL;
483 msg->ReadULong(prd_cnt);
484 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: product count: " << prd_cnt << TLOG_ENDL;
489 const ProductList& productList = ProductMetaData::instance().productList();
491 for (
unsigned long I = 0; I < prd_cnt; ++I)
493 std::unique_ptr<BranchKey> bk;
496 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: Reading branch key." << TLOG_ENDL;
497 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
498 "ArtdaqInput::readDataProducts"));
501 if (art::debugit() >= 1)
503 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: got product class: '"
504 << bk->friendlyClassName_
508 << bk->productInstanceName_
510 << bk->processName_ << TLOG_ENDL;
512 ProductList::const_iterator iter;
514 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: looking up product ..." << TLOG_ENDL;
515 iter = productList.find(*bk);
516 if (iter == productList.end())
518 throw art::Exception(art::errors::ProductNotFound)
519 <<
"No product is registered for\n"
520 <<
" process name: '"
521 << bk->processName_ <<
"'\n"
522 <<
" module label: '"
523 << bk->moduleLabel_ <<
"'\n"
524 <<
" product friendly class name: '"
525 << bk->friendlyClassName_ <<
"'\n"
526 <<
" product instance name: '"
527 << bk->productInstanceName_ <<
"'\n";
532 const BranchDescription& bd = iter->second;
533 std::unique_ptr<EDProduct> prd;
535 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: Reading product." << TLOG_ENDL;
542 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
544 prd.reset(reinterpret_cast<EDProduct*>(p));
547 std::unique_ptr<const ProductProvenance> prdprov;
549 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: Reading product provenance." << TLOG_ENDL;
550 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
551 "art::ProductProvenance",
552 "ArtdaqInput::readDataProducts"));
555 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: inserting product: class: '"
556 << bd.friendlyClassName()
560 << bd.productInstanceName()
562 << bd.processName() << TLOG_ENDL;
563 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
568 template <
typename U>
571 putInPrincipal(RunPrincipal*& rp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
573 rp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forRun(rp->id()));
576 template <
typename U>
579 putInPrincipal(SubRunPrincipal*& srp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
581 srp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forSubRun(srp->id()));
584 template <
typename U>
587 putInPrincipal(EventPrincipal*& ep, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
589 ep->put(std::move(prd), bd, std::move(prdprov));
593 template <
typename U>
596 readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
597 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
598 art::EventPrincipal*& outE)
600 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::readNext" << TLOG_ENDL;
601 if (outputFileCloseNeeded_)
603 outputFileCloseNeeded_ =
false;
606 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
607 "returning false on outputFileCloseNeeded_" << TLOG_ENDL;
608 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
612 std::unique_ptr<TBufferFile> msg;
613 communicationWrapper_.receiveMessage(msg);
617 shutdownMsgReceived_ =
true;
624 unsigned long msg_type_code = 0;
626 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
627 "getting message type code ..." << TLOG_ENDL;
628 msg->ReadULong(msg_type_code);
629 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
630 "message type: " << msg_type_code << TLOG_ENDL;
632 if (msg_type_code == 5)
635 shutdownMsgReceived_ =
true;
636 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
637 "returning false on Shutdown message." << TLOG_ENDL;
638 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
642 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR,
647 if (msg_type_code == 2)
651 readDataProducts(msg, outR);
653 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
654 "returning false on EndRun message." << TLOG_ENDL;
655 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
658 else if (msg_type_code == 3)
669 if (inR != 0 && inSR != 0 && outR != 0 && outSR != 0)
671 if (inR->id().isFlush() && inSR->id().isFlush() &&
672 outR->id().isFlush() && outSR->id().isFlush())
676 outputFileCloseNeeded_ =
true;
681 readDataProducts(msg, outSR);
684 outputFileCloseNeeded_ =
true;
685 TLOG_ARB(5,
"ArtdaqInput") <<
"readNext: returning true on EndSubRun message." << TLOG_ENDL;
686 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
689 else if (msg_type_code == 4)
692 readDataProducts(msg, outE);
693 TLOG_ARB(5,
"ArtdaqInput") <<
"readNext: returning true on Event message." << TLOG_ENDL;
694 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
699 TLOG_ARB(5,
"ArtdaqInput") <<
"readNext: returning false on unknown msg_type_code!" << TLOG_ENDL;
700 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.