DetectorClocksServiceStandard_service.cc
Go to the documentation of this file.
2 // vim: set sw=2 expandtab :
3 
10 #include "art_root_io/RootDB/SQLite3Wrapper.h"
14 #include "cetlib_except/exception.h"
15 #include "fhiclcpp/ParameterSet.h"
19 
20 #include "TFile.h"
21 #include "TTree.h"
22 
23 #include "art_root_io/Inputfwd.h"
24 #include "art_root_io/detail/readMetadata.h"
25 
26 #include <bitset>
27 #include <string>
28 #include <vector>
29 
30 using namespace std;
31 
32 namespace detinfo {
33 
34  DetectorClocksServiceStandard::DetectorClocksServiceStandard(fhicl::ParameterSet const& pset,
36  : fClocks{pset}, fInheritClockConfig{pset.get<bool>("InheritClockConfig")}
37  {
38  reg.sPostOpenFile.watch(this, &DetectorClocksServiceStandard::postOpenFile);
39  reg.sPreBeginRun.watch(this, &DetectorClocksServiceStandard::preBeginRun);
40  }
41 
42  void
44  {
45  // This callback probably is not necessary.
47  }
48 
49  void
51  {
52  if (!fInheritClockConfig) { return; }
53  if (filename.empty()) { return; }
54  std::unique_ptr<TFile> file{TFile::Open(filename.c_str(), "READ")};
55  if (!file || file->IsZombie() || !file->IsOpen()) { return; }
56  std::unique_ptr<TTree> metaDataTree{
57  file->Get<TTree>(art::rootNames::metaDataTreeName().c_str())};
58  if (metaDataTree == nullptr) {
59  throw cet::exception("DetectorClocksServiceStandard",
60  "Input file does not contain a metadata tree!");
61  }
62  auto const fileFormatVersion =
63  art::detail::readMetadata<art::FileFormatVersion>(metaDataTree.get());
65  vector<string> const cfgName(fClocks.ConfigNames());
66  vector<double> const cfgValue(fClocks.ConfigValues());
67  bitset<kConfigTypeMax> config_set;
68  vector<double> config_value(kConfigTypeMax, 0);
69 
70  auto count_configuration_changes =
71  [&cfgName, &config_set, &config_value](fhicl::ParameterSet const& ps) {
72  for (size_t i = 0; i < kConfigTypeMax; ++i) {
73  auto const value_from_file = ps.get<double>(cfgName[i]);
74  if (not config_set[i]) {
75  config_value[i] = value_from_file;
76  config_set[i] = true;
77  }
78  else if (config_value[i] != value_from_file) {
79  throw cet::exception("DetectorClocksServiceStandard")
80  << "Found historical value disagreement for " << cfgName[i] << " ... "
81  << config_value[i] << " != " << value_from_file;
82  }
83  }
84  };
85 
86  if (fileFormatVersion.value_ < 5) {
87  art::ParameterSetMap psetMap;
88  if (!art::detail::readMetadata(metaDataTree.get(), psetMap)) {
89  throw cet::exception("DetectorClocksServiceStandard",
90  "Could not read ParameterSetMap from metadata tree!");
91  }
92 
93  for (auto const& psEntry : psetMap) {
95  ps = fhicl::ParameterSet::make(psEntry.second.pset_);
96  if (!fClocks.IsRightConfig(ps)) { continue; }
97 
98  count_configuration_changes(ps);
99  }
100  }
101  else {
102  art::SQLite3Wrapper sqliteDB(file.get(), "RootFileDB");
103  sqlite3_stmt* stmt{nullptr};
104  sqlite3_prepare_v2(sqliteDB, "SELECT PSetBlob from ParameterSets;", -1, &stmt, nullptr);
105  while (sqlite3_step(stmt) == SQLITE_ROW) {
107  ps = fhicl::ParameterSet::make(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)));
108  if (!fClocks.IsRightConfig(ps)) { continue; }
109 
110  count_configuration_changes(ps);
111  }
112  sqlite3_finalize(stmt);
113  }
114 
115  for (size_t i = 0; i < kConfigTypeMax; ++i) {
116  if (not config_set[i]) continue;
117  if (cfgValue[i] == config_value[i]) continue;
118 
119  cout << "Overriding configuration parameter " << cfgName[i] << " ... " << cfgValue[i]
120  << " (fcl) => " << config_value[i] << " (data file)" << endl;
121  fClocks.SetConfigValue(i, config_value[i]);
122  }
124  } // DetectorClocksServiceStandard::postOpenFile()
125 
126 
128  (art::Event const& e) const
130 
131 
132 } // namespace detinfo
133 
bool IsRightConfig(const fhicl::ParameterSet &ps) const
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
std::vector< std::string > const & ConfigNames() const override
STL namespace.
struct sqlite3_stmt sqlite3_stmt
string filename
Definition: train.py:213
std::vector< double > const & ConfigValues() const override
Definition: Run.h:17
std::string const & metaDataTreeName()
Definition: rootNames.cc:42
const double e
void ApplyParams()
Internal function to apply loaded parameters to member attributes.
General LArSoft Utilities.
static constexpr double ps
Definition: Units.h:99
art service managing detinfo::DetectorClocksStandard.
detinfo::DetectorClocksData detectorClocksStandardDataFor(detinfo::DetectorClocksStandard const &detClocks, Event const &event)
Returns DetectorClocksData tuned on the specified event.
void SetConfigValue(size_t i, double val)
Contains all timing reference information for the detector.
DetectorClocksData DataFor(art::Event const &e) const override
std::map< fhicl::ParameterSetID, ParameterSetBlob > ParameterSetMap
Helper to get clocks data from detinfo::DetectorClocksStandard.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)