LArSoft  v10_04_05
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"
9 
10 #include <fstream>
11 
12 namespace lariov {
13 
14  //constructor
16  : DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"))
17  , fEventTimeStamp(0)
18  , fCurrentTimeStamp(0)
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  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout const>()->Get();
57  for (unsigned int od = 0; od != geo->NOpDets(); ++od) {
58  if (wireReadoutGeom.IsValidOpChannel(od)) {
59  defaultGain.SetChannel(od);
60  fData.AddOrReplaceRow(defaultGain);
61  }
62  }
63  }
64  else if (fDataSource == DataSource::File) {
65  cet::search_path sp("FW_SEARCH_PATH");
66  std::string abs_fp = sp.find_file(fileName);
67  std::cout << "Using pmt gains from local file: " << abs_fp << "\n";
68  std::ifstream file(abs_fp);
69  if (!file) {
70  throw cet::exception("SIOVPmtGainProvider") << "File " << abs_fp << " is not found.";
71  }
72 
73  std::string line;
74  PmtGain dp(0);
75  while (std::getline(file, line)) {
76  if (line[0] == '#') continue;
77  size_t current_comma = line.find(',');
78  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
79  float gain = std::stof(
80  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
81 
82  current_comma = line.find(',', current_comma + 1);
83  float gain_err = std::stof(line.substr(current_comma + 1));
84 
85  CalibrationExtraInfo info("PmtGain");
86 
87  dp.SetChannel(ch);
88  dp.SetGain(gain);
89  dp.SetGainErr(gain_err);
90  dp.SetExtraInfo(info);
91 
92  fData.AddOrReplaceRow(dp);
93  }
94  }
95  else {
96  std::cout << "Using pmt gains from conditions database" << std::endl;
97  }
98  }
99 
100  // This method saves the time stamp of the latest event.
101 
103  {
104  mf::LogInfo("SIOVPmtGainProvider") << "SIOVPmtGainProvider::UpdateTimeStamp called.";
105  fEventTimeStamp = ts;
106  }
107 
108  // Maybe update method cached data (public non-const version).
109 
110  bool SIOVPmtGainProvider::Update(DBTimeStamp_t ts)
111  {
112 
113  fEventTimeStamp = ts;
114  return DBUpdate(ts);
115  }
116 
117  // Maybe update method cached data (private const version using current event time).
118 
120  {
121  return DBUpdate(fEventTimeStamp);
122  }
123 
124  // Maybe update method cached data (private const version).
125  // This is the function that does the actual work of updating data from database.
126 
127  bool SIOVPmtGainProvider::DBUpdate(DBTimeStamp_t ts) const
128  {
129 
130  bool result = false;
132 
133  mf::LogInfo("SIOVPmtGainProvider")
134  << "SIOVPmtGainProvider::DBUpdate called with new timestamp.";
135 
136  fCurrentTimeStamp = ts;
137 
138  // Call non-const base class method.
139 
140  result = const_cast<SIOVPmtGainProvider*>(this)->UpdateFolder(ts);
141  if (result) {
142  //DBFolder was updated, so now update the Snapshot
143  fData.Clear();
144  fData.SetIoV(this->Begin(), this->End());
145 
146  std::vector<DBChannelID_t> channels;
147  fFolder->GetChannelList(channels);
148  for (auto it = channels.begin(); it != channels.end(); ++it) {
149 
150  double gain, gain_err;
151  fFolder->GetNamedChannelData(*it, "gain", gain);
152  fFolder->GetNamedChannelData(*it, "gain_sigma", gain_err);
153 
154  PmtGain pg(*it);
155  pg.SetGain((float)gain);
156  pg.SetGainErr((float)gain_err);
157  pg.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
158 
159  fData.AddOrReplaceRow(pg);
160  }
161  }
162  }
163 
164  return result;
165  }
166 
167  const PmtGain& SIOVPmtGainProvider::PmtGainObject(DBChannelID_t ch) const
168  {
169  DBUpdate();
170  return fData.GetRow(ch);
171  }
172 
173  float SIOVPmtGainProvider::Gain(DBChannelID_t ch) const
174  {
175  return this->PmtGainObject(ch).Gain();
176  }
177 
178  float SIOVPmtGainProvider::GainErr(DBChannelID_t ch) const
179  {
180  return this->PmtGainObject(ch).GainErr();
181  }
182 
184  {
185  return this->PmtGainObject(ch).ExtraInfo();
186  }
187 
188 } //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
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
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()
ROOT libraries.
float GainErr(DBChannelID_t ch) const override
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33