LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
SIOVPmtGainProvider.cxx
Go to the documentation of this file.
1 #include "SIOVPmtGainProvider.h"
2 
3 // art/LArSoft libraries
5 #include "cetlib_except/exception.h"
8 
9 #include <fstream>
10 
11 namespace lariov {
12 
13  //constructor
15  : DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"))
16  , fEventTimeStamp(0)
17  , fCurrentTimeStamp(0)
18  {
19 
20  this->Reconfigure(p);
21  }
22 
24  {
25 
26  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
27  fData.Clear();
29  tmp.SetStamp(tmp.Stamp() - 1, tmp.SubStamp());
30  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
31 
32  bool UseDB = p.get<bool>("UseDB", false);
33  bool UseFile = p.get<bool>("UseFile", false);
34  std::string fileName = p.get<std::string>("FileName", "");
35 
36  //priority: (1) use db, (2) use table, (3) use defaults
37  //If none are specified, use defaults
38  if (UseDB)
40  else if (UseFile)
42  else
44 
46  float default_gain = p.get<float>("DefaultGain");
47  float default_gain_err = p.get<float>("DefaultGainErr");
48 
49  PmtGain defaultGain(0);
50 
51  defaultGain.SetGain(default_gain);
52  defaultGain.SetGainErr(default_gain_err);
53  defaultGain.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
54 
56  for (unsigned int od = 0; od != geo->NOpDets(); ++od) {
57  if (geo->IsValidOpChannel(od)) {
58  defaultGain.SetChannel(od);
59  fData.AddOrReplaceRow(defaultGain);
60  }
61  }
62  }
63  else if (fDataSource == DataSource::File) {
64  cet::search_path sp("FW_SEARCH_PATH");
65  std::string abs_fp = sp.find_file(fileName);
66  std::cout << "Using pmt gains from local file: " << abs_fp << "\n";
67  std::ifstream file(abs_fp);
68  if (!file) {
69  throw cet::exception("SIOVPmtGainProvider") << "File " << abs_fp << " is not found.";
70  }
71 
72  std::string line;
73  PmtGain dp(0);
74  while (std::getline(file, line)) {
75  if (line[0] == '#') continue;
76  size_t current_comma = line.find(',');
77  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
78  float gain = std::stof(
79  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
80 
81  current_comma = line.find(',', current_comma + 1);
82  float gain_err = std::stof(line.substr(current_comma + 1));
83 
84  CalibrationExtraInfo info("PmtGain");
85 
86  dp.SetChannel(ch);
87  dp.SetGain(gain);
88  dp.SetGainErr(gain_err);
89  dp.SetExtraInfo(info);
90 
91  fData.AddOrReplaceRow(dp);
92  }
93  }
94  else {
95  std::cout << "Using pmt gains from conditions database" << std::endl;
96  }
97  }
98 
99  // This method saves the time stamp of the latest event.
100 
102  {
103  mf::LogInfo("SIOVPmtGainProvider") << "SIOVPmtGainProvider::UpdateTimeStamp called.";
104  fEventTimeStamp = ts;
105  }
106 
107  // Maybe update method cached data (public non-const version).
108 
109  bool SIOVPmtGainProvider::Update(DBTimeStamp_t ts)
110  {
111 
112  fEventTimeStamp = ts;
113  return DBUpdate(ts);
114  }
115 
116  // Maybe update method cached data (private const version using current event time).
117 
119  {
120  return DBUpdate(fEventTimeStamp);
121  }
122 
123  // Maybe update method cached data (private const version).
124  // This is the function that does the actual work of updating data from database.
125 
126  bool SIOVPmtGainProvider::DBUpdate(DBTimeStamp_t ts) const
127  {
128 
129  bool result = false;
131 
132  mf::LogInfo("SIOVPmtGainProvider")
133  << "SIOVPmtGainProvider::DBUpdate called with new timestamp.";
134 
135  fCurrentTimeStamp = ts;
136 
137  // Call non-const base class method.
138 
139  result = const_cast<SIOVPmtGainProvider*>(this)->UpdateFolder(ts);
140  if (result) {
141  //DBFolder was updated, so now update the Snapshot
142  fData.Clear();
143  fData.SetIoV(this->Begin(), this->End());
144 
145  std::vector<DBChannelID_t> channels;
146  fFolder->GetChannelList(channels);
147  for (auto it = channels.begin(); it != channels.end(); ++it) {
148 
149  double gain, gain_err;
150  fFolder->GetNamedChannelData(*it, "gain", gain);
151  fFolder->GetNamedChannelData(*it, "gain_sigma", gain_err);
152 
153  PmtGain pg(*it);
154  pg.SetGain((float)gain);
155  pg.SetGainErr((float)gain_err);
156  pg.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
157 
158  fData.AddOrReplaceRow(pg);
159  }
160  }
161  }
162 
163  return result;
164  }
165 
166  const PmtGain& SIOVPmtGainProvider::PmtGainObject(DBChannelID_t ch) const
167  {
168  DBUpdate();
169  return fData.GetRow(ch);
170  }
171 
172  float SIOVPmtGainProvider::Gain(DBChannelID_t ch) const
173  {
174  return this->PmtGainObject(ch).Gain();
175  }
176 
177  float SIOVPmtGainProvider::GainErr(DBChannelID_t ch) const
178  {
179  return this->PmtGainObject(ch).GainErr();
180  }
181 
183  {
184  return this->PmtGainObject(ch).ExtraInfo();
185  }
186 
187 } //end namespace lariov
float GainErr() const
Definition: PmtGain.h:34
std::unique_ptr< DBFolder > fFolder
bool DBUpdate() const
Do actual database updates.
const PmtGain & PmtGainObject(DBChannelID_t ch) const
Retrieve gain information.
bool Update(DBTimeStamp_t ts)
Update Snapshot and inherited DBFolder if using database. Return true if updated. ...
virtual void Reconfigure(fhicl::ParameterSet const &p)
Configure using fhicl::ParameterSet.
void SetStamp(unsigned long stamp, unsigned int substamp=0)
Definition: IOVTimeStamp.h:41
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void SetExtraInfo(CalibrationExtraInfo const &info)
Definition: PmtGain.h:39
Float_t tmp
Definition: plot.C:35
SIOVPmtGainProvider(fhicl::ParameterSet const &p)
Constructors.
CalibrationExtraInfo const & ExtraInfo(DBChannelID_t ch) const override
unsigned long SubStamp() const
Definition: IOVTimeStamp.h:38
void SetGainErr(float v)
Definition: PmtGain.h:38
bool UpdateFolder(DBTimeStamp_t ts)
Return true if fFolder is successfully updated.
parameter set interface
T get(std::string const &key) const
Definition: ParameterSet.h:314
void SetGain(float v)
Definition: PmtGain.h:37
Retrieves information: pmt gain.
void SetChannel(unsigned int ch)
Definition: ChData.h:33
const IOVTimeStamp & End() const
unsigned int NOpDets() const
Number of OpDets in the whole detector.
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
float Gain() const
Definition: PmtGain.h:33
Filters for channels, events, etc.
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
unsigned long Stamp() const
Definition: IOVTimeStamp.h:37
CalibrationExtraInfo const & ExtraInfo() const
Definition: PmtGain.h:35
TFile * file
Class def header for a class SIOVPmtGainProvider.
const IOVTimeStamp & Begin() const
Get Timestamp information.
void Reconfigure(fhicl::ParameterSet const &p) override
Reconfigure function called by fhicl constructor.
float Gain(DBChannelID_t ch) const override
static IOVTimeStamp MaxTimeStamp()
Namespace collecting geometry-related classes utilities.
float GainErr(DBChannelID_t ch) const override
bool IsValidOpChannel(int opChannel) const
Is this a valid OpChannel number?
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33