LArSoft  v06_85_00
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"
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 
45  PmtGain defaultGain(0);
46 
47  defaultGain.SetGain(default_gain);
48  defaultGain.SetGainErr(default_gain_err);
49  defaultGain.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
50 
52  for (unsigned int od=0; od!=geo->NOpDets(); ++od) {
53  if (geo->IsValidOpChannel(od)) {
54  defaultGain.SetChannel(od);
55  fData.AddOrReplaceRow(defaultGain);
56  }
57  }
58 
59  }
60  else if (fDataSource == DataSource::File) {
61  cet::search_path sp("FW_SEARCH_PATH");
62  std::string abs_fp = sp.find_file(fileName);
63  std::cout << "Using pmt gains from local file: "<<abs_fp<<"\n";
64  std::ifstream file(abs_fp);
65  if (!file) {
66  throw cet::exception("SIOVPmtGainProvider")
67  << "File "<<abs_fp<<" is not found.";
68  }
69 
70  std::string line;
71  PmtGain dp(0);
72  while (std::getline(file, line)) {
73  if (line[0] == '#') continue;
74  size_t current_comma = line.find(',');
75  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
76  float gain = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
77 
78  current_comma = line.find(',',current_comma+1);
79  float gain_err = std::stof( line.substr(current_comma+1) );
80 
81  CalibrationExtraInfo info("PmtGain");
82 
83  dp.SetChannel(ch);
84  dp.SetGain(gain);
85  dp.SetGainErr(gain_err);
86  dp.SetExtraInfo(info);
87 
88  fData.AddOrReplaceRow(dp);
89  }
90  }
91  else {
92  std::cout << "Using pmt gains from conditions database"<<std::endl;
93  }
94  }
95 
96  bool SIOVPmtGainProvider::Update(DBTimeStamp_t ts) {
97 
98  if (fDataSource != DataSource::Database) return false;
99 
100  if (!this->UpdateFolder(ts)) return false;
101 
102  //DBFolder was updated, so now update the Snapshot
103  fData.Clear();
104  fData.SetIoV(this->Begin(), this->End());
105 
106  std::vector<DBChannelID_t> channels;
107  fFolder->GetChannelList(channels);
108  for (auto it = channels.begin(); it != channels.end(); ++it) {
109 
110  double gain, gain_err;
111  fFolder->GetNamedChannelData(*it, "gain", gain);
112  fFolder->GetNamedChannelData(*it, "gain_sigma", gain_err);
113 
114  PmtGain pg(*it);
115  pg.SetGain( (float)gain );
116  pg.SetGainErr( (float)gain_err );
117  pg.SetExtraInfo(CalibrationExtraInfo("PmtGain"));
118 
119  fData.AddOrReplaceRow(pg);
120  }
121 
122  return true;
123  }
124 
125  const PmtGain& SIOVPmtGainProvider::PmtGainObject(DBChannelID_t ch) const {
126  return fData.GetRow(ch);
127  }
128 
129  float SIOVPmtGainProvider::Gain(DBChannelID_t ch) const {
130  return this->PmtGainObject(ch).Gain();
131  }
132 
133  float SIOVPmtGainProvider::GainErr(DBChannelID_t ch) const {
134  return this->PmtGainObject(ch).GainErr();
135  }
136 
137  CalibrationExtraInfo const& SIOVPmtGainProvider::ExtraInfo(DBChannelID_t ch) const {
138  return this->PmtGainObject(ch).ExtraInfo();
139  }
140 
141 
142 }//end namespace lariov
143 
144 #endif
145 
std::unique_ptr< DBFolder > fFolder
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.
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
const IOVTimeStamp & End() const
unsigned int NOpDets() const
Number of OpDets in the whole detector.
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