Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:23:59

0001 // Make plots of the values of the gains
0002 #include <mbd/MbdCalib.h>
0003 
0004 #include <Rtypes.h> // defines R__LOAD_LIBRARY macro
0005 #include <TCanvas.h>
0006 #include <TF1.h>
0007 #include <TFile.h>
0008 #include <TGraphErrors.h>
0009 #include <TH1.h>
0010 
0011 #include <fstream>
0012 
0013 R__LOAD_LIBRARY(libmbd.so)
0014 R__LOAD_LIBRARY(libmbd_io.so)
0015 
0016 void read_calibgains(const char *flist);
0017 void plot_relwidth_onerun(int irun = 0);
0018 
0019 TFile *savefile {nullptr};
0020 
0021 const int MAX_RUNS = 2500;
0022 int nruns = 0;
0023 
0024 MbdCalib *bcal[MAX_RUNS];
0025 
0026 Double_t bqmean[128][MAX_RUNS];
0027 Double_t bqmeanerr[128][MAX_RUNS];
0028 Double_t bqwidth[128][MAX_RUNS];
0029 Double_t bqwidtherr[128][MAX_RUNS];
0030 Double_t bqrelwidth[128][MAX_RUNS];
0031 Double_t bqrelwidtherr[128][MAX_RUNS];
0032 Double_t listofruns[MAX_RUNS];
0033 Double_t runindex[MAX_RUNS];
0034 
0035 TGraphErrors *gainvals[128];
0036 TGraphErrors *g_relwidth[128];  // relative width
0037 TH1 *h_relwidth{nullptr};
0038 
0039 TCanvas *ac{nullptr};
0040 
0041 const int update_qfit = 1;    // whether to write update gain files
0042 const int write_plots = 1;    // whether to save plots to png
0043   
0044 
0045 // flist is a list of run-seq or run number directories
0046 // where the calibrations are stored
0047 void read_calibgains(const char *flist)
0048 {
0049   std::cout << "read_calibgains()" << std::endl;
0050   std::ifstream inflist(flist);
0051 
0052   std::ifstream cal_mip_file;
0053 
0054   TString calrunseq;
0055   TString calfile;
0056   TString name;
0057   while ( inflist >> calrunseq )
0058   {
0059     calfile = "results/" + calrunseq + "/mbd_qfit.calib";
0060     std::cout << calfile << std::endl;
0061     cal_mip_file.open( calfile );
0062 
0063     TString runtext = calrunseq;
0064     runtext.ReplaceAll("-0000","");
0065     listofruns[nruns] = runtext.Atof();
0066     std::cout << listofruns[nruns] << std::endl;
0067 
0068     runindex[nruns] = nruns;
0069 
0070     //bcal[nruns] = new MbdCalib();
0071     //bcal[nruns]->Download_Gains( calfile.Data() );
0072 
0073     int    temp_pmt;
0074     double integ;
0075     double best_peak;
0076     double width;
0077     double integerr;
0078     double best_peakerr;
0079     double widtherr;
0080     double chi2ndf;
0081 
0082 //    double corrected_peak;
0083 
0084     for (int ipmt=0; ipmt<128; ipmt++)
0085     {
0086       //float gain = bcal[nruns]->get_qgain(ipmt);
0087       //float gainerr = bcal[nruns]->get_qgainerr(ipmt);
0088 
0089       cal_mip_file >> temp_pmt >> integ >> best_peak >> width 
0090         >> integerr >> best_peakerr >> widtherr >> chi2ndf;
0091 
0092       if ( ipmt==0 ) { std::cout << temp_pmt << "\t" << integ << "\t" << best_peak << "\t" << width 
0093         << "\t" << integerr << "\t" << best_peakerr << "\t" << widtherr << "\t" << chi2ndf << std::endl;
0094 }
0095 
0096       bqmean[temp_pmt][nruns] = best_peak;
0097       bqmeanerr[temp_pmt][nruns] = best_peakerr;
0098       bqwidth[temp_pmt][nruns] = fabs(width);
0099       bqwidtherr[temp_pmt][nruns] = widtherr;
0100       
0101       /*
0102       if ( listofruns[nruns] == 21520 )
0103       {
0104         std::cout << "XXX " << calfile << "\t" << ipmt << "\t" << best_peakerr << "\t"
0105           << best_peak << "\t" << bqmean[temp_pmt][nruns-1] << std::endl;
0106       }
0107       */
0108 
0109       // check for bad fit
0110       //if ( integ < 100. || bqmean[temp_pmt][nruns]<0. || chi2ndf>4.0 )
0111       if ( integ < 100. || bqmean[temp_pmt][nruns]<0. || chi2ndf>10.0 )
0112       {
0113         std::cout << "BAD " << calfile << "\t" << ipmt << "\t" << best_peak
0114           << "\t" << integ << "\t" << bqmean[temp_pmt][nruns] << "\t" << chi2ndf << std::endl;
0115         bqmean[temp_pmt][nruns] = NAN;
0116       }
0117 
0118       if ( bqmeanerr[temp_pmt][nruns]>10. )
0119       {
0120         std::cout << "BADERR " << calfile << "\t" << ipmt << "\t" << best_peakerr << "\t"
0121           << best_peak << "\t" << bqmean[temp_pmt][nruns-1] << std::endl;
0122         bqmeanerr[temp_pmt][nruns] = NAN;
0123       }
0124     }
0125 
0126     cal_mip_file.close();
0127 
0128     nruns++;
0129   }
0130   inflist.close();
0131 }
0132 
0133 void plot_relwidth_onerun(int irun)
0134 {
0135   if ( h_relwidth==nullptr )
0136   {
0137     h_relwidth = new TH1F("h_relwidth","relative width",100,0.1,0.36);
0138   }
0139   std::cout << "== Relative Widths ==" << std::endl;
0140   for (int ipmt=0; ipmt<128; ipmt++)
0141   {
0142     double relwidth = bqwidth[ipmt][irun]/bqmean[ipmt][irun];
0143     double frac_merr = bqmeanerr[ipmt][irun]/bqmean[ipmt][irun];
0144     double frac_werr = bqwidtherr[ipmt][irun]/bqwidth[ipmt][irun];
0145     double relwidtherr = frac_merr*frac_merr + frac_werr*frac_werr;
0146     relwidtherr = relwidth*sqrt(relwidtherr);
0147 
0148     std::cout << ipmt << "\t" << relwidth << "\t" << relwidtherr << std::endl;
0149 
0150     h_relwidth->Fill( relwidth );
0151   }
0152   h_relwidth->Draw();
0153 }
0154 
0155 
0156 // Find any values that deviate greatly from prior run, and set to nan
0157 // Note that 1st run must be good
0158 //void CheckForLargeDeviation( const double max_deviation = 0.05 ) // 5% deviation
0159 void CheckForLargeDeviation( const double max_deviation = 0.10 ) // 10% deviation
0160 {
0161   for (int ipmt=0; ipmt<128; ipmt++)
0162   {
0163     for (int irun=1; irun<nruns; irun++)
0164     {
0165       if ( std::isnan(bqmean[ipmt][irun]) ) { continue;  // skip ones already bad
0166 }
0167 
0168       // search for prev good val
0169       double prevgoodmean = NAN;
0170       for (int prevrun=irun-1; prevrun>=0; prevrun--)
0171       {
0172         if ( !std::isnan( bqmean[ipmt][prevrun] ) )
0173         {
0174           prevgoodmean = bqmean[ipmt][prevrun];
0175           break;
0176         }
0177       }
0178 
0179       double deviation = fabs((bqmean[ipmt][irun] - prevgoodmean)/prevgoodmean);
0180       if ( deviation > max_deviation )
0181       {
0182         std::cout << "BADDEV " << listofruns[irun] << "\t" << ipmt << "\t" << bqmean[ipmt][irun]
0183           << "\t" << prevgoodmean << "\t" << deviation << std::endl;
0184         bqmean[ipmt][irun] = NAN;
0185       }
0186     }
0187   }
0188 
0189 }
0190 
0191 void plot_calibgains(const char *flist = "runs.list")
0192 {
0193   // Read in all the calibrations from flist
0194   read_calibgains(flist);
0195 
0196   if ( write_plots )
0197   {
0198     savefile = new TFile("results/calibgains.root","RECREATE");
0199   }
0200 
0201   // sanity check that gains aren't bad
0202   CheckForLargeDeviation();
0203 
0204   TString pdfname = "gainvals.pdf";
0205   if ( write_plots )
0206   {
0207     ac = new TCanvas("c_gainvals","gainvals",1100,850);
0208     gPad->Print( pdfname + "[");
0209   }
0210 
0211   TString name;
0212   TString title;
0213   for (int ipmt=0; ipmt<128; ipmt++)
0214   {
0215     // do corrections
0216     for (int irun=0; irun<nruns; irun++)
0217     {
0218 
0219       if ( std::isnan(bqmean[ipmt][irun]) )
0220       {
0221         std::cout << "FOUND BAD " << listofruns[irun] << " " << ipmt << " " << bqmean[ipmt][irun] << std::endl;
0222         // search for next good val
0223         double nextgood = NAN;
0224         for (int nextrun=irun+1; nextrun<nruns; nextrun++)
0225         {
0226           if ( !std::isnan( bqmean[ipmt][nextrun] ) )
0227           {
0228             nextgood = bqmean[ipmt][nextrun];
0229             break;
0230           }
0231         }
0232 
0233 
0234         // search for prev good val
0235         double prevgood = NAN;
0236         for (int prevrun=irun-1; prevrun>=0; prevrun--)
0237         {
0238           if ( !std::isnan( bqmean[ipmt][prevrun] ) )
0239           {
0240             prevgood = bqmean[ipmt][prevrun];
0241             break;
0242           }
0243         }
0244 
0245         if ( !std::isnan(nextgood) && !std::isnan(prevgood) )
0246         {
0247           bqmean[ipmt][irun] = (nextgood+prevgood)/2.0;
0248         }
0249         else if ( !std::isnan(nextgood) )
0250         {
0251           bqmean[ipmt][irun] = nextgood;
0252         }
0253         else if ( !std::isnan(prevgood) )
0254         {
0255           bqmean[ipmt][irun] = prevgood;
0256         }
0257         else
0258         {
0259           std::cout << "ERROR, no good run to interpolate from" << std::endl;
0260         }
0261 
0262         std::cout << "NEW VALUE FOR BAD " << irun << " " << ipmt << " " << bqmean[ipmt][irun] << std::endl;
0263       }
0264 
0265       if ( std::isnan(bqmeanerr[ipmt][irun]) )
0266       {
0267         // search for next good val
0268         double nextgood = NAN;
0269         for (int nextrun=irun+1; nextrun<nruns; nextrun++)
0270         {
0271           if ( !std::isnan( bqmeanerr[ipmt][nextrun] ) )
0272           {
0273             nextgood = bqmeanerr[ipmt][nextrun];
0274             break;
0275           }
0276         }
0277 
0278 
0279         // search for prev good val
0280         double prevgood = NAN;
0281         for (int prevrun=irun-1; prevrun>=0; prevrun--)
0282         {
0283           if ( !std::isnan( bqmeanerr[ipmt][prevrun] ) )
0284           {
0285             prevgood = bqmeanerr[ipmt][prevrun];
0286             break;
0287           }
0288         }
0289 
0290         if ( irun==0 )
0291         {
0292           bqmeanerr[ipmt][irun] = nextgood;
0293         }
0294         else if ( irun==nruns-1 )
0295         {
0296           bqmeanerr[ipmt][irun] = prevgood;
0297         }
0298         else
0299         {
0300           bqmeanerr[ipmt][irun] = (nextgood+prevgood)/2.0;
0301         }
0302       }
0303     }
0304 
0305     name = "gainvals"; name += ipmt;
0306     title = "gain, ch"; title += ipmt;
0307     //gainvals[ipmt] = new TGraphErrors(nruns,runindex,bqmean[ipmt],0,bqmeanerr[ipmt]);
0308     gainvals[ipmt] = new TGraphErrors(nruns,listofruns,bqmean[ipmt],nullptr,bqmeanerr[ipmt]);
0309     gainvals[ipmt]->SetName( name );
0310     gainvals[ipmt]->SetTitle( title );
0311 
0312     gainvals[ipmt]->Draw("ap");
0313     gPad->Modified();
0314     gPad->Update();
0315 
0316     if ( write_plots )
0317     {
0318       gPad->Print( pdfname, name );
0319       gainvals[ipmt]->Write();
0320     }
0321   }
0322 
0323   if ( write_plots )
0324   {
0325     gPad->Print( pdfname + "]");
0326   }
0327 
0328   // Get mean gain for each channel, and write out the values
0329   TString meangainfname = "results/"; meangainfname += flist;
0330   meangainfname.ReplaceAll(".list","_meangains.calib");
0331   std::ofstream meangainfile( meangainfname );
0332   TF1 *f_meangain[128] {nullptr};
0333   Double_t grp_mean[128];
0334   Double_t grp_meanerr[128];
0335   for (int ipmt=0; ipmt<128; ipmt++)
0336   {
0337     name = "f_meangain"; name += ipmt;
0338     f_meangain[ipmt] = new TF1(name,"pol0",0,1e9);
0339     gainvals[ipmt]->Fit(f_meangain[ipmt]);
0340     gPad->Update();
0341     gPad->Modified();
0342     grp_mean[ipmt] = f_meangain[ipmt]->GetParameter(0);
0343     grp_meanerr[ipmt] = f_meangain[ipmt]->GetParError(0);
0344 
0345     meangainfile << ipmt << "\t" << grp_mean[ipmt] << "\t" << grp_meanerr[ipmt] << std::endl;
0346     std::cout << ipmt << "\t" << grp_mean[ipmt] << "\t" << grp_meanerr[ipmt] << std::endl;
0347   }
0348   meangainfile.close();
0349 
0350   // generate new gains
0351   std::ifstream inflist;
0352   std::ifstream cal_mip_file;
0353   TString calrunseq;
0354   TString calfile;
0355 
0356   if ( update_qfit )
0357   {
0358     inflist.open(flist);
0359     std::ofstream newcal_mip_file;
0360 
0361     int    temp_pmt;
0362     double integ;
0363     double best_peak;
0364     double width;
0365     double integerr;
0366     double best_peakerr;
0367     double widtherr;
0368     double chi2ndf;
0369 
0370     int irun = 0;
0371     while ( inflist >> calrunseq )
0372     {
0373       calfile = "results/" + calrunseq + "/mbd_qfit.calib";
0374       std::cout << calfile << std::endl;
0375       cal_mip_file.open( calfile );
0376 
0377       // create new gain corr file
0378       calfile = "results/" + calrunseq + "/newgainvals_mbd_qfit.calib";
0379       newcal_mip_file.open( calfile );
0380 
0381       for (int ipmt=0; ipmt<128; ipmt++)
0382       {
0383 
0384         cal_mip_file >> temp_pmt >> integ >> best_peak >> width 
0385           >> integerr >> best_peakerr >> widtherr >> chi2ndf;
0386 
0387         newcal_mip_file << temp_pmt << "\t" << integ << "\t" << bqmean[ipmt][irun] << "\t" << width << "\t"
0388           << integerr << "\t" << bqmeanerr[ipmt][irun] << "\t" << widtherr << "\t"
0389           << chi2ndf << std::endl;
0390       }
0391 
0392       cal_mip_file.close();
0393       newcal_mip_file.close();
0394       irun++;
0395     }
0396   }
0397 
0398   if ( write_plots )
0399   {
0400     savefile->Write();
0401   }
0402 }
0403