All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
12 #include "art_root_io/Inputfwd.h"
13 #include "art_root_io/RootDB/SQLite3Wrapper.h"
14 #include "art_root_io/detail/readMetadata.h"
15 #include "art/Framework/Principal/Event.h"
16 #include "art/Framework/Principal/Run.h"
17 #include "art/Framework/Services/Registry/ActivityRegistry.h"
18 #include "art/Framework/Services/Registry/ServiceHandle.h"
19 #include "art/Persistency/Provenance/ScheduleContext.h"
20 #include "canvas/Persistency/Provenance/FileFormatVersion.h"
21 #include "canvas/Persistency/Provenance/ParameterSetMap.h"
22 #include "canvas/Persistency/Provenance/rootNames.h"
23 #include "fhiclcpp/ParameterSet.h"
24 #include "cetlib_except/exception.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,
36  art::ActivityRegistry& reg)
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 
43  void
45  {
46  // This callback probably is not necessary.
48  }
49 
50  void
52  {
53  if (!fInheritClockConfig) { return; }
54  if (filename.empty()) { return; }
55  std::unique_ptr<TFile> file{TFile::Open(filename.c_str(), "READ")};
56  if (!file || file->IsZombie() || !file->IsOpen()) { return; }
57  std::unique_ptr<TTree> metaDataTree{
58  file->Get<TTree>(art::rootNames::metaDataTreeName().c_str())};
59  if (metaDataTree == nullptr) {
60  throw cet::exception("DetectorClocksServiceStandard",
61  "Input file does not contain a metadata tree!");
62  }
63  auto const fileFormatVersion =
64  art::detail::readMetadata<art::FileFormatVersion>(metaDataTree.get());
65  fhicl::ParameterSet ps;
66  vector<string> const cfgName(fClocks.ConfigNames());
67  vector<double> const cfgValue(fClocks.ConfigValues());
68  bitset<kConfigTypeMax> config_set;
69  vector<double> config_value(kConfigTypeMax, 0);
70 
71  auto count_configuration_changes =
72  [&cfgName, &config_set, &config_value](fhicl::ParameterSet const& ps) {
73  for (size_t i = 0; i < kConfigTypeMax; ++i) {
74  auto const value_from_file = ps.get<double>(cfgName[i]);
75  if (not config_set[i]) {
76  config_value[i] = value_from_file;
77  config_set[i] = true;
78  }
79  else if (config_value[i] != value_from_file) {
80  throw cet::exception("DetectorClocksServiceStandard")
81  << "Found historical value disagreement for " << cfgName[i] << " ... "
82  << config_value[i] << " != " << value_from_file;
83  }
84  }
85  };
86 
87  if (fileFormatVersion.value_ < 5) {
88  art::ParameterSetMap psetMap;
89  if (!art::detail::readMetadata(metaDataTree.get(), psetMap)) {
90  throw cet::exception("DetectorClocksServiceStandard",
91  "Could not read ParameterSetMap from metadata tree!");
92  }
93 
94  for (auto const& psEntry : psetMap) {
95  fhicl::ParameterSet ps;
96  ps = fhicl::ParameterSet::make(psEntry.second.pset_);
97  if (!fClocks.IsRightConfig(ps)) { continue; }
98 
99  count_configuration_changes(ps);
100  }
101  }
102  else {
103  art::SQLite3Wrapper sqliteDB(file.get(), "RootFileDB");
104  sqlite3_stmt* stmt{nullptr};
105  sqlite3_prepare_v2(sqliteDB, "SELECT PSetBlob from ParameterSets;", -1, &stmt, nullptr);
106  while (sqlite3_step(stmt) == SQLITE_ROW) {
107  fhicl::ParameterSet ps;
108  ps = fhicl::ParameterSet::make(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)));
109  if (!fClocks.IsRightConfig(ps)) { continue; }
110 
111  count_configuration_changes(ps);
112  }
113  sqlite3_finalize(stmt);
114  }
115 
116  for (size_t i = 0; i < kConfigTypeMax; ++i) {
117  if (not config_set[i]) continue;
118  if (cfgValue[i] == config_value[i]) continue;
119 
120  cout << "Overriding configuration parameter " << cfgName[i] << " ... " << cfgValue[i]
121  << " (fcl) => " << config_value[i] << " (data file)" << endl;
122  fClocks.SetConfigValue(i, config_value[i]);
123  }
125  } // DetectorClocksServiceStandard::postOpenFile()
126 
127 
129  (art::Event const& e) const
131 
132 
133 } // namespace detinfo
134 
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
bool IsRightConfig(const fhicl::ParameterSet &ps) const
* file
Definition: file_to_url.sh:69
BEGIN_PROLOG could also be dds filename
std::vector< std::string > const & ConfigNames() const override
void ApplyParams()
Internal function to apply loaded parameters to member attributes.
std::vector< double > const & ConfigValues() const override
void postOpenFile(std::string const &filename)
detinfo::DetectorClocksData detectorClocksStandardDataFor(detinfo::DetectorClocksStandard const &detClocks, Event const &event)
Returns DetectorClocksData tuned on the specified event.
void SetConfigValue(size_t i, double val)
DetectorClocksData DataFor(art::Event const &e) const override
Contains all timing reference information for the detector.
do i e
Helper to get clocks data from detinfo::DetectorClocksStandard.
BEGIN_PROLOG could also be cout