LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
SIOVElectronicsCalibProvider.cxx
Go to the documentation of this file.
1 #ifndef SIOVELECTRONICSCALIBPROVIDER_CXX
2 #define SIOVELECTRONICSCALIBPROVIDER_CXX
3 
5 #include "WebError.h"
6 
7 // art/LArSoft libraries
8 #include "cetlib_except/exception.h"
11 
12 #include <fstream>
13 
14 namespace lariov {
15 
16  //constructor
18  DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg")),
19  fEventTimeStamp(0),
20  fCurrentTimeStamp(0) {
21 
22  this->Reconfigure(p);
23  }
24 
26 
27  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
28  fData.Clear();
29  IOVTimeStamp tmp = IOVTimeStamp::MaxTimeStamp();
30  tmp.SetStamp(tmp.Stamp()-1, tmp.SubStamp());
31  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
32 
33  bool UseDB = p.get<bool>("UseDB", false);
34  bool UseFile = p.get<bool>("UseFile", false);
35  std::string fileName = p.get<std::string>("FileName", "");
36 
37  //priority: (1) use db, (2) use table, (3) use defaults
38  //If none are specified, use defaults
39  if ( UseDB ) fDataSource = DataSource::Database;
40  else if (UseFile) fDataSource = DataSource::File;
41  else fDataSource = DataSource::Default;
42 
43  if (fDataSource == DataSource::Default) {
44  float default_gain = p.get<float>("DefaultGain");
45  float default_gain_err = p.get<float>("DefaultGainErr");
46  float default_st = p.get<float>("DefaultShapingTime");
47  float default_st_err = p.get<float>("DefaultShapingTimeErr");
48 
49  ElectronicsCalib defaultCalib(0);
50 
51  defaultCalib.SetGain(default_gain);
52  defaultCalib.SetGainErr(default_gain_err);
53  defaultCalib.SetShapingTime(default_st);
54  defaultCalib.SetShapingTimeErr(default_st_err);
55  defaultCalib.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
56 
59  for (; itW != geo->end_wire_id(); ++itW) {
60  DBChannelID_t ch = geo->PlaneWireToChannel(*itW);
61  defaultCalib.SetChannel(ch);
62  fData.AddOrReplaceRow(defaultCalib);
63  }
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( 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( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
85 
86  current_comma = line.find(',',current_comma+1);
87  float shaping_time = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
88 
89  current_comma = line.find(',',current_comma+1);
90  float shaping_time_err = std::stof( line.substr(current_comma+1) );
91 
92  CalibrationExtraInfo info("ElectronicsCalib");
93 
94  dp.SetChannel(ch);
95  dp.SetGain(gain);
96  dp.SetGainErr(gain_err);
97  dp.SetShapingTime(shaping_time);
98  dp.SetShapingTimeErr(shaping_time_err);
99  dp.SetExtraInfo(info);
100 
101  fData.AddOrReplaceRow(dp);
102  }
103  }
104  else {
105  std::cout << "Using electronics calibrations from conditions database"<<std::endl;
106  }
107  }
108 
109  // This method saves the time stamp of the latest event.
110 
112  mf::LogInfo("SIOVElectronicsCalibProvider") << "SIOVElectronicsCalibProvider::UpdateTimeStamp called.";
113  fEventTimeStamp = ts;
114  }
115 
116  // Maybe update method cached data (public non-const version).
117 
118  bool SIOVElectronicsCalibProvider::Update(DBTimeStamp_t ts) {
119 
120  fEventTimeStamp = ts;
121  return DBUpdate(ts);
122  }
123 
124  // Maybe update method cached data (private const version using current event time).
125 
127  return DBUpdate(fEventTimeStamp);
128  }
129 
130  // Maybe update method cached data (private const version).
131  // This is the function that does the actual work of updating data from database.
132 
133  bool SIOVElectronicsCalibProvider::DBUpdate(DBTimeStamp_t ts) const {
134 
135  bool result = false;
136  if (fDataSource == DataSource::Database && ts != fCurrentTimeStamp) {
137 
138  mf::LogInfo("SIOVElectronicsCalibProvider") << "SIOVElectronicsCalibProvider::DBUpdate called with new timestamp.";
139 
140  fCurrentTimeStamp = ts;
141 
142  // Call non-const base class method.
143 
144  result = const_cast<SIOVElectronicsCalibProvider*>(this)->UpdateFolder(ts);
145  if(result) {
146  //DBFolder was updated, so now update the Snapshot
147  fData.Clear();
148  fData.SetIoV(this->Begin(), this->End());
149 
150  std::vector<DBChannelID_t> channels;
151  fFolder->GetChannelList(channels);
152  for (auto it = channels.begin(); it != channels.end(); ++it) {
153 
154  double gain, gain_err, shaping_time, shaping_time_err;
155  fFolder->GetNamedChannelData(*it, "gain", gain);
156  fFolder->GetNamedChannelData(*it, "gain_err", gain_err);
157  fFolder->GetNamedChannelData(*it, "shaping_time", shaping_time);
158  fFolder->GetNamedChannelData(*it, "shaping_time_err", shaping_time_err);
159 
160 
161  ElectronicsCalib pg(*it);
162  pg.SetGain( (float)gain );
163  pg.SetGainErr( (float)gain_err );
164  pg.SetShapingTime( (float)shaping_time );
165  pg.SetShapingTimeErr( (float)shaping_time_err );
166  pg.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
167 
168  fData.AddOrReplaceRow(pg);
169  }
170  }
171  }
172 
173  return result;
174  }
175 
176  const ElectronicsCalib& SIOVElectronicsCalibProvider::ElectronicsCalibObject(DBChannelID_t ch) const {
177  DBUpdate();
178  return fData.GetRow(ch);
179  }
180 
181  float SIOVElectronicsCalibProvider::Gain(DBChannelID_t ch) const {
182  return this->ElectronicsCalibObject(ch).Gain();
183  }
184 
185  float SIOVElectronicsCalibProvider::GainErr(DBChannelID_t ch) const {
186  return this->ElectronicsCalibObject(ch).GainErr();
187  }
188 
189  float SIOVElectronicsCalibProvider::ShapingTime(DBChannelID_t ch) const {
190  return this->ElectronicsCalibObject(ch).ShapingTime();
191  }
192 
193  float SIOVElectronicsCalibProvider::ShapingTimeErr(DBChannelID_t ch) const {
194  return this->ElectronicsCalibObject(ch).ShapingTimeErr();
195  }
196 
197  CalibrationExtraInfo const& SIOVElectronicsCalibProvider::ExtraInfo(DBChannelID_t ch) const {
198  return this->ElectronicsCalibObject(ch).ExtraInfo();
199  }
200 
201 
202 }//end namespace lariov
203 
204 #endif
205 
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.
Base forward iterator browsing all wire IDs in the detector.
Definition: GeometryCore.h:567
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:37
float GainErr(DBChannelID_t ch) const override
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:231
float ShapingTime(DBChannelID_t ch) const override
wire_id_iterator end_wire_id() const
Returns an iterator pointing after the last wire ID in the detector.
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.
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.
wire_id_iterator begin_wire_id() const
Returns an iterator pointing to the first wire ID in the detector.
Namespace collecting geometry-related classes utilities.
Collection of exception classes for WebDBI.
bool DBUpdate() const
Do actual database updates.
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33