LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DataFromRunOrService.hh
Go to the documentation of this file.
1 // DataFromRunOrService
2 //
3 // A utility templated function to get data either from the Run record or from
4 // a service. The run record is tried first. If that fails and the service does not
5 // exist, then an exception is thrown. We assume that the service has a const method called
6 // getData() that returns a const refernce to the data type.
7 //
8 //
9 
10 #ifndef artg4tk_util_DataFromRunOrService_hh
11 #define artg4tk_util_DataFromRunOrService_hh
12 
17 
18 #include <string>
19 #include <vector>
20 
21 namespace artg4tk {
22 
23  template <typename DATATYPE, typename SERVICETYPE>
24  const DATATYPE&
26  const std::string& producerLabel,
27  const std::string& instanceLabel)
28  {
29 
30  // Let's try to get the data out of the Run first
31  // std::vector< art::Handle< DATATYPE > > handleVec;
32  // r.getManyByType(handleVec);
33  auto handleVec = r.getMany<DATATYPE>();
34 
35  if (!handleVec.empty()) {
36  // We found something in the run record
37  for (const art::Handle<DATATYPE>& aHandle : handleVec) {
38 
39  // Get the provenance for this handle
40  art::Provenance const* prov = aHandle.provenance();
41 
42  // Check the producer label
43  if (producerLabel.empty() || producerLabel == prov->moduleLabel()) {
44 
45  // Check the instance label
46  if (instanceLabel.empty() || instanceLabel == prov->productInstanceName()) {
47 
48  // We found it!
49  mf::LogDebug("DataFromRunOrService") << "Getting data from Run";
50  return *aHandle;
51  }
52  }
53  }
54  }
55 
56  // If we make it here, then we need to look at the service (throws an exception if the
57  // service does not exist)
58  mf::LogDebug("DataFromRunOrService") << "Getting data from Service";
59  return art::ServiceHandle<SERVICETYPE>()->getData();
60  }
61 
62 }
63 
64 #endif /* artg4tk_util_DataFromRunOrService_hh */
TRandom r
Definition: spectrum.C:23
const DATATYPE & dataFromRunOrService(const art::Run &r, const std::string &producerLabel, const std::string &instanceLabel)
std::string const & productInstanceName() const noexcept
Definition: Provenance.cc:60
Definition: Run.h:37
std::string const & moduleLabel() const noexcept
Definition: Provenance.cc:54
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const