DetectorPropertiesServiceStandard_service.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // \file DetectorProperties_service.cc
4 //
5 ////////////////////////////////////////////////////////////////////////
6 // Framework includes
7 
8 // LArSoft includes
13 #include "lardata/DetectorInfoServices/ServicePack.h" // lar::extractProviders()
15 
16 // 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>(),
31  lar::providerFrom<detinfo::LArPropertiesService>(),
32  std::set<std::string>({"InheritNumberTimeSamples"})}
33  , fPS{pset}
34  , fInheritNumberTimeSamples{pset.get<bool>("InheritNumberTimeSamples", false)}
35  {
36  reg.sPostOpenFile.watch(this, &DetectorPropertiesServiceStandard::postOpenFile);
37  }
38 
39  //--------------------------------------------------------------------
40  // Callback called after input file is opened.
41 
42  void
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) {
94  ps = fhicl::ParameterSet::make(reinterpret_cast<char const*>(sqlite3_column_text(stmt, 0)));
95  // Is this a DetectorPropertiesService parameter set?
96 
98 
99  // Check NumberTimeSamples
100 
101  auto const newNumberTimeSamples = ps.get<unsigned int>("NumberTimeSamples");
102 
103  // Ignore parameter values that match the current configuration.
104 
105  if (newNumberTimeSamples != fPS.get<unsigned int>("NumberTimeSamples")) {
106  if (nNumberTimeSamples == 0)
107  iNumberTimeSamples = newNumberTimeSamples;
108  else if (newNumberTimeSamples != iNumberTimeSamples) {
109  throw cet::exception(__FUNCTION__)
110  << "Historical values of NumberTimeSamples do not agree: " << iNumberTimeSamples
111  << " " << newNumberTimeSamples << "\n";
112  }
113  ++nNumberTimeSamples;
114  }
115  }
116  }
117 
118  // Done looping over parameter sets.
119  // Now decide which parameters we will actually override.
120 
121  if (nNumberTimeSamples != 0 && iNumberTimeSamples != fProp.NumberTimeSamples()) {
122  mf::LogInfo("DetectorPropertiesServiceStandard")
123  << "Overriding configuration parameter NumberTimeSamples using "
124  "historical value.\n"
125  << " Configured value: " << fProp.NumberTimeSamples() << "\n"
126  << " Historical (used) value: " << iNumberTimeSamples << "\n";
127  fProp.SetNumberTimeSamples(iNumberTimeSamples);
128  }
129  }
130 
131  //--------------------------------------------------------------------
132  // Determine whether a parameter set is a DetectorPropertiesService configuration.
133 
134  bool
136  const fhicl::ParameterSet& ps) const
137  {
138  // This method uses heuristics to determine whether the parameter
139  // set passed as argument is a DetectorPropertiesService configuration
140  // parameter set.
141 
142  return (ps.get<std::string>("service_type", "") == "DetectorPropertiesService") &&
143  (ps.get<std::string>("service_provider", "") == "DetectorPropertiesServiceStandard");
144  }
145 
146 } // namespace detinfo
147 
unsigned int NumberTimeSamples() const override
std::string string
Definition: nybbler.cc:12
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
struct sqlite3_stmt sqlite3_stmt
string filename
Definition: train.py:213
art framework interface to geometry description
DetectorPropertiesServiceStandard(fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)
T get(std::string const &key) const
Definition: ParameterSet.h:271
fhicl::ParameterSet fPS
Original parameter set.
General LArSoft Utilities.
static constexpr double ps
Definition: Units.h:99
bool isDetectorPropertiesServiceStandard(const fhicl::ParameterSet &ps) const
bool fInheritNumberTimeSamples
Flag saying whether to inherit NumberTimeSamples.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)