Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MbdCalib.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MbdCalib.cc
1 #include "MbdCalib.h"
2 
3 #include "MbdDefs.h"
4 #include "MbdGeomV1.h"
5 
6 // Database Includes
8 
9 #include <cdbobjects/CDBTTree.h>
10 
11 #include <phool/phool.h>
12 
13 #include <cmath>
14 #include <filesystem>
15 #include <fstream>
16 #include <iostream>
17 #include <regex>
18 #include <string>
19 
21 {
22  Reset();
24  _mbdgeom = std::make_unique<MbdGeomV1>();
25 
26  if (_rc->FlagExist("MBD_TEMPLATEFIT"))
27  {
28  do_templatefit = _rc->get_IntFlag("MBD_TEMPLATEFIT");
29  }
30  else
31  {
32  do_templatefit = 1;
33  }
34 }
35 
37 {
38  std::cout << PHWHERE << std::endl;
39  _status = 0;
40 
41  if (Verbosity() > 0)
42  {
43  std::cout << "MBD CDB " << _rc->get_StringFlag("CDB_GLOBALTAG") << "\t" << _rc->get_uint64Flag("TIMESTAMP") << std::endl;
44  }
46  // if rc flag MBD_CALDIR does not exist, we create it and set it to an empty string
47  if (!_rc->FlagExist("MBD_CALDIR"))
48  {
49  Verbosity(1);
50  std::string sampmax_url = _cdb->getUrl("MBD_SAMPMAX");
51  if (Verbosity() > 0)
52  {
53  std::cout << "sampmax_url " << sampmax_url << std::endl;
54  }
55  Download_SampMax(sampmax_url);
56 
57  std::string qfit_url = _cdb->getUrl("MBD_QFIT");
58  if (Verbosity() > 0)
59  {
60  std::cout << "qfit_url " << qfit_url << std::endl;
61  }
62  Download_Gains(qfit_url);
63 
64  /*
65  std::string tt_t0_url = _cdb->getUrl("MBD_TT_T0");
66  if ( Verbosity() > 0 ) std::cout << "tt_t0_url " << tt_t0_url << std::endl;
67  Download_TTT0(tt_t0_url);
68  */
69 
70  std::string tq_t0_url = _cdb->getUrl("MBD_TQ_T0");
71  if (Verbosity() > 0)
72  {
73  std::cout << "tq_t0_url " << tq_t0_url << std::endl;
74  }
75  Download_TQT0(tq_t0_url);
76 
77  /*
78  std::string slew_url = _cdb->getUrl("MBD_SLEW");
79  if ( Verbosity() > 0 ) std::cout << "slew_url " << slew_url << std::endl;
80  Download_Slew(slew_url);
81  */
82 
83  if (do_templatefit)
84  {
85  std::string shape_url = _cdb->getUrl("MBD_SHAPES");
86  if (Verbosity() > 0)
87  {
88  std::cout << "shape_url " << shape_url << std::endl;
89  }
90  Download_Shapes(shape_url);
91  }
92  Verbosity(0);
93  }
94  else
95  {
96  std::string bbc_caldir = _rc->get_StringFlag("MBD_CALDIR");
97  std::cout << "Reading MBD Calibrations from " << bbc_caldir << std::endl;
98 
99  std::string sampmax_file = bbc_caldir + "/bbc_sampmax.calib";
100  Download_SampMax(sampmax_file);
101 
102  std::string qfit_file = bbc_caldir + "/bbc_qfit.calib";
103  Download_Gains(qfit_file);
104 
105  // std::string tt_t0_file = bbc_caldir + "/bbc_tt_t0.calib";
106  // Download_TTT0(tt_t0_file);
107 
108  std::string tq_t0_file = bbc_caldir + "/bbc_tq_t0.calib";
109  Download_TQT0(tq_t0_file);
110 
111  // std::string slew_file = bbc_caldir + "/bbc_slew.calib";
112  // Download_Slew(slew_file);
113 
114  if (do_templatefit)
115  {
116  std::string shape_file = bbc_caldir + "/bbc_shape.calib";
117  Download_Shapes(shape_file);
118  }
119  }
120 
121  return _status;
122 }
123 
124 int MbdCalib::Download_Gains(const std::string& dbase_location)
125 {
126  // Reset All Values
127  _qfit_integ.fill(std::numeric_limits<float>::quiet_NaN());
128  _qfit_mpv.fill(std::numeric_limits<float>::quiet_NaN());
129  _qfit_sigma.fill(std::numeric_limits<float>::quiet_NaN());
130  _qfit_integerr.fill(std::numeric_limits<float>::quiet_NaN());
131  _qfit_mpverr.fill(std::numeric_limits<float>::quiet_NaN());
132  _qfit_sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
133  _qfit_chi2ndf.fill(std::numeric_limits<float>::quiet_NaN());
134 
135  if (Verbosity() > 0)
136  {
137  std::cout << "Opening " << dbase_location << std::endl;
138  }
139  std::filesystem::path dbase_file = dbase_location;
140  if (dbase_file.extension() == ".root") // read from database
141  {
142  CDBTTree* cdbttree = new CDBTTree(dbase_location);
143  cdbttree->LoadCalibrations();
144 
145  for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
146  {
147  _qfit_integ[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_integ");
148  _qfit_mpv[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_mpv");
149  _qfit_sigma[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_sigma");
150  _qfit_integerr[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_integerr");
151  _qfit_mpverr[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_mpverr");
152  _qfit_sigmaerr[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_sigmaerr");
153  _qfit_chi2ndf[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_chi2ndf");
154  if (Verbosity() > 0)
155  {
156  if (ipmt < 5)
157  {
158  std::cout << ipmt << "\t" << _qfit_mpv[ipmt] << std::endl;
159  }
160  }
161  }
162  delete cdbttree;
163  }
164  else if (dbase_file.extension() == ".calib") // read from text file
165  {
166  std::ifstream infile(dbase_location);
167  if (!infile.is_open())
168  {
169  std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
170  _status = -3;
171  return _status;
172  }
173 
174  int pmt = -1;
175  while (infile >> pmt)
176  {
177  infile >> _qfit_integ[pmt] >> _qfit_mpv[pmt] >> _qfit_sigma[pmt] >> _qfit_integerr[pmt] >> _qfit_mpverr[pmt] >> _qfit_sigmaerr[pmt] >> _qfit_chi2ndf[pmt];
178  if (Verbosity() > 0)
179  {
180  if (pmt < 5 || pmt >= MbdDefs::MBD_N_PMT - 5)
181  {
182  std::cout << pmt << "\t" << _qfit_integ[pmt] << "\t" << _qfit_mpv[pmt] << "\t" << _qfit_sigma[pmt]
183  << "\t" << _qfit_integerr[pmt] << "\t" << _qfit_mpverr[pmt] << "\t" << _qfit_sigmaerr[pmt]
184  << "\t" << _qfit_chi2ndf[pmt] << std::endl;
185  }
186  }
187  }
188  }
189  else
190  {
191  std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
192  _status = -1;
193  return _status;
194  }
195 
196  return 1;
197 }
198 
199 int MbdCalib::Download_TQT0(const std::string& dbase_location)
200 {
201  // Reset All Values
202  _tqfit_t0mean.fill(std::numeric_limits<float>::quiet_NaN());
203  _tqfit_t0meanerr.fill(std::numeric_limits<float>::quiet_NaN());
204  _tqfit_t0sigma.fill(std::numeric_limits<float>::quiet_NaN());
205  _tqfit_t0sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
206 
207  if (Verbosity() > 0)
208  {
209  std::cout << "Opening " << dbase_location << std::endl;
210  }
211  std::filesystem::path dbase_file = dbase_location;
212  if (dbase_file.extension() == ".root") // read from database
213  {
214  CDBTTree* cdbttree = new CDBTTree(dbase_location);
215  cdbttree->LoadCalibrations();
216 
217  for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
218  {
219  _tqfit_t0mean[ipmt] = cdbttree->GetFloatValue(ipmt, "tqfit_t0mean");
220  _tqfit_t0meanerr[ipmt] = cdbttree->GetFloatValue(ipmt, "tqfit_t0meanerr");
221  _tqfit_t0sigma[ipmt] = cdbttree->GetFloatValue(ipmt, "tqfit_t0sigma");
222  _tqfit_t0sigmaerr[ipmt] = cdbttree->GetFloatValue(ipmt, "tqfit_t0sigmaerr");
223  if (Verbosity() > 0)
224  {
225  if (ipmt < 5 || ipmt >= MbdDefs::MBD_N_PMT - 5)
226  {
227  std::cout << ipmt << "\t" << _tqfit_t0mean[ipmt] << std::endl;
228  }
229  }
230  }
231  delete cdbttree;
232  }
233  else if (dbase_file.extension() == ".calib") // read from text file
234  {
235  std::ifstream infile(dbase_location);
236  if (!infile.is_open())
237  {
238  std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
239  _status = -3;
240  return _status;
241  }
242 
243  int pmt = -1;
244  while (infile >> pmt)
245  {
246  infile >> _tqfit_t0mean[pmt] >> _tqfit_t0meanerr[pmt] >> _tqfit_t0sigma[pmt] >> _tqfit_t0sigmaerr[pmt];
247 
248  if (Verbosity() > 0)
249  {
250  if (pmt < 5 || pmt >= MbdDefs::MBD_N_PMT - 5)
251  {
252  std::cout << pmt << "\t" << _tqfit_t0mean[pmt] << "\t" << _tqfit_t0meanerr[pmt]
253  << "\t" << _tqfit_t0sigma[pmt] << "\t" << _tqfit_t0sigmaerr[pmt] << std::endl;
254  }
255  }
256  }
257  infile.close();
258  }
259  else
260  {
261  std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
262  _status = -1;
263  return _status;
264  }
265 
266  return 1;
267 }
268 
269 int MbdCalib::Download_TTT0(const std::string& dbase_location)
270 {
271  // Reset All Values
272  _ttfit_t0mean.fill(std::numeric_limits<float>::quiet_NaN());
273  _ttfit_t0meanerr.fill(std::numeric_limits<float>::quiet_NaN());
274  _ttfit_t0sigma.fill(std::numeric_limits<float>::quiet_NaN());
275  _ttfit_t0sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
276 
277  if (Verbosity() > 0)
278  {
279  std::cout << "Opening " << dbase_location << std::endl;
280  }
281  std::filesystem::path dbase_file = dbase_location;
282  if (dbase_file.extension() == ".root") // read from database
283  {
284  CDBTTree* cdbttree = new CDBTTree(dbase_location);
285  cdbttree->LoadCalibrations();
286 
287  for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
288  {
289  _ttfit_t0mean[ipmt] = cdbttree->GetFloatValue(ipmt, "ttfit_t0mean");
290  _ttfit_t0meanerr[ipmt] = cdbttree->GetFloatValue(ipmt, "ttfit_t0meanerr");
291  _ttfit_t0sigma[ipmt] = cdbttree->GetFloatValue(ipmt, "ttfit_t0sigma");
292  _ttfit_t0sigmaerr[ipmt] = cdbttree->GetFloatValue(ipmt, "ttfit_t0sigmaerr");
293  if (Verbosity() > 0)
294  {
295  if (ipmt < 5 || ipmt >= MbdDefs::MBD_N_PMT - 5)
296  {
297  std::cout << ipmt << "\t" << _ttfit_t0mean[ipmt] << std::endl;
298  }
299  }
300  }
301  delete cdbttree;
302  }
303  else if (dbase_file.extension() == ".calib") // read from text file
304  {
305  std::ifstream infile(dbase_location);
306  if (!infile.is_open())
307  {
308  std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
309  _status = -3;
310  return _status;
311  }
312 
313  int pmt = -1;
314  while (infile >> pmt)
315  {
316  infile >> _ttfit_t0mean[pmt] >> _ttfit_t0meanerr[pmt] >> _ttfit_t0sigma[pmt] >> _ttfit_t0sigmaerr[pmt];
317 
318  if (Verbosity() > 0)
319  {
320  if (pmt < 5 || pmt >= MbdDefs::MBD_N_PMT - 5)
321  {
322  std::cout << pmt << "\t" << _ttfit_t0mean[pmt] << "\t" << _ttfit_t0meanerr[pmt]
323  << "\t" << _ttfit_t0sigma[pmt] << "\t" << _ttfit_t0sigmaerr[pmt] << std::endl;
324  }
325  }
326  }
327  infile.close();
328  }
329  else
330  {
331  std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
332  _status = -1;
333  return _status;
334  }
335 
336  return 1;
337 }
338 
339 int MbdCalib::Download_SampMax(const std::string& dbase_location)
340 {
341  // Reset All Values
342  _sampmax.fill(-1);
343 
344  std::filesystem::path dbase_file = dbase_location;
345  if (dbase_file.extension() == ".root") // read from database
346  {
347  CDBTTree* cdbttree = new CDBTTree(dbase_location);
348  cdbttree->LoadCalibrations();
349 
350  for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
351  {
352  _sampmax[ifeech] = cdbttree->GetIntValue(ifeech, "sampmax");
353  if (Verbosity() > 0)
354  {
355  if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
356  {
357  std::cout << ifeech << "\t" << _sampmax[ifeech] << std::endl;
358  }
359  }
360  }
361  delete cdbttree;
362  }
363  else if (dbase_file.extension() == ".calib") // read from text file
364  {
365  std::ifstream infile(dbase_location);
366  if (!infile.is_open())
367  {
368  std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
369  _status = -3;
370  return _status;
371  }
372 
373  int feech = -1;
374  while (infile >> feech)
375  {
376  infile >> _sampmax[feech];
377  if (Verbosity() > 0)
378  {
379  if (feech < 5 || feech >= MbdDefs::MBD_N_FEECH - 5)
380  {
381  std::cout << feech << "\t" << _sampmax[feech] << std::endl;
382  }
383  }
384  }
385  infile.close();
386  }
387  else
388  {
389  std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
390  _status = -1;
391  return _status; // file not found
392  }
393 
394  return 1;
395 }
396 
397 int MbdCalib::Download_Slew(const std::string& dbase_location)
398 {
399  // Verbosity(100);
400  if (Verbosity())
401  {
402  std::cout << "In MbdCalib::Download_Slew" << std::endl;
403  }
404  // Reset All Values
405  for (auto& slew : _slew_y)
406  {
407  slew.clear();
408  }
409 
410  std::filesystem::path dbase_file = dbase_location;
411  if (dbase_file.extension() == ".root") // read from database
412  {
413  if (Verbosity())
414  {
415  std::cout << "Reading from CDB " << dbase_location << std::endl;
416  }
417  CDBTTree* cdbttree = new CDBTTree(dbase_location);
418  cdbttree->LoadCalibrations();
419 
420  for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
421  {
422  if (_mbdgeom->get_type(ifeech) == 0)
423  {
424  continue; // skip t-channels
425  }
426 
427  _slew_npts[ifeech] = cdbttree->GetIntValue(ifeech, "slew_npts");
428  _slew_minrange[ifeech] = cdbttree->GetFloatValue(ifeech, "slew_min");
429  _slew_maxrange[ifeech] = cdbttree->GetFloatValue(ifeech, "slew_max");
430 
431  _sherr_npts[ifeech] = cdbttree->GetIntValue(ifeech, "sherr_npts");
432  _sherr_minrange[ifeech] = cdbttree->GetFloatValue(ifeech, "sherr_min");
433  _sherr_maxrange[ifeech] = cdbttree->GetFloatValue(ifeech, "sherr_max");
434 
435  for (int ipt = 0; ipt < _slew_npts[ifeech]; ipt++)
436  {
437  int chtemp = 1000 * ipt + ifeech;
438 
439  float val = cdbttree->GetFloatValue(chtemp, "slew_val");
440  _slew_y[ifeech].push_back(val);
441 
442  val = cdbttree->GetFloatValue(chtemp, "sherr_val");
443  _sherr_yerr[ifeech].push_back(val);
444  }
445 
446  if (Verbosity() > 0)
447  {
448  if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
449  {
450  std::cout << ifeech << "\t" << _slew_y[ifeech][0] << std::endl;
451  }
452  }
453  }
454  delete cdbttree;
455  }
456  else if (dbase_file.extension() == ".calib") // read from text file
457  {
458  if (Verbosity())
459  {
460  std::cout << "Reading from " << dbase_location << std::endl;
461  }
462  std::ifstream infile(dbase_location);
463  if (!infile.is_open())
464  {
465  std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
466  _status = -3;
467  return _status;
468  }
469 
470  int temp_feech = -1;
471  int temp_npoints = -1;
472  float temp_begintime = -1;
473  float temp_endtime = -1;
474  while (infile >> temp_feech >> temp_npoints >> temp_begintime >> temp_endtime)
475  {
476  if (Verbosity())
477  {
478  std::cout << "slew " << temp_feech << "\t" << temp_npoints << "\t" << temp_begintime << "\t" << temp_endtime << std::endl;
479  }
480  if (temp_feech < 0 || temp_feech > 255)
481  {
482  std::cout << "ERROR, invalid FEECH " << temp_feech << " in MBD waveforms calibration" << std::endl;
483  _status = -2;
484  return _status;
485  }
486 
487  _slew_npts[temp_feech] = temp_npoints;
488  _slew_minrange[temp_feech] = temp_begintime;
489  _slew_maxrange[temp_feech] = temp_endtime;
490 
491  float temp_val{0.};
492  for (int isamp = 0; isamp < temp_npoints; isamp++)
493  {
494  infile >> temp_val;
495  _slew_y[temp_feech].push_back(temp_val);
496  if (Verbosity() && (temp_feech == 8 || temp_feech == 255))
497  {
498  std::cout << _slew_y[temp_feech][isamp] << " ";
499  if (isamp % 10 == 9)
500  {
501  std::cout << std::endl;
502  }
503  }
504  }
505  if (Verbosity())
506  {
507  std::cout << std::endl;
508  }
509  }
510 
511  infile.close();
512  }
513  else
514  {
515  std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
516  _status = -1;
517  return _status; // file not found
518  }
519 
520  // Verbosity(0);
521  return 1;
522 }
523 
524 int MbdCalib::Download_Shapes(const std::string& dbase_location)
525 {
526  // Verbosity(100);
527  if (Verbosity())
528  {
529  std::cout << "In MbdCalib::Download_Shapes" << std::endl;
530  }
531  // Reset All Values
532  for (auto& shape : _shape_y)
533  {
534  shape.clear();
535  }
536  for (auto& sherr : _sherr_yerr)
537  {
538  sherr.clear();
539  }
540 
541  std::filesystem::path dbase_file = dbase_location;
542  if (dbase_file.extension() == ".root") // read from database
543  {
544  if (Verbosity())
545  {
546  std::cout << "Reading from CDB " << dbase_location << std::endl;
547  }
548  CDBTTree* cdbttree = new CDBTTree(dbase_location);
549  cdbttree->LoadCalibrations();
550 
551  for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
552  {
553  if (_mbdgeom->get_type(ifeech) == 0)
554  {
555  continue; // skip t-channels
556  }
557 
558  _shape_npts[ifeech] = cdbttree->GetIntValue(ifeech, "shape_npts");
559  _shape_minrange[ifeech] = cdbttree->GetFloatValue(ifeech, "shape_min");
560  _shape_maxrange[ifeech] = cdbttree->GetFloatValue(ifeech, "shape_max");
561 
562  _sherr_npts[ifeech] = cdbttree->GetIntValue(ifeech, "sherr_npts");
563  _sherr_minrange[ifeech] = cdbttree->GetFloatValue(ifeech, "sherr_min");
564  _sherr_maxrange[ifeech] = cdbttree->GetFloatValue(ifeech, "sherr_max");
565 
566  for (int ipt = 0; ipt < _shape_npts[ifeech]; ipt++)
567  {
568  int chtemp = 1000 * ipt + ifeech;
569 
570  float val = cdbttree->GetFloatValue(chtemp, "shape_val");
571  _shape_y[ifeech].push_back(val);
572 
573  val = cdbttree->GetFloatValue(chtemp, "sherr_val");
574  _sherr_yerr[ifeech].push_back(val);
575  }
576 
577  if (Verbosity() > 0)
578  {
579  if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
580  {
581  std::cout << ifeech << "\t" << _shape_y[ifeech][0] << std::endl;
582  }
583  }
584  }
585  delete cdbttree;
586  }
587  else if (dbase_file.extension() == ".calib") // read from text file
588  {
589  if (Verbosity())
590  {
591  std::cout << "Reading from " << dbase_location << std::endl;
592  }
593  std::ifstream infile(dbase_location);
594  if (!infile.is_open())
595  {
596  std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
597  _status = -3;
598  return _status;
599  }
600 
601  int temp_feech = -1;
602  int temp_npoints = -1;
603  float temp_begintime = -1;
604  float temp_endtime = -1;
605  while (infile >> temp_feech >> temp_npoints >> temp_begintime >> temp_endtime)
606  {
607  if (Verbosity())
608  {
609  std::cout << "shape " << temp_feech << "\t" << temp_npoints << "\t" << temp_begintime << "\t" << temp_endtime << std::endl;
610  }
611  if (temp_feech < 0 || temp_feech > 255)
612  {
613  std::cout << "ERROR, invalid FEECH " << temp_feech << " in MBD waveforms calibration" << std::endl;
614  _status = -2;
615  return _status;
616  }
617 
618  _shape_npts[temp_feech] = temp_npoints;
619  _shape_minrange[temp_feech] = temp_begintime;
620  _shape_maxrange[temp_feech] = temp_endtime;
621 
622  float temp_val{0.};
623  for (int isamp = 0; isamp < temp_npoints; isamp++)
624  {
625  infile >> temp_val;
626  _shape_y[temp_feech].push_back(temp_val);
627  if (Verbosity() && (temp_feech == 8 || temp_feech == 255))
628  {
629  std::cout << _shape_y[temp_feech][isamp] << " ";
630  if (isamp % 10 == 9)
631  {
632  std::cout << std::endl;
633  }
634  }
635  }
636  if (Verbosity())
637  {
638  std::cout << std::endl;
639  }
640  }
641 
642  infile.close();
643 
644  // Now read in the sherr file
645  std::string sherr_dbase_location = std::regex_replace(dbase_location, std::regex("bbc_shape.calib"), "bbc_sherr.calib");
646  if (Verbosity())
647  {
648  std::cout << "Reading from " << sherr_dbase_location << std::endl;
649  }
650  infile.open(sherr_dbase_location);
651  if (!infile.is_open())
652  {
653  std::cout << PHWHERE << "unable to open " << sherr_dbase_location << std::endl;
654  _status = -3;
655  return _status;
656  }
657 
658  temp_feech = -1;
659  temp_npoints = -1;
660  temp_begintime = -1;
661  temp_endtime = -1;
662  while (infile >> temp_feech >> temp_npoints >> temp_begintime >> temp_endtime)
663  {
664  if (Verbosity())
665  {
666  std::cout << "sheer " << temp_feech << "\t" << temp_npoints << "\t" << temp_begintime << "\t" << temp_endtime << std::endl;
667  }
668  if (temp_feech < 0 || temp_feech > 255)
669  {
670  std::cout << "ERROR, invalid FEECH " << temp_feech << " in MBD waveforms calibration" << std::endl;
671  _status = -2;
672  return _status;
673  }
674 
675  _sherr_npts[temp_feech] = temp_npoints;
676  _sherr_minrange[temp_feech] = temp_begintime;
677  _sherr_maxrange[temp_feech] = temp_endtime;
678 
679  float temp_val{0.};
680  for (int isamp = 0; isamp < temp_npoints; isamp++)
681  {
682  infile >> temp_val;
683  _sherr_yerr[temp_feech].push_back(temp_val);
684  if (Verbosity() && (temp_feech == 8 || temp_feech == 255))
685  {
686  std::cout << _sherr_yerr[temp_feech][isamp] << " ";
687  if (isamp % 10 == 9)
688  {
689  std::cout << std::endl;
690  }
691  }
692  }
693  if (Verbosity())
694  {
695  std::cout << std::endl;
696  }
697  }
698 
699  infile.close();
700  }
701  else
702  {
703  std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
704  _status = -1;
705  return _status; // file not found
706  }
707 
708  // Verbosity(0);
709  return 1;
710 }
711 
713 {
714  CDBTTree* cdbttree{nullptr};
715 
716  std::cout << "Creating " << dbfile << std::endl;
717  cdbttree = new CDBTTree(dbfile);
718  cdbttree->SetSingleIntValue("version", 1);
719  cdbttree->CommitSingle();
720 
721  std::cout << "SAMPMAX" << std::endl;
722  for (size_t ifeech = 0; ifeech < _sampmax.size(); ifeech++)
723  {
724  // store in a CDBTree
725  cdbttree->SetIntValue(ifeech, "sampmax", _sampmax[ifeech]);
726 
727  if (ifeech < 12 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
728  {
729  std::cout << ifeech << "\t" << cdbttree->GetIntValue(ifeech, "sampmax") << std::endl;
730  }
731  }
732 
733  cdbttree->Commit();
734  // cdbttree->Print();
735 
736  // for now we create the tree after reading it
737  cdbttree->WriteCDBTTree();
738  delete cdbttree;
739 
740  return 1;
741 }
742 
744 {
745  std::ofstream cal_file;
746  cal_file.open(dbfile);
747  for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
748  {
749  cal_file << ipmt << "\t" << _sampmax[ipmt] << std::endl;
750  }
751  cal_file.close();
752 
753  return 1;
754 }
755 
757 {
758  std::ofstream cal_t0_file;
759  cal_t0_file.open(dbfile);
760  for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
761  {
762  cal_t0_file << ipmt << "\t" << _tqfit_t0mean[ipmt] << "\t" << _tqfit_t0meanerr[ipmt]
763  << "\t" << _tqfit_t0sigma[ipmt] << "\t" << _tqfit_t0sigmaerr[ipmt] << std::endl;
764  }
765  cal_t0_file.close();
766 
767  return 1;
768 }
769 
771 {
772  // store in a CDBTree
773  CDBTTree* cdbttree{nullptr};
774 
775  std::cout << "Creating " << dbfile << std::endl;
776  cdbttree = new CDBTTree(dbfile);
777  cdbttree->SetSingleIntValue("version", 1);
778  cdbttree->CommitSingle();
779 
780  std::cout << "SHAPES" << std::endl;
781  for (unsigned int ifeech = 0; ifeech < _sampmax.size(); ifeech++)
782  {
783  if (_mbdgeom->get_type(ifeech) == 0)
784  {
785  continue; // skip t-channels
786  }
787 
788  cdbttree->SetIntValue(ifeech, "shape_npts", _shape_npts[ifeech]);
789  cdbttree->SetFloatValue(ifeech, "shape_min", _shape_minrange[ifeech]);
790  cdbttree->SetFloatValue(ifeech, "shape_max", _shape_maxrange[ifeech]);
791 
792  for (int ipt = 0; ipt < _shape_npts[ifeech]; ipt++)
793  {
794  int temp_ch = ipt * 1000 + ifeech;
795  cdbttree->SetFloatValue(temp_ch, "shape_val", _shape_y[ifeech][ipt]);
796  }
797 
798  cdbttree->SetIntValue(ifeech, "sherr_npts", _sherr_npts[ifeech]);
799  cdbttree->SetFloatValue(ifeech, "sherr_min", _sherr_minrange[ifeech]);
800  cdbttree->SetFloatValue(ifeech, "sherr_max", _sherr_maxrange[ifeech]);
801 
802  for (int ipt = 0; ipt < _shape_npts[ifeech]; ipt++)
803  {
804  int temp_ch = ipt * 1000 + ifeech;
805  cdbttree->SetFloatValue(temp_ch, "sherr_val", _sherr_yerr[ifeech][ipt]);
806  }
807  }
808 
809  cdbttree->Commit();
810  // cdbttree->Print();
811 
812  for (unsigned int ifeech = 0; ifeech < _sampmax.size(); ifeech++)
813  {
814  if (_mbdgeom->get_type(ifeech) == 0)
815  {
816  continue; // skip t-channels
817  }
818 
819  if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
820  {
821  std::cout << ifeech << "\t" << cdbttree->GetIntValue(ifeech, "shape_npts") << std::endl;
822  for (int ipt = 0; ipt < 10; ipt++)
823  {
824  int temp_ch = ipt * 1000 + ifeech;
825  std::cout << cdbttree->GetFloatValue(temp_ch, "shape_val") << " ";
826  }
827  std::cout << std::endl;
828  }
829  }
830 
831  // for now we create the tree after reading it
832  cdbttree->WriteCDBTTree();
833  delete cdbttree;
834 
835  return 1;
836 }
837 
839 {
840  return 1;
841 }
842 
844 {
845  // Set all initial values
846  _qfit_integ.fill(std::numeric_limits<float>::quiet_NaN());
847  _qfit_mpv.fill(std::numeric_limits<float>::quiet_NaN());
848  _qfit_sigma.fill(std::numeric_limits<float>::quiet_NaN());
849  _qfit_integerr.fill(std::numeric_limits<float>::quiet_NaN());
850  _qfit_mpverr.fill(std::numeric_limits<float>::quiet_NaN());
851  _qfit_sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
852  _qfit_chi2ndf.fill(std::numeric_limits<float>::quiet_NaN());
853 
854  _tqfit_t0mean.fill(std::numeric_limits<float>::quiet_NaN());
855  _tqfit_t0meanerr.fill(std::numeric_limits<float>::quiet_NaN());
856  _tqfit_t0sigma.fill(std::numeric_limits<float>::quiet_NaN());
857  _tqfit_t0sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
858 
859  _sampmax.fill(-1);
860 }