Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-07 03:06:14

0001 //////////////////////////////////////////////////////////////////////////////
0002 // Usage:
0003 // .L CalibFitPlots.C+g
0004 //             For standard set of histograms from CalibMonitor
0005 //  FitHistStandard(infile, outfile, prefix, mode, type, append, saveAll,
0006 //                  debug);
0007 //      Defaults: mode=11111, type=0, append=true, saveAll=false, debug=false
0008 //
0009 //             For extended set of histograms from CalibMonitor
0010 //  FitHistExtended(infile, outfile, prefix, numb, type, append, fiteta, iname,
0011 //                  debug);
0012 //      Defaults: numb=50, type=3, append=true, fiteta=true, iname=3,
0013 //                debug=false
0014 //
0015 //             Same as FitHistExtend with summary of each ieta/depth
0016 //  FitHistExtended2(infile, outfile, prefix, numb, append, iname, debug);
0017 //      Defaults: numb=50, append=true, iname=3, debug=false
0018 //
0019 //             For RBX dependence in sets of histograms from CalibMonitor
0020 //  FitHistRBX(infile, outfile, prefix, append, iname);
0021 //      Defaults: append=true, iname=2
0022 //
0023 //             For plotting stored histograms from FitHist's
0024 //  PlotHist(infile, prefix, text, modePlot, kopt, lumi, ener, isRealData,
0025 //           drawStatBox, save);
0026 //      Defaults: modePlot=4, kopt=100, lumi=0, ener=13, isRealData=false,
0027 //                drawStatBox=true, save=0
0028 //
0029 //             For plotting histograms corresponding to individual ieta's
0030 //  PlotHistEta(infile, prefix, text, iene, numb, ieta, lumi, ener, isRealData,
0031 //           drawStatBox, save);
0032 //      Defaults iene=3, numb=50, ieta=0, lumi=0, ener=13.0, isRealData=false,
0033 //                drawStatBox=true, save=0
0034 //
0035 //             For plotting several histograms in the same plot
0036 //             (fits to different data sets for example)
0037 //  PlotHists(infile, prefix, text, drawStatBox, save)
0038 //      Defaults: drawStatBox=true; save=0;
0039 //      Note prefix is common part for all histograms
0040 //
0041 //             For plotting on the same canvas plots with different
0042 //             prefixes residing in the same file with approrprate text
0043 //  PlotTwoHists(infile, prefix1, text1, prefix2, text2, text0, type, iname,
0044 //               lumi, ener, drawStatBox, save);
0045 //      Defaults: type=0; iname=2; lumi=0; ener=13; drawStatBox=true;
0046 //                save=0;
0047 //      Note prefixN, textN have the same meaning as prefix and text for set N
0048 //           text0 is the text for general title added within ()
0049 //           type=0 plots response distributions and MPV of response vs ieta
0050 //               =1 plots MPV of response vs RBX #
0051 //
0052 //             For plotting stored histograms from CalibTree
0053 //  PlotFiveHists(infile, text0, prefix0, type, iname, drawStatBox, normalize,
0054 //                save, prefix1, text1, prefix2, text2, prefix3, text3,
0055 //                 prefix4, text4, prefix5, text5);
0056 //      Defaults: type=0; iname=0; drawStatBox=true; normalize=false;
0057 //                save=0; prefixN=""; textN=""; (for N > 0)
0058 //      Note prefixN, textN have the same meaning as prefix and text for set N
0059 //           text0 is the text for general title added within ()
0060 //           prefix0 is the tag attached to the canvas name
0061 //           type has the same meaning as in PlotTwoHists
0062 //
0063 //  PlotHistCorrResults(infile, text, prefixF, save);
0064 //      Defaults: save=0
0065 //
0066 //             For plotting correction factors
0067 //  PlotHistCorrFactor(infile, text, prefixF, scale, nmin, isRealData,
0068 //                     drawStatBox, iformat, save);
0069 //      Defaults: isRealData=true, drwaStatBox=false, nmin=100, iformat=0,
0070 //                save=0
0071 //
0072 //             For plotting correction factors for a sigle depth
0073 //  PlotHistCorrFactor(infile, text, depth, prefixF, scale, nmin, isRealData,
0074 //                     drawStatBox, iformat, save);
0075 //      Defaults: isRealData=true, drwaStatBox=false, nmin=100, iformat=0,
0076 //                save=0
0077 //
0078 //             For plotting (fractional) asymmetry in the correction factors
0079 //
0080 //  PlotHistCorrAsymmetry(infile, text, prefixF, depth, iformat, save);
0081 //      Defaults: prefixF="", depth = -1, iformat=0, save=0
0082 //
0083 //             For plotting correction factors from upto 5 different runs
0084 //             on the same canvas
0085 //
0086 //  PlotHistCorrFactors(infile1, text1, infile2, text2, infile3, text3,
0087 //                      infile4, text4, infile5, text5, prefixF, ratio,
0088 //                      drawStatBox, nmin, isRealData, year, iformat, save)
0089 //      Defaults: ratio=false, drawStatBox=true, nmin=100, isRealData=false,
0090 //                year="2024", iformat=0, save=0
0091 //
0092 //  PlotHistCorr2Factors(infile1, text1, infile2, text2, depth, prefixF, ratio,
0093 //                      drawStatBox, nmin, isRealData, year, iformat, save)
0094 //      Defaults: ratio=true, drawStatBox=false, nmin=100, isRealData=true,
0095 //                year="2024", iformat=0, save=0
0096 //
0097 //  PlotHistCorrDFactors(infile1, text1, infile2, text2, infile3, text3,
0098 //                      infile4, text4, infile5, text5, depth, prefixF, ratio,
0099 //                      drawStatBox, nmin, isRealData, year, iformat, save)
0100 //      Defaults: ratio=true, drawStatBox=false, nmin=100, isRealData=true,
0101 //                year="2024", iformat=0, save=0
0102 //
0103 //             For plotting correction factors including systematics
0104 //  PlotHistCorrSys(infilec, conds, text, save)
0105 //      Defaults: save=0
0106 //
0107 //             For plotting uncertainties in correction factors with decreasing
0108 //             integrated luminpsoties starting from *lumi*
0109 //  PlotHistCorrLumis(infilec, conds, lumi, save)
0110 //      Defaults: save=0
0111 //
0112 //             For plotting correlation of correction factors
0113 //  PlotHistCorrRel(infile1, infile2, text1, text2, iformat1, iformat2, save)
0114 //      Defaults: iformat1=0, iformat2=0, save=0
0115 //
0116 //             For plotting difference of correction factors for a given depth
0117 //  PlotHistCorrDepth(infile1, infile2, text1, text2, depth, iformat1, iformat2,
0118 //                    save)
0119 //      Defaults: iformat1=0, iformat2=0, save=0
0120 //
0121 //             For plotting four histograms
0122 //  PlotFourHists(infile, prefix0, type, drawStatBox, normalize, save, prefix1,
0123 //                text1, prefix2, text2, prefix3, text3, prefix4, text4)
0124 //      Defaults: type=0, drawStatBox=0, normalize=false, save=0,
0125 //                prefixN="", textN=""
0126 //
0127 //            For plotting PU corrected histograms (o/p of CalibPlotCombine)
0128 //  PlotPUCorrHists(infile, prefix drawStatBox, approve, save)
0129 //      Defaults: infile = "corrfac.root", prefix = "", drawStatBox = 0,
0130 //                approve = true, save = 0
0131 //
0132 //             For plotting histograms obtained from fits to PU correction
0133 //             (o/p of CalibFitPU) for a given ieta using 2D/profile/Graphs
0134 //  PlotHistCorr(infile, prefix, text, eta, mode, drawStatBox, save)
0135 //      Defaults eta = 0 (all ieta values), mode = 1 (profile histograms),
0136 //               drawStatBox = true, save = 0
0137 //
0138 //             For plotting histograms created by CalibPlotProperties
0139 //  PlotPropertyHist(infile, prefix, text, etaMax, lumi, ener, isRealData,
0140 //           drawStatBox, save)
0141 //      Defaults etaMax = 25 (draws for eta = 1 .. etaMax), lumi = 0,
0142 //               ener = 13.0, isRealData = false,  drawStatBox = true, save = 0
0143 //
0144 //            For plotting mean response and resolution as a function of
0145 //            particle momentum
0146 //  PlotMeanError(infilest, region, resol, save, debug)
0147 //      Defaults region = 3 (overall), resol = false (response), save = 0,
0148 //               debug = false
0149 //      Format of the input file:
0150 //       # of energy points, # of types, # of regions
0151 //       Then for each type, energy point
0152 //         Type, lower and higher edge of momentum
0153 //         Mean response and its error for the 4 regions
0154 //         Width of response and uts error for the 4 regions
0155 //
0156 //            For plotting depth dependent correction factors from muon study
0157 //  PlotDepthCorrFactor(infile, text, prefix, isRealData, drawStatBox, save)
0158 //      Defaults prefix = "", isRealData = true, drawStatBox = true, save = 0
0159 //      Format for the input file: ieta and correcrion factor with its
0160 //             uncertainty for each depth
0161 //
0162 //            For plotting ratio of correction factors as defined in a file
0163 //            give by infileX for 2 depths (depth1, depth2) as a function of
0164 //            ieta obaned from 2 sources of data (defined by text1 and text2)
0165 //  PlotHistCorrRatio(infile1, text1, infile2, text2, depth1, depth2, prefix,
0166 //                    text0, etaMin, etaMax, doFit, isRealData, year, iformat,
0167 //                    save)
0168 //      Defaults etaMin = -1, etaMax = -1, doFit = true, isRealData = true,
0169 //               year = "2024", iformat = 0, save = 0
0170 //      text0 is a general description common to both sets of corr factors
0171 //      etaMin < 0 and etaMax > 0 will take ieta range from -etaMax to +etaMax;
0172 //      etaMin > 0 will select ieta's where |ieta| is greater than etaMin
0173 //      with the plot either between -etaMax to etaMax if etaMax > 0 otherwise
0174 //      determined from data files;
0175 //      doFit determines if a Pol0 fit is to be done
0176 //
0177 //  where:
0178 //  infile   (std::string)  = Name of the input ROOT file
0179 //  outfile  (std::string)  = Name of the output ROOT file
0180 //  prefix   (std::string)  = Prefix for the histogram names
0181 //  mode     (int)          = Flag to check which set of histograms to be
0182 //                            done. It has the format lthdo where each of
0183 //                            l, t,h,d,o can have a value 0 or 1 to select
0184 //                            or deselect. l,t,h,d,o for momentum range
0185 //                            60-100, 30-40, all, 20-30, 40-60 Gev (11111)
0186 //  type     (int)          = defines eta binning type (see CalibMonitor)
0187 //  append   (bool)         = Open the output in Update/Recreate mode (True)
0188 //  fiteta   (bool)         = fit the eta dependence with pol0
0189 //  iname    (int)          = choose the momentum bin (3: 40-60 GeV)
0190 //  saveAll  (bool)         = Flag to save intermediate plots (False)
0191 //  numb     (int)          = Number of eta bins (42 for -21:21)
0192 //  text     (std::string)  = Extra text to be put in the text title
0193 //  modePlot (int)          = Flag to plot E/p distribution (0);
0194 //                            <E/p> as a function of ieta (1);
0195 //                            <E/p> as a function of distance from L1 (2);
0196 //                            <E/p> as a function of number of vertex (3);
0197 //                            E/p for barrel, endcap and transition (4)
0198 //  kopt     (int)          = Option in format "hdo" where each of d, o can
0199 //                            have a value of 0 or 1 to select or deselect.
0200 //                            o>0 to carry out pol0 fit, o>1 to restrict
0201 //                            fit region between -20 & 20; d=1 to show grid;
0202 //                            h=0,1 to show plots with 2- or 1-Gaussian fit
0203 //  ieta     (int)          = specific ieta histogram to be plotted; if 0
0204 //                            histograms for all ieta's from -numb/2 to numb/2
0205 //                            will be plotted
0206 //  lumi     (double)       = Integrated luminosity of the dataset used which
0207 //                            needs to be drawn on the top of the canvas
0208 //                            along with CM energy (if lumi > 0)
0209 //  ener     (double)       = CM energy of the dataset used
0210 //  save     (int)          = if > 0 it saves the canvas as a pdf file; or
0211 //                            if < 0 it saves the canvas as a C file
0212 //  normalize(bool)         = if the histograms to be scaled to get
0213 //                            normalization to 1
0214 //  prefixF  (string)       = string to be included to the pad name
0215 //  infileN  (char*)        = name of the correction file and the corresponding
0216 //  textN    (string)         texts (if blank ignored)
0217 //  nmin     (int)          = minimum # of #ieta points needed to show the
0218 //                            fitted line
0219 //  scale    (double)       = constant scale factor applied to the factors
0220 //  ratio    (bool)         = set to show the ratio plot (false)
0221 //  drawStatBox (bool)      = set to show the statistical box (true)
0222 //  year     (char *)       = Year of data taking (applicable to Data)
0223 //  infilc   (string)       = prefix of the file names of correction factors
0224 //                            (assumes file name would be the prefix followed
0225 //                            by _condX.txt where X=0 for the default version
0226 //                            and 1..conds for the variations)
0227 //  conds    (int)          = number of variations in estimating systematic
0228 //                            checks
0229 //  infilest (string)       = input file name containing the responses and
0230 //                            resolutions for barrel, transition, endcap,
0231 //                            overall regions at 5 energies using 3 methods
0232 //  region   (int)         = region to be selected: 0 = barrel, 1 = transition,
0233 //                           2 = endcap, 3 = overall (3)
0234 //  resol    (bool)        = parameter to be plotted: true = resolution,
0235 //                           false = response (false)
0236 //  iformat  (int)         = flag if it is created by standard (0) or by
0237 //                           Marina's (1) code
0238 //////////////////////////////////////////////////////////////////////////////
0239 
0240 #include <TCanvas.h>
0241 #include <TChain.h>
0242 #include <TProfile.h>
0243 #include <TF1.h>
0244 #include <TFile.h>
0245 #include <TFitResult.h>
0246 #include <TFitResultPtr.h>
0247 #include <TH1D.h>
0248 #include <TLegend.h>
0249 #include <TLine.h>
0250 #include <TGraph.h>
0251 #include <TGraphErrors.h>
0252 #include <TGraphAsymmErrors.h>
0253 #include <TMath.h>
0254 #include <TPaveStats.h>
0255 #include <TPaveText.h>
0256 #include <TROOT.h>
0257 #include <TStyle.h>
0258 #include <cstdlib>
0259 #include <fstream>
0260 #include <iomanip>
0261 #include <iostream>
0262 #include <string>
0263 #include <vector>
0264 
0265 #include "CalibCorr.C"
0266 
0267 const double fitrangeFactor = 1.5;
0268 
0269 struct cfactors {
0270   int ieta, depth;
0271   double corrf, dcorr;
0272   cfactors(int ie = 0, int dp = 0, double cor = 1, double dc = 0) : ieta(ie), depth(dp), corrf(cor), dcorr(dc) {};
0273 };
0274 
0275 struct results {
0276   double mean, errmean, width, errwidth;
0277   results(double v1 = 0, double er1 = 0, double v2 = 0, double er2 = 0)
0278       : mean(v1), errmean(er1), width(v2), errwidth(er2) {};
0279 };
0280 
0281 std::pair<double, double> GetMean(TH1D* hist, double xmin, double xmax, double& rms) {
0282   double mean(0), err(0), wt(0);
0283   rms = 0;
0284   for (int i = 1; i <= hist->GetNbinsX(); ++i) {
0285     double xlow = hist->GetBinLowEdge(i);
0286     double xhigh = xlow + hist->GetBinWidth(i);
0287     if ((xlow >= xmin) && (xhigh <= xmax)) {
0288       double cont = hist->GetBinContent(i);
0289       double valu = 0.5 * (xlow + xhigh);
0290       wt += cont;
0291       mean += (valu * cont);
0292       rms += (valu * valu * cont);
0293     }
0294   }
0295   if (wt > 0) {
0296     mean /= wt;
0297     rms /= wt;
0298     err = std::sqrt((rms - mean * mean) / wt);
0299   }
0300   return std::pair<double, double>(mean, err);
0301 }
0302 
0303 std::pair<double, double> GetWidth(TH1D* hist, double xmin, double xmax) {
0304   double mean(0), mom2(0), rms(0), err(0), wt(0);
0305   for (int i = 1; i <= hist->GetNbinsX(); ++i) {
0306     double xlow = hist->GetBinLowEdge(i);
0307     double xhigh = xlow + hist->GetBinWidth(i);
0308     if ((xlow >= xmin) && (xhigh <= xmax)) {
0309       double cont = hist->GetBinContent(i);
0310       double valu = 0.5 * (xlow + xhigh);
0311       wt += cont;
0312       mean += (valu * cont);
0313       mom2 += (valu * valu * cont);
0314     }
0315   }
0316   if (wt > 0) {
0317     mean /= wt;
0318     mom2 /= wt;
0319     rms = std::sqrt(mom2 - mean * mean);
0320     err = rms / std::sqrt(2 * wt);
0321   }
0322   return std::pair<double, double>(rms, err);
0323 }
0324 
0325 Double_t langaufun(Double_t* x, Double_t* par) {
0326   //Fit parameters:
0327   //par[0]=Most Probable (MP, location) parameter of Landau density
0328   //par[1]=Total area (integral -inf to inf, normalization constant)
0329   //par[2]=Width (sigma) of convoluted Gaussian function
0330   //
0331   //In the Landau distribution (represented by the CERNLIB approximation),
0332   //the maximum is located at x=-0.22278298 with the location parameter=0.
0333   //This shift is corrected within this function, so that the actual
0334   //maximum is identical to the MP parameter.
0335 
0336   // Numeric constants
0337   Double_t invsq2pi = 0.3989422804014;  // (2 pi)^(-1/2)
0338   Double_t mpshift = -0.22278298;       // Landau maximum location
0339 
0340   // Control constants
0341   Double_t np = 100.0;  // number of convolution steps
0342   Double_t sc = 5.0;    // convolution extends to +-sc Gaussian sigmas
0343 
0344   // Variables
0345   Double_t xx;
0346   Double_t mpc;
0347   Double_t fland;
0348   Double_t sum = 0.0;
0349   Double_t xlow, xupp;
0350   Double_t step;
0351   Double_t i;
0352   Double_t par0 = 0.2;
0353 
0354   // MP shift correction
0355   mpc = par[0] - mpshift * par0 * par[0];
0356 
0357   // Range of convolution integral
0358   xlow = x[0] - sc * par[2];
0359   xupp = x[0] + sc * par[2];
0360 
0361   step = (xupp - xlow) / np;
0362 
0363   // Convolution integral of Landau and Gaussian by sum
0364   for (i = 1.0; i <= np / 2; i++) {
0365     xx = xlow + (i - .5) * step;
0366     fland = TMath::Landau(xx, mpc, par0 * par[0], kTRUE);  // / par[0];
0367     sum += fland * TMath::Gaus(x[0], xx, par[2]);
0368 
0369     xx = xupp - (i - .5) * step;
0370     fland = TMath::Landau(xx, mpc, par0 * par[0], kTRUE);  // / par[0];
0371     sum += fland * TMath::Gaus(x[0], xx, par[2]);
0372   }
0373 
0374   return (par[1] * step * sum * invsq2pi / par[2]);
0375 }
0376 
0377 Double_t doubleGauss(Double_t* x, Double_t* par) {
0378   double x1 = x[0] - par[1];
0379   double sig1 = par[2];
0380   double x2 = x[0] - par[4];
0381   double sig2 = par[5];
0382   double yval =
0383       (par[0] * std::exp(-0.5 * (x1 / sig1) * (x1 / sig1)) + par[3] * std::exp(-0.5 * (x2 / sig2) * (x2 / sig2)));
0384   return yval;
0385 }
0386 
0387 TFitResultPtr functionFit(TH1D* hist, double* fitrange, double* startvalues, double* parlimitslo, double* parlimitshi) {
0388   char FunName[100];
0389   sprintf(FunName, "Fitfcn_%s", hist->GetName());
0390   TF1* ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName);
0391   if (ffitold)
0392     delete ffitold;
0393 
0394   int npar(6);
0395   TObject* ob = gROOT->FindObject(FunName);
0396   if (ob)
0397     ob->Delete();
0398   TF1* ffit = new TF1(FunName, doubleGauss, fitrange[0], fitrange[1], npar);
0399   ffit->SetParameters(startvalues);
0400   ffit->SetLineColor(kBlue);
0401   ffit->SetParNames("Area1", "Mean1", "Width1", "Area2", "Mean2", "Width2");
0402   for (int i = 0; i < npar; i++)
0403     ffit->SetParLimits(i, parlimitslo[i], parlimitshi[i]);
0404   TFitResultPtr Fit = hist->Fit(FunName, "QRWLS");
0405   return Fit;
0406 }
0407 
0408 std::pair<double, double> fitLanGau(TH1D* hist, bool debug) {
0409   double rms;
0410   std::pair<double, double> mrms = GetMean(hist, 0.005, 0.25, rms);
0411   double mean = mrms.first;
0412   double LowEdge = 0.005, HighEdge = mean + 3 * rms;
0413   TFitResultPtr Fit1 = hist->Fit("gaus", "+0wwqRS", "", LowEdge, HighEdge);
0414   if (debug)
0415     std::cout << hist->GetName() << " 0 " << Fit1->Value(0) << " 1 " << Fit1->Value(1) << " 2 " << Fit1->Value(2)
0416               << std::endl;
0417   double startvalues[3];
0418   startvalues[0] = Fit1->Value(1);
0419   startvalues[1] = Fit1->Value(0);
0420   startvalues[2] = Fit1->Value(2);
0421 
0422   char FunName[100];
0423   sprintf(FunName, "Fitfcn_%s", hist->GetName());
0424   TF1* ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName);
0425   if (ffitold)
0426     delete ffitold;
0427 
0428   TObject* ob = gROOT->FindObject(FunName);
0429   if (ob)
0430     ob->Delete();
0431   TF1* ffit = new TF1(FunName, langaufun, LowEdge, HighEdge, 3);
0432   ffit->SetParameters(startvalues);
0433   ffit->SetParNames("MP", "Area", "GSigma");
0434   TFitResultPtr Fit2 = hist->Fit(FunName, "QRWLS");
0435   double value = Fit2->Value(0);
0436   double error = Fit2->FitResult::Error(0);
0437   if (debug)
0438     std::cout << hist->GetName() << " 0 " << Fit2->Value(0) << " 1 " << Fit2->Value(1) << " 2 " << Fit2->Value(2)
0439               << std::endl;
0440   return std::pair<double, double>(value, error);
0441 }
0442 
0443 results fitTwoGauss(TH1D* hist, bool debug) {
0444   double rms;
0445   std::pair<double, double> mrms = GetMean(hist, 0.2, 2.0, rms);
0446   double mean = mrms.first;
0447   double LowEdge = mean - fitrangeFactor * rms;
0448   double HighEdge = mean + fitrangeFactor * rms;
0449   if (LowEdge < 0.15)
0450     LowEdge = 0.15;
0451   std::string option = (hist->GetEntries() > 100) ? "QRS" : "QRWLS";
0452   TObject* ob = gROOT->FindObject("g1");
0453   if (ob)
0454     ob->Delete();
0455   TF1* g1 = new TF1("g1", "gaus", LowEdge, HighEdge);
0456   g1->SetLineColor(kGreen);
0457   TFitResultPtr Fit = hist->Fit(g1, option.c_str(), "");
0458 
0459   if (debug) {
0460     for (int k = 0; k < 3; ++k)
0461       std::cout << "Initial Parameter[" << k << "] = " << Fit->Value(k) << " +- " << Fit->FitResult::Error(k)
0462                 << std::endl;
0463   }
0464   double startvalues[6], fitrange[2], lowValue[6], highValue[6];
0465   startvalues[0] = Fit->Value(0);
0466   lowValue[0] = 0.5 * startvalues[0];
0467   highValue[0] = 2. * startvalues[0];
0468   startvalues[1] = Fit->Value(1);
0469   lowValue[1] = 0.5 * startvalues[1];
0470   highValue[1] = 2. * startvalues[1];
0471   startvalues[2] = Fit->Value(2);
0472   lowValue[2] = 0.5 * startvalues[2];
0473   highValue[2] = 2. * startvalues[2];
0474   startvalues[3] = 0.1 * Fit->Value(0);
0475   lowValue[3] = 0.0;
0476   highValue[3] = 10. * startvalues[3];
0477   startvalues[4] = Fit->Value(1);
0478   lowValue[4] = 0.5 * startvalues[4];
0479   highValue[4] = 2. * startvalues[4];
0480   startvalues[5] = 2.0 * Fit->Value(2);
0481   lowValue[5] = 0.5 * startvalues[5];
0482   highValue[5] = 100. * startvalues[5];
0483   //fitrange[0] = mean - 2.0*rms; fitrange[1] = mean + 2.0*rms;
0484   fitrange[0] = Fit->Value(1) - fitrangeFactor * Fit->Value(2);
0485   fitrange[1] = Fit->Value(1) + fitrangeFactor * Fit->Value(2);
0486   TFitResultPtr Fitfun = functionFit(hist, fitrange, startvalues, lowValue, highValue);
0487   double wt1 = (Fitfun->Value(0)) * (Fitfun->Value(2));
0488   double value1 = Fitfun->Value(1);
0489   double error1 = Fitfun->FitResult::Error(1);
0490   double wval1 = Fitfun->Value(2);
0491   double werr1 = Fitfun->FitResult::Error(2);
0492   double wt2 = (Fitfun->Value(3)) * (Fitfun->Value(5));
0493   double value2 = Fitfun->Value(4);
0494   double error2 = Fitfun->FitResult::Error(4);
0495   double wval2 = Fitfun->Value(5);
0496   double werr2 = Fitfun->FitResult::Error(5);
0497   double value = (wt1 * value1 + wt2 * value2) / (wt1 + wt2);
0498   double wval = (wt1 * wval1 + wt2 * wval2) / (wt1 + wt2);
0499   double error = (sqrt((wt1 * error1) * (wt1 * error1) + (wt2 * error2) * (wt2 * error2)) / (wt1 + wt2));
0500   double werror = (sqrt((wt1 * werr1) * (wt1 * werr1) + (wt2 * werr2) * (wt2 * werr2)) / (wt1 + wt2));
0501   std::cout << hist->GetName() << " Fit " << value << "+-" << error << " width " << wval << " +- " << werror
0502             << " First  " << value1 << "+-" << error1 << ":" << wval1 << "+-" << werr1 << ":" << wt1 << " Second "
0503             << value2 << "+-" << error2 << ":" << wval2 << "+-" << werr2 << ":" << wt2 << std::endl;
0504   if (debug) {
0505     for (int k = 0; k < 6; ++k)
0506       std::cout << hist->GetName() << ":Parameter[" << k << "] = " << Fitfun->Value(k) << " +- "
0507                 << Fitfun->FitResult::Error(k) << std::endl;
0508   }
0509   return results(value, error, wval, werror);
0510 }
0511 
0512 results fitOneGauss(TH1D* hist, bool fitTwice, bool debug) {
0513   double rms;
0514   std::pair<double, double> mrms = GetMean(hist, 0.2, 2.0, rms);
0515   double mean = mrms.first;
0516   double LowEdge = ((mean - fitrangeFactor * rms) < 0.5) ? 0.5 : (mean - fitrangeFactor * rms);
0517   double HighEdge = (mean + fitrangeFactor * rms);
0518   if (debug)
0519     std::cout << hist->GetName() << " Mean " << mean << " RMS " << rms << " Range " << LowEdge << ":" << HighEdge
0520               << "\n";
0521   std::string option = (hist->GetEntries() > 100) ? "QRS" : "QRWLS";
0522   TObject* ob = gROOT->FindObject("g1");
0523   if (ob)
0524     ob->Delete();
0525   TF1* g1 = new TF1("g1", "gaus", LowEdge, HighEdge);
0526   g1->SetLineColor(kGreen);
0527   TFitResultPtr Fit1 = hist->Fit(g1, option.c_str(), "");
0528   double value = Fit1->Value(1);
0529   double error = Fit1->FitResult::Error(1);
0530   double width = Fit1->Value(2);
0531   double werror = Fit1->FitResult::Error(2);
0532   if (fitTwice) {
0533     LowEdge = Fit1->Value(1) - fitrangeFactor * Fit1->Value(2);
0534     HighEdge = Fit1->Value(1) + fitrangeFactor * Fit1->Value(2);
0535     if (LowEdge < 0.5)
0536       LowEdge = 0.5;
0537     if (HighEdge > 5.0)
0538       HighEdge = 5.0;
0539     if (debug)
0540       std::cout << " Range for second Fit " << LowEdge << ":" << HighEdge << std::endl;
0541     TFitResultPtr Fit = hist->Fit("gaus", option.c_str(), "", LowEdge, HighEdge);
0542     value = Fit->Value(1);
0543     error = Fit->FitResult::Error(1);
0544     width = Fit->Value(2);
0545     werror = Fit->FitResult::Error(2);
0546   }
0547 
0548   std::pair<double, double> meaner = GetMean(hist, 0.2, 2.0, rms);
0549   std::pair<double, double> rmserr = GetWidth(hist, 0.2, 2.0);
0550   if (debug) {
0551     std::cout << "Fit " << value << ":" << error << ":" << hist->GetMeanError() << " Mean " << meaner.first << ":"
0552               << meaner.second << " Width " << rmserr.first << ":" << rmserr.second;
0553   }
0554   double minvalue(0.30);
0555   if (value < minvalue || value > 2.0 || error > 0.5) {
0556     value = meaner.first;
0557     error = meaner.second;
0558     width = rmserr.first;
0559     werror = rmserr.second;
0560   }
0561   if (debug) {
0562     std::cout << " Final " << value << "+-" << error << ":" << width << "+-" << werror << std::endl;
0563   }
0564   return results(value, error, width, werror);
0565 }
0566 
0567 void readCorrFactors(char* infile,
0568                      double scale,
0569                      std::map<int, cfactors>& cfacs,
0570                      int& etamin,
0571                      int& etamax,
0572                      int& maxdepth,
0573                      int iformat = 0,
0574                      bool debug = false) {
0575   cfacs.clear();
0576   std::ifstream fInput(infile);
0577   if (!fInput.good()) {
0578     std::cout << "Cannot open file " << infile << std::endl;
0579   } else {
0580     char buffer[1024];
0581     unsigned int all(0), good(0);
0582     while (fInput.getline(buffer, 1024)) {
0583       ++all;
0584       if (buffer[0] == '#')
0585         continue;  //ignore comment
0586       std::vector<std::string> items = splitString(std::string(buffer));
0587       if (items.size() != 5) {
0588         std::cout << "Ignore  line: " << buffer << std::endl;
0589       } else {
0590         ++good;
0591         int ieta = (iformat == 1) ? std::atoi(items[0].c_str()) : std::atoi(items[1].c_str());
0592         int depth = (iformat == 1) ? std::atoi(items[1].c_str()) : std::atoi(items[2].c_str());
0593         float corrf = std::atof(items[3].c_str());
0594         float dcorr = (iformat == 1) ? (0.02 * corrf) : std::atof(items[4].c_str());
0595         cfactors cfac(ieta, depth, scale * corrf, scale * dcorr);
0596         int detId = (iformat == 1) ? repackId(items[2], ieta, depth) : repackId(ieta, depth);
0597         cfacs[detId] = cfactors(ieta, depth, corrf, dcorr);
0598         if (ieta > etamax)
0599           etamax = ieta;
0600         if (ieta < etamin)
0601           etamin = ieta;
0602         if (depth > maxdepth)
0603           maxdepth = depth;
0604       }
0605     }
0606     fInput.close();
0607     std::cout << "Reads total of " << all << " and " << good << " good records"
0608               << " from " << infile << std::endl;
0609   }
0610   if (debug) {
0611     unsigned k(0);
0612     std::cout << "Eta Range " << etamin << ":" << etamax << " Max Depth " << maxdepth << std::endl;
0613     for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr, ++k)
0614       std::cout << "[" << k << "] " << std::hex << itr->first << std::dec << ": " << (itr->second).ieta << " "
0615                 << (itr->second).depth << " " << (itr->second).corrf << " " << (itr->second).dcorr << std::endl;
0616   }
0617 }
0618 
0619 void FitHistStandard(std::string infile,
0620                      std::string outfile,
0621                      std::string prefix,
0622                      int mode = 11111,
0623                      int type = 0,
0624                      bool append = true,
0625                      bool saveAll = false,
0626                      bool debug = false) {
0627   int iname[6] = {0, 1, 2, 3, 4, 5};
0628   int checkmode[6] = {1000, 10, 10000, 1, 100000, 100};
0629   double xbin0[9] = {-21.0, -16.0, -12.0, -6.0, 0.0, 6.0, 12.0, 16.0, 21.0};
0630   double xbins[11] = {-25.0, -20.0, -15.0, -10.0, -5.0, 0.0, 5.0, 10.0, 15.0, 20.0, 25.0};
0631   double vbins[6] = {0.0, 7.0, 10.0, 13.0, 16.0, 50.0};
0632   double dlbins[9] = {0.0, 0.10, 0.20, 0.50, 1.0, 2.0, 2.5, 3.0, 10.0};
0633   std::string sname[4] = {"ratio", "etaR", "dl1R", "nvxR"};
0634   std::string lname[4] = {"Z", "E", "L", "V"};
0635   std::string wname[4] = {"W", "F", "M", "X"};
0636   std::string xname[4] = {"i#eta", "i#eta", "d_{L1}", "# Vertex"};
0637   int numb[4] = {10, 8, 8, 5};
0638 
0639   if (type == 0) {
0640     numb[0] = 8;
0641     for (int i = 0; i < 9; ++i)
0642       xbins[i] = xbin0[i];
0643   }
0644   TFile* file = new TFile(infile.c_str());
0645   std::vector<TH1D*> hists;
0646   char name[100], namw[100];
0647   if (file != nullptr) {
0648     for (int m1 = 0; m1 < 4; ++m1) {
0649       for (int m2 = 0; m2 < 6; ++m2) {
0650         sprintf(name, "%s%s%d0", prefix.c_str(), sname[m1].c_str(), iname[m2]);
0651         TH1D* hist0 = (TH1D*)file->FindObjectAny(name);
0652         bool ok = ((hist0 != nullptr) && (hist0->GetEntries() > 25));
0653         if ((mode / checkmode[m2]) % 10 > 0 && ok) {
0654           TH1D *histo(0), *histw(0);
0655           sprintf(name, "%s%s%d", prefix.c_str(), lname[m1].c_str(), iname[m2]);
0656           sprintf(namw, "%s%s%d", prefix.c_str(), wname[m1].c_str(), iname[m2]);
0657           if (m1 <= 1) {
0658             histo = new TH1D(name, hist0->GetTitle(), numb[m1], xbins);
0659             histw = new TH1D(namw, hist0->GetTitle(), numb[m1], xbins);
0660           } else if (m1 == 2) {
0661             histo = new TH1D(name, hist0->GetTitle(), numb[m1], dlbins);
0662             histw = new TH1D(namw, hist0->GetTitle(), numb[m1], dlbins);
0663           } else {
0664             histo = new TH1D(name, hist0->GetTitle(), numb[m1], vbins);
0665             histw = new TH1D(namw, hist0->GetTitle(), numb[m1], vbins);
0666           }
0667           int jmin(numb[m1]), jmax(0);
0668           for (int j = 0; j <= numb[m1]; ++j) {
0669             sprintf(name, "%s%s%d%d", prefix.c_str(), sname[m1].c_str(), iname[m2], j);
0670             TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
0671             TH1D* hist = (TH1D*)hist1->Clone();
0672             double value(0), error(0), width(0), werror(0);
0673             if (hist->GetEntries() > 0) {
0674               value = hist->GetMean();
0675               error = hist->GetRMS();
0676               std::pair<double, double> rmserr = GetWidth(hist, 0.2, 2.0);
0677               width = rmserr.first;
0678               werror = rmserr.second;
0679             }
0680             if (hist->GetEntries() > 4) {
0681               bool flag = (j == 0) ? true : false;
0682               results meaner = fitOneGauss(hist, flag, debug);
0683               value = meaner.mean;
0684               error = meaner.errmean;
0685               width = meaner.width;
0686               werror = meaner.errwidth;
0687               if (j != 0) {
0688                 if (j < jmin)
0689                   jmin = j;
0690                 if (j > jmax)
0691                   jmax = j;
0692               }
0693             }
0694             if (j == 0) {
0695               hists.push_back(hist);
0696             } else {
0697               double wbyv = width / value;
0698               double wverr = wbyv * std::sqrt((werror * werror) / (width * width) + (error * error) / (value * value));
0699               if (saveAll)
0700                 hists.push_back(hist);
0701               histo->SetBinContent(j, value);
0702               histo->SetBinError(j, error);
0703               histw->SetBinContent(j, wbyv);
0704               histw->SetBinError(j, wverr);
0705             }
0706           }
0707           if (histo->GetEntries() > 2) {
0708             double LowEdge = histo->GetBinLowEdge(jmin);
0709             double HighEdge = histo->GetBinLowEdge(jmax) + histo->GetBinWidth(jmax);
0710             TFitResultPtr Fit = histo->Fit("pol0", "+QRWLS", "", LowEdge, HighEdge);
0711             if (debug) {
0712               std::cout << "Fit to Pol0: " << Fit->Value(0) << " +- " << Fit->FitResult::Error(0) << std::endl;
0713             }
0714             histo->GetXaxis()->SetTitle(xname[m1].c_str());
0715             histo->GetYaxis()->SetTitle("MPV(E_{HCAL}/(p-E_{ECAL}))");
0716             histo->GetYaxis()->SetRangeUser(0.4, 1.6);
0717             histw->GetXaxis()->SetTitle(xname[m1].c_str());
0718             histw->GetYaxis()->SetTitle("Width/MPV of (E_{HCAL}/(p-E_{ECAL}))");
0719             histw->GetYaxis()->SetRangeUser(0.0, 0.5);
0720           }
0721           hists.push_back(histo);
0722           hists.push_back(histw);
0723         }
0724       }
0725     }
0726     TFile* theFile(0);
0727     if (append) {
0728       theFile = new TFile(outfile.c_str(), "UPDATE");
0729     } else {
0730       theFile = new TFile(outfile.c_str(), "RECREATE");
0731     }
0732     theFile->cd();
0733     for (unsigned int i = 0; i < hists.size(); ++i) {
0734       TH1D* hnew = (TH1D*)hists[i]->Clone();
0735       hnew->Write();
0736     }
0737     theFile->Close();
0738     file->Close();
0739   }
0740 }
0741 
0742 void FitHistExtended(const char* infile,
0743                      const char* outfile,
0744                      std::string prefix,
0745                      int numb = 50,
0746                      int type = 3,
0747                      bool append = true,
0748                      bool fiteta = true,
0749                      int iname = 3,
0750                      bool debug = false) {
0751   std::string sname("ratio"), lname("Z"), wname("W"), ename("etaB");
0752   double xbins[99];
0753   double xbin[23] = {-23.0, -21.0, -19.0, -17.0, -15.0, -13.0, -11.0, -9.0, -7.0, -5.0, -3.0, 0.0,
0754                      3.0,   5.0,   7.0,   9.0,   11.0,  13.0,  15.0,  17.0, 19.0, 21.0, 23.0};
0755   if (type == 2) {
0756     numb = 22;
0757     for (int k = 0; k <= numb; ++k)
0758       xbins[k] = xbin[k];
0759   } else if (type == 1) {
0760     numb = 1;
0761     xbins[0] = -25;
0762     xbins[1] = 25;
0763   } else {
0764     int neta = numb / 2;
0765     for (int k = 0; k < neta; ++k) {
0766       xbins[k] = (k - neta) - 0.5;
0767       xbins[numb - k] = (neta - k) + 0.5;
0768     }
0769     xbins[neta] = 0;
0770   }
0771   if (debug) {
0772     for (int k = 0; k <= numb; ++k)
0773       std::cout << " " << xbins[k];
0774     std::cout << std::endl;
0775   }
0776   TFile* file = new TFile(infile);
0777   std::vector<TH1D*> hists;
0778   char name[200];
0779   if (debug) {
0780     std::cout << infile << " " << file << std::endl;
0781   }
0782   if (file != nullptr) {
0783     sprintf(name, "%s%s%d0", prefix.c_str(), sname.c_str(), iname);
0784     TH1D* hist0 = (TH1D*)file->FindObjectAny(name);
0785     bool ok = (hist0 != nullptr);
0786     if (debug) {
0787       std::cout << name << " Pointer " << hist0 << " " << ok << std::endl;
0788     }
0789     if (ok) {
0790       TH1D *histo(0), *histw(0);
0791       if (numb > 0) {
0792         sprintf(name, "%s%s%d", prefix.c_str(), lname.c_str(), iname);
0793         histo = new TH1D(name, hist0->GetTitle(), numb, xbins);
0794         sprintf(name, "%s%s%d", prefix.c_str(), wname.c_str(), iname);
0795         histw = new TH1D(name, hist0->GetTitle(), numb, xbins);
0796         if (debug)
0797           std::cout << name << " " << histo->GetNbinsX() << std::endl;
0798       }
0799       if (hist0->GetEntries() > 10) {
0800         double rms;
0801         results meaner0 = fitOneGauss(hist0, true, debug);
0802         std::pair<double, double> meaner1 = GetMean(hist0, 0.2, 2.0, rms);
0803         std::pair<double, double> meaner2 = GetWidth(hist0, 0.2, 2.0);
0804         if (debug) {
0805           std::cout << "Fit " << meaner0.mean << ":" << meaner0.errmean << " Mean1 " << hist0->GetMean() << ":"
0806                     << hist0->GetMeanError() << " Mean2 " << meaner1.first << ":" << meaner1.second << " Width "
0807                     << meaner2.first << ":" << meaner2.second << std::endl;
0808         }
0809       }
0810       int nv1(100), nv2(0);
0811       int jmin(numb), jmax(0);
0812       for (int j = 0; j <= numb; ++j) {
0813         sprintf(name, "%s%s%d%d", prefix.c_str(), sname.c_str(), iname, j);
0814         TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
0815         if (debug) {
0816           std::cout << "Get Histogram for " << name << " at " << hist1 << std::endl;
0817         }
0818         double value(0), error(0), total(0), width(0), werror(0);
0819         if (hist1 == nullptr) {
0820           value = 1.0;
0821         } else {
0822           TH1D* hist = (TH1D*)hist1->Clone();
0823           if (debug)
0824             std::cout << "Histogram " << name << ":" << (hist->GetName()) << " with " << (hist->GetEntries())
0825                       << " entries" << std::endl;
0826           if (hist->GetEntries() > 0) {
0827             value = hist->GetMean();
0828             error = hist->GetRMS();
0829             for (int i = 1; i <= hist->GetNbinsX(); ++i)
0830               total += hist->GetBinContent(i);
0831             std::pair<double, double> rmserr = GetWidth(hist, 0.2, 2.0);
0832             width = rmserr.first;
0833             werror = rmserr.second;
0834           }
0835           if (total > 4) {
0836             if (nv1 > j)
0837               nv1 = j;
0838             if (nv2 < j)
0839               nv2 = j;
0840             if (j == 0) {
0841               sprintf(name, "%sOne", hist1->GetName());
0842               TH1D* hist2 = (TH1D*)hist1->Clone(name);
0843               fitOneGauss(hist2, true, debug);
0844               hists.push_back(hist2);
0845               results meaner = fitOneGauss(hist, true, debug);
0846               value = meaner.mean;
0847               error = meaner.errmean;
0848               width = meaner.width;
0849               werror = meaner.errwidth;
0850             } else {
0851               results meaner = fitOneGauss(hist, true, debug);
0852               value = meaner.mean;
0853               error = meaner.errmean;
0854               width = meaner.width;
0855               werror = meaner.errwidth;
0856             }
0857             if (j != 0) {
0858               if (j < jmin)
0859                 jmin = j;
0860               if (j > jmax)
0861                 jmax = j;
0862             }
0863           }
0864           hists.push_back(hist);
0865         }
0866         if (debug) {
0867           std::cout << "Hist " << j << " Value " << value << " +- " << error << std::endl;
0868         }
0869         if (j != 0) {
0870           double wbyv = width / value;
0871           double wverr = wbyv * std::sqrt((werror * werror) / (width * width) + (error * error) / (value * value));
0872           histo->SetBinContent(j, value);
0873           histo->SetBinError(j, error);
0874           histw->SetBinContent(j, wbyv);
0875           histw->SetBinError(j, wverr);
0876         }
0877       }
0878       if (histo != nullptr) {
0879         if (histo->GetEntries() > 2 && fiteta) {
0880           if (debug) {
0881             std::cout << "Jmin/max " << jmin << ":" << jmax << ":" << histo->GetNbinsX() << std::endl;
0882           }
0883           double LowEdge = histo->GetBinLowEdge(jmin);
0884           double HighEdge = histo->GetBinLowEdge(jmax) + histo->GetBinWidth(jmax);
0885           TFitResultPtr Fit = histo->Fit("pol0", "+QRWLS", "", LowEdge, HighEdge);
0886           if (debug) {
0887             std::cout << "Fit to Pol0: " << Fit->Value(0) << " +- " << Fit->FitResult::Error(0) << " in range " << nv1
0888                       << ":" << xbins[nv1] << ":" << nv2 << ":" << xbins[nv2] << std::endl;
0889           }
0890           histo->GetXaxis()->SetTitle("i#eta");
0891           histo->GetYaxis()->SetTitle("MPV(E_{HCAL}/(p-E_{ECAL}))");
0892           histo->GetYaxis()->SetRangeUser(0.4, 1.6);
0893           histw->GetXaxis()->SetTitle("i#eta");
0894           histw->GetYaxis()->SetTitle("MPV/Width(E_{HCAL}/(p-E_{ECAL}))");
0895           histw->GetYaxis()->SetRangeUser(0.0, 0.5);
0896         }
0897         hists.push_back(histo);
0898         hists.push_back(histw);
0899       } else {
0900         hists.push_back(hist0);
0901       }
0902 
0903       //Barrel,Endcap
0904       for (int j = 1; j <= 4; ++j) {
0905         sprintf(name, "%s%s%d%d", prefix.c_str(), ename.c_str(), iname, j);
0906         TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
0907         if (debug) {
0908           std::cout << "Get Histogram for " << name << " at " << hist1 << std::endl;
0909         }
0910         if (hist1 != nullptr) {
0911           TH1D* hist = (TH1D*)hist1->Clone();
0912           double value(0), error(0), total(0), width(0), werror(0);
0913           if (hist->GetEntries() > 0) {
0914             value = hist->GetMean();
0915             error = hist->GetRMS();
0916             for (int i = 1; i <= hist->GetNbinsX(); ++i)
0917               total += hist->GetBinContent(i);
0918           }
0919           if (total > 4) {
0920             sprintf(name, "%sOne", hist1->GetName());
0921             TH1D* hist2 = (TH1D*)hist1->Clone(name);
0922             results meanerr = fitOneGauss(hist2, true, debug);
0923             value = meanerr.mean;
0924             error = meanerr.errmean;
0925             width = meanerr.width;
0926             werror = meanerr.errwidth;
0927             double wbyv = width / value;
0928             double wverr = wbyv * std::sqrt((werror * werror) / (width * width) + (error * error) / (value * value));
0929             std::cout << hist2->GetName() << " MPV " << value << " +- " << error << " Width " << width << " +- "
0930                       << werror << " W/M " << wbyv << " +- " << wverr << std::endl;
0931             hists.push_back(hist2);
0932             if (hist1->GetBinLowEdge(1) < 0.1) {
0933               sprintf(name, "%sTwo", hist1->GetName());
0934               TH1D* hist3 = (TH1D*)hist1->Clone(name);
0935               fitLanGau(hist3, debug);
0936               hists.push_back(hist3);
0937             }
0938             //            results meaner0 = fitTwoGauss(hist, debug);
0939             results meaner0 = fitOneGauss(hist, true, debug);
0940             value = meaner0.mean;
0941             error = meaner0.errmean;
0942             double rms;
0943             std::pair<double, double> meaner = GetMean(hist, 0.2, 2.0, rms);
0944             if (debug) {
0945               std::cout << "Fit " << value << ":" << error << ":" << hist->GetMeanError() << " Mean " << meaner.first
0946                         << ":" << meaner.second << std::endl;
0947             }
0948           }
0949           hists.push_back(hist);
0950         }
0951       }
0952     }
0953     TFile* theFile(0);
0954     if (append) {
0955       if (debug) {
0956         std::cout << "Open file " << outfile << " in append mode" << std::endl;
0957       }
0958       theFile = new TFile(outfile, "UPDATE");
0959     } else {
0960       if (debug) {
0961         std::cout << "Open file " << outfile << " in recreate mode" << std::endl;
0962       }
0963       theFile = new TFile(outfile, "RECREATE");
0964     }
0965 
0966     theFile->cd();
0967     for (unsigned int i = 0; i < hists.size(); ++i) {
0968       TH1D* hnew = (TH1D*)hists[i]->Clone();
0969       if (debug) {
0970         std::cout << "Write Histogram " << hnew->GetTitle() << std::endl;
0971       }
0972       hnew->Write();
0973     }
0974     theFile->Close();
0975     file->Close();
0976   }
0977 }
0978 
0979 void FitHistExtended2(const char* infile,
0980                       const char* outfile,
0981                       std::string prefix,
0982                       int numb = 50,
0983                       bool append = true,
0984                       int iname = 3,
0985                       bool debug = false) {
0986   std::string sname("ratio"), lname("Z"), wname("W"), ename("etaB");
0987   double xbins[99];
0988   int ixbin[99];
0989   int neta = numb / 2;
0990   for (int k = 0; k < neta; ++k) {
0991     xbins[k] = (k - neta) - 0.5;
0992     xbins[numb - k] = (neta - k) + 0.5;
0993     ixbin[k] = (k - neta);
0994     ixbin[numb - k] = (neta - k);
0995   }
0996   xbins[neta] = 0;
0997   ixbin[neta] = 0;
0998   TFile* file = new TFile(infile);
0999   std::vector<TH1D*> hists;
1000   char name[200];
1001   if (debug) {
1002     std::cout << infile << " " << file << std::endl;
1003   }
1004   if (file != nullptr) {
1005     sprintf(name, "%s%s%d0", prefix.c_str(), sname.c_str(), iname);
1006     TH1D* hist0 = (TH1D*)file->FindObjectAny(name);
1007     bool ok = (hist0 != nullptr);
1008     if (debug) {
1009       std::cout << name << " Pointer " << hist0 << " " << ok << std::endl;
1010     }
1011     if (ok) {
1012       TH1D *histo(0), *histw(0);
1013       if (numb > 0) {
1014         sprintf(name, "%s%s%d", prefix.c_str(), lname.c_str(), iname);
1015         histo = new TH1D(name, hist0->GetTitle(), numb, xbins);
1016         sprintf(name, "%s%s%d", prefix.c_str(), wname.c_str(), iname);
1017         histw = new TH1D(name, hist0->GetTitle(), numb, xbins);
1018         if (debug)
1019           std::cout << name << " " << histo->GetNbinsX() << std::endl;
1020       }
1021       int nv1(100), nv2(0);
1022       int jmin(numb), jmax(0);
1023       for (int j = 0; j <= numb; ++j) {
1024         sprintf(name, "%s%s%d%d", prefix.c_str(), sname.c_str(), iname, j);
1025         TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
1026         if (debug) {
1027           std::cout << "Get Histogram for " << name << " at " << hist1 << std::endl;
1028         }
1029         double value(0), error(0), total(0), width(0), werror(0);
1030         if (hist1 == nullptr) {
1031           value = 1.0;
1032         } else {
1033           TH1D* hist = (TH1D*)hist1->Clone();
1034           if (debug)
1035             std::cout << "Histogram " << name << ":" << (hist->GetName()) << " with " << (hist->GetEntries())
1036                       << " entries" << std::endl;
1037           if (hist->GetEntries() > 0) {
1038             value = hist->GetMean();
1039             error = hist->GetRMS();
1040             for (int i = 1; i <= hist->GetNbinsX(); ++i)
1041               total += hist->GetBinContent(i);
1042             std::pair<double, double> rmserr = GetWidth(hist, 0.2, 2.0);
1043             width = rmserr.first;
1044             werror = rmserr.second;
1045           }
1046           if (total > 4) {
1047             if (nv1 > j)
1048               nv1 = j;
1049             if (nv2 < j)
1050               nv2 = j;
1051             if (j == 0) {
1052               sprintf(name, "%sOne", hist1->GetName());
1053               TH1D* hist2 = (TH1D*)hist1->Clone(name);
1054               fitOneGauss(hist2, true, debug);
1055               hists.push_back(hist2);
1056               results meaner = fitOneGauss(hist, true, debug);
1057               value = meaner.mean;
1058               error = meaner.errmean;
1059               width = meaner.width;
1060               werror = meaner.errwidth;
1061             } else {
1062               results meaner = fitOneGauss(hist, true, debug);
1063               value = meaner.mean;
1064               error = meaner.errmean;
1065               width = meaner.width;
1066               werror = meaner.errwidth;
1067             }
1068             if (j != 0) {
1069               if (j < jmin)
1070                 jmin = j;
1071               if (j > jmax)
1072                 jmax = j;
1073             }
1074             double wbyv0 = width / value;
1075             double wverr0 = wbyv0 * std::sqrt((werror * werror) / (width * width) + (error * error) / (value * value));
1076             if (ixbin[j] != 0)
1077               std::cout << ixbin[j] << " MPV " << value << " +- " << error << " Width/MPV " << wbyv0 << " +- " << wverr0
1078                         << std::endl;
1079           }
1080           hists.push_back(hist);
1081         }
1082         if (debug) {
1083           std::cout << "Hist " << j << " Value " << value << " +- " << error << std::endl;
1084         }
1085         if (j != 0) {
1086           double wbyv = width / value;
1087           double wverr = wbyv * std::sqrt((werror * werror) / (width * width) + (error * error) / (value * value));
1088           histo->SetBinContent(j, value);
1089           histo->SetBinError(j, error);
1090           histw->SetBinContent(j, wbyv);
1091           histw->SetBinError(j, wverr);
1092         }
1093       }
1094       if (histo != nullptr) {
1095         if (histo->GetEntries() > 2) {
1096           if (debug) {
1097             std::cout << "Jmin/max " << jmin << ":" << jmax << ":" << histo->GetNbinsX() << std::endl;
1098           }
1099           double LowEdge = histo->GetBinLowEdge(jmin);
1100           double HighEdge = histo->GetBinLowEdge(jmax) + histo->GetBinWidth(jmax);
1101           TFitResultPtr Fit = histo->Fit("pol0", "+QRWLS", "", LowEdge, HighEdge);
1102           if (debug) {
1103             std::cout << "Fit to Pol0: " << Fit->Value(0) << " +- " << Fit->FitResult::Error(0) << " in range " << nv1
1104                       << ":" << xbins[nv1] << ":" << nv2 << ":" << xbins[nv2] << std::endl;
1105           }
1106           histo->GetXaxis()->SetTitle("i#eta");
1107           histo->GetYaxis()->SetTitle("MPV(E_{HCAL}/(p-E_{ECAL}))");
1108           histo->GetYaxis()->SetRangeUser(0.4, 1.6);
1109           histw->GetXaxis()->SetTitle("i#eta");
1110           histw->GetYaxis()->SetTitle("MPV/Width(E_{HCAL}/(p-E_{ECAL}))");
1111           histw->GetYaxis()->SetRangeUser(0.0, 0.5);
1112         }
1113         hists.push_back(histo);
1114         hists.push_back(histw);
1115       } else {
1116         hists.push_back(hist0);
1117       }
1118 
1119       //Barrel,Endcap
1120       for (int j = 1; j <= 4; ++j) {
1121         sprintf(name, "%s%s%d%d", prefix.c_str(), ename.c_str(), iname, j);
1122         TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
1123         if (debug) {
1124           std::cout << "Get Histogram for " << name << " at " << hist1 << std::endl;
1125         }
1126         if (hist1 != nullptr) {
1127           TH1D* hist = (TH1D*)hist1->Clone();
1128           double value(0), error(0), total(0), width(0), werror(0);
1129           if (hist->GetEntries() > 0) {
1130             value = hist->GetMean();
1131             error = hist->GetRMS();
1132             for (int i = 1; i <= hist->GetNbinsX(); ++i)
1133               total += hist->GetBinContent(i);
1134           }
1135           if (total > 4) {
1136             sprintf(name, "%sOne", hist1->GetName());
1137             TH1D* hist2 = (TH1D*)hist1->Clone(name);
1138             results meanerr = fitOneGauss(hist2, true, debug);
1139             value = meanerr.mean;
1140             error = meanerr.errmean;
1141             width = meanerr.width;
1142             werror = meanerr.errwidth;
1143             double wbyv = width / value;
1144             double wverr = wbyv * std::sqrt((werror * werror) / (width * width) + (error * error) / (value * value));
1145             std::cout << hist2->GetName() << " MPV " << value << " +- " << error << " Width " << width << " +- "
1146                       << werror << " W/M " << wbyv << " +- " << wverr << std::endl;
1147             hists.push_back(hist2);
1148             results meaner0 = fitOneGauss(hist, true, debug);
1149             value = meaner0.mean;
1150             error = meaner0.errmean;
1151             double rms;
1152             std::pair<double, double> meaner = GetMean(hist, 0.2, 2.0, rms);
1153             if (debug) {
1154               std::cout << "Fit " << value << ":" << error << ":" << hist->GetMeanError() << " Mean " << meaner.first
1155                         << ":" << meaner.second << std::endl;
1156             }
1157           }
1158           hists.push_back(hist);
1159         }
1160       }
1161     }
1162     TFile* theFile(0);
1163     if (append) {
1164       if (debug) {
1165         std::cout << "Open file " << outfile << " in append mode" << std::endl;
1166       }
1167       theFile = new TFile(outfile, "UPDATE");
1168     } else {
1169       if (debug) {
1170         std::cout << "Open file " << outfile << " in recreate mode" << std::endl;
1171       }
1172       theFile = new TFile(outfile, "RECREATE");
1173     }
1174 
1175     theFile->cd();
1176     for (unsigned int i = 0; i < hists.size(); ++i) {
1177       TH1D* hnew = (TH1D*)hists[i]->Clone();
1178       if (debug) {
1179         std::cout << "Write Histogram " << hnew->GetTitle() << std::endl;
1180       }
1181       hnew->Write();
1182     }
1183     theFile->Close();
1184     file->Close();
1185   }
1186 }
1187 
1188 void FitHistRBX(const char* infile, const char* outfile, std::string prefix, bool append = true, int iname = 3) {
1189   std::string sname("RBX"), lname("R");
1190   int numb(18);
1191   bool debug(false);
1192   char name[200];
1193 
1194   TFile* file = new TFile(infile);
1195   std::vector<TH1D*> hists;
1196   sprintf(name, "%s%s%d", prefix.c_str(), lname.c_str(), iname);
1197   TH1D* histo = new TH1D(name, "", numb, 0, numb);
1198   if (debug) {
1199     std::cout << infile << " " << file << std::endl;
1200   }
1201   if (file != nullptr) {
1202     for (int j = 0; j < numb; ++j) {
1203       sprintf(name, "%s%s%d%d", prefix.c_str(), sname.c_str(), iname, j + 1);
1204       TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
1205       if (debug) {
1206         std::cout << "Get Histogram for " << name << " at " << hist1 << std::endl;
1207       }
1208       TH1D* hist = (TH1D*)hist1->Clone();
1209       double value(0), error(0), total(0);
1210       if (hist->GetEntries() > 0) {
1211         value = hist->GetMean();
1212         error = hist->GetRMS();
1213         for (int i = 1; i <= hist->GetNbinsX(); ++i)
1214           total += hist->GetBinContent(i);
1215       }
1216       if (total > 4) {
1217         results meaner = fitOneGauss(hist, false, debug);
1218         value = meaner.mean;
1219         error = meaner.errmean;
1220       }
1221       hists.push_back(hist);
1222       histo->SetBinContent(j + 1, value);
1223       histo->SetBinError(j + 1, error);
1224     }
1225     histo->GetXaxis()->SetTitle("RBX #");
1226     histo->GetYaxis()->SetTitle("MPV(E_{HCAL}/(p-E_{ECAL}))");
1227     histo->GetYaxis()->SetRangeUser(0.75, 1.20);
1228     hists.push_back(histo);
1229 
1230     TFile* theFile(0);
1231     if (append) {
1232       if (debug) {
1233         std::cout << "Open file " << outfile << " in append mode" << std::endl;
1234       }
1235       theFile = new TFile(outfile, "UPDATE");
1236     } else {
1237       if (debug) {
1238         std::cout << "Open file " << outfile << " in recreate mode" << std::endl;
1239       }
1240       theFile = new TFile(outfile, "RECREATE");
1241     }
1242 
1243     theFile->cd();
1244     for (unsigned int i = 0; i < hists.size(); ++i) {
1245       TH1D* hnew = (TH1D*)hists[i]->Clone();
1246       if (debug) {
1247         std::cout << "Write Histogram " << hnew->GetTitle() << std::endl;
1248       }
1249       hnew->Write();
1250     }
1251     theFile->Close();
1252     file->Close();
1253   }
1254 }
1255 
1256 void PlotHist(const char* infile,
1257               std::string prefix,
1258               std::string text,
1259               int mode = 4,
1260               int kopt = 100,
1261               double lumi = 0,
1262               double ener = 13.0,
1263               bool isRealData = false,
1264               bool drawStatBox = true,
1265               int save = 0) {
1266   std::string name0[6] = {"ratio00", "ratio10", "ratio20", "ratio30", "ratio40", "ratio50"};
1267   std::string name1[5] = {"Z0", "Z1", "Z2", "Z3", "Z4"};
1268   std::string name2[5] = {"L0", "L1", "L2", "L3", "L4"};
1269   std::string name3[5] = {"V0", "V1", "V2", "V3", "V4"};
1270   std::string name4[20] = {"etaB41", "etaB42", "etaB43", "etaB44", "etaB31", "etaB32", "etaB33",
1271                            "etaB34", "etaB21", "etaB22", "etaB23", "etaB24", "etaB11", "etaB12",
1272                            "etaB13", "etaB14", "etaB01", "etaB02", "etaB03", "etaB04"};
1273   std::string name5[5] = {"W0", "W1", "W2", "W3", "W4"};
1274   std::string title[6] = {"Tracks with p = 10:20 GeV",
1275                           "Tracks with p = 20:30 GeV",
1276                           "Tracks with p = 30:40 GeV",
1277                           "Tracks with p = 40:60 GeV",
1278                           "Tracks with p = 60:100 GeV",
1279                           "Tracks with p = 20:100 GeV"};
1280   std::string title1[20] = {"Tracks with p = 60:100 GeV (Barrel)", "Tracks with p = 60:100 GeV (Transition)",
1281                             "Tracks with p = 60:100 GeV (Endcap)", "Tracks with p = 60:100 GeV",
1282                             "Tracks with p = 40:60 GeV (Barrel)",  "Tracks with p = 40:60 GeV (Transition)",
1283                             "Tracks with p = 40:60 GeV (Endcap)",  "Tracks with p = 40:60 GeV",
1284                             "Tracks with p = 30:40 GeV (Barrel)",  "Tracks with p = 30:40 GeV (Transition)",
1285                             "Tracks with p = 30:40 GeV (Endcap)",  "Tracks with p = 30:40 GeV",
1286                             "Tracks with p = 20:30 GeV (Barrel)",  "Tracks with p = 20:30 GeV (Transition)",
1287                             "Tracks with p = 20:30 GeV (Endcap)",  "Tracks with p = 20:30 GeV",
1288                             "Tracks with p = 10:20 GeV (Barrel)",  "Tracks with p = 10:20 GeV (Transition)",
1289                             "Tracks with p = 10:20 GeV (Endcap)",  "Tracks with p = 10:20 GeV"};
1290   std::string xtitl[5] = {"E_{HCAL}/(p-E_{ECAL})", "i#eta", "d_{L1}", "# Vertex", "E_{HCAL}/(p-E_{ECAL})"};
1291   std::string ytitl[5] = {
1292       "Tracks", "MPV(E_{HCAL}/(p-E_{ECAL}))", "MPV(E_{HCAL}/(p-E_{ECAL}))", "MPV(E_{HCAL}/(p-E_{ECAL}))", "Tracks"};
1293 
1294   gStyle->SetCanvasBorderMode(0);
1295   gStyle->SetCanvasColor(kWhite);
1296   gStyle->SetPadColor(kWhite);
1297   gStyle->SetFillColor(kWhite);
1298   gStyle->SetOptTitle(0);
1299   if (mode < 0 || mode > 5)
1300     mode = 0;
1301   if (drawStatBox) {
1302     int iopt(1110);
1303     if (mode != 0)
1304       iopt = 10;
1305     gStyle->SetOptStat(iopt);
1306     gStyle->SetOptFit(1);
1307   } else {
1308     gStyle->SetOptStat(0);
1309     gStyle->SetOptFit(0);
1310   }
1311   TFile* file = new TFile(infile);
1312   TLine* line(0);
1313   char name[100], namep[100];
1314   int kmax = (mode == 4) ? 20 : (((mode < 1) && (mode > 5)) ? 6 : 5);
1315   for (int k = 0; k < kmax; ++k) {
1316     if (mode == 1) {
1317       sprintf(name, "%s%s", prefix.c_str(), name1[k].c_str());
1318     } else if (mode == 2) {
1319       sprintf(name, "%s%s", prefix.c_str(), name2[k].c_str());
1320     } else if (mode == 3) {
1321       sprintf(name, "%s%s", prefix.c_str(), name3[k].c_str());
1322     } else if (mode == 4) {
1323       if ((kopt / 100) % 10 == 0) {
1324         sprintf(name, "%s%s", prefix.c_str(), name4[k].c_str());
1325       } else if ((kopt / 100) % 10 == 2) {
1326         sprintf(name, "%s%sTwo", prefix.c_str(), name4[k].c_str());
1327       } else {
1328         sprintf(name, "%s%sOne", prefix.c_str(), name4[k].c_str());
1329       }
1330     } else if (mode == 5) {
1331       sprintf(name, "%s%s", prefix.c_str(), name5[k].c_str());
1332     } else {
1333       if ((kopt / 100) % 10 == 0) {
1334         sprintf(name, "%s%s", prefix.c_str(), name0[k].c_str());
1335       } else if ((kopt / 100) % 10 == 2) {
1336         sprintf(name, "%s%sTwo", prefix.c_str(), name0[k].c_str());
1337       } else {
1338         sprintf(name, "%s%sOne", prefix.c_str(), name0[k].c_str());
1339       }
1340     }
1341     TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
1342     if (hist1 != nullptr) {
1343       TH1D* hist = (TH1D*)(hist1->Clone());
1344       double p0(1);
1345       double ymin(0.90);
1346       sprintf(namep, "c_%s", name);
1347       TCanvas* pad = new TCanvas(namep, namep, 700, 500);
1348       pad->SetRightMargin(0.10);
1349       pad->SetTopMargin(0.10);
1350       if ((kopt / 10) % 10 > 0)
1351         gPad->SetGrid();
1352       hist->GetXaxis()->SetTitleSize(0.04);
1353       hist->GetXaxis()->SetTitle(xtitl[mode].c_str());
1354       hist->GetYaxis()->SetTitle(ytitl[mode].c_str());
1355       hist->GetYaxis()->SetLabelOffset(0.005);
1356       hist->GetYaxis()->SetTitleSize(0.04);
1357       hist->GetYaxis()->SetLabelSize(0.035);
1358       hist->GetYaxis()->SetTitleOffset(1.10);
1359       if (mode == 0 || mode == 4) {
1360         if ((kopt / 100) % 10 == 2)
1361           hist->GetXaxis()->SetRangeUser(0.0, 0.30);
1362         else
1363           hist->GetXaxis()->SetRangeUser(0.25, 2.25);
1364       } else {
1365         if (mode == 5)
1366           hist->GetYaxis()->SetRangeUser(0.1, 0.50);
1367         else if (isRealData)
1368           hist->GetYaxis()->SetRangeUser(0.5, 1.50);
1369         else
1370           hist->GetYaxis()->SetRangeUser(0.8, 1.20);
1371         if (kopt % 10 > 0) {
1372           int nbin = hist->GetNbinsX();
1373           double LowEdge = (kopt % 10 == 1) ? hist->GetBinLowEdge(1) : -20;
1374           double HighEdge = (kopt % 10 == 1) ? hist->GetBinLowEdge(nbin) + hist->GetBinWidth(nbin) : 20;
1375           TFitResultPtr Fit = hist->Fit("pol0", "+QRWLS", "", LowEdge, HighEdge);
1376           p0 = Fit->Value(0);
1377         }
1378       }
1379       hist->SetMarkerStyle(20);
1380       hist->SetMarkerColor(2);
1381       hist->SetLineColor(2);
1382       hist->Draw();
1383       pad->Update();
1384       TPaveStats* st1 = (TPaveStats*)hist->GetListOfFunctions()->FindObject("stats");
1385       if (st1 != nullptr) {
1386         ymin = (mode == 0 || mode == 4) ? 0.70 : 0.80;
1387         st1->SetY1NDC(ymin);
1388         st1->SetY2NDC(0.90);
1389         st1->SetX1NDC(0.65);
1390         st1->SetX2NDC(0.90);
1391       }
1392       if (mode != 0 && mode != 4 && kopt % 10 > 0) {
1393         double xmin = hist->GetBinLowEdge(1);
1394         int nbin = hist->GetNbinsX();
1395         double xmax = hist->GetBinLowEdge(nbin) + hist->GetBinWidth(nbin);
1396         double mean(0), rms(0), total(0);
1397         int kount(0);
1398         for (int k = 2; k < nbin; ++k) {
1399           double x = hist->GetBinContent(k);
1400           double w = hist->GetBinError(k);
1401           mean += (x * w);
1402           rms += (x * x * w);
1403           total += w;
1404           ++kount;
1405         }
1406         mean /= total;
1407         rms /= total;
1408         double error = sqrt(rms - mean * mean) / total;
1409         line = new TLine(xmin, p0, xmax, p0);  //etamin,1.0,etamax,1.0);
1410         std::cout << xmin << ":" << xmax << ":" << p0 << " Mean " << nbin << ":" << kount << ":" << total << ":" << mean
1411                   << ":" << rms << ":" << error << std::endl;
1412         line->SetLineWidth(2);
1413         line->SetLineStyle(2);
1414         line->Draw("same");
1415       }
1416       pad->Update();
1417       double ymx(0.96), xmi(0.25), xmx(0.90);
1418       char txt[100];
1419       if (lumi > 0.1) {
1420         ymx = ymin - 0.005;
1421         xmi = 0.45;
1422         TPaveText* txt0 = new TPaveText(0.65, 0.91, 0.90, 0.96, "blNDC");
1423         txt0->SetFillColor(0);
1424         sprintf(txt, "%4.1f TeV %5.1f fb^{-1}", ener, lumi);
1425         txt0->AddText(txt);
1426         txt0->Draw("same");
1427       }
1428       double ymi = ymx - 0.05;
1429       TPaveText* txt1 = new TPaveText(xmi, ymi, xmx, ymx, "blNDC");
1430       txt1->SetFillColor(0);
1431       if (text == "") {
1432         if (mode == 4)
1433           sprintf(txt, "%s", title1[k].c_str());
1434         else
1435           sprintf(txt, "%s", title[k].c_str());
1436       } else {
1437         if (mode == 4)
1438           sprintf(txt, "%s (%s)", title1[k].c_str(), text.c_str());
1439         else
1440           sprintf(txt, "%s (%s)", title[k].c_str(), text.c_str());
1441       }
1442       txt1->AddText(txt);
1443       txt1->Draw("same");
1444       double xmax = (isRealData) ? 0.33 : 0.44;
1445       ymi = (lumi > 0.1) ? 0.91 : 0.84;
1446       ymx = ymi + 0.05;
1447       TPaveText* txt2 = new TPaveText(0.11, ymi, xmax, ymx, "blNDC");
1448       txt2->SetFillColor(0);
1449       if (isRealData)
1450         sprintf(txt, "CMS Preliminary");
1451       else
1452         sprintf(txt, "CMS Simulation Preliminary");
1453       txt2->AddText(txt);
1454       txt2->Draw("same");
1455       pad->Modified();
1456       pad->Update();
1457       if (save > 0) {
1458         sprintf(name, "%s.pdf", pad->GetName());
1459         pad->Print(name);
1460       } else if (save < 0) {
1461         sprintf(name, "%s.C", pad->GetName());
1462         pad->Print(name);
1463       }
1464     }
1465   }
1466 }
1467 
1468 void PlotHistEta(const char* infile,
1469                  std::string prefix,
1470                  std::string text,
1471                  int iene = 3,
1472                  int numb = 50,
1473                  int ieta = 0,
1474                  double lumi = 0,
1475                  double ener = 13.0,
1476                  bool isRealData = false,
1477                  bool drawStatBox = true,
1478                  int save = 0) {
1479   std::string name0 = "ratio";
1480   std::string title[5] = {"10:20", "20:30", "30:40", "40:60", "60:100"};
1481   std::string xtitl = "E_{HCAL}/(p-E_{ECAL})";
1482   std::string ytitl = "Tracks";
1483 
1484   gStyle->SetCanvasBorderMode(0);
1485   gStyle->SetCanvasColor(kWhite);
1486   gStyle->SetPadColor(kWhite);
1487   gStyle->SetFillColor(kWhite);
1488   gStyle->SetOptTitle(0);
1489   if (drawStatBox) {
1490     int iopt(1110);
1491     gStyle->SetOptStat(iopt);
1492     gStyle->SetOptFit(1);
1493   } else {
1494     gStyle->SetOptStat(0);
1495     gStyle->SetOptFit(0);
1496   }
1497   if (iene < 0 || iene >= 5)
1498     iene = 3;
1499   int numb2 = numb / 2;
1500   if (ieta < -numb2 || ieta > numb2)
1501     ieta = 0;
1502   int ietaMin = ((ieta == 0) ? 1 : ((ieta > 0) ? (numb2 + ieta) : (numb2 + ieta + 1)));
1503   int ietaMax = (ieta == 0) ? numb : ietaMin;
1504   TFile* file = new TFile(infile);
1505   char name[100], namep[100];
1506   for (int k = ietaMin; k <= ietaMax; ++k) {
1507     int eta = (k > numb2) ? (k - numb2) : (k - numb2 - 1);
1508     sprintf(name, "%s%s%d%d", prefix.c_str(), name0.c_str(), iene, k);
1509     TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
1510     std::cout << name << " at " << hist1 << std::endl;
1511     if (hist1 != nullptr) {
1512       TH1D* hist = (TH1D*)(hist1->Clone());
1513       double ymin(0.90);
1514       sprintf(namep, "c_%s", name);
1515       TCanvas* pad = new TCanvas(namep, namep, 700, 500);
1516       pad->SetRightMargin(0.10);
1517       pad->SetTopMargin(0.10);
1518       hist->GetXaxis()->SetTitleSize(0.04);
1519       hist->GetXaxis()->SetTitle(xtitl.c_str());
1520       hist->GetYaxis()->SetTitle(ytitl.c_str());
1521       hist->GetYaxis()->SetLabelOffset(0.005);
1522       hist->GetYaxis()->SetTitleSize(0.04);
1523       hist->GetYaxis()->SetLabelSize(0.035);
1524       hist->GetYaxis()->SetTitleOffset(1.10);
1525       hist->GetXaxis()->SetRangeUser(0.25, 2.25);
1526       hist->SetMarkerStyle(20);
1527       hist->SetMarkerColor(2);
1528       hist->SetLineColor(2);
1529       hist->Draw();
1530       pad->Update();
1531       TPaveStats* st1 = (TPaveStats*)hist->GetListOfFunctions()->FindObject("stats");
1532       if (st1 != nullptr) {
1533         ymin = 0.70;
1534         st1->SetY1NDC(ymin);
1535         st1->SetY2NDC(0.90);
1536         st1->SetX1NDC(0.65);
1537         st1->SetX2NDC(0.90);
1538       }
1539       double ymx(0.96), xmi(0.25), xmx(0.90);
1540       char txt[100];
1541       if (lumi > 0.1) {
1542         ymx = ymin - 0.005;
1543         xmi = 0.45;
1544         TPaveText* txt0 = new TPaveText(0.65, 0.91, 0.90, 0.96, "blNDC");
1545         txt0->SetFillColor(0);
1546         sprintf(txt, "%4.1f TeV %5.1f fb^{-1}", ener, lumi);
1547         txt0->AddText(txt);
1548         txt0->Draw("same");
1549       }
1550       double ymi = ymx - 0.05;
1551       TPaveText* txt1 = new TPaveText(xmi, ymi, xmx, ymx, "blNDC");
1552       txt1->SetFillColor(0);
1553       if (text == "") {
1554         sprintf(txt, "Tracks with p = %s GeV at i#eta = %d", title[iene].c_str(), eta);
1555       } else {
1556         sprintf(txt, "Tracks with p = %s GeV at i#eta = %d (%s)", title[iene].c_str(), eta, text.c_str());
1557       }
1558       txt1->AddText(txt);
1559       txt1->Draw("same");
1560       double xmax = (isRealData) ? 0.33 : 0.44;
1561       ymi = (lumi > 0.1) ? 0.91 : 0.84;
1562       ymx = ymi + 0.05;
1563       TPaveText* txt2 = new TPaveText(0.11, ymi, xmax, ymx, "blNDC");
1564       txt2->SetFillColor(0);
1565       if (isRealData)
1566         sprintf(txt, "CMS Preliminary");
1567       else
1568         sprintf(txt, "CMS Simulation Preliminary");
1569       txt2->AddText(txt);
1570       txt2->Draw("same");
1571       pad->Modified();
1572       pad->Update();
1573       if (save > 0) {
1574         sprintf(name, "%s.pdf", pad->GetName());
1575         pad->Print(name);
1576       } else if (save < 0) {
1577         sprintf(name, "%s.C", pad->GetName());
1578         pad->Print(name);
1579       }
1580     }
1581   }
1582 }
1583 
1584 void PlotHists(std::string infile, std::string prefix, std::string text, bool drawStatBox = true, int save = 0) {
1585   int colors[6] = {1, 6, 4, 7, 2, 9};
1586   std::string types[6] = {"B", "C", "D", "E", "F", "G"};
1587   std::string names[3] = {"ratio20", "Z2", "W2"};
1588   std::string xtitl[3] = {"E_{HCAL}/(p-E_{ECAL})", "i#eta", "i#eta"};
1589   std::string ytitl[3] = {"Tracks", "MPV(E_{HCAL}/(p-E_{ECAL}))", "MPV/Width(E_{HCAL}/(p-E_{ECAL}))"};
1590 
1591   gStyle->SetCanvasBorderMode(0);
1592   gStyle->SetCanvasColor(kWhite);
1593   gStyle->SetPadColor(kWhite);
1594   gStyle->SetFillColor(kWhite);
1595   gStyle->SetOptTitle(0);
1596   if (drawStatBox)
1597     gStyle->SetOptFit(10);
1598   else
1599     gStyle->SetOptFit(0);
1600 
1601   char name[100], namep[100];
1602   TFile* file = new TFile(infile.c_str());
1603   for (int i = 0; i < 3; ++i) {
1604     std::vector<TH1D*> hists;
1605     std::vector<int> kks;
1606     double ymax(0.77);
1607     if (drawStatBox) {
1608       if (i == 0)
1609         gStyle->SetOptStat(1100);
1610       else
1611         gStyle->SetOptStat(10);
1612     } else {
1613       gStyle->SetOptStat(0);
1614       ymax = 0.89;
1615     }
1616     for (int k = 0; k < 6; ++k) {
1617       sprintf(name, "%s%s%s", prefix.c_str(), types[k].c_str(), names[i].c_str());
1618       TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
1619       if (hist1 != nullptr) {
1620         hists.push_back((TH1D*)(hist1->Clone()));
1621         kks.push_back(k);
1622       }
1623     }
1624     if (hists.size() > 0) {
1625       sprintf(namep, "c_%s%s", prefix.c_str(), names[i].c_str());
1626       TCanvas* pad = new TCanvas(namep, namep, 700, 500);
1627       TLegend* legend = new TLegend(0.44, 0.89 - 0.055 * hists.size(), 0.69, ymax);
1628       legend->SetFillColor(kWhite);
1629       pad->SetRightMargin(0.10);
1630       pad->SetTopMargin(0.10);
1631       double ymax(0.90);
1632       double dy = (i == 0) ? 0.13 : 0.08;
1633       for (unsigned int jk = 0; jk < hists.size(); ++jk) {
1634         int k = kks[jk];
1635         hists[jk]->GetXaxis()->SetTitle(xtitl[i].c_str());
1636         hists[jk]->GetYaxis()->SetTitle(ytitl[i].c_str());
1637         hists[jk]->GetYaxis()->SetLabelOffset(0.005);
1638         hists[jk]->GetYaxis()->SetLabelSize(0.035);
1639         hists[jk]->GetYaxis()->SetTitleOffset(1.15);
1640         if (i == 0) {
1641           hists[jk]->GetXaxis()->SetRangeUser(0.0, 2.5);
1642         } else if (i == 1) {
1643           hists[jk]->GetYaxis()->SetRangeUser(0.5, 2.0);
1644         } else {
1645           hists[jk]->GetYaxis()->SetRangeUser(0.0, 0.5);
1646         }
1647         hists[jk]->SetMarkerStyle(20);
1648         hists[jk]->SetMarkerColor(colors[k]);
1649         hists[jk]->SetLineColor(colors[k]);
1650         if (jk == 0)
1651           hists[jk]->Draw();
1652         else
1653           hists[jk]->Draw("sames");
1654         pad->Update();
1655         TPaveStats* st1 = (TPaveStats*)hists[jk]->GetListOfFunctions()->FindObject("stats");
1656         if (st1 != nullptr) {
1657           double ymin = ymax - dy;
1658           st1->SetLineColor(colors[k]);
1659           st1->SetTextColor(colors[k]);
1660           st1->SetY1NDC(ymin);
1661           st1->SetY2NDC(ymax);
1662           st1->SetX1NDC(0.70);
1663           st1->SetX2NDC(0.90);
1664           ymax = ymin;
1665         }
1666         sprintf(name, "%s%s", text.c_str(), types[k].c_str());
1667         legend->AddEntry(hists[jk], name, "lp");
1668       }
1669       legend->Draw("same");
1670       pad->Update();
1671       TPaveText* txt1 = new TPaveText(0.34, 0.825, 0.69, 0.895, "blNDC");
1672       txt1->SetFillColor(0);
1673       char txt[100];
1674       sprintf(txt, "Tracks with p = 40:60 GeV");
1675       txt1->AddText(txt);
1676       txt1->Draw("same");
1677       TPaveText* txt2 = new TPaveText(0.11, 0.825, 0.33, 0.895, "blNDC");
1678       txt2->SetFillColor(0);
1679       sprintf(txt, "CMS Preliminary");
1680       txt2->AddText(txt);
1681       txt2->Draw("same");
1682       if (!drawStatBox && i == 1) {
1683         double xmin = hists[0]->GetBinLowEdge(1);
1684         int nbin = hists[0]->GetNbinsX();
1685         double xmax = hists[0]->GetBinLowEdge(nbin) + hists[0]->GetBinWidth(nbin);
1686         TLine line = TLine(xmin, 1.0, xmax, 1.0);  //etamin,1.0,etamax,1.0);
1687         line.SetLineWidth(4);
1688         line.Draw("same");
1689         pad->Update();
1690       }
1691       pad->Modified();
1692       pad->Update();
1693       if (save > 0) {
1694         sprintf(name, "%s.pdf", pad->GetName());
1695         pad->Print(name);
1696       } else if (save < 0) {
1697         sprintf(name, "%s.C", pad->GetName());
1698         pad->Print(name);
1699       }
1700     }
1701   }
1702 }
1703 
1704 void PlotTwoHists(std::string infile,
1705                   std::string prefix1,
1706                   std::string text1,
1707                   std::string prefix2,
1708                   std::string text2,
1709                   std::string text0,
1710                   int type = 0,
1711                   int iname = 3,
1712                   double lumi = 0,
1713                   double ener = 13.0,
1714                   int drawStatBox = 0,
1715                   int save = 0) {
1716   int colors[2] = {2, 4};
1717   int numb[2] = {5, 1};
1718   std::string names0[5] = {"ratio00", "ratio00One", "etaB04One", "Z0", "W0"};
1719   std::string names1[5] = {"ratio10", "ratio10One", "etaB14One", "Z1", "W1"};
1720   std::string names2[5] = {"ratio30", "ratio30One", "etaB34One", "Z3", "W3"};
1721   std::string xtitl1[5] = {"E_{HCAL}/(p-E_{ECAL})", "E_{HCAL}/(p-E_{ECAL})", "E_{HCAL}/(p-E_{ECAL})", "i#eta", "i#eta"};
1722   std::string ytitl1[5] = {
1723       "Tracks", "Tracks", "Tracks", "MPV(E_{HCAL}/(p-E_{ECAL}))", "MPV/Width(E_{HCAL}/(p-E_{ECAL}))"};
1724   std::string names3[1] = {"R"};
1725   std::string xtitl2[1] = {"RBX #"};
1726   std::string ytitl2[1] = {"MPV(E_{HCAL}/(p-E_{ECAL}))"};
1727 
1728   gStyle->SetCanvasBorderMode(0);
1729   gStyle->SetCanvasColor(kWhite);
1730   gStyle->SetPadColor(kWhite);
1731   gStyle->SetFillColor(kWhite);
1732   gStyle->SetOptTitle(0);
1733   if ((drawStatBox / 10) % 10 > 0)
1734     gStyle->SetOptFit(10);
1735   else
1736     gStyle->SetOptFit(0);
1737 
1738   if (type != 1)
1739     type = 0;
1740   char name[100], namep[100];
1741   TFile* file = new TFile(infile.c_str());
1742   for (int i = 0; i < numb[type]; ++i) {
1743     std::vector<TH1D*> hists;
1744     std::vector<int> kks;
1745     double ymax(0.77);
1746     if (drawStatBox % 10 > 0) {
1747       if (i != 2)
1748         gStyle->SetOptStat(1100);
1749       else
1750         gStyle->SetOptStat(10);
1751     } else {
1752       gStyle->SetOptStat(0);
1753       ymax = 0.82;
1754     }
1755     for (int k = 0; k < 2; ++k) {
1756       std::string prefix = (k == 0) ? prefix1 : prefix2;
1757       if (type == 0) {
1758         if (iname == 0)
1759           sprintf(name, "%s%s", prefix.c_str(), names0[i].c_str());
1760         else if (iname == 1)
1761           sprintf(name, "%s%s", prefix.c_str(), names1[i].c_str());
1762         else
1763           sprintf(name, "%s%s", prefix.c_str(), names2[i].c_str());
1764       } else {
1765         sprintf(name, "%s%s%d", prefix.c_str(), names3[i].c_str(), iname);
1766       }
1767       TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
1768       if (hist1 != nullptr) {
1769         hists.push_back((TH1D*)(hist1->Clone()));
1770         kks.push_back(k);
1771       }
1772     }
1773     if (hists.size() == 2) {
1774       if (type == 0) {
1775         if (iname == 0)
1776           sprintf(namep, "c_%s%s%s", prefix1.c_str(), prefix2.c_str(), names0[i].c_str());
1777         else if (iname == 1)
1778           sprintf(namep, "c_%s%s%s", prefix1.c_str(), prefix2.c_str(), names1[i].c_str());
1779         else
1780           sprintf(namep, "c_%s%s%s", prefix1.c_str(), prefix2.c_str(), names2[i].c_str());
1781       } else {
1782         sprintf(namep, "c_%s%s%s%d", prefix1.c_str(), prefix2.c_str(), names3[i].c_str(), iname);
1783       }
1784       double ymax(0.90);
1785       double dy = (i == 0) ? 0.13 : 0.08;
1786       double ymx0 = (drawStatBox == 0) ? (ymax - .01) : (ymax - dy * hists.size() - .01);
1787       TCanvas* pad = new TCanvas(namep, namep, 700, 500);
1788       TLegend* legend = new TLegend(0.64, ymx0 - 0.05 * hists.size(), 0.89, ymx0);
1789       legend->SetFillColor(kWhite);
1790       pad->SetRightMargin(0.10);
1791       pad->SetTopMargin(0.10);
1792       for (unsigned int jk = 0; jk < hists.size(); ++jk) {
1793         int k = kks[jk];
1794         hists[jk]->GetXaxis()->SetTitleSize(0.040);
1795         if (type == 0) {
1796           hists[jk]->GetXaxis()->SetTitle(xtitl1[i].c_str());
1797           hists[jk]->GetYaxis()->SetTitle(ytitl1[i].c_str());
1798         } else {
1799           hists[jk]->GetXaxis()->SetTitle(xtitl2[i].c_str());
1800           hists[jk]->GetYaxis()->SetTitle(ytitl2[i].c_str());
1801         }
1802         hists[jk]->GetYaxis()->SetLabelOffset(0.005);
1803         hists[jk]->GetYaxis()->SetLabelSize(0.035);
1804         hists[jk]->GetYaxis()->SetTitleSize(0.040);
1805         hists[jk]->GetYaxis()->SetTitleOffset(1.15);
1806         if ((type == 0) && (i != 3) && (i != 4))
1807           hists[jk]->GetXaxis()->SetRangeUser(0.0, 5.0);
1808         if (type == 0) {
1809           if (i == 3)
1810             hists[jk]->GetYaxis()->SetRangeUser(0.8, 1.2);
1811           else if (i == 4)
1812             hists[jk]->GetYaxis()->SetRangeUser(0.0, 0.5);
1813         }
1814         if (type != 0)
1815           hists[jk]->GetYaxis()->SetRangeUser(0.75, 1.2);
1816         hists[jk]->SetMarkerStyle(20);
1817         hists[jk]->SetMarkerColor(colors[k]);
1818         hists[jk]->SetLineColor(colors[k]);
1819         if (jk == 0)
1820           hists[jk]->Draw();
1821         else
1822           hists[jk]->Draw("sames");
1823         pad->Update();
1824         TPaveStats* st1 = (TPaveStats*)hists[jk]->GetListOfFunctions()->FindObject("stats");
1825         if (st1 != nullptr) {
1826           double ymin = ymax - dy;
1827           st1->SetLineColor(colors[k]);
1828           st1->SetTextColor(colors[k]);
1829           st1->SetY1NDC(ymin);
1830           st1->SetY2NDC(ymax);
1831           st1->SetX1NDC(0.70);
1832           st1->SetX2NDC(0.90);
1833           ymax = ymin;
1834         }
1835         if (k == 0)
1836           sprintf(name, "%s", text1.c_str());
1837         else
1838           sprintf(name, "%s", text2.c_str());
1839         legend->AddEntry(hists[jk], name, "lp");
1840       }
1841       legend->Draw("same");
1842       pad->Update();
1843       char txt[100];
1844       double xmi(0.10), xmx(0.895), ymx(0.95);
1845       if (lumi > 0.01) {
1846         xmx = 0.70;
1847         xmi = 0.30;
1848         TPaveText* txt0 = new TPaveText(0.705, 0.905, 0.90, 0.95, "blNDC");
1849         txt0->SetFillColor(0);
1850         sprintf(txt, "%4.1f TeV %5.1f fb^{-1}", ener, lumi);
1851         txt0->AddText(txt);
1852         txt0->Draw("same");
1853       }
1854       double ymi = ymx - 0.045;
1855       TPaveText* txt1 = new TPaveText(xmi, ymi, xmx, ymx, "blNDC");
1856       txt1->SetFillColor(0);
1857       if (iname == 0)
1858         sprintf(txt, "p = 20:30 GeV %s", text0.c_str());
1859       else
1860         sprintf(txt, "p = 40:60 GeV %s", text0.c_str());
1861       txt1->AddText(txt);
1862       txt1->Draw("same");
1863       ymi = (lumi > 0.1) ? 0.905 : 0.85;
1864       ymx = ymi + 0.045;
1865       TPaveText* txt2 = new TPaveText(0.105, ymi, 0.295, ymx, "blNDC");
1866       txt2->SetFillColor(0);
1867       sprintf(txt, "CMS Preliminary");
1868       txt2->AddText(txt);
1869       txt2->Draw("same");
1870       pad->Modified();
1871       if ((drawStatBox == 0) && (i == 3)) {
1872         double xmin = hists[0]->GetBinLowEdge(1);
1873         int nbin = hists[0]->GetNbinsX();
1874         double xmax = hists[0]->GetBinLowEdge(nbin) + hists[0]->GetBinWidth(nbin);
1875         TLine* line = new TLine(xmin, 1.0, xmax, 1.0);  //etamin,1.0,etamax,1.0);
1876         line->SetLineWidth(2);
1877         line->Draw("same");
1878         pad->Update();
1879       }
1880       pad->Update();
1881       if (save > 0) {
1882         sprintf(name, "%s.pdf", pad->GetName());
1883         pad->Print(name);
1884       } else if (save < 0) {
1885         sprintf(name, "%s.C", pad->GetName());
1886         pad->Print(name);
1887       }
1888     }
1889   }
1890 }
1891 
1892 void PlotFiveHists(std::string infile,
1893                    std::string text0,
1894                    std::string prefix0,
1895                    int type = 0,
1896                    int iname = 3,
1897                    int drawStatBox = 0,
1898                    bool normalize = false,
1899                    int save = 0,
1900                    std::string prefix1 = "",
1901                    std::string text1 = "",
1902                    std::string prefix2 = "",
1903                    std::string text2 = "",
1904                    std::string prefix3 = "",
1905                    std::string text3 = "",
1906                    std::string prefix4 = "",
1907                    std::string text4 = "",
1908                    std::string prefix5 = "",
1909                    std::string text5 = "") {
1910   int colors[5] = {2, 4, 6, 1, 7};
1911   int numb[3] = {5, 1, 4};
1912   std::string names0[5] = {"ratio00", "ratio00One", "etaB04", "Z0", "W0"};
1913   std::string names1[5] = {"ratio10", "ratio10One", "etaB14", "Z1", "W1"};
1914   std::string names2[5] = {"ratio30", "ratio30One", "etaB34", "Z3", "W3"};
1915   std::string xtitl1[5] = {"E_{HCAL}/(p-E_{ECAL})", "E_{HCAL}/(p-E_{ECAL})", "E_{HCAL}/(p-E_{ECAL})", "i#eta", "i#eta"};
1916   std::string ytitl1[5] = {
1917       "Tracks", "Tracks", "Tracks", "MPV(E_{HCAL}/(p-E_{ECAL}))", "MPV/Width(E_{HCAL}/(p-E_{ECAL}))"};
1918   std::string names3[1] = {"R"};
1919   std::string xtitl2[1] = {"RBX #"};
1920   std::string ytitl2[1] = {"MPV(E_{HCAL}/(p-E_{ECAL}))"};
1921   std::string names4[4] = {"pp21", "pp22", "pp23", "pp24"};
1922   std::string xtitl3[4] = {"p (GeV)", "p (GeV)", "p (GeV)", "p (GeV)"};
1923   std::string ytitl3[4] = {"Tracks", "Tracks", "Tracks", "Tracks"};
1924   std::string title3[4] = {"Barrel", "Transition", "Endcap", "Combined"};
1925 
1926   gStyle->SetCanvasBorderMode(0);
1927   gStyle->SetCanvasColor(kWhite);
1928   gStyle->SetPadColor(kWhite);
1929   gStyle->SetFillColor(kWhite);
1930   gStyle->SetOptTitle(0);
1931   if ((drawStatBox / 10) % 10 > 0)
1932     gStyle->SetOptFit(10);
1933   else
1934     gStyle->SetOptFit(0);
1935 
1936   if (type != 1 && type != 2)
1937     type = 0;
1938   char name[100], namep[100];
1939   TFile* file = new TFile(infile.c_str());
1940   for (int i = 0; i < numb[type]; ++i) {
1941     std::vector<TH1D*> hists;
1942     std::vector<int> kks;
1943     std::vector<std::string> texts;
1944     double ymax(0.77);
1945     if (drawStatBox % 10 > 0) {
1946       if (type == 2)
1947         gStyle->SetOptStat(1110);
1948       else if (i != 3)
1949         gStyle->SetOptStat(1100);
1950       else
1951         gStyle->SetOptStat(10);
1952     } else {
1953       gStyle->SetOptStat(0);
1954       ymax = 0.82;
1955     }
1956     for (int k = 0; k < 5; ++k) {
1957       std::string prefix, text;
1958       if (k == 0) {
1959         prefix = prefix1;
1960         text = text1;
1961       } else if (k == 1) {
1962         prefix = prefix2;
1963         text = text2;
1964       } else if (k == 2) {
1965         prefix = prefix3;
1966         text = text3;
1967       } else if (k == 3) {
1968         prefix = prefix4;
1969         text = text4;
1970       } else {
1971         prefix = prefix5;
1972         text = text5;
1973       }
1974       if (prefix != "") {
1975         if (type == 0) {
1976           if (iname == 0)
1977             sprintf(name, "%s%s", prefix.c_str(), names0[i].c_str());
1978           else if (iname == 1)
1979             sprintf(name, "%s%s", prefix.c_str(), names1[i].c_str());
1980           else
1981             sprintf(name, "%s%s", prefix.c_str(), names2[i].c_str());
1982         } else if (type == 1) {
1983           sprintf(name, "%s%s%d", prefix.c_str(), names3[i].c_str(), iname);
1984         } else {
1985           sprintf(name, "%s%s", prefix.c_str(), names4[i].c_str());
1986         }
1987         TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
1988         if (hist1 != nullptr) {
1989           hists.push_back((TH1D*)(hist1->Clone()));
1990           kks.push_back(k);
1991           texts.push_back(text);
1992         }
1993       }
1994     }
1995     if (hists.size() > 0) {
1996       if (type == 0) {
1997         if (iname == 0)
1998           sprintf(namep, "c_%s%s", prefix0.c_str(), names0[i].c_str());
1999         else if (iname == 1)
2000           sprintf(namep, "c_%s%s", prefix0.c_str(), names1[i].c_str());
2001         else
2002           sprintf(namep, "c_%s%s", prefix0.c_str(), names2[i].c_str());
2003       } else if (type == 1) {
2004         sprintf(namep, "c_%s%s%d", prefix0.c_str(), names3[i].c_str(), iname);
2005       } else {
2006         sprintf(namep, "c_%s%s", prefix0.c_str(), names4[i].c_str());
2007       }
2008       double ymax(0.90);
2009       double dy = (i == 0 && type == 0) ? 0.13 : 0.08;
2010       double ymx0 = (drawStatBox == 0) ? (ymax - .01) : (ymax - dy * hists.size() - .01);
2011       TCanvas* pad = new TCanvas(namep, namep, 700, 500);
2012       TLegend* legend = new TLegend(0.64, ymx0 - 0.05 * hists.size(), 0.89, ymx0);
2013       legend->SetFillColor(kWhite);
2014       pad->SetRightMargin(0.10);
2015       pad->SetTopMargin(0.10);
2016       for (unsigned int jk = 0; jk < hists.size(); ++jk) {
2017         int k = kks[jk];
2018         hists[jk]->GetXaxis()->SetTitleSize(0.040);
2019         if (type == 0) {
2020           hists[jk]->GetXaxis()->SetTitle(xtitl1[i].c_str());
2021           hists[jk]->GetYaxis()->SetTitle(ytitl1[i].c_str());
2022         } else if (type == 1) {
2023           hists[jk]->GetXaxis()->SetTitle(xtitl2[i].c_str());
2024           hists[jk]->GetYaxis()->SetTitle(ytitl2[i].c_str());
2025         } else {
2026           hists[jk]->GetXaxis()->SetTitle(xtitl3[i].c_str());
2027           hists[jk]->GetYaxis()->SetTitle(ytitl3[i].c_str());
2028         }
2029         hists[jk]->GetYaxis()->SetLabelOffset(0.005);
2030         hists[jk]->GetYaxis()->SetLabelSize(0.035);
2031         hists[jk]->GetYaxis()->SetTitleSize(0.040);
2032         hists[jk]->GetYaxis()->SetTitleOffset(1.15);
2033         if ((type == 0) && (i != 3) && (i != 4))
2034           hists[jk]->GetXaxis()->SetRangeUser(0.0, 2.5);
2035         if (type == 0) {
2036           if (i == 3)
2037             hists[jk]->GetYaxis()->SetRangeUser(0.8, 1.2);
2038           else if (i == 4)
2039             hists[jk]->GetYaxis()->SetRangeUser(0.0, 0.5);
2040         }
2041         if (type == 1)
2042           hists[jk]->GetYaxis()->SetRangeUser(0.75, 1.2);
2043         hists[jk]->SetMarkerStyle(20);
2044         hists[jk]->SetMarkerColor(colors[k]);
2045         hists[jk]->SetLineColor(colors[k]);
2046         if (normalize && ((type == 2) || ((type == 0) && (i != 3)))) {
2047           if (jk == 0)
2048             hists[jk]->DrawNormalized();
2049           else
2050             hists[jk]->DrawNormalized("sames");
2051         } else {
2052           if (jk == 0)
2053             hists[jk]->Draw();
2054           else
2055             hists[jk]->Draw("sames");
2056         }
2057         pad->Update();
2058         TPaveStats* st1 = (TPaveStats*)hists[jk]->GetListOfFunctions()->FindObject("stats");
2059         if (st1 != nullptr) {
2060           double ymin = ymax - dy;
2061           st1->SetLineColor(colors[k]);
2062           st1->SetTextColor(colors[k]);
2063           st1->SetY1NDC(ymin);
2064           st1->SetY2NDC(ymax);
2065           st1->SetX1NDC(0.70);
2066           st1->SetX2NDC(0.90);
2067           ymax = ymin;
2068         }
2069         sprintf(name, "%s", texts[jk].c_str());
2070         legend->AddEntry(hists[jk], name, "lp");
2071       }
2072       legend->Draw("same");
2073       pad->Update();
2074       TPaveText* txt1 = new TPaveText(0.10, 0.905, 0.80, 0.95, "blNDC");
2075       txt1->SetFillColor(0);
2076       char txt[100];
2077       if (type == 2) {
2078         sprintf(txt, "p = 40:60 GeV (%s)", title3[i].c_str());
2079       } else if (((type == 0) && (iname == 0))) {
2080         sprintf(txt, "p = 20:30 GeV %s", text0.c_str());
2081       } else {
2082         sprintf(txt, "p = 40:60 GeV %s", text0.c_str());
2083       }
2084       txt1->AddText(txt);
2085       txt1->Draw("same");
2086       TPaveText* txt2 = new TPaveText(0.11, 0.825, 0.33, 0.895, "blNDC");
2087       txt2->SetFillColor(0);
2088       sprintf(txt, "CMS Preliminary");
2089       txt2->AddText(txt);
2090       txt2->Draw("same");
2091       pad->Modified();
2092       if ((drawStatBox == 0) && (i == 3)) {
2093         double xmin = hists[0]->GetBinLowEdge(1);
2094         int nbin = hists[0]->GetNbinsX();
2095         double xmax = hists[0]->GetBinLowEdge(nbin) + hists[0]->GetBinWidth(nbin);
2096         TLine* line = new TLine(xmin, 1.0, xmax, 1.0);  //etamin,1.0,etamax,1.0);
2097         line->SetLineWidth(2);
2098         line->Draw("same");
2099         pad->Update();
2100       }
2101       pad->Update();
2102       if (save > 0) {
2103         sprintf(name, "%s.pdf", pad->GetName());
2104         pad->Print(name);
2105       } else if (save < 0) {
2106         sprintf(name, "%s.C", pad->GetName());
2107         pad->Print(name);
2108       }
2109     }
2110   }
2111 }
2112 
2113 void PlotHistCorrResults(std::string infile, std::string text, std::string prefixF, int save = 0) {
2114   std::string name[5] = {"Eta1Bf", "Eta2Bf", "Eta1Af", "Eta2Af", "Cvg0"};
2115   std::string title[5] = {"Mean at the start of itertions",
2116                           "Median at the start of itertions",
2117                           "Mean at the end of itertions",
2118                           "Median at the end of itertions",
2119                           ""};
2120   int type[5] = {0, 0, 0, 0, 1};
2121 
2122   gStyle->SetCanvasBorderMode(0);
2123   gStyle->SetCanvasColor(kWhite);
2124   gStyle->SetPadColor(kWhite);
2125   gStyle->SetFillColor(kWhite);
2126   gStyle->SetOptTitle(0);
2127   gStyle->SetOptStat(10);
2128   gStyle->SetOptFit(10);
2129   TFile* file = new TFile(infile.c_str());
2130   char namep[100];
2131   for (int k = 0; k < 5; ++k) {
2132     TH1D* hist1 = (TH1D*)file->FindObjectAny(name[k].c_str());
2133     if (hist1 != nullptr) {
2134       TH1D* hist = (TH1D*)(hist1->Clone());
2135       sprintf(namep, "c_%s%s", prefixF.c_str(), name[k].c_str());
2136       TCanvas* pad = new TCanvas(namep, namep, 700, 500);
2137       pad->SetRightMargin(0.10);
2138       pad->SetTopMargin(0.10);
2139       hist->GetYaxis()->SetLabelOffset(0.005);
2140       hist->GetYaxis()->SetTitleOffset(1.20);
2141       double xmin = hist->GetBinLowEdge(1);
2142       int nbin = hist->GetNbinsX();
2143       double xmax = hist->GetBinLowEdge(nbin) + hist->GetBinWidth(nbin);
2144       std::cout << hist->GetTitle() << " Bins " << nbin << ":" << xmin << ":" << xmax << std::endl;
2145       double xlow(0.12), ylow(0.82);
2146       char txt[100], option[2];
2147       if (type[k] == 0) {
2148         sprintf(namep, "f_%s", name[k].c_str());
2149         TF1* func = new TF1(namep, "pol0", xmin, xmax);
2150         hist->Fit(func, "+QWL", "");
2151         if (text == "")
2152           sprintf(txt, "%s", title[k].c_str());
2153         else
2154           sprintf(txt, "%s (balancing the %s)", title[k].c_str(), text.c_str());
2155         sprintf(option, "%s", "");
2156       } else {
2157         hist->GetXaxis()->SetTitle("Iterations");
2158         hist->GetYaxis()->SetTitle("Convergence");
2159         hist->SetMarkerStyle(20);
2160         hist->SetMarkerColor(2);
2161         hist->SetMarkerSize(0.8);
2162         xlow = 0.50;
2163         ylow = 0.86;
2164         sprintf(txt, "(%s)", text.c_str());
2165         sprintf(option, "%s", "p");
2166       }
2167       hist->Draw(option);
2168       pad->Update();
2169       TPaveStats* st1 = (TPaveStats*)hist->GetListOfFunctions()->FindObject("stats");
2170       if (st1 != nullptr) {
2171         st1->SetY1NDC(ylow);
2172         st1->SetY2NDC(0.90);
2173         st1->SetX1NDC(0.70);
2174         st1->SetX2NDC(0.90);
2175       }
2176       TPaveText* txt1 = new TPaveText(xlow, 0.82, 0.68, 0.88, "blNDC");
2177       txt1->SetFillColor(0);
2178       txt1->AddText(txt);
2179       txt1->Draw("same");
2180       pad->Modified();
2181       pad->Update();
2182       if (save > 0) {
2183         sprintf(namep, "%s.pdf", pad->GetName());
2184         pad->Print(namep);
2185       } else if (save < 0) {
2186         sprintf(namep, "%s.C", pad->GetName());
2187         pad->Print(namep);
2188       }
2189     }
2190   }
2191 }
2192 
2193 void PlotHistCorrFactor(char* infile,
2194                         std::string text,
2195                         std::string prefixF,
2196                         double scale = 1.0,
2197                         int nmin = 100,
2198                         bool isRealData = true,
2199                         bool drawStatBox = false,
2200                         int iformat = 0,
2201                         int save = 0) {
2202   std::map<int, cfactors> cfacs;
2203   int etamin(100), etamax(-100), maxdepth(0);
2204   readCorrFactors(infile, scale, cfacs, etamin, etamax, maxdepth, iformat);
2205 
2206   gStyle->SetCanvasBorderMode(0);
2207   gStyle->SetCanvasColor(kWhite);
2208   gStyle->SetPadColor(kWhite);
2209   gStyle->SetFillColor(kWhite);
2210   gStyle->SetOptTitle(0);
2211   if (drawStatBox) {
2212     gStyle->SetOptStat(10);
2213     gStyle->SetOptFit(10);
2214   } else {
2215     gStyle->SetOptStat(0);
2216     gStyle->SetOptFit(0);
2217   }
2218   int colors[7] = {1, 6, 4, 7, 2, 9, 3};
2219   int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
2220   int nbin = etamax - etamin + 1;
2221   std::vector<TH1D*> hists;
2222   std::vector<int> entries;
2223   char name[100];
2224   double dy(0);
2225   int fits(0);
2226   for (int j = 0; j < maxdepth; ++j) {
2227     sprintf(name, "hd%d", j + 1);
2228     TObject* ob = gROOT->FindObject(name);
2229     if (ob)
2230       ob->Delete();
2231     TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
2232     int nent(0);
2233     double chi2(0);
2234     for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr) {
2235       if ((itr->second).depth == j + 1) {
2236         int ieta = (itr->second).ieta;
2237         int bin = ieta - etamin + 1;
2238         float val = (itr->second).corrf;
2239         float dvl = (itr->second).dcorr;
2240         h->SetBinContent(bin, val);
2241         h->SetBinError(bin, dvl);
2242         nent++;
2243         chi2 += (((val - 1.0) / dvl) * ((val - 1.0) / dvl));
2244       }
2245     }
2246     std::cout << "Depth = " << (j + 1) << " chi2 = " << chi2 << "/" << nent << std::endl;
2247     if (nent > nmin) {
2248       fits++;
2249       dy += 0.025;
2250       sprintf(name, "hdf%d", j + 1);
2251       TObject* ob = gROOT->FindObject(name);
2252       if (ob)
2253         ob->Delete();
2254       TF1* func = new TF1(name, "pol0", etamin, etamax);
2255       h->Fit(func, "+QWLR", "");
2256     }
2257     h->SetLineColor(colors[j]);
2258     h->SetMarkerColor(colors[j]);
2259     h->SetMarkerStyle(mtype[j]);
2260     h->GetXaxis()->SetTitle("i#eta");
2261     h->GetYaxis()->SetTitle("Correction Factor");
2262     h->GetYaxis()->SetLabelOffset(0.005);
2263     h->GetYaxis()->SetTitleOffset(1.20);
2264     h->GetYaxis()->SetRangeUser(0.0, 2.0);
2265     hists.push_back(h);
2266     entries.push_back(nent);
2267     dy += 0.025;
2268   }
2269   sprintf(name, "c_%sCorrFactor", prefixF.c_str());
2270   TCanvas* pad = new TCanvas(name, name, 700, 500);
2271   pad->SetRightMargin(0.10);
2272   pad->SetTopMargin(0.10);
2273   double yh = 0.90;
2274   // double yl = yh - 0.025 * hists.size() - dy - 0.01;
2275   double yl = 0.15;
2276   TLegend* legend = new TLegend(0.35, yl, 0.65, yl + 0.04 * hists.size());
2277   legend->SetFillColor(kWhite);
2278   for (unsigned int k = 0; k < hists.size(); ++k) {
2279     if (k == 0)
2280       hists[k]->Draw("");
2281     else
2282       hists[k]->Draw("sames");
2283     pad->Update();
2284     TPaveStats* st1 = (TPaveStats*)hists[k]->GetListOfFunctions()->FindObject("stats");
2285     if (st1 != nullptr) {
2286       dy = (entries[k] > nmin) ? 0.05 : 0.025;
2287       st1->SetLineColor(colors[k]);
2288       st1->SetTextColor(colors[k]);
2289       st1->SetY1NDC(yh - dy);
2290       st1->SetY2NDC(yh);
2291       st1->SetX1NDC(0.70);
2292       st1->SetX2NDC(0.90);
2293       yh -= dy;
2294     }
2295     sprintf(name, "Depth %d (%s)", k + 1, text.c_str());
2296     legend->AddEntry(hists[k], name, "lp");
2297   }
2298   legend->Draw("same");
2299   pad->Update();
2300   if (fits < 1) {
2301     double xmin = hists[0]->GetBinLowEdge(1);
2302     int nbin = hists[0]->GetNbinsX();
2303     double xmax = hists[0]->GetBinLowEdge(nbin) + hists[0]->GetBinWidth(nbin);
2304     TLine* line = new TLine(xmin, 1.0, xmax, 1.0);
2305     line->SetLineColor(9);
2306     line->SetLineWidth(2);
2307     line->SetLineStyle(2);
2308     line->Draw("same");
2309     pad->Modified();
2310     pad->Update();
2311   }
2312   char txt1[30];
2313   double xmax = (isRealData) ? 0.33 : 0.44;
2314   TPaveText* txt2 = new TPaveText(0.11, 0.85, xmax, 0.89, "blNDC");
2315   txt2->SetFillColor(0);
2316   if (isRealData)
2317     sprintf(txt1, "CMS Preliminary");
2318   else
2319     sprintf(txt1, "CMS Simulation Preliminary");
2320   txt2->AddText(txt1);
2321   txt2->Draw("same");
2322   pad->Modified();
2323   pad->Update();
2324   if (save > 0) {
2325     sprintf(name, "%s.pdf", pad->GetName());
2326     pad->Print(name);
2327   } else if (save < 0) {
2328     sprintf(name, "%s.C", pad->GetName());
2329     pad->Print(name);
2330   }
2331 }
2332 
2333 void PlotHistCorrFactor(char* infile,
2334                         std::string text,
2335                         int depth,
2336                         std::string prefixF,
2337                         double scale = 1.0,
2338                         int nmin = 100,
2339                         bool isRealData = true,
2340                         bool drawStatBox = false,
2341                         int iformat = 0,
2342                         int save = 0) {
2343   std::map<int, cfactors> cfacs;
2344   int etamin(100), etamax(-100), maxdepth(0);
2345   readCorrFactors(infile, scale, cfacs, etamin, etamax, maxdepth, iformat);
2346 
2347   gStyle->SetCanvasBorderMode(0);
2348   gStyle->SetCanvasColor(kWhite);
2349   gStyle->SetPadColor(kWhite);
2350   gStyle->SetFillColor(kWhite);
2351   gStyle->SetOptTitle(0);
2352   if (drawStatBox) {
2353     gStyle->SetOptStat(10);
2354     gStyle->SetOptFit(10);
2355   } else {
2356     gStyle->SetOptStat(0);
2357     gStyle->SetOptFit(0);
2358   }
2359   int colors[7] = {1, 6, 4, 7, 2, 9, 3};
2360   int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
2361   int nbin = etamax - etamin + 1;
2362   std::vector<TH1D*> hists;
2363   std::vector<int> entries;
2364   char name[100];
2365   double dy(0);
2366   int fits(0);
2367   sprintf(name, "hd%d", depth);
2368   TObject* ob = gROOT->FindObject(name);
2369   if (ob)
2370     ob->Delete();
2371   TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
2372   int nent(0);
2373   double chi2(0);
2374   for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr) {
2375     if ((itr->second).depth == depth) {
2376       int ieta = (itr->second).ieta;
2377       int bin = ieta - etamin + 1;
2378       float val = (itr->second).corrf;
2379       float dvl = (itr->second).dcorr;
2380       h->SetBinContent(bin, val);
2381       h->SetBinError(bin, dvl);
2382       nent++;
2383       chi2 += (((val - 1.0) / dvl) * ((val - 1.0) / dvl));
2384     }
2385   }
2386   std::cout << "Depth = " << depth << " chi2 = " << chi2 << "/" << nent << std::endl;
2387   if (nent > nmin) {
2388     fits++;
2389     dy += 0.025;
2390     sprintf(name, "hdf%d", depth);
2391     TObject* ob = gROOT->FindObject(name);
2392     if (ob)
2393       ob->Delete();
2394     TF1* func = new TF1(name, "pol0", etamin, etamax);
2395     h->Fit(func, "+QWLR", "");
2396   }
2397   h->SetLineColor(colors[depth - 1]);
2398   h->SetMarkerColor(colors[depth - 1]);
2399   h->SetMarkerStyle(mtype[depth - 1]);
2400   h->GetXaxis()->SetTitle("i#eta");
2401   h->GetYaxis()->SetTitle("Correction Factor");
2402   h->GetYaxis()->SetLabelOffset(0.005);
2403   h->GetYaxis()->SetTitleOffset(1.20);
2404   h->GetYaxis()->SetRangeUser(0.0, 2.0);
2405   hists.push_back(h);
2406   entries.push_back(nent);
2407   dy += 0.025;
2408 
2409   sprintf(name, "c_%sD%dCorrFactor", prefixF.c_str(), depth);
2410   TCanvas* pad = new TCanvas(name, name, 700, 500);
2411   pad->SetRightMargin(0.10);
2412   pad->SetTopMargin(0.10);
2413   double yh = 0.90;
2414   // double yl = yh - 0.025 * hists.size() - dy - 0.01;
2415   double yl = 0.15;
2416   TLegend* legend = new TLegend(0.35, yl, 0.85, yl + 0.04 * hists.size());
2417   legend->SetFillColor(kWhite);
2418   for (unsigned int k = 0; k < hists.size(); ++k) {
2419     if (k == 0)
2420       hists[k]->Draw("");
2421     else
2422       hists[k]->Draw("sames");
2423     pad->Update();
2424     if (drawStatBox) {
2425       TPaveStats* st1 = (TPaveStats*)hists[k]->GetListOfFunctions()->FindObject("stats");
2426       if (st1 != nullptr) {
2427         dy = (entries[k] > nmin) ? 0.05 : 0.025;
2428         st1->SetLineColor(colors[k]);
2429         st1->SetTextColor(colors[k]);
2430         st1->SetY1NDC(yh - dy);
2431         st1->SetY2NDC(yh);
2432         st1->SetX1NDC(0.70);
2433         st1->SetX2NDC(0.90);
2434         yh -= dy;
2435       }
2436     }
2437     sprintf(name, "Depth %d (%s)", depth, text.c_str());
2438     legend->AddEntry(hists[k], name, "lp");
2439   }
2440   legend->Draw("same");
2441   pad->Update();
2442   if (fits < 1) {
2443     double xmin = hists[0]->GetBinLowEdge(1);
2444     int nbin = hists[0]->GetNbinsX();
2445     double xmax = hists[0]->GetBinLowEdge(nbin) + hists[0]->GetBinWidth(nbin);
2446     TLine* line = new TLine(xmin, 1.0, xmax, 1.0);
2447     line->SetLineColor(9);
2448     line->SetLineWidth(2);
2449     line->SetLineStyle(2);
2450     line->Draw("same");
2451     pad->Modified();
2452     pad->Update();
2453   }
2454   char txt1[30];
2455   double xmax = (isRealData) ? 0.33 : 0.44;
2456   TPaveText* txt2 = new TPaveText(0.11, 0.85, xmax, 0.89, "blNDC");
2457   txt2->SetFillColor(0);
2458   if (isRealData)
2459     sprintf(txt1, "CMS Preliminary");
2460   else
2461     sprintf(txt1, "CMS Simulation Preliminary");
2462   txt2->AddText(txt1);
2463   txt2->Draw("same");
2464   pad->Modified();
2465   pad->Update();
2466   if (save > 0) {
2467     sprintf(name, "%s.pdf", pad->GetName());
2468     pad->Print(name);
2469   } else if (save < 0) {
2470     sprintf(name, "%s.C", pad->GetName());
2471     pad->Print(name);
2472   }
2473 }
2474 
2475 void PlotHistCorrAsymmetry(
2476     char* infile, std::string text, std::string prefixF = "", int depth = -1, int iformat = 0, int save = 0) {
2477   std::map<int, cfactors> cfacs;
2478   int etamin(100), etamax(-100), maxdepth(0);
2479   double scale(1.0);
2480   readCorrFactors(infile, scale, cfacs, etamin, etamax, maxdepth, iformat);
2481 
2482   gStyle->SetCanvasBorderMode(0);
2483   gStyle->SetCanvasColor(kWhite);
2484   gStyle->SetPadColor(kWhite);
2485   gStyle->SetFillColor(kWhite);
2486   gStyle->SetOptTitle(0);
2487   gStyle->SetOptStat(0);
2488   gStyle->SetOptFit(10);
2489   int colors[7] = {1, 6, 4, 7, 2, 9, 3};
2490   int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
2491   int nbin = etamax + 1;
2492   std::vector<TH1D*> hists;
2493   std::vector<int> entries;
2494   char name[100];
2495   double dy(0);
2496   int maxd = (depth < 0) ? maxdepth : 1;
2497   for (int j = 0; j < maxd; ++j) {
2498     int dep = (depth <= 0) ? (j + 1) : depth;
2499     sprintf(name, "hd%d", dep);
2500     TObject* ob = gROOT->FindObject(name);
2501     if (ob)
2502       ob->Delete();
2503     TH1D* h = new TH1D(name, name, nbin, 0, etamax);
2504     int nent(0);
2505     for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr) {
2506       if ((itr->second).depth == dep) {
2507         int ieta = (itr->second).ieta;
2508         float vl1 = (itr->second).corrf;
2509         float dv1 = (itr->second).dcorr;
2510         if (ieta > 0) {
2511           for (std::map<int, cfactors>::const_iterator ktr = cfacs.begin(); ktr != cfacs.end(); ++ktr) {
2512             if (((ktr->second).depth == dep) && ((ktr->second).ieta == -ieta)) {
2513               float vl2 = (ktr->second).corrf;
2514               float dv2 = (ktr->second).dcorr;
2515               float val = 2.0 * (vl1 - vl2) / (vl1 + vl2);
2516               float dvl = (4.0 * sqrt(vl1 * vl1 * dv2 * dv2 + vl2 * vl2 * dv1 * dv1) / ((vl1 + vl2) * (vl1 + vl2)));
2517               int bin = ieta;
2518               h->SetBinContent(bin, val);
2519               h->SetBinError(bin, dvl);
2520               nent++;
2521             }
2522           }
2523         }
2524       }
2525     }
2526     h->SetLineColor(colors[dep - 1]);
2527     h->SetMarkerColor(colors[dep - 1]);
2528     h->SetMarkerStyle(mtype[dep - 1]);
2529     h->GetXaxis()->SetTitle("i#eta");
2530     h->GetYaxis()->SetTitle("Asymmetry in Correction Factor");
2531     h->GetYaxis()->SetLabelOffset(0.005);
2532     h->GetYaxis()->SetTitleOffset(1.20);
2533     h->GetYaxis()->SetRangeUser(-0.25, 0.25);
2534     hists.push_back(h);
2535     entries.push_back(nent);
2536     dy += 0.025;
2537   }
2538   if (depth < 0)
2539     sprintf(name, "c_%sCorrAsymmetry", prefixF.c_str());
2540   else
2541     sprintf(name, "c_%sCorrAsymmetryD%d", prefixF.c_str(), depth);
2542   TCanvas* pad = new TCanvas(name, name, 700, 500);
2543   pad->SetRightMargin(0.10);
2544   pad->SetTopMargin(0.10);
2545   double yh = 0.90;
2546   double yl = yh - 0.035 * hists.size() - dy - 0.01;
2547   TLegend* legend = new TLegend(0.60, yl, 0.90, yl + 0.035 * hists.size());
2548   legend->SetFillColor(kWhite);
2549   for (unsigned int k = 0; k < hists.size(); ++k) {
2550     int dep = (depth < 0) ? (k + 1) : depth;
2551     if (k == 0)
2552       hists[k]->Draw("");
2553     else
2554       hists[k]->Draw("sames");
2555     pad->Update();
2556     sprintf(name, "Depth %d (%s)", dep, text.c_str());
2557     legend->AddEntry(hists[k], name, "lp");
2558   }
2559   legend->Draw("same");
2560   pad->Update();
2561 
2562   TLine* line = new TLine(0.0, 0.0, etamax, 0.0);
2563   line->SetLineColor(9);
2564   line->SetLineWidth(2);
2565   line->SetLineStyle(2);
2566   line->Draw("same");
2567   pad->Update();
2568 
2569   if (save > 0) {
2570     sprintf(name, "%s.pdf", pad->GetName());
2571     pad->Print(name);
2572   } else if (save < 0) {
2573     sprintf(name, "%s.C", pad->GetName());
2574     pad->Print(name);
2575   }
2576 }
2577 
2578 void PlotHistCorrFactors(char* infile1,
2579                          std::string text1,
2580                          char* infile2,
2581                          std::string text2,
2582                          char* infile3,
2583                          std::string text3,
2584                          char* infile4,
2585                          std::string text4,
2586                          char* infile5,
2587                          std::string text5,
2588                          std::string prefixF,
2589                          bool ratio = false,
2590                          bool drawStatBox = true,
2591                          int nmin = 100,
2592                          bool isRealData = false,
2593                          const char* year = "2024",
2594                          int iformat = 0,
2595                          int save = 0) {
2596   std::map<int, cfactors> cfacs[5];
2597   std::vector<std::string> texts;
2598   int nfile(0), etamin(100), etamax(-100), maxdepth(0);
2599   const char* blank("");
2600   if (infile1 != blank) {
2601     readCorrFactors(infile1, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
2602     if (cfacs[nfile].size() > 0) {
2603       texts.push_back(text1);
2604       ++nfile;
2605     }
2606   }
2607   if (infile2 != blank) {
2608     readCorrFactors(infile2, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
2609     if (cfacs[nfile].size() > 0) {
2610       texts.push_back(text2);
2611       ++nfile;
2612     }
2613   }
2614   if (infile3 != blank) {
2615     readCorrFactors(infile3, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
2616     if (cfacs[nfile].size() > 0) {
2617       texts.push_back(text3);
2618       ++nfile;
2619     }
2620   }
2621   if (infile4 != blank) {
2622     readCorrFactors(infile4, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
2623     if (cfacs[nfile].size() > 0) {
2624       texts.push_back(text4);
2625       ++nfile;
2626     }
2627   }
2628   if (infile5 != blank) {
2629     readCorrFactors(infile5, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
2630     if (cfacs[nfile].size() > 0) {
2631       texts.push_back(text5);
2632       ++nfile;
2633     }
2634   }
2635 
2636   if (nfile > 0) {
2637     gStyle->SetCanvasBorderMode(0);
2638     gStyle->SetCanvasColor(kWhite);
2639     gStyle->SetPadColor(kWhite);
2640     gStyle->SetFillColor(kWhite);
2641     gStyle->SetOptTitle(0);
2642     if ((!ratio) && drawStatBox) {
2643       gStyle->SetOptStat(10);
2644       gStyle->SetOptFit(10);
2645     } else {
2646       gStyle->SetOptStat(0);
2647       gStyle->SetOptFit(0);
2648     }
2649     int colors[7] = {1, 6, 4, 7, 2, 9, 3};
2650     int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
2651     int nbin = etamax - etamin + 1;
2652     std::vector<TH1D*> hists;
2653     std::vector<int> entries, htype, depths;
2654     std::vector<double> fitr;
2655     char name[100];
2656     double dy(0);
2657     int fits(0);
2658     int nline(0);
2659     if (ratio) {
2660       for (int ih = 1; ih < nfile; ++ih) {
2661         for (int j = 0; j < maxdepth; ++j) {
2662           sprintf(name, "h%dd%d", ih, j + 1);
2663           TObject* ob = gROOT->FindObject(name);
2664           if (ob)
2665             ob->Delete();
2666           TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
2667           double sumNum(0), sumDen(0), chi2(0);
2668           int npt(0);
2669           std::map<int, cfactors>::const_iterator ktr = cfacs[ih].begin();
2670           for (std::map<int, cfactors>::const_iterator itr = cfacs[0].begin(); itr != cfacs[0].end(); ++itr, ++ktr) {
2671             int dep = (itr->second).depth;
2672             if (dep == j + 1) {
2673               int ieta = (itr->second).ieta;
2674               int bin = ieta - etamin + 1;
2675               float val = (itr->second).corrf / (ktr->second).corrf;
2676               float dvl =
2677                   val *
2678                   sqrt((((itr->second).dcorr * (itr->second).dcorr) / ((itr->second).corrf * (itr->second).corrf)) +
2679                        (((ktr->second).dcorr * (ktr->second).dcorr) / ((ktr->second).corrf * (ktr->second).corrf)));
2680               h->SetBinContent(bin, val);
2681               h->SetBinError(bin, dvl);
2682               sumNum += (val / (dvl * dvl));
2683               sumDen += (1.0 / (dvl * dvl));
2684               ++npt;
2685               chi2 += (((val - 1.0) / dvl) * ((val - 1.0) / dvl));
2686             }
2687           }
2688           double fit = (sumDen > 0) ? (sumNum / sumDen) : 1.0;
2689           std::cout << texts[ih] << " Depth = " << (j + 1) << " Fit to Pol0: " << fit << " chi2: " << chi2 << "/" << npt
2690                     << std::endl;
2691           h->SetLineColor(colors[ih]);
2692           h->SetMarkerColor(colors[ih]);
2693           h->SetMarkerStyle(mtype[j]);
2694           h->SetMarkerSize(0.9);
2695           h->GetXaxis()->SetTitle("i#eta");
2696           if (nfile > 2)
2697             sprintf(name, "CF_{%s}/CF_{Set}", texts[0].c_str());
2698           else
2699             sprintf(name, "CF_{%s}/CF_{%s}", texts[0].c_str(), texts[ih].c_str());
2700           h->GetYaxis()->SetTitle(name);
2701           h->GetYaxis()->SetLabelOffset(0.005);
2702           h->GetYaxis()->SetTitleSize(0.036);
2703           h->GetYaxis()->SetTitleOffset(1.20);
2704           h->GetYaxis()->SetRangeUser(0.50, 1.50);
2705           hists.push_back(h);
2706           fitr.push_back(fit);
2707           htype.push_back(ih);
2708           depths.push_back(j + 1);
2709         }
2710         if ((ih == 1) || (maxdepth <= 4))
2711           nline += hists.size();
2712         else
2713           ++nline;
2714       }
2715     } else {
2716       for (int k1 = 0; k1 < nfile; ++k1) {
2717         for (int j = 0; j < maxdepth; ++j) {
2718           sprintf(name, "h%dd%d", k1, j + 1);
2719           TObject* ob = gROOT->FindObject(name);
2720           if (ob)
2721             ob->Delete();
2722           TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
2723           int nent(0);
2724           for (std::map<int, cfactors>::const_iterator itr = cfacs[k1].begin(); itr != cfacs[k1].end(); ++itr) {
2725             int dep = (itr->second).depth;
2726             if (dep == j + 1) {
2727               int ieta = (itr->second).ieta;
2728               int bin = ieta - etamin + 1;
2729               float val = (itr->second).corrf;
2730               float dvl = (itr->second).dcorr;
2731               h->SetBinContent(bin, val);
2732               h->SetBinError(bin, dvl);
2733               nent++;
2734             }
2735           }
2736           if (nent > nmin) {
2737             fits++;
2738             if (drawStatBox)
2739               dy += 0.025;
2740             sprintf(name, "h%ddf%d", k1, j + 1);
2741             TObject* ob = gROOT->FindObject(name);
2742             if (ob)
2743               ob->Delete();
2744             TF1* func = new TF1(name, "pol0", etamin, etamax);
2745             h->Fit(func, "+QWLR", "");
2746           }
2747           h->SetLineColor(colors[k1]);
2748           h->SetMarkerColor(colors[k1]);
2749           h->SetMarkerStyle(mtype[j]);
2750           h->SetMarkerSize(0.9);
2751           h->GetXaxis()->SetTitle("i#eta");
2752           h->GetYaxis()->SetTitle("Correction Factor");
2753           h->GetYaxis()->SetLabelOffset(0.005);
2754           h->GetYaxis()->SetTitleOffset(1.20);
2755           h->GetYaxis()->SetRangeUser(0.5, 1.5);
2756           hists.push_back(h);
2757           entries.push_back(nent);
2758           if (drawStatBox)
2759             dy += 0.025;
2760           htype.push_back(k1);
2761           depths.push_back(j + 1);
2762         }
2763         if ((k1 <= 1) || (maxdepth <= 4))
2764           nline += hists.size();
2765         else
2766           ++nline;
2767       }
2768     }
2769     if (ratio)
2770       sprintf(name, "c_Corr%sRatio", prefixF.c_str());
2771     else
2772       sprintf(name, "c_Corr%s", prefixF.c_str());
2773     TCanvas* pad = new TCanvas(name, name, 700, 500);
2774     pad->SetRightMargin(0.10);
2775     pad->SetTopMargin(0.10);
2776     double yh = 0.90;
2777     double yl = yh - 0.035 * hists.size() - dy - 0.01;
2778     TLegend* legend = new TLegend(0.45, yl, 0.90, yl + 0.035 * nline);
2779     legend->SetFillColor(kWhite);
2780     for (unsigned int k = 0; k < hists.size(); ++k) {
2781       if (k == 0)
2782         hists[k]->Draw("");
2783       else
2784         hists[k]->Draw("sames");
2785       pad->Update();
2786       int k1 = htype[k];
2787       if (!ratio) {
2788         TPaveStats* st1 = (TPaveStats*)hists[k]->GetListOfFunctions()->FindObject("stats");
2789         if (st1 != nullptr) {
2790           dy = (entries[k] > nmin) ? 0.05 : 0.025;
2791           st1->SetLineColor(colors[k1]);
2792           st1->SetTextColor(colors[k1]);
2793           st1->SetY1NDC(yh - dy);
2794           st1->SetY2NDC(yh);
2795           st1->SetX1NDC(0.70);
2796           st1->SetX2NDC(0.90);
2797           yh -= dy;
2798         }
2799         sprintf(name, "Depth %d (%s)", depths[k], texts[k1].c_str());
2800       } else {
2801         sprintf(name, "Depth %d (Mean[CF_{%s}/CF_{%s}] = %5.3f)", depths[k], text1.c_str(), texts[k1].c_str(), fitr[k]);
2802       }
2803       if ((depths[k] == 1) || (k1 <= 1) || (maxdepth <= 4))
2804         legend->AddEntry(hists[k], name, "lp");
2805     }
2806     legend->Draw("same");
2807     TPaveText* txt0 = new TPaveText(0.11, 0.84, 0.45, 0.89, "blNDC");
2808     txt0->SetFillColor(0);
2809     char txt[40];
2810     if (isRealData)
2811       sprintf(txt, "CMS Preliminary (%s)", year);
2812     else
2813       sprintf(txt, "CMS Simulation Preliminary (%s)", year);
2814     txt0->AddText(txt);
2815     txt0->Draw("same");
2816     pad->Update();
2817     if (fits < 1) {
2818       double xmin = hists[0]->GetBinLowEdge(1);
2819       int nbin = hists[0]->GetNbinsX();
2820       double xmax = hists[0]->GetBinLowEdge(nbin) + hists[0]->GetBinWidth(nbin);
2821       TLine* line = new TLine(xmin, 1.0, xmax, 1.0);
2822       line->SetLineColor(9);
2823       line->SetLineWidth(2);
2824       line->SetLineStyle(2);
2825       line->Draw("same");
2826       pad->Update();
2827     }
2828     if (save > 0) {
2829       sprintf(name, "%s.pdf", pad->GetName());
2830       pad->Print(name);
2831     } else if (save < 0) {
2832       sprintf(name, "%s.C", pad->GetName());
2833       pad->Print(name);
2834     }
2835   }
2836 }
2837 
2838 void PlotHistCorr2Factors(char* infile1,
2839                           std::string text1,
2840                           char* infile2,
2841                           std::string text2,
2842                           int depth,
2843                           std::string prefixF,
2844                           bool ratio = true,
2845                           bool drawStatBox = false,
2846                           int nmin = 100,
2847                           bool isRealData = true,
2848                           const char* year = "2024",
2849                           int iformat = 0,
2850                           int save = 0) {
2851   std::map<int, cfactors> cfacs[5];
2852   std::vector<std::string> texts;
2853   int nfile(0), etamin(100), etamax(-100), maxdepth(0);
2854   const char* blank("");
2855   if (infile1 != blank) {
2856     readCorrFactors(infile1, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
2857     if (cfacs[nfile].size() > 0) {
2858       texts.push_back(text1);
2859       ++nfile;
2860     }
2861   }
2862   if (infile2 != blank) {
2863     readCorrFactors(infile2, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
2864     if (cfacs[nfile].size() > 0) {
2865       texts.push_back(text2);
2866       ++nfile;
2867     }
2868   }
2869 
2870   if (nfile > 0) {
2871     gStyle->SetCanvasBorderMode(0);
2872     gStyle->SetCanvasColor(kWhite);
2873     gStyle->SetPadColor(kWhite);
2874     gStyle->SetFillColor(kWhite);
2875     gStyle->SetOptTitle(0);
2876     if ((!ratio) && drawStatBox) {
2877       gStyle->SetOptStat(10);
2878       gStyle->SetOptFit(10);
2879     } else {
2880       gStyle->SetOptStat(0);
2881       gStyle->SetOptFit(0);
2882     }
2883     int colors[7] = {1, 6, 4, 7, 2, 9, 3};
2884     int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
2885     int nbin = etamax - etamin + 1;
2886     std::vector<TH1D*> hists;
2887     std::vector<int> entries, htype;
2888     std::vector<double> fitr;
2889     char name[100];
2890     double dy(0);
2891     int fits(0);
2892     int nline(0);
2893     if (ratio) {
2894       for (int ih = 1; ih < nfile; ++ih) {
2895         sprintf(name, "h%dd%d", ih, depth);
2896         TObject* ob = gROOT->FindObject(name);
2897         if (ob)
2898           ob->Delete();
2899         TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
2900         double sumNum(0), sumDen(0), chi2(0);
2901         int npt(0);
2902         std::map<int, cfactors>::const_iterator ktr = cfacs[ih].begin();
2903         for (std::map<int, cfactors>::const_iterator itr = cfacs[0].begin(); itr != cfacs[0].end(); ++itr, ++ktr) {
2904           int dep = (itr->second).depth;
2905           if (dep == depth) {
2906             int ieta = (itr->second).ieta;
2907             int bin = ieta - etamin + 1;
2908             float val = (itr->second).corrf / (ktr->second).corrf;
2909             float dvl =
2910                 val * sqrt((((itr->second).dcorr * (itr->second).dcorr) / ((itr->second).corrf * (itr->second).corrf)) +
2911                            (((ktr->second).dcorr * (ktr->second).dcorr) / ((ktr->second).corrf * (ktr->second).corrf)));
2912             h->SetBinContent(bin, val);
2913             h->SetBinError(bin, dvl);
2914             sumNum += (val / (dvl * dvl));
2915             sumDen += (1.0 / (dvl * dvl));
2916             ++npt;
2917             chi2 += (((val - 1.0) / dvl) * ((val - 1.0) / dvl));
2918           }
2919         }
2920         double fit = (sumDen > 0) ? (sumNum / sumDen) : 1.0;
2921         std::cout << "Depth = " << depth << " Fit to Pol0: " << fit << " chi2 = " << chi2 << "/" << npt << std::endl;
2922         h->SetLineColor(colors[ih]);
2923         h->SetMarkerColor(colors[ih]);
2924         h->SetMarkerStyle(mtype[depth - 1]);
2925         h->SetMarkerSize(0.9);
2926         h->GetXaxis()->SetTitle("i#eta");
2927         sprintf(name, "CF_{%s}/CF_{%s}", texts[0].c_str(), texts[1].c_str());
2928         h->GetYaxis()->SetTitle(name);
2929         h->GetYaxis()->SetLabelOffset(0.005);
2930         h->GetYaxis()->SetTitleSize(0.036);
2931         h->GetYaxis()->SetTitleOffset(1.20);
2932         h->GetYaxis()->SetRangeUser(0.80, 1.20);
2933         hists.push_back(h);
2934         fitr.push_back(fit);
2935         htype.push_back(ih);
2936         ++nline;
2937       }
2938     } else {
2939       for (int k1 = 0; k1 < nfile; ++k1) {
2940         sprintf(name, "h%dd%d", k1, depth);
2941         TObject* ob = gROOT->FindObject(name);
2942         if (ob)
2943           ob->Delete();
2944         TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
2945         int nent(0);
2946         for (std::map<int, cfactors>::const_iterator itr = cfacs[k1].begin(); itr != cfacs[k1].end(); ++itr) {
2947           int dep = (itr->second).depth;
2948           if (dep == depth) {
2949             int ieta = (itr->second).ieta;
2950             int bin = ieta - etamin + 1;
2951             float val = (itr->second).corrf;
2952             float dvl = (itr->second).dcorr;
2953             h->SetBinContent(bin, val);
2954             h->SetBinError(bin, dvl);
2955             nent++;
2956           }
2957         }
2958         if (nent > nmin) {
2959           fits++;
2960           if (drawStatBox)
2961             dy += 0.025;
2962           sprintf(name, "h%ddf%d", k1, depth);
2963           TObject* ob = gROOT->FindObject(name);
2964           if (ob)
2965             ob->Delete();
2966           TF1* func = new TF1(name, "pol0", etamin, etamax);
2967           h->Fit(func, "+QWLR", "");
2968         }
2969         h->SetLineColor(colors[k1]);
2970         h->SetMarkerColor(colors[k1]);
2971         h->SetMarkerStyle(mtype[depth - 1]);
2972         h->SetMarkerSize(0.9);
2973         h->GetXaxis()->SetTitle("i#eta");
2974         h->GetYaxis()->SetTitle("Correction Factor");
2975         h->GetYaxis()->SetLabelOffset(0.005);
2976         h->GetYaxis()->SetTitleOffset(1.20);
2977         h->GetYaxis()->SetRangeUser(0.8, 1.2);
2978         hists.push_back(h);
2979         entries.push_back(nent);
2980         if (drawStatBox)
2981           dy += 0.025;
2982         htype.push_back(k1);
2983       }
2984       ++nline;
2985     }
2986     if (ratio)
2987       sprintf(name, "c_Corr%sD%dRatio", prefixF.c_str(), depth);
2988     else
2989       sprintf(name, "c_Corr%sD%d", prefixF.c_str(), depth);
2990     TCanvas* pad = new TCanvas(name, name, 700, 500);
2991     pad->SetRightMargin(0.10);
2992     pad->SetTopMargin(0.10);
2993     double yh = 0.90;
2994     double yl = yh - 0.035 * hists.size() - dy - 0.01;
2995     TLegend* legend = new TLegend(0.45, yl, 0.90, yl + 0.035 * nline);
2996     legend->SetFillColor(kWhite);
2997     for (unsigned int k = 0; k < hists.size(); ++k) {
2998       if (k == 0)
2999         hists[k]->Draw("");
3000       else
3001         hists[k]->Draw("sames");
3002       pad->Update();
3003       int k1 = htype[k];
3004       if (!ratio) {
3005         TPaveStats* st1 = (TPaveStats*)hists[k]->GetListOfFunctions()->FindObject("stats");
3006         if (st1 != nullptr) {
3007           dy = (entries[k] > nmin) ? 0.05 : 0.025;
3008           st1->SetLineColor(colors[k1]);
3009           st1->SetTextColor(colors[k1]);
3010           st1->SetY1NDC(yh - dy);
3011           st1->SetY2NDC(yh);
3012           st1->SetX1NDC(0.70);
3013           st1->SetX2NDC(0.90);
3014           yh -= dy;
3015         }
3016         sprintf(name, "Depth %d (%s)", depth, texts[k1].c_str());
3017       } else {
3018         sprintf(name, "Depth %d (Mean[CF_{%s}/CF_{%s}] = %5.3f)", depth, text1.c_str(), texts[k1].c_str(), fitr[k]);
3019       }
3020       legend->AddEntry(hists[k], name, "lp");
3021     }
3022     legend->Draw("same");
3023     TPaveText* txt0 = new TPaveText(0.11, 0.84, 0.45, 0.89, "blNDC");
3024     txt0->SetFillColor(0);
3025     char txt[40];
3026     if (isRealData)
3027       sprintf(txt, "CMS Preliminary (%s)", year);
3028     else
3029       sprintf(txt, "CMS Simulation Preliminary (%s)", year);
3030     txt0->AddText(txt);
3031     txt0->Draw("same");
3032     pad->Update();
3033     if (fits < 1) {
3034       double xmin = hists[0]->GetBinLowEdge(1);
3035       int nbin = hists[0]->GetNbinsX();
3036       double xmax = hists[0]->GetBinLowEdge(nbin) + hists[0]->GetBinWidth(nbin);
3037       TLine* line = new TLine(xmin, 1.0, xmax, 1.0);
3038       line->SetLineColor(9);
3039       line->SetLineWidth(2);
3040       line->SetLineStyle(2);
3041       line->Draw("same");
3042       pad->Update();
3043     }
3044     if (save > 0) {
3045       sprintf(name, "%s.pdf", pad->GetName());
3046       pad->Print(name);
3047     } else if (save < 0) {
3048       sprintf(name, "%s.C", pad->GetName());
3049       pad->Print(name);
3050     }
3051   }
3052 }
3053 
3054 void PlotHistCorrDFactors(char* infile1,
3055                           std::string text1,
3056                           char* infile2,
3057                           std::string text2,
3058                           char* infile3,
3059                           std::string text3,
3060                           char* infile4,
3061                           std::string text4,
3062                           char* infile5,
3063                           std::string text5,
3064                           int depth,
3065                           std::string prefixF,
3066                           bool ratio = true,
3067                           bool drawStatBox = false,
3068                           int nmin = 100,
3069                           bool isRealData = true,
3070                           const char* year = "2024",
3071                           int iformat = 0,
3072                           int save = 0) {
3073   std::map<int, cfactors> cfacs[5];
3074   std::vector<std::string> texts;
3075   int nfile(0), etamin(100), etamax(-100), maxdepth(0);
3076   const char* blank("");
3077   if (infile1 != blank) {
3078     readCorrFactors(infile1, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
3079     if (cfacs[nfile].size() > 0) {
3080       texts.push_back(text1);
3081       ++nfile;
3082     }
3083   }
3084   if (infile2 != blank) {
3085     readCorrFactors(infile2, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
3086     if (cfacs[nfile].size() > 0) {
3087       texts.push_back(text2);
3088       ++nfile;
3089     }
3090   }
3091   if (infile3 != blank) {
3092     readCorrFactors(infile3, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
3093     if (cfacs[nfile].size() > 0) {
3094       texts.push_back(text3);
3095       ++nfile;
3096     }
3097   }
3098   if (infile4 != blank) {
3099     readCorrFactors(infile4, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
3100     if (cfacs[nfile].size() > 0) {
3101       texts.push_back(text4);
3102       ++nfile;
3103     }
3104   }
3105   if (infile5 != blank) {
3106     readCorrFactors(infile5, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
3107     if (cfacs[nfile].size() > 0) {
3108       texts.push_back(text5);
3109       ++nfile;
3110     }
3111   }
3112 
3113   if (nfile > 0) {
3114     gStyle->SetCanvasBorderMode(0);
3115     gStyle->SetCanvasColor(kWhite);
3116     gStyle->SetPadColor(kWhite);
3117     gStyle->SetFillColor(kWhite);
3118     gStyle->SetOptTitle(0);
3119     if ((!ratio) && drawStatBox) {
3120       gStyle->SetOptStat(10);
3121       gStyle->SetOptFit(10);
3122     } else {
3123       gStyle->SetOptStat(0);
3124       gStyle->SetOptFit(0);
3125     }
3126     int colors[7] = {1, 6, 4, 7, 2, 9, 3};
3127     int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
3128     int nbin = etamax - etamin + 1;
3129     std::vector<TH1D*> hists;
3130     std::vector<int> entries, htype;
3131     std::vector<double> fitr;
3132     char name[100];
3133     double dy(0);
3134     int fits(0);
3135     int nline(0);
3136     if (ratio) {
3137       for (int ih = 1; ih < nfile; ++ih) {
3138         sprintf(name, "h%dd%d", ih, depth);
3139         TObject* ob = gROOT->FindObject(name);
3140         if (ob)
3141           ob->Delete();
3142         TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
3143         double sumNum(0), sumDen(0), chi2(0);
3144         int npt(0);
3145         std::map<int, cfactors>::const_iterator ktr = cfacs[ih].begin();
3146         for (std::map<int, cfactors>::const_iterator itr = cfacs[0].begin(); itr != cfacs[0].end(); ++itr, ++ktr) {
3147           int dep = (itr->second).depth;
3148           if (dep == depth) {
3149             int ieta = (itr->second).ieta;
3150             int bin = ieta - etamin + 1;
3151             float val = (itr->second).corrf / (ktr->second).corrf;
3152             float dvl =
3153                 val * sqrt((((itr->second).dcorr * (itr->second).dcorr) / ((itr->second).corrf * (itr->second).corrf)) +
3154                            (((ktr->second).dcorr * (ktr->second).dcorr) / ((ktr->second).corrf * (ktr->second).corrf)));
3155             h->SetBinContent(bin, val);
3156             h->SetBinError(bin, dvl);
3157             sumNum += (val / (dvl * dvl));
3158             sumDen += (1.0 / (dvl * dvl));
3159             ++npt;
3160             chi2 += (((val - 1.0) / dvl) * ((val - 1.0) / dvl));
3161           }
3162         }
3163         double fit = (sumDen > 0) ? (sumNum / sumDen) : 1.0;
3164         std::cout << texts[ih] << " Depth = " << depth << " Fit to Pol0: " << fit << " chi2 = " << chi2 << "/" << npt
3165                   << std::endl;
3166         h->SetLineColor(colors[ih]);
3167         h->SetMarkerColor(colors[ih]);
3168         h->SetMarkerStyle(mtype[depth - 1]);
3169         h->SetMarkerSize(0.9);
3170         h->GetXaxis()->SetTitle("i#eta");
3171         if (nfile > 2)
3172           sprintf(name, "CF_{%s}/CF_{Set}", texts[0].c_str());
3173         else
3174           sprintf(name, "CF_{%s}/CF_{%s}", texts[0].c_str(), texts[ih].c_str());
3175         h->GetYaxis()->SetTitle(name);
3176         h->GetYaxis()->SetLabelOffset(0.005);
3177         h->GetYaxis()->SetTitleSize(0.036);
3178         h->GetYaxis()->SetTitleOffset(1.20);
3179         h->GetYaxis()->SetRangeUser(0.80, 1.20);
3180         hists.push_back(h);
3181         fitr.push_back(fit);
3182         htype.push_back(ih);
3183         ++nline;
3184       }
3185     } else {
3186       for (int k1 = 0; k1 < nfile; ++k1) {
3187         sprintf(name, "h%dd%d", k1, depth);
3188         TObject* ob = gROOT->FindObject(name);
3189         if (ob)
3190           ob->Delete();
3191         TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
3192         int nent(0);
3193         for (std::map<int, cfactors>::const_iterator itr = cfacs[k1].begin(); itr != cfacs[k1].end(); ++itr) {
3194           int dep = (itr->second).depth;
3195           if (dep == depth) {
3196             int ieta = (itr->second).ieta;
3197             int bin = ieta - etamin + 1;
3198             float val = (itr->second).corrf;
3199             float dvl = (itr->second).dcorr;
3200             h->SetBinContent(bin, val);
3201             h->SetBinError(bin, dvl);
3202             nent++;
3203           }
3204         }
3205         if (nent > nmin) {
3206           fits++;
3207           if (drawStatBox)
3208             dy += 0.025;
3209           sprintf(name, "h%ddf%d", k1, depth);
3210           TObject* ob = gROOT->FindObject(name);
3211           if (ob)
3212             ob->Delete();
3213           TF1* func = new TF1(name, "pol0", etamin, etamax);
3214           h->Fit(func, "+QWLR", "");
3215         }
3216         h->SetLineColor(colors[k1]);
3217         h->SetMarkerColor(colors[k1]);
3218         h->SetMarkerStyle(mtype[depth - 1]);
3219         h->SetMarkerSize(0.9);
3220         h->GetXaxis()->SetTitle("i#eta");
3221         h->GetYaxis()->SetTitle("Correction Factor");
3222         h->GetYaxis()->SetLabelOffset(0.005);
3223         h->GetYaxis()->SetTitleOffset(1.20);
3224         h->GetYaxis()->SetRangeUser(0.8, 1.2);
3225         hists.push_back(h);
3226         entries.push_back(nent);
3227         if (drawStatBox)
3228           dy += 0.025;
3229         htype.push_back(k1);
3230         ++nline;
3231       }
3232     }
3233     if (ratio)
3234       sprintf(name, "c_Corr%sRatioD%d", prefixF.c_str(), depth);
3235     else
3236       sprintf(name, "c_Corr%sD%d", prefixF.c_str(), depth);
3237     TCanvas* pad = new TCanvas(name, name, 700, 500);
3238     pad->SetRightMargin(0.10);
3239     pad->SetTopMargin(0.10);
3240     double yh = 0.90;
3241     double yl = yh - 0.035 * hists.size() - dy - 0.01;
3242     TLegend* legend = new TLegend(0.45, yl, 0.90, yl + 0.035 * nline);
3243     legend->SetFillColor(kWhite);
3244     for (unsigned int k = 0; k < hists.size(); ++k) {
3245       if (k == 0)
3246         hists[k]->Draw("");
3247       else
3248         hists[k]->Draw("sames");
3249       pad->Update();
3250       int k1 = htype[k];
3251       if (!ratio) {
3252         TPaveStats* st1 = (TPaveStats*)hists[k]->GetListOfFunctions()->FindObject("stats");
3253         if (st1 != nullptr) {
3254           dy = (entries[k] > nmin) ? 0.05 : 0.025;
3255           st1->SetLineColor(colors[k1]);
3256           st1->SetTextColor(colors[k1]);
3257           st1->SetY1NDC(yh - dy);
3258           st1->SetY2NDC(yh);
3259           st1->SetX1NDC(0.70);
3260           st1->SetX2NDC(0.90);
3261           yh -= dy;
3262         }
3263         sprintf(name, "Depth %d (%s)", depth, texts[k1].c_str());
3264       } else {
3265         sprintf(name, "Depth %d (Mean[CF_{%s}/CF_{%s}] = %5.3f)", depth, text1.c_str(), texts[k1].c_str(), fitr[k]);
3266       }
3267       legend->AddEntry(hists[k], name, "lp");
3268     }
3269     legend->Draw("same");
3270     TPaveText* txt0 = new TPaveText(0.11, 0.84, 0.45, 0.89, "blNDC");
3271     txt0->SetFillColor(0);
3272     char txt[40];
3273     if (isRealData)
3274       sprintf(txt, "CMS Preliminary (%s)", year);
3275     else
3276       sprintf(txt, "CMS Simulation Preliminary (%s)", year);
3277     txt0->AddText(txt);
3278     txt0->Draw("same");
3279     pad->Update();
3280     if (fits < 1) {
3281       double xmin = hists[0]->GetBinLowEdge(1);
3282       int nbin = hists[0]->GetNbinsX();
3283       double xmax = hists[0]->GetBinLowEdge(nbin) + hists[0]->GetBinWidth(nbin);
3284       TLine* line = new TLine(xmin, 1.0, xmax, 1.0);
3285       line->SetLineColor(9);
3286       line->SetLineWidth(2);
3287       line->SetLineStyle(2);
3288       line->Draw("same");
3289       pad->Update();
3290     }
3291     if (save > 0) {
3292       sprintf(name, "%s.pdf", pad->GetName());
3293       pad->Print(name);
3294     } else if (save < 0) {
3295       sprintf(name, "%s.C", pad->GetName());
3296       pad->Print(name);
3297     }
3298   }
3299 }
3300 
3301 void PlotHistCorrSys(std::string infilec, int conds, std::string text, int save = 0) {
3302   char fname[100];
3303   int iformat(0);
3304   sprintf(fname, "%s_cond0.txt", infilec.c_str());
3305   int etamin(100), etamax(-100), maxdepth(0);
3306   std::map<int, cfactors> cfacs;
3307   readCorrFactors(fname, 1.0, cfacs, etamin, etamax, maxdepth, iformat);
3308   // There are good records from the master file
3309   if (cfacs.size() > 0) {
3310     // Now read the other files
3311     std::map<int, cfactors> errfacs;
3312     for (int i = 0; i < conds; ++i) {
3313       sprintf(fname, "%s_cond%d.txt", infilec.c_str(), i + 1);
3314       std::map<int, cfactors> cfacx;
3315       int etamin1(100), etamax1(-100), maxdepth1(0);
3316       readCorrFactors(fname, 1.0, cfacx, etamin1, etamax1, maxdepth1, iformat);
3317       for (std::map<int, cfactors>::const_iterator itr1 = cfacx.begin(); itr1 != cfacx.end(); ++itr1) {
3318         std::map<int, cfactors>::iterator itr2 = errfacs.find(itr1->first);
3319         float corrf = (itr1->second).corrf;
3320         if (itr2 == errfacs.end()) {
3321           errfacs[itr1->first] = cfactors(1, 0, corrf, corrf * corrf);
3322         } else {
3323           int nent = (itr2->second).ieta + 1;
3324           float c1 = (itr2->second).corrf + corrf;
3325           float c2 = (itr2->second).dcorr + (corrf * corrf);
3326           errfacs[itr1->first] = cfactors(nent, 0, c1, c2);
3327         }
3328       }
3329     }
3330     // find the RMS from the distributions
3331     for (std::map<int, cfactors>::iterator itr = errfacs.begin(); itr != errfacs.end(); ++itr) {
3332       int nent = (itr->second).ieta;
3333       float mean = (itr->second).corrf / nent;
3334       float rms2 = (itr->second).dcorr / nent - (mean * mean);
3335       float rms = rms2 > 0 ? sqrt(rms2) : 0;
3336       errfacs[itr->first] = cfactors(nent, 0, mean, rms);
3337     }
3338     // Now combine the errors and plot
3339     int k(0);
3340     gStyle->SetCanvasBorderMode(0);
3341     gStyle->SetCanvasColor(kWhite);
3342     gStyle->SetPadColor(kWhite);
3343     gStyle->SetFillColor(kWhite);
3344     gStyle->SetOptTitle(0);
3345     gStyle->SetOptStat(10);
3346     gStyle->SetOptFit(10);
3347     int colors[7] = {1, 6, 4, 7, 2, 9, 3};
3348     int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
3349     std::vector<TH1D*> hists;
3350     char name[100];
3351     int nbin = etamax - etamin + 1;
3352     for (int j = 0; j < maxdepth; ++j) {
3353       sprintf(name, "hd%d", j + 1);
3354       TObject* ob = gROOT->FindObject(name);
3355       if (ob)
3356         ob->Delete();
3357       TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
3358       h->SetLineColor(colors[j]);
3359       h->SetMarkerColor(colors[j]);
3360       h->SetMarkerStyle(mtype[j]);
3361       h->GetXaxis()->SetTitle("i#eta");
3362       h->GetYaxis()->SetTitle("Correction Factor");
3363       h->GetYaxis()->SetLabelOffset(0.005);
3364       h->GetYaxis()->SetTitleOffset(1.20);
3365       h->GetYaxis()->SetRangeUser(0.0, 2.0);
3366       hists.push_back(h);
3367     }
3368     for (std::map<int, cfactors>::iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr, ++k) {
3369       std::map<int, cfactors>::iterator itr2 = errfacs.find(itr->first);
3370       float mean2 = (itr2 == errfacs.end()) ? 0 : (itr2->second).corrf;
3371       float ersys = (itr2 == errfacs.end()) ? 0 : (itr2->second).dcorr;
3372       float erstt = (itr->second).dcorr;
3373       float ertot = sqrt(erstt * erstt + ersys * ersys);
3374       float mean = (itr->second).corrf;
3375       int ieta = (itr->second).ieta;
3376       int depth = (itr->second).depth;
3377       std::cout << "[" << k << "] " << ieta << " " << depth << " " << mean << ":" << mean2 << " " << erstt << ":"
3378                 << ersys << ":" << ertot << std::endl;
3379       int bin = ieta - etamin + 1;
3380       hists[depth - 1]->SetBinContent(bin, mean);
3381       hists[depth - 1]->SetBinError(bin, ertot);
3382     }
3383     TCanvas* pad = new TCanvas("CorrFactorSys", "CorrFactorSys", 700, 500);
3384     pad->SetRightMargin(0.10);
3385     pad->SetTopMargin(0.10);
3386     double yh = 0.90;
3387     double yl = yh - 0.050 * hists.size() - 0.01;
3388     TLegend* legend = new TLegend(0.60, yl, 0.90, yl + 0.025 * hists.size());
3389     legend->SetFillColor(kWhite);
3390     for (unsigned int k = 0; k < hists.size(); ++k) {
3391       if (k == 0)
3392         hists[k]->Draw("");
3393       else
3394         hists[k]->Draw("sames");
3395       pad->Update();
3396       TPaveStats* st1 = (TPaveStats*)hists[k]->GetListOfFunctions()->FindObject("stats");
3397       if (st1 != nullptr) {
3398         st1->SetLineColor(colors[k]);
3399         st1->SetTextColor(colors[k]);
3400         st1->SetY1NDC(yh - 0.025);
3401         st1->SetY2NDC(yh);
3402         st1->SetX1NDC(0.70);
3403         st1->SetX2NDC(0.90);
3404         yh -= 0.025;
3405       }
3406       sprintf(name, "Depth %d (%s)", k + 1, text.c_str());
3407       legend->AddEntry(hists[k], name, "lp");
3408     }
3409     legend->Draw("same");
3410     pad->Update();
3411     if (save > 0) {
3412       sprintf(name, "%s.pdf", pad->GetName());
3413       pad->Print(name);
3414     } else if (save < 0) {
3415       sprintf(name, "%s.C", pad->GetName());
3416       pad->Print(name);
3417     }
3418   }
3419 }
3420 
3421 void PlotHistCorrLumis(std::string infilec, int conds, double lumi, int save = 0) {
3422   char fname[100];
3423   int iformat(0);
3424   sprintf(fname, "%s_0.txt", infilec.c_str());
3425   std::map<int, cfactors> cfacs;
3426   int etamin(100), etamax(-100), maxdepth(0);
3427   readCorrFactors(fname, 1.0, cfacs, etamin, etamax, maxdepth, iformat);
3428   int nbin = etamax - etamin + 1;
3429   std::cout << "Max Depth " << maxdepth << " and " << nbin << " eta bins for " << etamin << ":" << etamax << std::endl;
3430 
3431   // There are good records from the master file
3432   int colors[8] = {4, 2, 6, 7, 1, 9, 3, 5};
3433   int mtype[8] = {20, 21, 22, 23, 24, 25, 26, 27};
3434   if (cfacs.size() > 0) {
3435     // Now read the other files
3436     std::vector<TH1D*> hists;
3437     char name[100];
3438     for (int i = 0; i < conds; ++i) {
3439       int ih = (int)(hists.size());
3440       for (int j = 0; j < maxdepth; ++j) {
3441         sprintf(name, "hd%d%d", j + 1, i);
3442         TObject* ob = gROOT->FindObject(name);
3443         if (ob)
3444           ob->Delete();
3445         TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
3446         h->SetLineColor(colors[j]);
3447         h->SetMarkerColor(colors[j]);
3448         h->SetMarkerStyle(mtype[i]);
3449         h->SetMarkerSize(0.9);
3450         h->GetXaxis()->SetTitle("i#eta");
3451         h->GetYaxis()->SetTitle("Fractional Error");
3452         h->GetYaxis()->SetLabelOffset(0.005);
3453         h->GetYaxis()->SetTitleOffset(1.20);
3454         h->GetYaxis()->SetRangeUser(0.0, 0.10);
3455         hists.push_back(h);
3456       }
3457       sprintf(fname, "%s_%d.txt", infilec.c_str(), i);
3458       int etamin1(100), etamax1(-100), maxdepth1(0);
3459       readCorrFactors(fname, 1.0, cfacs, etamin1, etamax1, maxdepth1, iformat);
3460       for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr) {
3461         double value = (itr->second).dcorr / (itr->second).corrf;
3462         int bin = (itr->second).ieta - etamin + 1;
3463         hists[ih + (itr->second).depth - 1]->SetBinContent(bin, value);
3464         hists[ih + (itr->second).depth - 1]->SetBinError(bin, 0.0001);
3465       }
3466     }
3467 
3468     // Now plot the histograms
3469     gStyle->SetCanvasBorderMode(0);
3470     gStyle->SetCanvasColor(kWhite);
3471     gStyle->SetPadColor(kWhite);
3472     gStyle->SetFillColor(kWhite);
3473     gStyle->SetOptTitle(0);
3474     gStyle->SetOptStat(0);
3475     gStyle->SetOptFit(0);
3476     TCanvas* pad = new TCanvas("CorrFactorErr", "CorrFactorErr", 700, 500);
3477     pad->SetRightMargin(0.10);
3478     pad->SetTopMargin(0.10);
3479     double yh(0.89);
3480     TLegend* legend = new TLegend(0.60, yh - 0.04 * conds, 0.89, yh);
3481     legend->SetFillColor(kWhite);
3482     double lumic(lumi);
3483     for (unsigned int k = 0; k < hists.size(); ++k) {
3484       if (k == 0)
3485         hists[k]->Draw("");
3486       else
3487         hists[k]->Draw("sames");
3488       pad->Update();
3489       if (k % maxdepth == 0) {
3490         sprintf(name, "L = %5.2f fb^{-1}", lumic);
3491         legend->AddEntry(hists[k], name, "lp");
3492         lumic *= 0.5;
3493       }
3494     }
3495     legend->Draw("same");
3496     pad->Update();
3497     if (save > 0) {
3498       sprintf(name, "%s.pdf", pad->GetName());
3499       pad->Print(name);
3500     } else if (save < 0) {
3501       sprintf(name, "%s.C", pad->GetName());
3502       pad->Print(name);
3503     }
3504   }
3505 }
3506 
3507 void PlotHistCorrRel(char* infile1,
3508                      char* infile2,
3509                      std::string text1,
3510                      std::string text2,
3511                      int iformat1 = 0,
3512                      int iformat2 = 0,
3513                      int save = 0) {
3514   std::map<int, cfactors> cfacs1, cfacs2;
3515   int etamin(100), etamax(-100), maxdepth(0);
3516   readCorrFactors(infile1, 1.0, cfacs1, etamin, etamax, maxdepth, iformat1);
3517   readCorrFactors(infile2, 1.0, cfacs2, etamin, etamax, maxdepth, iformat2);
3518   std::map<int, std::pair<cfactors, cfactors> > cfacs;
3519   for (std::map<int, cfactors>::iterator itr = cfacs1.begin(); itr != cfacs1.end(); ++itr) {
3520     std::map<int, cfactors>::iterator ktr = cfacs2.find(itr->first);
3521     if (ktr == cfacs2.end()) {
3522       cfactors fac2(((itr->second).ieta), ((itr->second).depth), 0, -1);
3523       cfacs[itr->first] = std::pair<cfactors, cfactors>((itr->second), fac2);
3524     } else {
3525       cfactors fac2(ktr->second);
3526       cfacs[itr->first] = std::pair<cfactors, cfactors>((itr->second), fac2);
3527     }
3528   }
3529   for (std::map<int, cfactors>::iterator itr = cfacs2.begin(); itr != cfacs2.end(); ++itr) {
3530     std::map<int, cfactors>::const_iterator ktr = cfacs1.find(itr->first);
3531     if (ktr == cfacs1.end()) {
3532       cfactors fac1(((itr->second).ieta), ((itr->second).depth), 0, -1);
3533       cfacs[itr->first] = std::pair<cfactors, cfactors>(fac1, (itr->second));
3534     }
3535   }
3536 
3537   // There are good records in bothe the files
3538   if ((cfacs1.size() > 0) && (cfacs2.size() > 0)) {
3539     int k(0);
3540     gStyle->SetCanvasBorderMode(0);
3541     gStyle->SetCanvasColor(kWhite);
3542     gStyle->SetPadColor(kWhite);
3543     gStyle->SetFillColor(kWhite);
3544     gStyle->SetOptTitle(0);
3545     gStyle->SetOptStat(10);
3546     gStyle->SetOptFit(10);
3547     int colors[7] = {1, 6, 4, 7, 2, 9, 3};
3548     int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
3549     std::vector<TH1D*> hists;
3550     char name[100];
3551     int nbin = etamax - etamin + 1;
3552     for (int i = 0; i < 2; ++i) {
3553       for (int j = 0; j < maxdepth; ++j) {
3554         int j1 = (i == 0) ? j : maxdepth + j;
3555         sprintf(name, "hd%d%d", i, j + 1);
3556         TObject* ob = gROOT->FindObject(name);
3557         if (ob)
3558           ob->Delete();
3559         TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
3560         h->SetLineColor(colors[j1]);
3561         h->SetMarkerColor(colors[j1]);
3562         h->SetMarkerStyle(mtype[i]);
3563         h->GetXaxis()->SetTitle("i#eta");
3564         h->GetYaxis()->SetTitle("Correction Factor");
3565         h->GetYaxis()->SetLabelOffset(0.005);
3566         h->GetYaxis()->SetTitleOffset(1.20);
3567         h->GetYaxis()->SetRangeUser(0.0, 2.0);
3568         hists.push_back(h);
3569       }
3570     }
3571     for (std::map<int, std::pair<cfactors, cfactors> >::iterator it = cfacs.begin(); it != cfacs.end(); ++it, ++k) {
3572       float mean1 = (it->second).first.corrf;
3573       float error1 = (it->second).first.dcorr;
3574       float mean2 = (it->second).second.corrf;
3575       float error2 = (it->second).second.dcorr;
3576       int ieta = (it->second).first.ieta;
3577       int depth = (it->second).first.depth;
3578       /*
3579       std::cout << "[" << k << "] " << ieta << " " << depth << " " 
3580         << mean1 << ":" << mean2 << " " << error1 << ":" << error2
3581         << std::endl;
3582       */
3583       int bin = ieta - etamin + 1;
3584       if (error1 >= 0) {
3585         hists[depth - 1]->SetBinContent(bin, mean1);
3586         hists[depth - 1]->SetBinError(bin, error1);
3587       }
3588       if (error2 >= 0) {
3589         hists[maxdepth + depth - 1]->SetBinContent(bin, mean2);
3590         hists[maxdepth + depth - 1]->SetBinError(bin, error2);
3591       }
3592     }
3593     TCanvas* pad = new TCanvas("CorrFactors", "CorrFactors", 700, 500);
3594     pad->SetRightMargin(0.10);
3595     pad->SetTopMargin(0.10);
3596     double yh = 0.90;
3597     double yl = yh - 0.050 * hists.size() - 0.01;
3598     TLegend* legend = new TLegend(0.60, yl, 0.90, yl + 0.025 * hists.size());
3599     legend->SetFillColor(kWhite);
3600     for (unsigned int k = 0; k < hists.size(); ++k) {
3601       if (k == 0)
3602         hists[k]->Draw("");
3603       else
3604         hists[k]->Draw("sames");
3605       pad->Update();
3606       TPaveStats* st1 = (TPaveStats*)hists[k]->GetListOfFunctions()->FindObject("stats");
3607       if (st1 != nullptr) {
3608         st1->SetLineColor(colors[k]);
3609         st1->SetTextColor(colors[k]);
3610         st1->SetY1NDC(yh - 0.025);
3611         st1->SetY2NDC(yh);
3612         st1->SetX1NDC(0.70);
3613         st1->SetX2NDC(0.90);
3614         yh -= 0.025;
3615       }
3616       if (k < (unsigned int)(maxdepth)) {
3617         sprintf(name, "Depth %d (%s)", k + 1, text1.c_str());
3618       } else {
3619         sprintf(name, "Depth %d (%s)", k - maxdepth + 1, text2.c_str());
3620       }
3621       legend->AddEntry(hists[k], name, "lp");
3622     }
3623     legend->Draw("same");
3624     pad->Update();
3625     if (save > 0) {
3626       sprintf(name, "%s.pdf", pad->GetName());
3627       pad->Print(name);
3628     } else if (save < 0) {
3629       sprintf(name, "%s.C", pad->GetName());
3630       pad->Print(name);
3631     }
3632   }
3633 }
3634 
3635 void PlotHistCorrDepth(char* infile1,
3636                        char* infile2,
3637                        std::string text1,
3638                        std::string text2,
3639                        int depth,
3640                        int ietamax,
3641                        int iformat1 = 0,
3642                        int iformat2 = 0,
3643                        int save = 0,
3644                        int debug = 1) {
3645   std::map<int, cfactors> cfacs1, cfacs2;
3646   int etamin(100), etamax(-100), maxdepth(0);
3647   readCorrFactors(infile1, 1.0, cfacs1, etamin, etamax, maxdepth, iformat1, (debug > 1));
3648   readCorrFactors(infile2, 1.0, cfacs2, etamin, etamax, maxdepth, iformat2, (debug > 1));
3649 
3650   double sumNum(0), sumDen(0), ratMax(0);
3651   int npt0(0), npt1(0);
3652   for (std::map<int, cfactors>::iterator itr = cfacs1.begin(); itr != cfacs1.end(); ++itr) {
3653     std::map<int, cfactors>::iterator ktr = cfacs2.find(itr->first);
3654     if ((ktr != cfacs2.end()) && ((itr->second).depth == depth)) {
3655       double er1 = (itr->second).dcorr / (itr->second).corrf;
3656       double er2 = (ktr->second).dcorr / (ktr->second).corrf;
3657       double tmp = (ktr->second).corrf / (itr->second).corrf;
3658       double dtmp = tmp * sqrt(er1 * er1 + er2 * er2);
3659       double rat = (tmp > 1.0) ? 1.0 / tmp : tmp;
3660       double drt = (tmp > 1.0) ? dtmp / (tmp * tmp) : dtmp;
3661       rat = fabs(1.0 - rat);
3662       ratMax = std::max(ratMax, rat);
3663       ++npt0;
3664       if (debug > 0)
3665         std::cout << std::hex << (itr->first) << std::dec << " ieta:depth" << (itr->second).ieta << ":"
3666                   << (itr->second).depth << " Corr " << (itr->second).corrf << ":" << (ktr->second).corrf << " Ratio "
3667                   << rat << ":" << drt << std::endl;
3668       if (std::abs((itr->second).ieta) <= ietamax) {
3669         sumNum += (rat / (drt * drt));
3670         sumDen += (1.0 / (drt * drt));
3671         ++npt1;
3672       }
3673     }
3674   }
3675   sumNum = (sumDen > 0) ? (sumNum / sumDen) : 0;
3676   sumDen = (sumDen > 0) ? 1.0 / sqrt(sumDen) : 0;
3677   std::cout << "Get Ratio of mean for " << npt0 << ":" << npt1 << " points for depth " << depth << " Mean " << sumNum
3678             << " +- " << sumDen << " (Maximum " << ratMax << ")" << std::endl;
3679 
3680   gStyle->SetCanvasBorderMode(0);
3681   gStyle->SetCanvasColor(kWhite);
3682   gStyle->SetPadColor(kWhite);
3683   gStyle->SetFillColor(kWhite);
3684   gStyle->SetOptTitle(0);
3685   gStyle->SetOptStat(0);
3686   gStyle->SetOptFit(0);
3687   int colors[2] = {1, 2};
3688   int mtype[2] = {20, 24};
3689   int nbin = etamax - etamin + 1;
3690   std::vector<TH1D*> hists;
3691   char name[100];
3692   for (int j = 0; j < 2; ++j) {
3693     sprintf(name, "hd%d", (j + 1));
3694     TObject* ob = gROOT->FindObject(name);
3695     if (ob)
3696       ob->Delete();
3697     TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
3698     if (j == 0) {
3699       for (std::map<int, cfactors>::const_iterator itr = cfacs1.begin(); itr != cfacs1.end(); ++itr) {
3700         if ((itr->second).depth == depth) {
3701           int ieta = (itr->second).ieta;
3702           int bin = ieta - etamin + 1;
3703           float val = (itr->second).corrf;
3704           float dvl = (itr->second).dcorr;
3705           h->SetBinContent(bin, val);
3706           h->SetBinError(bin, dvl);
3707         }
3708       }
3709     } else {
3710       for (std::map<int, cfactors>::const_iterator itr = cfacs2.begin(); itr != cfacs2.end(); ++itr) {
3711         if ((itr->second).depth == depth) {
3712           int ieta = (itr->second).ieta;
3713           int bin = ieta - etamin + 1;
3714           float val = (itr->second).corrf;
3715           float dvl = (itr->second).dcorr;
3716           h->SetBinContent(bin, val);
3717           h->SetBinError(bin, dvl);
3718         }
3719       }
3720     }
3721     h->SetLineColor(colors[j]);
3722     h->SetMarkerColor(colors[j]);
3723     h->SetMarkerStyle(mtype[j]);
3724     h->GetXaxis()->SetTitle("i#eta");
3725     h->GetYaxis()->SetTitle("Correction Factor");
3726     h->GetYaxis()->SetLabelOffset(0.005);
3727     h->GetYaxis()->SetTitleOffset(1.20);
3728     h->GetYaxis()->SetRangeUser(0.0, 2.0);
3729     hists.push_back(h);
3730   }
3731   sprintf(name, "c_CorrFactorDepth%d", depth);
3732   TCanvas* pad = new TCanvas(name, name, 700, 500);
3733   pad->SetRightMargin(0.10);
3734   pad->SetTopMargin(0.10);
3735   double yl = 0.25;
3736   TLegend* legend = new TLegend(0.25, yl, 0.85, yl + 0.04 * hists.size());
3737   legend->SetFillColor(kWhite);
3738   for (unsigned int k = 0; k < hists.size(); ++k) {
3739     if (k == 0) {
3740       hists[k]->Draw("");
3741       sprintf(name, "Depth %d (%s)", depth, text1.c_str());
3742     } else {
3743       hists[k]->Draw("sames");
3744       sprintf(name, "Depth %d (%s)", depth, text2.c_str());
3745     }
3746     pad->Update();
3747     legend->AddEntry(hists[k], name, "lp");
3748   }
3749   legend->Draw("same");
3750   pad->Update();
3751   if (save > 0) {
3752     sprintf(name, "%s.pdf", pad->GetName());
3753     pad->Print(name);
3754   } else if (save < 0) {
3755     sprintf(name, "%s.C", pad->GetName());
3756     pad->Print(name);
3757   }
3758 }
3759 
3760 void PlotFourHists(std::string infile,
3761                    std::string prefix0,
3762                    int type = 0,
3763                    int drawStatBox = 0,
3764                    bool normalize = false,
3765                    int save = 0,
3766                    std::string prefix1 = "",
3767                    std::string text1 = "",
3768                    std::string prefix2 = "",
3769                    std::string text2 = "",
3770                    std::string prefix3 = "",
3771                    std::string text3 = "",
3772                    std::string prefix4 = "",
3773                    std::string text4 = "") {
3774   int colors[4] = {2, 4, 6, 1};
3775   std::string names[5] = {"eta03", "eta13", "eta23", "eta33", "eta43"};
3776   std::string xtitle[5] = {"i#eta", "i#eta", "i#eta", "i#eta", "i#eta"};
3777   std::string ytitle[5] = {"Tracks", "Tracks", "Tracks", "Tracks", "Tracks"};
3778   std::string title[5] = {"All Tracks (p = 40:60 GeV)",
3779                           "Good Quality Tracks (p = 40:60 GeV)",
3780                           "Selected Tracks (p = 40:60 GeV)",
3781                           "Isolated Tracks (p = 40:60 GeV)",
3782                           "Isolated MIP Tracks (p = 40:60 GeV)"};
3783 
3784   gStyle->SetCanvasBorderMode(0);
3785   gStyle->SetCanvasColor(kWhite);
3786   gStyle->SetPadColor(kWhite);
3787   gStyle->SetFillColor(kWhite);
3788   gStyle->SetOptTitle(0);
3789   gStyle->SetOptFit(0);
3790   if (drawStatBox == 0)
3791     gStyle->SetOptStat(0);
3792   else
3793     gStyle->SetOptStat(1110);
3794 
3795   if (type < 0 || type > 4)
3796     type = 0;
3797   char name[100], namep[100];
3798   TFile* file = new TFile(infile.c_str());
3799 
3800   std::vector<TH1D*> hists;
3801   std::vector<int> kks;
3802   std::vector<std::string> texts;
3803   for (int k = 0; k < 4; ++k) {
3804     std::string prefix, text;
3805     if (k == 0) {
3806       prefix = prefix1;
3807       text = text1;
3808     } else if (k == 1) {
3809       prefix = prefix2;
3810       text = text2;
3811     } else if (k == 2) {
3812       prefix = prefix3;
3813       text = text3;
3814     } else {
3815       prefix = prefix4;
3816       text = text4;
3817     }
3818     if (prefix != "") {
3819       sprintf(name, "%s%s", prefix.c_str(), names[type].c_str());
3820       TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
3821       if (hist1 != nullptr) {
3822         hists.push_back((TH1D*)(hist1->Clone()));
3823         kks.push_back(k);
3824         texts.push_back(text);
3825       }
3826     }
3827   }
3828   if (hists.size() > 0) {
3829     sprintf(namep, "c_%s%s", prefix0.c_str(), names[type].c_str());
3830     double ymax(0.90), dy(0.13);
3831     double ymx0 = (drawStatBox == 0) ? (ymax - .01) : (ymax - dy * hists.size() - .01);
3832     TCanvas* pad = new TCanvas(namep, namep, 700, 500);
3833     TLegend* legend = new TLegend(0.64, ymx0 - 0.05 * hists.size(), 0.89, ymx0);
3834     legend->SetFillColor(kWhite);
3835     pad->SetRightMargin(0.10);
3836     pad->SetTopMargin(0.10);
3837     for (unsigned int jk = 0; jk < hists.size(); ++jk) {
3838       int k = kks[jk];
3839       hists[jk]->GetXaxis()->SetTitleSize(0.040);
3840       hists[jk]->GetXaxis()->SetTitle(xtitle[type].c_str());
3841       hists[jk]->GetYaxis()->SetTitle(ytitle[type].c_str());
3842       hists[jk]->GetYaxis()->SetLabelOffset(0.005);
3843       hists[jk]->GetYaxis()->SetLabelSize(0.035);
3844       hists[jk]->GetYaxis()->SetTitleSize(0.040);
3845       hists[jk]->GetYaxis()->SetTitleOffset(1.15);
3846       hists[jk]->SetMarkerStyle(20);
3847       hists[jk]->SetMarkerColor(colors[k]);
3848       hists[jk]->SetLineColor(colors[k]);
3849       if (normalize) {
3850         if (jk == 0)
3851           hists[jk]->DrawNormalized();
3852         else
3853           hists[jk]->DrawNormalized("sames");
3854       } else {
3855         if (jk == 0)
3856           hists[jk]->Draw();
3857         else
3858           hists[jk]->Draw("sames");
3859       }
3860       pad->Update();
3861       TPaveStats* st1 = (TPaveStats*)hists[jk]->GetListOfFunctions()->FindObject("stats");
3862       if (st1 != nullptr) {
3863         double ymin = ymax - dy;
3864         st1->SetLineColor(colors[k]);
3865         st1->SetTextColor(colors[k]);
3866         st1->SetY1NDC(ymin);
3867         st1->SetY2NDC(ymax);
3868         st1->SetX1NDC(0.70);
3869         st1->SetX2NDC(0.90);
3870         ymax = ymin;
3871       }
3872       sprintf(name, "%s", texts[jk].c_str());
3873       legend->AddEntry(hists[jk], name, "lp");
3874     }
3875     legend->Draw("same");
3876     pad->Update();
3877     TPaveText* txt1 = new TPaveText(0.10, 0.905, 0.80, 0.95, "blNDC");
3878     txt1->SetFillColor(0);
3879     char txt[100];
3880     sprintf(txt, "%s", title[type].c_str());
3881     txt1->AddText(txt);
3882     txt1->Draw("same");
3883     /*
3884     TPaveText *txt2 = new TPaveText(0.11,0.825,0.33,0.895,"blNDC");
3885     txt2->SetFillColor(0);
3886     sprintf (txt, "CMS Preliminary");
3887     txt2->AddText(txt);
3888     txt2->Draw("same");
3889     */
3890     pad->Modified();
3891     pad->Update();
3892     if (save > 0) {
3893       sprintf(name, "%s.pdf", pad->GetName());
3894       pad->Print(name);
3895     } else if (save < 0) {
3896       sprintf(name, "%s.C", pad->GetName());
3897       pad->Print(name);
3898     }
3899   }
3900 }
3901 
3902 void PlotPUCorrHists(std::string infile = "corrfac.root",
3903                      std::string prefix = "",
3904                      int drawStatBox = 0,
3905                      bool approve = true,
3906                      int save = 0) {
3907   std::string name1[4] = {"W0", "W1", "W2", "P"};
3908   std::string name2[4] = {"All", "Barrel", "Endcap", ""};
3909   std::string name3[2] = {"", "p = 40:60 GeV"};
3910   std::string name4[2] = {"Loose Isolation", "Tight Isolation"};
3911   std::string xtitle[4] = {"Correction Factor", "Correction Factor", "Correction Factor", "i#eta"};
3912   std::string ytitle[4] = {"Tracks", "Tracks", "Tracks", "Correction Factor"};
3913 
3914   gStyle->SetCanvasBorderMode(0);
3915   gStyle->SetCanvasColor(kWhite);
3916   gStyle->SetPadColor(kWhite);
3917   gStyle->SetFillColor(kWhite);
3918   gStyle->SetOptTitle(0);
3919   gStyle->SetOptFit(0);
3920   if (drawStatBox == 0)
3921     gStyle->SetOptStat(0);
3922   else
3923     gStyle->SetOptStat(1110);
3924 
3925   char name[100], namep[100], title[100];
3926   TFile* file = new TFile(infile.c_str());
3927 
3928   if (file != nullptr) {
3929     for (int i1 = 0; i1 < 4; ++i1) {
3930       for (int i2 = 0; i2 < 2; ++i2) {
3931         for (int i3 = 0; i3 < 2; ++i3) {
3932           sprintf(name, "%s%d%d", name1[i1].c_str(), i2, i3);
3933           if (i2 == 0)
3934             sprintf(title, "%s Tracks Selected with %s", name2[i1].c_str(), name4[i3].c_str());
3935           else
3936             sprintf(title, "%s Tracks Selected with %s (%s)", name2[i1].c_str(), name4[i3].c_str(), name3[i2].c_str());
3937           TH1D* hist1(nullptr);
3938           TProfile* hist2(nullptr);
3939           if (i1 != 3) {
3940             TH1D* hist = (TH1D*)file->FindObjectAny(name);
3941             if (hist != nullptr) {
3942               hist1 = (TH1D*)(hist->Clone());
3943               hist1->GetXaxis()->SetTitleSize(0.040);
3944               hist1->GetXaxis()->SetTitle(xtitle[i1].c_str());
3945               hist1->GetYaxis()->SetTitle(ytitle[i1].c_str());
3946               hist1->GetYaxis()->SetLabelOffset(0.005);
3947               hist1->GetYaxis()->SetLabelSize(0.035);
3948               hist1->GetYaxis()->SetTitleSize(0.040);
3949               hist1->GetYaxis()->SetTitleOffset(1.15);
3950             }
3951           } else {
3952             TProfile* hist = (TProfile*)file->FindObjectAny(name);
3953             if (hist != nullptr) {
3954               hist2 = (TProfile*)(hist->Clone());
3955               hist2->GetXaxis()->SetTitleSize(0.040);
3956               hist2->GetXaxis()->SetTitle(xtitle[i1].c_str());
3957               hist2->GetYaxis()->SetTitle(ytitle[i1].c_str());
3958               hist2->GetYaxis()->SetLabelOffset(0.005);
3959               hist2->GetYaxis()->SetLabelSize(0.035);
3960               hist2->GetYaxis()->SetTitleSize(0.040);
3961               hist2->GetYaxis()->SetTitleOffset(1.15);
3962               //          hist2->GetYaxis()->SetRangeUser(0.0, 1.5);
3963               hist2->SetMarkerStyle(20);
3964             }
3965           }
3966           if ((hist1 != nullptr) || (hist2 != nullptr)) {
3967             sprintf(namep, "c_%s%s", name, prefix.c_str());
3968             TCanvas* pad = new TCanvas(namep, namep, 700, 500);
3969             pad->SetRightMargin(0.10);
3970             pad->SetTopMargin(0.10);
3971             if (hist1 != nullptr) {
3972               pad->SetLogy();
3973               hist1->Draw();
3974               pad->Update();
3975               TPaveStats* st1 = (TPaveStats*)hist1->GetListOfFunctions()->FindObject("stats");
3976               if (st1 != nullptr) {
3977                 st1->SetY1NDC(0.77);
3978                 st1->SetY2NDC(0.90);
3979                 st1->SetX1NDC(0.70);
3980                 st1->SetX2NDC(0.90);
3981               }
3982             } else {
3983               hist2->Draw();
3984               pad->Update();
3985             }
3986             TPaveText* txt1 = new TPaveText(0.10, 0.905, 0.80, 0.95, "blNDC");
3987             txt1->SetFillColor(0);
3988             char txt[100];
3989             sprintf(txt, "%s", title);
3990             txt1->AddText(txt);
3991             txt1->Draw("same");
3992             if (approve) {
3993               double xoff = (i1 == 3) ? 0.11 : 0.22;
3994               TPaveText* txt2 = new TPaveText(xoff, 0.825, xoff + 0.22, 0.895, "blNDC");
3995               txt2->SetFillColor(0);
3996               sprintf(txt, "CMS Preliminary");
3997               txt2->AddText(txt);
3998               txt2->Draw("same");
3999             }
4000             pad->Modified();
4001             pad->Update();
4002             if (save > 0) {
4003               sprintf(name, "%s.pdf", pad->GetName());
4004               pad->Print(name);
4005             } else if (save < 0) {
4006               sprintf(name, "%s.C", pad->GetName());
4007               pad->Print(name);
4008             }
4009           }
4010         }
4011       }
4012     }
4013   }
4014 }
4015 
4016 void PlotHistCorr(const char* infile,
4017                   std::string prefix,
4018                   std::string text0,
4019                   int eta = 0,
4020                   int mode = 1,
4021                   bool drawStatBox = true,
4022                   int save = 0) {
4023   gStyle->SetCanvasBorderMode(0);
4024   gStyle->SetCanvasColor(kWhite);
4025   gStyle->SetPadColor(kWhite);
4026   gStyle->SetFillColor(kWhite);
4027   gStyle->SetOptTitle(0);
4028   if (drawStatBox)
4029     gStyle->SetOptStat(1100);
4030   else
4031     gStyle->SetOptStat(0);
4032 
4033   std::string tags[3] = {"UnNoPU", "UnPU", "Cor"};
4034   std::string text[3] = {"Uncorrected no PU", "Uncorrected PU", "Corrected PU"};
4035   int colors[3] = {1, 4, 2};
4036   int styles[3] = {1, 3, 2};
4037   TFile* file = new TFile(infile);
4038   if (mode < 0 || mode > 2)
4039     mode = 1;
4040   int etamin = (eta == 0) ? -27 : eta;
4041   int etamax = (eta == 0) ? 27 : eta;
4042   for (int ieta = etamin; ieta <= etamax; ++ieta) {
4043     char name[20];
4044     double yh(0.90), dy(0.09);
4045     double yh1 = drawStatBox ? (yh - 3 * dy - 0.01) : (yh - 0.01);
4046     TLegend* legend = new TLegend(0.55, yh1 - 0.15, 0.89, yh1);
4047     legend->SetFillColor(kWhite);
4048     sprintf(name, "c_%sEovp%d", prefix.c_str(), ieta);
4049     TCanvas* pad = new TCanvas(name, name, 700, 500);
4050     pad->SetRightMargin(0.10);
4051     pad->SetTopMargin(0.10);
4052     TH1D* hist[3];
4053     double ymax(0);
4054     for (int k = 0; k < 3; ++k) {
4055       if (k < 2)
4056         sprintf(name, "EovP_ieta%d%s", ieta, tags[k].c_str());
4057       else
4058         sprintf(name, "EovP_ieta%dCor%dPU", ieta, mode);
4059       TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
4060       if (hist1 != nullptr) {
4061         hist[k] = (TH1D*)(hist1->Clone());
4062         ymax = std::max(ymax, (hist1->GetMaximum()));
4063       }
4064     }
4065     int imax = 10 * (2 + int(0.1 * ymax));
4066     for (int k = 0; k < 3; ++k) {
4067       hist[k]->GetYaxis()->SetLabelOffset(0.005);
4068       hist[k]->GetYaxis()->SetTitleOffset(1.20);
4069       hist[k]->GetXaxis()->SetTitle("E/p");
4070       hist[k]->GetYaxis()->SetTitle("Tracks");
4071       hist[k]->SetLineColor(colors[k]);
4072       hist[k]->SetLineStyle(styles[k]);
4073       hist[k]->GetYaxis()->SetRangeUser(0.0, imax);
4074       if (k == 0)
4075         hist[k]->Draw();
4076       else
4077         hist[k]->Draw("sames");
4078       legend->AddEntry(hist[k], text[k].c_str(), "lp");
4079       pad->Update();
4080       if (drawStatBox) {
4081         TPaveStats* st1 = (TPaveStats*)hist[k]->GetListOfFunctions()->FindObject("stats");
4082         if (st1 != nullptr) {
4083           st1->SetLineColor(colors[k]);
4084           st1->SetTextColor(colors[k]);
4085           st1->SetY1NDC(yh - dy);
4086           st1->SetY2NDC(yh);
4087           st1->SetX1NDC(0.70);
4088           st1->SetX2NDC(0.90);
4089           yh -= dy;
4090         }
4091       }
4092     }
4093     pad->Update();
4094     legend->Draw("same");
4095     pad->Update();
4096     TPaveText* txt1 = new TPaveText(0.10, 0.905, 0.80, 0.95, "blNDC");
4097     txt1->SetFillColor(0);
4098     char title[100];
4099     sprintf(title, "%s for i#eta = %d", text0.c_str(), ieta);
4100     txt1->AddText(title);
4101     txt1->Draw("same");
4102     pad->Modified();
4103     pad->Update();
4104     if (save > 0) {
4105       sprintf(name, "%s.pdf", pad->GetName());
4106       pad->Print(name);
4107     } else if (save < 0) {
4108       sprintf(name, "%s.C", pad->GetName());
4109       pad->Print(name);
4110     }
4111   }
4112 }
4113 
4114 void PlotPropertyHist(const char* infile,
4115                       std::string prefix,
4116                       std::string text,
4117                       int etaMax = 25,
4118                       double lumi = 0,
4119                       double ener = 13.0,
4120                       bool isRealData = false,
4121                       bool drawStatBox = true,
4122                       int save = 0) {
4123   std::string name0[3] = {"energyE2", "energyH2", "energyP2"};
4124   std::string title0[3] = {"Energy in ECAL", "Energy in HCAL", "Track Momentum"};
4125   std::string xtitl0[3] = {"Energy (GeV)", "Energy (GeV)", "p (GeV)"};
4126   std::string name1[5] = {"eta02", "eta12", "eta22", "eta32", "eta42"};
4127   std::string name10[5] = {"eta0", "eta1", "eta2", "eta3", "eta4"};
4128   std::string xtitl1 = "i#eta";
4129   std::string name2[5] = {"p0", "p1", "p2", "p3", "p4"};
4130   std::string xtitl2 = "p (GeV)";
4131   std::string title1[5] = {"Tracks with p=40:60 GeV",
4132                            "Good Quality Tracks with p=40:60 GeV",
4133                            "Selected Tracks with p=40:60 GeV",
4134                            "Isolated Tracks with p=40:60 GeV",
4135                            "Isolated Tracks with p=40:60 GeV and MIPS in ECAL"};
4136   std::string title2[5] = {
4137       "All Tracks", "Good Quality Tracks", "Selected Tracks", "Isolated Tracks", "Isolated Tracks with MIPS in ECAL"};
4138   std::string ytitle = "Tracks";
4139 
4140   gStyle->SetCanvasBorderMode(0);
4141   gStyle->SetCanvasColor(kWhite);
4142   gStyle->SetPadColor(kWhite);
4143   gStyle->SetFillColor(kWhite);
4144   gStyle->SetOptTitle(0);
4145   if (drawStatBox)
4146     gStyle->SetOptStat(1110);
4147   else
4148     gStyle->SetOptStat(0);
4149   gStyle->SetOptFit(0);
4150 
4151   TFile* file = new TFile(infile);
4152   char name[100], namep[100];
4153   for (int k = 1; k <= etaMax; ++k) {
4154     for (int j = 0; j < 3; ++j) {
4155       sprintf(name, "%s%s%d", prefix.c_str(), name0[j].c_str(), k);
4156       TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
4157       if (hist1 != nullptr) {
4158         TH1D* hist = (TH1D*)(hist1->Clone());
4159         double ymin(0.90);
4160         sprintf(namep, "c_%s", name);
4161         TCanvas* pad = new TCanvas(namep, namep, 700, 500);
4162         pad->SetLogy();
4163         pad->SetRightMargin(0.10);
4164         pad->SetTopMargin(0.10);
4165         hist->GetXaxis()->SetTitleSize(0.04);
4166         hist->GetXaxis()->SetTitle(xtitl0[j].c_str());
4167         hist->GetYaxis()->SetTitle(ytitle.c_str());
4168         hist->GetYaxis()->SetLabelOffset(0.005);
4169         hist->GetYaxis()->SetTitleSize(0.04);
4170         hist->GetYaxis()->SetLabelSize(0.035);
4171         hist->GetYaxis()->SetTitleOffset(1.10);
4172         hist->SetMarkerStyle(20);
4173         hist->Draw();
4174         pad->Update();
4175         TPaveStats* st1 = (TPaveStats*)hist->GetListOfFunctions()->FindObject("stats");
4176         if (st1 != nullptr) {
4177           st1->SetY1NDC(0.78);
4178           st1->SetY2NDC(0.90);
4179           st1->SetX1NDC(0.65);
4180           st1->SetX2NDC(0.90);
4181         }
4182         pad->Update();
4183         double ymx(0.96), xmi(0.35), xmx(0.95);
4184         char txt[100];
4185         if (lumi > 0.1) {
4186           ymx = ymin - 0.005;
4187           xmi = 0.45;
4188           TPaveText* txt0 = new TPaveText(0.65, 0.91, 0.90, 0.96, "blNDC");
4189           txt0->SetFillColor(0);
4190           sprintf(txt, "%4.1f TeV %5.1f fb^{-1}", ener, lumi);
4191           txt0->AddText(txt);
4192           txt0->Draw("same");
4193         }
4194         double ymi = ymx - 0.05;
4195         TPaveText* txt1 = new TPaveText(xmi, ymi, xmx, ymx, "blNDC");
4196         txt1->SetFillColor(0);
4197         if (text == "") {
4198           sprintf(txt, "%s", title0[j].c_str());
4199         } else {
4200           sprintf(txt, "%s (%s)", title0[j].c_str(), text.c_str());
4201         }
4202         txt1->AddText(txt);
4203         txt1->Draw("same");
4204         double xmax = (isRealData) ? 0.24 : 0.35;
4205         ymi = 0.91;
4206         ymx = ymi + 0.05;
4207         TPaveText* txt2 = new TPaveText(0.02, ymi, xmax, ymx, "blNDC");
4208         txt2->SetFillColor(0);
4209         if (isRealData)
4210           sprintf(txt, "CMS Preliminary");
4211         else
4212           sprintf(txt, "CMS Simulation Preliminary");
4213         txt2->AddText(txt);
4214         txt2->Draw("same");
4215         pad->Modified();
4216         pad->Update();
4217         if (save > 0) {
4218           sprintf(name, "%s.pdf", pad->GetName());
4219           pad->Print(name);
4220         } else if (save < 0) {
4221           sprintf(name, "%s.C", pad->GetName());
4222           pad->Print(name);
4223         }
4224       }
4225     }
4226   }
4227 
4228   for (int k = 0; k < 3; ++k) {
4229     for (int j = 0; j < 5; ++j) {
4230       if (k == 0)
4231         sprintf(name, "%s%s", prefix.c_str(), name1[j].c_str());
4232       else if (k == 1)
4233         sprintf(name, "%s%s", prefix.c_str(), name10[j].c_str());
4234       else
4235         sprintf(name, "%s%s", prefix.c_str(), name2[j].c_str());
4236       TH1D* hist1 = (TH1D*)file->FindObjectAny(name);
4237       if (hist1 != nullptr) {
4238         TH1D* hist = (TH1D*)(hist1->Clone());
4239         double ymin(0.90);
4240         sprintf(namep, "c_%s", name);
4241         TCanvas* pad = new TCanvas(namep, namep, 700, 500);
4242         pad->SetLogy();
4243         pad->SetRightMargin(0.10);
4244         pad->SetTopMargin(0.10);
4245         hist->GetXaxis()->SetTitleSize(0.04);
4246         if (k <= 1)
4247           hist->GetXaxis()->SetTitle(xtitl1.c_str());
4248         else
4249           hist->GetXaxis()->SetTitle(xtitl2.c_str());
4250         hist->GetYaxis()->SetTitle(ytitle.c_str());
4251         hist->GetYaxis()->SetLabelOffset(0.005);
4252         hist->GetYaxis()->SetTitleSize(0.04);
4253         hist->GetYaxis()->SetLabelSize(0.035);
4254         hist->GetYaxis()->SetTitleOffset(1.10);
4255         hist->SetMarkerStyle(20);
4256         hist->Draw();
4257         pad->Update();
4258         TPaveStats* st1 = (TPaveStats*)hist->GetListOfFunctions()->FindObject("stats");
4259         if (st1 != nullptr) {
4260           st1->SetY1NDC(0.78);
4261           st1->SetY2NDC(0.90);
4262           st1->SetX1NDC(0.65);
4263           st1->SetX2NDC(0.90);
4264         }
4265         pad->Update();
4266         double ymx(0.96), xmi(0.35), xmx(0.95);
4267         char txt[100];
4268         if (lumi > 0.1) {
4269           ymx = ymin - 0.005;
4270           xmi = 0.45;
4271           TPaveText* txt0 = new TPaveText(0.65, 0.91, 0.90, 0.96, "blNDC");
4272           txt0->SetFillColor(0);
4273           sprintf(txt, "%4.1f TeV %5.1f fb^{-1}", ener, lumi);
4274           txt0->AddText(txt);
4275           txt0->Draw("same");
4276         }
4277         double ymi = ymx - 0.05;
4278         TPaveText* txt1 = new TPaveText(xmi, ymi, xmx, ymx, "blNDC");
4279         txt1->SetFillColor(0);
4280         if (text == "") {
4281           if (k == 0)
4282             sprintf(txt, "%s", title1[j].c_str());
4283           else
4284             sprintf(txt, "%s", title2[j].c_str());
4285         } else {
4286           if (k == 0)
4287             sprintf(txt, "%s (%s)", title1[j].c_str(), text.c_str());
4288           else
4289             sprintf(txt, "%s (%s)", title2[j].c_str(), text.c_str());
4290         }
4291         txt1->AddText(txt);
4292         txt1->Draw("same");
4293         double xmax = (isRealData) ? 0.24 : 0.35;
4294         ymi = 0.91;
4295         ymx = ymi + 0.05;
4296         TPaveText* txt2 = new TPaveText(0.02, ymi, xmax, ymx, "blNDC");
4297         txt2->SetFillColor(0);
4298         if (isRealData)
4299           sprintf(txt, "CMS Preliminary");
4300         else
4301           sprintf(txt, "CMS Simulation Preliminary");
4302         txt2->AddText(txt);
4303         txt2->Draw("same");
4304         pad->Modified();
4305         pad->Update();
4306         if (save > 0) {
4307           sprintf(name, "%s.pdf", pad->GetName());
4308           pad->Print(name);
4309         } else if (save < 0) {
4310           sprintf(name, "%s.C", pad->GetName());
4311           pad->Print(name);
4312         }
4313       }
4314     }
4315   }
4316 }
4317 
4318 void PlotMeanError(const std::string infilest, int reg = 3, bool resol = false, int save = 0, bool debug = false) {
4319   bool ok(false);
4320   const int ntypmx = 3;
4321   const int nregmx = 4;
4322   if (reg < 0 || reg >= nregmx)
4323     reg = nregmx - 1;
4324   int nEner(0), nType(0), nPts(0);
4325   std::vector<double> energy[ntypmx], denergy[ntypmx], value[ntypmx], dvalue[ntypmx];
4326   // First read the data
4327   std::ifstream fInput(infilest.c_str());
4328   if (!fInput.good()) {
4329     std::cout << "Cannot open file " << infilest << std::endl;
4330   } else {
4331     ok = true;
4332     fInput >> nEner >> nType >> nPts;
4333     int nmax = nEner * nType;
4334     int type, elow, ehigh;
4335     double v1[4], e1[4], v2[4], e2[4];
4336     for (int n = 0; n < nmax; ++n) {
4337       fInput >> type >> elow >> ehigh;
4338       fInput >> v1[0] >> e1[0] >> v1[1] >> e1[1] >> v1[2] >> e1[2] >> v1[3] >> e1[3];
4339       fInput >> v2[0] >> e2[0] >> v2[1] >> e2[1] >> v2[2] >> e2[2] >> v2[3] >> e2[3];
4340       double ener = 0.5 * (ehigh + elow);
4341       double dene = 0.5 * (ehigh - elow);
4342       energy[type].push_back(ener);
4343       denergy[type].push_back(dene);
4344       if (resol) {
4345         value[type].push_back(v2[reg]);
4346         dvalue[type].push_back(e2[reg]);
4347       } else {
4348         value[type].push_back(v1[reg]);
4349         dvalue[type].push_back(e1[reg]);
4350       }
4351     }
4352     fInput.close();
4353     std::cout << "Reads " << (nmax + 1) << " cards from " << infilest << " with measurements for " << nEner
4354               << " energies and " << nType << " types" << std::endl;
4355     if (debug) {
4356       for (int n = 0; n < nType; ++n) {
4357         std::cout << "Type " << n << " with " << energy[n].size() << " points\n";
4358         for (unsigned int k = 0; k < energy[n].size(); ++k)
4359           std::cout << " [" << k << "] " << energy[n][k] << " +- " << denergy[n][k] << " Value " << value[n][k]
4360                     << " +- " << dvalue[n][k] << std::endl;
4361       }
4362     }
4363   }
4364 
4365   // Now the plots
4366   if (ok) {
4367     int mvsres = (resol) ? 1 : 0;
4368     std::string names[2] = {"Mean", "Resol"};
4369     std::string namet[nregmx] = {"Barrel", "Transition", "Endcap", "Combined"};
4370     char cname[100];
4371     sprintf(cname, "c_%s%s", names[mvsres].c_str(), namet[reg].c_str());
4372     int color[ntypmx] = {2, 4, 1};
4373     int mtype[ntypmx] = {20, 21, 22};
4374     double ymin[2] = {0.65, 0.10};
4375     double ymax[2] = {1.30, 0.50};
4376     gStyle->SetCanvasBorderMode(0);
4377     gStyle->SetCanvasColor(kWhite);
4378     gStyle->SetPadColor(kWhite);
4379     gStyle->SetFillColor(kWhite);
4380     gStyle->SetOptTitle(kFALSE);
4381     gStyle->SetPadBorderMode(0);
4382     gStyle->SetCanvasBorderMode(0);
4383     gStyle->SetOptStat(0);
4384     TCanvas* canvas = new TCanvas(cname, cname, 500, 500);
4385     canvas->SetTopMargin(0.05);
4386     canvas->SetBottomMargin(0.14);
4387     canvas->SetLeftMargin(0.15);
4388     canvas->SetRightMargin(0.10);
4389     TH1F* vFrame = canvas->DrawFrame(0.0, ymin[mvsres], 120.0, ymax[mvsres]);
4390     vFrame->GetXaxis()->SetRangeUser(0.0, 120.0);
4391     vFrame->GetYaxis()->SetRangeUser(ymin[mvsres], ymax[mvsres]);
4392     vFrame->GetXaxis()->SetLabelSize(0.04);
4393     vFrame->GetYaxis()->SetLabelSize(0.04);
4394     vFrame->GetXaxis()->SetTitleSize(0.04);
4395     vFrame->GetYaxis()->SetTitleSize(0.04);
4396     vFrame->GetXaxis()->SetTitleOffset(1.2);
4397     vFrame->GetYaxis()->SetTitleOffset(1.6);
4398     vFrame->GetXaxis()->SetLabelOffset(0.0);
4399     vFrame->GetXaxis()->SetTitle("p_{Beam} (GeV/c)");
4400     if (resol) {
4401       vFrame->GetYaxis()->SetTitle("Width(E_{HCAL}/(p-E_{ECAL}))");
4402     } else {
4403       vFrame->GetYaxis()->SetTitle("MPV(E_{HCAL}/(p-E_{ECAL}))");
4404     }
4405     TLegend* legend = new TLegend(0.70, 0.80, 0.90, 0.94);
4406     legend->SetFillColor(kWhite);
4407     std::string nameg[ntypmx] = {"MAHI", "M0", "M2"};
4408     for (int n = 0; n < nType; ++n) {
4409       unsigned int nmax0 = energy[n].size();
4410       double mom[nmax0], dmom[nmax0], mean[nmax0], dmean[nmax0];
4411       for (unsigned int k = 0; k < nmax0; ++k) {
4412         mom[k] = energy[n][k];
4413         dmom[k] = denergy[n][k];
4414         mean[k] = value[n][k];
4415         dmean[k] = dvalue[n][k];
4416       }
4417       TGraphErrors* graph = new TGraphErrors(nmax0, mom, mean, dmom, dmean);
4418       graph->SetMarkerStyle(mtype[n]);
4419       graph->SetMarkerColor(color[n]);
4420       graph->SetMarkerSize(1.4);
4421       graph->SetLineColor(color[n]);
4422       graph->SetLineWidth(2);
4423       sprintf(cname, "%s", nameg[n].c_str());
4424       legend->AddEntry(graph, cname, "lp");
4425       graph->Draw("P");
4426     }
4427     legend->Draw("same");
4428     std::string regions[nregmx] = {"20118B Barrel", "2018B Transition", "2018B Endcap", "2018B"};
4429     sprintf(cname, "%s", regions[reg].c_str());
4430     TPaveText* txt0 = new TPaveText(0.16, 0.90, 0.40, 0.94, "blNDC");
4431     txt0->SetFillColor(0);
4432     txt0->AddText(cname);
4433     txt0->Draw("same");
4434     TPaveText* txt1 = new TPaveText(0.15, 0.95, 0.40, 0.99, "blNDC");
4435     txt1->SetFillColor(0);
4436     sprintf(cname, "CMS Preliminary");
4437     txt1->AddText(cname);
4438     txt1->Draw("same");
4439     canvas->Update();
4440     if (save > 0) {
4441       sprintf(cname, "%s.pdf", canvas->GetName());
4442       canvas->Print(cname);
4443     } else if (save < 0) {
4444       sprintf(cname, "%s.C", canvas->GetName());
4445       canvas->Print(cname);
4446     }
4447   }
4448 }
4449 
4450 void PlotDepthCorrFactor(char* infile,
4451                          std::string text,
4452                          std::string prefix = "",
4453                          bool isRealData = true,
4454                          bool drawStatBox = true,
4455                          int save = 0) {
4456   std::map<int, cfactors> cfacs;
4457   int etamin(100), etamax(-100), maxdepth(0);
4458   std::ifstream ifile(infile);
4459   if (!ifile.is_open()) {
4460     std::cout << "Cannot open duplicate file " << infile << std::endl;
4461   } else {
4462     unsigned int all(0), good(0);
4463     char buffer[1024];
4464     while (ifile.getline(buffer, 1024)) {
4465       ++all;
4466       std::string bufferString(buffer);
4467       if (bufferString.substr(0, 1) == "#") {
4468         continue;  //ignore other comments
4469       } else {
4470         std::vector<std::string> items = splitString(bufferString);
4471         if (items.size() < 3) {
4472           std::cout << "Ignore  line: " << buffer << " Size " << items.size();
4473           for (unsigned int k = 0; k < items.size(); ++k)
4474             std::cout << " [" << k << "] : " << items[k];
4475           std::cout << std::endl;
4476         } else {
4477           ++good;
4478           int ieta = std::atoi(items[0].c_str());
4479           if (ieta < etamin)
4480             etamin = ieta;
4481           if (ieta > etamax)
4482             etamax = ieta;
4483           unsigned int k(1);
4484           int depth(0);
4485           std::cout << "ieta " << ieta;
4486           while (k < items.size()) {
4487             ++depth;
4488             double corrf = std::atof(items[k].c_str());
4489             double dcorr = std::atof(items[k + 1].c_str());
4490             if (depth > maxdepth)
4491               maxdepth = depth;
4492             if ((depth == 1) && ((std::abs(ieta) == 17) || (std::abs(ieta) == 18))) {
4493             } else {
4494               int detId = repackId(ieta, depth);
4495               cfacs[detId] = cfactors(ieta, depth, corrf, dcorr);
4496               std::cout << " Depth " << depth << " " << corrf << " +- " << dcorr;
4497             }
4498             k += 2;
4499           }
4500           std::cout << std::endl;
4501         }
4502       }
4503     }
4504     ifile.close();
4505     std::cout << "Reads total of " << all << " and " << good << " good records of depth dependent factors from "
4506               << infile << " Eta range " << etamin << ":" << etamax << " maxdepth " << maxdepth << std::endl;
4507   }
4508 
4509   gStyle->SetCanvasBorderMode(0);
4510   gStyle->SetCanvasColor(kWhite);
4511   gStyle->SetPadColor(kWhite);
4512   gStyle->SetFillColor(kWhite);
4513   gStyle->SetOptTitle(0);
4514   if (drawStatBox) {
4515     gStyle->SetOptStat(10);
4516     gStyle->SetOptFit(10);
4517   } else {
4518     gStyle->SetOptStat(0);
4519     gStyle->SetOptFit(0);
4520   }
4521   int colors[7] = {1, 6, 4, 7, 2, 9, 3};
4522   int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
4523   int nbin = etamax - etamin + 1;
4524   std::vector<TH1D*> hists;
4525   std::vector<int> entries;
4526   char name[100];
4527   double dy(0);
4528   int fits(0);
4529   for (int j = 0; j < maxdepth; ++j) {
4530     sprintf(name, "hd%d", j + 1);
4531     TObject* ob = gROOT->FindObject(name);
4532     if (ob)
4533       ob->Delete();
4534     TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
4535     int nent(0);
4536     for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr) {
4537       if ((itr->second).depth == j + 1) {
4538         int ieta = (itr->second).ieta;
4539         int bin = ieta - etamin + 1;
4540         float val = (itr->second).corrf;
4541         float dvl = (itr->second).dcorr;
4542         h->SetBinContent(bin, val);
4543         h->SetBinError(bin, dvl);
4544         nent++;
4545       }
4546     }
4547     h->SetLineColor(colors[j]);
4548     h->SetMarkerColor(colors[j]);
4549     h->SetMarkerStyle(mtype[j]);
4550     h->GetXaxis()->SetTitle("i#eta");
4551     h->GetYaxis()->SetTitle("Depth Dependent Correction Factor");
4552     h->GetYaxis()->SetLabelOffset(0.005);
4553     h->GetYaxis()->SetTitleOffset(1.20);
4554     h->GetYaxis()->SetRangeUser(0.0, 2.0);
4555     hists.push_back(h);
4556     entries.push_back(nent);
4557     dy += 0.025;
4558   }
4559   sprintf(name, "c_%sCorrFactor", prefix.c_str());
4560   TCanvas* pad = new TCanvas(name, name, 700, 500);
4561   pad->SetRightMargin(0.10);
4562   pad->SetTopMargin(0.10);
4563   double yh = 0.90;
4564   // double yl = yh - 0.025 * hists.size() - dy - 0.01;
4565   double yl = 0.15;
4566   TLegend* legend = new TLegend(0.35, yl, 0.65, yl + 0.04 * hists.size());
4567   legend->SetFillColor(kWhite);
4568   for (unsigned int k = 0; k < hists.size(); ++k) {
4569     if (k == 0)
4570       hists[k]->Draw("");
4571     else
4572       hists[k]->Draw("sames");
4573     pad->Update();
4574     TPaveStats* st1 = (TPaveStats*)hists[k]->GetListOfFunctions()->FindObject("stats");
4575     if (st1 != nullptr) {
4576       dy = 0.025;
4577       st1->SetLineColor(colors[k]);
4578       st1->SetTextColor(colors[k]);
4579       st1->SetY1NDC(yh - dy);
4580       st1->SetY2NDC(yh);
4581       st1->SetX1NDC(0.70);
4582       st1->SetX2NDC(0.90);
4583       yh -= dy;
4584     }
4585     sprintf(name, "Depth %d (%s)", k + 1, text.c_str());
4586     legend->AddEntry(hists[k], name, "lp");
4587   }
4588   legend->Draw("same");
4589   pad->Update();
4590   if (fits < 1) {
4591     double xmin = hists[0]->GetBinLowEdge(1);
4592     int nbin = hists[0]->GetNbinsX();
4593     double xmax = hists[0]->GetBinLowEdge(nbin) + hists[0]->GetBinWidth(nbin);
4594     TLine* line = new TLine(xmin, 1.0, xmax, 1.0);
4595     line->SetLineColor(9);
4596     line->SetLineWidth(2);
4597     line->SetLineStyle(2);
4598     line->Draw("same");
4599     pad->Modified();
4600     pad->Update();
4601   }
4602   char txt1[30];
4603   double xmax = (isRealData) ? 0.33 : 0.44;
4604   TPaveText* txt2 = new TPaveText(0.11, 0.85, xmax, 0.89, "blNDC");
4605   txt2->SetFillColor(0);
4606   if (isRealData)
4607     sprintf(txt1, "CMS Preliminary");
4608   else
4609     sprintf(txt1, "CMS Simulation Preliminary");
4610   txt2->AddText(txt1);
4611   txt2->Draw("same");
4612   pad->Modified();
4613   pad->Update();
4614   if (save > 0) {
4615     sprintf(name, "%s.pdf", pad->GetName());
4616     pad->Print(name);
4617   } else if (save < 0) {
4618     sprintf(name, "%s.C", pad->GetName());
4619     pad->Print(name);
4620   }
4621 }
4622 
4623 void DrawHistPhiSymmetry(TH1D* hist0, bool isRealData, bool drawStatBox, bool save) {
4624   char name[30], namep[30], txt1[30];
4625   TH1D* hist = (TH1D*)(hist0->Clone());
4626   sprintf(namep, "c_%s", hist->GetName());
4627   TCanvas* pad = new TCanvas(namep, namep, 700, 500);
4628   pad->SetRightMargin(0.10);
4629   pad->SetTopMargin(0.10);
4630   hist->GetXaxis()->SetTitleSize(0.04);
4631   hist->GetXaxis()->SetTitle(hist->GetTitle());
4632   hist->GetYaxis()->SetTitle("Channels");
4633   hist->GetYaxis()->SetLabelOffset(0.005);
4634   hist->GetYaxis()->SetTitleSize(0.04);
4635   hist->GetYaxis()->SetLabelSize(0.035);
4636   hist->GetYaxis()->SetTitleOffset(1.10);
4637   hist->Draw();
4638   pad->Update();
4639   if (drawStatBox) {
4640     TPaveStats* st1 = (TPaveStats*)hist->GetListOfFunctions()->FindObject("stats");
4641     if (st1 != nullptr) {
4642       st1->SetY1NDC(0.70);
4643       st1->SetY2NDC(0.90);
4644       st1->SetX1NDC(0.65);
4645       st1->SetX2NDC(0.90);
4646     }
4647     pad->Modified();
4648     pad->Update();
4649   }
4650   TPaveText* txt2 = new TPaveText(0.11, 0.85, 0.44, 0.89, "blNDC");
4651   txt2->SetFillColor(0);
4652   if (isRealData)
4653     sprintf(txt1, "CMS Preliminary");
4654   else
4655     sprintf(txt1, "CMS Simulation Preliminary");
4656   txt2->AddText(txt1);
4657   txt2->Draw("same");
4658   pad->Modified();
4659   pad->Update();
4660   if (save) {
4661     sprintf(name, "%s.pdf", pad->GetName());
4662     pad->Print(name);
4663   }
4664 }
4665 
4666 void PlotPhiSymmetryResults(
4667     char* infile, bool isRealData = true, bool drawStatBox = true, bool debug = false, bool save = false) {
4668   const int maxDepthHB(4), maxDepthHE(7);
4669   const double cfacMin(0.70), cfacMax(1.5);
4670   const int nbin = (100.0 * (cfacMax - cfacMin));
4671   std::vector<TH1D*> histHB, histHE;
4672   char name[20], title[80];
4673   for (int k = 0; k < maxDepthHB; ++k) {
4674     sprintf(name, "HB%d", k);
4675     sprintf(title, "Correction factor for depth %d of HB", k);
4676     TObject* ob = gROOT->FindObject(name);
4677     if (ob)
4678       ob->Delete();
4679     TH1D* h = new TH1D(name, title, nbin, cfacMin, cfacMax);
4680     histHB.push_back(h);
4681     if (debug)
4682       std::cout << "Book " << h->GetName() << " Title " << h->GetTitle() << " range " << nbin << ":" << cfacMin << ":"
4683                 << cfacMax << std::endl;
4684   }
4685   for (int k = 0; k < maxDepthHE; ++k) {
4686     sprintf(name, "HE%d", k);
4687     sprintf(title, "Correction factor for depth %d of HE", k);
4688     TObject* ob = gROOT->FindObject(name);
4689     if (ob)
4690       ob->Delete();
4691     TH1D* h = new TH1D(name, title, nbin, cfacMin, cfacMax);
4692     histHE.push_back(h);
4693     if (debug)
4694       std::cout << "Book " << h->GetName() << " Title " << h->GetTitle() << " range " << nbin << ":" << cfacMin << ":"
4695                 << cfacMax << std::endl;
4696   }
4697   std::ifstream fInput(infile);
4698   if (!fInput.good()) {
4699     std::cout << "Cannot open file " << infile << std::endl;
4700   } else {
4701     char buffer[1024];
4702     int all(0), good(0);
4703     std::map<int, int> kount;
4704     while (fInput.getline(buffer, 1024)) {
4705       ++all;
4706       std::string bufferString(buffer);
4707       if (bufferString.substr(0, 1) == "#") {
4708         continue;  //ignore other comments
4709       } else {
4710         std::vector<std::string> items = splitString(bufferString);
4711         if (items.size() < 5) {
4712           for (unsigned int k = 0; k < items.size(); ++k)
4713             std::cout << " [" << k << "] : " << items[k];
4714           std::cout << std::endl;
4715         } else {
4716           ++good;
4717           int subdet = std::atoi(items[0].c_str());
4718           int ieta = std::atoi(items[1].c_str());
4719           int depth = std::atoi(items[3].c_str());
4720           double corrf = std::atof(items[4].c_str());
4721           if ((debug) && (good % 100 == 0))
4722             std::cout << "Subdet " << subdet << " Depth " << depth << " Corr " << corrf << std::endl;
4723           if ((corrf < cfacMin) || (corrf > cfacMax))
4724             std::cout << "Outlier: " << buffer << std::endl;
4725           if (subdet == 1) {
4726             if (depth <= maxDepthHB)
4727               histHB[depth - 1]->Fill(corrf);
4728           } else if (subdet == 2) {
4729             if (depth <= maxDepthHE)
4730               histHE[depth - 1]->Fill(corrf);
4731           }
4732           if ((subdet == 1) || (subdet == 2)) {
4733             int indx = (ieta > 0) ? (ieta * 10 + depth) : (10000 + 10 * (-ieta) + depth);
4734             if (kount.find(indx) == kount.end())
4735               kount[indx] = 1;
4736             else
4737               ++kount[indx];
4738           }
4739         }
4740       }
4741     }
4742     fInput.close();
4743     std::cout << "Reads total of " << all << " and " << good << " good records of phi-symmetry factors from " << infile
4744               << std::endl;
4745     for (std::map<int, int>::iterator itr = kount.begin(); itr != kount.end(); ++itr) {
4746       int depth = (itr->first) % 10;
4747       int ieta = ((itr->first) / 10) % 100;
4748       int nphi = itr->second;
4749       bool miss = (((ieta <= 20) && (nphi != 72)) || ((ieta > 20) && (nphi != 36)));
4750       if (itr->first >= 10000)
4751         ieta = -ieta;
4752       if (debug || miss)
4753         std::cout << "Tower[" << ieta << ":" << depth << "] has " << nphi << " entries" << std::endl;
4754     }
4755   }
4756 
4757   // Now plot the histograms
4758   gStyle->SetCanvasBorderMode(0);
4759   gStyle->SetCanvasColor(kWhite);
4760   gStyle->SetPadColor(kWhite);
4761   gStyle->SetFillColor(kWhite);
4762   gStyle->SetOptTitle(0);
4763   gStyle->SetOptStat(111110);
4764   gStyle->SetOptFit(0);
4765 
4766   // HB first
4767   for (unsigned int k = 0; k < histHB.size(); ++k) {
4768     DrawHistPhiSymmetry(histHB[k], isRealData, drawStatBox, save);
4769   }
4770 
4771   // Then HE
4772   for (unsigned int k = 0; k < histHE.size(); ++k) {
4773     DrawHistPhiSymmetry(histHE[k], isRealData, drawStatBox, save);
4774   }
4775 }
4776 
4777 void PlotHistCorrRatio(char* infile1,
4778                        std::string text1,
4779                        char* infile2,
4780                        std::string text2,
4781                        int depth1,
4782                        int depth2,
4783                        std::string prefixF,
4784                        std::string text0,
4785                        int etaMin = -1,
4786                        int etaMax = -1,
4787                        bool doFit = true,
4788                        bool isRealData = true,
4789                        const char* year = "2024",
4790                        int iformat = 0,
4791                        int save = 0) {
4792   std::map<int, cfactors> cfacs[2];
4793   std::vector<std::string> texts;
4794   int nfile(0), etamin(100), etamax(-100), maxdepth(0);
4795   const char* blank("");
4796   if (infile1 != blank) {
4797     readCorrFactors(infile1, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
4798     if (cfacs[nfile].size() > 0) {
4799       texts.push_back(text1);
4800       ++nfile;
4801     }
4802   }
4803   if (infile2 != blank) {
4804     readCorrFactors(infile2, 1.0, cfacs[nfile], etamin, etamax, maxdepth, iformat);
4805     if (cfacs[nfile].size() > 0) {
4806       texts.push_back(text2);
4807       ++nfile;
4808     }
4809   }
4810 
4811   if (etaMax > 0) {
4812     etamin = -etaMax;
4813     etamax = etaMax;
4814   }
4815   if (nfile == 2) {
4816     gStyle->SetCanvasBorderMode(0);
4817     gStyle->SetCanvasColor(kWhite);
4818     gStyle->SetPadColor(kWhite);
4819     gStyle->SetFillColor(kWhite);
4820     gStyle->SetOptTitle(0);
4821     if (doFit) {
4822       gStyle->SetOptStat(10);
4823       gStyle->SetOptFit(10);
4824     } else {
4825       gStyle->SetOptStat(0);
4826       gStyle->SetOptFit(0);
4827     }
4828     int colors[7] = {1, 6, 4, 7, 2, 9, 3};
4829     int mtype[7] = {20, 21, 22, 23, 24, 25, 26};
4830     int styles[7] = {2, 3, 1, 4, 1, 3, 2};
4831     int nbin = etamax - etamin + 1;
4832     std::vector<TH1D*> hists;
4833     std::vector<double> fitr, dfit;
4834     char name[100];
4835     for (int ih = 0; ih < nfile; ++ih) {
4836       sprintf(name, "h%d", ih);
4837       TObject* ob = gROOT->FindObject(name);
4838       if (ob)
4839         ob->Delete();
4840       TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
4841       double sumNum(0), sumDen(0);
4842       int npt(0);
4843       for (std::map<int, cfactors>::const_iterator itr = cfacs[ih].begin(); itr != cfacs[ih].end(); ++itr) {
4844         int ieta = (itr->second).ieta;
4845         bool seleta = (etaMin > 0) ? (std::abs(ieta) > etaMin) : true;
4846         if ((ieta >= etamin) && (ieta <= etamax) && seleta && ((itr->second).depth == depth1)) {
4847           ++npt;
4848           int bin = ieta - etamin + 1;
4849           for (std::map<int, cfactors>::const_iterator ktr = cfacs[ih].begin(); ktr != cfacs[ih].end(); ++ktr) {
4850             if (((ktr->second).ieta == ieta) && ((ktr->second).depth == depth2)) {
4851               double er1 = (itr->second).dcorr / (itr->second).corrf;
4852               double er2 = (ktr->second).dcorr / (ktr->second).corrf;
4853               float val = (itr->second).corrf / (ktr->second).corrf;
4854               float dvl = val * sqrt(er1 * er1 + er2 * er2);
4855               double temp1 = ((itr->second).corrf > 1.0) ? 1.0 / (itr->second).corrf : (itr->second).corrf;
4856               double temp2 = ((itr->second).corrf > 1.0)
4857                                  ? (itr->second).dcorr / ((itr->second).corrf * (itr->second).corrf)
4858                                  : (itr->second).dcorr;
4859               h->SetBinContent(bin, val);
4860               h->SetBinError(bin, dvl);
4861               sumNum += (std::abs(1 - temp1) / (temp2 * temp2));
4862               sumDen += (1.0 / (temp2 * temp2));
4863               break;
4864             }
4865           }
4866         }
4867       }
4868       h->SetLineColor(colors[ih]);
4869       h->SetMarkerColor(colors[ih]);
4870       h->SetMarkerStyle(mtype[ih]);
4871       h->SetMarkerSize(0.9);
4872       h->GetXaxis()->SetTitle("i#eta");
4873       sprintf(name, "CF_{%d}/CF_{%d}", depth1, depth2);
4874       h->GetYaxis()->SetTitle(name);
4875       h->GetYaxis()->SetLabelOffset(0.005);
4876       h->GetYaxis()->SetTitleSize(0.036);
4877       h->GetYaxis()->SetTitleOffset(1.20);
4878       h->GetYaxis()->SetRangeUser(0.0, 3.0);
4879       if (doFit) {
4880         TObject* ob = gROOT->FindObject(name);
4881         if (ob)
4882           ob->Delete();
4883         TF1* func = new TF1(name, "pol0", etamin, etamax);
4884         func->SetLineColor(colors[ih]);
4885         func->SetLineStyle(styles[ih]);
4886         h->Fit(func, "+QWLR", "");
4887       }
4888       hists.push_back(h);
4889       sumNum = (sumDen > 0) ? (sumNum / sumDen) : 0;
4890       sumDen = (sumDen > 0) ? 1.0 / sqrt(sumDen) : 0;
4891       fitr.push_back(sumNum);
4892       dfit.push_back(sumDen);
4893       std::cout << "Get Ratio of mean for " << npt << " points: Mean " << sumNum << " +- " << sumDen << std::endl;
4894     }
4895     sprintf(name, "c_Corr%sRatio", prefixF.c_str());
4896     TCanvas* pad = new TCanvas(name, name, 700, 500);
4897     pad->SetRightMargin(0.10);
4898     pad->SetTopMargin(0.10);
4899     double yh = 0.90;
4900     double yl = yh - 0.035 * hists.size() - 0.01;
4901     TLegend* legend = new TLegend(0.11, yl, 0.50, yh - 0.01);
4902     legend->SetFillColor(kWhite);
4903     for (unsigned int k = 0; k < hists.size(); ++k) {
4904       if (k == 0)
4905         hists[k]->Draw("");
4906       else
4907         hists[k]->Draw("sames");
4908       pad->Update();
4909       if (doFit) {
4910         TPaveStats* st1 = (TPaveStats*)hists[k]->GetListOfFunctions()->FindObject("stats");
4911         if (st1 != nullptr) {
4912           st1->SetLineColor(colors[k]);
4913           st1->SetTextColor(colors[k]);
4914           yh = 0.90 - 0.070 * k;
4915           st1->SetY1NDC(yh - 0.07);
4916           st1->SetY2NDC(yh);
4917           st1->SetX1NDC(0.65);
4918           st1->SetX2NDC(0.90);
4919         }
4920       }
4921       pad->Update();
4922       sprintf(name, "%s (Mean dev. = %5.3f)", texts[k].c_str(), fitr[k]);
4923       legend->AddEntry(hists[k], name, "lp");
4924     }
4925     legend->Draw("same");
4926     TPaveText* txt0 = new TPaveText(0.12, 0.91, 0.49, 0.96, "blNDC");
4927     txt0->SetFillColor(0);
4928     char txt[40];
4929     if (isRealData)
4930       sprintf(txt, "CMS Preliminary (%s)", year);
4931     else
4932       sprintf(txt, "CMS Simulation Preliminary (%s)", year);
4933     txt0->AddText(txt);
4934     txt0->Draw("same");
4935     TPaveText* txt2 = new TPaveText(0.65, 0.91, 0.90, 0.96, "blNDC");
4936     txt2->SetFillColor(0);
4937     txt2->AddText(text0.c_str());
4938     txt2->Draw("same");
4939     pad->Update();
4940     if (save > 0) {
4941       sprintf(name, "%s.pdf", pad->GetName());
4942       pad->Print(name);
4943     } else if (save < 0) {
4944       sprintf(name, "%s.C", pad->GetName());
4945       pad->Print(name);
4946     }
4947   }
4948 }