artdaq  v3_10_01
ArtdaqInputHelper.hh
1 #ifndef ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
2 #define ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
3 
4 #include "art/Framework/Core/Frameworkfwd.h"
5 
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"
17 
18 #include "art/Persistency/Provenance/ProcessHistoryRegistry.h"
19 #include "art_root_io/setup.h"
20 
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"
35 
36 #include "fhiclcpp/ParameterSet.h"
37 #include "fhiclcpp/ParameterSetID.h"
38 #include "fhiclcpp/ParameterSetRegistry.h"
39 #include "artdaq-utilities/Plugins/MakeParameterSet.hh"
40 
41 #include <TBufferFile.h>
42 #include <TClass.h>
43 #include <TList.h>
44 #include <TStreamerInfo.h>
45 
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"
56 
57 #include <sys/time.h>
58 #include <cstdio>
59 #include <iomanip>
60 #include <iostream>
61 #include <list>
62 #include <map>
63 #include <memory>
64 #include <set>
65 #include <sstream>
66 #include <string>
67 #include <unordered_map>
68 #include <utility>
69 #include <vector>
70 
71 namespace art {
72 template<typename U>
74 } // namespace art
75 
90 template<typename U>
92 {
93 public:
97  ArtdaqInputHelper(const ArtdaqInputHelper&) = delete;
98 
104 
109 
116  ArtdaqInputHelper(const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper, art::SourceHelper const& pm);
117 
121  void closeCurrentFile();
122 
127  void readFile(const std::string&, art::FileBlock*& fb);
128 
133  bool hasMoreData() const;
134 
144  bool readNext(art::RunPrincipal* const inR, art::SubRunPrincipal* const inSR, art::RunPrincipal*& outR,
145  art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE);
146 
147 private:
150 
151  void readAndConstructPrincipal(std::unique_ptr<TBufferFile>&, ULong_t, art::RunPrincipal* const,
152  art::SubRunPrincipal* const, art::RunPrincipal*&, art::SubRunPrincipal*&,
153  art::EventPrincipal*&);
154 
155  bool constructPrincipal(artdaq::Fragment::type_t, art::RunPrincipal* const,
156  art::SubRunPrincipal* const, art::RunPrincipal*&, art::SubRunPrincipal*&,
157  art::EventPrincipal*&);
158 
159  template<class T>
160  void readDataProducts(std::list<std::unique_ptr<TBufferFile>>&, T*&);
161 
162  void putInPrincipal(RunPrincipal*&, std::unique_ptr<EDProduct>&&, const BranchDescription&,
163  std::unique_ptr<const ProductProvenance>&&);
164 
165  void putInPrincipal(SubRunPrincipal*&, std::unique_ptr<EDProduct>&&, const BranchDescription&,
166  std::unique_ptr<const ProductProvenance>&&);
167 
168  void putInPrincipal(EventPrincipal*&, std::unique_ptr<EDProduct>&&, const BranchDescription&,
169  std::unique_ptr<const ProductProvenance>&&);
170 
171  void readFragments(std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>> const& eventMap, art::EventPrincipal*& outE);
172 
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;
181  size_t bytesRead;
182  std::chrono::steady_clock::time_point last_read_time;
183 };
184 
185 template<typename U>
186 art::ArtdaqInputHelper<U>::ArtdaqInputHelper(const fhicl::ParameterSet& ps, art::ProductRegistryHelper& helper,
187  art::SourceHelper const& pm)
188  : shutdownMsgReceived_(false)
189  , outputFileCloseNeeded_(false)
190  , pm_(pm)
191  , communicationWrapper_(ps)
192  , productList_()
193  , fragmentsOnlyMode_(false)
194  , pretend_module_name(ps.get<std::string>("raw_data_label", "daq"))
195  , bytesRead(0)
196  , last_read_time(std::chrono::steady_clock::now())
197 {
198  root::setup();
199  // Instantiate ArtdaqSharedMemoryService to set up artdaq Globals and MetricManager
200  art::ServiceHandle<ArtdaqSharedMemoryServiceInterface> shm;
201 
202 #if 0
203  volatile bool loop = true;
204  while (loop)
205  {
206  usleep(1000);
207  }
208 #endif
209 
210  // JCF, May-27-2016
211 
212  // Something will have to be done about the labeling of this class,
213  // since it's just a template class- the user will care about the
214  // specific instantiation when it comes to messages
215 
216  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper") << "Begin: ArtdaqInputHelper::ArtdaqInputHelper("
217  << "const fhicl::ParameterSet& ps, "
218  << "art::ProductRegistryHelper& helper, "
219  << "const art::SourceHelper& pm)";
220 
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";
224 
225  if (!initFrags.empty() && initFrags.front().get()->type() == artdaq::Fragment::EndOfDataFragmentType)
226  {
227  TLOG_ERROR("ArtdaqInputHelper") << "Received EndOfData as first broadcast! This process neveer received any data!";
228  shutdownMsgReceived_ = true;
229  outputFileCloseNeeded_ = true;
230  }
231  else
232  {
233  if (initFrags.empty() || initFrags.back().get()->dataSize() == 0)
234  {
235  TLOG_DEBUG("ArtdaqInputHelper") << "No init message received or zero-size init message: Fragments-only mode activated! This is an EventBuilder!";
236  fragmentsOnlyMode_ = true;
237  }
238  else
239  {
240  std::list<std::unique_ptr<TBufferFile>> msgs;
241  for (auto& initFrag : initFrags)
242  {
243  auto header = initFrag->metadata<artdaq::NetMonHeader>();
244  msgs.emplace_back(new TBufferFile(TBuffer::kRead, header->data_length, initFrag->dataBegin(), kFALSE, nullptr));
245  }
246 
247  std::list<History*> processHistories;
248 
249  for (auto& msg : msgs)
250  {
251  // This first unsigned long is the message type code, ignored here in the constructor
252  ULong_t dummy = 0;
253  msg->ReadULong(dummy);
254 
255  // ELF: 6/11/2019: This code is taken from TSocket::RecvStreamerInfos
256  auto list = dynamic_cast<TList*>(msg->ReadObject(TList::Class()));
257 
258  TIter next(list);
259  TStreamerInfo* info;
260  TObjLink* lnk = list->FirstLink();
261  // First call BuildCheck for regular class
262  while (lnk)
263  {
264  info = dynamic_cast<TStreamerInfo*>(lnk->GetObject());
265  TObject* element = info->GetElements()->UncheckedAt(0);
266  Bool_t isstl = element && strcmp("This", element->GetName()) == 0;
267  if (!isstl)
268  {
269  info->BuildCheck();
270  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper") << "ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() << ", version = " << info->GetClassVersion();
271  }
272  lnk = lnk->Next();
273  }
274  // Then call BuildCheck for stl class
275  lnk = list->FirstLink();
276  while (lnk)
277  {
278  info = dynamic_cast<TStreamerInfo*>(lnk->GetObject());
279  TObject* element = info->GetElements()->UncheckedAt(0);
280  Bool_t isstl = element && strcmp("This", element->GetName()) == 0;
281  if (isstl)
282  {
283  info->BuildCheck();
284  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper") << "ArtdaqInputHelper: importing TStreamerInfo: " << info->GetName() << ", version = " << info->GetClassVersion();
285  }
286  lnk = lnk->Next();
287  }
288  // ELF: 6/11/2019: End TSocket snippet
289 
290  //
291  // Read the ParameterSetRegistry.
292  //
293  ULong_t ps_cnt = 0;
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)
298  {
299  std::string pset_str = ""; // = ReadObjectAny<std::string>(msg, "std::string", "ArtdaqInputHelper::ArtdaqInputHelper");
300  msg->ReadStdString(pset_str);
301 
302  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper") << "ArtdaqInputHelper: parameter set: " << pset_str;
303 
304  fhicl::ParameterSet pset;
305  pset = artdaq::make_pset(pset_str);
306  // Force id calculation.
307  pset.id();
308  fhicl::ParameterSetRegistry::put(pset);
309  }
310  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper") << "ArtdaqInputHelper: finished reading parameter sets.";
311 
312  //
313  // Read the MasterProductRegistry.
314  //
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();
318 
319  bool productListInitialized = productList_ != nullptr;
320  if (!productListInitialized) productList_ = thisProductList;
321  for (auto I = thisProductList->begin(), E = thisProductList->end(); I != E; ++I)
322  {
323 #ifndef __OPTIMIZE__
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()));
328 #endif
329  if (productListInitialized)
330  {
331  productList_->emplace(*I);
332  }
333  }
334 
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");
338  printProcessMap(*phm, "ArtdaqInputHelper's ProcessHistoryMap");
339 
340  ProcessHistoryRegistry::put(*phm);
341  printProcessMap(ProcessHistoryRegistry::get(), "ArtdaqInputHelper's ProcessHistoryRegistry");
342 
343  //
344  // Read the ParentageRegistry.
345  //
346  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper") << "ArtdaqInputHelper: Reading ParentageMap";
347  auto parentageMap = ReadObjectAny<ParentageMap>(msg, "art::ParentageMap", "ArtdaqInputHelper::ArtdaqInputHelper");
348  ParentageRegistry::put(*parentageMap);
349 
350  //
351  // Read the History
352  //
353  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper") << "ArtdaqInputHelper: Reading History";
354  processHistories.push_back(ReadObjectAny<History>(msg, "art::History", "ArtdaqInputHelper::ArtdaqInputHelper"));
355  }
356 
357  // We're going to make a fake History using the collected process histories!
358  art::ProcessHistory fake_process_history;
359  for (auto& hist : processHistories)
360  {
361  auto const& id = hist->processHistoryID();
362  ProcessHistory thisProcessHistory;
363  if (ProcessHistoryRegistry::get(id, thisProcessHistory))
364  {
365  for (auto& conf : thisProcessHistory)
366  fake_process_history.push_back(conf);
367  }
368  }
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)
375  {
376  for (auto& es : hist->eventSelectionIDs())
377  {
378  history_to_use_->addEventSelectionEntry(es);
379  }
380  }
381 
382  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper")
383  << "ArtdaqInputHelper: Product list sz=" << productList_->size();
384 
385  // helper now owns productList_!
386 
387  helper.productList(std::unique_ptr<art::ProductList>(productList_));
388  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper") << "ArtdaqInputHelper: got product list";
389  }
390 
391  helper.reconstitutes<artdaq::detail::RawEventHeader, art::InEvent>(pretend_module_name, "RawEventHeader");
392 
393  if (ps.get<bool>("register_fragment_types", true))
394  {
395  TLOG_DEBUG("ArtdaqInputHelper") << "Registering known Fragment labels from ArtdaqFragmentNamingServiceInterface";
396 
397  art::ServiceHandle<ArtdaqFragmentNamingServiceInterface> translator;
398  helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, translator->GetUnidentifiedInstanceName());
399  // Workaround for #22979
400  helper.reconstitutes<artdaq::Fragments, art::InRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
401  helper.reconstitutes<artdaq::Fragments, art::InSubRun>(pretend_module_name, translator->GetUnidentifiedInstanceName());
402 
403  std::set<std::string> instance_names = translator->GetAllProductInstanceNames();
404  for (const auto& set_iter : instance_names)
405  {
406  helper.reconstitutes<artdaq::Fragments, art::InEvent>(pretend_module_name, set_iter);
407  }
408  }
409  //
410  // Finished with init message.
411  //
412  TLOG(TLVL_DEBUG + 2, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::ArtdaqInputHelper("
413  << "const fhicl::ParameterSet& ps, "
414  << "art::ProductRegistryHelper& helper, "
415  << "const art::SourceHelper& pm)";
416  }
417 }
418 
419 template<typename U>
421 {}
422 
423 template<typename U>
425 {
426  TLOG(11, "ArtdaqInputHelper") << "Begin/End: ArtdaqInputHelper::closeCurrentFile()";
427 }
428 
429 template<typename U>
430 void art::ArtdaqInputHelper<U>::readFile(const std::string&, art::FileBlock*& fb)
431 {
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)";
437 }
438 
439 template<typename U>
441 {
442  TLOG(13, "ArtdaqInputHelper") << "Begin: ArtdaqInputHelper::hasMoreData()";
443  if (shutdownMsgReceived_)
444  {
445  TLOG(13, "ArtdaqInputHelper") << "ArtdaqInputHelper::hasMoreData(): "
446  "returning false on shutdownMsgReceived_.";
447  TLOG(13, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::hasMoreData()";
448  return false;
449  }
450  TLOG(8, "ArtdaqInputHelper") << "ArtdaqInputHelper::hasMoreData(): "
451  "returning true on not shutdownMsgReceived_.";
452  TLOG(13, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::hasMoreData()";
453  return true;
454 }
455 
456 template<typename U>
457 void art::ArtdaqInputHelper<U>::readAndConstructPrincipal(std::unique_ptr<TBufferFile>& msg, ULong_t msg_type_code,
458  art::RunPrincipal* const inR, art::SubRunPrincipal* const inSR,
459  art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR,
460  art::EventPrincipal*& outE)
461 {
462  //
463  // Process the message.
464  //
465  std::unique_ptr<art::RunAuxiliary> run_aux;
466  std::unique_ptr<art::SubRunAuxiliary> subrun_aux;
467  std::unique_ptr<art::EventAuxiliary> event_aux;
468 
469  std::unique_ptr<art::History> history_from_event;
470 
471  // Establish default 'results'
472  outR = nullptr;
473  outSR = nullptr;
474  outE = nullptr;
475 
476  if (msg_type_code == 2)
477  { // EndRun message.
478 
479  TLOG(14, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
480  << "processing EndRun message ...";
481 
482  run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg, "art::RunAuxiliary", "ArtdaqInputHelper::readAndConstructPrincipal"));
483  printProcessHistoryID("readAndConstructPrincipal", run_aux.get());
484 
485  TLOG(14, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
486  << "making flush RunPrincipal ...";
487  outR = pm_.makeRunPrincipal(RunID::flushRun(), run_aux->beginTime());
488 
489  TLOG(14, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
490  << "making flush SubRunPrincipal ...";
491  outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), run_aux->beginTime());
492 
493  TLOG(14, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
494  << "making flush EventPrincipal ...";
495  outE = pm_.makeEventPrincipal(EventID::flushEvent(), run_aux->endTime(), true, EventAuxiliary::Any);
496 
497  TLOG(14, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
498  << "finished processing EndRun message.";
499  }
500  else if (msg_type_code == 3)
501  { // EndSubRun message.
502 
503  TLOG(15, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
504  << "processing EndSubRun message ...";
505 
506  subrun_aux.reset(
507  ReadObjectAny<art::SubRunAuxiliary>(msg, "art::SubRunAuxiliary", "ArtdaqInputHelper::readAndConstructPrincipal"));
508  printProcessHistoryID("readAndConstructPrincipal", subrun_aux.get());
509 
510  TLOG(15, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
511  << "making flush RunPrincipal ...";
512  outR = pm_.makeRunPrincipal(RunID::flushRun(), subrun_aux->beginTime());
513 
514  // 28-Feb-2014, KAB: added the setting of the end time in the *current*
515  // run and subrun. This is how we set the endTime in the RunPrincipal
516  // and SubRunPrincipal objects that are written to the disk file.
517  // This needs to happen here because:
518  // A) setting them in every event doesn't work because the RunPrincipal
519  // only allows us to set an end time value once
520  // B) setting them in the "outputFileCloseNeeded_" block in the readNext()
521  // method below doesn't work because that is too late. When this
522  // method returns an outR with a different run number (flushRun),
523  // the art output system closes the current file then.
524  // C) setting them in the EndRun message block immediately above this
525  // block wouldn't work because a) we're not currently sending endRun
526  // events from the EBs to the AG, and b) because presumably that would
527  // be too late, also.
528 
529  art::Timestamp currentTime = time(nullptr);
530  if (inR != nullptr)
531  {
532  inR->endTime(currentTime);
533  }
534  if (inSR != nullptr)
535  {
536  inSR->endTime(currentTime);
537  }
538 
539  TLOG(15, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
540  << "making flush SubRunPrincipal ...";
541  outSR = pm_.makeSubRunPrincipal(SubRunID::flushSubRun(), subrun_aux->beginTime());
542 
543  TLOG(15, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
544  << "making flush EventPrincipal ...";
545  outE = pm_.makeEventPrincipal(EventID::flushEvent(), subrun_aux->endTime(), true, EventAuxiliary::Any);
546 
547  TLOG(15, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
548  << "finished processing EndSubRun message.";
549  }
550  else if (msg_type_code == 4)
551  { // Event message.
552 
553  TLOG(16, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
554  << "processing Event message ...";
555 
556  run_aux.reset(ReadObjectAny<art::RunAuxiliary>(msg, "art::RunAuxiliary", "ArtdaqInputHelper::readAndConstructPrincipal"));
557  printProcessHistoryID("readAndConstructPrincipal", run_aux.get());
558 
559  subrun_aux.reset(
560  ReadObjectAny<art::SubRunAuxiliary>(msg, "art::SubRunAuxiliary", "ArtdaqInputHelper::readAndConstructPrincipal"));
561  printProcessHistoryID("readAndConstructPrincipal", subrun_aux.get());
562 
563  event_aux.reset(
564  ReadObjectAny<art::EventAuxiliary>(msg, "art::EventAuxiliary", "ArtdaqInputHelper::readAndConstructPrincipal"));
565 
566  history_from_event.reset(ReadObjectAny<art::History>(msg, "art::History", "ArtdaqInputHelper::readAndConstructPrincipal"));
567  printProcessHistoryID("readAndConstructPrincipal", history_from_event.get());
568 
569  // Every event should have a valid history
570  if (!history_from_event->processHistoryID().isValid())
571  {
572  throw art::Exception(art::errors::Unknown) // NOLINT(cert-err60-cpp)
573  << "readAndConstructPrincipal: processHistoryID of history in Event message is invalid!";
574  }
575 
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()))
585  {
586  // New run, either we have no input RunPrincipal, or the
587  // input run number does not match the event run number.
588  TLOG(16, "ArtdaqInputHelper") << "readAndConstructPrincipal: making RunPrincipal ...";
589  outR = pm_.makeRunPrincipal(*run_aux);
590  }
591  art::SubRunID subrun_check(event_aux->run(), event_aux->subRun());
592  if (inSR == nullptr || subrun_check != inSR->subRunID())
593  {
594  // New SubRun, either we have no input SubRunPrincipal, or the
595  // input subRun number does not match the event subRun number.
596  TLOG(16, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
597  << "making SubRunPrincipal ...";
598  outSR = pm_.makeSubRunPrincipal(*subrun_aux);
599  }
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()))
603  {
604  TLOG_WARNING("ArtdaqInputHelper") << "Stored history is not in ProcessHistoryRegistry, this event may have issues!";
605  }
606  outE = pm_.makeEventPrincipal(*event_aux, std::move(historyPtr));
607 
608  TLOG(16, "ArtdaqInputHelper") << "readAndConstructPrincipal: "
609  << "finished processing Event message.";
610  }
611 }
612 
613 template<typename U>
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)
615 {
616  // We return false, indicating we're done reading, if:
617  // 1) we did not obtain an event, because we timed out and were
618  // configured NOT to keep trying after a timeout, or
619  // 2) the event we read was the end-of-data marker: a null
620  // pointer
621  if (firstFragmentType == artdaq::Fragment::EndOfDataFragmentType)
622  {
623  TLOG_DEBUG("ArtdaqInputHelper") << "Received shutdown message, returning false";
624  shutdownMsgReceived_ = true;
625  return false;
626  }
627 
628  auto evtHeader = communicationWrapper_.getEventHeader();
629  if (!evtHeader)
630  {
631  TLOG_ERROR("ArtdaqInputHelper") << "No RawEventHeader received, cannot construct principals!";
632  shutdownMsgReceived_ = true;
633  return false;
634  }
635 
636  // Check the number of fragments in the RawEvent. If we have a single
637  // fragment and that fragment is marked as EndRun or EndSubrun we'll create
638  // the special principals for that.
639  art::Timestamp currentTime = 0;
640 #if 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);
644 #endif
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 << ")";
649  if (retcode == 0)
650  {
651  currentTime = ((hi_res_time.tv_sec & 0xffffffff) << 32) | (hi_res_time.tv_nsec & 0xffffffff);
652  }
653  else
654  {
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;
658  }
659 
660  // make new run if inR is 0 or if the run has changed
661  if (inR == nullptr || inR->run() != evtHeader->run_id)
662  {
663  TLOG(20, "ArtdaqInputHelper") << "Making run principal with run_id " << evtHeader->run_id;
664  outR = pm_.makeRunPrincipal(evtHeader->run_id, currentTime);
665  }
666 
667  if (firstFragmentType == artdaq::Fragment::EndOfRunFragmentType)
668  {
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);
674  return true;
675  }
676  else if (firstFragmentType == artdaq::Fragment::EndOfSubrunFragmentType)
677  {
678  TLOG(20, "ArtdaqInputHelper") << "EndOfSubrunFragment received, creating new Subrun Principal";
679  // Check if inR == 0 or is a new run
680  if (inR == nullptr || inR->run() != evtHeader->run_id)
681  {
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);
686  }
687  else
688  {
689  // If the previous subrun was neither 0 nor flush and was identical with the current
690  // subrun, then it must have been associated with a data event. In that case, we need
691  // to generate a flush event with a valid run but flush subrun and event number in order
692  // to end the subrun.
693  if (inSR != nullptr && !inSR->subRunID().isFlush() && inSR->subRun() == evtHeader->subrun_id)
694  {
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);
699  // If this is either a new or another empty subrun, then generate a flush event with
700  // valid run and subrun numbers but flush event number
701  //} else if(inSR==0 || inSR->id().isFlush()){
702  }
703  else
704  {
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);
709  // Possible error condition
710  //} else {
711  }
712  outR = nullptr;
713  }
714  // outputFileCloseNeeded = true;
715  return true;
716  }
717 
718  // make new subrun if inSR is 0 or if the subrun has changed
719  art::SubRunID subrun_check(evtHeader->run_id, evtHeader->subrun_id);
720  if (inSR == nullptr || subrun_check != inSR->subRunID())
721  {
722  TLOG(20, "ArtdaqInputHelper") << "Making subrun principal with subrun_id " << evtHeader->subrun_id;
723  outSR = pm_.makeSubRunPrincipal(evtHeader->run_id, evtHeader->subrun_id, currentTime);
724  }
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);
727  return true;
728 }
729 
730 template<typename U>
731 template<class T>
732 void art::ArtdaqInputHelper<U>::readDataProducts(std::list<std::unique_ptr<TBufferFile>>& msgs, T*& outPrincipal)
733 {
734  for (auto& msg : msgs)
735  {
736  ULong_t prd_cnt = 0;
737  {
738  TLOG(17, "ArtdaqInputHelper") << "readDataProducts: reading data product count ...";
739  msg->ReadULong(prd_cnt);
740  TLOG(17, "ArtdaqInputHelper") << "readDataProducts: product count: " << prd_cnt;
741  }
742  //
743  // Read the data products.
744  //
745  for (ULong_t I = 0; I < prd_cnt; ++I)
746  {
747  std::unique_ptr<BranchKey> bk;
748  {
749  TLOG(17, "ArtdaqInputHelper") << "readDataProducts: Reading branch key.";
750  bk.reset(ReadObjectAny<BranchKey>(msg, "art::BranchKey", "ArtdaqInputHelper::readDataProducts"));
751  }
752 
753 #ifndef __OPTIMIZE__
754  TLOG(18, "ArtdaqInputHelper") << "readDataProducts: got product class: '" << bk->friendlyClassName_ << "' modlbl: '"
755  << bk->moduleLabel_ << "' instnm: '" << bk->productInstanceName_ << "' procnm: '"
756  << bk->processName_;
757 #endif
758  ProductList::const_iterator iter;
759  {
760  TLOG(17, "ArtdaqInputHelper") << "readDataProducts: looking up product ...";
761  iter = productList_->find(*bk);
762  if (iter == productList_->end())
763  {
764  throw art::Exception(art::errors::ProductNotFound) // NOLINT(cert-err60-cpp)
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";
770  }
771  }
772  // Note: This must be a reference to the unique copy in
773  // the master product registry!
774  const BranchDescription& bd = iter->second;
775  std::unique_ptr<EDProduct> prd;
776  {
777  TLOG(17, "ArtdaqInputHelper") << "readDataProducts: Reading product with wrapped name: " << bd.wrappedName()
778  << ", TClass = " << static_cast<void*>(TClass::GetClass(bd.wrappedName().c_str()));
779 
780  // JCF, May-25-2016
781  // Currently unclear why the templatized version of ReadObjectAny doesn't work here...
782 
783  // prd.reset(ReadObjectAny<EDProduct>(msg, bd.wrappedName()));
784 
785  void* p = msg->ReadObjectAny(TClass::GetClass(bd.wrappedName().c_str()));
786  auto pp = reinterpret_cast<EDProduct*>(p); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
787 
788  TLOG(17, "ArtdaqInputHelper") << "readDataProducts: After ReadObjectAny(prd): p=" << p << ", EDProduct::isPresent: " << pp->isPresent();
789  prd.reset(pp);
790  p = nullptr;
791  }
792  std::unique_ptr<const ProductProvenance> prdprov;
793  {
794  TLOG(17, "ArtdaqInputHelper") << "readDataProducts: Reading product provenance.";
795  prdprov.reset(ReadObjectAny<ProductProvenance>(msg, "art::ProductProvenance", "ArtdaqInputHelper::readDataProducts"));
796  }
797 
798  {
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));
803  }
804  }
805  }
806 }
807 
808 template<typename U>
809 void art::ArtdaqInputHelper<U>::putInPrincipal(RunPrincipal*& rp, std::unique_ptr<EDProduct>&& prd,
810  const BranchDescription& bd,
811  std::unique_ptr<const ProductProvenance>&& prdprov)
812 {
813  rp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forRun(rp->runID())));
814 }
815 
816 template<typename U>
817 void art::ArtdaqInputHelper<U>::putInPrincipal(SubRunPrincipal*& srp, std::unique_ptr<EDProduct>&& prd,
818  const BranchDescription& bd,
819  std::unique_ptr<const ProductProvenance>&& prdprov)
820 {
821  srp->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::forSubRun(srp->subRunID())));
822 }
823 
824 template<typename U>
825 void art::ArtdaqInputHelper<U>::putInPrincipal(EventPrincipal*& ep, std::unique_ptr<EDProduct>&& prd,
826  const BranchDescription& bd,
827  std::unique_ptr<const ProductProvenance>&& prdprov)
828 {
829  TLOG(19, "ArtdaqInputHelper") << "EventPrincipal size before put: " << ep->size();
830 
831  ep->put(bd, std::move(prdprov), std::move(prd), std::make_unique<RangeSet>(RangeSet::invalid()));
832 
833  TLOG(19, "ArtdaqInputHelper") << "EventPrincipal size after put: " << ep->size();
834 }
835 
836 template<typename U>
837 void art::ArtdaqInputHelper<U>::readFragments(std::unordered_map<artdaq::Fragment::type_t, std::unique_ptr<artdaq::Fragments>> const& eventMap, art::EventPrincipal*& outE)
838 {
839  // Now read in Fragments
840  double fragmentLatency = 0;
841  double fragmentLatencyMax = 0.0;
842  size_t fragmentCount = 0;
843 
844  art::ServiceHandle<ArtdaqFragmentNamingServiceInterface> translator;
845 
846  // insert the Fragments of each type into the EventPrincipal
847  for (auto& fragmentTypePair : eventMap)
848  {
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)
851  {
852  TLOG_TRACE("ArtdaqInputHelper") << "Skipping system Fragment with type " << static_cast<int>(type_code) << " ( " << translator->GetInstanceNameForType(type_code) << " )";
853  continue;
854  }
855  TLOG_TRACE("ArtdaqInputHelper") << "type is " << static_cast<int>(type_code) << ", number of fragments is " << fragmentTypePair.second->size();
856 
857  std::unordered_map<std::string, std::unique_ptr<artdaq::Fragments>> derived_fragments;
858  for (auto& frag : *fragmentTypePair.second)
859  {
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);
864 
865  fragmentLatency += latency;
866  fragmentCount++;
867  if (latency > fragmentLatencyMax) fragmentLatencyMax = latency;
868 
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)
873  {
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 << "\".";
878  }
879  if (!derived_fragments.count(label))
880  {
881  TLOG(21, "ArtdaqInputHelper") << "Creating output Fragment storage for label " << label;
882  derived_fragments[label] = std::make_unique<artdaq::Fragments>();
883  }
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));
886  }
887  for (auto& type : derived_fragments)
888  {
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);
891  }
892  }
893  if (metricMan)
894  {
895  metricMan->sendMetric("bytesRead", bytesRead, "B", 3, artdaq::MetricMode::LastPoint);
896 
897  metricMan->sendMetric("ArtdaqInputHelper Latency", fragmentLatency / fragmentCount, "s", 4, artdaq::MetricMode::Average);
898  metricMan->sendMetric("ArtdaqInputHelper Maximum Latency", fragmentLatencyMax, "s", 4, artdaq::MetricMode::Maximum);
899  }
900 }
901 
902 template<typename U>
903 bool art::ArtdaqInputHelper<U>::readNext(art::RunPrincipal* const inR, art::SubRunPrincipal* const inSR,
904  art::RunPrincipal*& outR, art::SubRunPrincipal*& outSR, art::EventPrincipal*& outE)
905 {
906  TLOG(20, "ArtdaqInputHelper") << "Begin: ArtdaqInputHelper::readNext";
907  bool ret = false;
908 
909  if (outputFileCloseNeeded_)
910  {
911  outputFileCloseNeeded_ = false;
912  // Signal that we need the output file closed by returning false,
913  // but answering true to the hasMoreData() query.
914  TLOG(20, "ArtdaqInputHelper") << "ArtdaqInputHelper::readNext: "
915  << "returning false on outputFileCloseNeeded_";
916  TLOG(20, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::readNext";
917  return false;
918  }
919  auto read_start_time = std::chrono::steady_clock::now();
920 
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();
923 
924  if (eventMap.empty())
925  {
926  TLOG(TLVL_ERROR, "ArtdaqInputHelper") << "No Fragments received! Aborting...";
927  shutdownMsgReceived_ = true;
928  TLOG(22, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::readNext";
929  return false;
930  }
931 
932  if (!fragmentsOnlyMode_ && !eventMap.count(artdaq::Fragment::DataFragmentType))
933  {
934  TLOG(20, "ArtdaqInputHelper") << "ArtdaqInputHelper::readNext got a message without a DataFragment";
935  shutdownMsgReceived_ = true;
936  TLOG(22, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::readNext";
937  return false;
938  }
939 
940  if (fragmentsOnlyMode_)
941  {
942  if (eventMap.count(artdaq::Fragment::DataFragmentType))
943  {
944  TLOG(20, "ArtdaqInputHelper") << "ArtdaqInputHelper::readNext unexpectedly got a message with a DataFragment. This art Event will NOT be reconstructed!";
945  }
946 
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))
950  {
951  readFragments(eventMap, outE);
952  ret = true;
953  }
954  else
955  {
956  ret = false;
957  }
958  }
959  else
960  {
961  std::list<std::unique_ptr<TBufferFile>> msgs;
962  for (auto& dataFrag : *(eventMap[artdaq::Fragment::DataFragmentType]))
963  {
964  auto header = dataFrag.metadata<artdaq::NetMonHeader>();
965  msgs.emplace_back(new TBufferFile(TBuffer::kRead, header->data_length, dataFrag.dataBegin(), kFALSE, nullptr));
966  }
967 
968  //
969  // Read message type code.
970  //
971  ULong_t msg_type_code = 0;
972  ULong_t msg_type_code_tmp = 0;
973  for (auto& msg : msgs)
974  {
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;
980 
981  if (msg_type_code == 0)
982  msg_type_code = msg_type_code_tmp;
983  else if (msg_type_code != msg_type_code_tmp)
984  {
985  TLOG(TLVL_ERROR, "ArtdaqInputHelper") << "ArtdaqInputHelper::readNext: Received conflicting message type codes! Aborting...";
986 
987  shutdownMsgReceived_ = true;
988  TLOG(22, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::readNext";
989  return false;
990  }
991  }
992  if (msg_type_code == 5)
993  {
994  // Shutdown message.
995  shutdownMsgReceived_ = true;
996  TLOG(21, "ArtdaqInputHelper") << "ArtdaqInputHelper::readNext: "
997  << "returning false on Shutdown message.";
998  TLOG(21, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::readNext";
999  return false;
1000  }
1001 
1002  for (auto& msg : msgs)
1003  {
1004  readAndConstructPrincipal(msg, msg_type_code, inR, inSR, outR, outSR, outE);
1005  }
1006  //
1007  // Read per-event metadata needed to construct principal.
1008  //
1009  if (msg_type_code == 2)
1010  {
1011  // EndRun message.
1012  // FIXME: We need to merge these into the input RunPrincipal.
1013  readDataProducts(msgs, outR);
1014  // Signal that we should close the input and output file.
1015  TLOG(22, "ArtdaqInputHelper") << "ArtdaqInputHelper::readNext: "
1016  << "returning false on EndRun message.";
1017  TLOG(22, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::readNext";
1018  return false;
1019  }
1020  else if (msg_type_code == 3)
1021  {
1022  // EndSubRun message.
1023  // From the code above, EndRun and EndSubRun messages cause
1024  // the construction of principals that have:
1025  // Run:Subrun:Event=flush:flush:flush.
1026  // This is a problem when you have two neighboring EndSubRuns
1027  // which are both associated with empty subruns because art will
1028  // complain that you a new subrun with a subrun number identical
1029  // to that of the previous subrun. So the solution is to not
1030  // return new principals.
1031  if (inR != nullptr && inSR != nullptr && outR != nullptr && outSR != nullptr)
1032  {
1033  if (inR->runID().isFlush() && inSR->subRunID().isFlush() && outR->runID().isFlush() &&
1034  outSR->subRunID().isFlush())
1035  {
1036  outR = nullptr;
1037  outSR = nullptr;
1038  outputFileCloseNeeded_ = true;
1039  return true;
1040  }
1041  }
1042  // FIXME: We need to merge these into the input SubRunPrincipal.
1043  readDataProducts(msgs, outSR);
1044  // Remember that we should ask for file close next time
1045  // we are called.
1046  outputFileCloseNeeded_ = true;
1047  TLOG(23, "ArtdaqInputHelper") << "readNext: returning true on EndSubRun message.";
1048  ret = true;
1049  }
1050  else if (msg_type_code == 4)
1051  {
1052  // Event message.
1053  readDataProducts(msgs, outE);
1054 
1055  if (eventMap.size() > 1)
1056  {
1057  readFragments(eventMap, outE);
1058  }
1059 
1060  TLOG(24, "ArtdaqInputHelper") << "readNext: returning true on Event message.";
1061  ret = true;
1062  }
1063  }
1064 
1065  if (outE != nullptr)
1066  {
1067  auto artHdrPtr = std::make_unique<artdaq::detail::RawEventHeader>();
1068  auto daqHdrPtr = communicationWrapper_.getEventHeader();
1069 
1070  if (daqHdrPtr != nullptr)
1071  {
1072  memcpy(artHdrPtr.get(), daqHdrPtr.get(), sizeof(artdaq::detail::RawEventHeader));
1073  put_product_in_principal(std::move(artHdrPtr), *outE, pretend_module_name, "RawEventHeader");
1074  }
1075  }
1076 
1077  auto read_finish_time = std::chrono::steady_clock::now();
1078  TLOG(20, "ArtdaqInputHelper") << "readNext: bytesRead=" << bytesRead
1079  << " metricMan=" << static_cast<void*>(metricMan.get());
1080  if (metricMan)
1081  {
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);
1088  }
1089 
1090  TLOG(25, "ArtdaqInputHelper") << "End: ArtdaqInputHelper::readNext ret=" << std::boolalpha << ret;
1091  last_read_time = std::chrono::steady_clock::now();
1092  return ret;
1093 }
1094 
1095 #endif // ARTDAQ_ARTDAQ_ARTMODULES_ARTDAQINPUTHELPER_HH_
void printProcessHistoryID(const std::string &label, const T &object)
Print the processHistoryID from the object.
~ArtdaqInputHelper()
ArtdaqInputHelper Destructor.
bool hasMoreData() const
Whether additional events are expected from the source.
bool readNext(art::RunPrincipal *const inR, art::SubRunPrincipal *const inSR, art::RunPrincipal *&outR, art::SubRunPrincipal *&outSR, art::EventPrincipal *&outE)
Read the next event from the communication wrapper.
void readFile(const std::string &, art::FileBlock *&fb)
Emulate reading a file.
void printProcessMap(const T &mappable, const std::string &description)
Print data from a map-like class.
void closeCurrentFile()
Called by art to close the input source. No-Op.
ArtdaqInputHelper & operator=(const ArtdaqInputHelper &)=delete
Copy Assignment operator is deleted.
Header with length information for NetMonTransport messages.
Definition: NetMonHeader.hh:13
ArtdaqInputHelper(const ArtdaqInputHelper &)=delete
Copy Constructor is deleted.
This template class provides a unified interface for reading data into art.