All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIOVPmtGainProvider.cxx
Go to the documentation of this file.
1 #include "SIOVPmtGainProvider.h"
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
14  SIOVPmtGainProvider::SIOVPmtGainProvider(fhicl::ParameterSet const& p) :
15  DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg")),
16  fEventTimeStamp(0),
17  fCurrentTimeStamp(0) {
18 
19  this->Reconfigure(p);
20  }
21 
22  void SIOVPmtGainProvider::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 
44  PmtGain defaultGain(0);
45 
46  defaultGain.SetGain(default_gain);
47  defaultGain.SetGainErr(default_gain_err);
48  defaultGain.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
49 
50  art::ServiceHandle<geo::Geometry const> geo;
51  for (unsigned int od=0; od!=geo->NOpDets(); ++od) {
52  if (geo->IsValidOpChannel(od)) {
53  defaultGain.SetChannel(od);
54  fData.AddOrReplaceRow(defaultGain);
55  }
56  }
57 
58  }
59  else if (fDataSource == DataSource::File) {
60  cet::search_path sp("FW_SEARCH_PATH");
61  std::string abs_fp = sp.find_file(fileName);
62  std::cout << "Using pmt gains from local file: "<<abs_fp<<"\n";
63  std::ifstream file(abs_fp);
64  if (!file) {
65  throw cet::exception("SIOVPmtGainProvider")
66  << "File "<<abs_fp<<" is not found.";
67  }
68 
69  std::string line;
70  PmtGain dp(0);
71  while (std::getline(file, line)) {
72  if (line[0] == '#') continue;
73  size_t current_comma = line.find(',');
74  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
75  float gain = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
76 
77  current_comma = line.find(',',current_comma+1);
78  float gain_err = std::stof( line.substr(current_comma+1) );
79 
80  CalibrationExtraInfo info("PmtGain");
81 
82  dp.SetChannel(ch);
83  dp.SetGain(gain);
84  dp.SetGainErr(gain_err);
85  dp.SetExtraInfo(info);
86 
87  fData.AddOrReplaceRow(dp);
88  }
89  }
90  else {
91  std::cout << "Using pmt gains from conditions database"<<std::endl;
92  }
93  }
94 
95  // This method saves the time stamp of the latest event.
96 
98  mf::LogInfo("SIOVPmtGainProvider") << "SIOVPmtGainProvider::UpdateTimeStamp called.";
99  fEventTimeStamp = ts;
100  }
101 
102  // Maybe update method cached data (public non-const version).
103 
105 
106  fEventTimeStamp = ts;
107  return DBUpdate(ts);
108  }
109 
110  // Maybe update method cached data (private const version using current event time).
111 
113  return DBUpdate(fEventTimeStamp);
114  }
115 
116  // Maybe update method cached data (private const version).
117  // This is the function that does the actual work of updating data from database.
118 
120 
121  bool result = false;
123 
124  mf::LogInfo("SIOVPmtGainProvider") << "SIOVPmtGainProvider::DBUpdate called with new timestamp.";
125 
126  fCurrentTimeStamp = ts;
127 
128  // Call non-const base class method.
129 
130  result = const_cast<SIOVPmtGainProvider*>(this)->UpdateFolder(ts);
131  if(result) {
132  //DBFolder was updated, so now update the Snapshot
133  fData.Clear();
134  fData.SetIoV(this->Begin(), this->End());
135 
136  std::vector<DBChannelID_t> channels;
137  fFolder->GetChannelList(channels);
138  for (auto it = channels.begin(); it != channels.end(); ++it) {
139 
140  double gain, gain_err;
141  fFolder->GetNamedChannelData(*it, "gain", gain);
142  fFolder->GetNamedChannelData(*it, "gain_sigma", gain_err);
143 
144  PmtGain pg(*it);
145  pg.SetGain( (float)gain );
146  pg.SetGainErr( (float)gain_err );
147  pg.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
148 
149  fData.AddOrReplaceRow(pg);
150  }
151  }
152  }
153 
154  return result;
155  }
156 
158  DBUpdate();
159  return fData.GetRow(ch);
160  }
161 
163  return this->PmtGainObject(ch).Gain();
164  }
165 
167  return this->PmtGainObject(ch).GainErr();
168  }
169 
171  return this->PmtGainObject(ch).ExtraInfo();
172  }
173 
174 
175 }//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.
void SetStamp(unsigned long stamp, unsigned int substamp=0)
Definition: IOVTimeStamp.h:41
void SetExtraInfo(CalibrationExtraInfo const &info)
Definition: PmtGain.h:42
std::uint32_t DBChannelID_t
* file
Definition: file_to_url.sh:69
pdgs p
Definition: selectors.fcl:22
std::uint64_t DBTimeStamp_t
SIOVPmtGainProvider(fhicl::ParameterSet const &p)
Constructors.
unsigned long SubStamp() const
Definition: IOVTimeStamp.h:38
float Gain(DBChannelID_t ch) const override
void SetGainErr(float v)
Definition: PmtGain.h:41
bool UpdateFolder(DBTimeStamp_t ts)
Return true if fFolder is successfully updated.
void SetGain(float v)
Definition: PmtGain.h:40
Retrieves information: pmt gain.
void SetChannel(unsigned int ch)
Definition: ChData.h:34
const IOVTimeStamp & End() const
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
float Gain() const
Definition: PmtGain.h:36
unsigned long Stamp() const
Definition: IOVTimeStamp.h:37
CalibrationExtraInfo const & ExtraInfo() const
Definition: PmtGain.h:38
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.
static IOVTimeStamp MaxTimeStamp()
CalibrationExtraInfo const & ExtraInfo(DBChannelID_t ch) const override
float GainErr(DBChannelID_t ch) const override
art framework interface to geometry description
BEGIN_PROLOG could also be cout