LArSoft  v06_85_00
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"
12 
13 //C/C++
14 #include <fstream>
15 
16 namespace lariov {
17 
18  //constructors
19  DetPedestalRetrievalAlg::DetPedestalRetrievalAlg(const std::string& foldername,
20  const std::string& url,
21  const std::string& tag /*=""*/) :
22  DatabaseRetrievalAlg(foldername, url, tag),
23  fDataSource(DataSource::Database) {
24 
25  fData.Clear();
26  IOVTimeStamp tmp = IOVTimeStamp::MaxTimeStamp();
27  tmp.SetStamp(tmp.Stamp()-1, tmp.SubStamp());
28  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
29  }
30 
31 
33  DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg")) {
34 
35  this->Reconfigure(p);
36  }
37 
39 
40  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
41  fData.Clear();
42  IOVTimeStamp tmp = IOVTimeStamp::MaxTimeStamp();
43  tmp.SetStamp(tmp.Stamp()-1, tmp.SubStamp());
44  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
45 
46  bool UseDB = p.get<bool>("UseDB", false);
47  bool UseFile = p.get<bool>("UseFile", false);
48  std::string fileName = p.get<std::string>("FileName", "");
49 
50  //priority: (1) use db, (2) use table, (3) use defaults
51  //If none are specified, use defaults
52  if ( UseDB ) fDataSource = DataSource::Database;
53  else if (UseFile) fDataSource = DataSource::File;
54  else fDataSource = DataSource::Default;
55 
56  if (fDataSource == DataSource::Default) {
57  std::cout << "Using default pedestal values\n";
58  float default_collmean = p.get<float>("DefaultCollMean", 400.0);
59  float default_collrms = p.get<float>("DefaultCollRms", 0.3);
60  float default_mean_err = p.get<float>("DefaultMeanErr", 0.0);
61  float default_rms_err = p.get<float>("DefaultRmsErr", 0.0);
62  float default_indmean = p.get<float>("DefaultIndMean", 2048.0);
63  float default_indrms = p.get<float>("DefaultIndRms", 0.3);
64 
65  DetPedestal DefaultColl(0);
66  DetPedestal DefaultInd(0);
67 
68  DefaultColl.SetPedMean(default_collmean);
69  DefaultColl.SetPedMeanErr(default_mean_err);
70  DefaultColl.SetPedRms(default_collrms);
71  DefaultColl.SetPedRmsErr(default_rms_err);
72 
73  DefaultInd.SetPedMean(default_indmean);
74  DefaultInd.SetPedMeanErr(default_mean_err);
75  DefaultInd.SetPedRms(default_indrms);
76  DefaultInd.SetPedRmsErr(default_rms_err);
77 
80  for ( ; itW != geo->end_wire_id(); ++itW) {
81  DBChannelID_t ch = geo->PlaneWireToChannel(*itW);
82 
83  if (geo->SignalType(ch) == geo::kCollection) {
84  DefaultColl.SetChannel(ch);
85  fData.AddOrReplaceRow(DefaultColl);
86  }
87  else if (geo->SignalType(ch) == geo::kInduction) {
88  DefaultInd.SetChannel(ch);
89  fData.AddOrReplaceRow(DefaultInd);
90  }
91  else throw IOVDataError("Wire type is not collection or induction!");
92  }
93  }
94  else if (fDataSource == DataSource::File) {
95  cet::search_path sp("FW_SEARCH_PATH");
96  std::string abs_fp = sp.find_file(fileName);
97  std::cout << "Using pedestals from local file: "<<abs_fp<<"\n";
98  std::ifstream file(abs_fp);
99  if (!file) {
100  throw cet::exception("DetPedestalRetrievalAlg")
101  << "File "<<abs_fp<<" is not found.";
102  }
103 
104  std::string line;
105  DetPedestal dp(0);
106  while (std::getline(file, line)) {
107  size_t current_comma = line.find(',');
108  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
109  float ped = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
110 
111  current_comma = line.find(',',current_comma+1);
112  float rms = 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 ped_err = 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 rms_err = std::stof( line.substr(current_comma+1) );
119 
120  dp.SetChannel(ch);
121  dp.SetPedMean(ped);
122  dp.SetPedMeanErr(ped_err);
123  dp.SetPedRms(rms);
124  dp.SetPedRmsErr(rms_err);
125  fData.AddOrReplaceRow(dp);
126  }
127  } // if source from file
128  else {
129  std::cout << "Using pedestals from conditions database\n";
130  }
131  }
132 
133 
134  bool DetPedestalRetrievalAlg::Update(DBTimeStamp_t ts) {
135 
136  if (fDataSource != DataSource::Database) return false;
137 
138  if (!this->UpdateFolder(ts)) return false;
139 
140  //DBFolder was updated, so now update the Snapshot
141  fData.Clear();
142  fData.SetIoV(this->Begin(), this->End());
143 
144  std::vector<DBChannelID_t> channels;
145  fFolder->GetChannelList(channels);
146  for (auto it = channels.begin(); it != channels.end(); ++it) {
147 
148  double mean, mean_err, rms, rms_err;
149  fFolder->GetNamedChannelData(*it, "mean", mean);
150  fFolder->GetNamedChannelData(*it, "mean_err", mean_err);
151  fFolder->GetNamedChannelData(*it, "rms", rms);
152  fFolder->GetNamedChannelData(*it, "rms_err", rms_err);
153 
154  DetPedestal pd(*it);
155  pd.SetPedMean( (float)mean );
156  pd.SetPedMeanErr( (float)mean_err );
157  pd.SetPedRms( (float)rms );
158  pd.SetPedRmsErr( (float)rms_err );
159 
160  fData.AddOrReplaceRow(pd);
161  }
162 
163  return true;
164 
165  }
166 
167  const DetPedestal& DetPedestalRetrievalAlg::Pedestal(DBChannelID_t ch) const {
168  return fData.GetRow(ch);
169  }
170 
171  float DetPedestalRetrievalAlg::PedMean(DBChannelID_t ch) const {
172  return this->Pedestal(ch).PedMean();
173  }
174 
175  float DetPedestalRetrievalAlg::PedRms(DBChannelID_t ch) const {
176  return this->Pedestal(ch).PedRms();
177  }
178 
179  float DetPedestalRetrievalAlg::PedMeanErr(DBChannelID_t ch) const {
180  return this->Pedestal(ch).PedMeanErr();
181  }
182 
183  float DetPedestalRetrievalAlg::PedRmsErr(DBChannelID_t ch) const {
184  return this->Pedestal(ch).PedRmsErr();
185  }
186 
187 
188 
189 }//end namespace lariov
190 
191 #endif
192 
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
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
parameter set interface
T get(std::string const &key) const
Definition: ParameterSet.h:231
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.
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