LArSoft  v09_90_00
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...
9 #include "larcorealg/Geometry/GeometryCore.h" // for wire_id_...
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 
31  fData.Clear();
33  tmp.SetStamp(tmp.Stamp() - 1, tmp.SubStamp());
34  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
35  }
36 
38  : DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"))
39  {
40 
41  this->Reconfigure(p);
42  }
43 
45  {
46 
47  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
48  fData.Clear();
50  tmp.SetStamp(tmp.Stamp() - 1, tmp.SubStamp());
51  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
52 
53  bool UseDB = p.get<bool>("UseDB", false);
54  bool UseFile = p.get<bool>("UseFile", false);
55  std::string fileName = p.get<std::string>("FileName", "");
56 
57  //priority: (1) use db, (2) use table, (3) use defaults
58  //If none are specified, use defaults
59  if (UseDB)
61  else if (UseFile)
63  else
65 
67  std::cout << "Using default pedestal values\n";
68  float default_collmean = p.get<float>("DefaultCollMean", 400.0);
69  float default_collrms = p.get<float>("DefaultCollRms", 0.3);
70  float default_mean_err = p.get<float>("DefaultMeanErr", 0.0);
71  float default_rms_err = p.get<float>("DefaultRmsErr", 0.0);
72  float default_indmean = p.get<float>("DefaultIndMean", 2048.0);
73  float default_indrms = p.get<float>("DefaultIndRms", 0.3);
74 
75  DetPedestal DefaultColl(0);
76  DetPedestal DefaultInd(0);
77 
78  DefaultColl.SetPedMean(default_collmean);
79  DefaultColl.SetPedMeanErr(default_mean_err);
80  DefaultColl.SetPedRms(default_collrms);
81  DefaultColl.SetPedRmsErr(default_rms_err);
82 
83  DefaultInd.SetPedMean(default_indmean);
84  DefaultInd.SetPedMeanErr(default_mean_err);
85  DefaultInd.SetPedRms(default_indrms);
86  DefaultInd.SetPedRmsErr(default_rms_err);
87 
89  for (auto const& wid : geo->Iterate<geo::WireID>()) {
90  DBChannelID_t ch = geo->PlaneWireToChannel(wid);
91 
92  if (geo->SignalType(ch) == geo::kCollection) {
93  DefaultColl.SetChannel(ch);
94  fData.AddOrReplaceRow(DefaultColl);
95  }
96  else if (geo->SignalType(ch) == geo::kInduction) {
97  DefaultInd.SetChannel(ch);
98  fData.AddOrReplaceRow(DefaultInd);
99  }
100  else
101  throw IOVDataError("Wire type is not collection or induction!");
102  }
103  }
104  else if (fDataSource == DataSource::File) {
105  cet::search_path sp("FW_SEARCH_PATH");
106  std::string abs_fp = sp.find_file(fileName);
107  std::cout << "Using pedestals from local file: " << abs_fp << "\n";
108  std::ifstream file(abs_fp);
109  if (!file) {
110  throw cet::exception("DetPedestalRetrievalAlg") << "File " << abs_fp << " is not found.";
111  }
112 
113  std::string line;
114  DetPedestal dp(0);
115  while (std::getline(file, line)) {
116  size_t current_comma = line.find(',');
117  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
118  float ped = std::stof(
119  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
120 
121  current_comma = line.find(',', current_comma + 1);
122  float rms = std::stof(
123  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
124 
125  current_comma = line.find(',', current_comma + 1);
126  float ped_err = std::stof(
127  line.substr(current_comma + 1, line.find(',', current_comma + 1) - (current_comma + 1)));
128 
129  current_comma = line.find(',', current_comma + 1);
130  float rms_err = std::stof(line.substr(current_comma + 1));
131 
132  dp.SetChannel(ch);
133  dp.SetPedMean(ped);
134  dp.SetPedMeanErr(ped_err);
135  dp.SetPedRms(rms);
136  dp.SetPedRmsErr(rms_err);
137  fData.AddOrReplaceRow(dp);
138  }
139  } // if source from file
140  else {
141  std::cout << "Using pedestals from conditions database\n";
142  }
143  }
144 
145  // This method saves the time stamp of the latest event.
146 
148  {
149  mf::LogInfo("DetPedestalRetrievalAlg") << "DetPedestalRetrievalAlg::UpdateTimeStamp called.";
150  fEventTimeStamp = ts;
151  }
152 
153  // Maybe update method cached data (public non-const version).
154 
155  bool DetPedestalRetrievalAlg::Update(DBTimeStamp_t ts)
156  {
157 
158  fEventTimeStamp = ts;
159  return DBUpdate(ts);
160  }
161 
162  // Maybe update method cached data (private const version using current event time).
163 
165  {
166  return DBUpdate(fEventTimeStamp);
167  }
168 
169  // Maybe update method cached data (private const version).
170  // This is the function that does the actual work of updating data from database.
171 
172  bool DetPedestalRetrievalAlg::DBUpdate(DBTimeStamp_t ts) const
173  {
174 
175  // A static mutex that is shared across all invocations of the function.
176  static std::mutex mutex;
177  std::lock_guard<std::mutex> lock(mutex);
178 
179  bool result = false;
181 
182  mf::LogInfo("DetPedestalRetrievalAlg")
183  << "DetPedestalRetrievalAlg::DBUpdate called with new timestamp.";
184  fCurrentTimeStamp = ts;
185 
186  // Call non-const base class method.
187 
188  result = const_cast<DetPedestalRetrievalAlg*>(this)->UpdateFolder(ts);
189  if (result) {
190 
191  //DBFolder was updated, so now update the Snapshot
192  fData.Clear();
193  fData.SetIoV(this->Begin(), this->End());
194 
195  std::vector<DBChannelID_t> channels;
196  fFolder->GetChannelList(channels);
197  for (auto it = channels.begin(); it != channels.end(); ++it) {
198 
199  double mean, mean_err, rms, rms_err;
200  fFolder->GetNamedChannelData(*it, "mean", mean);
201  fFolder->GetNamedChannelData(*it, "mean_err", mean_err);
202  fFolder->GetNamedChannelData(*it, "rms", rms);
203  fFolder->GetNamedChannelData(*it, "rms_err", rms_err);
204 
205  DetPedestal pd(*it);
206  pd.SetPedMean((float)mean);
207  pd.SetPedMeanErr((float)mean_err);
208  pd.SetPedRms((float)rms);
209  pd.SetPedRmsErr((float)rms_err);
210 
211  fData.AddOrReplaceRow(pd);
212  }
213  }
214  }
215 
216  return result;
217  }
218 
219  const DetPedestal& DetPedestalRetrievalAlg::Pedestal(DBChannelID_t ch) const
220  {
221  DBUpdate();
222  return fData.GetRow(ch);
223  }
224 
225  float DetPedestalRetrievalAlg::PedMean(DBChannelID_t ch) const
226  {
227  return this->Pedestal(ch).PedMean();
228  }
229 
230  float DetPedestalRetrievalAlg::PedRms(DBChannelID_t ch) const
231  {
232  return this->Pedestal(ch).PedRms();
233  }
234 
235  float DetPedestalRetrievalAlg::PedMeanErr(DBChannelID_t ch) const
236  {
237  return this->Pedestal(ch).PedMeanErr();
238  }
239 
240  float DetPedestalRetrievalAlg::PedRmsErr(DBChannelID_t ch) const
241  {
242  return this->Pedestal(ch).PedRmsErr();
243  }
244 
245 } //end namespace lariov
details::range_type< T > Iterate() const
Initializes the specified ID with the ID of the first cryostat.
Definition: GeometryCore.h:541
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
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
Access the description of detector geometry.
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:151
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
raw::ChannelID_t PlaneWireToChannel(WireID const &wireid) const
Returns the ID of the TPC channel connected to the specified wire.
SigType_t SignalType(PlaneID const &pid) const
Returns the type of signal on the channels of specified TPC plane.
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
Namespace collecting geometry-related classes utilities.
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:152