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/DAQdata/Globals.hh"
41 #include "artdaq/ArtModules/InputUtilities.hh"
42 #include "artdaq-core/Data/detail/ParentageMap.hh"
43 #include "artdaq-core/Utilities/ExceptionHandler.hh"
100 ArtdaqInput(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper,
101 const art::SourceHelper& pm);
112 void readFile(
const std::string&, art::FileBlock*& fb);
129 bool readNext(art::RunPrincipal*
const inR,
130 art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
131 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
135 readAndConstructPrincipal(std::unique_ptr<TBufferFile>&,
137 art::RunPrincipal*
const,
138 art::SubRunPrincipal*
const,
140 art::SubRunPrincipal*&,
141 art::EventPrincipal*&);
144 void readDataProducts(std::unique_ptr<TBufferFile>&, T*&);
146 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
148 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
150 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&, std::unique_ptr<const ProductProvenance>&&);
154 bool shutdownMsgReceived_;
155 bool outputFileCloseNeeded_;
156 const art::SourceHelper& pm_;
157 U communicationWrapper_;
160 template <
typename U>
163 art::ProductRegistryHelper& helper,
164 const art::SourceHelper& pm)
165 : shutdownMsgReceived_(false)
166 , outputFileCloseNeeded_(false)
168 , communicationWrapper_(ps)
170 artdaq::configureMessageFacility(
"artdaqart");
178 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::ArtdaqInput(" <<
179 "const fhicl::ParameterSet& ps, " <<
180 "art::ProductRegistryHelper& helper, " <<
181 "const art::SourceHelper& pm)" << TLOG_ENDL;
184 TLOG_ARB(5,
"ArtdaqInput") <<
"Going to receive init message" << TLOG_ENDL;
185 std::unique_ptr<TBufferFile> msg(
nullptr);
186 communicationWrapper_.receiveInitMessage(msg);
187 TLOG_ARB(5,
"ArtdaqInput") <<
"Init message received" << TLOG_ENDL;
191 throw art::Exception(art::errors::DataCorruption) <<
192 "ArtdaqInput: Could not receive init message!";
196 unsigned long dummy = 0;
197 msg->ReadULong(dummy);
202 unsigned long ps_cnt = 0;
203 msg->ReadULong(ps_cnt);
204 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set count: " << ps_cnt << TLOG_ENDL;
205 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: reading parameter sets ..." << TLOG_ENDL;
206 for (
unsigned long I = 0; I < ps_cnt; ++I)
208 std::string pset_str =
"";
209 msg->ReadStdString(pset_str);
211 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: parameter set: " << pset_str << TLOG_ENDL;
213 fhicl::ParameterSet pset;
214 fhicl::make_ParameterSet(pset_str, pset);
217 fhicl::ParameterSetRegistry::put(pset);
219 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: finished reading parameter sets." << TLOG_ENDL;
224 art::ProductList* productlist = ReadObjectAny<art::ProductList>(msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInput::ArtdaqInput");
225 helper.productList(productlist);
227 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: got product list" << TLOG_ENDL;
228 if (art::debugit() >= 1)
230 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: before BranchIDLists" << TLOG_ENDL;
232 #if ART_HEX_VERSION >= 0x20703
233 BranchIDLists
const * bil = &BranchIDListRegistry::instance().data();
235 BranchIDLists* bil = &BranchIDListRegistry::instance()->data();
237 int max_bli = bil->size();
238 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: max_bli: " << max_bli << TLOG_ENDL;
239 for (
int i = 0; i < max_bli; ++i)
241 int max_prdidx = (*bil)[i].size();
242 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: max_prdidx: " << max_prdidx << TLOG_ENDL;
243 for (
int j = 0; j < max_prdidx; ++j)
245 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput:"
252 <<
static_cast<unsigned long>((*bil)[i][j])
253 << std::dec << TLOG_ENDL;
258 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ProcessHistory" << TLOG_ENDL;
259 art::ProcessHistoryMap* phm = ReadObjectAny<art::ProcessHistoryMap>(msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInput::ArtdaqInput");
262 ProcessHistoryRegistry::put(*phm);
263 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInput's ProcessHistoryRegistry");
268 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput: Reading ParentageMap" << TLOG_ENDL;
269 ParentageMap* parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInput::ArtdaqInput");
270 ParentageRegistry::put(*parentageMap);
275 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::ArtdaqInput(" <<
"const fhicl::ParameterSet& ps, " <<
"art::ProductRegistryHelper& helper, " <<
"const art::SourceHelper& pm)" << TLOG_ENDL;
278 template <
typename U>
282 template <
typename U>
287 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin/End: ArtdaqInput::closeCurrentFile()" << TLOG_ENDL;
290 template <
typename U>
295 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::"
296 "readFile(const std::string& name, art::FileBlock*& fb)" << TLOG_ENDL;
297 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInput2013"),
299 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::"
300 "readFile(const std::string& name, art::FileBlock*& fb)" << TLOG_ENDL;
303 template <
typename U>
308 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::hasMoreData()" << TLOG_ENDL;
309 if (shutdownMsgReceived_)
311 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
312 "returning false on shutdownMsgReceived_." << TLOG_ENDL;
313 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()" << TLOG_ENDL;
316 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::hasMoreData(): "
317 "returning true on not shutdownMsgReceived_." << TLOG_ENDL;
318 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::hasMoreData()" << TLOG_ENDL;
322 template <
typename U>
326 unsigned long msg_type_code,
327 art::RunPrincipal*
const inR,
328 art::SubRunPrincipal*
const inSR,
329 art::RunPrincipal*& outR,
330 art::SubRunPrincipal*& outSR,
331 art::EventPrincipal*& outE)
336 std::unique_ptr<art::RunAuxiliary> run_aux;
337 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
338 std::unique_ptr<art::EventAuxiliary> event_aux;
339 std::shared_ptr<History> history;
341 if (msg_type_code == 2)
344 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing EndRun message ..." << TLOG_ENDL;
346 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
349 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush RunPrincipal ..." << TLOG_ENDL;
350 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
352 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush SubRunPrincipal ..." << TLOG_ENDL;
353 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
355 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush EventPrincipal ..." << TLOG_ENDL;
356 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
358 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
359 "finished processing EndRun message." << TLOG_ENDL;
361 else if (msg_type_code == 3)
364 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing EndSubRun message ..." << TLOG_ENDL;
366 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
369 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush RunPrincipal ..." << TLOG_ENDL;
370 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
387 art::Timestamp currentTime = time(0);
388 if (inR !=
nullptr) { inR->setEndTime(currentTime); }
389 if (inSR !=
nullptr) { inSR->setEndTime(currentTime); }
391 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush SubRunPrincipal ..." << TLOG_ENDL;
392 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
394 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making flush EventPrincipal ..." << TLOG_ENDL;
395 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
397 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"finished processing EndSubRun message." << TLOG_ENDL;
399 else if (msg_type_code == 4)
402 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"processing Event message ..." << TLOG_ENDL;
404 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
407 subrun_aux.reset(ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
410 event_aux.reset(ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInput::readAndConstructPrincipal"));
412 history.reset(ReadObjectAny<art::History>(msg,
"art::History",
"ArtdaqInput::readAndConstructPrincipal"));
416 if (!history->processHistoryID().isValid())
418 throw art::Exception(art::errors::Unknown) <<
"readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
421 if ((inR ==
nullptr) || !inR->id().isValid() || (inR->run() != event_aux->run()))
425 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: making RunPrincipal ..." << TLOG_ENDL;
426 outR = pm_.makeRunPrincipal(*run_aux.get());
428 if ((inSR ==
nullptr) || !inSR->id().isValid() || (inSR->subRun() != event_aux->subRun()))
432 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"making SubRunPrincipal ..." << TLOG_ENDL;
433 outSR = pm_.makeSubRunPrincipal(*subrun_aux.get());
435 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: making EventPrincipal ..." << TLOG_ENDL;
436 outE = pm_.makeEventPrincipal(*event_aux.get(), std::move(history));
438 TLOG_ARB(5,
"ArtdaqInput") <<
"readAndConstructPrincipal: " <<
"finished processing Event message." << TLOG_ENDL;
442 template <
typename U>
448 unsigned long prd_cnt = 0;
450 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: reading data product count ..." << TLOG_ENDL;
451 msg->ReadULong(prd_cnt);
452 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: product count: " << prd_cnt << TLOG_ENDL;
457 const ProductList& productList = ProductMetaData::instance().productList();
459 for (
unsigned long I = 0; I < prd_cnt; ++I)
461 std::unique_ptr<BranchKey> bk;
464 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: Reading branch key." << TLOG_ENDL;
465 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInput::readDataProducts"));
468 if (art::debugit() >= 1)
470 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: got product class: '"
471 << bk->friendlyClassName_
475 << bk->productInstanceName_
477 << bk->processName_ << TLOG_ENDL;
479 ProductList::const_iterator iter;
481 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: looking up product ..." << TLOG_ENDL;
482 iter = productList.find(*bk);
483 if (iter == productList.end())
485 throw art::Exception(art::errors::ProductNotFound)
486 <<
"No product is registered for\n"
487 <<
" process name: '"
488 << bk->processName_ <<
"'\n"
489 <<
" module label: '"
490 << bk->moduleLabel_ <<
"'\n"
491 <<
" product friendly class name: '"
492 << bk->friendlyClassName_ <<
"'\n"
493 <<
" product instance name: '"
494 << bk->productInstanceName_ <<
"'\n";
499 const BranchDescription& bd = iter->second;
500 std::unique_ptr<EDProduct> prd;
502 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: Reading product." << TLOG_ENDL;
509 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
511 prd.reset(reinterpret_cast<EDProduct*>(p));
514 std::unique_ptr<const ProductProvenance> prdprov;
516 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: Reading product provenance." << TLOG_ENDL;
517 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInput::readDataProducts"));
520 TLOG_ARB(5,
"ArtdaqInput") <<
"readDataProducts: inserting product: class: '"
521 << bd.friendlyClassName()
525 << bd.productInstanceName()
527 << bd.processName() << TLOG_ENDL;
528 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
533 template <
typename U>
536 putInPrincipal(RunPrincipal*& rp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
538 rp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forRun(rp->id()));
541 template <
typename U>
544 putInPrincipal(SubRunPrincipal*& srp, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
546 srp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forSubRun(srp->id()));
549 template <
typename U>
552 putInPrincipal(EventPrincipal*& ep, std::unique_ptr<EDProduct>&& prd,
const BranchDescription& bd, std::unique_ptr<const ProductProvenance>&& prdprov)
554 ep->put(std::move(prd), bd, std::move(prdprov));
558 template <
typename U>
561 readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
562 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
563 art::EventPrincipal*& outE)
565 TLOG_ARB(5,
"ArtdaqInput") <<
"Begin: ArtdaqInput::readNext" << TLOG_ENDL;
566 if (outputFileCloseNeeded_)
568 outputFileCloseNeeded_ =
false;
571 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on outputFileCloseNeeded_" << TLOG_ENDL;
572 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
576 std::unique_ptr<TBufferFile> msg;
577 communicationWrapper_.receiveMessage(msg);
581 shutdownMsgReceived_ =
true;
588 unsigned long msg_type_code = 0;
590 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"getting message type code ..." << TLOG_ENDL;
591 msg->ReadULong(msg_type_code);
592 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"message type: " << msg_type_code << TLOG_ENDL;
594 if (msg_type_code == 5)
597 shutdownMsgReceived_ =
true;
598 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on Shutdown message." << TLOG_ENDL;
599 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
603 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR,
608 if (msg_type_code == 2)
612 readDataProducts(msg, outR);
614 TLOG_ARB(5,
"ArtdaqInput") <<
"ArtdaqInput::readNext: " <<
"returning false on EndRun message." << TLOG_ENDL;
615 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
618 else if (msg_type_code == 3)
629 if (inR != 0 && inSR != 0 && outR != 0 && outSR != 0)
631 if (inR->id().isFlush() && inSR->id().isFlush() &&
632 outR->id().isFlush() && outSR->id().isFlush())
636 outputFileCloseNeeded_ =
true;
641 readDataProducts(msg, outSR);
644 outputFileCloseNeeded_ =
true;
645 TLOG_ARB(5,
"ArtdaqInput") <<
"readNext: returning true on EndSubRun message." << TLOG_ENDL;
646 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
649 else if (msg_type_code == 4)
652 readDataProducts(msg, outE);
653 TLOG_ARB(5,
"ArtdaqInput") <<
"readNext: returning true on Event message." << TLOG_ENDL;
654 TLOG_ARB(5,
"ArtdaqInput") <<
"End: ArtdaqInput::readNext" << TLOG_ENDL;
659 TLOG_ARB(5,
"ArtdaqInput") <<
"readNext: returning false on unknown msg_type_code!" << TLOG_ENDL;
660 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.