All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DetectorPropertiesServiceStandard.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()
14 #include "messagefacility/MessageLogger/MessageLogger.h"
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,
27  art::ActivityRegistry& reg)
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
43  {
44  // Use this method to figure out whether to inherit configuration
45  // parameters from previous jobs.
46 
47  // There is no way currently to correlate parameter sets saved in
48  // sqlite RootFileDB with process history (from MetaData tree).
49  // Therefore, we use the approach of scanning every historical
50  // parameter set in RootFileDB, and finding all parameter sets
51  // that appear to be DetectorPropertiesService configurations. If all
52  // historical parameter sets are in agreement about the value of
53  // an inherited parameter, then we accept the historical value,
54  // print a message, and override the configuration parameter. In
55  // cases where the historical configurations are not in agreement
56  // about the value of an inherited parameter, we ignore any
57  // historical parameter values that are the same as the current
58  // configured value of the parameter (that is, we resolve the
59  // conflict in favor of parameters values that are different than
60  // the current configuration). If two or more historical values
61  // differ from the current configuration, throw an exception.
62  // Note that it is possible to give precendence to the current
63  // configuration by disabling inheritance for that configuration
64  // parameter.
65 
66  // Don't do anything if no parameters are supposed to be inherited.
67 
68  if (!fInheritNumberTimeSamples) return;
69 
70  // The only way to access art service metadata from the input file
71  // is to open it as a separate TFile object. Do that now.
72 
73  if (filename.empty()) { return; }
74 
75  std::unique_ptr<TFile> file{TFile::Open(filename.c_str(), "READ")};
76  if (!file) { return; }
77 
78  if (file->IsZombie() || !file->IsOpen()) { return; }
79 
80  // Open the sqlite datatabase.
81 
82  art::SQLite3Wrapper sqliteDB(file.get(), "RootFileDB");
83 
84  // Loop over all stored ParameterSets.
85 
86  unsigned int iNumberTimeSamples = 0; // Combined value of NumberTimeSamples.
87  unsigned int nNumberTimeSamples = 0; // Number of NumberTimeSamples parameters seen.
88 
89  sqlite3_stmt* stmt = nullptr;
90  sqlite3_prepare_v2(sqliteDB, "SELECT PSetBlob from ParameterSets;", -1, &stmt, nullptr);
91  while (sqlite3_step(stmt) == SQLITE_ROW) {
92  fhicl::ParameterSet ps;
93  ps = fhicl::ParameterSet::make(reinterpret_cast<char const*>(sqlite3_column_text(stmt, 0)));
94  // Is this a DetectorPropertiesService parameter set?
95 
97 
98  // Check NumberTimeSamples
99 
100  auto const newNumberTimeSamples = ps.get<unsigned int>("NumberTimeSamples");
101 
102  // Ignore parameter values that match the current configuration.
103 
104  if (newNumberTimeSamples != fPS.get<unsigned int>("NumberTimeSamples")) {
105  if (nNumberTimeSamples == 0)
106  iNumberTimeSamples = newNumberTimeSamples;
107  else if (newNumberTimeSamples != iNumberTimeSamples) {
108  throw cet::exception(__FUNCTION__)
109  << "Historical values of NumberTimeSamples do not agree: " << iNumberTimeSamples
110  << " " << newNumberTimeSamples << "\n";
111  }
112  ++nNumberTimeSamples;
113  }
114  }
115  }
116 
117  // Done looping over parameter sets.
118  // Now decide which parameters we will actually override.
119 
120  if (nNumberTimeSamples != 0 && iNumberTimeSamples != fProp.NumberTimeSamples()) {
121  mf::LogInfo("DetectorPropertiesServiceStandard")
122  << "Overriding configuration parameter NumberTimeSamples using "
123  "historical value.\n"
124  << " Configured value: " << fProp.NumberTimeSamples() << "\n"
125  << " Historical (used) value: " << iNumberTimeSamples << "\n";
126  fProp.SetNumberTimeSamples(iNumberTimeSamples);
127  }
128  }
129 
130  //--------------------------------------------------------------------
131  // Determine whether a parameter set is a DetectorPropertiesService configuration.
132 
133  bool
135  const fhicl::ParameterSet& ps) const
136  {
137  // This method uses heuristics to determine whether the parameter
138  // set passed as argument is a DetectorPropertiesService configuration
139  // parameter set.
140 
141  return (ps.get<std::string>("service_type", "") == "DetectorPropertiesService") &&
142  (ps.get<std::string>("service_provider", "") == "DetectorPropertiesServiceStandard");
143  }
144 
145 } // namespace detinfo
146 
Utilities to manage ProviderPack objects with art.
unsigned int NumberTimeSamples() const override
* file
Definition: file_to_url.sh:69
BEGIN_PROLOG could also be dds filename
DetectorPropertiesServiceStandard(fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)
fhicl::ParameterSet fPS
Original parameter set.
bool isDetectorPropertiesServiceStandard(const fhicl::ParameterSet &ps) const
bool fInheritNumberTimeSamples
Flag saying whether to inherit NumberTimeSamples.
void postOpenFile(const std::string &filename)
art framework interface to geometry description