All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DetPedestalRetrievalAlg.cxx
Go to the documentation of this file.
3 
4 // art/LArSoft libraries
5 #include "art/Framework/Services/Registry/ServiceHandle.h"
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
14 #include "messagefacility/MessageLogger/MessageLogger.h"
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 
36 
38  DatabaseRetrievalAlg(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg")) {
39 
40  this->Reconfigure(p);
41  }
42 
43  void DetPedestalRetrievalAlg::Reconfigure(fhicl::ParameterSet const& p) {
44 
45  this->DatabaseRetrievalAlg::Reconfigure(p.get<fhicl::ParameterSet>("DatabaseRetrievalAlg"));
46  fData.Clear();
48  tmp.SetStamp(tmp.Stamp()-1, tmp.SubStamp());
49  fData.SetIoV(tmp, IOVTimeStamp::MaxTimeStamp());
50 
51  bool UseDB = p.get<bool>("UseDB", false);
52  bool UseFile = p.get<bool>("UseFile", false);
53  std::string fileName = p.get<std::string>("FileName", "");
54 
55  //priority: (1) use db, (2) use table, (3) use defaults
56  //If none are specified, use defaults
57  if ( UseDB ) fDataSource = DataSource::Database;
58  else if (UseFile) fDataSource = DataSource::File;
60 
62  std::cout << "Using default pedestal values\n";
63  float default_collmean = p.get<float>("DefaultCollMean", 400.0);
64  float default_collrms = p.get<float>("DefaultCollRms", 0.3);
65  float default_mean_err = p.get<float>("DefaultMeanErr", 0.0);
66  float default_rms_err = p.get<float>("DefaultRmsErr", 0.0);
67  float default_indmean = p.get<float>("DefaultIndMean", 2048.0);
68  float default_indrms = p.get<float>("DefaultIndRms", 0.3);
69 
70  DetPedestal DefaultColl(0);
71  DetPedestal DefaultInd(0);
72 
73  DefaultColl.SetPedMean(default_collmean);
74  DefaultColl.SetPedMeanErr(default_mean_err);
75  DefaultColl.SetPedRms(default_collrms);
76  DefaultColl.SetPedRmsErr(default_rms_err);
77 
78  DefaultInd.SetPedMean(default_indmean);
79  DefaultInd.SetPedMeanErr(default_mean_err);
80  DefaultInd.SetPedRms(default_indrms);
81  DefaultInd.SetPedRmsErr(default_rms_err);
82 
83  art::ServiceHandle<geo::Geometry const> geo;
84  geo::wire_id_iterator itW = geo->begin_wire_id();
85  for ( ; itW != geo->end_wire_id(); ++itW) {
86  DBChannelID_t ch = geo->PlaneWireToChannel(*itW);
87 
88  if (geo->SignalType(ch) == geo::kCollection) {
89  DefaultColl.SetChannel(ch);
90  fData.AddOrReplaceRow(DefaultColl);
91  }
92  else if (geo->SignalType(ch) == geo::kInduction) {
93  DefaultInd.SetChannel(ch);
94  fData.AddOrReplaceRow(DefaultInd);
95  }
96  else throw IOVDataError("Wire type is not collection or induction!");
97  }
98  }
99  else if (fDataSource == DataSource::File) {
100  cet::search_path sp("FW_SEARCH_PATH");
101  std::string abs_fp = sp.find_file(fileName);
102  std::cout << "Using pedestals from local file: "<<abs_fp<<"\n";
103  std::ifstream file(abs_fp);
104  if (!file) {
105  throw cet::exception("DetPedestalRetrievalAlg")
106  << "File "<<abs_fp<<" is not found.";
107  }
108 
109  std::string line;
110  DetPedestal dp(0);
111  while (std::getline(file, line)) {
112  size_t current_comma = line.find(',');
113  DBChannelID_t ch = (DBChannelID_t)std::stoi(line.substr(0, current_comma));
114  float ped = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
115 
116  current_comma = line.find(',',current_comma+1);
117  float rms = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
118 
119  current_comma = line.find(',',current_comma+1);
120  float ped_err = std::stof( line.substr(current_comma+1, line.find(',',current_comma+1)-(current_comma+1)) );
121 
122  current_comma = line.find(',',current_comma+1);
123  float rms_err = std::stof( line.substr(current_comma+1) );
124 
125  dp.SetChannel(ch);
126  dp.SetPedMean(ped);
127  dp.SetPedMeanErr(ped_err);
128  dp.SetPedRms(rms);
129  dp.SetPedRmsErr(rms_err);
130  fData.AddOrReplaceRow(dp);
131  }
132  } // if source from file
133  else {
134  std::cout << "Using pedestals from conditions database\n";
135  }
136  }
137 
138 
139  // This method saves the time stamp of the latest event.
140 
142  mf::LogInfo("DetPedestalRetrievalAlg") << "DetPedestalRetrievalAlg::UpdateTimeStamp called.";
143  fEventTimeStamp = ts;
144  }
145 
146  // Maybe update method cached data (public non-const version).
147 
149 
150  fEventTimeStamp = ts;
151  return DBUpdate(ts);
152  }
153 
154  // Maybe update method cached data (private const version using current event time).
155 
157  return DBUpdate(fEventTimeStamp);
158  }
159 
160  // Maybe update method cached data (private const version).
161  // This is the function that does the actual work of updating data from database.
162 
164 
165  bool result = false;
167 
168  mf::LogInfo("DetPedestalRetrievalAlg") << "DetPedestalRetrievalAlg::DBUpdate called with new timestamp.";
169  fCurrentTimeStamp = ts;
170 
171  // Call non-const base class method.
172 
173  result = const_cast<DetPedestalRetrievalAlg*>(this)->UpdateFolder(ts);
174  if(result) {
175 
176  //DBFolder was updated, so now update the Snapshot
177  fData.Clear();
178  fData.SetIoV(this->Begin(), this->End());
179 
180  std::vector<DBChannelID_t> channels;
181  fFolder->GetChannelList(channels);
182  for (auto it = channels.begin(); it != channels.end(); ++it) {
183 
184  double mean, mean_err, rms, rms_err;
185  fFolder->GetNamedChannelData(*it, "mean", mean);
186  fFolder->GetNamedChannelData(*it, "mean_err", mean_err);
187  fFolder->GetNamedChannelData(*it, "rms", rms);
188  fFolder->GetNamedChannelData(*it, "rms_err", rms_err);
189 
190  DetPedestal pd(*it);
191  pd.SetPedMean( (float)mean );
192  pd.SetPedMeanErr( (float)mean_err );
193  pd.SetPedRms( (float)rms );
194  pd.SetPedRmsErr( (float)rms_err );
195 
196  fData.AddOrReplaceRow(pd);
197  }
198  }
199  }
200 
201  return result;
202 
203  }
204 
206  DBUpdate();
207  return fData.GetRow(ch);
208  }
209 
211  return this->Pedestal(ch).PedMean();
212  }
213 
215  return this->Pedestal(ch).PedRms();
216  }
217 
219  return this->Pedestal(ch).PedMeanErr();
220  }
221 
223  return this->Pedestal(ch).PedRmsErr();
224  }
225 
226 
227 
228 }//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:35
Base forward iterator browsing all wire IDs in the detector.
Definition: GeometryCore.h:587
std::uint32_t DBChannelID_t
* file
Definition: file_to_url.sh:69
pdgs p
Definition: selectors.fcl:22
float PedMeanErr(DBChannelID_t ch) const override
std::uint64_t DBTimeStamp_t
Class def header for a class DetPedestalRetrievalAlg.
void SetPedRms(float pedRms)
Definition: DetPedestal.h:39
float PedMean() const
Definition: DetPedestal.h:33
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:41
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:40
Signal from induction planes.
Definition: geo_types.h:145
bool DBUpdate() const
Do actual database updates.
DataSource
Is this data-file representing beam spills or cosmic spills?
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:34
const IOVTimeStamp & End() const
Definition of data types for geometry description.
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
unsigned long Stamp() const
Definition: IOVTimeStamp.h:37
float PedRmsErr() const
Definition: DetPedestal.h:36
void SetPedMean(float pedMean)
Definition: DetPedestal.h:38
Collection of exception classes for IOVData.
const IOVTimeStamp & Begin() const
Get Timestamp information.
float PedRmsErr(DBChannelID_t ch) const override
float PedMean(DBChannelID_t ch) const override
float PedRms(DBChannelID_t ch) const override
static IOVTimeStamp MaxTimeStamp()
float PedRms() const
Definition: DetPedestal.h:34
void UpdateTimeStamp(DBTimeStamp_t ts)
Update event time stamp.
art framework interface to geometry description
BEGIN_PROLOG could also be cout
void Reconfigure(fhicl::ParameterSet const &p) override
Reconfigure function called by fhicl constructor.
Signal from collection planes.
Definition: geo_types.h:146