1 #ifndef ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
2 #define ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
4 #include "TRACE/tracemf.h"
6 #include "artdaq-core/Data/Fragment.hh"
7 #include "artdaq/ArtModules/InputUtilities.hh"
9 #include "artdaq-core/Data/detail/ParentageMap.hh"
10 #include "artdaq-core/Utilities/TimeUtils.hh"
11 #include "artdaq/ArtModules/ArtdaqFragmentNamingService.h"
12 #include "artdaq/ArtModules/ArtdaqSharedMemoryServiceInterface.h"
13 #include "artdaq/DAQdata/Globals.hh"
14 #include "artdaq/DAQdata/NetMonHeader.hh"
16 #include "art/Framework/Core/FileBlock.h"
17 #include "art/Framework/Core/ProductRegistryHelper.h"
18 #include "art/Framework/IO/Sources/SourceHelper.h"
19 #include "art/Framework/IO/Sources/put_product_in_principal.h"
20 #include "art/Framework/Principal/EventPrincipal.h"
21 #include "art/Framework/Principal/RunPrincipal.h"
22 #include "art/Framework/Principal/SubRunPrincipal.h"
23 #include "art/Framework/Services/Registry/ServiceHandle.h"
25 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
26 #include "art_root_io/setup.h"
28 #include "canvas/Persistency/Common/EDProduct.h"
29 #include "canvas/Persistency/Provenance/BranchDescription.h"
30 #include "canvas/Persistency/Provenance/BranchKey.h"
31 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
32 #if ART_HEX_VERSION < 0x31100
33 #include "canvas/Persistency/Provenance/History.h"
35 #include "canvas/Persistency/Provenance/Compatibility/History.h"
37 #include "canvas/Persistency/Provenance/ParentageRegistry.h"
38 #include "canvas/Persistency/Provenance/ProcessHistory.h"
39 #include "canvas/Persistency/Provenance/ProcessHistoryID.h"
40 #include "canvas/Persistency/Provenance/ProductList.h"
41 #include "canvas/Persistency/Provenance/ProductProvenance.h"
43 #include "fhiclcpp/ParameterSet.h"
44 #include "fhiclcpp/ParameterSetID.h"
45 #include "fhiclcpp/ParameterSetRegistry.h"
47 #include <TBufferFile.h>
50 #include <TStreamerInfo.h>
63 #include <unordered_map>
112 ArtdaqInputHelper(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper, art::SourceHelper
const& pm);
123 void readFile(
const std::string&, art::FileBlock*& fb);
140 bool readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
141 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
147 void readAndConstructPrincipal(std::unique_ptr<TBufferFile>&, ULong_t, art::RunPrincipal*
const,
148 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
149 art::EventPrincipal*&);
151 bool constructPrincipal(artdaq::Fragment::type_t, art::RunPrincipal*
const,
152 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
153 art::EventPrincipal*&);
156 void readDataProducts(std::list<std::unique_ptr<TBufferFile>>&, T*&);
158 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
159 std::unique_ptr<const ProductProvenance>&&);
161 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
162 std::unique_ptr<const ProductProvenance>&&);
164 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
165 std::unique_ptr<const ProductProvenance>&&);
167 void readFragments(std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>>
const& eventMap, art::EventPrincipal*& outE);
169 bool shutdownMsgReceived_;
170 bool outputFileCloseNeeded_;
171 art::SourceHelper
const& pm_;
172 U communicationWrapper_;
173 ProductList* productList_;
174 std::unique_ptr<art::History> history_to_use_;
175 bool fragmentsOnlyMode_;
176 std::string pretend_module_name;
178 std::chrono::steady_clock::time_point last_read_time;
183 art::SourceHelper
const& pm)
184 : shutdownMsgReceived_(false)
185 , outputFileCloseNeeded_(false)
187 , communicationWrapper_(ps)
189 , fragmentsOnlyMode_(false)
190 , pretend_module_name(ps.get<std::string>(
"raw_data_label",
"daq"))
192 , last_read_time(std::chrono::steady_clock::now())
196 art::ServiceHandle<ArtdaqSharedMemoryServiceInterface> shm;
199 volatile bool loop =
true;
212 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::ArtdaqInputHelper("
213 <<
"const fhicl::ParameterSet& ps, "
214 <<
"art::ProductRegistryHelper& helper, "
215 <<
"const art::SourceHelper& pm)";
217 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"Going to receive init message";
218 artdaq::FragmentPtrs initFrags = communicationWrapper_.receiveInitMessage();
219 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"Init message received";
221 if (!initFrags.empty() && initFrags.front().get()->type() == artdaq::Fragment::EndOfDataFragmentType)
223 TLOG_ERROR(
"ArtdaqInputHelper") <<
"Received EndOfData as first broadcast! This process neveer received any data!";
224 shutdownMsgReceived_ =
true;
225 outputFileCloseNeeded_ =
true;
229 if (initFrags.empty() || initFrags.back().get()->dataSize() == 0)
231 TLOG(TLVL_DEBUG + 32,
"ArtdaqInputHelper") <<
"No init message received or zero-size init message: Fragments-only mode activated! This is an EventBuilder!";
232 fragmentsOnlyMode_ =
true;
236 std::list<std::unique_ptr<TBufferFile>> msgs;
237 for (
auto& initFrag : initFrags)
240 msgs.emplace_back(
new TBufferFile(TBuffer::kRead, header->data_length, initFrag->dataBegin(), kFALSE,
nullptr));
243 std::set<art::ProcessHistoryID> history_ids;
245 for (
auto& msg : msgs)
249 msg->ReadULong(dummy);
252 auto list =
dynamic_cast<TList*
>(msg->ReadObject(TList::Class()));
256 TObjLink* lnk = list->FirstLink();
260 info =
dynamic_cast<TStreamerInfo*
>(lnk->GetObject());
261 TObject* element = info->GetElements()->UncheckedAt(0);
262 Bool_t isstl = element && strcmp(
"This", element->GetName()) == 0;
266 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() <<
", version = " << info->GetClassVersion();
271 lnk = list->FirstLink();
274 info =
dynamic_cast<TStreamerInfo*
>(lnk->GetObject());
275 TObject* element = info->GetElements()->UncheckedAt(0);
276 Bool_t isstl = element && strcmp(
"This", element->GetName()) == 0;
280 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() <<
", version = " << info->GetClassVersion();
290 msg->ReadULong(ps_cnt);
291 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: parameter set count: " << ps_cnt;
292 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: reading parameter sets ...";
293 for (ULong_t I = 0; I < ps_cnt; ++I)
295 std::string pset_str =
"";
296 msg->ReadStdString(pset_str);
298 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: parameter set: " << pset_str;
300 fhicl::ParameterSet pset;
301 pset = fhicl::ParameterSet::make(pset_str);
304 fhicl::ParameterSetRegistry::put(pset);
306 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: finished reading parameter sets.";
311 auto thisProductList = ReadObjectAny<art::ProductList>(
312 msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInputHelper::ArtdaqInputHelper");
313 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Input Product list sz=" << thisProductList->size();
315 bool productListInitialized = productList_ !=
nullptr;
316 if (!productListInitialized) productList_ = thisProductList;
317 for (
auto I = thisProductList->begin(), E = thisProductList->end(); I != E; ++I)
320 TLOG(50,
"ArtdaqInputHelper") <<
"Branch key: class: '" << I->first.friendlyClassName_ <<
"' modlbl: '"
321 << I->first.moduleLabel_ <<
"' instnm: '" << I->first.productInstanceName_ <<
"' procnm: '"
322 << I->first.processName_ <<
"', branch description name: " << I->second.wrappedName()
323 <<
", TClass = " <<
static_cast<void*
>(TClass::GetClass(I->second.wrappedName().c_str()));
325 if (productListInitialized)
327 productList_->emplace(*I);
331 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading ProcessHistory";
332 auto phm = ReadObjectAny<art::ProcessHistoryMap>(
333 msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInputHelper::ArtdaqInputHelper");
336 for (
auto& proc_hist : *phm)
338 history_ids.insert(proc_hist.second.id());
341 ProcessHistoryRegistry::put(*phm);
342 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInputHelper's ProcessHistoryRegistry");
347 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading ParentageMap";
348 auto parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInputHelper::ArtdaqInputHelper");
349 ParentageRegistry::put(*parentageMap);
353 art::ProcessHistory fake_process_history;
354 for (
auto& hist : history_ids)
358 TLOG(TLVL_WARNING,
"ArtdaqInputHelper") <<
"Encountered invalid history ID!";
362 ProcessHistory thisProcessHistory;
363 if (ProcessHistoryRegistry::get(hist, thisProcessHistory))
365 for (
auto& conf : thisProcessHistory)
367 if (
auto e = fake_process_history.end();
368 std::find(fake_process_history.begin(), e, conf) == e)
370 fake_process_history.push_back(conf);
375 art::ProcessHistoryMap fake_process_history_map;
376 fake_process_history_map[fake_process_history.id()] = fake_process_history;
377 ProcessHistoryRegistry::put(fake_process_history_map);
378 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInputHelper's ProcessHistoryRegistry w/fake history");
380 history_to_use_.reset(
new History());
381 history_to_use_->setProcessHistoryID(fake_process_history.id());
383 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper")
384 <<
"ArtdaqInputHelper: Product list sz=" << productList_->size();
388 helper.productList(std::unique_ptr<art::ProductList>(productList_));
389 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: got product list";
392 helper.reconstitutes<artdaq::detail::RawEventHeader, art::InEvent>(pretend_module_name,
"RawEventHeader");
394 if (ps.get<
bool>(
"register_fragment_types",
true))
396 TLOG(TLVL_DEBUG + 32,
"ArtdaqInputHelper") <<
"Registering known Fragment labels from ArtdaqFragmentNamingServiceInterface";
398 art::ServiceHandle<ArtdaqFragmentNamingServiceInterface> translator;
399 helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, translator->GetUnidentifiedInstanceName());
401 helper.reconstitutes<artdaq::Fragments, art::InRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
402 helper.reconstitutes<artdaq::Fragments, art::InSubRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
404 std::set<std::string> instance_names = translator->GetAllProductInstanceNames();
405 for (
const auto& set_iter : instance_names)
407 helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, set_iter);
413 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::ArtdaqInputHelper("
414 <<
"const fhicl::ParameterSet& ps, "
415 <<
"art::ProductRegistryHelper& helper, "
416 <<
"const art::SourceHelper& pm)";
427 TLOG(TLVL_DEBUG + 34,
"ArtdaqInputHelper") <<
"Begin/End: ArtdaqInputHelper::closeCurrentFile()";
433 TLOG(TLVL_DEBUG + 35,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::"
434 "readFile(const std::string& name, art::FileBlock*& fb)";
435 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInputHelper2013"),
"");
436 TLOG(TLVL_DEBUG + 35,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::"
437 "readFile(const std::string& name, art::FileBlock*& fb)";
443 TLOG(TLVL_DEBUG + 36,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::hasMoreData()";
444 if (shutdownMsgReceived_)
446 TLOG(TLVL_DEBUG + 36,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::hasMoreData(): "
447 "returning false on shutdownMsgReceived_.";
448 TLOG(TLVL_DEBUG + 36,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::hasMoreData()";
451 TLOG(TLVL_DEBUG + 32,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::hasMoreData(): "
452 "returning true on not shutdownMsgReceived_.";
453 TLOG(TLVL_DEBUG + 36,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::hasMoreData()";
459 art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
460 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
461 art::EventPrincipal*& outE)
466 std::unique_ptr<art::RunAuxiliary> run_aux;
467 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
468 std::unique_ptr<art::EventAuxiliary> event_aux;
475 if (msg_type_code == 2)
478 TLOG(TLVL_DEBUG + 37,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
479 <<
"processing EndRun message ...";
481 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
484 TLOG(TLVL_DEBUG + 37,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
485 <<
"making flush RunPrincipal ...";
486 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
488 TLOG(TLVL_DEBUG + 37,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
489 <<
"making flush SubRunPrincipal ...";
490 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
492 TLOG(TLVL_DEBUG + 37,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
493 <<
"making flush EventPrincipal ...";
494 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
496 TLOG(TLVL_DEBUG + 37,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
497 <<
"finished processing EndRun message.";
499 else if (msg_type_code == 3)
502 TLOG(TLVL_DEBUG + 38,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
503 <<
"processing EndSubRun message ...";
506 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
509 TLOG(TLVL_DEBUG + 38,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
510 <<
"making flush RunPrincipal ...";
511 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
528 #if ART_HEX_VERSION < 0x31100
530 art::Timestamp currentTime = time(
nullptr);
533 inR->endTime(currentTime);
537 inSR->endTime(currentTime);
541 TLOG(TLVL_DEBUG + 38,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
542 <<
"making flush SubRunPrincipal ...";
543 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
545 TLOG(TLVL_DEBUG + 38,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
546 <<
"making flush EventPrincipal ...";
547 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
549 TLOG(TLVL_DEBUG + 38,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
550 <<
"finished processing EndSubRun message.";
552 else if (msg_type_code == 4)
555 TLOG(TLVL_DEBUG + 39,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
556 <<
"processing Event message ...";
558 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
562 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
566 ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
568 TLOG(TLVL_DEBUG + 39,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
569 <<
"inR: " <<
static_cast<void*
>(inR) <<
" run/expected "
570 << (inR ? std::to_string(inR->run()) :
"invalid") <<
"/" << event_aux->run();
571 TLOG(TLVL_DEBUG + 39,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
572 <<
"inSR: " <<
static_cast<void*
>(inSR) <<
" run/expected "
573 << (inSR ? std::to_string(inSR->run()) :
"invalid") <<
"/" << event_aux->run()
574 <<
", subrun/expected " << (inSR ? std::to_string(inSR->subRun()) :
"invalid") <<
"/"
575 << event_aux->subRun();
576 if ((inR ==
nullptr) || !inR->runID().isValid() || (inR->run() != event_aux->run()))
580 TLOG(TLVL_DEBUG + 39,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: making RunPrincipal ...";
581 outR = pm_.makeRunPrincipal(*run_aux);
583 art::SubRunID subrun_check(event_aux->run(), event_aux->subRun());
584 if (inSR ==
nullptr || subrun_check != inSR->subRunID())
588 TLOG(TLVL_DEBUG + 39,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
589 <<
"making SubRunPrincipal ...";
590 outSR = pm_.makeSubRunPrincipal(*subrun_aux);
592 TLOG(TLVL_DEBUG + 34,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: making EventPrincipal ...";
593 #if ART_HEX_VERSION < 0x31100
594 auto historyPtr = std::unique_ptr<art::History>(
new History(*(history_to_use_.get())));
595 if (!art::ProcessHistoryRegistry::get().count(history_to_use_->processHistoryID()))
597 TLOG_WARNING(
"ArtdaqInputHelper") <<
"Stored history is not in ProcessHistoryRegistry, this event may have issues!";
599 outE = pm_.makeEventPrincipal(*event_aux, std::move(historyPtr));
601 if (!art::ProcessHistoryRegistry::get().count(history_to_use_->processHistoryID()))
603 TLOG_WARNING(
"ArtdaqInputHelper") <<
"Stored history is not in ProcessHistoryRegistry, this event may have issues!";
605 event_aux->setProcessHistoryID(history_to_use_->processHistoryID());
606 outE = pm_.makeEventPrincipal(*event_aux);
608 TLOG(TLVL_DEBUG + 39,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
609 <<
"finished processing Event message.";
614 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)
621 if (firstFragmentType == artdaq::Fragment::EndOfDataFragmentType)
623 TLOG(TLVL_DEBUG + 32,
"ArtdaqInputHelper") <<
"Received shutdown message, returning false";
624 shutdownMsgReceived_ =
true;
628 auto evtHeader = communicationWrapper_.getEventHeader();
631 TLOG_ERROR(
"ArtdaqInputHelper") <<
"No RawEventHeader received, cannot construct principals!";
632 shutdownMsgReceived_ =
true;
639 art::Timestamp currentTime = 0;
641 art::TimeValue_t lo_res_time = time(0);
642 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"lo_res_time = " << lo_res_time;
643 currentTime = ((lo_res_time & 0xffffffff) << 32);
645 timespec hi_res_time;
646 int retcode = clock_gettime(CLOCK_REALTIME, &hi_res_time);
647 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"hi_res_time tv_sec = " << hi_res_time.tv_sec
648 <<
" tv_nsec = " << hi_res_time.tv_nsec <<
" (retcode = " << retcode <<
")";
651 currentTime = ((hi_res_time.tv_sec & 0xffffffff) << 32) | (hi_res_time.tv_nsec & 0xffffffff);
655 TLOG_ERROR(
"ArtdaqInputHelper")
656 <<
"Unable to fetch a high-resolution time with clock_gettime for art::Event Timestamp. "
657 <<
"The art::Event Timestamp will be zero for event " << evtHeader->event_id;
661 if (inR ==
nullptr || inR->run() != evtHeader->run_id)
663 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"Making run principal with run_id " << evtHeader->run_id;
664 outR = pm_.makeRunPrincipal(evtHeader->run_id, currentTime);
667 if (firstFragmentType == artdaq::Fragment::EndOfRunFragmentType)
669 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"EndOfRunFragment received, returning Flush event";
670 art::EventID
const evid(art::EventID::flushEvent());
671 outR = pm_.makeRunPrincipal(evid.runID(), currentTime);
672 outSR = pm_.makeSubRunPrincipal(evid.subRunID(), currentTime);
673 outE = pm_.makeEventPrincipal(evid, currentTime);
676 else if (firstFragmentType == artdaq::Fragment::EndOfSubrunFragmentType)
678 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"EndOfSubrunFragment received, creating new Subrun Principal";
680 if (inR ==
nullptr || inR->run() != evtHeader->run_id)
682 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
683 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
684 art::EventID
const evid(art::EventID::flushEvent(outSR->subRunID()));
685 outE = pm_.makeEventPrincipal(evid, currentTime);
693 if (inSR !=
nullptr && !inSR->subRunID().isFlush() && inSR->subRun() == evtHeader->subrun_id)
695 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"Flushing old run id " << inR->runID();
696 art::EventID
const evid(art::EventID::flushEvent(inR->runID()));
697 outSR = pm_.makeSubRunPrincipal(evid.subRunID(), currentTime);
698 outE = pm_.makeEventPrincipal(evid, currentTime);
705 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
706 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
707 art::EventID
const evid(art::EventID::flushEvent(outSR->subRunID()));
708 outE = pm_.makeEventPrincipal(evid, currentTime);
719 art::SubRunID subrun_check(evtHeader->run_id, evtHeader->subrun_id);
720 if (inSR ==
nullptr || subrun_check != inSR->subRunID())
722 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
723 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
725 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"Making event principal with event_id " << evtHeader->event_id;
726 outE = pm_.makeEventPrincipal(evtHeader->run_id, evtHeader->subrun_id, evtHeader->event_id, currentTime);
734 for (
auto& msg : msgs)
738 TLOG(TLVL_DEBUG + 40,
"ArtdaqInputHelper") <<
"readDataProducts: reading data product count ...";
739 msg->ReadULong(prd_cnt);
740 TLOG(TLVL_DEBUG + 40,
"ArtdaqInputHelper") <<
"readDataProducts: product count: " << prd_cnt;
745 for (ULong_t I = 0; I < prd_cnt; ++I)
747 std::unique_ptr<BranchKey> bk;
749 TLOG(TLVL_DEBUG + 40,
"ArtdaqInputHelper") <<
"readDataProducts: Reading branch key.";
750 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInputHelper::readDataProducts"));
754 TLOG(TLVL_DEBUG + 41,
"ArtdaqInputHelper") <<
"readDataProducts: got product class: '" << bk->friendlyClassName_ <<
"' modlbl: '"
755 << bk->moduleLabel_ <<
"' instnm: '" << bk->productInstanceName_ <<
"' procnm: '"
758 ProductList::const_iterator iter;
760 TLOG(TLVL_DEBUG + 40,
"ArtdaqInputHelper") <<
"readDataProducts: looking up product ...";
761 iter = productList_->find(*bk);
762 if (iter == productList_->end())
764 throw art::Exception(art::errors::ProductNotFound)
765 <<
"No product is registered for\n"
766 <<
" process name: '" << bk->processName_ <<
"'\n"
767 <<
" module label: '" << bk->moduleLabel_ <<
"'\n"
768 <<
" product friendly class name: '" << bk->friendlyClassName_ <<
"'\n"
769 <<
" product instance name: '" << bk->productInstanceName_ <<
"'\n";
774 const BranchDescription& bd = iter->second;
775 std::unique_ptr<EDProduct> prd;
777 TLOG(TLVL_DEBUG + 40,
"ArtdaqInputHelper") <<
"readDataProducts: Reading product with wrapped name: " << bd.wrappedName()
778 <<
", TClass = " <<
static_cast<void*
>(TClass::GetClass(bd.wrappedName().c_str()));
785 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
786 auto pp =
reinterpret_cast<EDProduct*
>(p);
788 TLOG(TLVL_DEBUG + 40,
"ArtdaqInputHelper") <<
"readDataProducts: After ReadObjectAny(prd): p=" << p <<
", EDProduct::isPresent: " << pp->isPresent();
792 std::unique_ptr<const ProductProvenance> prdprov;
794 TLOG(TLVL_DEBUG + 40,
"ArtdaqInputHelper") <<
"readDataProducts: Reading product provenance.";
795 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInputHelper::readDataProducts"));
799 TLOG(TLVL_DEBUG + 40,
"ArtdaqInputHelper") <<
"readDataProducts: inserting product: class: '" << bd.friendlyClassName()
800 <<
"' modlbl: '" << bd.moduleLabel() <<
"' instnm: '" << bd.productInstanceName()
801 <<
"' procnm: '" << bd.processName() <<
"' id: '" << bd.productID() <<
"'";
802 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
810 const BranchDescription& bd,
811 std::unique_ptr<const ProductProvenance>&& prdprov)
813 rp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forRun(rp->runID())));
818 const BranchDescription& bd,
819 std::unique_ptr<const ProductProvenance>&& prdprov)
821 srp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forSubRun(srp->subRunID())));
826 const BranchDescription& bd,
827 std::unique_ptr<const ProductProvenance>&& prdprov)
829 TLOG(TLVL_DEBUG + 42,
"ArtdaqInputHelper") <<
"EventPrincipal size before put: " << ep->size();
831 ep->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::invalid()));
833 TLOG(TLVL_DEBUG + 42,
"ArtdaqInputHelper") <<
"EventPrincipal size after put: " << ep->size();
840 double fragmentLatency = 0;
841 double fragmentLatencyMax = 0.0;
842 size_t fragmentCount = 0;
844 art::ServiceHandle<ArtdaqFragmentNamingServiceInterface> translator;
847 for (
auto& fragmentTypePair : eventMap)
849 auto type_code = fragmentTypePair.first;
850 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)
852 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"Skipping system Fragment with type " <<
static_cast<int>(type_code) <<
" ( " << translator->GetInstanceNameForType(type_code) <<
" )";
855 TLOG(TLVL_DEBUG + 33,
"ArtdaqInputHelper") <<
"type is " <<
static_cast<int>(type_code) <<
", number of fragments is " << fragmentTypePair.second->size();
857 std::unordered_map<std::string, std::unique_ptr<artdaq::Fragments>> derived_fragments;
858 for (
auto& frag : *fragmentTypePair.second)
860 TLOG(TLVL_DEBUG + 44,
"ArtdaqInputHelper") <<
"Processing Fragment with ID " << frag.fragmentID();
861 bytesRead += frag.sizeBytes();
862 auto latency_s = frag.getLatency(
true);
863 double latency = latency_s.tv_sec + (latency_s.tv_nsec / 1000000000.0);
865 fragmentLatency += latency;
867 if (latency > fragmentLatencyMax) fragmentLatencyMax = latency;
869 std::pair<bool, std::string> instance_name_result =
870 translator->GetInstanceNameForFragment(frag);
871 std::string label = instance_name_result.second;
872 if (!instance_name_result.first)
874 TLOG_WARNING(
"ArtdaqInputHelper")
875 <<
"UnknownFragmentType: The product instance name mapping for fragment type \"" <<
static_cast<int>(type_code)
876 <<
"\" is not known. Fragments of this "
877 <<
"type will be stored in the event with an instance name of \"" << label <<
"\".";
879 if (!derived_fragments.count(label))
881 TLOG(TLVL_DEBUG + 44,
"ArtdaqInputHelper") <<
"Creating output Fragment storage for label " << label;
882 derived_fragments[label] = std::make_unique<artdaq::Fragments>();
884 TLOG(TLVL_DEBUG + 44,
"ArtdaqInputHelper") <<
"Adding Fragment " << frag.fragmentID() <<
" to storage with label " << label <<
" (sz=" << derived_fragments[label]->size() + 1 <<
")";
885 derived_fragments[label]->emplace_back(std::move(frag));
887 for (
auto& type : derived_fragments)
889 TLOG(TLVL_DEBUG + 44,
"ArtdaqInputHelper") <<
"Adding " << type.second->size() <<
" Fragments with label " << type.first <<
" to event.";
890 put_product_in_principal(std::move(type.second), *outE, pretend_module_name, type.first);
895 metricMan->sendMetric(
"bytesRead", bytesRead,
"B", 3, artdaq::MetricMode::LastPoint);
897 metricMan->sendMetric(
"ArtdaqInputHelper Latency", fragmentLatency / fragmentCount,
"s", 4, artdaq::MetricMode::Average);
898 metricMan->sendMetric(
"ArtdaqInputHelper Maximum Latency", fragmentLatencyMax,
"s", 4, artdaq::MetricMode::Maximum);
904 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE)
906 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::readNext";
909 if (outputFileCloseNeeded_)
911 outputFileCloseNeeded_ =
false;
914 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
915 <<
"returning false on outputFileCloseNeeded_";
916 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
919 auto read_start_time = std::chrono::steady_clock::now();
921 std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>> eventMap = communicationWrapper_.receiveMessages();
922 auto got_event_time = std::chrono::steady_clock::now();
924 if (eventMap.empty())
926 TLOG(TLVL_ERROR,
"ArtdaqInputHelper") <<
"No Fragments received! Aborting...";
927 shutdownMsgReceived_ =
true;
928 TLOG(TLVL_DEBUG + 45,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
932 if (!fragmentsOnlyMode_ && !eventMap.count(artdaq::Fragment::DataFragmentType))
934 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext got a message without a DataFragment";
935 shutdownMsgReceived_ =
true;
936 TLOG(TLVL_DEBUG + 45,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
940 if (fragmentsOnlyMode_)
942 if (eventMap.count(artdaq::Fragment::DataFragmentType))
944 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext unexpectedly got a message with a DataFragment. This art Event will NOT be reconstructed!";
947 auto firstFragmentType = eventMap.begin()->first;
948 TLOG(TLVL_DEBUG + 32,
"ArtdaqInputHelper") <<
"First Fragment type is " <<
static_cast<int>(firstFragmentType);
949 if (constructPrincipal(firstFragmentType, inR, inSR, outR, outSR, outE))
951 readFragments(eventMap, outE);
961 std::list<std::unique_ptr<TBufferFile>> msgs;
962 for (
auto& dataFrag : *(eventMap[artdaq::Fragment::DataFragmentType]))
965 msgs.emplace_back(
new TBufferFile(TBuffer::kRead, header->data_length, dataFrag.dataBegin(), kFALSE,
nullptr));
971 ULong_t msg_type_code = 0;
972 ULong_t msg_type_code_tmp = 0;
973 for (
auto& msg : msgs)
975 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
976 <<
"getting message type code ...";
977 msg->ReadULong(msg_type_code_tmp);
978 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
979 <<
"message type: " << msg_type_code_tmp;
981 if (msg_type_code == 0)
982 msg_type_code = msg_type_code_tmp;
983 else if (msg_type_code != msg_type_code_tmp)
985 TLOG(TLVL_ERROR,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: Received conflicting message type codes! Aborting...";
987 shutdownMsgReceived_ =
true;
988 TLOG(TLVL_DEBUG + 45,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
992 if (msg_type_code == 5)
995 shutdownMsgReceived_ =
true;
996 TLOG(TLVL_DEBUG + 44,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
997 <<
"returning false on Shutdown message.";
998 TLOG(TLVL_DEBUG + 44,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
1002 for (
auto& msg : msgs)
1004 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR, outSR, outE);
1009 if (msg_type_code == 2)
1013 readDataProducts(msgs, outR);
1015 TLOG(TLVL_DEBUG + 45,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
1016 <<
"returning false on EndRun message.";
1017 TLOG(TLVL_DEBUG + 45,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
1020 else if (msg_type_code == 3)
1031 if (inR !=
nullptr && inSR !=
nullptr && outR !=
nullptr && outSR !=
nullptr)
1033 if (inR->runID().isFlush() && inSR->subRunID().isFlush() && outR->runID().isFlush() &&
1034 outSR->subRunID().isFlush())
1038 outputFileCloseNeeded_ =
true;
1043 readDataProducts(msgs, outSR);
1046 outputFileCloseNeeded_ =
true;
1047 TLOG(TLVL_DEBUG + 46,
"ArtdaqInputHelper") <<
"readNext: returning true on EndSubRun message.";
1050 else if (msg_type_code == 4)
1053 readDataProducts(msgs, outE);
1055 if (eventMap.size() > 1)
1057 readFragments(eventMap, outE);
1060 TLOG(TLVL_DEBUG + 47,
"ArtdaqInputHelper") <<
"readNext: returning true on Event message.";
1065 if (outE !=
nullptr)
1067 auto artHdrPtr = std::make_unique<artdaq::detail::RawEventHeader>();
1068 auto daqHdrPtr = communicationWrapper_.getEventHeader();
1070 if (daqHdrPtr !=
nullptr)
1072 memcpy(artHdrPtr.get(), daqHdrPtr.get(),
sizeof(artdaq::detail::RawEventHeader));
1073 put_product_in_principal(std::move(artHdrPtr), *outE, pretend_module_name,
"RawEventHeader");
1077 auto read_finish_time = std::chrono::steady_clock::now();
1078 TLOG(TLVL_DEBUG + 43,
"ArtdaqInputHelper") <<
"readNext: bytesRead=" << bytesRead
1079 <<
" metricMan=" <<
static_cast<void*
>(metricMan.get());
1082 metricMan->sendMetric(
"Avg Processing Time", artdaq::TimeUtils::GetElapsedTime(last_read_time, read_start_time),
1083 "s", 2, artdaq::MetricMode::Average);
1084 metricMan->sendMetric(
"Avg Input Wait Time", artdaq::TimeUtils::GetElapsedTime(read_start_time, got_event_time),
1085 "s", 3, artdaq::MetricMode::Average);
1086 metricMan->sendMetric(
"Avg Read Time", artdaq::TimeUtils::GetElapsedTime(got_event_time, read_finish_time),
"s",
1087 3, artdaq::MetricMode::Average);
1090 TLOG(TLVL_DEBUG + 48,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext ret=" << std::boolalpha << ret;
1091 last_read_time = std::chrono::steady_clock::now();
1095 #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.