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/ParameterSet.h"
27 #include "fhiclcpp/ParameterSetID.h"
28 #include "fhiclcpp/ParameterSetRegistry.h"
29 #include "fhiclcpp/make_ParameterSet.h"
31 #include <TBufferFile.h>
35 #include "artdaq-core/Data/detail/ParentageMap.hh"
36 #include "artdaq-core/Utilities/ExceptionHandler.hh"
37 #include "artdaq/ArtModules/InputUtilities.hh"
38 #include "artdaq/DAQdata/Globals.hh"
93 ArtdaqInput(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper, art::SourceHelper
const& pm);
104 void readFile(
const std::string&, art::FileBlock*& fb);
121 bool readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
122 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
125 void readAndConstructPrincipal(std::unique_ptr<TBufferFile>&,
unsigned long, art::RunPrincipal*
const,
126 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
127 art::EventPrincipal*&);
130 void readDataProducts(std::unique_ptr<TBufferFile>&, T*&);
132 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
133 std::unique_ptr<const ProductProvenance>&&);
135 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
136 std::unique_ptr<const ProductProvenance>&&);
138 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
139 std::unique_ptr<const ProductProvenance>&&);
142 bool shutdownMsgReceived_;
143 bool outputFileCloseNeeded_;
144 art::SourceHelper
const& pm_;
145 U communicationWrapper_;
148 template <
typename U>
150 art::SourceHelper
const& pm)
151 : shutdownMsgReceived_(false), outputFileCloseNeeded_(false), pm_(pm), communicationWrapper_(ps) {
152 artdaq::configureMessageFacility(
"artdaqart");
160 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::ArtdaqInput("
161 <<
"const fhicl::ParameterSet& ps, "
162 <<
"art::ProductRegistryHelper& helper, "
163 <<
"const art::SourceHelper& pm)";
166 TLOG_ARB(5,
"ArtdaqInput") <<
"Going to receive init message";
167 std::unique_ptr<TBufferFile> msg(
nullptr);
168 communicationWrapper_.receiveInitMessage(msg);
169 TLOG_ARB(5,
"ArtdaqInput") <<
"Init message received";
172 throw art::Exception(art::errors::DataCorruption) <<
"ArtdaqInput: Could not receive init message!";
176 unsigned long dummy = 0;
177 msg->ReadULong(dummy);
182 unsigned long ps_cnt = 0;
183 msg->ReadULong(ps_cnt);
184 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set count: " << ps_cnt;
185 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: reading parameter sets ...";
186 for (
unsigned long I = 0; I < ps_cnt; ++I) {
187 std::string pset_str =
"";
188 msg->ReadStdString(pset_str);
190 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set: " << pset_str;
192 fhicl::ParameterSet pset;
193 fhicl::make_ParameterSet(pset_str, pset);
196 fhicl::ParameterSetRegistry::put(pset);
198 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: finished reading parameter sets.";
203 art::ProductList* productlist = ReadObjectAny<art::ProductList>(
204 msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInput::ArtdaqInput");
205 helper.productList(productlist);
206 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: got product list";
208 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ProcessHistory";
209 art::ProcessHistoryMap* phm = ReadObjectAny<art::ProcessHistoryMap>(
210 msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInput::ArtdaqInput");
213 ProcessHistoryRegistry::put(*phm);
214 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInput's ProcessHistoryRegistry");
219 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ParentageMap";
220 ParentageMap* parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInput::ArtdaqInput");
221 ParentageRegistry::put(*parentageMap);
226 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::ArtdaqInput("
227 <<
"const fhicl::ParameterSet& ps, "
228 <<
"art::ProductRegistryHelper& helper, "
229 <<
"const art::SourceHelper& pm)";
232 template <
typename U>
235 template <
typename U>
237 TLOG_ARB(6,
"ArtdaqInput") <<
"Begin/End: ArtdaqInput::closeCurrentFile()";
240 template <
typename U>
242 TLOG_ARB(7,
"ArtdaqInput") <<
"Begin: ArtdaqInput::"
243 "readFile(const std::string& name, art::FileBlock*& fb)";
244 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInput2013"),
"nothing");
245 TLOG_ARB(7,
"ArtdaqInput") <<
"End: ArtdaqInput::"
246 "readFile(const std::string& name, art::FileBlock*& fb)";
249 template <
typename U>
251 TLOG_ARB(8,
"ArtdaqInput") <<
"Begin: ArtdaqInput::hasMoreData()";
252 if (shutdownMsgReceived_) {
253 TLOG_ARB(8,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
254 "returning false on shutdownMsgReceived_.";
255 TLOG_ARB(8,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()";
258 TLOG_ARB(8,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
259 "returning true on not shutdownMsgReceived_.";
260 TLOG_ARB(8,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()";
264 template <
typename U>
266 art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
267 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
268 art::EventPrincipal*& outE) {
272 std::unique_ptr<art::RunAuxiliary> run_aux;
273 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
274 std::unique_ptr<art::EventAuxiliary> event_aux;
275 std::shared_ptr<History> history;
282 if (msg_type_code == 2) {
284 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
285 <<
"processing EndRun message ...";
287 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
290 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
291 <<
"making flush RunPrincipal ...";
292 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
294 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
295 <<
"making flush SubRunPrincipal ...";
296 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
298 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
299 <<
"making flush EventPrincipal ...";
300 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
302 TLOG_ARB(9,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
303 <<
"finished processing EndRun message.";
304 }
else if (msg_type_code == 3) {
306 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
307 <<
"processing EndSubRun message ...";
310 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
313 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
314 <<
"making flush RunPrincipal ...";
315 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
332 art::Timestamp currentTime = time(0);
333 if (inR !=
nullptr) {
334 inR->setEndTime(currentTime);
336 if (inSR !=
nullptr) {
337 inSR->setEndTime(currentTime);
340 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
341 <<
"making flush SubRunPrincipal ...";
342 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
344 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
345 <<
"making flush EventPrincipal ...";
346 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
348 TLOG_ARB(10,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
349 <<
"finished processing EndSubRun message.";
350 }
else if (msg_type_code == 4) {
352 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
353 <<
"processing Event message ...";
355 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
359 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
363 ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
365 history.reset(ReadObjectAny<art::History>(msg,
"art::History",
"ArtdaqInput::readAndConstructPrincipal"));
369 if (!history->processHistoryID().isValid()) {
370 throw art::Exception(art::errors::Unknown)
371 <<
"readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
374 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
375 <<
"inR: " << (
void*)inR <<
" run/expected "
376 << (inR ? std::to_string(inR->run()) :
"invalid") <<
"/" << event_aux->run();
377 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
378 <<
"inSR: " << (
void*)inSR <<
" run/expected "
379 << (inSR ? std::to_string(inSR->run()) :
"invalid") <<
"/" << event_aux->run()
380 <<
", subrun/expected " << (inSR ? std::to_string(inSR->subRun()) :
"invalid") <<
"/"
381 << event_aux->subRun();
383 if ((inR ==
nullptr) || !inR->id().isValid() || (inR->run() != event_aux->run())) {
386 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: making RunPrincipal ...";
387 outR = pm_.makeRunPrincipal(*run_aux.get());
389 art::SubRunID subrun_check(event_aux->run(), event_aux->subRun());
390 if (inSR == 0 || subrun_check != inSR->id()) {
393 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
394 <<
"making SubRunPrincipal ...";
395 outSR = pm_.makeSubRunPrincipal(*subrun_aux.get());
397 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: making EventPrincipal ...";
398 outE = pm_.makeEventPrincipal(*event_aux.get(), std::move(history));
400 TLOG_ARB(11,
"ArtdaqInput") <<
"readAndConstructPrincipal: "
401 <<
"finished processing Event message.";
405 template <
typename U>
408 unsigned long prd_cnt = 0;
410 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: reading data product count ...";
411 msg->ReadULong(prd_cnt);
412 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: product count: " << prd_cnt;
417 const ProductList& productList = ProductMetaData::instance().productList();
419 for (
unsigned long I = 0; I < prd_cnt; ++I) {
420 std::unique_ptr<BranchKey> bk;
423 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading branch key.";
424 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInput::readDataProducts"));
427 if (art::debugit() >= 1) {
428 TLOG_ARB(13,
"ArtdaqInput") <<
"readDataProducts: got product class: '" << bk->friendlyClassName_ <<
"' modlbl: '"
429 << bk->moduleLabel_ <<
"' instnm: '" << bk->productInstanceName_ <<
"' procnm: '"
432 ProductList::const_iterator iter;
434 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: looking up product ...";
435 iter = productList.find(*bk);
436 if (iter == productList.end()) {
437 throw art::Exception(art::errors::ProductNotFound)
438 <<
"No product is registered for\n"
439 <<
" process name: '" << bk->processName_ <<
"'\n"
440 <<
" module label: '" << bk->moduleLabel_ <<
"'\n"
441 <<
" product friendly class name: '" << bk->friendlyClassName_ <<
"'\n"
442 <<
" product instance name: '" << bk->productInstanceName_ <<
"'\n";
447 const BranchDescription& bd = iter->second;
448 std::unique_ptr<EDProduct> prd;
450 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading product with wrapped name: " << bd.wrappedName()
451 <<
", TClass = " << (
void*)TClass::GetClass(bd.wrappedName().c_str());
458 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
459 auto pp =
reinterpret_cast<EDProduct*
>(p);
461 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: After ReadObjectAny(prd): p=" << p
462 <<
", EDProduct::isPresent: " << pp->isPresent();
466 std::unique_ptr<const ProductProvenance> prdprov;
468 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: Reading product provenance.";
469 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInput::readDataProducts"));
472 TLOG_ARB(12,
"ArtdaqInput") <<
"readDataProducts: inserting product: class: '" << bd.friendlyClassName()
473 <<
"' modlbl: '" << bd.moduleLabel() <<
"' instnm: '" << bd.productInstanceName()
474 <<
"' procnm: '" << bd.processName();
475 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
480 template <
typename U>
482 const BranchDescription& bd,
483 std::unique_ptr<const ProductProvenance>&& prdprov) {
484 rp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forRun(rp->id()));
487 template <
typename U>
489 const BranchDescription& bd,
490 std::unique_ptr<const ProductProvenance>&& prdprov) {
491 srp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forSubRun(srp->id()));
494 template <
typename U>
496 const BranchDescription& bd,
497 std::unique_ptr<const ProductProvenance>&& prdprov) {
498 TLOG_ARB(14,
"ArtdaqInput") <<
"EventPrincipal size before put: " << ep->size();
499 ep->put(std::move(prd), bd, std::move(prdprov));
500 TLOG_ARB(14,
"ArtdaqInput") <<
"EventPrincipal size after put: " << ep->size();
503 template <
typename U>
505 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE) {
506 TLOG_ARB(15,
"ArtdaqInput") <<
"Begin: ArtdaqInput::readNext";
507 if (outputFileCloseNeeded_) {
508 outputFileCloseNeeded_ =
false;
511 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
512 <<
"returning false on outputFileCloseNeeded_";
513 TLOG_ARB(15,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
517 std::unique_ptr<TBufferFile> msg;
518 communicationWrapper_.receiveMessage(msg);
521 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext got an empty message";
522 shutdownMsgReceived_ =
true;
529 unsigned long msg_type_code = 0;
531 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
532 <<
"getting message type code ...";
533 msg->ReadULong(msg_type_code);
534 TLOG_ARB(15,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
535 <<
"message type: " << msg_type_code;
537 if (msg_type_code == 5) {
539 shutdownMsgReceived_ =
true;
540 TLOG_ARB(16,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
541 <<
"returning false on Shutdown message.";
542 TLOG_ARB(16,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
546 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR, outSR, outE);
550 if (msg_type_code == 2) {
553 readDataProducts(msg, outR);
555 TLOG_ARB(17,
"ArtdaqInput") <<
"ArtdaqInput::readNext: "
556 <<
"returning false on EndRun message.";
557 TLOG_ARB(17,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
559 }
else if (msg_type_code == 3) {
569 if (inR != 0 && inSR != 0 && outR != 0 && outSR != 0) {
570 if (inR->id().isFlush() && inSR->id().isFlush() && outR->id().isFlush() && outSR->id().isFlush()) {
573 outputFileCloseNeeded_ =
true;
578 readDataProducts(msg, outSR);
581 outputFileCloseNeeded_ =
true;
582 TLOG_ARB(18,
"ArtdaqInput") <<
"readNext: returning true on EndSubRun message.";
583 TLOG_ARB(18,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
585 }
else if (msg_type_code == 4) {
587 readDataProducts(msg, outE);
588 TLOG_ARB(19,
"ArtdaqInput") <<
"readNext: returning true on Event message.";
589 TLOG_ARB(19,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext";
594 TLOG_ARB(20,
"ArtdaqInput") <<
"readNext: returning false on unknown msg_type_code!";
595 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.