LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DetectorClocksServiceStandard.cc
Go to the documentation of this file.
2 // vim: set sw=2 expandtab :
3 
7 
8 #include "TFile.h"
9 // Must precede include of art_root_io/detail/readMetadata.h
10 #include "TTree.h"
11 
17 #include "art_root_io/Inputfwd.h"
18 #include "art_root_io/RootDB/SQLite3Wrapper.h"
19 #include "art_root_io/detail/readMetadata.h"
23 #include "cetlib_except/exception.h"
24 #include "fhiclcpp/ParameterSet.h"
25 
26 #include <bitset>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 
31 using namespace std;
32 
33 namespace detinfo {
34 
35  DetectorClocksServiceStandard::DetectorClocksServiceStandard(fhicl::ParameterSet const& pset,
37  : fClocks{pset}, fInheritClockConfig{pset.get<bool>("InheritClockConfig")}
38  {
39  reg.sPostOpenFile.watch(this, &DetectorClocksServiceStandard::postOpenFile);
40  reg.sPreBeginRun.watch(this, &DetectorClocksServiceStandard::preBeginRun);
41  }
42 
44  {
45  // This callback probably is not necessary.
47  }
48 
49  void DetectorClocksServiceStandard::postOpenFile(string const& filename)
50  {
51  if (!fInheritClockConfig) { return; }
52  if (filename.empty()) { return; }
53  std::unique_ptr<TFile> file{TFile::Open(filename.c_str(), "READ")};
54  if (!file || file->IsZombie() || !file->IsOpen()) { return; }
55  std::unique_ptr<TTree> metaDataTree{
56  file->Get<TTree>(art::rootNames::metaDataTreeName().c_str())};
57  if (metaDataTree == nullptr) {
58  throw cet::exception("DetectorClocksServiceStandard",
59  "Input file does not contain a metadata tree!");
60  }
61  auto const fileFormatVersion =
62  art::detail::readMetadata<art::FileFormatVersion>(metaDataTree.get());
64  vector<string> const cfgName(fClocks.ConfigNames());
65  vector<double> const cfgValue(fClocks.ConfigValues());
66  bitset<kConfigTypeMax> config_set;
67  vector<double> config_value(kConfigTypeMax, 0);
68 
69  auto count_configuration_changes =
70  [&cfgName, &config_set, &config_value](fhicl::ParameterSet const& ps) {
71  for (size_t i = 0; i < kConfigTypeMax; ++i) {
72  auto const value_from_file = ps.get<double>(cfgName[i]);
73  if (not config_set[i]) {
74  config_value[i] = value_from_file;
75  config_set[i] = true;
76  }
77  else if (config_value[i] != value_from_file) {
78  throw cet::exception("DetectorClocksServiceStandard")
79  << "Found historical value disagreement for " << cfgName[i] << " ... "
80  << config_value[i] << " != " << value_from_file;
81  }
82  }
83  };
84 
85  if (fileFormatVersion.value_ < 5) {
86  art::ParameterSetMap psetMap;
87  if (!art::detail::readMetadata(metaDataTree.get(), psetMap)) {
88  throw cet::exception("DetectorClocksServiceStandard",
89  "Could not read ParameterSetMap from metadata tree!");
90  }
91 
92  for (auto const& psEntry : psetMap) {
94  ps = fhicl::ParameterSet::make(psEntry.second.pset_);
95  if (!fClocks.IsRightConfig(ps)) { continue; }
96 
97  count_configuration_changes(ps);
98  }
99  }
100  else {
101  art::SQLite3Wrapper sqliteDB(file.get(), "RootFileDB");
102  sqlite3_stmt* stmt{nullptr};
103  sqlite3_prepare_v2(sqliteDB, "SELECT PSetBlob from ParameterSets;", -1, &stmt, nullptr);
104  while (sqlite3_step(stmt) == SQLITE_ROW) {
106  ps = fhicl::ParameterSet::make(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)));
107  if (!fClocks.IsRightConfig(ps)) { continue; }
108 
109  count_configuration_changes(ps);
110  }
111  sqlite3_finalize(stmt);
112  }
113 
114  for (size_t i = 0; i < kConfigTypeMax; ++i) {
115  if (not config_set[i]) continue;
116  if (cfgValue[i] == config_value[i]) continue;
117 
118  cout << "Overriding configuration parameter " << cfgName[i] << " ... " << cfgValue[i]
119  << " (fcl) => " << config_value[i] << " (data file)" << endl;
120  fClocks.SetConfigValue(i, config_value[i]);
121  }
123  } // DetectorClocksServiceStandard::postOpenFile()
124 
126  {
128  }
129 
130 } // namespace detinfo
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.
std::vector< double > const & ConfigValues() const override
Definition: Run.h:37
std::map< fhicl::ParameterSetID, ParameterSetBlob > ParameterSetMap
std::string const & metaDataTreeName()
Definition: rootNames.cc:42
void ApplyParams()
Internal function to apply loaded parameters to member attributes.
void postOpenFile(std::string const &filename)
General LArSoft Utilities.
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.
TFile * file
DetectorClocksData DataFor(art::Event const &e) const override
Helper to get clocks data from detinfo::DetectorClocksStandard.
Float_t e
Definition: plot.C:35
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33