LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
SIOVPmtGainProvider.cxx
Go to the documentation of this file.
1 #ifndef SIOVPMTGAINPROVIDER_CXX
2 #define SIOVPMTGAINPROVIDER_CXX
3 
4 #include "SIOVPmtGainProvider.h"
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 
47  PmtGain defaultGain(0);
48 
49  defaultGain.SetGain(default_gain);
50  defaultGain.SetGainErr(default_gain_err);
51  defaultGain.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
52 
54  for (unsigned int od=0; od!=geo->NOpDets(); ++od) {
55  if (geo->IsValidOpChannel(od)) {
56  defaultGain.SetChannel(od);
57  fData.AddOrReplaceRow(defaultGain);
58  }
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 pmt gains from local file: "<<abs_fp<<"\n";
66  std::ifstream file(abs_fp);
67  if (!file) {
68  throw cet::exception("SIOVPmtGainProvider")
69  << "File "<<abs_fp<<" is not found.";
70  }
71 
72  std::string line;
73  PmtGain dp(0);
74  while (std::getline(file, line)) {
75  if (line[0] == '#') continue;
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) );
82 
83  CalibrationExtraInfo info("PmtGain");
84 
85  dp.SetChannel(ch);
86  dp.SetGain(gain);
87  dp.SetGainErr(gain_err);
88  dp.SetExtraInfo(info);
89 
90  fData.AddOrReplaceRow(dp);
91  }
92  }
93  else {
94  std::cout << "Using pmt gains from conditions database"<<std::endl;
95  }
96  }
97 
98  // This method saves the time stamp of the latest event.
99 
100  void SIOVPmtGainProvider::UpdateTimeStamp(DBTimeStamp_t ts) {
101  mf::LogInfo("SIOVPmtGainProvider") << "SIOVPmtGainProvider::UpdateTimeStamp called.";
102  fEventTimeStamp = ts;
103  }
104 
105  // Maybe update method cached data (public non-const version).
106 
107  bool SIOVPmtGainProvider::Update(DBTimeStamp_t ts) {
108 
109  fEventTimeStamp = ts;
110  return DBUpdate(ts);
111  }
112 
113  // Maybe update method cached data (private const version using current event time).
114 
116  return DBUpdate(fEventTimeStamp);
117  }
118 
119  // Maybe update method cached data (private const version).
120  // This is the function that does the actual work of updating data from database.
121 
122  bool SIOVPmtGainProvider::DBUpdate(DBTimeStamp_t ts) const {
123 
124  bool result = false;
125  if (fDataSource == DataSource::Database && ts != fCurrentTimeStamp) {
126 
127  mf::LogInfo("SIOVPmtGainProvider") << "SIOVPmtGainProvider::DBUpdate called with new timestamp.";
128 
129  fCurrentTimeStamp = ts;
130 
131  // Call non-const base class method.
132 
133  result = const_cast<SIOVPmtGainProvider*>(this)->UpdateFolder(ts);
134  if(result) {
135  //DBFolder was updated, so now update the Snapshot
136  fData.Clear();
137  fData.SetIoV(this->Begin(), this->End());
138 
139  std::vector<DBChannelID_t> channels;
140  fFolder->GetChannelList(channels);
141  for (auto it = channels.begin(); it != channels.end(); ++it) {
142 
143  double gain, gain_err;
144  fFolder->GetNamedChannelData(*it, "gain", gain);
145  fFolder->GetNamedChannelData(*it, "gain_sigma", gain_err);
146 
147  PmtGain pg(*it);
148  pg.SetGain( (float)gain );
149  pg.SetGainErr( (float)gain_err );
150  pg.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
151 
152  fData.AddOrReplaceRow(pg);
153  }
154  }
155  }
156 
157  return result;
158  }
159 
160  const PmtGain& SIOVPmtGainProvider::PmtGainObject(DBChannelID_t ch) const {
161  DBUpdate();
162  return fData.GetRow(ch);
163  }
164 
165  float SIOVPmtGainProvider::Gain(DBChannelID_t ch) const {
166  return this->PmtGainObject(ch).Gain();
167  }
168 
169  float SIOVPmtGainProvider::GainErr(DBChannelID_t ch) const {
170  return this->PmtGainObject(ch).GainErr();
171  }
172 
173  CalibrationExtraInfo const& SIOVPmtGainProvider::ExtraInfo(DBChannelID_t ch) const {
174  return this->PmtGainObject(ch).ExtraInfo();
175  }
176 
177 
178 }//end namespace lariov
179 
180 #endif
181 
std::unique_ptr< DBFolder > fFolder
bool DBUpdate() const
Do actual database updates.
const PmtGain & PmtGainObject(DBChannelID_t ch) const
Retrieve gain information.
bool Update(DBTimeStamp_t ts)
Update Snapshot and inherited DBFolder if using database. Return true if updated. ...
virtual void Reconfigure(fhicl::ParameterSet const &p)
Configure using fhicl::ParameterSet.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Float_t tmp
Definition: plot.C:37
SIOVPmtGainProvider(fhicl::ParameterSet const &p)
Constructors.
CalibrationExtraInfo const & ExtraInfo(DBChannelID_t ch) const override
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
Retrieves information: pmt gain.
const IOVTimeStamp & End() const
unsigned int NOpDets() const
Number of OpDets in the whole detector.
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
Filters for channels, events, etc.
TFile * file
Class def header for a class SIOVPmtGainProvider.
const IOVTimeStamp & Begin() const
Get Timestamp information.
void Reconfigure(fhicl::ParameterSet const &p) override
Reconfigure function called by fhicl constructor.
float Gain(DBChannelID_t ch) const override
Namespace collecting geometry-related classes utilities.
Collection of exception classes for WebDBI.
float GainErr(DBChannelID_t ch) const override
bool IsValidOpChannel(int opChannel) const
Is this a valid OpChannel number?
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33