XYZCalibProtoDUNE.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \file XYZCalibProtoDUNE.cxx
3 //
4 // \brief implementation of class for accessing (x,y,z) calibration data for ProtoDUNE
5 //
6 // \author jpaley@fnal.gov
7 //
8 ////////////////////////////////////////////////////////////////////////
9 
10 // C++ language includes
11 #include <iostream>
12 #include <fstream>
13 #include <string>
14 #include <vector>
15 #include "math.h"
16 #include "stdio.h"
17 
18 // LArSoft includes
20 
21 // nutools includes
22 #include "nuevdb/IFDatabase/Table.h"
23 
24 // Framework includes
25 #include "cetlib_except/exception.h"
27 
28 //-----------------------------------------------
30 {
31  fIsMC = true;
32  fYZCorrLoaded = false;
33  fXCorrLoaded = false;
34  fNormCorrLoaded = false;
35  fInterpolate = false;
36  fCurrentTS = 0;
37  fXCorrFileName="";
38  fYZCorrFileName="";
40  fXCorrDBTag="";
41  fYZCorrDBTag="";
42  fNormCorrDBTag="";
43 
44 }
45 
46 
47 //-----------------------------------------------
49  fhicl::ParameterSet const& pset
50 )
51 {
52  fIsMC = true;
53  fYZCorrLoaded = false;
54  fXCorrLoaded = false;
55  fNormCorrLoaded = false;
56  fInterpolate = false;
57  fCurrentTS = 0;
58  fXCorrFileName="";
59  fYZCorrFileName="";
61  fXCorrDBTag="";
62  fYZCorrDBTag="";
63  fNormCorrDBTag="";
64  Configure(pset);
65 }
66 
67 //------------------------------------------------
69 {
70  fIsMC = pset.get<bool>("IsMC");
71  fUseCondbXYZCorr = pset.get<bool>("UseCondbXYZCorr");
72  fInterpolate = pset.get<bool>("Interpolate");
73  fXCorrFileName = pset.get<std::string>("XCorrFileName");
74  fYZCorrFileName = pset.get<std::string>("YZCorrFileName");
75  fNormCorrFileName= pset.get<std::string>("NormCorrFileName");
76  fXCorrDBTag = pset.get<std::string>("XCorrDBTag");
77  fYZCorrDBTag = pset.get<std::string>("YZCorrDBTag");
78  fNormCorrDBTag = pset.get<std::string>("NormCorrDBTag");
79  return true;
80 }
81 
82 //------------------------------------------------
84 {
85 
86  if (fYZCorrLoaded && ts != fCurrentTS) {
87  fYZCorrHist.clear();
88  fYZCorrLoaded = false;
89  }
90 
91  if (fXCorrLoaded && ts != fCurrentTS) {
92  fXCorrHist.clear();
93  fXCorrLoaded = false;
94  }
95 
96  if (fNormCorrLoaded && ts != fCurrentTS) {
97  fNormCorr.clear();
98  fNormCorrLoaded = false;
99  }
100 
101  fCurrentTS = ts;
102  // all done!
103 
104  return true;
105 }
106 
107 //------------------------------------------------
109 {
110  if (!fNormCorrLoaded) this->LoadNormCorr();
111 
112  if (fNormCorr.find(chanId) == fNormCorr.end()) {
113  mf::LogError("XYZCalibProtoDUNE") << "Plane not found!";
114  return 0.;
115  }
116 
117  return fNormCorr[chanId].corr;
118 }
119 
120 //------------------------------------------------
121 double calib::XYZCalibProtoDUNE::GetXCorr(int plane, double x)
122 {
123  if (!fXCorrLoaded) this->LoadXCorr();
124 
125  if (fXCorrHist.find(plane) == fXCorrHist.end()) {
126  mf::LogError("XYZCalibProtoDUNE") << "Plane not found!";
127  return 0.;
128  }
129 
130  if (fInterpolate)
131  return fXCorrHist[plane].Interpolate(x);
132  else {
133  int ix = fXCorrHist[plane].FindBin(x);
134  return fXCorrHist[plane].GetBinContent(ix);
135  }
136 
137 }
138 
139 //------------------------------------------------
140 double calib::XYZCalibProtoDUNE::GetYZCorr(int plane, int side,
141  double y, double z)
142 {
143  if (!fYZCorrLoaded) this->LoadYZCorr();
144 
145  int chanId = plane*10+side;
146 
147  if (fYZCorrHist.find(chanId) == fYZCorrHist.end()) {
148  mf::LogError("XYZCalibProtoDUNE") << "Plane not found!";
149  return 0.;
150  }
151 
152  if (fInterpolate)
153  return fYZCorrHist[chanId].Interpolate(z,y);
154  else {
155  int iz = fYZCorrHist[chanId].GetXaxis()->FindBin(z);
156  int iy = fYZCorrHist[chanId].GetYaxis()->FindBin(y);
157  return fYZCorrHist[chanId].GetBinContent(iz,iy);
158  }
159 
160 }
161 
162 //------------------------------------------------
164 {
165  if (!fUseCondbXYZCorr) return true;
166 
167  if (fNormCorrLoaded) return true;
168 
169  nutools::dbi::Table NormCorrTable;
170 
171  NormCorrTable.SetDetector("pdunesp");
172  NormCorrTable.SetTableName("distcorrnorm");
173  NormCorrTable.SetTableType(nutools::dbi::kConditionsTable);
174  NormCorrTable.SetDataTypeMask(nutools::dbi::kDataOnly);
175  if (fIsMC)
176  NormCorrTable.SetDataTypeMask(nutools::dbi::kMCOnly);
177 
178  int normIdx = NormCorrTable.AddCol("norm","double");
179  int normErrIdx = NormCorrTable.AddCol("norm_err","double");
180 
181  NormCorrTable.SetMinTSVld(fCurrentTS);
182  NormCorrTable.SetMaxTSVld(fCurrentTS);
183  NormCorrTable.SetTag(fNormCorrDBTag);
184 
185  NormCorrTable.SetVerbosity(100);
186 
187  bool readOk = false;
188  if (!fNormCorrFileName.empty())
189  readOk = NormCorrTable.LoadFromCSV(fNormCorrFileName);
190  else
191  readOk = NormCorrTable.Load();
192 
193  if (! readOk) {
194  mf::LogError("XYZCalibProtoDUNE") << "Load from norm calib database table failed.";
195 
196  return false; //std::abort();
197 
198  }
199 
200  if (NormCorrTable.NRow() == 0) {
201  mf::LogError("XYZCalibProtoDUNE") << "Number of rows in norm calib table is 0. This should never be the case!";
202  return false;
203  }
204 
205  nutools::dbi::Row* row;
206  uint64_t chan;
207  for (int i=0; i<NormCorrTable.NRow(); ++i) {
209  row = NormCorrTable.GetRow(i);
210  chan = row->Channel();
211  row->Col(normIdx).Get(norm.corr);
212  row->Col(normErrIdx).Get(norm.corr_err);
213  fNormCorr[chan] = norm;
214  }
215 
216  fNormCorrLoaded = true;
217  return true;
218 }
219 
220 //------------------------------------------------
222 {
223  if (!fUseCondbXYZCorr) return true;
224 
225  if (fXCorrLoaded) return true;
226 
227  nutools::dbi::Table XCorrTable;
228 
229  XCorrTable.SetDetector("pdunesp");
230  XCorrTable.SetTableName("distcorrx");
231  XCorrTable.SetTableType(nutools::dbi::kConditionsTable);
232  XCorrTable.SetDataTypeMask(nutools::dbi::kDataOnly);
233  if (fIsMC)
234  XCorrTable.SetDataTypeMask(nutools::dbi::kMCOnly);
235 
236  int shapeIdx = XCorrTable.AddCol("shape","double");
237  int shapeErrIdx = XCorrTable.AddCol("shape_err","double");
238  int xIdx = XCorrTable.AddCol("x","double");
239  int dxIdx = XCorrTable.AddCol("dx","double");
240 
241  XCorrTable.SetMinTSVld(fCurrentTS);
242  XCorrTable.SetMaxTSVld(fCurrentTS);
243  XCorrTable.SetTag(fXCorrDBTag);
244 
245  XCorrTable.SetVerbosity(100);
246 
247  bool readOk = false;
248  if (!fXCorrFileName.empty())
249  readOk = XCorrTable.LoadFromCSV(fXCorrFileName);
250  else
251  readOk = XCorrTable.Load();
252 
253  if (! readOk) {
254  mf::LogError("XYZCalibProtoDUNE") << "Load from x calib database table failed.";
255  return false; //std::abort();
256  }
257 
258  if (XCorrTable.NRow() == 0) {
259  mf::LogError("XYZCalibProtoDUNE") << "Number of rows in x calib table is 0. This should never be the case!";
260  return false;
261  }
262 
263  nutools::dbi::Row* row;
264  uint64_t chan;
265  int plane;
266  std::vector<int> planeVec;
267 
268  std::map<int,std::vector<XCorr_t> > fXCorr;
269 
270  for (int i=0; i<XCorrTable.NRow(); ++i) {
271  row = XCorrTable.GetRow(i);
272  chan = row->Channel();
273  plane = int(chan/10000);
274 
275  XCorr_t xcorr;
276  row->Col(xIdx).Get(xcorr.x);
277  row->Col(dxIdx).Get(xcorr.dx);
278  row->Col(shapeIdx).Get(xcorr.corr);
279  row->Col(shapeErrIdx).Get(xcorr.corr_err);
280 
281  if (fXCorr.find(plane) == fXCorr.end()) {
282  planeVec.push_back(plane);
283  std::vector<XCorr_t> xcorrVec;
284  fXCorr[plane] = xcorrVec;
285  }
286 
287  fXCorr[plane].push_back(xcorr);
288 
289  }
290 
291  // sort the x-corrections by x for easy look-up
292  for (unsigned int i=0; i<planeVec.size(); ++i) {
293  int ip = planeVec[i];
294  std::sort(fXCorr[ip].begin(),fXCorr[ip].end());
295  char hname[256];
296  sprintf(hname,"xCorrHist_%d",ip);
297  int nbins = int(fXCorr[ip].size());
298  double xmin = fXCorr[ip][0].x;
299  double xmax = fXCorr[ip][nbins-1].x;
300  double bw = (xmax-xmin)/(nbins-1);
301  fXCorrHist[ip] = TH1F(hname,"",nbins,xmin-bw/2,xmax+bw/2);
302  std::cout<<"fXcorrHist["<<ip<<"] binning: Nbins = "<<fXCorrHist[ip].GetNbinsX()<<" Xmin = "<<fXCorrHist[ip].GetXaxis()->GetXmin()<<" Xmax = "<<fXCorrHist[ip].GetXaxis()->GetXmax()<<std::endl;
303  for (unsigned int j=0; j<fXCorr[ip].size(); ++j)
304  fXCorrHist[ip].SetBinContent(j+1,fXCorr[ip][j].corr);
305  }
306 
307  fXCorrLoaded = true;
308  return true;
309 
310 }
311 
312 //------------------------------------------------
314 {
315  if (!fUseCondbXYZCorr) return true;
316 
317  if (fYZCorrLoaded) return true;
318 
319  nutools::dbi::Table YZCorrTable;
320 
321  YZCorrTable.SetDetector("pdunesp");
322  YZCorrTable.SetTableName("distcorryz");
323  YZCorrTable.SetTableType(nutools::dbi::kConditionsTable);
324  YZCorrTable.SetDataTypeMask(nutools::dbi::kDataOnly);
325  if (fIsMC)
326  YZCorrTable.SetDataTypeMask(nutools::dbi::kMCOnly);
327 
328  int corrIdx = YZCorrTable.AddCol("corr","double");
329  int corrErrIdx = YZCorrTable.AddCol("corr_err","double");
330  int yIdx = YZCorrTable.AddCol("y","double");
331  // int dyIdx = YZCorrTable.AddCol("dy","double");
332  int zIdx = YZCorrTable.AddCol("z","double");
333  // int dzIdx = YZCorrTable.AddCol("dz","double");
334 
335  YZCorrTable.SetMinTSVld(fCurrentTS);
336  YZCorrTable.SetMaxTSVld(fCurrentTS);
337  YZCorrTable.SetTag(fYZCorrDBTag);
338 
339  YZCorrTable.SetVerbosity(100);
340 
341  bool readOk = false;
342  if (!fYZCorrFileName.empty())
343  readOk = YZCorrTable.LoadFromCSV(fYZCorrFileName);
344  else
345  readOk = YZCorrTable.Load();
346 
347  if (! readOk) {
348  mf::LogError("XYZCalibProtoDUNE") << "Load from yz calib database table failed.";
349  return false; //std::abort();
350  }
351 
352  if (YZCorrTable.NRow() == 0) {
353  mf::LogError("XYZCalibProtoDUNE") << "Number of rows in yz calib table is 0. This should never be the case!";
354  return false;
355  }
356 
357  nutools::dbi::Row* row;
358  uint64_t chan;
359  std::vector<int> planeVec;
360 
361  std::map<int,std::vector<YZCorr_t> > fYZCorr;
362 
363  for (int i=0; i<YZCorrTable.NRow(); ++i) {
364  row = YZCorrTable.GetRow(i);
365  chan = row->Channel();
366  int plane = chan/10000000;
367  int side = (chan-plane*10000000)/1000000;
368  int chanId = plane*10+side;
369 
370  YZCorr_t yzcorr;
371  row->Col(yIdx).Get(yzcorr.y);
372  // row->Col(dyIdx).Get(yzcorr.dy);
373  row->Col(zIdx).Get(yzcorr.z);
374  // row->Col(dzIdx).Get(yzcorr.dz);
375  row->Col(corrIdx).Get(yzcorr.corr);
376  row->Col(corrErrIdx).Get(yzcorr.corr_err);
377 
378  if (fYZCorr.find(chanId) == fYZCorr.end())
379  planeVec.push_back(chanId);
380 
381  fYZCorr[chanId].push_back(yzcorr);
382 
383  }
384 
385  // sort the (y,z)-corrections by y and then by z for easy look-up
386  int nbinsy=0;
387  int nbinsz=0;
388  double ymin=0.;
389  double ymax=0.;
390  double zmin=0;
391  double zmax=0.;
392  double bwy=0.;
393  double bwz=0.;
394 
395  for (unsigned int i=0; i<planeVec.size(); ++i) {
396  int ip = planeVec[i];
397  std::sort(fYZCorr[ip].begin(),fYZCorr[ip].end());
398  if (i==0) {
399  ymin = fYZCorr[ip][0].y;
400  zmin = fYZCorr[ip][0].z;
401  ymax = fYZCorr[ip][fYZCorr[ip].size()-1].y;
402  zmax = fYZCorr[ip][fYZCorr[ip].size()-1].z;
403  // now figure out how many z bins there are
404  for (unsigned j=1; j<fYZCorr[ip].size(); ++j, ++nbinsz)
405  if (fYZCorr[ip][j].z < fYZCorr[ip][j-1].z) break;
406  nbinsy = int(fYZCorr[ip].size())/++nbinsz;
407  bwz = (zmax-zmin)/(nbinsz-1);
408  bwy = (ymax-ymin)/(nbinsy-1);
409  }
410  char hname[256];
411  sprintf(hname,"yzCorrHist_%d",i);
412  fYZCorrHist[ip] = TH2F(hname,"",nbinsz,zmin-bwz/2,zmax+bwz/2,
413  nbinsy,ymin-bwy/2,ymax+bwy/2);
414  std::cout<<"fYZcorrHist["<<ip<<"] binning: Nbins = "<<fYZCorrHist[ip].GetNbinsX()<<" Xmin = "<<fYZCorrHist[ip].GetXaxis()->GetXmin()<<" Xmax = "<<fYZCorrHist[ip].GetXaxis()->GetXmax()<<" Ymin = "<<fYZCorrHist[ip].GetYaxis()->GetXmin()<<" Ymax = "<<fYZCorrHist[ip].GetYaxis()->GetXmax()<<std::endl;
415 
416  for (unsigned int j=0; j<fYZCorr[ip].size(); ++j)
417  fYZCorrHist[ip].Fill(fYZCorr[ip][j].z,fYZCorr[ip][j].y,fYZCorr[ip][j].corr);
418  }
419 
420  fYZCorrLoaded = true;
421  return true;
422 
423 }
424 
425 
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
std::map< int, TH1F > fXCorrHist
bool Update(uint64_t ts=0)
virtual double GetNormCorr(int plane) override
std::string string
Definition: nybbler.cc:12
bool Configure(fhicl::ParameterSet const &pset)
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
virtual double GetYZCorr(int plane, int side, double y, double z) override
std::map< int, TH2F > fYZCorrHist
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
std::map< int, NormCorr_t > fNormCorr
T get(std::string const &key) const
Definition: ParameterSet.h:271
auto norm(Vector const &v)
Return norm of the specified vector.
list x
Definition: train.py:276
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
virtual double GetXCorr(int plane, double x) override
QTextStream & endl(QTextStream &s)