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 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
19 #include "art_root_io/setup.h"
21 #include "canvas/Persistency/Common/EDProduct.h"
22 #include "canvas/Persistency/Common/Wrapper.h"
23 #include "canvas/Persistency/Provenance/BranchDescription.h"
24 #include "canvas/Persistency/Provenance/BranchKey.h"
25 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
26 #include "canvas/Persistency/Provenance/History.h"
27 #include "canvas/Persistency/Provenance/ParentageRegistry.h"
28 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
29 #include "canvas/Persistency/Provenance/ProcessHistory.h"
30 #include "canvas/Persistency/Provenance/ProcessHistoryID.h"
31 #include "canvas/Persistency/Provenance/ProductList.h"
32 #include "canvas/Persistency/Provenance/ProductProvenance.h"
33 #include "canvas/Persistency/Provenance/ProductTables.h"
34 #include "canvas/Utilities/DebugMacros.h"
36 #include "artdaq-utilities/Plugins/MakeParameterSet.hh"
37 #include "fhiclcpp/ParameterSet.h"
38 #include "fhiclcpp/ParameterSetID.h"
39 #include "fhiclcpp/ParameterSetRegistry.h"
41 #include <TBufferFile.h>
44 #include <TStreamerInfo.h>
46 #include "artdaq-core/Data/ContainerFragment.hh"
47 #include "artdaq-core/Data/Fragment.hh"
48 #include "artdaq-core/Data/detail/ParentageMap.hh"
49 #include "artdaq-core/Utilities/ExceptionHandler.hh"
50 #include "artdaq-core/Utilities/TimeUtils.hh"
51 #include "artdaq/ArtModules/ArtdaqFragmentNamingService.h"
52 #include "artdaq/ArtModules/ArtdaqSharedMemoryService.h"
53 #include "artdaq/ArtModules/InputUtilities.hh"
54 #include "artdaq/DAQdata/Globals.hh"
55 #include "artdaq/DAQdata/NetMonHeader.hh"
67 #include <unordered_map>
116 ArtdaqInputHelper(
const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper, art::SourceHelper
const& pm);
127 void readFile(
const std::string&, art::FileBlock*& fb);
144 bool readNext(art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR, art::RunPrincipal*& outR,
145 art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
151 void readAndConstructPrincipal(std::unique_ptr<TBufferFile>&, ULong_t, art::RunPrincipal*
const,
152 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
153 art::EventPrincipal*&);
155 bool constructPrincipal(artdaq::Fragment::type_t, art::RunPrincipal*
const,
156 art::SubRunPrincipal*
const, art::RunPrincipal*&, art::SubRunPrincipal*&,
157 art::EventPrincipal*&);
160 void readDataProducts(std::list<std::unique_ptr<TBufferFile>>&, T*&);
162 void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
163 std::unique_ptr<const ProductProvenance>&&);
165 void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
166 std::unique_ptr<const ProductProvenance>&&);
168 void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&,
const BranchDescription&,
169 std::unique_ptr<const ProductProvenance>&&);
171 void readFragments(std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>>
const& eventMap, art::EventPrincipal*& outE);
173 bool shutdownMsgReceived_;
174 bool outputFileCloseNeeded_;
175 art::SourceHelper
const& pm_;
176 U communicationWrapper_;
177 ProductList* productList_;
178 std::unique_ptr<art::History> history_to_use_;
179 bool fragmentsOnlyMode_;
180 std::string pretend_module_name;
182 std::chrono::steady_clock::time_point last_read_time;
187 art::SourceHelper
const& pm)
188 : shutdownMsgReceived_(false)
189 , outputFileCloseNeeded_(false)
191 , communicationWrapper_(ps)
193 , fragmentsOnlyMode_(false)
194 , pretend_module_name(ps.get<std::string>(
"raw_data_label",
"daq"))
196 , last_read_time(std::chrono::steady_clock::now())
200 art::ServiceHandle<ArtdaqSharedMemoryServiceInterface> shm;
203 volatile bool loop =
true;
216 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::ArtdaqInputHelper("
217 <<
"const fhicl::ParameterSet& ps, "
218 <<
"art::ProductRegistryHelper& helper, "
219 <<
"const art::SourceHelper& pm)";
221 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"Going to receive init message";
222 artdaq::FragmentPtrs initFrags = communicationWrapper_.receiveInitMessage();
223 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"Init message received";
225 if (!initFrags.empty() && initFrags.front().get()->type() == artdaq::Fragment::EndOfDataFragmentType)
227 TLOG_ERROR(
"ArtdaqInputHelper") <<
"Received EndOfData as first broadcast! This process neveer received any data!";
228 shutdownMsgReceived_ =
true;
229 outputFileCloseNeeded_ =
true;
233 if (initFrags.empty() || initFrags.back().get()->dataSize() == 0)
235 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"No init message received or zero-size init message: Fragments-only mode activated! This is an EventBuilder!";
236 fragmentsOnlyMode_ =
true;
240 std::list<std::unique_ptr<TBufferFile>> msgs;
241 for (
auto& initFrag : initFrags)
244 msgs.emplace_back(
new TBufferFile(TBuffer::kRead, header->data_length, initFrag->dataBegin(), kFALSE,
nullptr));
247 std::list<History*> processHistories;
249 for (
auto& msg : msgs)
253 msg->ReadULong(dummy);
256 auto list =
dynamic_cast<TList*
>(msg->ReadObject(TList::Class()));
260 TObjLink* lnk = list->FirstLink();
264 info =
dynamic_cast<TStreamerInfo*
>(lnk->GetObject());
265 TObject* element = info->GetElements()->UncheckedAt(0);
266 Bool_t isstl = element && strcmp(
"This", element->GetName()) == 0;
270 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() <<
", version = " << info->GetClassVersion();
275 lnk = list->FirstLink();
278 info =
dynamic_cast<TStreamerInfo*
>(lnk->GetObject());
279 TObject* element = info->GetElements()->UncheckedAt(0);
280 Bool_t isstl = element && strcmp(
"This", element->GetName()) == 0;
284 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() <<
", version = " << info->GetClassVersion();
294 msg->ReadULong(ps_cnt);
295 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: parameter set count: " << ps_cnt;
296 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: reading parameter sets ...";
297 for (ULong_t I = 0; I < ps_cnt; ++I)
299 std::string pset_str =
"";
300 msg->ReadStdString(pset_str);
302 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: parameter set: " << pset_str;
304 fhicl::ParameterSet pset;
305 pset = artdaq::make_pset(pset_str);
308 fhicl::ParameterSetRegistry::put(pset);
310 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: finished reading parameter sets.";
315 auto thisProductList = ReadObjectAny<art::ProductList>(
316 msg,
"std::map<art::BranchKey,art::BranchDescription>",
"ArtdaqInputHelper::ArtdaqInputHelper");
317 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Input Product list sz=" << thisProductList->size();
319 bool productListInitialized = productList_ !=
nullptr;
320 if (!productListInitialized) productList_ = thisProductList;
321 for (
auto I = thisProductList->begin(), E = thisProductList->end(); I != E; ++I)
324 TLOG(50,
"ArtdaqInputHelper") <<
"Branch key: class: '" << I->first.friendlyClassName_ <<
"' modlbl: '"
325 << I->first.moduleLabel_ <<
"' instnm: '" << I->first.productInstanceName_ <<
"' procnm: '"
326 << I->first.processName_ <<
"', branch description name: " << I->second.wrappedName()
327 <<
", TClass = " <<
static_cast<void*
>(TClass::GetClass(I->second.wrappedName().c_str()));
329 if (productListInitialized)
331 productList_->emplace(*I);
335 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading ProcessHistory";
336 auto phm = ReadObjectAny<art::ProcessHistoryMap>(
337 msg,
"std::map<const art::Hash<2>,art::ProcessHistory>",
"ArtdaqInputHelper::ArtdaqInputHelper");
340 ProcessHistoryRegistry::put(*phm);
341 printProcessMap(ProcessHistoryRegistry::get(),
"ArtdaqInputHelper's ProcessHistoryRegistry");
346 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading ParentageMap";
347 auto parentageMap = ReadObjectAny<ParentageMap>(msg,
"art::ParentageMap",
"ArtdaqInputHelper::ArtdaqInputHelper");
348 ParentageRegistry::put(*parentageMap);
353 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: Reading History";
354 processHistories.push_back(ReadObjectAny<History>(msg,
"art::History",
"ArtdaqInputHelper::ArtdaqInputHelper"));
358 art::ProcessHistory fake_process_history;
359 for (
auto& hist : processHistories)
361 auto const&
id = hist->processHistoryID();
362 ProcessHistory thisProcessHistory;
363 if (ProcessHistoryRegistry::get(
id, thisProcessHistory))
365 for (
auto& conf : thisProcessHistory)
366 fake_process_history.push_back(conf);
369 art::ProcessHistoryMap fake_process_history_map;
370 fake_process_history_map[fake_process_history.id()] = fake_process_history;
371 ProcessHistoryRegistry::put(fake_process_history_map);
372 history_to_use_.reset(
new History());
373 history_to_use_->setProcessHistoryID(fake_process_history.id());
374 for (
auto& hist : processHistories)
376 for (
auto& es : hist->eventSelectionIDs())
378 history_to_use_->addEventSelectionEntry(es);
382 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper")
383 <<
"ArtdaqInputHelper: Product list sz=" << productList_->size();
387 helper.productList(std::unique_ptr<art::ProductList>(productList_));
388 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper: got product list";
391 helper.reconstitutes<artdaq::detail::RawEventHeader, art::InEvent>(pretend_module_name,
"RawEventHeader");
393 if (ps.get<
bool>(
"register_fragment_types",
true))
395 TLOG_DEBUG(
"ArtdaqInputHelper") <<
"Registering known Fragment labels from ArtdaqFragmentNamingServiceInterface";
397 art::ServiceHandle<ArtdaqFragmentNamingServiceInterface> translator;
398 helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, translator->GetUnidentifiedInstanceName());
400 helper.reconstitutes<artdaq::Fragments, art::InRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
401 helper.reconstitutes<artdaq::Fragments, art::InSubRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
403 std::set<std::string> instance_names = translator->GetAllProductInstanceNames();
404 for (
const auto& set_iter : instance_names)
406 helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, set_iter);
412 TLOG(TLVL_DEBUG + 2,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::ArtdaqInputHelper("
413 <<
"const fhicl::ParameterSet& ps, "
414 <<
"art::ProductRegistryHelper& helper, "
415 <<
"const art::SourceHelper& pm)";
426 TLOG(11,
"ArtdaqInputHelper") <<
"Begin/End: ArtdaqInputHelper::closeCurrentFile()";
432 TLOG(12,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::"
433 "readFile(const std::string& name, art::FileBlock*& fb)";
434 fb =
new art::FileBlock(art::FileFormatVersion(1,
"ArtdaqInputHelper2013"),
"");
435 TLOG(12,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::"
436 "readFile(const std::string& name, art::FileBlock*& fb)";
442 TLOG(13,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::hasMoreData()";
443 if (shutdownMsgReceived_)
445 TLOG(13,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::hasMoreData(): "
446 "returning false on shutdownMsgReceived_.";
447 TLOG(13,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::hasMoreData()";
450 TLOG(8,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::hasMoreData(): "
451 "returning true on not shutdownMsgReceived_.";
452 TLOG(13,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::hasMoreData()";
458 art::RunPrincipal*
const inR, art::SubRunPrincipal*
const inSR,
459 art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
460 art::EventPrincipal*& outE)
465 std::unique_ptr<art::RunAuxiliary> run_aux;
466 std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
467 std::unique_ptr<art::EventAuxiliary> event_aux;
469 std::unique_ptr<art::History> history_from_event;
476 if (msg_type_code == 2)
479 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
480 <<
"processing EndRun message ...";
482 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
485 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
486 <<
"making flush RunPrincipal ...";
487 outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
489 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
490 <<
"making flush SubRunPrincipal ...";
491 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
493 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
494 <<
"making flush EventPrincipal ...";
495 outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(),
true, EventAuxiliary::Any);
497 TLOG(14,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
498 <<
"finished processing EndRun message.";
500 else if (msg_type_code == 3)
503 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
504 <<
"processing EndSubRun message ...";
507 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
510 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
511 <<
"making flush RunPrincipal ...";
512 outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
529 art::Timestamp currentTime = time(
nullptr);
532 inR->endTime(currentTime);
536 inSR->endTime(currentTime);
539 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
540 <<
"making flush SubRunPrincipal ...";
541 outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
543 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
544 <<
"making flush EventPrincipal ...";
545 outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(),
true, EventAuxiliary::Any);
547 TLOG(15,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
548 <<
"finished processing EndSubRun message.";
550 else if (msg_type_code == 4)
553 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
554 <<
"processing Event message ...";
556 run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg,
"art::RunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
560 ReadObjectAny<art::SubRunAuxiliary>(msg,
"art::SubRunAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
564 ReadObjectAny<art::EventAuxiliary>(msg,
"art::EventAuxiliary",
"ArtdaqInputHelper::readAndConstructPrincipal"));
566 history_from_event.reset(ReadObjectAny<art::History>(msg,
"art::History",
"ArtdaqInputHelper::readAndConstructPrincipal"));
570 if (!history_from_event->processHistoryID().isValid())
572 throw art::Exception(art::errors::Unknown)
573 <<
"readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
576 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
577 <<
"inR: " <<
static_cast<void*
>(inR) <<
" run/expected "
578 << (inR ? std::to_string(inR->run()) :
"invalid") <<
"/" << event_aux->run();
579 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
580 <<
"inSR: " <<
static_cast<void*
>(inSR) <<
" run/expected "
581 << (inSR ? std::to_string(inSR->run()) :
"invalid") <<
"/" << event_aux->run()
582 <<
", subrun/expected " << (inSR ? std::to_string(inSR->subRun()) :
"invalid") <<
"/"
583 << event_aux->subRun();
584 if ((inR ==
nullptr) || !inR->runID().isValid() || (inR->run() != event_aux->run()))
588 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: making RunPrincipal ...";
589 outR = pm_.makeRunPrincipal(*run_aux);
591 art::SubRunID subrun_check(event_aux->run(), event_aux->subRun());
592 if (inSR ==
nullptr || subrun_check != inSR->subRunID())
596 TLOG(16,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: "
597 <<
"making SubRunPrincipal ...";
598 outSR = pm_.makeSubRunPrincipal(*subrun_aux);
600 TLOG(11,
"ArtdaqInputHelper") <<
"readAndConstructPrincipal: making EventPrincipal ...";
601 auto historyPtr = std::unique_ptr<art::History>(
new History(*(history_to_use_.get())));
602 if (!art::ProcessHistoryRegistry::get().count(history_to_use_->processHistoryID()))
604 TLOG_WARNING(
"ArtdaqInputHelper") <<
"Stored history is not in ProcessHistoryRegistry, this event may have issues!";
606 outE = pm_.makeEventPrincipal(*event_aux, std::move(historyPtr));
608 TLOG(16,
"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_DEBUG(
"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(20,
"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(20,
"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(20,
"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(20,
"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(20,
"ArtdaqInputHelper") <<
"EndOfSubrunFragment received, creating new Subrun Principal";
680 if (inR ==
nullptr || inR->run() != evtHeader->run_id)
682 TLOG(20,
"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(20,
"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(20,
"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(20,
"ArtdaqInputHelper") <<
"Making subrun principal with subrun_id " << evtHeader->subrun_id;
723 outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
725 TLOG(20,
"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(17,
"ArtdaqInputHelper") <<
"readDataProducts: reading data product count ...";
739 msg->ReadULong(prd_cnt);
740 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: product count: " << prd_cnt;
745 for (ULong_t I = 0; I < prd_cnt; ++I)
747 std::unique_ptr<BranchKey> bk;
749 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: Reading branch key.";
750 bk.reset(ReadObjectAny<BranchKey>(msg,
"art::BranchKey",
"ArtdaqInputHelper::readDataProducts"));
754 TLOG(18,
"ArtdaqInputHelper") <<
"readDataProducts: got product class: '" << bk->friendlyClassName_ <<
"' modlbl: '"
755 << bk->moduleLabel_ <<
"' instnm: '" << bk->productInstanceName_ <<
"' procnm: '"
758 ProductList::const_iterator iter;
760 TLOG(17,
"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(17,
"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(17,
"ArtdaqInputHelper") <<
"readDataProducts: After ReadObjectAny(prd): p=" << p <<
", EDProduct::isPresent: " << pp->isPresent();
792 std::unique_ptr<const ProductProvenance> prdprov;
794 TLOG(17,
"ArtdaqInputHelper") <<
"readDataProducts: Reading product provenance.";
795 prdprov.reset(ReadObjectAny<ProductProvenance>(msg,
"art::ProductProvenance",
"ArtdaqInputHelper::readDataProducts"));
799 TLOG(17,
"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(19,
"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(19,
"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_TRACE(
"ArtdaqInputHelper") <<
"Skipping system Fragment with type " <<
static_cast<int>(type_code) <<
" ( " << translator->GetInstanceNameForType(type_code) <<
" )";
855 TLOG_TRACE(
"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(21,
"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(21,
"ArtdaqInputHelper") <<
"Creating output Fragment storage for label " << label;
882 derived_fragments[label] = std::make_unique<artdaq::Fragments>();
884 TLOG(21,
"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(21,
"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(20,
"ArtdaqInputHelper") <<
"Begin: ArtdaqInputHelper::readNext";
909 if (outputFileCloseNeeded_)
911 outputFileCloseNeeded_ =
false;
914 TLOG(20,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
915 <<
"returning false on outputFileCloseNeeded_";
916 TLOG(20,
"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(22,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
932 if (!fragmentsOnlyMode_ && !eventMap.count(artdaq::Fragment::DataFragmentType))
934 TLOG(20,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext got a message without a DataFragment";
935 shutdownMsgReceived_ =
true;
936 TLOG(22,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
940 if (fragmentsOnlyMode_)
942 if (eventMap.count(artdaq::Fragment::DataFragmentType))
944 TLOG(20,
"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_DEBUG(
"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(20,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
976 <<
"getting message type code ...";
977 msg->ReadULong(msg_type_code_tmp);
978 TLOG(20,
"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(22,
"ArtdaqInputHelper") <<
"End: ArtdaqInputHelper::readNext";
992 if (msg_type_code == 5)
995 shutdownMsgReceived_ =
true;
996 TLOG(21,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
997 <<
"returning false on Shutdown message.";
998 TLOG(21,
"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(22,
"ArtdaqInputHelper") <<
"ArtdaqInputHelper::readNext: "
1016 <<
"returning false on EndRun message.";
1017 TLOG(22,
"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(23,
"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(24,
"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(20,
"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(25,
"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.