LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
SIOVElectronicsCalibProvider.cxx
Go to the documentation of this file.
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  float default_st = p.get<float>("DefaultShapingTime");
49  float default_st_err = p.get<float>("DefaultShapingTimeErr");
50 
51  ElectronicsCalib defaultCalib(0);
52 
53  defaultCalib.SetGain(default_gain);
54  defaultCalib.SetGainErr(default_gain_err);
55  defaultCalib.SetShapingTime(default_st);
56  defaultCalib.SetShapingTimeErr(default_st_err);
57  defaultCalib.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
58 
60  for (auto const& wid : geo->Iterate<geo::WireID>()) {
61  DBChannelID_t ch = geo->PlaneWireToChannel(wid);
62  defaultCalib.SetChannel(ch);
63  fData.AddOrReplaceRow(defaultCalib);
64  }
65  }
66  else if (fDataSource == DataSource::File) {
67  cet::search_path sp("FW_SEARCH_PATH");
68  std::string abs_fp = sp.find_file(fileName);
69  std::cout << "Using electronics calibrations from local file: " << abs_fp << "\n";
70  std::ifstream file(abs_fp);
71  if (!file) {
72  throw cet::exception("SIOVElectronicsCalibProvider")
73  << "File " << abs_fp << " is not found.";
74  }
75 
76  std::string line;
77  ElectronicsCalib dp(0);
78  while (std::getline(file, line)) {
79  size_t current_comma = line.find(',');
80  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
81  float gain = std::stof(
82  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
83 
84  current_comma = line.find(',', current_comma + 1);
85  float gain_err = std::stof(
86  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
87 
88  current_comma = line.find(',', current_comma + 1);
89  float shaping_time = std::stof(
90  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
91 
92  current_comma = line.find(',', current_comma + 1);
93  float shaping_time_err = std::stof(line.substr(current_comma + 1));
94 
95  CalibrationExtraInfo info("ElectronicsCalib");
96 
97  dp.SetChannel(ch);
98  dp.SetGain(gain);
99  dp.SetGainErr(gain_err);
100  dp.SetShapingTime(shaping_time);
101  dp.SetShapingTimeErr(shaping_time_err);
102  dp.SetExtraInfo(info);
103 
104  fData.AddOrReplaceRow(dp);
105  }
106  }
107  else {
108  std::cout << "Using electronics calibrations from conditions database" << std::endl;
109  }
110  }
111 
112  // This method saves the time stamp of the latest event.
113 
115  {
116  mf::LogInfo("SIOVElectronicsCalibProvider")
117  << "SIOVElectronicsCalibProvider::UpdateTimeStamp called.";
118  fEventTimeStamp = ts;
119  }
120 
121  // Maybe update method cached data (public non-const version).
122 
124  {
125 
126  fEventTimeStamp = ts;
127  return DBUpdate(ts);
128  }
129 
130  // Maybe update method cached data (private const version using current event time).
131 
133  {
134  return DBUpdate(fEventTimeStamp);
135  }
136 
137  // Maybe update method cached data (private const version).
138  // This is the function that does the actual work of updating data from database.
139 
140  bool SIOVElectronicsCalibProvider::DBUpdate(DBTimeStamp_t ts) const
141  {
142 
143  bool result = false;
145 
146  mf::LogInfo("SIOVElectronicsCalibProvider")
147  << "SIOVElectronicsCalibProvider::DBUpdate called with new timestamp.";
148 
149  fCurrentTimeStamp = ts;
150 
151  // Call non-const base class method.
152 
153  result = const_cast<SIOVElectronicsCalibProvider*>(this)->UpdateFolder(ts);
154  if (result) {
155  //DBFolder was updated, so now update the Snapshot
156  fData.Clear();
157  fData.SetIoV(this->Begin(), this->End());
158 
159  std::vector<DBChannelID_t> channels;
160  fFolder->GetChannelList(channels);
161  for (auto it = channels.begin(); it != channels.end(); ++it) {
162 
163  double gain, gain_err, shaping_time, shaping_time_err;
164  fFolder->GetNamedChannelData(*it, "gain", gain);
165  fFolder->GetNamedChannelData(*it, "gain_err", gain_err);
166  fFolder->GetNamedChannelData(*it, "shaping_time", shaping_time);
167  fFolder->GetNamedChannelData(*it, "shaping_time_err", shaping_time_err);
168 
169  ElectronicsCalib pg(*it);
170  pg.SetGain((float)gain);
171  pg.SetGainErr((float)gain_err);
172  pg.SetShapingTime((float)shaping_time);
173  pg.SetShapingTimeErr((float)shaping_time_err);
174  pg.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
175 
176  fData.AddOrReplaceRow(pg);
177  }
178  }
179  }
180 
181  return result;
182  }
183 
185  DBChannelID_t ch) const
186  {
187  DBUpdate();
188  return fData.GetRow(ch);
189  }
190 
191  float SIOVElectronicsCalibProvider::Gain(DBChannelID_t ch) const
192  {
193  return this->ElectronicsCalibObject(ch).Gain();
194  }
195 
196  float SIOVElectronicsCalibProvider::GainErr(DBChannelID_t ch) const
197  {
198  return this->ElectronicsCalibObject(ch).GainErr();
199  }
200 
201  float SIOVElectronicsCalibProvider::ShapingTime(DBChannelID_t ch) const
202  {
203  return this->ElectronicsCalibObject(ch).ShapingTime();
204  }
205 
206  float SIOVElectronicsCalibProvider::ShapingTimeErr(DBChannelID_t ch) const
207  {
208  return this->ElectronicsCalibObject(ch).ShapingTimeErr();
209  }
210 
212  {
213  return this->ElectronicsCalibObject(ch).ExtraInfo();
214  }
215 
216 } //end namespace lariov
float ShapingTimeErr(DBChannelID_t ch) const override
details::range_type< T > Iterate() const
Initializes the specified ID with the ID of the first cryostat.
Definition: GeometryCore.h:541
Class def header for a class SIOVElectronicsCalibProvider.
std::unique_ptr< DBFolder > fFolder
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
Retrieves information: electronics calibrations, specifically gain and shaping time.
CalibrationExtraInfo const & ExtraInfo(DBChannelID_t ch) const override
Float_t tmp
Definition: plot.C:35
float GainErr(DBChannelID_t ch) const override
unsigned long SubStamp() const
Definition: IOVTimeStamp.h:38
const ElectronicsCalib & ElectronicsCalibObject(DBChannelID_t ch) const
Retrieve electronics calibration information.
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
float ShapingTime(DBChannelID_t ch) const override
void SetChannel(unsigned int ch)
Definition: ChData.h:33
const IOVTimeStamp & End() const
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
Filters for channels, events, etc.
raw::ChannelID_t PlaneWireToChannel(WireID const &wireid) const
Returns the ID of the TPC channel connected to the specified wire.
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
unsigned long Stamp() const
Definition: IOVTimeStamp.h:37
float Gain(DBChannelID_t ch) const override
TFile * file
bool Update(DBTimeStamp_t ts)
Update Snapshot and inherited DBFolder if using database. Return true if updated. ...
SIOVElectronicsCalibProvider(fhicl::ParameterSet const &p)
Constructors.
const IOVTimeStamp & Begin() const
Get Timestamp information.
void Reconfigure(fhicl::ParameterSet const &p) override
Reconfigure function called by fhicl constructor.
void SetShapingTimeErr(float v)
static IOVTimeStamp MaxTimeStamp()
void SetExtraInfo(CalibrationExtraInfo const &info)
Namespace collecting geometry-related classes utilities.
bool DBUpdate() const
Do actual database updates.
CalibrationExtraInfo const & ExtraInfo() const
float ShapingTimeErr() const
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33