All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIOVElectronicsCalibProvider.cxx
Go to the documentation of this file.
2 
3 // art/LArSoft libraries
4 #include "art/Framework/Services/Registry/ServiceHandle.h"
5 #include "cetlib_except/exception.h"
7 #include "messagefacility/MessageLogger/MessageLogger.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 
22  void SIOVElectronicsCalibProvider::Reconfigure(fhicl::ParameterSet const& p) {
23 
24  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
25  fData.Clear();
27  tmp.SetStamp(tmp.Stamp()-1, tmp.SubStamp());
28  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
29 
30  bool UseDB = p.get<bool>("UseDB", false);
31  bool UseFile = p.get<bool>("UseFile", false);
32  std::string fileName = p.get<std::string>("FileName", "");
33 
34  //priority: (1) use db, (2) use table, (3) use defaults
35  //If none are specified, use defaults
36  if ( UseDB ) fDataSource = DataSource::Database;
37  else if (UseFile) fDataSource = DataSource::File;
39 
41  float default_gain = p.get<float>("DefaultGain");
42  float default_gain_err = p.get<float>("DefaultGainErr");
43  float default_st = p.get<float>("DefaultShapingTime");
44  float default_st_err = p.get<float>("DefaultShapingTimeErr");
45 
46  ElectronicsCalib defaultCalib(0);
47 
48  defaultCalib.SetGain(default_gain);
49  defaultCalib.SetGainErr(default_gain_err);
50  defaultCalib.SetShapingTime(default_st);
51  defaultCalib.SetShapingTimeErr(default_st_err);
52  defaultCalib.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
53 
54  art::ServiceHandle<geo::Geometry const> geo;
55  geo::wire_id_iterator itW = geo->begin_wire_id();
56  for (; itW != geo->end_wire_id(); ++itW) {
57  DBChannelID_t ch = geo->PlaneWireToChannel(*itW);
58  defaultCalib.SetChannel(ch);
59  fData.AddOrReplaceRow(defaultCalib);
60  }
61 
62  }
63  else if (fDataSource == DataSource::File) {
64  cet::search_path sp("FW_SEARCH_PATH");
65  std::string abs_fp = sp.find_file(fileName);
66  std::cout << "Using electronics calibrations from local file: "<<abs_fp<<"\n";
67  std::ifstream file(abs_fp);
68  if (!file) {
69  throw cet::exception("SIOVElectronicsCalibProvider")
70  << "File "<<abs_fp<<" is not found.";
71  }
72 
73  std::string line;
74  ElectronicsCalib dp(0);
75  while (std::getline(file, line)) {
76  size_t current_comma = line.find(',');
77  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
78  float gain = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
79 
80  current_comma = line.find(',',current_comma+1);
81  float gain_err = 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 shaping_time = 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_err = std::stof( line.substr(current_comma+1) );
88 
89  CalibrationExtraInfo info("ElectronicsCalib");
90 
91  dp.SetChannel(ch);
92  dp.SetGain(gain);
93  dp.SetGainErr(gain_err);
94  dp.SetShapingTime(shaping_time);
95  dp.SetShapingTimeErr(shaping_time_err);
96  dp.SetExtraInfo(info);
97 
98  fData.AddOrReplaceRow(dp);
99  }
100  }
101  else {
102  std::cout << "Using electronics calibrations from conditions database"<<std::endl;
103  }
104  }
105 
106  // This method saves the time stamp of the latest event.
107 
109  mf::LogInfo("SIOVElectronicsCalibProvider") << "SIOVElectronicsCalibProvider::UpdateTimeStamp called.";
110  fEventTimeStamp = ts;
111  }
112 
113  // Maybe update method cached data (public non-const version).
114 
116 
117  fEventTimeStamp = ts;
118  return DBUpdate(ts);
119  }
120 
121  // Maybe update method cached data (private const version using current event time).
122 
124  return DBUpdate(fEventTimeStamp);
125  }
126 
127  // Maybe update method cached data (private const version).
128  // This is the function that does the actual work of updating data from database.
129 
131 
132  bool result = false;
134 
135  mf::LogInfo("SIOVElectronicsCalibProvider") << "SIOVElectronicsCalibProvider::DBUpdate called with new timestamp.";
136 
137  fCurrentTimeStamp = ts;
138 
139  // Call non-const base class method.
140 
141  result = const_cast<SIOVElectronicsCalibProvider*>(this)->UpdateFolder(ts);
142  if(result) {
143  //DBFolder was updated, so now update the Snapshot
144  fData.Clear();
145  fData.SetIoV(this->Begin(), this->End());
146 
147  std::vector<DBChannelID_t> channels;
148  fFolder->GetChannelList(channels);
149  for (auto it = channels.begin(); it != channels.end(); ++it) {
150 
151  double gain, gain_err, shaping_time, shaping_time_err;
152  fFolder->GetNamedChannelData(*it, "gain", gain);
153  fFolder->GetNamedChannelData(*it, "gain_err", gain_err);
154  fFolder->GetNamedChannelData(*it, "shaping_time", shaping_time);
155  fFolder->GetNamedChannelData(*it, "shaping_time_err", shaping_time_err);
156 
157 
158  ElectronicsCalib pg(*it);
159  pg.SetGain( (float)gain );
160  pg.SetGainErr( (float)gain_err );
161  pg.SetShapingTime( (float)shaping_time );
162  pg.SetShapingTimeErr( (float)shaping_time_err );
163  pg.SetExtraInfo(CalibrationExtraInfo("ElectronicsCalib"));
164 
165  fData.AddOrReplaceRow(pg);
166  }
167  }
168  }
169 
170  return result;
171  }
172 
174  DBUpdate();
175  return fData.GetRow(ch);
176  }
177 
179  return this->ElectronicsCalibObject(ch).Gain();
180  }
181 
183  return this->ElectronicsCalibObject(ch).GainErr();
184  }
185 
187  return this->ElectronicsCalibObject(ch).ShapingTime();
188  }
189 
191  return this->ElectronicsCalibObject(ch).ShapingTimeErr();
192  }
193 
195  return this->ElectronicsCalibObject(ch).ExtraInfo();
196  }
197 
198 
199 }//end namespace lariov
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
Base forward iterator browsing all wire IDs in the detector.
Definition: GeometryCore.h:587
std::uint32_t DBChannelID_t
* file
Definition: file_to_url.sh:69
pdgs p
Definition: selectors.fcl:22
float ShapingTimeErr(DBChannelID_t ch) const override
std::uint64_t DBTimeStamp_t
Retrieves information: electronics calibrations, specifically gain and shaping time.
CalibrationExtraInfo const & ExtraInfo(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.
void SetChannel(unsigned int ch)
Definition: ChData.h:34
const IOVTimeStamp & End() const
float GainErr(DBChannelID_t ch) const override
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
float ShapingTime(DBChannelID_t ch) const override
unsigned long Stamp() const
Definition: IOVTimeStamp.h:37
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)
float Gain(DBChannelID_t ch) const override
static IOVTimeStamp MaxTimeStamp()
void SetExtraInfo(CalibrationExtraInfo const &info)
bool DBUpdate() const
Do actual database updates.
CalibrationExtraInfo const & ExtraInfo() const
float ShapingTimeErr() const
art framework interface to geometry description
BEGIN_PROLOG could also be cout