LifetimeCalibProtoDUNE.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \file LifetimeCalibProtoDUNE.cxx
3 //
4 // \brief implementation of class for accessing (x,y,z) calibration data for ProtoDUNE
5 //
6 // \author tjyang@fnal.gov
7 // wwu@fnal.gov
8 //
9 ////////////////////////////////////////////////////////////////////////
10 
11 // C++ language includes
12 #include <iostream>
13 #include <fstream>
14 #include <string>
15 #include <vector>
16 #include "math.h"
17 #include "stdio.h"
18 #include <iomanip>
19 
20 // LArSoft includes
22 
23 // nutools includes
24 #include "nuevdb/IFDatabase/Table.h"
25 
26 // Framework includes
27 #include "cetlib_except/exception.h"
29 
30 //-----------------------------------------------
32 {
33  fIsMC = true;
34  fLifetimeLoaded = false;
35  fInterpolate = false;
36  fCurrentTS = 0;
38  fLifetimeDBTag="";
39 }
40 
41 
42 //-----------------------------------------------
44  fhicl::ParameterSet const& pset
45 )
46 {
47  fIsMC = true;
48  fLifetimeLoaded = false;
49  fInterpolate = false;
50  fCurrentTS = 0;
52  fLifetimeDBTag="";
53  Configure(pset);
54 }
55 
56 //------------------------------------------------
58 {
59  fIsMC = pset.get<bool>("IsMC");
60  fUseCondbLifetime = pset.get<bool>("UseCondbLifetime");
61  fInterpolate = pset.get<bool>("Interpolate");
62  fLifetimeFileName = pset.get<std::string>("LifetimeFileName");
63  fLifetimeDBTag = pset.get<std::string>("LifetimeDBTag");
64  return true;
65 }
66 
67 //------------------------------------------------
69 {
70 
71  if (fLifetimeLoaded && ts != fCurrentTS) {
72  fLifetimePurMon.clear();
73  fLifetimeLoaded = false;
74  }
75 
76  fCurrentTS = ts;
77  // all done!
78 
79  return true;
80 }
81 
82 //------------------------------------------------
84 {
85  if (!fLifetimeLoaded) this->LoadLifetime();
86 
87  if (fLifetimePurMon.find(1) == fLifetimePurMon.end()) {
88  mf::LogError("LifetimeCalibProtoDUNE") << "channel not found!";
89  return 0.;
90  }
91 
92  return fLifetimePurMon[1].center;
93 }
94 
95 //------------------------------------------------
97 {
98  if (!fLifetimeLoaded) this->LoadLifetime();
99 
100  if (fLifetimePurMon.find(1) == fLifetimePurMon.end()) {
101  mf::LogError("LifetimeCalibProtoDUNE") << "channel not found!";
102  return 0.;
103  }
104 
105  return fLifetimePurMon[1].high;
106 }
107 
108 //------------------------------------------------
110 {
111  if (!fLifetimeLoaded) this->LoadLifetime();
112 
113  if (fLifetimePurMon.find(1) == fLifetimePurMon.end()) {
114  mf::LogError("LifetimeCalibProtoDUNE") << "channel not found!";
115  return 0.;
116  }
117 
118  return fLifetimePurMon[1].low;
119 }
120 
121 //------------------------------------------------
123 {
124  if (!fUseCondbLifetime) return true;
125 
126  if (fLifetimeLoaded) return true;
127 
128  nutools::dbi::Table LifetimePurMonTable;
129  LifetimePurMonTable.SetDetector("pdunesp");
130  LifetimePurMonTable.SetTableName("lifetime_purmon");
131  LifetimePurMonTable.SetTableType(nutools::dbi::kConditionsTable);
132  LifetimePurMonTable.SetDataTypeMask(nutools::dbi::kDataOnly);
133  if (fIsMC) LifetimePurMonTable.SetDataTypeMask(nutools::dbi::kMCOnly);
134 
135  int centerIdx = LifetimePurMonTable.AddCol("center", "double");
136  int lowIdx = LifetimePurMonTable.AddCol("low", "double");
137  int highIdx = LifetimePurMonTable.AddCol("high", "double");
138 
139  if (!fInterpolate) {
140  LifetimePurMonTable.SetMinTSVld(fCurrentTS); // only load one previous lifetime
141  LifetimePurMonTable.SetMaxTSVld(fCurrentTS);
142  }
143  else {
144  // load lifetime IOV: runtime +- 1 days for interpolation
145  LifetimePurMonTable.SetMinTSVld(fCurrentTS-1*86400.);
146  LifetimePurMonTable.SetMaxTSVld(fCurrentTS+1*86400.);
147  }
148  LifetimePurMonTable.SetTag(fLifetimeDBTag);
149 
150  LifetimePurMonTable.SetVerbosity(100);
151 
152  bool readOk = false;
153  if (!fLifetimeFileName.empty())
154  readOk = LifetimePurMonTable.LoadFromCSV(fLifetimeFileName);
155  else
156  readOk = LifetimePurMonTable.Load();
157 
158  if (! readOk) {
159  mf::LogError("LifetimeCalibProtoDUNE") << "Load from lifetime calib database table failed.";
160  throw cet::exception("LifetimeCalibProtoDUNE") << "Failed to query lifetime database. Please check URL: https://dbdata0vm.fnal.gov:9443/dune_con_prod/app" << "\n";
161 
162  return false; //std::abort();
163  }
164 
165  if (LifetimePurMonTable.NRow() == 0) {
166  mf::LogError("LifetimeCalibProtoDUNE") << "Number of rows in lifetime calib table is 0. This should never be the case!";
167  return false;
168  }
169 
170  std::vector<double> loaded_tv;
171  std::vector<double> loaded_center;
172  std::vector<double> loaded_low;
173  std::vector<double> loaded_high;
174  loaded_tv.clear();
175  loaded_center.clear();
176  loaded_low.clear();
177  loaded_high.clear();
178 
179  if (LifetimePurMonTable.NRow() == 1) fInterpolate = false;
180 
181  nutools::dbi::Row* row;
182  uint64_t chan;
183 
184  for (int i=0; i<LifetimePurMonTable.NRow(); ++i) {
185 
186  LifetimePurMon_t lifetime;
187  row = LifetimePurMonTable.GetRow(i);
188  chan = row->Channel(); // One channel only, start with 1
189 
190  if (chan != 1) {
191  mf::LogError("LifetimeCalibProtoDUNE") << "Channel numuber in lifetime calib table is not 1. This should never be the case!";
192  return false;
193  }
194 
195  row->Col(centerIdx).Get(lifetime.center);
196  row->Col(lowIdx).Get(lifetime.low);
197  row->Col(highIdx).Get(lifetime.high);
198 
199  loaded_tv.push_back(row->VldTime());
200  loaded_center.push_back(lifetime.center);
201  loaded_low.push_back(lifetime.low);
202  loaded_high.push_back(lifetime.high);
203 
204  if (!fInterpolate) fLifetimePurMon[chan] = lifetime;
205  }
206 
207  if (fInterpolate && loaded_tv.size()>1) {
208 
209  LifetimePurMon_t interpolate_lifetime;
210 
211  // find the previous and following lifetime for fCurrentTS
212  int t0_idx = -1;
213  int t1_idx = -1;
214  double temp_0 = 1e10;
215  double temp_1 = 1e10;
216  for (size_t j=0; j<loaded_tv.size(); j++) {
217  if ( fCurrentTS - loaded_tv[j] >= 0 && fCurrentTS - loaded_tv[j] < temp_0 ) {
218  temp_0 = fCurrentTS- loaded_tv[j];
219  t0_idx = j;
220  }
221 
222  if ( loaded_tv[j]-fCurrentTS >= 0 && loaded_tv[j]-fCurrentTS < temp_1 ) {
223  temp_1 = loaded_tv[j]-fCurrentTS;
224  t1_idx = j;
225  }
226  }
227 
228  if (t0_idx == -1) { // only lifetime measurement after fCurrentTS during selected IOV
229  // use the following one only
230  interpolate_lifetime.center = loaded_center[t1_idx];
231  interpolate_lifetime.low = loaded_low[t1_idx];
232  interpolate_lifetime.high = loaded_high[t1_idx];
233  }
234  if (t1_idx == -1) { // only lifetime measurement before fCurrentTS during selected IOV
235  // use the previous one only
236  interpolate_lifetime.center = loaded_center[t0_idx];
237  interpolate_lifetime.low = loaded_low[t0_idx];
238  interpolate_lifetime.high = loaded_high[t0_idx];
239  }
240  if (t0_idx != -1 && t1_idx != -1) {
241  if (t0_idx == t1_idx) { // ideally, this is not possible
242  interpolate_lifetime.center = loaded_center[t0_idx];
243  interpolate_lifetime.low = loaded_low[t0_idx];
244  interpolate_lifetime.high = loaded_high[t0_idx];
245  }
246  else {
247  // do linear interpolation using previous one and following one
248  // refer: https://en.wikipedia.org/wiki/Linear_interpolation
249  interpolate_lifetime.center = ( loaded_center[t0_idx]*(loaded_tv[t1_idx]-fCurrentTS) + loaded_center[t1_idx]*(fCurrentTS-loaded_tv[t0_idx]) ) / (loaded_tv[t1_idx]-loaded_tv[t0_idx]);
250  interpolate_lifetime.low = ( loaded_low[t0_idx]*(loaded_tv[t1_idx]-fCurrentTS) + loaded_low[t1_idx]*(fCurrentTS-loaded_tv[t0_idx]) ) / (loaded_tv[t1_idx]-loaded_tv[t0_idx]);
251  interpolate_lifetime.high = ( loaded_high[t0_idx]*(loaded_tv[t1_idx]-fCurrentTS) + loaded_high[t1_idx]*(fCurrentTS-loaded_tv[t0_idx]) ) / (loaded_tv[t1_idx]-loaded_tv[t0_idx]);
252  }
253  }
254 
255  fLifetimePurMon[chan] = interpolate_lifetime;
256  } // end if fInterpolate
257 
258  fLifetimeLoaded = true;
259  return true;
260 }
261 
262 
263 
std::string string
Definition: nybbler.cc:12
std::map< int, LifetimePurMon_t > fLifetimePurMon
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
T get(std::string const &key) const
Definition: ParameterSet.h:271
bool Configure(fhicl::ParameterSet const &pset)
virtual double GetLifetime() override
virtual double GetLifetimeHigh() override
virtual double GetLifetimeLow() override
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33