DetectorPropertiesServiceStandardGAr_service.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // \file DetectorProperties_service.cc
4 //
5 ////////////////////////////////////////////////////////////////////////
6 // Framework includes
7 
8 // GArSoft includes
12 #include "Geometry/GeometryGAr.h"
13 #include "DetectorInfo/ServicePack.h" // gar::extractProviders()
17 
18 #include "TFile.h"
19 
20 // Art includes
21 #include "art_root_io/RootDB/SQLite3Wrapper.h"
24 
25 namespace gar {
26  namespace detinfo{
27 
28  //--------------------------------------------------------------------
31  : fInheritNumberTimeSamples(pset.get<bool>("InheritNumberTimeSamples", false))
32  {
33  // Register for callbacks.
34 
37  fProp = std::make_unique<detinfo::DetectorPropertiesStandard>(pset,
42  std::set<std::string>({ "InheritNumberTimeSamples" })
43  );
44 
45  // at this point we need and expect the provider to be fully configured
46  fProp->CheckIfConfigured();
47 
48  // Save the parameter set.
49  fPS = pset;
50 
51  }
52 
53  //--------------------------------------------------------------------
55  {
56  fProp->ValidateAndConfigure(p, { "InheritNumberTimeSamples" });
57 
58  // Save the parameter set.
59  fPS = p;
60 
61  return;
62  }
63 
64  //-------------------------------------------------------------
66  {
67  // Make sure TPC Clock is updated with TimeService (though in principle it shouldn't change
68  fProp->UpdateClocks(gar::providerFrom<detinfo::DetectorClocksServiceGAr>());
69  }
70 
71 
72  //--------------------------------------------------------------------
73  // Callback called after input file is opened.
75  {
76  // Use this method to figure out whether to inherit configuration
77  // parameters from previous jobs.
78 
79  // There is no way currently to correlate parameter sets saved in
80  // sqlite RootFileDB with process history (from MetaData tree).
81  // Therefore, we use the approach of scanning every historical
82  // parameter set in RootFileDB, and finding all parameter sets
83  // that appear to be DetectorPropertiesService configurations. If all
84  // historical parameter sets are in agreement about the value of
85  // an inherited parameter, then we accept the historical value,
86  // print a message, and override the configuration parameter. In
87  // cases where the historical configurations are not in agreement
88  // about the value of an inherited parameter, we ignore any
89  // historical parameter values that are the same as the current
90  // configured value of the parameter (that is, we resolve the
91  // conflict in favor of parameters values that are different than
92  // the current configuration). If two or more historical values
93  // differ from the current configuration, throw an exception.
94  // Note that it is possible to give precendence to the current
95  // configuration by disabling inheritance for that configuration
96  // parameter.
97 
98  // Don't do anything if no parameters are supposed to be inherited.
99 
100  if(!fInheritNumberTimeSamples) return;
101 
102  // The only way to access art service metadata from the input file
103  // is to open it as a separate TFile object. Do that now.
104 
105  if(filename.size() != 0) {
106 
107  TFile* file = (TFile*) new TFile(filename.c_str(), "READ");
108  if(file != 0 && !file->IsZombie() && file->IsOpen()) {
109 
110  // Open the sqlite datatabase.
111 
112  ::art::SQLite3Wrapper sqliteDB(file, "RootFileDB");
113 
114  // Loop over all stored ParameterSets.
115 
116  unsigned int iNumberTimeSamples = 0; // Combined value of NumberTimeSamples.
117  unsigned int nNumberTimeSamples = 0; // Number of NumberTimeSamples parameters seen.
118 
119  sqlite3_stmt * stmt = 0;
120  sqlite3_prepare_v2(sqliteDB, "SELECT PSetBlob from ParameterSets;", -1, &stmt, NULL);
121  while (sqlite3_step(stmt) == SQLITE_ROW) {
122  //fhicl::ParameterSet ps;
123  //fhicl::make_ParameterSet(reinterpret_cast<char const *>(sqlite3_column_text(stmt, 0)), ps);
124  auto ps = fhicl::ParameterSet::make(reinterpret_cast<char const *>(sqlite3_column_text(stmt, 0)));
125 
126  // Is this a DetectorPropertiesService parameter set?
127 
129  if(psok) {
130 
131  // Check NumberTimeSamples
132 
133  // if(fInheritNumberTimeSamples) {
134  unsigned int newNumberTimeSamples = ps.get<unsigned int>("NumberTimeSamples");
135 
136  // Ignore parameter values that match the current configuration.
137 
138  if(newNumberTimeSamples != fPS.get<unsigned int>("NumberTimeSamples")) {
139  if(nNumberTimeSamples == 0)
140  iNumberTimeSamples = newNumberTimeSamples;
141  else if(newNumberTimeSamples != iNumberTimeSamples) {
142  throw cet::exception(__FUNCTION__)
143  << "Historical values of NumberTimeSamples do not agree: "
144  << iNumberTimeSamples << " " << newNumberTimeSamples << "\n" ;
145  }
146  ++nNumberTimeSamples;
147  // }
148  }
149  }
150  }
151 
152  // Done looping over parameter sets.
153  // Now decide which parameters we will actually override.
154 
155  if(// fInheritNumberTimeSamples &&
156  nNumberTimeSamples != 0 &&
157  iNumberTimeSamples != fProp->NumberTimeSamples()) {
158  MF_LOG_INFO("DetectorPropertiesServiceStandardGAr")
159  << "Overriding configuration parameter NumberTimeSamples using historical value.\n"
160  << " Configured value: " << fProp->NumberTimeSamples() << "\n"
161  << " Historical (used) value: " << iNumberTimeSamples;
162 
163  fProp->SetNumberTimeSamples(iNumberTimeSamples);
164  }
165  }
166 
167  // Close file.
168  if(file != 0) {
169  if(file->IsOpen())
170  file->Close();
171  delete file;
172  }
173  }
174 
175  }
176 
177  //--------------------------------------------------------------------
178  // Determine whether a parameter set is a DetectorPropertiesService configuration.
179 
181  {
182  // This method uses heuristics to determine whether the parameter
183  // set passed as argument is a DetectorPropertiesService configuration
184  // parameter set.
185 
186  return
187  (ps.get<std::string>("service_type", "") == "DetectorPropertiesService")
188  && (ps.get<std::string>("service_provider", "") == "DetectorPropertiesServiceStandardGAr")
189  ;
190 #if 0
191  // old heuristics here:
192  std::string s;
193  double d;
194  int i;
195  unsigned int u;
196 
197  bool result = !ps.get_if_present("module_label", s);
198  result = result && ps.get_if_present("TriggerOffset", i);
199  result = result && ps.get_if_present("SamplingRate", d);
200  result = result && ps.get_if_present("NumberTimeSamples", u);
201 
202  return result;
203 #endif // 0
204  }
205 
206  } // namespace detinfo
207 
208 } // gar
209 
bool fInheritNumberTimeSamples
Flag saying whether to inherit NumberTimeSamples.
static QCString result
std::string string
Definition: nybbler.cc:12
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
struct sqlite3_stmt sqlite3_stmt
string filename
Definition: train.py:213
std::unique_ptr< detinfo::DetectorPropertiesStandard > fProp
ProviderPackFromServices< Services... > extractProviders()
Returns a provider pack with providers from specified services.
Definition: ServicePack.h:53
void preProcessEvent(::art::Event const &evt, art::ScheduleContext)
T get(std::string const &key) const
Definition: ParameterSet.h:271
virtual void reconfigure(fhicl::ParameterSet const &pset) override
p
Definition: test.py:223
General LArSoft Utilities.
static constexpr double ps
Definition: Units.h:99
#define MF_LOG_INFO(category)
GlobalSignal< detail::SignalResponseType::FIFO, void(Event const &, ScheduleContext)> sPreProcessEvent
General GArSoft Utilities.
Definition: types.h:32
GlobalSignal< detail::SignalResponseType::LIFO, void(std::string const &)> sPostOpenFile
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
The geometry of one entire detector, as served by art.
Definition: GeometryGAr.h:104
DetectorPropertiesServiceStandardGAr(fhicl::ParameterSet const &pset,::art::ActivityRegistry &reg)
"Standard" implementation of DetectorProperties service
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
art framework interface to geometry description
int bool
Definition: qglobal.h:345
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)