SIOVElectronicsCalibProvider.cxx
Go to the documentation of this file.
2 
3 // art/LArSoft libraries
4 #include "cetlib_except/exception.h"
7 
8 #include <fstream>
9 
10 namespace lariov {
11 
12  //constructor
14  DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg")),
15  fEventTimeStamp(0),
16  fCurrentTimeStamp(0) {
17 
18  this->Reconfigure(p);
19  }
20 
22 
23  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
24  fData.Clear();
26  tmp.SetStamp(tmp.Stamp()-1, tmp.SubStamp());
27  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
28 
29  bool UseDB = p.get<bool>("UseDB", false);
30  bool UseFile = p.get<bool>("UseFile", false);
31  std::string fileName = p.get<std::string>("FileName", "");
32 
33  //priority: (1) use db, (2) use table, (3) use defaults
34  //If none are specified, use defaults
35  if ( UseDB ) fDataSource = DataSource::Database;
36  else if (UseFile) fDataSource = DataSource::File;
38 
40  float default_gain = p.get<float>("DefaultGain");
41  float default_gain_err = p.get<float>("DefaultGainErr");
42  float default_st = p.get<float>("DefaultShapingTime");
43  float default_st_err = p.get<float>("DefaultShapingTimeErr");
44 
45  ElectronicsCalib defaultCalib(0);
46 
47  defaultCalib.SetGain(default_gain);
48  defaultCalib.SetGainErr(default_gain_err);
49  defaultCalib.SetShapingTime(default_st);
50  defaultCalib.SetShapingTimeErr(default_st_err);
51  defaultCalib.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
52 
55  for (; itW != geo->end_wire_id(); ++itW) {
56  DBChannelID_t ch = geo->PlaneWireToChannel(*itW);
57  defaultCalib.SetChannel(ch);
58  fData.AddOrReplaceRow(defaultCalib);
59  }
60 
61  }
62  else if (fDataSource == DataSource::File) {
63  cet::search_path sp("FW_SEARCH_PATH");
64  std::string abs_fp = sp.find_file(fileName);
65  std::cout << "Using electronics calibrations from local file: "<<abs_fp<<"\n";
66  std::ifstream file(abs_fp);
67  if (!file) {
68  throw cet::exception("SIOVElectronicsCalibProvider")
69  << "File "<<abs_fp<<" is not found.";
70  }
71 
73  ElectronicsCalib dp(0);
74  while (std::getline(file, line)) {
75  size_t current_comma = line.find(',');
76  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
77  float gain = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
78 
79  current_comma = line.find(',',current_comma+1);
80  float gain_err = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
81 
82  current_comma = line.find(',',current_comma+1);
83  float shaping_time = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
84 
85  current_comma = line.find(',',current_comma+1);
86  float shaping_time_err = std::stof( line.substr(current_comma+1) );
87 
88  CalibrationExtraInfo info("ElectronicsCalib");
89 
90  dp.SetChannel(ch);
91  dp.SetGain(gain);
92  dp.SetGainErr(gain_err);
93  dp.SetShapingTime(shaping_time);
94  dp.SetShapingTimeErr(shaping_time_err);
95  dp.SetExtraInfo(info);
96 
97  fData.AddOrReplaceRow(dp);
98  }
99  }
100  else {
101  std::cout << "Using electronics calibrations from conditions database"<<std::endl;
102  }
103  }
104 
105  // This method saves the time stamp of the latest event.
106 
108  mf::LogInfo("SIOVElectronicsCalibProvider") << "SIOVElectronicsCalibProvider::UpdateTimeStamp called.";
109  fEventTimeStamp = ts;
110  }
111 
112  // Maybe update method cached data (public non-const version).
113 
115 
116  fEventTimeStamp = ts;
117  return DBUpdate(ts);
118  }
119 
120  // Maybe update method cached data (private const version using current event time).
121 
123  return DBUpdate(fEventTimeStamp);
124  }
125 
126  // Maybe update method cached data (private const version).
127  // This is the function that does the actual work of updating data from database.
128 
130 
131  bool result = false;
133 
134  mf::LogInfo("SIOVElectronicsCalibProvider") << "SIOVElectronicsCalibProvider::DBUpdate called with new timestamp.";
135 
136  fCurrentTimeStamp = ts;
137 
138  // Call non-const base class method.
139 
140  result = const_cast<SIOVElectronicsCalibProvider*>(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, shaping_time, shaping_time_err;
151  fFolder->GetNamedChannelData(*it, "gain", gain);
152  fFolder->GetNamedChannelData(*it, "gain_err", gain_err);
153  fFolder->GetNamedChannelData(*it, "shaping_time", shaping_time);
154  fFolder->GetNamedChannelData(*it, "shaping_time_err", shaping_time_err);
155 
156 
157  ElectronicsCalib pg(*it);
158  pg.SetGain( (float)gain );
159  pg.SetGainErr( (float)gain_err );
160  pg.SetShapingTime( (float)shaping_time );
161  pg.SetShapingTimeErr( (float)shaping_time_err );
162  pg.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
163 
164  fData.AddOrReplaceRow(pg);
165  }
166  }
167  }
168 
169  return result;
170  }
171 
173  DBUpdate();
174  return fData.GetRow(ch);
175  }
176 
178  return this->ElectronicsCalibObject(ch).Gain();
179  }
180 
182  return this->ElectronicsCalibObject(ch).GainErr();
183  }
184 
186  return this->ElectronicsCalibObject(ch).ShapingTime();
187  }
188 
190  return this->ElectronicsCalibObject(ch).ShapingTimeErr();
191  }
192 
194  return this->ElectronicsCalibObject(ch).ExtraInfo();
195  }
196 
197 
198 }//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.
static QCString result
void SetStamp(unsigned long stamp, unsigned int substamp=0)
Definition: IOVTimeStamp.h:41
Base forward iterator browsing all wire IDs in the detector.
Definition: GeometryCore.h:587
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
std::uint32_t DBChannelID_t
std::uint64_t DBTimeStamp_t
Retrieves information: electronics calibrations, specifically gain and shaping time.
CalibrationExtraInfo const & ExtraInfo(DBChannelID_t ch) const override
float GainErr(DBChannelID_t ch) const override
art framework interface to geometry description
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.
fileName
Definition: dumpTree.py:9
T get(std::string const &key) const
Definition: ParameterSet.h:271
float ShapingTime(DBChannelID_t ch) const override
p
Definition: test.py:223
void SetChannel(unsigned int ch)
Definition: ChData.h:34
string tmp
Definition: languages.py:63
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.
unsigned long Stamp() const
Definition: IOVTimeStamp.h:37
float Gain(DBChannelID_t ch) const override
void line(double t, double *p, double &x, double &y, double &z)
bool Update(DBTimeStamp_t ts)
Update Snapshot and inherited DBFolder if using database. Return true if updated. ...
std::string find_file(std::string const &filename) const
Definition: search_path.cc:96
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.
void SetShapingTimeErr(float v)
static IOVTimeStamp MaxTimeStamp()
void SetExtraInfo(CalibrationExtraInfo const &info)
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
LArSoft geometry interface.
Definition: ChannelGeo.h:16
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
QTextStream & endl(QTextStream &s)