Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:51

0001 #include "MbdCalib.h"
0002 
0003 #include "MbdDefs.h"
0004 #include "MbdGeomV1.h"
0005 
0006 // Database Includes
0007 #ifndef ONLINE
0008 #include <ffamodules/CDBInterface.h>
0009 #include <cdbobjects/CDBTTree.h>
0010 #endif
0011 
0012 #include <phool/phool.h>
0013 
0014 #include <cmath>
0015 #include <fstream>
0016 #include <iostream>
0017 #include <regex>
0018 #include <string>
0019 
0020 #include <TString.h>
0021 
0022 
0023 MbdCalib::MbdCalib()
0024 {
0025   Reset();
0026   _mbdgeom = std::make_unique<MbdGeomV1>();
0027 
0028 #ifndef ONLINE
0029   // clang-tidy suggests to put this into the ctor but then ONLINE
0030   // does not compile anymore
0031   // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
0032   _rc = recoConsts::instance();
0033   if ( _rc->FlagExist("MBD_TEMPLATEFIT") )
0034   {
0035     do_templatefit = _rc->get_IntFlag("MBD_TEMPLATEFIT");
0036   }
0037   else
0038   {
0039     do_templatefit = 1;
0040   }
0041 #else
0042   do_templatefit = 0;
0043 #endif
0044 
0045 }
0046 
0047 int MbdCalib::Download_All()
0048 {
0049   if ( Verbosity()>0 )
0050   {
0051     std::cout << PHWHERE << " In MbdCalib::Download_All()" << std::endl;
0052   }
0053   _status = 0;
0054 
0055   std::string bbc_caldir;
0056 
0057 #ifdef ONLINE
0058   bbc_caldir = getenv("BBCCALIB");
0059   if (bbc_caldir.size()==0)
0060   {
0061     std::cout << "BBCCALIB environment variable not set" << std::endl;
0062     exit(1);
0063   }
0064 #else
0065   if (Verbosity() > 0)
0066   {
0067     std::cout << "MBD CDB " << _rc->get_StringFlag("CDB_GLOBALTAG") << "\t" << _rc->get_uint64Flag("TIMESTAMP") << std::endl;
0068   }
0069   _cdb = CDBInterface::instance();
0070 
0071   if (_rc->FlagExist("MBD_CALDIR"))
0072   {
0073     bbc_caldir = _rc->get_StringFlag("MBD_CALDIR");
0074     std::cout << "Reading MBD Calibrations from " << bbc_caldir << std::endl;
0075   }
0076 
0077   // if rc flag MBD_CALDIR does not exist, we create it and set it to an empty string
0078   if (!_rc->FlagExist("MBD_CALDIR"))
0079   {
0080     std::string sampmax_url = _cdb->getUrl("MBD_SAMPMAX");
0081     if (Verbosity() > 0)
0082     {
0083       std::cout << "sampmax_url " << sampmax_url << std::endl;
0084     }
0085     Download_SampMax(sampmax_url);
0086 
0087     std::string qfit_url = _cdb->getUrl("MBD_QFIT");
0088     if (Verbosity() > 0)
0089     {
0090       std::cout << "qfit_url " << qfit_url << std::endl;
0091     }
0092     Download_Gains(qfit_url);
0093 
0094     std::string tt_t0_url = _cdb->getUrl("MBD_TT_T0");
0095     if ( Verbosity() > 0 )
0096     {
0097       std::cout << "tt_t0_url " << tt_t0_url << std::endl;
0098     }
0099     Download_TTT0(tt_t0_url);
0100 
0101     std::string tq_t0_url = _cdb->getUrl("MBD_TQ_T0");
0102     if (Verbosity() > 0)
0103     {
0104       std::cout << "tq_t0_url " << tq_t0_url << std::endl;
0105     }
0106     Download_TQT0(tq_t0_url);
0107 
0108     std::string t0corr_url = _cdb->getUrl("MBD_T0CORR");
0109     if ( Verbosity() > 0 )
0110     {
0111       std::cout << "t0corr_url " << t0corr_url << std::endl;
0112     }
0113     Download_T0Corr(t0corr_url);
0114 
0115     std::string ped_url = _cdb->getUrl("MBD_PED");
0116     if (Verbosity() > 0)
0117     {
0118       std::cout << "ped_url " << ped_url << std::endl;
0119     }
0120     Download_Ped(ped_url);
0121 
0122     std::string timecorr_url = _cdb->getUrl("MBD_TIMECORR");
0123     if ( Verbosity() > 0 )
0124     {
0125       std::cout << "timecorr_url " << timecorr_url << std::endl;
0126     }
0127     Download_TimeCorr(timecorr_url);
0128 
0129     std::string slew_url = _cdb->getUrl("MBD_SLEWCORR");
0130     if ( Verbosity() > 0 )
0131     {
0132       std::cout << "slew_url " << slew_url << std::endl;
0133     }
0134     Download_SlewCorr(slew_url);
0135 
0136     std::string pileup_url = _cdb->getUrl("MBD_PILEUP");
0137     if (Verbosity() > 0)
0138     {
0139       std::cout << "pileup_url " << pileup_url << std::endl;
0140     }
0141     Download_Pileup(pileup_url);
0142 
0143     if (do_templatefit)
0144     {
0145       std::string shape_url = _cdb->getUrl("MBD_SHAPES");
0146       if (Verbosity() > 0)
0147       {
0148         std::cout << "shape_url " << shape_url << std::endl;
0149       }
0150       Download_Shapes(shape_url);
0151     }
0152     Verbosity(0);
0153   }
0154 #endif
0155 
0156   // download local calibs (text versions)
0157   if ( !bbc_caldir.empty() )
0158   {
0159     std::string sampmax_file = bbc_caldir + "/mbd_sampmax.calib";
0160     Download_SampMax(sampmax_file);
0161 
0162     std::string qfit_file = bbc_caldir + "/mbd_qfit.calib";
0163     Download_Gains(qfit_file);
0164 
0165     std::string tq_t0_file = bbc_caldir + "/mbd_tq_t0.calib";
0166     Download_TQT0(tq_t0_file);
0167 
0168     std::string tt_t0_file = bbc_caldir + "/mbd_tt_t0.calib";
0169     Download_TTT0(tt_t0_file);
0170 
0171     std::string t0corr_file = bbc_caldir + "/mbd_t0corr.calib";
0172     Download_T0Corr(t0corr_file);
0173 
0174     std::string ped_file = bbc_caldir + "/mbd_ped.calib";
0175     Download_Ped(ped_file);
0176 
0177     std::string tt_tcorr_file = bbc_caldir + "/mbd_timecorr.calib";
0178     Download_TimeCorr(tt_tcorr_file);
0179 
0180     std::string slew_file = bbc_caldir + "/mbd_slewcorr.calib";
0181     Download_SlewCorr(slew_file);
0182 
0183     std::string pileup_file = bbc_caldir + "/mbd_pileup.calib";
0184     Download_Pileup(pileup_file);
0185 
0186     if (do_templatefit)
0187     {
0188       std::string shape_file = bbc_caldir + "/mbd_shape.calib";
0189       Download_Shapes(shape_file);
0190     }
0191   }
0192 
0193   return _status;
0194 }
0195 
0196 int MbdCalib::Download_Gains(const std::string& dbase_location)
0197 {
0198   // Reset All Values
0199   _qfit_integ.fill(std::numeric_limits<float>::quiet_NaN());
0200   _qfit_mpv.fill(std::numeric_limits<float>::quiet_NaN());
0201   _qfit_sigma.fill(std::numeric_limits<float>::quiet_NaN());
0202   _qfit_integerr.fill(std::numeric_limits<float>::quiet_NaN());
0203   _qfit_mpverr.fill(std::numeric_limits<float>::quiet_NaN());
0204   _qfit_sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
0205   _qfit_chi2ndf.fill(std::numeric_limits<float>::quiet_NaN());
0206 
0207   if (Verbosity() > 0)
0208   {
0209     std::cout << "Opening " << dbase_location << std::endl;
0210   }
0211   TString dbase_file = dbase_location;
0212 
0213 #ifndef ONLINE
0214   if (dbase_file.EndsWith(".root"))  // read from database
0215   {
0216     CDBTTree* cdbttree = new CDBTTree(dbase_location);
0217     cdbttree->LoadCalibrations();
0218 
0219     for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
0220     {
0221       _qfit_integ[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_integ");
0222       _qfit_mpv[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_mpv");
0223       _qfit_sigma[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_sigma");
0224       _qfit_integerr[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_integerr");
0225       _qfit_mpverr[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_mpverr");
0226       _qfit_sigmaerr[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_sigmaerr");
0227       _qfit_chi2ndf[ipmt] = cdbttree->GetFloatValue(ipmt, "qfit_chi2ndf");
0228       if (Verbosity() > 0)
0229       {
0230         if (ipmt < 5)
0231         {
0232           std::cout << ipmt << "\t" << _qfit_mpv[ipmt] << std::endl;
0233         }
0234       }
0235     }
0236     delete cdbttree;
0237   }
0238 #endif
0239 
0240   if (dbase_file.EndsWith(".calib"))  // read from text file
0241   {
0242     std::ifstream infile(dbase_location);
0243     if (!infile.is_open())
0244     {
0245       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
0246       _status = -3;
0247       return _status;
0248     }
0249 
0250     int pmt = -1;
0251     while (infile >> pmt)
0252     {
0253       infile >> _qfit_integ[pmt] >> _qfit_mpv[pmt] >> _qfit_sigma[pmt] >> _qfit_integerr[pmt] >> _qfit_mpverr[pmt] >> _qfit_sigmaerr[pmt] >> _qfit_chi2ndf[pmt];
0254       if (Verbosity() > 0)
0255       {
0256         if (pmt < 5 || pmt >= MbdDefs::MBD_N_PMT - 5)
0257         {
0258           std::cout << pmt << "\t" << _qfit_integ[pmt] << "\t" << _qfit_mpv[pmt] << "\t" << _qfit_sigma[pmt]
0259                     << "\t" << _qfit_integerr[pmt] << "\t" << _qfit_mpverr[pmt] << "\t" << _qfit_sigmaerr[pmt]
0260                     << "\t" << _qfit_chi2ndf[pmt] << std::endl;
0261         }
0262       }
0263     }
0264   }
0265   
0266   if ( std::isnan(_qfit_mpv[0]) )
0267   {
0268     std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
0269     _status = -1;
0270     return _status;
0271   }
0272 
0273   return 1;
0274 }
0275 
0276 int MbdCalib::Download_TQT0(const std::string& dbase_location)
0277 {
0278   // Reset All Values
0279   _tqfit_t0mean.fill(std::numeric_limits<float>::quiet_NaN());
0280   _tqfit_t0meanerr.fill(std::numeric_limits<float>::quiet_NaN());
0281   _tqfit_t0sigma.fill(std::numeric_limits<float>::quiet_NaN());
0282   _tqfit_t0sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
0283 
0284   if (Verbosity() > 0)
0285   {
0286     std::cout << "Opening " << dbase_location << std::endl;
0287   }
0288   TString dbase_file = dbase_location;
0289 
0290 #ifndef ONLINE
0291   if (dbase_file.EndsWith(".root"))  // read from database
0292   {
0293     CDBTTree* cdbttree = new CDBTTree(dbase_location);
0294     cdbttree->LoadCalibrations();
0295 
0296     for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
0297     {
0298       _tqfit_t0mean[ipmt] = cdbttree->GetFloatValue(ipmt, "tqfit_t0mean");
0299       _tqfit_t0meanerr[ipmt] = cdbttree->GetFloatValue(ipmt, "tqfit_t0meanerr");
0300       _tqfit_t0sigma[ipmt] = cdbttree->GetFloatValue(ipmt, "tqfit_t0sigma");
0301       _tqfit_t0sigmaerr[ipmt] = cdbttree->GetFloatValue(ipmt, "tqfit_t0sigmaerr");
0302       if (Verbosity() > 0)
0303       {
0304         if (ipmt < 5 || ipmt >= MbdDefs::MBD_N_PMT - 5)
0305         {
0306           std::cout << ipmt << "\t" << _tqfit_t0mean[ipmt] << std::endl;
0307         }
0308       }
0309     }
0310     delete cdbttree;
0311   }
0312 #endif
0313 
0314   if (dbase_file.EndsWith(".calib"))  // read from text file
0315   {
0316     std::ifstream infile(dbase_location);
0317     if (!infile.is_open())
0318     {
0319       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
0320       _status = -3;
0321       return _status;
0322     }
0323 
0324     int pmt = -1;
0325     while (infile >> pmt)
0326     {
0327       infile >> _tqfit_t0mean[pmt] >> _tqfit_t0meanerr[pmt] >> _tqfit_t0sigma[pmt] >> _tqfit_t0sigmaerr[pmt];
0328 
0329       if (Verbosity() > 0)
0330       {
0331         if (pmt < 5 || pmt >= MbdDefs::MBD_N_PMT - 5)
0332         {
0333           std::cout << pmt << "\t" << _tqfit_t0mean[pmt] << "\t" << _tqfit_t0meanerr[pmt]
0334                     << "\t" << _tqfit_t0sigma[pmt] << "\t" << _tqfit_t0sigmaerr[pmt] << std::endl;
0335         }
0336       }
0337     }
0338     infile.close();
0339   }
0340 
0341   if ( std::isnan(_tqfit_t0mean[0]) )
0342   {
0343     std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
0344     _status = -1;
0345     return _status;
0346   }
0347 
0348   return 1;
0349 }
0350 
0351 int MbdCalib::Download_TTT0(const std::string& dbase_location)
0352 {
0353   // Reset All Values
0354   _ttfit_t0mean.fill(std::numeric_limits<float>::quiet_NaN());
0355   _ttfit_t0meanerr.fill(std::numeric_limits<float>::quiet_NaN());
0356   _ttfit_t0sigma.fill(std::numeric_limits<float>::quiet_NaN());
0357   _ttfit_t0sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
0358 
0359   if (Verbosity() > 0)
0360   {
0361     std::cout << "Opening " << dbase_location << std::endl;
0362   }
0363   TString dbase_file = dbase_location;
0364 
0365 #ifndef ONLINE
0366   if (dbase_file.EndsWith(".root"))  // read from database
0367   {
0368     CDBTTree* cdbttree = new CDBTTree(dbase_location);
0369     cdbttree->LoadCalibrations();
0370 
0371     for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
0372     {
0373       _ttfit_t0mean[ipmt] = cdbttree->GetFloatValue(ipmt, "ttfit_t0mean");
0374       _ttfit_t0meanerr[ipmt] = cdbttree->GetFloatValue(ipmt, "ttfit_t0meanerr");
0375       _ttfit_t0sigma[ipmt] = cdbttree->GetFloatValue(ipmt, "ttfit_t0sigma");
0376       _ttfit_t0sigmaerr[ipmt] = cdbttree->GetFloatValue(ipmt, "ttfit_t0sigmaerr");
0377 
0378       if (Verbosity() > 0)
0379       {
0380         if (ipmt < 5 || ipmt >= MbdDefs::MBD_N_PMT - 5)
0381         {
0382           std::cout << ipmt << "\t" << _ttfit_t0mean[ipmt] << std::endl;
0383         }
0384       }
0385     }
0386     delete cdbttree;
0387   }
0388 #endif
0389 
0390   if (dbase_file.EndsWith(".calib"))  // read from text file
0391   {
0392     std::ifstream infile(dbase_location);
0393     if (!infile.is_open())
0394     {
0395       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
0396       _status = -3;
0397       return _status;
0398     }
0399 
0400     int pmt = -1;
0401     while (infile >> pmt)
0402     {
0403       infile >> _ttfit_t0mean[pmt] >> _ttfit_t0meanerr[pmt] >> _ttfit_t0sigma[pmt] >> _ttfit_t0sigmaerr[pmt];
0404 
0405       if (Verbosity() > 0)
0406       {
0407         if (pmt < 5 || pmt >= MbdDefs::MBD_N_PMT - 5)
0408         {
0409           std::cout << pmt << "\t" << _ttfit_t0mean[pmt] << "\t" << _ttfit_t0meanerr[pmt]
0410                     << "\t" << _ttfit_t0sigma[pmt] << "\t" << _ttfit_t0sigmaerr[pmt] << std::endl;
0411         }
0412       }
0413     }
0414     infile.close();
0415   }
0416 
0417   if ( std::isnan(_ttfit_t0mean[0]) )
0418   {
0419     std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
0420     _status = -1;
0421     return _status;
0422   }
0423 
0424   return 1;
0425 }
0426 
0427 
0428 int MbdCalib::Download_T0Corr(const std::string& dbase_location)
0429 {
0430   // Reset All Values
0431   _t0corrmean = 0.;
0432   _t0corrmeanerr = 0.;
0433   _t0corr_fitmean.fill(std::numeric_limits<float>::quiet_NaN());
0434   _t0corr_fitmeanerr.fill(std::numeric_limits<float>::quiet_NaN());
0435   _t0corr_fitsigma.fill(std::numeric_limits<float>::quiet_NaN());
0436   _t0corr_fitsigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
0437   _t0corr_hmean.fill(std::numeric_limits<float>::quiet_NaN());
0438   _t0corr_hmeanerr.fill(std::numeric_limits<float>::quiet_NaN());
0439   _t0corr_hstddev.fill(std::numeric_limits<float>::quiet_NaN());
0440   _t0corr_hstddeverr.fill(std::numeric_limits<float>::quiet_NaN());
0441 
0442   if ( dbase_location.empty() )
0443   {
0444     return 0;
0445   }
0446 
0447   if (Verbosity() > 0)
0448   {
0449     std::cout << "Opening " << dbase_location << std::endl;
0450   }
0451   TString dbase_file = dbase_location;
0452 
0453 #ifndef ONLINE
0454   if (dbase_file.EndsWith(".root"))  // read from database
0455   {
0456     CDBTTree* cdbttree = new CDBTTree(dbase_location);
0457     if ( cdbttree == nullptr )
0458     {
0459       std::cerr << "T0Corr not found, skipping" << std::endl;
0460       _status = -1;
0461       return _status;
0462     }
0463     cdbttree->LoadCalibrations();
0464 
0465     _t0corrmean = cdbttree->GetSingleFloatValue("_t0corrmean");
0466     _t0corrmeanerr = cdbttree->GetSingleFloatValue("_t0corrmeanerr");
0467     _t0corr_fitmean[0] = cdbttree->GetSingleFloatValue("_t0corr_fitmean0");
0468     _t0corr_fitmeanerr[0] = cdbttree->GetSingleFloatValue("_t0corr_fitmeanerr0");
0469     _t0corr_fitsigma[0] = cdbttree->GetSingleFloatValue("_t0corr_fitsigma0");
0470     _t0corr_fitsigmaerr[0] = cdbttree->GetSingleFloatValue("_t0corr_fitsigmaerr0");
0471     _t0corr_fitmean[1] = cdbttree->GetSingleFloatValue("_t0corr_fitmean1");
0472     _t0corr_fitmeanerr[1] = cdbttree->GetSingleFloatValue("_t0corr_fitmeanerr1");
0473     _t0corr_fitsigma[1] = cdbttree->GetSingleFloatValue("_t0corr_fitsigma1");
0474     _t0corr_fitsigmaerr[1] = cdbttree->GetSingleFloatValue("_t0corr_fitsigmaerr1");
0475     _t0corr_hmean[0] = cdbttree->GetSingleFloatValue("_t0corr_hmean0");
0476     _t0corr_hmeanerr[0] = cdbttree->GetSingleFloatValue("_t0corr_hmeanerr0");
0477     _t0corr_hstddev[0] = cdbttree->GetSingleFloatValue("_t0corr_hstddev0");
0478     _t0corr_hstddeverr[0] = cdbttree->GetSingleFloatValue("_t0corr_hstddeverr0");
0479     _t0corr_hmean[1] = cdbttree->GetSingleFloatValue("_t0corr_hmean1");
0480     _t0corr_hmeanerr[1] = cdbttree->GetSingleFloatValue("_t0corr_hmeanerr1");
0481     _t0corr_hstddev[1] = cdbttree->GetSingleFloatValue("_t0corr_hstddev1");
0482     _t0corr_hstddeverr[1] = cdbttree->GetSingleFloatValue("_t0corr_hstddeverr1");
0483 
0484     if (Verbosity() > 0)
0485     {
0486       std::cout << "T0Corr\t" << _t0corrmean << std::endl;
0487     }
0488     delete cdbttree;
0489   }
0490 #endif
0491 
0492   if (dbase_file.EndsWith(".calib"))  // read from text file
0493   {
0494     std::ifstream infile(dbase_location);
0495     if (!infile.is_open())
0496     {
0497       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
0498       _status = -3;
0499       return _status;
0500     }
0501 
0502     infile >> _t0corrmean >> _t0corrmeanerr
0503       >> _t0corr_fitmean[0] >> _t0corr_fitmeanerr[0] >> _t0corr_fitsigma[0] >> _t0corr_fitsigmaerr[0]
0504       >> _t0corr_fitmean[1] >> _t0corr_fitmeanerr[1] >> _t0corr_fitsigma[1] >> _t0corr_fitsigmaerr[1]
0505       >> _t0corr_hmean[0] >> _t0corr_hmeanerr[0] >> _t0corr_hstddev[0] >> _t0corr_hstddeverr[0]
0506       >> _t0corr_hmean[1] >> _t0corr_hmeanerr[1] >> _t0corr_hstddev[1] >> _t0corr_hstddeverr[1];
0507 
0508     if (Verbosity() > 0)
0509     {
0510       std::cout << "T0Corr\t" << _t0corrmean << "\t" << _t0corrmeanerr << std::endl;
0511     }
0512     infile.close();
0513   }
0514 
0515   if ( std::isnan(_t0corrmean) )
0516   {
0517     std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
0518     _status = -1;
0519     return _status;
0520   }
0521 
0522   return 1;
0523 }
0524 
0525 int MbdCalib::Download_Ped(const std::string& dbase_location)
0526 {
0527   // Reset All Values
0528   _pedmean.fill(std::numeric_limits<float>::quiet_NaN());
0529   _pedmeanerr.fill(std::numeric_limits<float>::quiet_NaN());
0530   _pedsigma.fill(std::numeric_limits<float>::quiet_NaN());
0531   _pedsigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
0532 
0533   if (Verbosity() > 0)
0534   {
0535     std::cout << "Opening " << dbase_location << std::endl;
0536   }
0537   TString dbase_file = dbase_location;
0538 
0539 #ifndef ONLINE
0540   if (dbase_file.EndsWith(".root"))  // read from database
0541   {
0542     CDBTTree* cdbttree = new CDBTTree(dbase_location);
0543     cdbttree->LoadCalibrations();
0544 
0545     for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
0546     {
0547       _pedmean[ifeech] = cdbttree->GetFloatValue(ifeech, "pedmean");
0548       _pedmeanerr[ifeech] = cdbttree->GetFloatValue(ifeech, "pedmeanerr");
0549       _pedsigma[ifeech] = cdbttree->GetFloatValue(ifeech, "pedsigma");
0550       _pedsigmaerr[ifeech] = cdbttree->GetFloatValue(ifeech, "pedsigmaerr");
0551 
0552       if (Verbosity() > 0)
0553       {
0554         if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
0555         {
0556           std::cout << ifeech << "\t" << _pedmean[ifeech] << std::endl;
0557         }
0558       }
0559     }
0560     delete cdbttree;
0561   }
0562 #endif
0563 
0564   if (dbase_file.EndsWith(".calib"))  // read from text file
0565   {
0566     std::ifstream infile(dbase_location);
0567     if (!infile.is_open())
0568     {
0569       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
0570       _status = -3;
0571       return _status;
0572     }
0573 
0574     int feech = -1;
0575     while (infile >> feech)
0576     {
0577       infile >> _pedmean[feech] >> _pedmeanerr[feech] >> _pedsigma[feech] >> _pedsigmaerr[feech];
0578 
0579       if (Verbosity() > 0)
0580       {
0581         if (feech < 5 || feech >= MbdDefs::MBD_N_FEECH - 5)
0582         {
0583           std::cout << feech << "\t" << _pedmean[feech] << "\t" << _pedmeanerr[feech]
0584                     << "\t" << _pedsigma[feech] << "\t" << _pedsigmaerr[feech] << std::endl;
0585         }
0586       }
0587     }
0588     infile.close();
0589   }
0590 
0591   if ( std::isnan(_pedmean[0]) )
0592   {
0593     std::cout << PHWHERE << ", WARNING, ped calib missing, " << dbase_location << std::endl;
0594     _status = -1;
0595     return _status;
0596   }
0597 
0598   return 1;
0599 }
0600 
0601 int MbdCalib::Download_SampMax(const std::string& dbase_location)
0602 {
0603   // Reset All Values
0604   _sampmax.fill(-1);
0605 
0606   TString dbase_file = dbase_location;
0607 
0608 #ifndef ONLINE
0609   if (dbase_file.EndsWith(".root"))  // read from database
0610   {
0611     CDBTTree* cdbttree = new CDBTTree(dbase_location);
0612     cdbttree->LoadCalibrations();
0613 
0614     for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
0615     {
0616       _sampmax[ifeech] = cdbttree->GetIntValue(ifeech, "sampmax");
0617       if (Verbosity() > 0)
0618       {
0619         if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
0620         {
0621           std::cout << ifeech << "\t" << _sampmax[ifeech] << std::endl;
0622         }
0623       }
0624     }
0625     delete cdbttree;
0626   }
0627 #endif
0628 
0629   if (dbase_file.EndsWith(".calib"))  // read from text file
0630   {
0631     std::ifstream infile(dbase_location);
0632     if (!infile.is_open())
0633     {
0634       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
0635       _status = -3;
0636       return _status;
0637     }
0638 
0639     int feech = -1;
0640     while (infile >> feech)
0641     {
0642       infile >> _sampmax[feech];
0643       if (Verbosity() > 0)
0644       {
0645         if (feech < 5 || feech >= MbdDefs::MBD_N_FEECH - 5)
0646         {
0647           std::cout << "sampmax\t" << feech << "\t" << _sampmax[feech] << std::endl;
0648         }
0649       }
0650     }
0651     infile.close();
0652   }
0653   
0654 
0655   if ( _sampmax[0] == -1 )
0656   {
0657     std::cout << PHWHERE << ", WARNING, sampmax calib missing, " << dbase_location << std::endl;
0658     _status = -1;
0659     return _status;  // file not found
0660   }
0661 
0662   return 1;
0663 }
0664 
0665 int MbdCalib::Download_Shapes(const std::string& dbase_location)
0666 {
0667   // Verbosity(100);
0668   if (Verbosity())
0669   {
0670     std::cout << "In MbdCalib::Download_Shapes" << std::endl;
0671   }
0672   // Reset All Values
0673   for (auto& shape : _shape_y)
0674   {
0675     shape.clear();
0676   }
0677   for (auto& sherr : _sherr_yerr)
0678   {
0679     sherr.clear();
0680   }
0681 
0682   TString dbase_file = dbase_location;
0683 
0684 #ifndef ONLINE
0685   if (dbase_file.EndsWith(".root"))  // read from database
0686   {
0687     if (Verbosity())
0688     {
0689       std::cout << "Reading from CDB " << dbase_location << std::endl;
0690     }
0691     CDBTTree* cdbttree = new CDBTTree(dbase_location);
0692     cdbttree->LoadCalibrations();
0693 
0694     for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
0695     {
0696       if (_mbdgeom->get_type(ifeech) == 0)
0697       {
0698         continue;  // skip t-channels
0699       }
0700 
0701       _shape_npts[ifeech] = cdbttree->GetIntValue(ifeech, "shape_npts");
0702       _shape_minrange[ifeech] = cdbttree->GetFloatValue(ifeech, "shape_min");
0703       _shape_maxrange[ifeech] = cdbttree->GetFloatValue(ifeech, "shape_max");
0704 
0705       _sherr_npts[ifeech] = cdbttree->GetIntValue(ifeech, "sherr_npts");
0706       _sherr_minrange[ifeech] = cdbttree->GetFloatValue(ifeech, "sherr_min");
0707       _sherr_maxrange[ifeech] = cdbttree->GetFloatValue(ifeech, "sherr_max");
0708 
0709       for (int ipt = 0; ipt < _shape_npts[ifeech]; ipt++)
0710       {
0711         int chtemp = (1000 * ipt) + ifeech;
0712 
0713         float val = cdbttree->GetFloatValue(chtemp, "shape_val");
0714         _shape_y[ifeech].push_back(val);
0715 
0716         val = cdbttree->GetFloatValue(chtemp, "sherr_val");
0717         _sherr_yerr[ifeech].push_back(val);
0718       }
0719 
0720       if (Verbosity() > 0)
0721       {
0722         if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
0723         {
0724           std::cout << ifeech << "\t" << _shape_y[ifeech][0] << std::endl;
0725         }
0726       }
0727     }
0728     delete cdbttree;
0729   }
0730 #endif
0731 
0732   if (dbase_file.EndsWith(".calib"))  // read from text file
0733   {
0734     if (Verbosity())
0735     {
0736       std::cout << "Reading from " << dbase_location << std::endl;
0737     }
0738     std::ifstream infile(dbase_location);
0739     if (!infile.is_open())
0740     {
0741       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
0742       _status = -3;
0743       return _status;
0744     }
0745 
0746     int temp_feech = -1;
0747     int temp_npoints = -1;
0748     float temp_begintime = -1;
0749     float temp_endtime = -1;
0750     while (infile >> temp_feech >> temp_npoints >> temp_begintime >> temp_endtime)
0751     {
0752       if (Verbosity())
0753       {
0754         std::cout << "shape " << temp_feech << "\t" << temp_npoints << "\t" << temp_begintime << "\t" << temp_endtime << std::endl;
0755       }
0756       if (temp_feech < 0 || temp_feech > 255)
0757       {
0758         std::cout << "ERROR, invalid FEECH " << temp_feech << " in MBD waveforms calibration" << std::endl;
0759         _status = -2;
0760         return _status;
0761       }
0762 
0763       _shape_npts[temp_feech] = temp_npoints;
0764       _shape_minrange[temp_feech] = temp_begintime;
0765       _shape_maxrange[temp_feech] = temp_endtime;
0766 
0767       float temp_val{0.};
0768       for (int isamp = 0; isamp < temp_npoints; isamp++)
0769       {
0770         infile >> temp_val;
0771         _shape_y[temp_feech].push_back(temp_val);
0772         if (Verbosity() && (temp_feech == 8 || temp_feech == 255))
0773         {
0774           std::cout << _shape_y[temp_feech][isamp] << " ";
0775           if (isamp % 10 == 9)
0776           {
0777             std::cout << std::endl;
0778           }
0779         }
0780       }
0781       if (Verbosity())
0782       {
0783         std::cout << std::endl;
0784       }
0785     }
0786 
0787     infile.close();
0788 
0789     // Now read in the sherr file
0790     std::string sherr_dbase_location = std::regex_replace(dbase_location, std::regex("bbc_shape.calib"), "bbc_sherr.calib");
0791     if (Verbosity())
0792     {
0793       std::cout << "Reading from " << sherr_dbase_location << std::endl;
0794     }
0795     infile.open(sherr_dbase_location);
0796     if (!infile.is_open())
0797     {
0798       std::cout << PHWHERE << "unable to open " << sherr_dbase_location << std::endl;
0799       _status = -3;
0800       return _status;
0801     }
0802 
0803     temp_feech = -1;
0804     temp_npoints = -1;
0805     temp_begintime = -1;
0806     temp_endtime = -1;
0807     while (infile >> temp_feech >> temp_npoints >> temp_begintime >> temp_endtime)
0808     {
0809       if (Verbosity())
0810       {
0811         std::cout << "sheer " << temp_feech << "\t" << temp_npoints << "\t" << temp_begintime << "\t" << temp_endtime << std::endl;
0812       }
0813       if (temp_feech < 0 || temp_feech > 255)
0814       {
0815         std::cout << "ERROR, invalid FEECH " << temp_feech << " in MBD waveforms calibration" << std::endl;
0816         _status = -2;
0817         return _status;
0818       }
0819 
0820       _sherr_npts[temp_feech] = temp_npoints;
0821       _sherr_minrange[temp_feech] = temp_begintime;
0822       _sherr_maxrange[temp_feech] = temp_endtime;
0823 
0824       float temp_val{0.};
0825       for (int isamp = 0; isamp < temp_npoints; isamp++)
0826       {
0827         infile >> temp_val;
0828         _sherr_yerr[temp_feech].push_back(temp_val);
0829         if (Verbosity() && (temp_feech == 8 || temp_feech == 255))
0830         {
0831           std::cout << _sherr_yerr[temp_feech][isamp] << " ";
0832           if (isamp % 10 == 9)
0833           {
0834             std::cout << std::endl;
0835           }
0836         }
0837       }
0838       if (Verbosity())
0839       {
0840         std::cout << std::endl;
0841       }
0842     }
0843 
0844     infile.close();
0845   }
0846 
0847   if ( _shape_y[8].empty() )
0848   {
0849     std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
0850     _status = -1;
0851     return _status;  // file not found
0852   }
0853 
0854   // Verbosity(0);
0855   return 1;
0856 }
0857 
0858 
0859 int MbdCalib::Download_TimeCorr(const std::string& dbase_location)
0860 {
0861   //Verbosity(100);
0862   if ( Verbosity() )
0863   {
0864     std::cout << "In MbdCalib::Download_TimeCorr" << std::endl;
0865   }
0866   // Reset All Values
0867   for(auto& tcorr : _tcorr_y) {
0868     tcorr.clear();
0869   }
0870 
0871   TString dbase_file = dbase_location;
0872 
0873 #ifndef ONLINE
0874   if (dbase_file.EndsWith(".root"))  // read from CDB database file
0875   {
0876     if ( Verbosity() )
0877     {
0878       std::cout << "Reading from CDB " << dbase_location << std::endl;
0879     }
0880     CDBTTree* cdbttree = new CDBTTree(dbase_location);
0881     cdbttree->LoadCalibrations();
0882 
0883     for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
0884     {
0885       if ( _mbdgeom->get_type(ifeech) == 1 )
0886       {
0887         continue;  // skip q-channels
0888       }
0889 
0890       _tcorr_npts[ifeech] = cdbttree->GetIntValue(ifeech, "tcorr_npts");
0891       _tcorr_minrange[ifeech] = cdbttree->GetFloatValue(ifeech, "tcorr_min");
0892       _tcorr_maxrange[ifeech] = cdbttree->GetFloatValue(ifeech, "tcorr_max");
0893 
0894       for (int ipt=0; ipt<_tcorr_npts[ifeech]; ipt++)
0895       {
0896         int chtemp = (1000*ipt) + ifeech; // in cdbtree, entry has id = 1000*datapoint + ifeech
0897 
0898         float val = cdbttree->GetFloatValue(chtemp, "tcorr_val");
0899         _tcorr_y[ifeech].push_back( val );
0900       }
0901 
0902       if (Verbosity() > 0)
0903       {
0904         if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
0905         {
0906           std::cout << ifeech << "\t" << _tcorr_y[ifeech][0] << std::endl;
0907         }
0908       }
0909     }
0910     delete cdbttree;
0911   }
0912 #endif
0913 
0914   if (dbase_file.EndsWith(".calib"))  // read from text file
0915   {
0916     if ( Verbosity() )
0917     {
0918       std::cout << "Reading from " << dbase_location << std::endl;
0919     }
0920     std::ifstream infile(dbase_location);
0921     if (!infile.is_open())
0922     {
0923       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
0924       _status = -3;
0925       return _status;
0926     }
0927 
0928     int temp_feech = -1;
0929     int temp_npoints = -1;
0930     float temp_begintime = -1;
0931     float temp_endtime = -1;
0932     while ( infile >> temp_feech >> temp_npoints >> temp_begintime >> temp_endtime )
0933     {
0934       if ( Verbosity() )
0935       {
0936         std::cout << "tcorr " << temp_feech << "\t" <<  temp_npoints << "\t" <<  temp_begintime << "\t" <<  temp_endtime << std::endl;
0937       }
0938       if ( temp_feech<0 || temp_feech>255 )
0939       {
0940         std::cout << "ERROR, invalid FEECH " << temp_feech << " in MBD timecorr calibration" << std::endl;
0941         _status = -2;
0942         return _status;
0943       }
0944 
0945       _tcorr_npts[temp_feech] = temp_npoints;
0946       _tcorr_minrange[temp_feech] = temp_begintime;
0947       _tcorr_maxrange[temp_feech] = temp_endtime;
0948 
0949       float temp_val{0.};
0950       for (int isamp=0; isamp<temp_npoints; isamp++)
0951       {
0952         infile >> temp_val;
0953         _tcorr_y[temp_feech].push_back( temp_val );
0954         if ( Verbosity() && (temp_feech==0 || temp_feech==64) )
0955         {
0956           std::cout << _tcorr_y[temp_feech][isamp] << " ";
0957           if ( isamp%10==9 )
0958           {
0959             std::cout << std::endl;
0960           }
0961         }
0962       }
0963       if ( Verbosity() )
0964       {
0965         std::cout << std::endl;
0966       }
0967     }
0968 
0969     infile.close();
0970   }
0971 
0972   if ( _tcorr_y[0].empty() )
0973   {
0974     std::cout << PHWHERE << ", ERROR, MBD tcorr not loaded, " << dbase_location << std::endl;
0975     _status = -1;
0976     return _status;  // file not loaded
0977   }
0978 
0979   // Now we interpolate the timecorr
0980   for (size_t ifeech=0; ifeech<MbdDefs::MBD_N_FEECH; ifeech++) 
0981   {
0982     if ( _mbdgeom->get_type(ifeech) == 1 )
0983     {
0984       continue;  // skip q-channels
0985     }
0986 
0987     int step = static_cast<int>( (_tcorr_maxrange[ifeech] - _tcorr_minrange[ifeech]) / (_tcorr_npts[ifeech]-1) );
0988     //std::cout << ifeech << " step = " << step << std::endl;
0989 
0990     for (int itdc=0; itdc<=_tcorr_maxrange[ifeech]; itdc++)
0991     {
0992       int calib_index = itdc/step;
0993       int interp = itdc%step;
0994 
0995       // simple linear interpolation for now
0996       double slope = (_tcorr_y[ifeech][calib_index+1] - _tcorr_y[ifeech][calib_index])/step;
0997       float tcorr_interp = _tcorr_y[ifeech][calib_index] + (interp*slope);
0998  
0999       _tcorr_y_interp[ifeech].push_back( tcorr_interp );
1000 
1001       if ( ifeech==0 && itdc<2*step && Verbosity() )
1002       {
1003         std::cout << _tcorr_y_interp[ifeech][itdc] << " ";
1004         if ( itdc%step==(step-1) )
1005         {
1006           std::cout << std::endl;
1007         }
1008       }
1009     }
1010 
1011   }
1012 
1013   //Verbosity(0);
1014   return 1;
1015 }
1016 
1017 int MbdCalib::Download_SlewCorr(const std::string& dbase_location)
1018 {
1019   //Verbosity(100);
1020   if ( Verbosity() )
1021   {
1022     std::cout << "In MbdCalib::Download_SlewCorr" << std::endl;
1023   }
1024   // Reset All Values
1025   for(auto& scorr : _scorr_y) {
1026     scorr.clear();
1027   }
1028   std::fill(_scorr_npts.begin(), _scorr_npts.end(), 0);
1029   
1030   TString dbase_file = dbase_location;
1031 
1032 #ifndef ONLINE
1033   if (dbase_file.EndsWith(".root"))  // read from CDB database file
1034   {
1035     if ( Verbosity() )
1036     {
1037       std::cout << "Reading from CDB " << dbase_location << std::endl;
1038     }
1039     CDBTTree* cdbttree = new CDBTTree(dbase_location);
1040     cdbttree->LoadCalibrations();
1041 
1042     for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
1043     {
1044       if ( _mbdgeom->get_type(ifeech) == 1 )
1045       {
1046         continue;  // skip q-channels
1047       }
1048 
1049       _scorr_npts[ifeech] = cdbttree->GetIntValue(ifeech, "scorr_npts");
1050       _scorr_minrange[ifeech] = cdbttree->GetFloatValue(ifeech, "scorr_min");
1051       _scorr_maxrange[ifeech] = cdbttree->GetFloatValue(ifeech, "scorr_max");
1052 
1053       for (int ipt=0; ipt<_scorr_npts[ifeech]; ipt++)
1054       {
1055         int chtemp = (1000*ipt) + ifeech; // in cdbtree, entry has id = 1000*datapoint + ifeech
1056 
1057         float val = cdbttree->GetFloatValue(chtemp, "scorr_val");
1058         _scorr_y[ifeech].push_back( val );
1059       }
1060 
1061       if (Verbosity() > 0)
1062       {
1063         if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
1064         {
1065           std::cout << ifeech << "\t" << _scorr_y[ifeech][0] << std::endl;
1066         }
1067       }
1068     }
1069     delete cdbttree;
1070   }
1071 #endif
1072 
1073   if (dbase_file.EndsWith(".calib"))  // read from text file
1074   {
1075     if ( Verbosity() )
1076     {
1077       std::cout << "Reading from " << dbase_location << std::endl;
1078     }
1079 
1080     std::ifstream infile(dbase_location);
1081     if (!infile.is_open())
1082     {
1083       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
1084       _status = -3;
1085       return _status;
1086     }
1087 
1088     int temp_feech = -1;
1089     int temp_npoints = 0;
1090     float temp_beginadc = -1;
1091     float temp_endadc = -1;
1092     while ( infile >> temp_feech >> temp_npoints >> temp_beginadc >> temp_endadc )
1093     {
1094       if ( Verbosity() )
1095       {
1096         std::cout << "scorr " << temp_feech << "\t" <<  temp_npoints << "\t" <<  temp_beginadc << "\t" <<  temp_endadc << std::endl;
1097       }
1098 
1099       if ( temp_feech<0 || temp_feech>255 )
1100       {
1101         std::cout << "ERROR, invalid FEECH " << temp_feech << " in MBD slewcorr calibration" << std::endl;
1102         _status = -2;
1103         return _status;
1104       }
1105 
1106       _scorr_npts[temp_feech] = temp_npoints;
1107       _scorr_minrange[temp_feech] = temp_beginadc;
1108       _scorr_maxrange[temp_feech] = temp_endadc;
1109 
1110       float temp_val{0.};
1111       for (int isamp=0; isamp<temp_npoints; isamp++)
1112       {
1113         infile >> temp_val;
1114         _scorr_y[temp_feech].push_back( temp_val );
1115         if ( Verbosity() && (temp_feech==0 || temp_feech==64) )
1116         {
1117           std::cout << _scorr_y[temp_feech][isamp] << " ";
1118           if ( isamp%10==9 )
1119           {
1120             std::cout << std::endl;
1121           }
1122         }
1123       }
1124       if ( Verbosity() )
1125       {
1126         std::cout << std::endl;
1127       }
1128     }
1129 
1130     infile.close();
1131   }
1132 
1133   if ( _scorr_y[0].empty() )
1134   {
1135     std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
1136     _status = -1;
1137     return _status;  // file not found
1138   }
1139 
1140   // Now we interpolate the slewcorr
1141   for (size_t ifeech=0; ifeech<MbdDefs::MBD_N_FEECH; ifeech++) 
1142   {
1143     if ( _mbdgeom->get_type(ifeech) == 1 )
1144     {
1145       continue;  // skip q-channels
1146     }
1147     // skip bad t-channels
1148     if ( _scorr_npts[ifeech] == 0 )
1149     {
1150       //std::cout << "skipping " << ifeech << std::endl;
1151       continue;
1152     }
1153 
1154     int step = static_cast<int>( (_scorr_maxrange[ifeech] - _scorr_minrange[ifeech]) / (_scorr_npts[ifeech]-1) );
1155     //std::cout << ifeech << " step = " << step << std::endl;
1156 
1157     for (int iadc=0; iadc<=_scorr_maxrange[ifeech]; iadc++)
1158     {
1159       int calib_index = iadc/step;
1160       int interp = iadc%step;
1161 
1162       // simple linear interpolation for now
1163       double slope = (_scorr_y[ifeech][calib_index+1] - _scorr_y[ifeech][calib_index])/step;
1164       float scorr_interp = _scorr_y[ifeech][calib_index] + (interp*slope);
1165  
1166       _scorr_y_interp[ifeech].push_back( scorr_interp );
1167 
1168 
1169       if ( ifeech==4 && iadc<12 && Verbosity() )
1170       {
1171         if ( iadc==0 )
1172         {
1173           std::cout << "slewcorr " << ifeech << "\t" << _scorr_npts[ifeech] << "\t"
1174             << _scorr_minrange[ifeech] << "\t" << _scorr_maxrange[ifeech] << std::endl;
1175         }
1176         std::cout << _scorr_y_interp[ifeech][iadc] << " ";
1177         if ( iadc%step==(step-1) )
1178         {
1179           std::cout << std::endl;
1180         }
1181       }
1182     }
1183 
1184   }
1185 
1186   //Verbosity(0);
1187   return 1;
1188 }
1189 
1190 int MbdCalib::Download_Pileup(const std::string& dbase_location)
1191 {
1192   // Reset All Values
1193   Reset_Pileup();
1194 
1195   if (Verbosity() > 0)
1196   {
1197     std::cout << "Opening " << dbase_location << std::endl;
1198   }
1199   TString dbase_file = dbase_location;
1200 
1201 #ifndef ONLINE
1202   if (dbase_file.EndsWith(".root"))  // read from database
1203   {
1204     CDBTTree* cdbttree = new CDBTTree(dbase_location);
1205     if ( cdbttree == nullptr )
1206     {
1207       std::cerr << "MBD pileup calib not found, skipping" << std::endl;
1208       _status = -1;
1209       return _status;
1210     }
1211     cdbttree->LoadCalibrations();
1212 
1213     for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
1214     {
1215       _pileup_p0[ifeech] = cdbttree->GetFloatValue(ifeech, "pileup_p0");
1216       _pileup_p0err[ifeech] = cdbttree->GetFloatValue(ifeech, "pileup_p0err");
1217       _pileup_p1[ifeech] = cdbttree->GetFloatValue(ifeech, "pileup_p1");
1218       _pileup_p1err[ifeech] = cdbttree->GetFloatValue(ifeech, "pileup_p1err");
1219       _pileup_p2[ifeech] = cdbttree->GetFloatValue(ifeech, "pileup_p2");
1220       _pileup_p2err[ifeech] = cdbttree->GetFloatValue(ifeech, "pileup_p2err");
1221       _pileup_chi2ndf[ifeech] = cdbttree->GetFloatValue(ifeech, "pileup_chi2ndf");
1222       if (Verbosity() > 0)
1223       {
1224         if (ifeech < 2 || ifeech >= (MbdDefs::MBD_N_FEECH-2) )
1225         {
1226           std::cout << ifeech << "\t" << _pileup_p0[ifeech] << std::endl;
1227         }
1228       }
1229     }
1230     delete cdbttree;
1231   }
1232 #endif
1233 
1234   if (dbase_file.EndsWith(".calib"))  // read from text file
1235   {
1236     std::ifstream infile(dbase_location);
1237     if (!infile.is_open())
1238     {
1239       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
1240       _status = -3;
1241       return _status;
1242     }
1243 
1244     int feech = -1;
1245     while (infile >> feech)
1246     {
1247       infile >> _pileup_p0[feech] >> _pileup_p0err[feech]
1248              >> _pileup_p1[feech] >> _pileup_p1err[feech]
1249              >> _pileup_p2[feech] >> _pileup_p2err[feech]
1250              >> _pileup_chi2ndf[feech];
1251 
1252       if (Verbosity() > 0)
1253       {
1254         if (feech < 2 || feech >= MbdDefs::MBD_N_PMT - 2)
1255         {
1256           std::cout << feech << "\t" << _pileup_p0[feech] << "\t" << _pileup_p0err[feech]
1257                              << "\t" << _pileup_p1[feech] << "\t" << _pileup_p1err[feech]
1258                              << "\t" << _pileup_p2[feech] << "\t" << _pileup_p2err[feech]
1259                              << "\t" << _pileup_chi2ndf[feech] << std::endl;
1260         }
1261       }
1262     }
1263   }
1264   
1265   if ( std::isnan(_pileup_p0[0]) )
1266   {
1267     std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
1268     _status = -1;
1269     return _status;
1270   }
1271 
1272   return 1;
1273 }
1274 
1275 int MbdCalib::Download_Thresholds(const std::string& dbase_location)
1276 {
1277   // Reset All Values
1278   Reset_Thresholds();
1279 
1280   if (Verbosity() > 0)
1281   {
1282     std::cout << "Opening " << dbase_location << std::endl;
1283   }
1284   TString dbase_file = dbase_location;
1285 
1286 #ifndef ONLINE
1287   if (dbase_file.EndsWith(".root"))  // read from database
1288   {
1289     CDBTTree* cdbttree = new CDBTTree(dbase_location);
1290     cdbttree->LoadCalibrations();
1291 
1292     for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
1293     {
1294       _thresh_mean[ipmt] = cdbttree->GetFloatValue(ipmt, "thresh_mean");
1295       _thresh_meanerr[ipmt] = cdbttree->GetFloatValue(ipmt, "thresh_meanerr");
1296       _thresh_width[ipmt] = cdbttree->GetFloatValue(ipmt, "thresh_width");
1297       _thresh_widtherr[ipmt] = cdbttree->GetFloatValue(ipmt, "thresh_widtherr");
1298       _thresh_eff[ipmt] = cdbttree->GetFloatValue(ipmt, "thresh_eff");
1299       _thresh_efferr[ipmt] = cdbttree->GetFloatValue(ipmt, "thresh_efferr");
1300       _thresh_chi2ndf[ipmt] = cdbttree->GetFloatValue(ipmt, "thresh_chi2ndf");
1301       if (Verbosity() > 0)
1302       {
1303         if (ipmt < 5)
1304         {
1305           std::cout << ipmt << "\t" << _thresh_mean[ipmt] << std::endl;
1306         }
1307       }
1308     }
1309     delete cdbttree;
1310   }
1311 #endif
1312 
1313   if (dbase_file.EndsWith(".calib"))  // read from text file
1314   {
1315     std::ifstream infile(dbase_location);
1316     if (!infile.is_open())
1317     {
1318       std::cout << PHWHERE << "unable to open " << dbase_location << std::endl;
1319       _status = -3;
1320       return _status;
1321     }
1322 
1323     int pmt = -1;
1324     while (infile >> pmt)
1325     {
1326       infile >> _thresh_mean[pmt] >> _thresh_meanerr[pmt] >> _thresh_width[pmt] >> _thresh_widtherr[pmt] >> _thresh_eff[pmt] >> _thresh_efferr[pmt] >> _thresh_chi2ndf[pmt];
1327       if (Verbosity() > 0)
1328       {
1329         if (pmt < 5 || pmt >= MbdDefs::MBD_N_PMT - 5)
1330         {
1331           std::cout << pmt << "\t" << _thresh_mean[pmt] << "\t" << _thresh_meanerr[pmt] << "\t" << _thresh_width[pmt]
1332                     << "\t" << _thresh_widtherr[pmt] << "\t" << _thresh_eff[pmt] << "\t" << _thresh_efferr[pmt]
1333                     << "\t" << _thresh_chi2ndf[pmt] << std::endl;
1334         }
1335       }
1336     }
1337   }
1338   
1339   if ( std::isnan(_thresh_mean[0]) )
1340   {
1341     std::cout << PHWHERE << ", ERROR, unknown file type, " << dbase_location << std::endl;
1342     _status = -1;
1343     return _status;
1344   }
1345 
1346   return 1;
1347 }
1348 
1349 #ifndef ONLINE
1350 int MbdCalib::Write_CDB_SampMax(const std::string& dbfile)
1351 {
1352   CDBTTree* cdbttree{ nullptr };
1353 
1354   std::cout << "Creating " << dbfile << std::endl;
1355   cdbttree = new CDBTTree( dbfile );
1356   cdbttree->SetSingleIntValue("version", 1);
1357   cdbttree->CommitSingle();
1358 
1359   std::cout << "SAMPMAX" << std::endl;
1360   for (size_t ifeech = 0; ifeech < _sampmax.size(); ifeech++)
1361   {
1362     // store in a CDBTree
1363     cdbttree->SetIntValue(ifeech, "sampmax", _sampmax[ifeech]);
1364 
1365     if (ifeech < 12 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
1366     {
1367       std::cout << ifeech << "\t" << cdbttree->GetIntValue(ifeech, "sampmax") << std::endl;
1368     }
1369   }
1370 
1371   cdbttree->Commit();
1372   // cdbttree->Print();
1373 
1374   // for now we create the tree after reading it
1375   cdbttree->WriteCDBTTree();
1376   delete cdbttree;
1377 
1378   return 1;
1379 }
1380 #endif
1381 
1382 int MbdCalib::Write_SampMax(const std::string& dbfile)
1383 {
1384   std::ofstream cal_file;
1385   cal_file.open(dbfile);
1386   for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
1387   {
1388     cal_file << ifeech << "\t" << _sampmax[ifeech] << std::endl;
1389   }
1390   cal_file.close();
1391 
1392   return 1;
1393 }
1394 
1395 #ifndef ONLINE
1396 int MbdCalib::Write_CDB_TTT0(const std::string& dbfile)
1397 {
1398   CDBTTree* cdbttree{ nullptr };
1399 
1400   std::cout << "Creating " << dbfile << std::endl;
1401   cdbttree = new CDBTTree( dbfile );
1402   cdbttree->SetSingleIntValue("version", 1);
1403   cdbttree->CommitSingle();
1404 
1405   std::cout << "TTT0" << std::endl;
1406   for (size_t ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
1407   {
1408     // store in a CDBTree
1409     cdbttree->SetFloatValue(ipmt, "ttfit_t0mean", _ttfit_t0mean[ipmt]);
1410     cdbttree->SetFloatValue(ipmt, "ttfit_t0meanerr", _ttfit_t0meanerr[ipmt]);
1411     cdbttree->SetFloatValue(ipmt, "ttfit_t0sigma", _ttfit_t0sigma[ipmt]);
1412     cdbttree->SetFloatValue(ipmt, "ttfit_t0sigmaerr", _ttfit_t0sigmaerr[ipmt]);
1413 
1414     if (ipmt < 5 || ipmt >= MbdDefs::MBD_N_PMT - 5)
1415     {
1416       std::cout << ipmt << "\t" << cdbttree->GetFloatValue(ipmt, "ttfit_t0mean") << std::endl;
1417     }
1418   }
1419 
1420   cdbttree->Commit();
1421   // cdbttree->Print();
1422 
1423   // for now we create the tree after reading it
1424   cdbttree->WriteCDBTTree();
1425   delete cdbttree;
1426 
1427   return 1;
1428 }
1429 #endif
1430 
1431 int MbdCalib::Write_TTT0(const std::string& dbfile)
1432 {
1433   std::ofstream cal_t0_file;
1434   cal_t0_file.open(dbfile);
1435   for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
1436   {
1437     cal_t0_file << ipmt << "\t" << _ttfit_t0mean[ipmt] << "\t" << _ttfit_t0meanerr[ipmt]
1438       << "\t" << _ttfit_t0sigma[ipmt] << "\t" << _ttfit_t0sigmaerr[ipmt] << std::endl;
1439   }
1440   cal_t0_file.close();
1441 
1442   return 1;
1443 }
1444 
1445 #ifndef ONLINE
1446 int MbdCalib::Write_CDB_TQT0(const std::string& dbfile)
1447 {
1448   CDBTTree* cdbttree{ nullptr };
1449 
1450   std::cout << "Creating " << dbfile << std::endl;
1451   cdbttree = new CDBTTree( dbfile );
1452   cdbttree->SetSingleIntValue("version", 1);
1453   cdbttree->CommitSingle();
1454 
1455   std::cout << "TQT0" << std::endl;
1456   for (size_t ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
1457   {
1458     // store in a CDBTree
1459     cdbttree->SetFloatValue(ipmt, "tqfit_t0mean", _tqfit_t0mean[ipmt]);
1460     cdbttree->SetFloatValue(ipmt, "tqfit_t0meanerr", _tqfit_t0meanerr[ipmt]);
1461     cdbttree->SetFloatValue(ipmt, "tqfit_t0sigma", _tqfit_t0sigma[ipmt]);
1462     cdbttree->SetFloatValue(ipmt, "tqfit_t0sigmaerr", _tqfit_t0sigmaerr[ipmt]);
1463 
1464     if (ipmt < 5 || ipmt >= MbdDefs::MBD_N_PMT - 5)
1465     {
1466       std::cout << ipmt << "\t" << cdbttree->GetFloatValue(ipmt, "tqfit_t0mean") << std::endl;
1467     }
1468   }
1469 
1470   cdbttree->Commit();
1471   // cdbttree->Print();
1472 
1473   // for now we create the tree after reading it
1474   cdbttree->WriteCDBTTree();
1475   delete cdbttree;
1476 
1477   return 1;
1478 }
1479 #endif
1480 
1481 int MbdCalib::Write_TQT0(const std::string& dbfile)
1482 {
1483   std::ofstream cal_t0_file;
1484   cal_t0_file.open(dbfile);
1485   for (int ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
1486   {
1487     cal_t0_file << ipmt << "\t" << _tqfit_t0mean[ipmt] << "\t" << _tqfit_t0meanerr[ipmt]
1488       << "\t" << _tqfit_t0sigma[ipmt] << "\t" << _tqfit_t0sigmaerr[ipmt] << std::endl;
1489   }
1490   cal_t0_file.close();
1491 
1492   return 1;
1493 }
1494 
1495 #ifndef ONLINE
1496 int MbdCalib::Write_CDB_T0Corr(const std::string& dbfile)
1497 {
1498   CDBTTree* cdbttree{ nullptr };
1499 
1500   std::cout << "Creating " << dbfile << std::endl;
1501   cdbttree = new CDBTTree( dbfile );
1502   cdbttree->SetSingleIntValue("version", 1);
1503 
1504   std::cout << "T0Corr" << std::endl;
1505 
1506   // store in a CDBTree
1507   cdbttree->SetSingleFloatValue("_t0corrmean", _t0corrmean);
1508   cdbttree->SetSingleFloatValue("_t0corrmeanerr", _t0corrmeanerr);
1509   cdbttree->SetSingleFloatValue("_t0corr_fitmean0", _t0corr_fitmean[0]);
1510   cdbttree->SetSingleFloatValue("_t0corr_fitmeanerr0", _t0corr_fitmeanerr[0]);
1511   cdbttree->SetSingleFloatValue("_t0corr_fitsigma0", _t0corr_fitsigma[0]);
1512   cdbttree->SetSingleFloatValue("_t0corr_fitsigmaerr0", _t0corr_fitsigmaerr[0]);
1513   cdbttree->SetSingleFloatValue("_t0corr_fitmean1", _t0corr_fitmean[1]);
1514   cdbttree->SetSingleFloatValue("_t0corr_fitmeanerr1", _t0corr_fitmeanerr[1]);
1515   cdbttree->SetSingleFloatValue("_t0corr_fitsigma1", _t0corr_fitsigma[1]);
1516   cdbttree->SetSingleFloatValue("_t0corr_fitsigmaerr1", _t0corr_fitsigmaerr[1]);
1517   cdbttree->SetSingleFloatValue("_t0corr_hmean0", _t0corr_hmean[0]);
1518   cdbttree->SetSingleFloatValue("_t0corr_hmeanerr0", _t0corr_hmeanerr[0]);
1519   cdbttree->SetSingleFloatValue("_t0corr_hstddev0", _t0corr_hstddev[0]);
1520   cdbttree->SetSingleFloatValue("_t0corr_hstddeverr0", _t0corr_hstddeverr[0]);
1521   cdbttree->SetSingleFloatValue("_t0corr_hmean1", _t0corr_hmean[1]);
1522   cdbttree->SetSingleFloatValue("_t0corr_hmeanerr1", _t0corr_hmeanerr[1]);
1523   cdbttree->SetSingleFloatValue("_t0corr_hstddev1", _t0corr_hstddev[1]);
1524   cdbttree->SetSingleFloatValue("_t0corr_hstddeverr1", _t0corr_hstddeverr[1]);
1525 
1526   std::cout << "T0Corr\t" << cdbttree->GetSingleFloatValue("_t0corrmean") << std::endl;
1527 
1528   cdbttree->CommitSingle();
1529   //cdbttree->Commit();
1530   //cdbttree->Print();
1531 
1532   // for now we create the tree after reading it
1533   cdbttree->WriteCDBTTree();
1534   delete cdbttree;
1535 
1536   return 1;
1537 }
1538 #endif
1539 
1540 int MbdCalib::Write_T0Corr(const std::string& dbfile)
1541 {
1542   std::ofstream cal_t0corr_file;
1543   cal_t0corr_file.open(dbfile);
1544   cal_t0corr_file << _t0corrmean << "\t" << _t0corrmeanerr << std::endl;
1545   cal_t0corr_file << _t0corr_fitmean[0] << "\t" << _t0corr_fitmeanerr[0] << "\t" << _t0corr_fitsigma[0] << "\t" << _t0corr_fitsigmaerr[0] << std::endl;
1546   cal_t0corr_file << _t0corr_fitmean[1] << "\t" << _t0corr_fitmeanerr[1] << "\t" << _t0corr_fitsigma[1] << "\t" << _t0corr_fitsigmaerr[1] << std::endl;
1547   cal_t0corr_file << _t0corr_hmean[0] << "\t" << _t0corr_hmeanerr[0] << "\t" << _t0corr_hstddev[0] << "\t" << _t0corr_hstddeverr[0] << std::endl;
1548   cal_t0corr_file << _t0corr_hmean[1] << "\t" << _t0corr_hmeanerr[1] << "\t" << _t0corr_hstddev[1] << "\t" << _t0corr_hstddeverr[1] << std::endl;
1549   cal_t0corr_file.close();
1550 
1551   return 1;
1552 }
1553 
1554 #ifndef ONLINE
1555 int MbdCalib::Write_CDB_Ped(const std::string& dbfile)
1556 {
1557   CDBTTree* cdbttree{ nullptr };
1558 
1559   std::cout << "Creating " << dbfile << std::endl;
1560   cdbttree = new CDBTTree( dbfile );
1561   cdbttree->SetSingleIntValue("version", 1);
1562 
1563   std::cout << "Ped" << std::endl;
1564   for (size_t ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
1565   {
1566     // store in a CDBTree
1567     cdbttree->SetFloatValue(ifeech, "pedmean", _pedmean[ifeech]);
1568     cdbttree->SetFloatValue(ifeech, "pedmeanerr", _pedmeanerr[ifeech]);
1569     cdbttree->SetFloatValue(ifeech, "pedsigma", _pedsigma[ifeech]);
1570     cdbttree->SetFloatValue(ifeech, "pedsigmaerr", _pedsigmaerr[ifeech]);
1571 
1572     if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
1573     {
1574       std::cout << ifeech << "\t" << cdbttree->GetFloatValue(ifeech, "pedmean") << std::endl;
1575     }
1576   }
1577   cdbttree->CommitSingle();
1578 
1579   cdbttree->Commit();
1580   // cdbttree->Print();
1581 
1582   // for now we create the tree after reading it
1583   cdbttree->WriteCDBTTree();
1584   delete cdbttree;
1585 
1586   return 1;
1587 }
1588 #endif
1589 
1590 int MbdCalib::Write_Ped(const std::string& dbfile)
1591 {
1592   std::ofstream cal_ped_file;
1593   cal_ped_file.open(dbfile);
1594   for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
1595   {
1596     cal_ped_file << ifeech << "\t" << _pedmean[ifeech] << "\t" << _pedmeanerr[ifeech]
1597       << "\t" << _pedsigma[ifeech] << "\t" << _pedsigmaerr[ifeech] << std::endl;
1598   }
1599   cal_ped_file.close();
1600 
1601   return 1;
1602 }
1603 
1604 #ifndef ONLINE
1605 int MbdCalib::Write_CDB_Shapes(const std::string& dbfile)
1606 {
1607   // store in a CDBTree
1608   CDBTTree* cdbttree{nullptr};
1609 
1610   std::cout << "Creating " << dbfile << std::endl;
1611   cdbttree = new CDBTTree(dbfile);
1612   cdbttree->SetSingleIntValue("version", 1);
1613   cdbttree->CommitSingle();
1614 
1615   std::cout << "SHAPES" << std::endl;
1616   for (unsigned int ifeech = 0; ifeech < _sampmax.size(); ifeech++)
1617   {
1618     if (_mbdgeom->get_type(ifeech) == 0)
1619     {
1620       continue;  // skip t-channels
1621     }
1622 
1623     cdbttree->SetIntValue(ifeech, "shape_npts", _shape_npts[ifeech]);
1624     cdbttree->SetFloatValue(ifeech, "shape_min", _shape_minrange[ifeech]);
1625     cdbttree->SetFloatValue(ifeech, "shape_max", _shape_maxrange[ifeech]);
1626 
1627     for (int ipt = 0; ipt < _shape_npts[ifeech]; ipt++)
1628     {
1629       int temp_ch = (ipt * 1000) + ifeech;
1630       cdbttree->SetFloatValue(temp_ch, "shape_val", _shape_y[ifeech][ipt]);
1631     }
1632 
1633     cdbttree->SetIntValue(ifeech, "sherr_npts", _sherr_npts[ifeech]);
1634     cdbttree->SetFloatValue(ifeech, "sherr_min", _sherr_minrange[ifeech]);
1635     cdbttree->SetFloatValue(ifeech, "sherr_max", _sherr_maxrange[ifeech]);
1636 
1637     for (int ipt = 0; ipt < _shape_npts[ifeech]; ipt++)
1638     {
1639       int temp_ch = (ipt * 1000) + ifeech;
1640       cdbttree->SetFloatValue(temp_ch, "sherr_val", _sherr_yerr[ifeech][ipt]);
1641     }
1642   }
1643 
1644   cdbttree->Commit();
1645   // cdbttree->Print();
1646 
1647   for (unsigned int ifeech = 0; ifeech < _sampmax.size(); ifeech++)
1648   {
1649     if (_mbdgeom->get_type(ifeech) == 0)
1650     {
1651       continue;  // skip t-channels
1652     }
1653 
1654     if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_FEECH - 5)
1655     {
1656       std::cout << ifeech << "\t" << cdbttree->GetIntValue(ifeech, "shape_npts") << std::endl;
1657       for (int ipt = 0; ipt < 10; ipt++)
1658       {
1659         int temp_ch = (ipt * 1000) + (int)ifeech;
1660         std::cout << cdbttree->GetFloatValue(temp_ch, "shape_val") << "  ";
1661       }
1662       std::cout << std::endl;
1663     }
1664   }
1665 
1666   // for now we create the tree after reading it
1667   cdbttree->WriteCDBTTree();
1668   delete cdbttree;
1669 
1670   return 1;
1671 }
1672 #endif
1673 
1674 #ifndef ONLINE
1675 int MbdCalib::Write_CDB_TimeCorr(const std::string& dbfile)
1676 {
1677   // store in a CDBTree
1678   CDBTTree *cdbttree {nullptr};
1679 
1680   std::cout << "Creating " << dbfile << std::endl;
1681   cdbttree = new CDBTTree( dbfile );
1682   cdbttree->SetSingleIntValue("version",1);
1683   cdbttree->CommitSingle();
1684 
1685   std::cout << "TIMECORR" << std::endl;
1686   for (int ifeech=0; ifeech<MbdDefs::MBD_N_FEECH; ifeech++) 
1687   {
1688     if ( _mbdgeom->get_type(ifeech) == 1 )
1689     {
1690       continue;  // skip q-channels
1691     }
1692 
1693     cdbttree->SetIntValue(ifeech,"tcorr_npts",_tcorr_npts[ifeech]);
1694     cdbttree->SetFloatValue(ifeech,"tcorr_min",_tcorr_minrange[ifeech]);
1695     cdbttree->SetFloatValue(ifeech,"tcorr_max",_tcorr_maxrange[ifeech]);
1696 
1697     for (int ipt=0; ipt<_tcorr_npts[ifeech]; ipt++)
1698     {
1699       int temp_ch = (ipt*1000) + ifeech;
1700       cdbttree->SetFloatValue(temp_ch,"tcorr_val",_tcorr_y[ifeech][ipt]);
1701     }
1702   }
1703 
1704   cdbttree->Commit();
1705   //cdbttree->Print();
1706 
1707   for (size_t ifeech=0; ifeech<MbdDefs::MBD_N_FEECH; ifeech++) 
1708   {
1709     if ( _mbdgeom->get_type(ifeech) == 1 )
1710     {
1711       continue;  // skip q-channels
1712     }
1713 
1714     if ( ifeech<5 || ifeech>=MbdDefs::MBD_N_FEECH-5-8 )
1715     {
1716       std::cout << ifeech << "\t" <<  cdbttree->GetIntValue(ifeech,"tcorr_npts") << std::endl;
1717       for (int ipt=0; ipt<10; ipt++)
1718       {
1719         int temp_ch = (ipt*1000) + (int)ifeech;
1720         std::cout << cdbttree->GetFloatValue(temp_ch,"tcorr_val") << "  ";
1721       }
1722       std::cout << std::endl;
1723     }
1724   }
1725 
1726   // for now we create the tree after reading it
1727   cdbttree->WriteCDBTTree();
1728   delete cdbttree;
1729 
1730   return 1;
1731 }
1732 #endif
1733 
1734 #ifndef ONLINE
1735 int MbdCalib::Write_CDB_SlewCorr(const std::string& dbfile)
1736 {
1737   // store in a CDBTree
1738   CDBTTree *cdbttree {nullptr};
1739 
1740   std::cout << "Creating " << dbfile << std::endl;
1741   cdbttree = new CDBTTree( dbfile );
1742   cdbttree->SetSingleIntValue("version",1);
1743   cdbttree->CommitSingle();
1744 
1745   std::cout << "SLEWCORR" << std::endl;
1746   //for (size_t ifeech=0; ifeech<_sampmax.size(); ifeech++) 
1747   for (size_t ifeech=0; ifeech<MbdDefs::MBD_N_FEECH; ifeech++) 
1748   {
1749     if ( _mbdgeom->get_type(ifeech) == 1 )
1750     {
1751       continue;  // skip q-channels
1752     }
1753 
1754     cdbttree->SetIntValue(ifeech,"scorr_npts",_scorr_npts[ifeech]);
1755     cdbttree->SetFloatValue(ifeech,"scorr_min",_scorr_minrange[ifeech]);
1756     cdbttree->SetFloatValue(ifeech,"scorr_max",_scorr_maxrange[ifeech]);
1757 
1758     for (int ipt=0; ipt<_scorr_npts[ifeech]; ipt++)
1759     {
1760       int temp_ch = (ipt*1000) + (int)ifeech;
1761       cdbttree->SetFloatValue(temp_ch,"scorr_val",_scorr_y[ifeech][ipt]);
1762     }
1763   }
1764 
1765   cdbttree->Commit();
1766   //cdbttree->Print();
1767 
1768   for (size_t ifeech=0; ifeech<MbdDefs::MBD_N_FEECH; ifeech++) 
1769   {
1770     if ( _mbdgeom->get_type(ifeech) == 1 )
1771     {
1772       continue;  // skip q-channels
1773     }
1774 
1775     if ( ifeech<5 || ifeech>=MbdDefs::MBD_N_FEECH-8-5 )
1776     {
1777       std::cout << ifeech << "\t" <<  cdbttree->GetIntValue(ifeech,"scorr_npts") << std::endl;
1778       for (int ipt=0; ipt<10; ipt++)
1779       {
1780         int temp_ch = (ipt*1000) + (int)ifeech;
1781         std::cout << cdbttree->GetFloatValue(temp_ch,"scorr_val") << "  ";
1782       }
1783       std::cout << std::endl;
1784     }
1785   }
1786 
1787   // for now we create the tree after reading it
1788   cdbttree->WriteCDBTTree();
1789   delete cdbttree;
1790 
1791   return 1;
1792 }
1793 #endif
1794 
1795 #ifndef ONLINE
1796 int MbdCalib::Write_CDB_Gains(const std::string& dbfile)
1797 {
1798   CDBTTree* cdbttree{ nullptr };
1799 
1800   std::cout << "Creating " << dbfile << std::endl;
1801   cdbttree = new CDBTTree( dbfile );
1802   cdbttree->SetSingleIntValue("version", 1);
1803   cdbttree->CommitSingle();
1804 
1805   std::cout << "MBD_QFIT" << std::endl;
1806   for (size_t ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
1807   {
1808     // store in a CDBTree
1809     cdbttree->SetFloatValue(ipmt, "qfit_integ", _qfit_integ[ipmt]);
1810     cdbttree->SetFloatValue(ipmt, "qfit_mpv", _qfit_mpv[ipmt]);
1811     cdbttree->SetFloatValue(ipmt, "qfit_sigma", _qfit_sigma[ipmt]);
1812     cdbttree->SetFloatValue(ipmt, "qfit_integerr", _qfit_integerr[ipmt]);
1813     cdbttree->SetFloatValue(ipmt, "qfit_mpverr", _qfit_mpverr[ipmt]);
1814     cdbttree->SetFloatValue(ipmt, "qfit_sigmaerr", _qfit_sigmaerr[ipmt]);
1815     cdbttree->SetFloatValue(ipmt, "qfit_chi2ndf", _qfit_chi2ndf[ipmt]);
1816 
1817     if (ipmt < 5 || ipmt >= MbdDefs::MBD_N_PMT - 5)
1818     {
1819       std::cout << ipmt << "\t" << cdbttree->GetFloatValue(ipmt, "qfit_mpv") << std::endl;
1820     }
1821   }
1822 
1823   cdbttree->Commit();
1824   // cdbttree->Print();
1825 
1826   // for now we create the tree after reading it
1827   cdbttree->WriteCDBTTree();
1828   delete cdbttree;
1829 
1830   return 1;
1831 }
1832 #endif
1833 
1834 int MbdCalib::Write_Gains(const std::string& dbfile)
1835 {
1836   std::ofstream cal_gains_file;
1837   cal_gains_file.open(dbfile);
1838   for (int ipmtch = 0; ipmtch < MbdDefs::MBD_N_PMT; ipmtch++)
1839   {
1840     cal_gains_file << ipmtch << "\t" << _qfit_integ[ipmtch] << "\t" << _qfit_mpv[ipmtch]
1841       << "\t" << _qfit_sigma[ipmtch] << "\t" << _qfit_integerr[ipmtch]
1842       << "\t" << _qfit_mpverr[ipmtch] << "\t" << _qfit_sigmaerr[ipmtch]
1843       << "\t" << _qfit_chi2ndf[ipmtch] << std::endl;
1844   }
1845   cal_gains_file.close();
1846 
1847   return 1;
1848 }
1849 
1850 #ifndef ONLINE
1851 int MbdCalib::Write_CDB_Pileup(const std::string& dbfile)
1852 {
1853   CDBTTree* cdbttree{ nullptr };
1854 
1855   std::cout << "Creating " << dbfile << std::endl;
1856   cdbttree = new CDBTTree( dbfile );
1857   cdbttree->SetSingleIntValue("version", 1);
1858   cdbttree->CommitSingle();
1859 
1860   std::cout << "MBD_PILEUP" << std::endl;
1861   for (size_t ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
1862   {
1863     // store in a CDBTree
1864     cdbttree->SetFloatValue(ifeech, "pileup_p0", _pileup_p0[ifeech]);
1865     cdbttree->SetFloatValue(ifeech, "pileup_p0err", _pileup_p0err[ifeech]);
1866     cdbttree->SetFloatValue(ifeech, "pileup_p1", _pileup_p1[ifeech]);
1867     cdbttree->SetFloatValue(ifeech, "pileup_p1err", _pileup_p1err[ifeech]);
1868     cdbttree->SetFloatValue(ifeech, "pileup_p2", _pileup_p2[ifeech]);
1869     cdbttree->SetFloatValue(ifeech, "pileup_p2err", _pileup_p2err[ifeech]);
1870     cdbttree->SetFloatValue(ifeech, "pileup_chi2ndf", _pileup_chi2ndf[ifeech]);
1871 
1872     if (ifeech < 5 || ifeech >= MbdDefs::MBD_N_PMT - 5)
1873     {
1874       std::cout << ifeech << "\t" << cdbttree->GetFloatValue(ifeech, "pileup_p0") << std::endl;
1875     }
1876   }
1877 
1878   cdbttree->Commit();
1879   // cdbttree->Print();
1880 
1881   // for now we create the tree after reading it
1882   cdbttree->WriteCDBTTree();
1883   delete cdbttree;
1884 
1885   return 1;
1886 }
1887 #endif
1888 
1889 int MbdCalib::Write_Pileup(const std::string& dbfile)
1890 {
1891   std::ofstream cal_pileup_file;
1892   cal_pileup_file.open(dbfile);
1893   for (int ifeech = 0; ifeech < MbdDefs::MBD_N_FEECH; ifeech++)
1894   {
1895     cal_pileup_file << ifeech << "\t" << _pileup_p0[ifeech] << "\t" << _pileup_p0err[ifeech]
1896       << "\t" << _pileup_p1[ifeech] << "\t" << _pileup_p1err[ifeech]
1897       << "\t" << _pileup_p2[ifeech] << "\t" << _pileup_p2err[ifeech]
1898       << "\t" << _pileup_chi2ndf[ifeech] << std::endl;
1899   }
1900   cal_pileup_file.close();
1901 
1902   return 1;
1903 }
1904 
1905 #ifndef ONLINE
1906 int MbdCalib::Write_CDB_Thresholds(const std::string& dbfile)
1907 {
1908   CDBTTree* cdbttree{ nullptr };
1909 
1910   std::cout << "Creating " << dbfile << std::endl;
1911   cdbttree = new CDBTTree( dbfile );
1912   cdbttree->SetSingleIntValue("version", 1);
1913   cdbttree->CommitSingle();
1914 
1915   std::cout << "MBD_THRESHOLDS" << std::endl;
1916   for (size_t ipmt = 0; ipmt < MbdDefs::MBD_N_PMT; ipmt++)
1917   {
1918     // store in a CDBTree
1919     cdbttree->SetFloatValue(ipmt, "thresh_mean", _thresh_mean[ipmt]);
1920     cdbttree->SetFloatValue(ipmt, "thresh_meanerr", _thresh_meanerr[ipmt]);
1921     cdbttree->SetFloatValue(ipmt, "thresh_width", _thresh_width[ipmt]);
1922     cdbttree->SetFloatValue(ipmt, "thresh_widtherr", _thresh_widtherr[ipmt]);
1923     cdbttree->SetFloatValue(ipmt, "thresh_eff", _thresh_eff[ipmt]);
1924     cdbttree->SetFloatValue(ipmt, "thresh_efferr", _thresh_efferr[ipmt]);
1925     cdbttree->SetFloatValue(ipmt, "thresh_chi2ndf", _thresh_chi2ndf[ipmt]);
1926 
1927     if (ipmt < 5 || ipmt >= MbdDefs::MBD_N_PMT - 5)
1928     {
1929       std::cout << ipmt << "\t" << cdbttree->GetFloatValue(ipmt, "thresh_mpv") << std::endl;
1930     }
1931   }
1932 
1933   cdbttree->Commit();
1934   // cdbttree->Print();
1935 
1936   // for now we create the tree after reading it
1937   cdbttree->WriteCDBTTree();
1938   delete cdbttree;
1939 
1940   return 1;
1941 }
1942 #endif
1943 
1944 int MbdCalib::Write_Thresholds(const std::string& dbfile)
1945 {
1946   std::ofstream cal_thresh_file;
1947   cal_thresh_file.open(dbfile);
1948   for (int ipmtch = 0; ipmtch < MbdDefs::MBD_N_PMT; ipmtch++)
1949   {
1950     cal_thresh_file << ipmtch << "\t" << _thresh_mean[ipmtch] << "\t" << _thresh_meanerr[ipmtch]
1951       << "\t" << _thresh_width[ipmtch] << "\t" << _thresh_widtherr[ipmtch]
1952       << "\t" << _thresh_eff[ipmtch] << "\t" << _thresh_efferr[ipmtch]
1953       << "\t" << _thresh_chi2ndf[ipmtch] << std::endl;
1954   }
1955   cal_thresh_file.close();
1956 
1957   return 1;
1958 }
1959 
1960 #ifndef ONLINE
1961 int MbdCalib::Write_CDB_All()
1962 {
1963   return 1;
1964 }
1965 #endif
1966 
1967 // dz is what we need to move the MBD z by
1968 // dt is what we change the MBD t0 by
1969 void MbdCalib::Update_TQT0(const float dz, const float dt)
1970 {
1971   const float z_dt = dz/MbdDefs::C;
1972 
1973   for (int ipmt=0; ipmt<MbdDefs::MBD_N_PMT; ipmt++)
1974   {
1975     // update zvtx
1976     if ( ipmt<64 )  // south
1977     {
1978       _tqfit_t0mean[ipmt] -= z_dt;
1979     }
1980     else
1981     {
1982       _tqfit_t0mean[ipmt] += z_dt;
1983     }
1984 
1985     // update t0
1986     _tqfit_t0mean[ipmt] -= dt;
1987   }
1988 }
1989 
1990 void MbdCalib::Update_TTT0(const float dz, const float dt)
1991 {
1992   // dz is what we need to move the MBD z by
1993   const float z_dt = dz/MbdDefs::C;
1994 
1995   for (int ipmt=0; ipmt<MbdDefs::MBD_N_PMT; ipmt++)
1996   {
1997     if ( ipmt<64 )  // south
1998     {
1999       _ttfit_t0mean[ipmt] -= z_dt;
2000     }
2001     else
2002     {
2003       _ttfit_t0mean[ipmt] += z_dt;
2004     }
2005 
2006     // update t0
2007     _ttfit_t0mean[ipmt] -= dt;
2008   }
2009 }
2010 
2011 void MbdCalib::Reset_TTT0()
2012 {
2013   _ttfit_t0mean.fill( 0. );
2014   _ttfit_t0meanerr.fill( 0. );
2015   _ttfit_t0sigma.fill(std::numeric_limits<float>::quiet_NaN());
2016   _ttfit_t0sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
2017 }
2018 
2019 void MbdCalib::Reset_TQT0()
2020 {
2021   _tqfit_t0mean.fill( 0. );
2022   _tqfit_t0meanerr.fill( 0. );
2023   _tqfit_t0sigma.fill(std::numeric_limits<float>::quiet_NaN());
2024   _tqfit_t0sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
2025 }
2026 
2027 void MbdCalib::Reset_T0Corr()
2028 {
2029   _t0corrmean = 0.;
2030   _t0corrmeanerr = 0.;
2031   _t0corr_fitmean.fill(std::numeric_limits<float>::quiet_NaN());
2032   _t0corr_fitmeanerr.fill(std::numeric_limits<float>::quiet_NaN());
2033   _t0corr_fitsigma.fill(std::numeric_limits<float>::quiet_NaN());
2034   _t0corr_fitsigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
2035   _t0corr_hmean.fill(std::numeric_limits<float>::quiet_NaN());
2036   _t0corr_hmeanerr.fill(std::numeric_limits<float>::quiet_NaN());
2037   _t0corr_hstddev.fill(std::numeric_limits<float>::quiet_NaN());
2038   _t0corr_hstddeverr.fill(std::numeric_limits<float>::quiet_NaN());
2039 }
2040 
2041 void MbdCalib::Reset_Ped()
2042 {
2043   _tqfit_t0mean.fill( 0. );
2044   _tqfit_t0meanerr.fill( 0. );
2045   _tqfit_t0sigma.fill(std::numeric_limits<float>::quiet_NaN());
2046   _tqfit_t0sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
2047 }
2048 
2049 void MbdCalib::Reset_Gains()
2050 {
2051   // Set all initial values
2052   _qfit_integ.fill(std::numeric_limits<float>::quiet_NaN());
2053   _qfit_mpv.fill( 1.0 );
2054   _qfit_sigma.fill(std::numeric_limits<float>::quiet_NaN());
2055   _qfit_integerr.fill(std::numeric_limits<float>::quiet_NaN());
2056   _qfit_mpverr.fill(std::numeric_limits<float>::quiet_NaN());
2057   _qfit_sigmaerr.fill(std::numeric_limits<float>::quiet_NaN());
2058   _qfit_chi2ndf.fill(std::numeric_limits<float>::quiet_NaN());
2059 }
2060 
2061 void MbdCalib::Reset_Pileup()
2062 {
2063   // Set all initial values
2064   _pileup_p0.fill(std::numeric_limits<float>::quiet_NaN());
2065   _pileup_p1.fill(std::numeric_limits<float>::quiet_NaN());
2066   _pileup_p2.fill(std::numeric_limits<float>::quiet_NaN());
2067   _pileup_p0err.fill(std::numeric_limits<float>::quiet_NaN());
2068   _pileup_p1err.fill(std::numeric_limits<float>::quiet_NaN());
2069   _pileup_p2err.fill(std::numeric_limits<float>::quiet_NaN());
2070   _qfit_chi2ndf.fill(std::numeric_limits<float>::quiet_NaN());
2071 }
2072 
2073 void MbdCalib::Reset_Thresholds()
2074 {
2075   // Set all initial values
2076   _thresh_mean.fill(std::numeric_limits<float>::quiet_NaN());
2077   _thresh_meanerr.fill(std::numeric_limits<float>::quiet_NaN());
2078   _thresh_width.fill(std::numeric_limits<float>::quiet_NaN());
2079   _thresh_widtherr.fill(std::numeric_limits<float>::quiet_NaN());
2080   _thresh_eff.fill(std::numeric_limits<float>::quiet_NaN());
2081   _thresh_efferr.fill(std::numeric_limits<float>::quiet_NaN());
2082   _thresh_chi2ndf.fill(std::numeric_limits<float>::quiet_NaN());
2083 }
2084 
2085 void MbdCalib::Reset()
2086 {
2087   Reset_TTT0();
2088   Reset_TQT0();
2089   Reset_Ped();
2090   Reset_Gains();
2091   Reset_T0Corr();
2092   Reset_Pileup();
2093   Reset_Thresholds();
2094 
2095   _sampmax.fill(-1);
2096 }
2097 
2098 void MbdCalib::set_ped(const int ifeech, const float m, const float merr, const float s, const float serr)
2099 {
2100   _pedmean[ifeech] = m;
2101   _pedmeanerr[ifeech] = merr;
2102   _pedsigma[ifeech] = s;
2103   _pedsigmaerr[ifeech] = serr;
2104 }
2105 
2106 
2107 float MbdCalib::get_threshold(const int pmtch, const int rel_or_abs)
2108 {
2109   if ( rel_or_abs==0 )
2110   {
2111     return _thresh_mean[pmtch]/_qfit_mpv[pmtch];
2112   }
2113   else
2114   {
2115     return _thresh_mean[pmtch];
2116   }
2117 
2118   return -1.; // error
2119 }