LArSoft  v06_85_00
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"
10 
11 
12 #include <fstream>
13 
14 namespace lariov {
15 
16  //constructor
18  DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg")) {
19 
20  this->Reconfigure(p);
21  }
22 
24 
25  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
26  fData.Clear();
27  IOVTimeStamp tmp = IOVTimeStamp::MaxTimeStamp();
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 ) fDataSource = DataSource::Database;
38  else if (UseFile) fDataSource = DataSource::File;
39  else fDataSource = DataSource::Default;
40 
41  if (fDataSource == DataSource::Default) {
42  float default_gain = p.get<float>("DefaultGain");
43  float default_gain_err = p.get<float>("DefaultGainErr");
44  float default_st = p.get<float>("DefaultShapingTime");
45  float default_st_err = p.get<float>("DefaultShapingTimeErr");
46 
47  ElectronicsCalib defaultCalib(0);
48 
49  defaultCalib.SetGain(default_gain);
50  defaultCalib.SetGainErr(default_gain_err);
51  defaultCalib.SetShapingTime(default_st);
52  defaultCalib.SetShapingTimeErr(default_st_err);
53  defaultCalib.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
54 
57  for (; itW != geo->end_wire_id(); ++itW) {
58  DBChannelID_t ch = geo->PlaneWireToChannel(*itW);
59  defaultCalib.SetChannel(ch);
60  fData.AddOrReplaceRow(defaultCalib);
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 electronics calibrations from local file: "<<abs_fp<<"\n";
68  std::ifstream file(abs_fp);
69  if (!file) {
70  throw cet::exception("SIOVElectronicsCalibProvider")
71  << "File "<<abs_fp<<" is not found.";
72  }
73 
74  std::string line;
75  ElectronicsCalib dp(0);
76  while (std::getline(file, line)) {
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( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
80 
81  current_comma = line.find(',',current_comma+1);
82  float gain_err = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
83 
84  current_comma = line.find(',',current_comma+1);
85  float shaping_time = std::stof( 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_err = std::stof( line.substr(current_comma+1) );
89 
90  CalibrationExtraInfo info("ElectronicsCalib");
91 
92  dp.SetChannel(ch);
93  dp.SetGain(gain);
94  dp.SetGainErr(gain_err);
95  dp.SetShapingTime(shaping_time);
96  dp.SetShapingTimeErr(shaping_time_err);
97  dp.SetExtraInfo(info);
98 
99  fData.AddOrReplaceRow(dp);
100  }
101  }
102  else {
103  std::cout << "Using electronics calibrations from conditions database"<<std::endl;
104  }
105  }
106 
107  bool SIOVElectronicsCalibProvider::Update(DBTimeStamp_t ts) {
108 
109  if (fDataSource != DataSource::Database) return false;
110 
111  if (!this->UpdateFolder(ts)) return false;
112 
113  //DBFolder was updated, so now update the Snapshot
114  fData.Clear();
115  fData.SetIoV(this->Begin(), this->End());
116 
117  std::vector<DBChannelID_t> channels;
118  fFolder->GetChannelList(channels);
119  for (auto it = channels.begin(); it != channels.end(); ++it) {
120 
121  double gain, gain_err, shaping_time, shaping_time_err;
122  fFolder->GetNamedChannelData(*it, "gain", gain);
123  fFolder->GetNamedChannelData(*it, "gain_err", gain_err);
124  fFolder->GetNamedChannelData(*it, "shaping_time", shaping_time);
125  fFolder->GetNamedChannelData(*it, "shaping_time_err", shaping_time_err);
126 
127 
128  ElectronicsCalib pg(*it);
129  pg.SetGain( (float)gain );
130  pg.SetGainErr( (float)gain_err );
131  pg.SetShapingTime( (float)shaping_time );
132  pg.SetShapingTimeErr( (float)shaping_time_err );
133  pg.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
134 
135  fData.AddOrReplaceRow(pg);
136  }
137 
138  return true;
139  }
140 
141  const ElectronicsCalib& SIOVElectronicsCalibProvider::ElectronicsCalibObject(DBChannelID_t ch) const {
142  return fData.GetRow(ch);
143  }
144 
145  float SIOVElectronicsCalibProvider::Gain(DBChannelID_t ch) const {
146  return this->ElectronicsCalibObject(ch).Gain();
147  }
148 
149  float SIOVElectronicsCalibProvider::GainErr(DBChannelID_t ch) const {
150  return this->ElectronicsCalibObject(ch).GainErr();
151  }
152 
153  float SIOVElectronicsCalibProvider::ShapingTime(DBChannelID_t ch) const {
154  return this->ElectronicsCalibObject(ch).ShapingTime();
155  }
156 
157  float SIOVElectronicsCalibProvider::ShapingTimeErr(DBChannelID_t ch) const {
158  return this->ElectronicsCalibObject(ch).ShapingTimeErr();
159  }
160 
161  CalibrationExtraInfo const& SIOVElectronicsCalibProvider::ExtraInfo(DBChannelID_t ch) const {
162  return this->ElectronicsCalibObject(ch).ExtraInfo();
163  }
164 
165 
166 }//end namespace lariov
167 
168 #endif
169 
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
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
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.
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33