LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
DetPedestalRetrievalAlg.cxx
Go to the documentation of this file.
1 #ifndef WEBDB_DETPEDESTALRETRIEVALALG_CXX
2 #define WEBDB_DETPEDESTALRETRIEVALALG_CXX
3 
5 #include "WebError.h"
6 #include "larevt/CalibrationDBI/IOVData/IOVDataConstants.h"
7 
8 // art/LArSoft libraries
11 #include "cetlib_except/exception.h"
13 
14 //C/C++
15 #include <fstream>
16 
17 namespace lariov {
18 
19  //constructors
20  DetPedestalRetrievalAlg::DetPedestalRetrievalAlg(const std::string& foldername,
21  const std::string& url,
22  const std::string& tag /*=""*/) :
23  DatabaseRetrievalAlg(foldername, url, tag),
24  fEventTimeStamp(0),
25  fCurrentTimeStamp(0),
26  fDataSource(DataSource::Database) {
27 
28  fData.Clear();
29  IOVTimeStamp tmp = IOVTimeStamp::MaxTimeStamp();
30  tmp.SetStamp(tmp.Stamp()-1, tmp.SubStamp());
31  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
32  }
33 
34 
36  DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg")) {
37 
38  this->Reconfigure(p);
39  }
40 
42 
43  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
44  fData.Clear();
45  IOVTimeStamp tmp = IOVTimeStamp::MaxTimeStamp();
46  tmp.SetStamp(tmp.Stamp()-1, tmp.SubStamp());
47  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
48 
49  bool UseDB = p.get<bool>("UseDB", false);
50  bool UseFile = p.get<bool>("UseFile", false);
51  std::string fileName = p.get<std::string>("FileName", "");
52 
53  //priority: (1) use db, (2) use table, (3) use defaults
54  //If none are specified, use defaults
55  if ( UseDB ) fDataSource = DataSource::Database;
56  else if (UseFile) fDataSource = DataSource::File;
57  else fDataSource = DataSource::Default;
58 
59  if (fDataSource == DataSource::Default) {
60  std::cout << "Using default pedestal values\n";
61  float default_collmean = p.get<float>("DefaultCollMean", 400.0);
62  float default_collrms = p.get<float>("DefaultCollRms", 0.3);
63  float default_mean_err = p.get<float>("DefaultMeanErr", 0.0);
64  float default_rms_err = p.get<float>("DefaultRmsErr", 0.0);
65  float default_indmean = p.get<float>("DefaultIndMean", 2048.0);
66  float default_indrms = p.get<float>("DefaultIndRms", 0.3);
67 
68  DetPedestal DefaultColl(0);
69  DetPedestal DefaultInd(0);
70 
71  DefaultColl.SetPedMean(default_collmean);
72  DefaultColl.SetPedMeanErr(default_mean_err);
73  DefaultColl.SetPedRms(default_collrms);
74  DefaultColl.SetPedRmsErr(default_rms_err);
75 
76  DefaultInd.SetPedMean(default_indmean);
77  DefaultInd.SetPedMeanErr(default_mean_err);
78  DefaultInd.SetPedRms(default_indrms);
79  DefaultInd.SetPedRmsErr(default_rms_err);
80 
83  for ( ; itW != geo->end_wire_id(); ++itW) {
84  DBChannelID_t ch = geo->PlaneWireToChannel(*itW);
85 
86  if (geo->SignalType(ch) == geo::kCollection) {
87  DefaultColl.SetChannel(ch);
88  fData.AddOrReplaceRow(DefaultColl);
89  }
90  else if (geo->SignalType(ch) == geo::kInduction) {
91  DefaultInd.SetChannel(ch);
92  fData.AddOrReplaceRow(DefaultInd);
93  }
94  else throw IOVDataError("Wire type is not collection or induction!");
95  }
96  }
97  else if (fDataSource == DataSource::File) {
98  cet::search_path sp("FW_SEARCH_PATH");
99  std::string abs_fp = sp.find_file(fileName);
100  std::cout << "Using pedestals from local file: "<<abs_fp<<"\n";
101  std::ifstream file(abs_fp);
102  if (!file) {
103  throw cet::exception("DetPedestalRetrievalAlg")
104  << "File "<<abs_fp<<" is not found.";
105  }
106 
107  std::string line;
108  DetPedestal dp(0);
109  while (std::getline(file, line)) {
110  size_t current_comma = line.find(',');
111  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
112  float ped = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
113 
114  current_comma = line.find(',',current_comma+1);
115  float rms = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
116 
117  current_comma = line.find(',',current_comma+1);
118  float ped_err = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
119 
120  current_comma = line.find(',',current_comma+1);
121  float rms_err = std::stof( line.substr(current_comma+1) );
122 
123  dp.SetChannel(ch);
124  dp.SetPedMean(ped);
125  dp.SetPedMeanErr(ped_err);
126  dp.SetPedRms(rms);
127  dp.SetPedRmsErr(rms_err);
128  fData.AddOrReplaceRow(dp);
129  }
130  } // if source from file
131  else {
132  std::cout << "Using pedestals from conditions database\n";
133  }
134  }
135 
136 
137  // This method saves the time stamp of the latest event.
138 
140  mf::LogInfo("DetPedestalRetrievalAlg") << "DetPedestalRetrievalAlg::UpdateTimeStamp called.";
141  fEventTimeStamp = ts;
142  }
143 
144  // Maybe update method cached data (public non-const version).
145 
146  bool DetPedestalRetrievalAlg::Update(DBTimeStamp_t ts) {
147 
148  fEventTimeStamp = ts;
149  return DBUpdate(ts);
150  }
151 
152  // Maybe update method cached data (private const version using current event time).
153 
155  return DBUpdate(fEventTimeStamp);
156  }
157 
158  // Maybe update method cached data (private const version).
159  // This is the function that does the actual work of updating data from database.
160 
161  bool DetPedestalRetrievalAlg::DBUpdate(DBTimeStamp_t ts) const {
162 
163  bool result = false;
164  if(fDataSource == DataSource::Database && ts != fCurrentTimeStamp) {
165 
166  mf::LogInfo("DetPedestalRetrievalAlg") << "DetPedestalRetrievalAlg::DBUpdate called with new timestamp.";
167  fCurrentTimeStamp = ts;
168 
169  // Call non-const base class method.
170 
171  result = const_cast<DetPedestalRetrievalAlg*>(this)->UpdateFolder(ts);
172  if(result) {
173 
174  //DBFolder was updated, so now update the Snapshot
175  fData.Clear();
176  fData.SetIoV(this->Begin(), this->End());
177 
178  std::vector<DBChannelID_t> channels;
179  fFolder->GetChannelList(channels);
180  for (auto it = channels.begin(); it != channels.end(); ++it) {
181 
182  double mean, mean_err, rms, rms_err;
183  fFolder->GetNamedChannelData(*it, "mean", mean);
184  fFolder->GetNamedChannelData(*it, "mean_err", mean_err);
185  fFolder->GetNamedChannelData(*it, "rms", rms);
186  fFolder->GetNamedChannelData(*it, "rms_err", rms_err);
187 
188  DetPedestal pd(*it);
189  pd.SetPedMean( (float)mean );
190  pd.SetPedMeanErr( (float)mean_err );
191  pd.SetPedRms( (float)rms );
192  pd.SetPedRmsErr( (float)rms_err );
193 
194  fData.AddOrReplaceRow(pd);
195  }
196  }
197  }
198 
199  return result;
200 
201  }
202 
203  const DetPedestal& DetPedestalRetrievalAlg::Pedestal(DBChannelID_t ch) const {
204  DBUpdate();
205  return fData.GetRow(ch);
206  }
207 
208  float DetPedestalRetrievalAlg::PedMean(DBChannelID_t ch) const {
209  return this->Pedestal(ch).PedMean();
210  }
211 
212  float DetPedestalRetrievalAlg::PedRms(DBChannelID_t ch) const {
213  return this->Pedestal(ch).PedRms();
214  }
215 
216  float DetPedestalRetrievalAlg::PedMeanErr(DBChannelID_t ch) const {
217  return this->Pedestal(ch).PedMeanErr();
218  }
219 
220  float DetPedestalRetrievalAlg::PedRmsErr(DBChannelID_t ch) const {
221  return this->Pedestal(ch).PedRmsErr();
222  }
223 
224 
225 
226 }//end namespace lariov
227 
228 #endif
229 
std::unique_ptr< DBFolder > fFolder
virtual void Reconfigure(fhicl::ParameterSet const &p)
Configure using fhicl::ParameterSet.
float PedRmsErr(DBChannelID_t ch) const override
Base forward iterator browsing all wire IDs in the detector.
Definition: GeometryCore.h:567
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Class def header for a class DetPedestalRetrievalAlg.
float PedRms(DBChannelID_t ch) const override
float PedMeanErr(DBChannelID_t ch) const override
Float_t tmp
Definition: plot.C:37
SigType_t SignalType(geo::PlaneID const &pid) const
Returns the type of signal on the channels of specified TPC plane.
bool Update(DBTimeStamp_t ts)
Update Snapshot and inherited DBFolder if using database. Return true if updated. ...
const DetPedestal & Pedestal(DBChannelID_t ch) const
Retrieve pedestal information.
bool UpdateFolder(DBTimeStamp_t ts)
Return true if fFolder is successfully updated.
Signal from induction planes.
Definition: geo_types.h:92
bool DBUpdate() const
Do actual database updates.
parameter set interface
T get(std::string const &key) const
Definition: ParameterSet.h:231
Retrieves channel information: pedestal and RMS.
DetPedestalRetrievalAlg(const std::string &foldername, const std::string &url, const std::string &tag="")
Constructors.
wire_id_iterator end_wire_id() const
Returns an iterator pointing after the last wire ID in the detector.
const IOVTimeStamp & End() const
Filters for channels, events, etc.
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:15
raw::ChannelID_t PlaneWireToChannel(WireID const &wireid) const
Returns the ID of the TPC channel connected to the specified wire.
TFile * file
const IOVTimeStamp & Begin() const
Get Timestamp information.
wire_id_iterator begin_wire_id() const
Returns an iterator pointing to the first wire ID in the detector.
float PedMean(DBChannelID_t ch) const override
Namespace collecting geometry-related classes utilities.
Collection of exception classes for WebDBI.
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void Reconfigure(fhicl::ParameterSet const &p) override
Reconfigure function called by fhicl constructor.
Signal from collection planes.
Definition: geo_types.h:93