1 #ifndef ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
2 #define ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
4 #include "art/Framework/Core/Frameworkfwd.h"
6 #include "art/Framework/Core/FileBlock.h"
7 #include "art/Framework/Core/InputSourceMacros.h"
8 #include "art/Framework/Core/ProductRegistryHelper.h"
9 #include "art/Framework/IO/Sources/Source.h"
10 #include "art/Framework/IO/Sources/SourceHelper.h"
11 #include "art/Framework/IO/Sources/SourceTraits.h"
12 #include "art/Framework/IO/Sources/put_product_in_principal.h"
13 #include "art/Framework/Principal/EventPrincipal.h"
14 #include "art/Framework/Principal/RunPrincipal.h"
15 #include "art/Framework/Principal/SubRunPrincipal.h"
16 #include "art/Framework/Services/Registry/ServiceHandle.h"
18 #if ART_HEX_VERSION < 0x30000
19 #include "art/Persistency/Provenance/MasterProductRegistry.h"
20 #include "art/Persistency/Provenance/ProductMetaData.h"
22 #if ART_HEX_VERSION >= 0x30200
23 #include "art_root_io/setup.h"
25 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
27 #include "canvas/Persistency/Common/EDProduct.h"
28 #include "canvas/Persistency/Common/Wrapper.h"
29 #include "canvas/Persistency/Provenance/BranchDescription.h"
30 #include "canvas/Persistency/Provenance/BranchKey.h"
31 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
32 #include "canvas/Persistency/Provenance/History.h"
33 #include "canvas/Persistency/Provenance/ParentageRegistry.h"
34 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
35 #include "canvas/Persistency/Provenance/ProcessHistory.h"
36 #include "canvas/Persistency/Provenance/ProcessHistoryID.h"
37 #include "canvas/Persistency/Provenance/ProductList.h"
38 #include "canvas/Persistency/Provenance/ProductProvenance.h"
39 #include "canvas/Persistency/Provenance/ProductTables.h"
40 #include "canvas/Utilities/DebugMacros.h"
42 #include "fhiclcpp/ParameterSet.h"
43 #include "fhiclcpp/ParameterSetID.h"
44 #include "fhiclcpp/ParameterSetRegistry.h"
45 #include "fhiclcpp/make_ParameterSet.h"
47 #include <TBufferFile.h>
50 #include <TStreamerInfo.h>
52 #include "artdaq-core/Data/ContainerFragment.hh"
53 #include "artdaq-core/Data/Fragment.hh"
54 #include "artdaq-core/Data/detail/ParentageMap.hh"
55 #include "artdaq-core/Utilities/ExceptionHandler.hh"
56 #include "artdaq-core/Utilities/TimeUtils.hh"
57 #include "artdaq/ArtModules/ArtdaqFragmentNamingService.h"
58 #include "artdaq/ArtModules/ArtdaqSharedMemoryService.h"
59 #include "artdaq/ArtModules/InputUtilities.hh"
60 #include "artdaq/DAQdata/Globals.hh"
61 #include "artdaq/DAQdata/NetMonHeader.hh"
73 #include <unordered_map>
77 #if ART_HEX_VERSION < 0x30000
81 #define HISTORY_PTR_T std::shared_ptr<art::History>
83 #define EVENT_ID eventID
84 #define SUBRUN_ID subRunID
86 #define HISTORY_PTR_T std::unique_ptr<art::History>
134 ArtdaqInputHelper(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper, art::SourceHelper
const& pm);
145 void readFile(
const std::string&, art::FileBlock*& fb);
162 bool readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
163 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
169 void readAndConstructPrincipal(std::unique_ptr<TBufferFile>&, ULong_t, art::RunPrincipal*
const,
170 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
171 art::EventPrincipal*&);
173 bool constructPrincipal(artdaq::Fragment::type_t, art::RunPrincipal*
const,
174 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
175 art::EventPrincipal*&);
178 void readDataProducts(std::list<std::unique_ptr<TBufferFile>>&, T*&);
180 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
181 std::unique_ptr<const ProductProvenance>&&);
183 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
184 std::unique_ptr<const ProductProvenance>&&);
186 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
187 std::unique_ptr<const ProductProvenance>&&);
189 void readFragments(std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>>
const& eventMap, art::EventPrincipal*& outE);
191 bool shutdownMsgReceived_;
192 bool outputFileCloseNeeded_;
193 art::SourceHelper
const& pm_;
194 U communicationWrapper_;
195 ProductList* productList_;
196 HISTORY_PTR_T history_to_use_;
197 bool fragmentsOnlyMode_;
198 std::string pretend_module_name;
200 std::chrono::steady_clock::time_point last_read_time;
205 art::SourceHelper
const& pm)
206 : shutdownMsgReceived_(false)
207 , outputFileCloseNeeded_(false)
209 , communicationWrapper_(ps)
211 , fragmentsOnlyMode_(false)
212 , pretend_module_name(ps.get<std::string>(
"raw_data_label",
"daq"))
214 , last_read_time(std::chrono::steady_clock::now())
216 #if ART_HEX_VERSION >= 0x30200
220 art::ServiceHandle<ArtdaqSharedMemoryServiceInterface> shm;
223 volatile bool loop =
true;
236 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::ArtdaqInputHelper("
237 <<
"const fhicl::ParameterSet& ps, "
238 <<
"art::ProductRegistryHelper& helper, "
239 <<
"const art::SourceHelper& pm)";
241 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"Going to receive init message";
242 artdaq::FragmentPtrs initFrags = communicationWrapper_.receiveInitMessage();
243 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"Init message received";
245 if (!initFrags.empty() && initFrags.front().get()->type() == artdaq::Fragment::EndOfDataFragmentType)
247 TLOG_ERROR(
"ArtdaqInputHelper") <<
"Received EndOfData as first broadcast! This process neveer received any data!";
248 shutdownMsgReceived_ =
true;
249 outputFileCloseNeeded_ =
true;
253 if (initFrags.empty() || initFrags.back().get()->dataSize() == 0)
255 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"No init message received or zero-size init message: Fragments-only mode activated! This is an EventBuilder!";
256 fragmentsOnlyMode_ =
true;
260 std::list<std::unique_ptr<TBufferFile>> msgs;
261 for (
auto& initFrag : initFrags)
264 msgs.emplace_back(
new TBufferFile(TBuffer::kRead, header->data_length, initFrag->dataBegin(), kFALSE,
nullptr));
267 std::list<History*> processHistories;
269 for (
auto& msg : msgs)
273 msg->ReadULong(dummy);
276 auto list =
dynamic_cast<TList*
>(msg->ReadObject(TList::Class()));
280 TObjLink* lnk = list->FirstLink();
284 info =
dynamic_cast<TStreamerInfo*
>(lnk->GetObject());
285 TObject* element = info->GetElements()->UncheckedAt(0);
286 Bool_t isstl = element && strcmp(
"This", element->GetName()) == 0;
290 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() <<
", version = " << info->GetClassVersion();
295 lnk = list->FirstLink();
298 info =
dynamic_cast<TStreamerInfo*
>(lnk->GetObject());
299 TObject* element = info->GetElements()->UncheckedAt(0);
300 Bool_t isstl = element && strcmp(
"This", element->GetName()) == 0;
304 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() <<
", version = " << info->GetClassVersion();
314 msg->ReadULong(ps_cnt);
315 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: parameter set count: " << ps_cnt;
316 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: reading parameter sets ...";
317 for (ULong_t I = 0; I < ps_cnt; ++I)
319 std::string pset_str =
"";
320 msg->ReadStdString(pset_str);
322 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: parameter set: " << pset_str;
324 fhicl::ParameterSet pset;
325 fhicl::make_ParameterSet(pset_str, pset);
328 fhicl::ParameterSetRegistry::put(pset);
330 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: finished reading parameter sets.";
335 auto thisProductList = ReadObjectAny<art::ProductList>(
336 msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInputHelper::ArtdaqInputHelper");
337 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Input Product list sz=" << thisProductList->size();
339 bool productListInitialized = productList_ !=
nullptr;
340 if (!productListInitialized) productList_ = thisProductList;
341 for (
auto I = thisProductList->begin(), E = thisProductList->end(); I != E; ++I)
344 TLOG_ARB(50,
"ArtdaqInputHelper") <<
"Branch key: class: '" << I->first.friendlyClassName_ <<
"' modlbl: '"
345 << I->first.moduleLabel_ <<
"' instnm: '" << I->first.productInstanceName_ <<
"' procnm: '"
346 << I->first.processName_ <<
"', branch description name: " << I->second.wrappedName()
347 <<
", TClass = " <<
static_cast<void*
>(TClass::GetClass(I->second.wrappedName().c_str()));
349 if (productListInitialized)
351 productList_->emplace(*I);
355 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading ProcessHistory";
356 auto phm = ReadObjectAny<art::ProcessHistoryMap>(
357 msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInputHelper::ArtdaqInputHelper");
360 ProcessHistoryRegistry::put(*phm);
361 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInputHelper's ProcessHistoryRegistry");
366 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading ParentageMap";
367 auto parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInputHelper::ArtdaqInputHelper");
368 ParentageRegistry::put(*parentageMap);
373 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading History";
374 processHistories.push_back(ReadObjectAny<History>(msg,
"art::History",
"ArtdaqInputHelper::ArtdaqInputHelper"));
378 art::ProcessHistory fake_process_history;
379 for (
auto& hist : processHistories)
381 auto const&
id = hist->processHistoryID();
382 ProcessHistory thisProcessHistory;
383 if (ProcessHistoryRegistry::get(
id, thisProcessHistory))
385 for (
auto& conf : thisProcessHistory)
386 fake_process_history.push_back(conf);
389 art::ProcessHistoryMap fake_process_history_map;
390 fake_process_history_map[fake_process_history.id()] = fake_process_history;
391 ProcessHistoryRegistry::put(fake_process_history_map);
392 history_to_use_.reset(
new History());
393 history_to_use_->setProcessHistoryID(fake_process_history.id());
394 for (
auto& hist : processHistories)
396 for (
auto& es : hist->eventSelectionIDs())
398 history_to_use_->addEventSelectionEntry(es);
402 TLOG_ARB(5,
"ArtdaqInputHelper")
403 <<
"ArtdaqInputHelper: Product list sz=" << productList_->size();
406 #if ART_HEX_VERSION < 0x30000
407 helper.productList(productList_);
409 helper.productList(std::unique_ptr<art::ProductList>(productList_));
411 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: got product list";
414 helper.reconstitutes<artdaq::detail::RawEventHeader, art::InEvent>(pretend_module_name,
"RawEventHeader");
416 if (ps.get<
bool>(
"register_fragment_types",
true))
418 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"Registering known Fragment labels from ArtdaqFragmentNamingServiceInterface";
420 art::ServiceHandle<ArtdaqFragmentNamingServiceInterface> translator;
421 helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, translator->GetUnidentifiedInstanceName());
423 helper.reconstitutes<artdaq::Fragments, art::InRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
424 helper.reconstitutes<artdaq::Fragments, art::InSubRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
426 std::set<std::string> instance_names = translator->GetAllProductInstanceNames();
427 for (
const auto& set_iter : instance_names)
429 helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, set_iter);
435 TLOG_ARB(5,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::ArtdaqInputHelper("
436 <<
"const fhicl::ParameterSet& ps, "
437 <<
"art::ProductRegistryHelper& helper, "
438 <<
"const art::SourceHelper& pm)";
449 TLOG_ARB(6,
"ArtdaqInputHelper") <<
"Begin/End: ArtdaqInputHelper::closeCurrentFile()";
455 TLOG_ARB(7,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::"
456 "readFile(const std::string& name, art::FileBlock*& fb)";
457 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInputHelper2013"),
"nothing");
458 TLOG_ARB(7,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::"
459 "readFile(const std::string& name, art::FileBlock*& fb)";
465 TLOG_ARB(8,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::hasMoreData()";
466 if (shutdownMsgReceived_)
468 TLOG_ARB(8,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::hasMoreData(): "
469 "returning false on shutdownMsgReceived_.";
470 TLOG_ARB(8,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::hasMoreData()";
473 TLOG_ARB(8,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::hasMoreData(): "
474 "returning true on not shutdownMsgReceived_.";
475 TLOG_ARB(8,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::hasMoreData()";
481 art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
482 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
483 art::EventPrincipal*& outE)
488 std::unique_ptr<art::RunAuxiliary> run_aux;
489 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
490 std::unique_ptr<art::EventAuxiliary> event_aux;
492 HISTORY_PTR_T history_from_event;
499 if (msg_type_code == 2)
502 TLOG_ARB(9,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
503 <<
"processing EndRun message ...";
505 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
508 TLOG_ARB(9,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
509 <<
"making flush RunPrincipal ...";
510 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
512 TLOG_ARB(9,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
513 <<
"making flush SubRunPrincipal ...";
514 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
516 TLOG_ARB(9,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
517 <<
"making flush EventPrincipal ...";
518 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
520 TLOG_ARB(9,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
521 <<
"finished processing EndRun message.";
523 else if (msg_type_code == 3)
526 TLOG_ARB(10,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
527 <<
"processing EndSubRun message ...";
530 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
533 TLOG_ARB(10,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
534 <<
"making flush RunPrincipal ...";
535 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
552 art::Timestamp currentTime = time(
nullptr);
555 #if ART_HEX_VERSION < 0x30000
556 inR->setEndTime(currentTime);
558 inR->endTime(currentTime);
563 #if ART_HEX_VERSION < 0x30000
564 inSR->setEndTime(currentTime);
566 inSR->endTime(currentTime);
570 TLOG_ARB(10,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
571 <<
"making flush SubRunPrincipal ...";
572 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
574 TLOG_ARB(10,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
575 <<
"making flush EventPrincipal ...";
576 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
578 TLOG_ARB(10,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
579 <<
"finished processing EndSubRun message.";
581 else if (msg_type_code == 4)
584 TLOG_ARB(11,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
585 <<
"processing Event message ...";
587 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
591 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
595 ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
597 history_from_event.reset(ReadObjectAny<art::History>(msg,
"art::History",
"ArtdaqInputHelper::readAndConstructPrincipal"));
601 if (!history_from_event->processHistoryID().isValid())
603 throw art::Exception(art::errors::Unknown)
604 <<
"readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
607 TLOG_ARB(11,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
608 <<
"inR: " <<
static_cast<void*
>(inR) <<
" run/expected "
609 << (inR ? std::to_string(inR->run()) :
"invalid") <<
"/" << event_aux->run();
610 TLOG_ARB(11,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
611 <<
"inSR: " <<
static_cast<void*
>(inSR) <<
" run/expected "
612 << (inSR ? std::to_string(inSR->run()) :
"invalid") <<
"/" << event_aux->run()
613 <<
", subrun/expected " << (inSR ? std::to_string(inSR->subRun()) :
"invalid") <<
"/"
614 << event_aux->subRun();
615 if ((inR ==
nullptr) || !inR->RUN_ID().isValid() || (inR->run() != event_aux->run()))
619 TLOG_ARB(11,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: making RunPrincipal ...";
620 outR = pm_.makeRunPrincipal(*run_aux);
622 art::SubRunID subrun_check(event_aux->run(), event_aux->subRun());
623 if (inSR ==
nullptr || subrun_check != inSR->SUBRUN_ID())
627 TLOG_ARB(11,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
628 <<
"making SubRunPrincipal ...";
629 outSR = pm_.makeSubRunPrincipal(*subrun_aux);
631 TLOG_ARB(11,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: making EventPrincipal ...";
632 auto historyPtr = HISTORY_PTR_T(
new History(*(history_to_use_.get())));
633 if (!art::ProcessHistoryRegistry::get().count(history_to_use_->processHistoryID()))
635 TLOG_WARNING(
"ArtdaqInputHelper") <<
"Stored history is not in ProcessHistoryRegistry, this event may have issues!";
637 outE = pm_.makeEventPrincipal(*event_aux, std::move(historyPtr));
639 TLOG_ARB(11,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
640 <<
"finished processing Event message.";
645 bool art::ArtdaqInputHelper<U>::constructPrincipal(artdaq::Fragment::type_t firstFragmentType, art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE)
652 if (firstFragmentType == artdaq::Fragment::EndOfDataFragmentType)
654 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"Received shutdown message, returning false";
655 shutdownMsgReceived_ =
true;
659 auto evtHeader = communicationWrapper_.getEventHeader();
662 TLOG_ERROR(
"ArtdaqInputHelper") <<
"No RawEventHeader received, cannot construct principals!";
663 shutdownMsgReceived_ =
true;
670 art::Timestamp currentTime = 0;
672 art::TimeValue_t lo_res_time = time(0);
673 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"lo_res_time = " << lo_res_time;
674 currentTime = ((lo_res_time & 0xffffffff) << 32);
676 timespec hi_res_time;
677 int retcode = clock_gettime(CLOCK_REALTIME, &hi_res_time);
678 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"hi_res_time tv_sec = " << hi_res_time.tv_sec
679 <<
" tv_nsec = " << hi_res_time.tv_nsec <<
" (retcode = " << retcode <<
")";
682 currentTime = ((hi_res_time.tv_sec & 0xffffffff) << 32) | (hi_res_time.tv_nsec & 0xffffffff);
686 TLOG_ERROR(
"ArtdaqInputHelper")
687 <<
"Unable to fetch a high-resolution time with clock_gettime for art::Event Timestamp. "
688 <<
"The art::Event Timestamp will be zero for event " << evtHeader->event_id;
692 if (inR ==
nullptr || inR->run() != evtHeader->run_id)
694 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"Making run principal with run_id " << evtHeader->run_id;
695 outR = pm_.makeRunPrincipal(evtHeader->run_id, currentTime);
698 if (firstFragmentType == artdaq::Fragment::EndOfRunFragmentType)
700 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"EndOfRunFragment received, returning Flush event";
701 art::EventID
const evid(art::EventID::flushEvent());
702 outR = pm_.makeRunPrincipal(evid.runID(), currentTime);
703 outSR = pm_.makeSubRunPrincipal(evid.subRunID(), currentTime);
704 outE = pm_.makeEventPrincipal(evid, currentTime);
707 else if (firstFragmentType == artdaq::Fragment::EndOfSubrunFragmentType)
709 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"EndOfSubrunFragment received, creating new Subrun Principal";
711 if (inR ==
nullptr || inR->run() != evtHeader->run_id)
713 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
714 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
715 art::EventID
const evid(art::EventID::flushEvent(outSR->SUBRUN_ID()));
716 outE = pm_.makeEventPrincipal(evid, currentTime);
724 if (inSR !=
nullptr && !inSR->SUBRUN_ID().isFlush() && inSR->subRun() == evtHeader->subrun_id)
726 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"Flushing old run id " << inR->RUN_ID();
727 art::EventID
const evid(art::EventID::flushEvent(inR->RUN_ID()));
728 outSR = pm_.makeSubRunPrincipal(evid.subRunID(), currentTime);
729 outE = pm_.makeEventPrincipal(evid, currentTime);
736 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
737 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
738 art::EventID
const evid(art::EventID::flushEvent(outSR->SUBRUN_ID()));
739 outE = pm_.makeEventPrincipal(evid, currentTime);
750 art::SubRunID subrun_check(evtHeader->run_id, evtHeader->subrun_id);
751 if (inSR ==
nullptr || subrun_check != inSR->SUBRUN_ID())
753 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
754 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
756 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"Making event principal with event_id " << evtHeader->event_id;
757 outE = pm_.makeEventPrincipal(evtHeader->run_id, evtHeader->subrun_id, evtHeader->event_id, currentTime);
765 for (
auto& msg : msgs)
769 TLOG_ARB(12,
"ArtdaqInputHelper") <<
"readDataProducts: reading data product count ...";
770 msg->ReadULong(prd_cnt);
771 TLOG_ARB(12,
"ArtdaqInputHelper") <<
"readDataProducts: product count: " << prd_cnt;
776 for (ULong_t I = 0; I < prd_cnt; ++I)
778 std::unique_ptr<BranchKey> bk;
780 TLOG_ARB(12,
"ArtdaqInputHelper") <<
"readDataProducts: Reading branch key.";
781 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInputHelper::readDataProducts"));
785 TLOG_ARB(13,
"ArtdaqInputHelper") <<
"readDataProducts: got product class: '" << bk->friendlyClassName_ <<
"' modlbl: '"
786 << bk->moduleLabel_ <<
"' instnm: '" << bk->productInstanceName_ <<
"' procnm: '"
789 ProductList::const_iterator iter;
791 TLOG_ARB(12,
"ArtdaqInputHelper") <<
"readDataProducts: looking up product ...";
792 iter = productList_->find(*bk);
793 if (iter == productList_->end())
795 throw art::Exception(art::errors::ProductNotFound)
796 <<
"No product is registered for\n"
797 <<
" process name: '" << bk->processName_ <<
"'\n"
798 <<
" module label: '" << bk->moduleLabel_ <<
"'\n"
799 <<
" product friendly class name: '" << bk->friendlyClassName_ <<
"'\n"
800 <<
" product instance name: '" << bk->productInstanceName_ <<
"'\n";
805 const BranchDescription& bd = iter->second;
806 std::unique_ptr<EDProduct> prd;
808 TLOG_ARB(12,
"ArtdaqInputHelper") <<
"readDataProducts: Reading product with wrapped name: " << bd.wrappedName()
809 <<
", TClass = " <<
static_cast<void*
>(TClass::GetClass(bd.wrappedName().c_str()));
816 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
817 auto pp =
reinterpret_cast<EDProduct*
>(p);
819 TLOG_ARB(12,
"ArtdaqInputHelper") <<
"readDataProducts: After ReadObjectAny(prd): p=" << p <<
", EDProduct::isPresent: " << pp->isPresent();
823 std::unique_ptr<const ProductProvenance> prdprov;
825 TLOG_ARB(12,
"ArtdaqInputHelper") <<
"readDataProducts: Reading product provenance.";
826 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInputHelper::readDataProducts"));
830 TLOG_ARB(12,
"ArtdaqInputHelper") <<
"readDataProducts: inserting product: class: '" << bd.friendlyClassName()
831 <<
"' modlbl: '" << bd.moduleLabel() <<
"' instnm: '" << bd.productInstanceName()
832 <<
"' procnm: '" << bd.processName() <<
"' id: '" << bd.productID() <<
"'";
833 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
841 const BranchDescription& bd,
842 std::unique_ptr<const ProductProvenance>&& prdprov)
844 #if ART_HEX_VERSION < 0x30000
845 rp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forRun(rp->RUN_ID()));
847 rp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forRun(rp->RUN_ID())));
853 const BranchDescription& bd,
854 std::unique_ptr<const ProductProvenance>&& prdprov)
856 #if ART_HEX_VERSION < 0x30000
857 srp->put(std::move(prd), bd, std::move(prdprov), RangeSet::forSubRun(srp->SUBRUN_ID()));
859 srp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forSubRun(srp->SUBRUN_ID())));
865 const BranchDescription& bd,
866 std::unique_ptr<const ProductProvenance>&& prdprov)
868 TLOG_ARB(14,
"ArtdaqInputHelper") <<
"EventPrincipal size before put: " << ep->size();
869 #if ART_HEX_VERSION < 0x30000
870 ep->put(std::move(prd), bd, std::move(prdprov));
872 ep->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::invalid()));
874 TLOG_ARB(14,
"ArtdaqInputHelper") <<
"EventPrincipal size after put: " << ep->size();
881 double fragmentLatency = 0;
882 double fragmentLatencyMax = 0.0;
883 size_t fragmentCount = 0;
885 art::ServiceHandle<ArtdaqFragmentNamingServiceInterface> translator;
888 for (
auto& fragmentTypePair : eventMap)
890 auto type_code = fragmentTypePair.first;
891 if (type_code == artdaq::Fragment::DataFragmentType || type_code == artdaq::Fragment::EndOfDataFragmentType || type_code == artdaq::Fragment::InitFragmentType || type_code == artdaq::Fragment::EndOfRunFragmentType || type_code == artdaq::Fragment::EndOfSubrunFragmentType || type_code == artdaq::Fragment::ShutdownFragmentType)
893 TLOG_TRACE(
"ArtdaqInputHelper") <<
"Skipping system Fragment with type " <<
static_cast<int>(type_code) <<
" ( " << translator->GetInstanceNameForType(type_code) <<
" )";
896 TLOG_TRACE(
"ArtdaqInputHelper") <<
"type is " <<
static_cast<int>(type_code) <<
", number of fragments is " << fragmentTypePair.second->size();
898 std::unordered_map<std::string, std::unique_ptr<artdaq::Fragments>> derived_fragments;
899 for (
auto& frag : *fragmentTypePair.second)
901 TLOG_ARB(16,
"ArtdaqInputHelper") <<
"Processing Fragment with ID " << frag.fragmentID();
902 bytesRead += frag.sizeBytes();
903 auto latency_s = frag.getLatency(
true);
904 double latency = latency_s.tv_sec + (latency_s.tv_nsec / 1000000000.0);
906 fragmentLatency += latency;
908 if (latency > fragmentLatencyMax) fragmentLatencyMax = latency;
910 std::pair<bool, std::string> instance_name_result =
911 translator->GetInstanceNameForFragment(frag);
912 std::string label = instance_name_result.second;
913 if (!instance_name_result.first)
915 TLOG_WARNING(
"ArtdaqInputHelper")
916 <<
"UnknownFragmentType: The product instance name mapping for fragment type \"" <<
static_cast<int>(type_code)
917 <<
"\" is not known. Fragments of this "
918 <<
"type will be stored in the event with an instance name of \"" << label <<
"\".";
920 if (!derived_fragments.count(label))
922 TLOG_ARB(16,
"ArtdaqInputHelper") <<
"Creating output Fragment storage for label " << label;
923 derived_fragments[label] = std::make_unique<artdaq::Fragments>();
925 TLOG_ARB(16,
"ArtdaqInputHelper") <<
"Adding Fragment " << frag.fragmentID() <<
" to storage with label " << label <<
" (sz=" << derived_fragments[label]->size() + 1 <<
")";
926 derived_fragments[label]->emplace_back(std::move(frag));
928 for (
auto& type : derived_fragments)
930 TLOG_ARB(16,
"ArtdaqInputHelper") <<
"Adding " << type.second->size() <<
" Fragments with label " << type.first <<
" to event.";
931 put_product_in_principal(std::move(type.second), *outE, pretend_module_name, type.first);
936 metricMan->sendMetric(
"bytesRead", bytesRead,
"B", 3, artdaq::MetricMode::LastPoint);
938 metricMan->sendMetric(
"ArtdaqInputHelper Latency", fragmentLatency / fragmentCount,
"s", 4, artdaq::MetricMode::Average);
939 metricMan->sendMetric(
"ArtdaqInputHelper Maximum Latency", fragmentLatencyMax,
"s", 4, artdaq::MetricMode::Maximum);
945 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE)
947 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::readNext";
950 if (outputFileCloseNeeded_)
952 outputFileCloseNeeded_ =
false;
955 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
956 <<
"returning false on outputFileCloseNeeded_";
957 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
960 auto read_start_time = std::chrono::steady_clock::now();
962 std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>> eventMap = communicationWrapper_.receiveMessages();
963 auto got_event_time = std::chrono::steady_clock::now();
965 if (eventMap.empty())
967 TLOG_ARB(TLVL_ERROR,
"ArtdaqInputHelper") <<
"No Fragments received! Aborting...";
968 shutdownMsgReceived_ =
true;
969 TLOG_ARB(17,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
973 if (!fragmentsOnlyMode_ && !eventMap.count(artdaq::Fragment::DataFragmentType))
975 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext got a message without a DataFragment";
976 shutdownMsgReceived_ =
true;
977 TLOG_ARB(17,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
981 if (fragmentsOnlyMode_)
983 if (eventMap.count(artdaq::Fragment::DataFragmentType))
985 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext unexpectedly got a message with a DataFragment. This art Event will NOT be reconstructed!";
988 auto firstFragmentType = eventMap.begin()->first;
989 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"First Fragment type is " <<
static_cast<int>(firstFragmentType);
990 if (constructPrincipal(firstFragmentType, inR, inSR, outR, outSR, outE))
992 readFragments(eventMap, outE);
1002 std::list<std::unique_ptr<TBufferFile>> msgs;
1003 for (
auto& dataFrag : *(eventMap[artdaq::Fragment::DataFragmentType]))
1006 msgs.emplace_back(
new TBufferFile(TBuffer::kRead, header->data_length, dataFrag.dataBegin(), kFALSE,
nullptr));
1012 ULong_t msg_type_code = 0;
1013 ULong_t msg_type_code_tmp = 0;
1014 for (
auto& msg : msgs)
1016 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
1017 <<
"getting message type code ...";
1018 msg->ReadULong(msg_type_code_tmp);
1019 TLOG_ARB(15,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
1020 <<
"message type: " << msg_type_code_tmp;
1022 if (msg_type_code == 0)
1023 msg_type_code = msg_type_code_tmp;
1024 else if (msg_type_code != msg_type_code_tmp)
1026 TLOG_ARB(TLVL_ERROR,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: Received conflicting message type codes! Aborting...";
1028 shutdownMsgReceived_ =
true;
1029 TLOG_ARB(17,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
1033 if (msg_type_code == 5)
1036 shutdownMsgReceived_ =
true;
1037 TLOG_ARB(16,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
1038 <<
"returning false on Shutdown message.";
1039 TLOG_ARB(16,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
1043 for (
auto& msg : msgs)
1045 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR, outSR, outE);
1050 if (msg_type_code == 2)
1054 readDataProducts(msgs, outR);
1056 TLOG_ARB(17,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
1057 <<
"returning false on EndRun message.";
1058 TLOG_ARB(17,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
1061 else if (msg_type_code == 3)
1072 if (inR !=
nullptr && inSR !=
nullptr && outR !=
nullptr && outSR !=
nullptr)
1074 if (inR->RUN_ID().isFlush() && inSR->SUBRUN_ID().isFlush() && outR->RUN_ID().isFlush() &&
1075 outSR->SUBRUN_ID().isFlush())
1079 outputFileCloseNeeded_ =
true;
1084 readDataProducts(msgs, outSR);
1087 outputFileCloseNeeded_ =
true;
1088 TLOG_ARB(18,
"ArtdaqInputHelper") <<
"readNext: returning true on EndSubRun message.";
1091 else if (msg_type_code == 4)
1094 readDataProducts(msgs, outE);
1096 if (eventMap.size() > 1)
1098 readFragments(eventMap, outE);
1101 TLOG_ARB(19,
"ArtdaqInputHelper") <<
"readNext: returning true on Event message.";
1106 if (outE !=
nullptr)
1108 auto artHdrPtr = std::make_unique<artdaq::detail::RawEventHeader>();
1109 auto daqHdrPtr = communicationWrapper_.getEventHeader();
1111 if (daqHdrPtr !=
nullptr)
1113 memcpy(artHdrPtr.get(), daqHdrPtr.get(),
sizeof(artdaq::detail::RawEventHeader));
1114 put_product_in_principal(std::move(artHdrPtr), *outE, pretend_module_name,
"RawEventHeader");
1118 auto read_finish_time = std::chrono::steady_clock::now();
1119 TLOG_ARB(10,
"ArtdaqInputHelper") <<
"readNext: bytesRead=" << bytesRead
1120 <<
" metricMan=" <<
static_cast<void*
>(metricMan.get());
1123 metricMan->sendMetric(
"Avg Processing Time", artdaq::TimeUtils::GetElapsedTime(last_read_time, read_start_time),
1124 "s", 2, artdaq::MetricMode::Average);
1125 metricMan->sendMetric(
"Avg Input Wait Time", artdaq::TimeUtils::GetElapsedTime(read_start_time, got_event_time),
1126 "s", 3, artdaq::MetricMode::Average);
1127 metricMan->sendMetric(
"Avg Read Time", artdaq::TimeUtils::GetElapsedTime(got_event_time, read_finish_time),
"s",
1128 3, artdaq::MetricMode::Average);
1131 TLOG_ARB(20,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext ret=" << std::boolalpha << ret;
1132 last_read_time = std::chrono::steady_clock::now();
1136 #endif // ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
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.