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 < 0x20900
10 #include "art/Persistency/Provenance/BranchIDListRegistry.h"
11 #include "canvas/Persistency/Provenance/BranchIDList.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/BranchKey.h"
21 #include "canvas/Persistency/Provenance/History.h"
22 #include "canvas/Persistency/Provenance/ParentageRegistry.h"
23 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
24 #include "canvas/Persistency/Provenance/ProcessHistory.h"
25 #include "canvas/Persistency/Provenance/ProcessHistoryID.h"
26 #include "canvas/Persistency/Provenance/ProductList.h"
27 #include "canvas/Persistency/Provenance/ProductProvenance.h"
28 #include "canvas/Utilities/DebugMacros.h"
30 #include "fhiclcpp/make_ParameterSet.h"
31 #include "fhiclcpp/ParameterSet.h"
32 #include "fhiclcpp/ParameterSetID.h"
33 #include "fhiclcpp/ParameterSetRegistry.h"
37 #include <TBufferFile.h>
39 #include "artdaq/DAQdata/Globals.hh"
40 #include "artdaq/ArtModules/InputUtilities.hh"
41 #include "artdaq-core/Data/detail/ParentageMap.hh"
42 #include "artdaq-core/Utilities/ExceptionHandler.hh"
99 ArtdaqInput(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper,
100 art::SourceHelper
const& 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 art::SourceHelper
const& pm_;
156 U communicationWrapper_;
159 template <
typename U>
162 art::ProductRegistryHelper& helper,
163 art::SourceHelper
const& pm)
164 : shutdownMsgReceived_(false)
165 , outputFileCloseNeeded_(false)
167 , communicationWrapper_(ps)
169 artdaq::configureMessageFacility(
"artdaqart");
177 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::ArtdaqInput(" <<
178 "const fhicl::ParameterSet& ps, " <<
179 "art::ProductRegistryHelper& helper, " <<
180 "const art::SourceHelper& pm)";
183 TLOG_ARB(5,
"ArtdaqInput") <<
"Going to receive init message";
184 std::unique_ptr<TBufferFile> msg(
nullptr);
185 communicationWrapper_.receiveInitMessage(msg);
186 TLOG_ARB(5,
"ArtdaqInput") <<
"Init message received";
190 throw art::Exception(art::errors::DataCorruption) <<
191 "ArtdaqInput: Could not receive init message!";
195 unsigned long dummy = 0;
196 msg->ReadULong(dummy);
201 unsigned long ps_cnt = 0;
202 msg->ReadULong(ps_cnt);
203 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set count: " << ps_cnt;
204 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: reading parameter sets ...";
205 for (
unsigned long I = 0; I < ps_cnt; ++I)
207 std::string pset_str =
"";
208 msg->ReadStdString(pset_str);
210 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set: " << pset_str;
212 fhicl::ParameterSet pset;
213 fhicl::make_ParameterSet(pset_str, pset);
216 fhicl::ParameterSetRegistry::put(pset);
218 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: finished reading parameter sets.";
223 art::ProductList* productlist = ReadObjectAny<art::ProductList>(msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInput::ArtdaqInput");
224 helper.productList(productlist);
225 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: got product list";
227 #if ART_HEX_VERSION < 0x20900
228 if (art::debugit() >= 1)
230 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: before BranchIDLists";
232 BranchIDLists
const * bil = &BranchIDListRegistry::instance().data();
233 int max_bli = bil->size();
234 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: max_bli: " << max_bli;
235 for (
int i = 0; i < max_bli; ++i)
237 int max_prdidx = (*bil)[i].size();
238 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: max_prdidx: " << max_prdidx;
239 for (
int j = 0; j < max_prdidx; ++j)
241 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput:"
248 <<
static_cast<unsigned long>((*bil)[i][j])
257 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ProcessHistory";
258 art::ProcessHistoryMap* phm = ReadObjectAny<art::ProcessHistoryMap>(msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInput::ArtdaqInput");
261 ProcessHistoryRegistry::put(*phm);
262 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInput's ProcessHistoryRegistry");
267 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ParentageMap";
268 ParentageMap* parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInput::ArtdaqInput");
269 ParentageRegistry::put(*parentageMap);
274 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::ArtdaqInput(" <<
"const fhicl::ParameterSet& ps, " <<
"art::ProductRegistryHelper& helper, " <<
"const art::SourceHelper& pm)";
277 template <
typename U>
281 template <
typename U>
286 TLOG_ARB(6,
"ArtdaqInput") <<
"Begin/End: ArtdaqInput::closeCurrentFile()";
289 template <
typename U>
294 TLOG_ARB(7,
"ArtdaqInput") <<
"Begin: ArtdaqInput::"
295 "readFile(const std::string& name, art::FileBlock*& fb)";
296 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInput2013"),
298 TLOG_ARB(7,
"ArtdaqInput") <<
"End: ArtdaqInput::"
299 "readFile(const std::string& name, art::FileBlock*& fb)";
302 template <
typename U>
307 TLOG_ARB(8,
"ArtdaqInput") <<
"Begin: ArtdaqInput::hasMoreData()";
308 if (shutdownMsgReceived_)
310 TLOG_ARB(8,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
311 "returning false on shutdownMsgReceived_.";
312 TLOG_ARB(8,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()";
315 TLOG_ARB(8,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
316 "returning true on not shutdownMsgReceived_.";
317 TLOG_ARB(8,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()";
321 template <
typename U>
325 unsigned long msg_type_code,
326 art::RunPrincipal*
const inR,
327 art::SubRunPrincipal*
const inSR,
328 art::RunPrincipal*& outR,
329 art::SubRunPrincipal*& outSR,
330 art::EventPrincipal*& outE)
335 std::unique_ptr<art::RunAuxiliary> run_aux;
336 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
337 std::unique_ptr<art::EventAuxiliary> event_aux;
338 std::shared_ptr<History> history;
340 if (msg_type_code == 2)
343 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing EndRun message ...";
345 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
348 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush RunPrincipal ...";
349 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
351 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush SubRunPrincipal ...";
352 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
354 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush EventPrincipal ...";
355 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
357 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
358 "finished processing EndRun message.";
360 else if (msg_type_code == 3)
363 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing EndSubRun message ...";
365 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
368 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush RunPrincipal ...";
369 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
386 art::Timestamp currentTime = time(0);
387 if (inR !=
nullptr) { inR->setEndTime(currentTime); }
388 if (inSR !=
nullptr) { inSR->setEndTime(currentTime); }
390 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush SubRunPrincipal ...";
391 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
393 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush EventPrincipal ...";
394 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
396 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"finished processing EndSubRun message.";
398 else if (msg_type_code == 4)
401 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing Event message ...";
403 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
406 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
409 event_aux.reset(ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
411 history.reset(ReadObjectAny<art::History>(msg,
"art::History",
"ArtdaqInput::readAndConstructPrincipal"));
415 if (!history->processHistoryID().isValid())
417 throw art::Exception(art::errors::Unknown) <<
"readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
420 if ((inR ==
nullptr) || !inR->id().isValid() || (inR->run() != event_aux->run()))
424 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: making RunPrincipal ...";
425 outR = pm_.makeRunPrincipal(*run_aux.get());
427 if ((inSR ==
nullptr) || !inSR->id().isValid() || (inSR->subRun() != event_aux->subRun()))
431 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making SubRunPrincipal ...";
432 outSR = pm_.makeSubRunPrincipal(*subrun_aux.get());
434 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: making EventPrincipal ...";
435 outE = pm_.makeEventPrincipal(*event_aux.get(), std::move(history));
437 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"finished processing Event message.";
441 template <
typename U>
447 unsigned long prd_cnt = 0;
449 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: reading data product count ...";
450 msg->ReadULong(prd_cnt);
451 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: product count: " << prd_cnt;
456 const ProductList& productList = ProductMetaData::instance().productList();
458 for (
unsigned long I = 0; I < prd_cnt; ++I)
460 std::unique_ptr<BranchKey> bk;
463 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading branch key.";
464 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInput::readDataProducts"));
467 if (art::debugit() >= 1)
469 TLOG_ARB(13,
"ArtdaqInput") <<
"readDataProducts: got product class: '"
470 << bk->friendlyClassName_
474 << bk->productInstanceName_
478 ProductList::const_iterator iter;
480 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: looking up product ...";
481 iter = productList.find(*bk);
482 if (iter == productList.end())
484 throw art::Exception(art::errors::ProductNotFound)
485 <<
"No product is registered for\n"
486 <<
" process name: '"
487 << bk->processName_ <<
"'\n"
488 <<
" module label: '"
489 << bk->moduleLabel_ <<
"'\n"
490 <<
" product friendly class name: '"
491 << bk->friendlyClassName_ <<
"'\n"
492 <<
" product instance name: '"
493 << bk->productInstanceName_ <<
"'\n";
498 const BranchDescription& bd = iter->second;
499 std::unique_ptr<EDProduct> prd;
501 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading product with wrapped name: " << bd.wrappedName()
502 <<
", TClass = " << (
void*)TClass::GetClass(bd.wrappedName().c_str());
509 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
510 auto pp =
reinterpret_cast<EDProduct*
>(p);
512 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: After ReadObjectAny(prd): p=" << p
513 <<
", EDProduct::isPresent: " << pp->isPresent();
517 std::unique_ptr<const ProductProvenance> prdprov;
519 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading product provenance.";
520 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInput::readDataProducts"));
523 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: inserting product: class: '"
524 << bd.friendlyClassName()
528 << bd.productInstanceName()
531 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
537 template <
typename U>
540 putInPrincipal(RunPrincipal*& rp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
542 rp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forRun(rp->id()));
545 template <
typename U>
548 putInPrincipal(SubRunPrincipal*& srp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
550 srp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forSubRun(srp->id()));
553 template <
typename U>
556 putInPrincipal(EventPrincipal*& ep, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
558 TLOG_ARB(14,
"ArtdaqInput") <<
"EventPrincipal size before put: " << ep->size();
559 ep->put(std::move(prd), bd, std::move(prdprov));
560 TLOG_ARB(14,
"ArtdaqInput") <<
"EventPrincipal size after put: " << ep->size();
564 template <
typename U>
567 readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
568 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
569 art::EventPrincipal*& outE)
571 TLOG_ARB(15,
"ArtdaqInput") <<
"Begin: ArtdaqInput::readNext";
572 if (outputFileCloseNeeded_)
574 outputFileCloseNeeded_ =
false;
577 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on outputFileCloseNeeded_";
578 TLOG_ARB(15,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
582 std::unique_ptr<TBufferFile> msg;
583 communicationWrapper_.receiveMessage(msg);
587 shutdownMsgReceived_ =
true;
594 unsigned long msg_type_code = 0;
596 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"getting message type code ...";
597 msg->ReadULong(msg_type_code);
598 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"message type: " << msg_type_code;
600 if (msg_type_code == 5)
603 shutdownMsgReceived_ =
true;
604 TLOG_ARB(16,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on Shutdown message.";
605 TLOG_ARB(16,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
609 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR,
614 if (msg_type_code == 2)
618 readDataProducts(msg, outR);
620 TLOG_ARB(17,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on EndRun message.";
621 TLOG_ARB(17,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
624 else if (msg_type_code == 3)
635 if (inR != 0 && inSR != 0 && outR != 0 && outSR != 0)
637 if (inR->id().isFlush() && inSR->id().isFlush() &&
638 outR->id().isFlush() && outSR->id().isFlush())
642 outputFileCloseNeeded_ =
true;
647 readDataProducts(msg, outSR);
650 outputFileCloseNeeded_ =
true;
651 TLOG_ARB(18,
"ArtdaqInput") <<
"readNext: returning true on EndSubRun message.";
652 TLOG_ARB(18,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
655 else if (msg_type_code == 4)
658 readDataProducts(msg, outE);
659 TLOG_ARB(19,
"ArtdaqInput") <<
"readNext: returning true on Event message.";
660 TLOG_ARB(19,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
665 TLOG_ARB(20,
"ArtdaqInput") <<
"readNext: returning false on unknown msg_type_code!";
666 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.