LArSoft  v10_04_05
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  this->Reconfigure(p);
20  }
21 
23  {
24 
25  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
26  fData.Clear();
28  tmp.SetStamp(tmp.Stamp() - 1, tmp.SubStamp());
29  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
30 
31  bool UseDB = p.get<bool>("UseDB", false);
32  bool UseFile = p.get<bool>("UseFile", false);
33  std::string fileName = p.get<std::string>("FileName", "");
34 
35  //priority: (1) use db, (2) use table, (3) use defaults
36  //If none are specified, use defaults
37  if (UseDB)
39  else if (UseFile)
41  else
43 
45  float default_gain = p.get<float>("DefaultGain");
46  float default_gain_err = p.get<float>("DefaultGainErr");
47  float default_st = p.get<float>("DefaultShapingTime");
48  float default_st_err = p.get<float>("DefaultShapingTimeErr");
49 
50  ElectronicsCalib defaultCalib(0);
51 
52  defaultCalib.SetGain(default_gain);
53  defaultCalib.SetGainErr(default_gain_err);
54  defaultCalib.SetShapingTime(default_st);
55  defaultCalib.SetShapingTimeErr(default_st_err);
56  defaultCalib.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
57 
58  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout const>()->Get();
59  for (auto const& wid : wireReadoutGeom.Iterate<geo::WireID>()) {
60  DBChannelID_t ch = wireReadoutGeom.PlaneWireToChannel(wid);
61  defaultCalib.SetChannel(ch);
62  fData.AddOrReplaceRow(defaultCalib);
63  }
64  }
65  else if (fDataSource == DataSource::File) {
66  cet::search_path sp("FW_SEARCH_PATH");
67  std::string abs_fp = sp.find_file(fileName);
68  std::cout << "Using electronics calibrations from local file: " << abs_fp << "\n";
69  std::ifstream file(abs_fp);
70  if (!file) {
71  throw cet::exception("SIOVElectronicsCalibProvider")
72  << "File " << abs_fp << " is not found.";
73  }
74 
75  std::string line;
76  ElectronicsCalib dp(0);
77  while (std::getline(file, line)) {
78  size_t current_comma = line.find(',');
79  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
80  float gain = std::stof(
81  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
82 
83  current_comma = line.find(',', current_comma + 1);
84  float gain_err = std::stof(
85  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
86 
87  current_comma = line.find(',', current_comma + 1);
88  float shaping_time = std::stof(
89  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
90 
91  current_comma = line.find(',', current_comma + 1);
92  float shaping_time_err = std::stof(line.substr(current_comma + 1));
93 
94  CalibrationExtraInfo info("ElectronicsCalib");
95 
96  dp.SetChannel(ch);
97  dp.SetGain(gain);
98  dp.SetGainErr(gain_err);
99  dp.SetShapingTime(shaping_time);
100  dp.SetShapingTimeErr(shaping_time_err);
101  dp.SetExtraInfo(info);
102 
103  fData.AddOrReplaceRow(dp);
104  }
105  }
106  else {
107  std::cout << "Using electronics calibrations from conditions database" << std::endl;
108  }
109  }
110 
111  // This method saves the time stamp of the latest event.
112 
114  {
115  mf::LogInfo("SIOVElectronicsCalibProvider")
116  << "SIOVElectronicsCalibProvider::UpdateTimeStamp called.";
117  fEventTimeStamp = ts;
118  }
119 
120  // Maybe update method cached data (public non-const version).
121 
123  {
124 
125  fEventTimeStamp = ts;
126  return DBUpdate(ts);
127  }
128 
129  // Maybe update method cached data (private const version using current event time).
130 
132  {
133  return DBUpdate(fEventTimeStamp);
134  }
135 
136  // Maybe update method cached data (private const version).
137  // This is the function that does the actual work of updating data from database.
138 
139  bool SIOVElectronicsCalibProvider::DBUpdate(DBTimeStamp_t ts) const
140  {
141 
142  bool result = false;
144 
145  mf::LogInfo("SIOVElectronicsCalibProvider")
146  << "SIOVElectronicsCalibProvider::DBUpdate called with new timestamp.";
147 
148  fCurrentTimeStamp = ts;
149 
150  // Call non-const base class method.
151 
152  result = const_cast<SIOVElectronicsCalibProvider*>(this)->UpdateFolder(ts);
153  if (result) {
154  //DBFolder was updated, so now update the Snapshot
155  fData.Clear();
156  fData.SetIoV(this->Begin(), this->End());
157 
158  std::vector<DBChannelID_t> channels;
159  fFolder->GetChannelList(channels);
160  for (auto it = channels.begin(); it != channels.end(); ++it) {
161 
162  double gain, gain_err, shaping_time, shaping_time_err;
163  fFolder->GetNamedChannelData(*it, "gain", gain);
164  fFolder->GetNamedChannelData(*it, "gain_err", gain_err);
165  fFolder->GetNamedChannelData(*it, "shaping_time", shaping_time);
166  fFolder->GetNamedChannelData(*it, "shaping_time_err", shaping_time_err);
167 
168  ElectronicsCalib pg(*it);
169  pg.SetGain((float)gain);
170  pg.SetGainErr((float)gain_err);
171  pg.SetShapingTime((float)shaping_time);
172  pg.SetShapingTimeErr((float)shaping_time_err);
173  pg.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
174 
175  fData.AddOrReplaceRow(pg);
176  }
177  }
178  }
179 
180  return result;
181  }
182 
184  DBChannelID_t ch) const
185  {
186  DBUpdate();
187  return fData.GetRow(ch);
188  }
189 
190  float SIOVElectronicsCalibProvider::Gain(DBChannelID_t ch) const
191  {
192  return this->ElectronicsCalibObject(ch).Gain();
193  }
194 
195  float SIOVElectronicsCalibProvider::GainErr(DBChannelID_t ch) const
196  {
197  return this->ElectronicsCalibObject(ch).GainErr();
198  }
199 
200  float SIOVElectronicsCalibProvider::ShapingTime(DBChannelID_t ch) const
201  {
202  return this->ElectronicsCalibObject(ch).ShapingTime();
203  }
204 
205  float SIOVElectronicsCalibProvider::ShapingTimeErr(DBChannelID_t ch) const
206  {
207  return this->ElectronicsCalibObject(ch).ShapingTimeErr();
208  }
209 
211  {
212  return this->ElectronicsCalibObject(ch).ExtraInfo();
213  }
214 
215 } //end namespace lariov
float ShapingTimeErr(DBChannelID_t ch) const override
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.
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
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.
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)
bool DBUpdate() const
Do actual database updates.
CalibrationExtraInfo const & ExtraInfo() const
float ShapingTimeErr() const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33