LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DetectorPropertiesServiceStandard.cc
Go to the documentation of this file.
1 //
3 // \file DetectorProperties_service.cc
4 //
6 // Framework includes
7 
8 // LArSoft includes
13 #include "lardata/DetectorInfoServices/ServicePack.h" // lar::extractProviders()
15 
16 // Art includes
17 #include "art_root_io/RootDB/SQLite3Wrapper.h"
18 
19 #include "TFile.h"
20 #include "TTree.h"
21 
22 namespace detinfo {
23 
24  //--------------------------------------------------------------------
26  fhicl::ParameterSet const& pset,
28  : fProp{pset,
29  lar::providerFrom<geo::Geometry>(),
30  lar::providerFrom<detinfo::LArPropertiesService>(),
31  std::set<std::string>({"InheritNumberTimeSamples"})}
32  , fPS{pset}
33  , fInheritNumberTimeSamples{pset.get<bool>("InheritNumberTimeSamples", false)}
34  {
35  reg.sPostOpenFile.watch(this, &DetectorPropertiesServiceStandard::postOpenFile);
36  }
37 
38  //--------------------------------------------------------------------
39  // Callback called after input file is opened.
40 
41  void DetectorPropertiesServiceStandard::postOpenFile(const std::string& filename)
42  {
43  // Use this method to figure out whether to inherit configuration
44  // parameters from previous jobs.
45 
46  // There is no way currently to correlate parameter sets saved in
47  // sqlite RootFileDB with process history (from MetaData tree).
48  // Therefore, we use the approach of scanning every historical
49  // parameter set in RootFileDB, and finding all parameter sets
50  // that appear to be DetectorPropertiesService configurations. If all
51  // historical parameter sets are in agreement about the value of
52  // an inherited parameter, then we accept the historical value,
53  // print a message, and override the configuration parameter. In
54  // cases where the historical configurations are not in agreement
55  // about the value of an inherited parameter, we ignore any
56  // historical parameter values that are the same as the current
57  // configured value of the parameter (that is, we resolve the
58  // conflict in favor of parameters values that are different than
59  // the current configuration). If two or more historical values
60  // differ from the current configuration, throw an exception.
61  // Note that it is possible to give precendence to the current
62  // configuration by disabling inheritance for that configuration
63  // parameter.
64 
65  // Don't do anything if no parameters are supposed to be inherited.
66 
67  if (!fInheritNumberTimeSamples) return;
68 
69  // The only way to access art service metadata from the input file
70  // is to open it as a separate TFile object. Do that now.
71 
72  if (filename.empty()) { return; }
73 
74  std::unique_ptr<TFile> file{TFile::Open(filename.c_str(), "READ")};
75  if (!file) { return; }
76 
77  if (file->IsZombie() || !file->IsOpen()) { return; }
78 
79  // Open the sqlite datatabase.
80 
81  art::SQLite3Wrapper sqliteDB(file.get(), "RootFileDB");
82 
83  // Loop over all stored ParameterSets.
84 
85  unsigned int iNumberTimeSamples = 0; // Combined value of NumberTimeSamples.
86  unsigned int nNumberTimeSamples = 0; // Number of NumberTimeSamples parameters seen.
87 
88  sqlite3_stmt* stmt = nullptr;
89  sqlite3_prepare_v2(sqliteDB, "SELECT PSetBlob from ParameterSets;", -1, &stmt, nullptr);
90  while (sqlite3_step(stmt) == SQLITE_ROW) {
92  ps = fhicl::ParameterSet::make(reinterpret_cast<char const*>(sqlite3_column_text(stmt, 0)));
93  // Is this a DetectorPropertiesService parameter set?
94 
96 
97  // Check NumberTimeSamples
98 
99  auto const newNumberTimeSamples = ps.get<unsigned int>("NumberTimeSamples");
100 
101  // Ignore parameter values that match the current configuration.
102 
103  if (newNumberTimeSamples != fPS.get<unsigned int>("NumberTimeSamples")) {
104  if (nNumberTimeSamples == 0)
105  iNumberTimeSamples = newNumberTimeSamples;
106  else if (newNumberTimeSamples != iNumberTimeSamples) {
107  throw cet::exception(__FUNCTION__)
108  << "Historical values of NumberTimeSamples do not agree: " << iNumberTimeSamples
109  << " " << newNumberTimeSamples << "\n";
110  }
111  ++nNumberTimeSamples;
112  }
113  }
114  }
115 
116  // Done looping over parameter sets.
117  // Now decide which parameters we will actually override.
118 
119  if (nNumberTimeSamples != 0 && iNumberTimeSamples != fProp.NumberTimeSamples()) {
120  mf::LogInfo("DetectorPropertiesServiceStandard")
121  << "Overriding configuration parameter NumberTimeSamples using "
122  "historical value.\n"
123  << " Configured value: " << fProp.NumberTimeSamples() << "\n"
124  << " Historical (used) value: " << iNumberTimeSamples << "\n";
125  fProp.SetNumberTimeSamples(iNumberTimeSamples);
126  }
127  }
128 
129  //--------------------------------------------------------------------
130  // Determine whether a parameter set is a DetectorPropertiesService configuration.
131 
133  const fhicl::ParameterSet& ps) const
134  {
135  // This method uses heuristics to determine whether the parameter
136  // set passed as argument is a DetectorPropertiesService configuration
137  // parameter set.
138 
139  return (ps.get<std::string>("service_type", "") == "DetectorPropertiesService") &&
140  (ps.get<std::string>("service_provider", "") == "DetectorPropertiesServiceStandard");
141  }
142 
143 } // namespace detinfo
Utilities to manage ProviderPack objects with art.
unsigned int NumberTimeSamples() const override
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
DetectorPropertiesServiceStandard(fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)
T get(std::string const &key) const
Definition: ParameterSet.h:314
fhicl::ParameterSet fPS
Original parameter set.
General LArSoft Utilities.
bool isDetectorPropertiesServiceStandard(const fhicl::ParameterSet &ps) const
bool fInheritNumberTimeSamples
Flag saying whether to inherit NumberTimeSamples.
TFile * file
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33