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 #include "art/Persistency/Provenance/MasterProductRegistry.h"
10 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
11 #include "art/Persistency/Provenance/ProductMetaData.h"
13 #include "canvas/Persistency/Common/EDProduct.h"
14 #include "canvas/Persistency/Common/Wrapper.h"
15 #include "canvas/Persistency/Provenance/BranchDescription.h"
16 #include "canvas/Persistency/Provenance/BranchKey.h"
17 #include "canvas/Persistency/Provenance/History.h"
18 #include "canvas/Persistency/Provenance/ParentageRegistry.h"
19 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
20 #include "canvas/Persistency/Provenance/ProcessHistory.h"
21 #include "canvas/Persistency/Provenance/ProcessHistoryID.h"
22 #include "canvas/Persistency/Provenance/ProductList.h"
23 #include "canvas/Persistency/Provenance/ProductProvenance.h"
24 #include "canvas/Utilities/DebugMacros.h"
26 #include "fhiclcpp/make_ParameterSet.h"
27 #include "fhiclcpp/ParameterSet.h"
28 #include "fhiclcpp/ParameterSetID.h"
29 #include "fhiclcpp/ParameterSetRegistry.h"
33 #include <TBufferFile.h>
35 #include "artdaq/DAQdata/Globals.hh"
36 #include "artdaq/ArtModules/InputUtilities.hh"
37 #include "artdaq-core/Data/detail/ParentageMap.hh"
38 #include "artdaq-core/Utilities/ExceptionHandler.hh"
95 ArtdaqInput(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper,
96 art::SourceHelper
const& pm);
107 void readFile(
const std::string&, art::FileBlock*& fb);
124 bool readNext(art::RunPrincipal*
const inR,
125 art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
126 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
130 readAndConstructPrincipal(std::unique_ptr<TBufferFile>&,
132 art::RunPrincipal*
const,
133 art::SubRunPrincipal*
const,
135 art::SubRunPrincipal*&,
136 art::EventPrincipal*&);
139 void readDataProducts(std::unique_ptr<TBufferFile>&, T*&);
141 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
143 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
145 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
149 bool shutdownMsgReceived_;
150 bool outputFileCloseNeeded_;
151 art::SourceHelper
const& pm_;
152 U communicationWrapper_;
155 template <
typename U>
158 art::ProductRegistryHelper& helper,
159 art::SourceHelper
const& pm)
160 : shutdownMsgReceived_(false)
161 , outputFileCloseNeeded_(false)
163 , communicationWrapper_(ps)
165 artdaq::configureMessageFacility(
"artdaqart");
173 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::ArtdaqInput(" <<
174 "const fhicl::ParameterSet& ps, " <<
175 "art::ProductRegistryHelper& helper, " <<
176 "const art::SourceHelper& pm)";
179 TLOG_ARB(5,
"ArtdaqInput") <<
"Going to receive init message";
180 std::unique_ptr<TBufferFile> msg(
nullptr);
181 communicationWrapper_.receiveInitMessage(msg);
182 TLOG_ARB(5,
"ArtdaqInput") <<
"Init message received";
186 throw art::Exception(art::errors::DataCorruption) <<
187 "ArtdaqInput: Could not receive init 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;
200 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: reading parameter sets ...";
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;
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.";
219 art::ProductList* productlist = ReadObjectAny<art::ProductList>(msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInput::ArtdaqInput");
220 helper.productList(productlist);
221 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: got product list";
223 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ProcessHistory";
224 art::ProcessHistoryMap* phm = ReadObjectAny<art::ProcessHistoryMap>(msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInput::ArtdaqInput");
227 ProcessHistoryRegistry::put(*phm);
228 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInput's ProcessHistoryRegistry");
233 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ParentageMap";
234 ParentageMap* parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInput::ArtdaqInput");
235 ParentageRegistry::put(*parentageMap);
240 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::ArtdaqInput(" <<
"const fhicl::ParameterSet& ps, " <<
"art::ProductRegistryHelper& helper, " <<
"const art::SourceHelper& pm)";
243 template <
typename U>
247 template <
typename U>
252 TLOG_ARB(6,
"ArtdaqInput") <<
"Begin/End: ArtdaqInput::closeCurrentFile()";
255 template <
typename U>
260 TLOG_ARB(7,
"ArtdaqInput") <<
"Begin: ArtdaqInput::"
261 "readFile(const std::string& name, art::FileBlock*& fb)";
262 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInput2013"),
264 TLOG_ARB(7,
"ArtdaqInput") <<
"End: ArtdaqInput::"
265 "readFile(const std::string& name, art::FileBlock*& fb)";
268 template <
typename U>
273 TLOG_ARB(8,
"ArtdaqInput") <<
"Begin: ArtdaqInput::hasMoreData()";
274 if (shutdownMsgReceived_)
276 TLOG_ARB(8,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
277 "returning false on shutdownMsgReceived_.";
278 TLOG_ARB(8,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()";
281 TLOG_ARB(8,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
282 "returning true on not shutdownMsgReceived_.";
283 TLOG_ARB(8,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()";
287 template <
typename U>
291 unsigned long msg_type_code,
292 art::RunPrincipal*
const inR,
293 art::SubRunPrincipal*
const inSR,
294 art::RunPrincipal*& outR,
295 art::SubRunPrincipal*& outSR,
296 art::EventPrincipal*& outE)
301 std::unique_ptr<art::RunAuxiliary> run_aux;
302 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
303 std::unique_ptr<art::EventAuxiliary> event_aux;
304 std::shared_ptr<History> history;
306 if (msg_type_code == 2)
309 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing EndRun message ...";
311 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
314 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush RunPrincipal ...";
315 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
317 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush SubRunPrincipal ...";
318 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
320 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush EventPrincipal ...";
321 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
323 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
324 "finished processing EndRun message.";
326 else if (msg_type_code == 3)
329 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing EndSubRun message ...";
331 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
334 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush RunPrincipal ...";
335 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
352 art::Timestamp currentTime = time(0);
353 if (inR !=
nullptr) { inR->setEndTime(currentTime); }
354 if (inSR !=
nullptr) { inSR->setEndTime(currentTime); }
356 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush SubRunPrincipal ...";
357 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
359 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush EventPrincipal ...";
360 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
362 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"finished processing EndSubRun message.";
364 else if (msg_type_code == 4)
367 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing Event message ...";
369 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
372 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
375 event_aux.reset(ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
377 history.reset(ReadObjectAny<art::History>(msg,
"art::History",
"ArtdaqInput::readAndConstructPrincipal"));
381 if (!history->processHistoryID().isValid())
383 throw art::Exception(art::errors::Unknown) <<
"readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
386 if ((inR ==
nullptr) || !inR->id().isValid() || (inR->run() != event_aux->run()))
390 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: making RunPrincipal ...";
391 outR = pm_.makeRunPrincipal(*run_aux.get());
393 if ((inSR ==
nullptr) || !inSR->id().isValid() || (inSR->subRun() != event_aux->subRun()))
397 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making SubRunPrincipal ...";
398 outSR = pm_.makeSubRunPrincipal(*subrun_aux.get());
400 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: making EventPrincipal ...";
401 outE = pm_.makeEventPrincipal(*event_aux.get(), std::move(history));
403 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"finished processing Event message.";
407 template <
typename U>
413 unsigned long prd_cnt = 0;
415 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: reading data product count ...";
416 msg->ReadULong(prd_cnt);
417 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: product count: " << prd_cnt;
422 const ProductList& productList = ProductMetaData::instance().productList();
424 for (
unsigned long I = 0; I < prd_cnt; ++I)
426 std::unique_ptr<BranchKey> bk;
429 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading branch key.";
430 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInput::readDataProducts"));
433 if (art::debugit() >= 1)
435 TLOG_ARB(13,
"ArtdaqInput") <<
"readDataProducts: got product class: '"
436 << bk->friendlyClassName_
440 << bk->productInstanceName_
444 ProductList::const_iterator iter;
446 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: looking up product ...";
447 iter = productList.find(*bk);
448 if (iter == productList.end())
450 throw art::Exception(art::errors::ProductNotFound)
451 <<
"No product is registered for\n"
452 <<
" process name: '"
453 << bk->processName_ <<
"'\n"
454 <<
" module label: '"
455 << bk->moduleLabel_ <<
"'\n"
456 <<
" product friendly class name: '"
457 << bk->friendlyClassName_ <<
"'\n"
458 <<
" product instance name: '"
459 << bk->productInstanceName_ <<
"'\n";
464 const BranchDescription& bd = iter->second;
465 std::unique_ptr<EDProduct> prd;
467 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading product with wrapped name: " << bd.wrappedName()
468 <<
", TClass = " << (
void*)TClass::GetClass(bd.wrappedName().c_str());
475 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
476 auto pp =
reinterpret_cast<EDProduct*
>(p);
478 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: After ReadObjectAny(prd): p=" << p
479 <<
", EDProduct::isPresent: " << pp->isPresent();
483 std::unique_ptr<const ProductProvenance> prdprov;
485 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading product provenance.";
486 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInput::readDataProducts"));
489 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: inserting product: class: '"
490 << bd.friendlyClassName()
494 << bd.productInstanceName()
497 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
503 template <
typename U>
506 putInPrincipal(RunPrincipal*& rp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
508 rp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forRun(rp->id()));
511 template <
typename U>
514 putInPrincipal(SubRunPrincipal*& srp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
516 srp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forSubRun(srp->id()));
519 template <
typename U>
522 putInPrincipal(EventPrincipal*& ep, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
524 TLOG_ARB(14,
"ArtdaqInput") <<
"EventPrincipal size before put: " << ep->size();
525 ep->put(std::move(prd), bd, std::move(prdprov));
526 TLOG_ARB(14,
"ArtdaqInput") <<
"EventPrincipal size after put: " << ep->size();
530 template <
typename U>
533 readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
534 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
535 art::EventPrincipal*& outE)
537 TLOG_ARB(15,
"ArtdaqInput") <<
"Begin: ArtdaqInput::readNext";
538 if (outputFileCloseNeeded_)
540 outputFileCloseNeeded_ =
false;
543 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on outputFileCloseNeeded_";
544 TLOG_ARB(15,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
548 std::unique_ptr<TBufferFile> msg;
549 communicationWrapper_.receiveMessage(msg);
553 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext got an empty message";
554 shutdownMsgReceived_ =
true;
561 unsigned long msg_type_code = 0;
563 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"getting message type code ...";
564 msg->ReadULong(msg_type_code);
565 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"message type: " << msg_type_code;
567 if (msg_type_code == 5)
570 shutdownMsgReceived_ =
true;
571 TLOG_ARB(16,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on Shutdown message.";
572 TLOG_ARB(16,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
576 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR,
581 if (msg_type_code == 2)
585 readDataProducts(msg, outR);
587 TLOG_ARB(17,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on EndRun message.";
588 TLOG_ARB(17,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
591 else if (msg_type_code == 3)
602 if (inR != 0 && inSR != 0 && outR != 0 && outSR != 0)
604 if (inR->id().isFlush() && inSR->id().isFlush() &&
605 outR->id().isFlush() && outSR->id().isFlush())
609 outputFileCloseNeeded_ =
true;
614 readDataProducts(msg, outSR);
617 outputFileCloseNeeded_ =
true;
618 TLOG_ARB(18,
"ArtdaqInput") <<
"readNext: returning true on EndSubRun message.";
619 TLOG_ARB(18,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
622 else if (msg_type_code == 4)
625 readDataProducts(msg, outE);
626 TLOG_ARB(19,
"ArtdaqInput") <<
"readNext: returning true on Event message.";
627 TLOG_ARB(19,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
632 TLOG_ARB(20,
"ArtdaqInput") <<
"readNext: returning false on unknown msg_type_code!";
633 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.