SIOVPmtGainProvider.cxx
Go to the documentation of this file.
1 #include "SIOVPmtGainProvider.h"
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 
43  PmtGain defaultGain(0);
44 
45  defaultGain.SetGain(default_gain);
46  defaultGain.SetGainErr(default_gain_err);
47  defaultGain.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
48 
50  for (unsigned int od=0; od!=geo->NOpDets(); ++od) {
51  if (geo->IsValidOpChannel(od)) {
52  defaultGain.SetChannel(od);
53  fData.AddOrReplaceRow(defaultGain);
54  }
55  }
56 
57  }
58  else if (fDataSource == DataSource::File) {
59  cet::search_path sp("FW_SEARCH_PATH");
60  std::string abs_fp = sp.find_file(fileName);
61  std::cout << "Using pmt gains from local file: "<<abs_fp<<"\n";
62  std::ifstream file(abs_fp);
63  if (!file) {
64  throw cet::exception("SIOVPmtGainProvider")
65  << "File "<<abs_fp<<" is not found.";
66  }
67 
69  PmtGain dp(0);
70  while (std::getline(file, line)) {
71  if (line[0] == '#') continue;
72  size_t current_comma = line.find(',');
73  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
74  float gain = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
75 
76  current_comma = line.find(',',current_comma+1);
77  float gain_err = std::stof( line.substr(current_comma+1) );
78 
79  CalibrationExtraInfo info("PmtGain");
80 
81  dp.SetChannel(ch);
82  dp.SetGain(gain);
83  dp.SetGainErr(gain_err);
84  dp.SetExtraInfo(info);
85 
86  fData.AddOrReplaceRow(dp);
87  }
88  }
89  else {
90  std::cout << "Using pmt gains from conditions database"<<std::endl;
91  }
92  }
93 
94  // This method saves the time stamp of the latest event.
95 
97  mf::LogInfo("SIOVPmtGainProvider") << "SIOVPmtGainProvider::UpdateTimeStamp called.";
98  fEventTimeStamp = ts;
99  }
100 
101  // Maybe update method cached data (public non-const version).
102 
104 
105  fEventTimeStamp = ts;
106  return DBUpdate(ts);
107  }
108 
109  // Maybe update method cached data (private const version using current event time).
110 
112  return DBUpdate(fEventTimeStamp);
113  }
114 
115  // Maybe update method cached data (private const version).
116  // This is the function that does the actual work of updating data from database.
117 
119 
120  bool result = false;
122 
123  mf::LogInfo("SIOVPmtGainProvider") << "SIOVPmtGainProvider::DBUpdate called with new timestamp.";
124 
125  fCurrentTimeStamp = ts;
126 
127  // Call non-const base class method.
128 
129  result = const_cast<SIOVPmtGainProvider*>(this)->UpdateFolder(ts);
130  if(result) {
131  //DBFolder was updated, so now update the Snapshot
132  fData.Clear();
133  fData.SetIoV(this->Begin(), this->End());
134 
135  std::vector<DBChannelID_t> channels;
136  fFolder->GetChannelList(channels);
137  for (auto it = channels.begin(); it != channels.end(); ++it) {
138 
139  double gain, gain_err;
140  fFolder->GetNamedChannelData(*it, "gain", gain);
141  fFolder->GetNamedChannelData(*it, "gain_sigma", gain_err);
142 
143  PmtGain pg(*it);
144  pg.SetGain( (float)gain );
145  pg.SetGainErr( (float)gain_err );
146  pg.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
147 
148  fData.AddOrReplaceRow(pg);
149  }
150  }
151  }
152 
153  return result;
154  }
155 
157  DBUpdate();
158  return fData.GetRow(ch);
159  }
160 
162  return this->PmtGainObject(ch).Gain();
163  }
164 
166  return this->PmtGainObject(ch).GainErr();
167  }
168 
170  return this->PmtGainObject(ch).ExtraInfo();
171  }
172 
173 
174 }//end namespace lariov
float GainErr() const
Definition: PmtGain.h:37
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.
static QCString result
void SetStamp(unsigned long stamp, unsigned int substamp=0)
Definition: IOVTimeStamp.h:41
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void SetExtraInfo(CalibrationExtraInfo const &info)
Definition: PmtGain.h:42
std::uint32_t DBChannelID_t
std::uint64_t DBTimeStamp_t
SIOVPmtGainProvider(fhicl::ParameterSet const &p)
Constructors.
CalibrationExtraInfo const & ExtraInfo(DBChannelID_t ch) const override
art framework interface to geometry description
unsigned long SubStamp() const
Definition: IOVTimeStamp.h:38
void SetGainErr(float v)
Definition: PmtGain.h:41
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
void SetGain(float v)
Definition: PmtGain.h:40
Retrieves information: pmt gain.
p
Definition: test.py:223
void SetChannel(unsigned int ch)
Definition: ChData.h:34
string tmp
Definition: languages.py:63
const IOVTimeStamp & End() const
unsigned int NOpDets() const
Number of OpDets in the whole detector.
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
float Gain() const
Definition: PmtGain.h:36
Filters for channels, events, etc.
unsigned long Stamp() const
Definition: IOVTimeStamp.h:37
CalibrationExtraInfo const & ExtraInfo() const
Definition: PmtGain.h:38
void line(double t, double *p, double &x, double &y, double &z)
Class def header for a class SIOVPmtGainProvider.
std::string find_file(std::string const &filename) const
Definition: search_path.cc:96
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
static IOVTimeStamp MaxTimeStamp()
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
LArSoft geometry interface.
Definition: ChannelGeo.h:16
float GainErr(DBChannelID_t ch) const override
bool IsValidOpChannel(int opChannel) const
Is this a valid OpChannel number?
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)