LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
DetPedestalRetrievalAlg.cxx
Go to the documentation of this file.
3 
4 // art/LArSoft libraries
6 #include "cetlib_except/exception.h"
7 #include "fhiclcpp/ParameterSet.h" // for Paramete...
10 #include "larcoreobj/SimpleTypesAndConstants/geo_types.h" // for kCollection
11 #include "larevt/CalibrationDBI/IOVData/IOVDataError.h" // for IOVDataE...
12 #include "larevt/CalibrationDBI/IOVData/IOVTimeStamp.h" // for IOVTimeS...
13 #include "larevt/CalibrationDBI/Providers/DBFolder.h" // for DBFolder
15 
16 //C/C++
17 #include <fstream>
18 
19 namespace lariov {
20 
21  //constructors
22  DetPedestalRetrievalAlg::DetPedestalRetrievalAlg(const std::string& foldername,
23  const std::string& url,
24  const std::string& tag /*=""*/)
25  : DatabaseRetrievalAlg(foldername, url, tag)
26  , fEventTimeStamp(0)
27  , fCurrentTimeStamp(0)
28  , fDataSource(DataSource::Database)
29  {
30  fData.Clear();
32  tmp.SetStamp(tmp.Stamp() - 1, tmp.SubStamp());
33  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
34  }
35 
37  : DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"))
38  {
39  this->Reconfigure(p);
40  }
41 
43  {
44  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
45  fData.Clear();
47  tmp.SetStamp(tmp.Stamp() - 1, tmp.SubStamp());
48  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
49 
50  bool UseDB = p.get<bool>("UseDB", false);
51  bool UseFile = p.get<bool>("UseFile", false);
52  std::string fileName = p.get<std::string>("FileName", "");
53 
54  //priority: (1) use db, (2) use table, (3) use defaults
55  //If none are specified, use defaults
56  if (UseDB)
58  else if (UseFile)
60  else
62 
64  std::cout << "Using default pedestal values\n";
65  float default_collmean = p.get<float>("DefaultCollMean", 400.0);
66  float default_collrms = p.get<float>("DefaultCollRms", 0.3);
67  float default_mean_err = p.get<float>("DefaultMeanErr", 0.0);
68  float default_rms_err = p.get<float>("DefaultRmsErr", 0.0);
69  float default_indmean = p.get<float>("DefaultIndMean", 2048.0);
70  float default_indrms = p.get<float>("DefaultIndRms", 0.3);
71 
72  DetPedestal DefaultColl(0);
73  DetPedestal DefaultInd(0);
74 
75  DefaultColl.SetPedMean(default_collmean);
76  DefaultColl.SetPedMeanErr(default_mean_err);
77  DefaultColl.SetPedRms(default_collrms);
78  DefaultColl.SetPedRmsErr(default_rms_err);
79 
80  DefaultInd.SetPedMean(default_indmean);
81  DefaultInd.SetPedMeanErr(default_mean_err);
82  DefaultInd.SetPedRms(default_indrms);
83  DefaultInd.SetPedRmsErr(default_rms_err);
84 
85  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout>()->Get();
86  for (auto const& wid : wireReadoutGeom.Iterate<geo::WireID>()) {
87  DBChannelID_t ch = wireReadoutGeom.PlaneWireToChannel(wid);
88 
89  if (wireReadoutGeom.SignalType(ch) == geo::kCollection) {
90  DefaultColl.SetChannel(ch);
91  fData.AddOrReplaceRow(DefaultColl);
92  }
93  else if (wireReadoutGeom.SignalType(ch) == geo::kInduction) {
94  DefaultInd.SetChannel(ch);
95  fData.AddOrReplaceRow(DefaultInd);
96  }
97  else
98  throw IOVDataError("Wire type is not collection or induction!");
99  }
100  }
101  else if (fDataSource == DataSource::File) {
102  cet::search_path sp("FW_SEARCH_PATH");
103  std::string abs_fp = sp.find_file(fileName);
104  std::cout << "Using pedestals from local file: " << abs_fp << "\n";
105  std::ifstream file(abs_fp);
106  if (!file) {
107  throw cet::exception("DetPedestalRetrievalAlg") << "File " << abs_fp << " is not found.";
108  }
109 
110  std::string line;
111  DetPedestal dp(0);
112  while (std::getline(file, line)) {
113  size_t current_comma = line.find(',');
114  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
115  float ped = std::stof(
116  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
117 
118  current_comma = line.find(',', current_comma + 1);
119  float rms = std::stof(
120  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
121 
122  current_comma = line.find(',', current_comma + 1);
123  float ped_err = std::stof(
124  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
125 
126  current_comma = line.find(',', current_comma + 1);
127  float rms_err = std::stof(line.substr(current_comma + 1));
128 
129  dp.SetChannel(ch);
130  dp.SetPedMean(ped);
131  dp.SetPedMeanErr(ped_err);
132  dp.SetPedRms(rms);
133  dp.SetPedRmsErr(rms_err);
134  fData.AddOrReplaceRow(dp);
135  }
136  } // if source from file
137  else {
138  std::cout << "Using pedestals from conditions database\n";
139  }
140  }
141 
142  // This method saves the time stamp of the latest event.
143 
145  {
146  mf::LogInfo("DetPedestalRetrievalAlg") << "DetPedestalRetrievalAlg::UpdateTimeStamp called.";
147  fEventTimeStamp = ts;
148  }
149 
150  // Maybe update method cached data (public non-const version).
151 
152  bool DetPedestalRetrievalAlg::Update(DBTimeStamp_t ts)
153  {
154  fEventTimeStamp = ts;
155  return DBUpdate(ts);
156  }
157 
158  // Maybe update method cached data (private const version using current event time).
159 
161  {
162  return DBUpdate(fEventTimeStamp);
163  }
164 
165  // Maybe update method cached data (private const version).
166  // This is the function that does the actual work of updating data from database.
167 
168  bool DetPedestalRetrievalAlg::DBUpdate(DBTimeStamp_t ts) const
169  {
170  // A static mutex that is shared across all invocations of the function.
171  static std::mutex mutex;
172  std::lock_guard<std::mutex> lock(mutex);
173 
174  bool result = false;
176 
177  mf::LogInfo("DetPedestalRetrievalAlg")
178  << "DetPedestalRetrievalAlg::DBUpdate called with new timestamp.";
179  fCurrentTimeStamp = ts;
180 
181  // Call non-const base class method.
182 
183  result = const_cast<DetPedestalRetrievalAlg*>(this)->UpdateFolder(ts);
184  if (result) {
185 
186  //DBFolder was updated, so now update the Snapshot
187  fData.Clear();
188  fData.SetIoV(this->Begin(), this->End());
189 
190  std::vector<DBChannelID_t> channels;
191  fFolder->GetChannelList(channels);
192  for (auto it = channels.begin(); it != channels.end(); ++it) {
193 
194  double mean, mean_err, rms, rms_err;
195  fFolder->GetNamedChannelData(*it, "mean", mean);
196  fFolder->GetNamedChannelData(*it, "mean_err", mean_err);
197  fFolder->GetNamedChannelData(*it, "rms", rms);
198  fFolder->GetNamedChannelData(*it, "rms_err", rms_err);
199 
200  DetPedestal pd(*it);
201  pd.SetPedMean((float)mean);
202  pd.SetPedMeanErr((float)mean_err);
203  pd.SetPedRms((float)rms);
204  pd.SetPedRmsErr((float)rms_err);
205 
206  fData.AddOrReplaceRow(pd);
207  }
208  }
209  }
210 
211  return result;
212  }
213 
214  const DetPedestal& DetPedestalRetrievalAlg::Pedestal(DBChannelID_t ch) const
215  {
216  DBUpdate();
217  return fData.GetRow(ch);
218  }
219 
220  float DetPedestalRetrievalAlg::PedMean(DBChannelID_t ch) const
221  {
222  return this->Pedestal(ch).PedMean();
223  }
224 
225  float DetPedestalRetrievalAlg::PedRms(DBChannelID_t ch) const
226  {
227  return this->Pedestal(ch).PedRms();
228  }
229 
230  float DetPedestalRetrievalAlg::PedMeanErr(DBChannelID_t ch) const
231  {
232  return this->Pedestal(ch).PedMeanErr();
233  }
234 
235  float DetPedestalRetrievalAlg::PedRmsErr(DBChannelID_t ch) const
236  {
237  return this->Pedestal(ch).PedRmsErr();
238  }
239 
240 } //end namespace lariov
std::unique_ptr< DBFolder > fFolder
virtual void Reconfigure(fhicl::ParameterSet const &p)
Configure using fhicl::ParameterSet.
void SetStamp(unsigned long stamp, unsigned int substamp=0)
Definition: IOVTimeStamp.h:41
float PedMeanErr() const
Definition: DetPedestal.h:34
float PedRmsErr(DBChannelID_t ch) const override
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Class def header for a class DetPedestalRetrievalAlg.
float PedRms(DBChannelID_t ch) const override
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
float PedMeanErr(DBChannelID_t ch) const override
Float_t tmp
Definition: plot.C:35
void SetPedRms(float pedRms)
Definition: DetPedestal.h:38
float PedMean() const
Definition: DetPedestal.h:32
bool Update(DBTimeStamp_t ts)
Update Snapshot and inherited DBFolder if using database. Return true if updated. ...
Class def header for a class IOVTimeStamp.
unsigned long SubStamp() const
Definition: IOVTimeStamp.h:38
const DetPedestal & Pedestal(DBChannelID_t ch) const
Retrieve pedestal information.
void SetPedRmsErr(float pedRmsErr)
Definition: DetPedestal.h:40
bool UpdateFolder(DBTimeStamp_t ts)
Return true if fFolder is successfully updated.
void SetPedMeanErr(float pedMeanErr)
Definition: DetPedestal.h:39
Signal from induction planes.
Definition: geo_types.h:147
bool DBUpdate() const
Do actual database updates.
parameter set interface
T get(std::string const &key) const
Definition: ParameterSet.h:314
Retrieves channel information: pedestal and RMS.
DetPedestalRetrievalAlg(const std::string &foldername, const std::string &url, const std::string &tag="")
Constructors.
void SetChannel(unsigned int ch)
Definition: ChData.h:33
const IOVTimeStamp & End() const
Definition of data types for geometry description.
Filters for channels, events, etc.
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
unsigned long Stamp() const
Definition: IOVTimeStamp.h:37
float PedRmsErr() const
Definition: DetPedestal.h:35
void SetPedMean(float pedMean)
Definition: DetPedestal.h:37
TFile * file
Collection of exception classes for IOVData.
const IOVTimeStamp & Begin() const
Get Timestamp information.
float PedMean(DBChannelID_t ch) const override
static IOVTimeStamp MaxTimeStamp()
float PedRms() const
Definition: DetPedestal.h:33
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
Interface to geometry for wire readouts .
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:148