1 #ifndef ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
2 #define ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
4 #include "art/Framework/Core/FileBlock.h"
5 #include "art/Framework/Core/InputSourceMacros.h"
6 #include "art/Framework/Core/ProductRegistryHelper.h"
7 #include "art/Framework/IO/Sources/Source.h"
8 #include "art/Framework/IO/Sources/SourceHelper.h"
9 #include "art/Framework/IO/Sources/SourceTraits.h"
10 #include "art/Framework/IO/Sources/put_product_in_principal.h"
11 #include "art/Framework/Principal/EventPrincipal.h"
12 #include "art/Framework/Principal/RunPrincipal.h"
13 #include "art/Framework/Principal/SubRunPrincipal.h"
14 #include "art/Framework/Services/Registry/ServiceHandle.h"
16 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
17 #include "art_root_io/setup.h"
19 #include "canvas/Persistency/Common/EDProduct.h"
20 #include "canvas/Persistency/Common/Wrapper.h"
21 #include "canvas/Persistency/Provenance/BranchDescription.h"
22 #include "canvas/Persistency/Provenance/BranchKey.h"
23 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
24 #include "canvas/Persistency/Provenance/History.h"
25 #include "canvas/Persistency/Provenance/ParentageRegistry.h"
26 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
27 #include "canvas/Persistency/Provenance/ProcessHistory.h"
28 #include "canvas/Persistency/Provenance/ProcessHistoryID.h"
29 #include "canvas/Persistency/Provenance/ProductList.h"
30 #include "canvas/Persistency/Provenance/ProductProvenance.h"
31 #include "canvas/Persistency/Provenance/ProductTables.h"
32 #include "canvas/Utilities/DebugMacros.h"
34 #include "artdaq-utilities/Plugins/MakeParameterSet.hh"
35 #include "fhiclcpp/ParameterSet.h"
36 #include "fhiclcpp/ParameterSetID.h"
37 #include "fhiclcpp/ParameterSetRegistry.h"
39 #include <TBufferFile.h>
42 #include <TStreamerInfo.h>
44 #include "artdaq-core/Data/ContainerFragment.hh"
45 #include "artdaq-core/Data/Fragment.hh"
46 #include "artdaq-core/Data/detail/ParentageMap.hh"
47 #include "artdaq-core/Utilities/ExceptionHandler.hh"
48 #include "artdaq-core/Utilities/TimeUtils.hh"
49 #include "artdaq/ArtModules/ArtdaqFragmentNamingService.h"
50 #include "artdaq/ArtModules/ArtdaqSharedMemoryService.h"
51 #include "artdaq/ArtModules/InputUtilities.hh"
52 #include "artdaq/DAQdata/Globals.hh"
53 #include "artdaq/DAQdata/NetMonHeader.hh"
65 #include <unordered_map>
114 ArtdaqInputHelper(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper, art::SourceHelper
const& pm);
125 void readFile(
const std::string&, art::FileBlock*& fb);
142 bool readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
143 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
149 void readAndConstructPrincipal(std::unique_ptr<TBufferFile>&, ULong_t, art::RunPrincipal*
const,
150 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
151 art::EventPrincipal*&);
153 bool constructPrincipal(artdaq::Fragment::type_t, art::RunPrincipal*
const,
154 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
155 art::EventPrincipal*&);
158 void readDataProducts(std::list<std::unique_ptr<TBufferFile>>&, T*&);
160 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
161 std::unique_ptr<const ProductProvenance>&&);
163 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
164 std::unique_ptr<const ProductProvenance>&&);
166 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
167 std::unique_ptr<const ProductProvenance>&&);
169 void readFragments(std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>>
const& eventMap, art::EventPrincipal*& outE);
171 bool shutdownMsgReceived_;
172 bool outputFileCloseNeeded_;
173 art::SourceHelper
const& pm_;
174 U communicationWrapper_;
175 ProductList* productList_;
176 std::unique_ptr<art::History> history_to_use_;
177 bool fragmentsOnlyMode_;
178 std::string pretend_module_name;
180 std::chrono::steady_clock::time_point last_read_time;
185 art::SourceHelper
const& pm)
186 : shutdownMsgReceived_(false)
187 , outputFileCloseNeeded_(false)
189 , communicationWrapper_(ps)
191 , fragmentsOnlyMode_(false)
192 , pretend_module_name(ps.get<std::string>(
"raw_data_label",
"daq"))
194 , last_read_time(std::chrono::steady_clock::now())
198 art::ServiceHandle<ArtdaqSharedMemoryServiceInterface> shm;
201 volatile bool loop =
true;
214 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::ArtdaqInputHelper("
215 <<
"const fhicl::ParameterSet& ps, "
216 <<
"art::ProductRegistryHelper& helper, "
217 <<
"const art::SourceHelper& pm)";
219 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"Going to receive init message";
220 artdaq::FragmentPtrs initFrags = communicationWrapper_.receiveInitMessage();
221 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"Init message received";
223 if (!initFrags.empty() && initFrags.front().get()->type() == artdaq::Fragment::EndOfDataFragmentType)
225 TLOG_ERROR(
"ArtdaqInputHelper") <<
"Received EndOfData as first broadcast! This process neveer received any data!";
226 shutdownMsgReceived_ =
true;
227 outputFileCloseNeeded_ =
true;
231 if (initFrags.empty() || initFrags.back().get()->dataSize() == 0)
233 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"No init message received or zero-size init message: Fragments-only mode activated! This is an EventBuilder!";
234 fragmentsOnlyMode_ =
true;
238 std::list<std::unique_ptr<TBufferFile>> msgs;
239 for (
auto& initFrag : initFrags)
242 msgs.emplace_back(
new TBufferFile(TBuffer::kRead, header->data_length, initFrag->dataBegin(), kFALSE,
nullptr));
245 std::list<History*> processHistories;
247 for (
auto& msg : msgs)
251 msg->ReadULong(dummy);
254 auto list =
dynamic_cast<TList*
>(msg->ReadObject(TList::Class()));
258 TObjLink* lnk = list->FirstLink();
262 info =
dynamic_cast<TStreamerInfo*
>(lnk->GetObject());
263 TObject* element = info->GetElements()->UncheckedAt(0);
264 Bool_t isstl = element && strcmp(
"This", element->GetName()) == 0;
268 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() <<
", version = " << info->GetClassVersion();
273 lnk = list->FirstLink();
276 info =
dynamic_cast<TStreamerInfo*
>(lnk->GetObject());
277 TObject* element = info->GetElements()->UncheckedAt(0);
278 Bool_t isstl = element && strcmp(
"This", element->GetName()) == 0;
282 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() <<
", version = " << info->GetClassVersion();
292 msg->ReadULong(ps_cnt);
293 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: parameter set count: " << ps_cnt;
294 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: reading parameter sets ...";
295 for (ULong_t I = 0; I < ps_cnt; ++I)
297 std::string pset_str =
"";
298 msg->ReadStdString(pset_str);
300 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: parameter set: " << pset_str;
302 fhicl::ParameterSet pset;
303 pset = artdaq::make_pset(pset_str);
306 fhicl::ParameterSetRegistry::put(pset);
308 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: finished reading parameter sets.";
313 auto thisProductList = ReadObjectAny<art::ProductList>(
314 msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInputHelper::ArtdaqInputHelper");
315 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Input Product list sz=" << thisProductList->size();
317 bool productListInitialized = productList_ !=
nullptr;
318 if (!productListInitialized) productList_ = thisProductList;
319 for (
auto I = thisProductList->begin(), E = thisProductList->end(); I != E; ++I)
322 TLOG(50,
"ArtdaqInputHelper") <<
"Branch key: class: '" << I->first.friendlyClassName_ <<
"' modlbl: '"
323 << I->first.moduleLabel_ <<
"' instnm: '" << I->first.productInstanceName_ <<
"' procnm: '"
324 << I->first.processName_ <<
"', branch description name: " << I->second.wrappedName()
325 <<
", TClass = " <<
static_cast<void*
>(TClass::GetClass(I->second.wrappedName().c_str()));
327 if (productListInitialized)
329 productList_->emplace(*I);
333 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading ProcessHistory";
334 auto phm = ReadObjectAny<art::ProcessHistoryMap>(
335 msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInputHelper::ArtdaqInputHelper");
338 ProcessHistoryRegistry::put(*phm);
339 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInputHelper's ProcessHistoryRegistry");
344 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading ParentageMap";
345 auto parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInputHelper::ArtdaqInputHelper");
346 ParentageRegistry::put(*parentageMap);
351 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading History";
352 processHistories.push_back(ReadObjectAny<History>(msg,
"art::History",
"ArtdaqInputHelper::ArtdaqInputHelper"));
356 art::ProcessHistory fake_process_history;
357 for (
auto& hist : processHistories)
359 auto const&
id = hist->processHistoryID();
360 ProcessHistory thisProcessHistory;
361 if (ProcessHistoryRegistry::get(
id, thisProcessHistory))
363 for (
auto& conf : thisProcessHistory)
364 fake_process_history.push_back(conf);
367 art::ProcessHistoryMap fake_process_history_map;
368 fake_process_history_map[fake_process_history.id()] = fake_process_history;
369 ProcessHistoryRegistry::put(fake_process_history_map);
370 history_to_use_.reset(
new History());
371 history_to_use_->setProcessHistoryID(fake_process_history.id());
372 for (
auto& hist : processHistories)
374 for (
auto& es : hist->eventSelectionIDs())
376 history_to_use_->addEventSelectionEntry(es);
380 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper")
381 <<
"ArtdaqInputHelper: Product list sz=" << productList_->size();
385 helper.productList(std::unique_ptr<art::ProductList>(productList_));
386 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: got product list";
389 helper.reconstitutes<artdaq::detail::RawEventHeader, art::InEvent>(pretend_module_name,
"RawEventHeader");
391 if (ps.get<
bool>(
"register_fragment_types",
true))
393 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"Registering known Fragment labels from ArtdaqFragmentNamingServiceInterface";
395 art::ServiceHandle<ArtdaqFragmentNamingServiceInterface> translator;
396 helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, translator->GetUnidentifiedInstanceName());
398 helper.reconstitutes<artdaq::Fragments, art::InRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
399 helper.reconstitutes<artdaq::Fragments, art::InSubRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
401 std::set<std::string> instance_names = translator->GetAllProductInstanceNames();
402 for (
const auto& set_iter : instance_names)
404 helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, set_iter);
410 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::ArtdaqInputHelper("
411 <<
"const fhicl::ParameterSet& ps, "
412 <<
"art::ProductRegistryHelper& helper, "
413 <<
"const art::SourceHelper& pm)";
424 TLOG(11,
"ArtdaqInputHelper") <<
"Begin/End: ArtdaqInputHelper::closeCurrentFile()";
430 TLOG(12,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::"
431 "readFile(const std::string& name, art::FileBlock*& fb)";
432 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInputHelper2013"),
"");
433 TLOG(12,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::"
434 "readFile(const std::string& name, art::FileBlock*& fb)";
440 TLOG(13,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::hasMoreData()";
441 if (shutdownMsgReceived_)
443 TLOG(13,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::hasMoreData(): "
444 "returning false on shutdownMsgReceived_.";
445 TLOG(13,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::hasMoreData()";
448 TLOG(8,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::hasMoreData(): "
449 "returning true on not shutdownMsgReceived_.";
450 TLOG(13,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::hasMoreData()";
456 art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
457 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
458 art::EventPrincipal*& outE)
463 std::unique_ptr<art::RunAuxiliary> run_aux;
464 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
465 std::unique_ptr<art::EventAuxiliary> event_aux;
467 std::unique_ptr<art::History> history_from_event;
474 if (msg_type_code == 2)
477 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
478 <<
"processing EndRun message ...";
480 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
483 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
484 <<
"making flush RunPrincipal ...";
485 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
487 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
488 <<
"making flush SubRunPrincipal ...";
489 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
491 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
492 <<
"making flush EventPrincipal ...";
493 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
495 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
496 <<
"finished processing EndRun message.";
498 else if (msg_type_code == 3)
501 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
502 <<
"processing EndSubRun message ...";
505 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
508 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
509 <<
"making flush RunPrincipal ...";
510 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
527 art::Timestamp currentTime = time(
nullptr);
530 inR->endTime(currentTime);
534 inSR->endTime(currentTime);
537 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
538 <<
"making flush SubRunPrincipal ...";
539 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
541 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
542 <<
"making flush EventPrincipal ...";
543 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
545 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
546 <<
"finished processing EndSubRun message.";
548 else if (msg_type_code == 4)
551 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
552 <<
"processing Event message ...";
554 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
558 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
562 ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
564 history_from_event.reset(ReadObjectAny<art::History>(msg,
"art::History",
"ArtdaqInputHelper::readAndConstructPrincipal"));
568 if (!history_from_event->processHistoryID().isValid())
570 throw art::Exception(art::errors::Unknown)
571 <<
"readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
574 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
575 <<
"inR: " <<
static_cast<void*
>(inR) <<
" run/expected "
576 << (inR ? std::to_string(inR->run()) :
"invalid") <<
"/" << event_aux->run();
577 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
578 <<
"inSR: " <<
static_cast<void*
>(inSR) <<
" run/expected "
579 << (inSR ? std::to_string(inSR->run()) :
"invalid") <<
"/" << event_aux->run()
580 <<
", subrun/expected " << (inSR ? std::to_string(inSR->subRun()) :
"invalid") <<
"/"
581 << event_aux->subRun();
582 if ((inR ==
nullptr) || !inR->runID().isValid() || (inR->run() != event_aux->run()))
586 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: making RunPrincipal ...";
587 outR = pm_.makeRunPrincipal(*run_aux);
589 art::SubRunID subrun_check(event_aux->run(), event_aux->subRun());
590 if (inSR ==
nullptr || subrun_check != inSR->subRunID())
594 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
595 <<
"making SubRunPrincipal ...";
596 outSR = pm_.makeSubRunPrincipal(*subrun_aux);
598 TLOG(11,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: making EventPrincipal ...";
599 auto historyPtr = std::unique_ptr<art::History>(
new History(*(history_to_use_.get())));
600 if (!art::ProcessHistoryRegistry::get().count(history_to_use_->processHistoryID()))
602 TLOG_WARNING(
"ArtdaqInputHelper") <<
"Stored history is not in ProcessHistoryRegistry, this event may have issues!";
604 outE = pm_.makeEventPrincipal(*event_aux, std::move(historyPtr));
606 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
607 <<
"finished processing Event message.";
612 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)
619 if (firstFragmentType == artdaq::Fragment::EndOfDataFragmentType)
621 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"Received shutdown message, returning false";
622 shutdownMsgReceived_ =
true;
626 auto evtHeader = communicationWrapper_.getEventHeader();
629 TLOG_ERROR(
"ArtdaqInputHelper") <<
"No RawEventHeader received, cannot construct principals!";
630 shutdownMsgReceived_ =
true;
637 art::Timestamp currentTime = 0;
639 art::TimeValue_t lo_res_time = time(0);
640 TLOG(20,
"ArtdaqInputHelper") <<
"lo_res_time = " << lo_res_time;
641 currentTime = ((lo_res_time & 0xffffffff) << 32);
643 timespec hi_res_time;
644 int retcode = clock_gettime(CLOCK_REALTIME, &hi_res_time);
645 TLOG(20,
"ArtdaqInputHelper") <<
"hi_res_time tv_sec = " << hi_res_time.tv_sec
646 <<
" tv_nsec = " << hi_res_time.tv_nsec <<
" (retcode = " << retcode <<
")";
649 currentTime = ((hi_res_time.tv_sec & 0xffffffff) << 32) | (hi_res_time.tv_nsec & 0xffffffff);
653 TLOG_ERROR(
"ArtdaqInputHelper")
654 <<
"Unable to fetch a high-resolution time with clock_gettime for art::Event Timestamp. "
655 <<
"The art::Event Timestamp will be zero for event " << evtHeader->event_id;
659 if (inR ==
nullptr || inR->run() != evtHeader->run_id)
661 TLOG(20,
"ArtdaqInputHelper") <<
"Making run principal with run_id " << evtHeader->run_id;
662 outR = pm_.makeRunPrincipal(evtHeader->run_id, currentTime);
665 if (firstFragmentType == artdaq::Fragment::EndOfRunFragmentType)
667 TLOG(20,
"ArtdaqInputHelper") <<
"EndOfRunFragment received, returning Flush event";
668 art::EventID
const evid(art::EventID::flushEvent());
669 outR = pm_.makeRunPrincipal(evid.runID(), currentTime);
670 outSR = pm_.makeSubRunPrincipal(evid.subRunID(), currentTime);
671 outE = pm_.makeEventPrincipal(evid, currentTime);
674 else if (firstFragmentType == artdaq::Fragment::EndOfSubrunFragmentType)
676 TLOG(20,
"ArtdaqInputHelper") <<
"EndOfSubrunFragment received, creating new Subrun Principal";
678 if (inR ==
nullptr || inR->run() != evtHeader->run_id)
680 TLOG(20,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
681 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
682 art::EventID
const evid(art::EventID::flushEvent(outSR->subRunID()));
683 outE = pm_.makeEventPrincipal(evid, currentTime);
691 if (inSR !=
nullptr && !inSR->subRunID().isFlush() && inSR->subRun() == evtHeader->subrun_id)
693 TLOG(20,
"ArtdaqInputHelper") <<
"Flushing old run id " << inR->runID();
694 art::EventID
const evid(art::EventID::flushEvent(inR->runID()));
695 outSR = pm_.makeSubRunPrincipal(evid.subRunID(), currentTime);
696 outE = pm_.makeEventPrincipal(evid, currentTime);
703 TLOG(20,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
704 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
705 art::EventID
const evid(art::EventID::flushEvent(outSR->subRunID()));
706 outE = pm_.makeEventPrincipal(evid, currentTime);
717 art::SubRunID subrun_check(evtHeader->run_id, evtHeader->subrun_id);
718 if (inSR ==
nullptr || subrun_check != inSR->subRunID())
720 TLOG(20,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
721 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
723 TLOG(20,
"ArtdaqInputHelper") <<
"Making event principal with event_id " << evtHeader->event_id;
724 outE = pm_.makeEventPrincipal(evtHeader->run_id, evtHeader->subrun_id, evtHeader->event_id, currentTime);
732 for (
auto& msg : msgs)
736 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: reading data product count ...";
737 msg->ReadULong(prd_cnt);
738 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: product count: " << prd_cnt;
743 for (ULong_t I = 0; I < prd_cnt; ++I)
745 std::unique_ptr<BranchKey> bk;
747 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: Reading branch key.";
748 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInputHelper::readDataProducts"));
752 TLOG(18,
"ArtdaqInputHelper") <<
"readDataProducts: got product class: '" << bk->friendlyClassName_ <<
"' modlbl: '"
753 << bk->moduleLabel_ <<
"' instnm: '" << bk->productInstanceName_ <<
"' procnm: '"
756 ProductList::const_iterator iter;
758 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: looking up product ...";
759 iter = productList_->find(*bk);
760 if (iter == productList_->end())
762 throw art::Exception(art::errors::ProductNotFound)
763 <<
"No product is registered for\n"
764 <<
" process name: '" << bk->processName_ <<
"'\n"
765 <<
" module label: '" << bk->moduleLabel_ <<
"'\n"
766 <<
" product friendly class name: '" << bk->friendlyClassName_ <<
"'\n"
767 <<
" product instance name: '" << bk->productInstanceName_ <<
"'\n";
772 const BranchDescription& bd = iter->second;
773 std::unique_ptr<EDProduct> prd;
775 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: Reading product with wrapped name: " << bd.wrappedName()
776 <<
", TClass = " <<
static_cast<void*
>(TClass::GetClass(bd.wrappedName().c_str()));
783 void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
784 auto pp =
reinterpret_cast<EDProduct*
>(p);
786 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: After ReadObjectAny(prd): p=" << p <<
", EDProduct::isPresent: " << pp->isPresent();
790 std::unique_ptr<const ProductProvenance> prdprov;
792 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: Reading product provenance.";
793 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInputHelper::readDataProducts"));
797 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: inserting product: class: '" << bd.friendlyClassName()
798 <<
"' modlbl: '" << bd.moduleLabel() <<
"' instnm: '" << bd.productInstanceName()
799 <<
"' procnm: '" << bd.processName() <<
"' id: '" << bd.productID() <<
"'";
800 putInPrincipal(outPrincipal, std::move(prd), bd, std::move(prdprov));
808 const BranchDescription& bd,
809 std::unique_ptr<const ProductProvenance>&& prdprov)
811 rp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forRun(rp->runID())));
816 const BranchDescription& bd,
817 std::unique_ptr<const ProductProvenance>&& prdprov)
819 srp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forSubRun(srp->subRunID())));
824 const BranchDescription& bd,
825 std::unique_ptr<const ProductProvenance>&& prdprov)
827 TLOG(19,
"ArtdaqInputHelper") <<
"EventPrincipal size before put: " << ep->size();
829 ep->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::invalid()));
831 TLOG(19,
"ArtdaqInputHelper") <<
"EventPrincipal size after put: " << ep->size();
838 double fragmentLatency = 0;
839 double fragmentLatencyMax = 0.0;
840 size_t fragmentCount = 0;
842 art::ServiceHandle<ArtdaqFragmentNamingServiceInterface> translator;
845 for (
auto& fragmentTypePair : eventMap)
847 auto type_code = fragmentTypePair.first;
848 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)
850 TLOG_TRACE(
"ArtdaqInputHelper") <<
"Skipping system Fragment with type " <<
static_cast<int>(type_code) <<
" ( " << translator->GetInstanceNameForType(type_code) <<
" )";
853 TLOG_TRACE(
"ArtdaqInputHelper") <<
"type is " <<
static_cast<int>(type_code) <<
", number of fragments is " << fragmentTypePair.second->size();
855 std::unordered_map<std::string, std::unique_ptr<artdaq::Fragments>> derived_fragments;
856 for (
auto& frag : *fragmentTypePair.second)
858 TLOG(21,
"ArtdaqInputHelper") <<
"Processing Fragment with ID " << frag.fragmentID();
859 bytesRead += frag.sizeBytes();
860 auto latency_s = frag.getLatency(
true);
861 double latency = latency_s.tv_sec + (latency_s.tv_nsec / 1000000000.0);
863 fragmentLatency += latency;
865 if (latency > fragmentLatencyMax) fragmentLatencyMax = latency;
867 std::pair<bool, std::string> instance_name_result =
868 translator->GetInstanceNameForFragment(frag);
869 std::string label = instance_name_result.second;
870 if (!instance_name_result.first)
872 TLOG_WARNING(
"ArtdaqInputHelper")
873 <<
"UnknownFragmentType: The product instance name mapping for fragment type \"" <<
static_cast<int>(type_code)
874 <<
"\" is not known. Fragments of this "
875 <<
"type will be stored in the event with an instance name of \"" << label <<
"\".";
877 if (!derived_fragments.count(label))
879 TLOG(21,
"ArtdaqInputHelper") <<
"Creating output Fragment storage for label " << label;
880 derived_fragments[label] = std::make_unique<artdaq::Fragments>();
882 TLOG(21,
"ArtdaqInputHelper") <<
"Adding Fragment " << frag.fragmentID() <<
" to storage with label " << label <<
" (sz=" << derived_fragments[label]->size() + 1 <<
")";
883 derived_fragments[label]->emplace_back(std::move(frag));
885 for (
auto& type : derived_fragments)
887 TLOG(21,
"ArtdaqInputHelper") <<
"Adding " << type.second->size() <<
" Fragments with label " << type.first <<
" to event.";
888 put_product_in_principal(std::move(type.second), *outE, pretend_module_name, type.first);
893 metricMan->sendMetric(
"bytesRead", bytesRead,
"B", 3, artdaq::MetricMode::LastPoint);
895 metricMan->sendMetric(
"ArtdaqInputHelper Latency", fragmentLatency / fragmentCount,
"s", 4, artdaq::MetricMode::Average);
896 metricMan->sendMetric(
"ArtdaqInputHelper Maximum Latency", fragmentLatencyMax,
"s", 4, artdaq::MetricMode::Maximum);
902 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE)
904 TLOG(20,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::readNext";
907 if (outputFileCloseNeeded_)
909 outputFileCloseNeeded_ =
false;
912 TLOG(20,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
913 <<
"returning false on outputFileCloseNeeded_";
914 TLOG(20,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
917 auto read_start_time = std::chrono::steady_clock::now();
919 std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>> eventMap = communicationWrapper_.receiveMessages();
920 auto got_event_time = std::chrono::steady_clock::now();
922 if (eventMap.empty())
924 TLOG(TLVL_ERROR,
"ArtdaqInputHelper") <<
"No Fragments received! Aborting...";
925 shutdownMsgReceived_ =
true;
926 TLOG(22,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
930 if (!fragmentsOnlyMode_ && !eventMap.count(artdaq::Fragment::DataFragmentType))
932 TLOG(20,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext got a message without a DataFragment";
933 shutdownMsgReceived_ =
true;
934 TLOG(22,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
938 if (fragmentsOnlyMode_)
940 if (eventMap.count(artdaq::Fragment::DataFragmentType))
942 TLOG(20,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext unexpectedly got a message with a DataFragment. This art Event will NOT be reconstructed!";
945 auto firstFragmentType = eventMap.begin()->first;
946 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"First Fragment type is " <<
static_cast<int>(firstFragmentType);
947 if (constructPrincipal(firstFragmentType, inR, inSR, outR, outSR, outE))
949 readFragments(eventMap, outE);
959 std::list<std::unique_ptr<TBufferFile>> msgs;
960 for (
auto& dataFrag : *(eventMap[artdaq::Fragment::DataFragmentType]))
963 msgs.emplace_back(
new TBufferFile(TBuffer::kRead, header->data_length, dataFrag.dataBegin(), kFALSE,
nullptr));
969 ULong_t msg_type_code = 0;
970 ULong_t msg_type_code_tmp = 0;
971 for (
auto& msg : msgs)
973 TLOG(20,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
974 <<
"getting message type code ...";
975 msg->ReadULong(msg_type_code_tmp);
976 TLOG(20,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
977 <<
"message type: " << msg_type_code_tmp;
979 if (msg_type_code == 0)
980 msg_type_code = msg_type_code_tmp;
981 else if (msg_type_code != msg_type_code_tmp)
983 TLOG(TLVL_ERROR,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: Received conflicting message type codes! Aborting...";
985 shutdownMsgReceived_ =
true;
986 TLOG(22,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
990 if (msg_type_code == 5)
993 shutdownMsgReceived_ =
true;
994 TLOG(21,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
995 <<
"returning false on Shutdown message.";
996 TLOG(21,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
1000 for (
auto& msg : msgs)
1002 readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR, outSR, outE);
1007 if (msg_type_code == 2)
1011 readDataProducts(msgs, outR);
1013 TLOG(22,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
1014 <<
"returning false on EndRun message.";
1015 TLOG(22,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
1018 else if (msg_type_code == 3)
1029 if (inR !=
nullptr && inSR !=
nullptr && outR !=
nullptr && outSR !=
nullptr)
1031 if (inR->runID().isFlush() && inSR->subRunID().isFlush() && outR->runID().isFlush() &&
1032 outSR->subRunID().isFlush())
1036 outputFileCloseNeeded_ =
true;
1041 readDataProducts(msgs, outSR);
1044 outputFileCloseNeeded_ =
true;
1045 TLOG(23,
"ArtdaqInputHelper") <<
"readNext: returning true on EndSubRun message.";
1048 else if (msg_type_code == 4)
1051 readDataProducts(msgs, outE);
1053 if (eventMap.size() > 1)
1055 readFragments(eventMap, outE);
1058 TLOG(24,
"ArtdaqInputHelper") <<
"readNext: returning true on Event message.";
1063 if (outE !=
nullptr)
1065 auto artHdrPtr = std::make_unique<artdaq::detail::RawEventHeader>();
1066 auto daqHdrPtr = communicationWrapper_.getEventHeader();
1068 if (daqHdrPtr !=
nullptr)
1070 memcpy(artHdrPtr.get(), daqHdrPtr.get(),
sizeof(artdaq::detail::RawEventHeader));
1071 put_product_in_principal(std::move(artHdrPtr), *outE, pretend_module_name,
"RawEventHeader");
1075 auto read_finish_time = std::chrono::steady_clock::now();
1076 TLOG(20,
"ArtdaqInputHelper") <<
"readNext: bytesRead=" << bytesRead
1077 <<
" metricMan=" <<
static_cast<void*
>(metricMan.get());
1080 metricMan->sendMetric(
"Avg Processing Time", artdaq::TimeUtils::GetElapsedTime(last_read_time, read_start_time),
1081 "s", 2, artdaq::MetricMode::Average);
1082 metricMan->sendMetric(
"Avg Input Wait Time", artdaq::TimeUtils::GetElapsedTime(read_start_time, got_event_time),
1083 "s", 3, artdaq::MetricMode::Average);
1084 metricMan->sendMetric(
"Avg Read Time", artdaq::TimeUtils::GetElapsedTime(got_event_time, read_finish_time),
"s",
1085 3, artdaq::MetricMode::Average);
1088 TLOG(25,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext ret=" << std::boolalpha << ret;
1089 last_read_time = std::chrono::steady_clock::now();
1093 #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.