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