Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-07-14 01:08:26

0001 //////////////////////////////////////////////////////////////////////////////
0002 // Usage:
0003 // .L CalibPlotProperties.C+g
0004 //  CalibPlotProperties c1(fname, dirname, dupFileName, prefix, corrFileName,
0005 //                     rcorFileName, puCorr, flag, isRealData,
0006 //                         truncateFlag, useGen, scale, useScale, etalo, etahi,
0007 //                         runlo, runhi, phimin, phimax, zside, nvxlo, nvxhi,
0008 //                         rbxFile, badRunFile, exclude, etamax);
0009 //  c1.Loop(nentries);
0010 //  c1.savePlot(histFileName, append, all, debug);
0011 //
0012 //        This will prepare a set of histograms with properties of the tracks
0013 //        which can be displayed by the method in this file
0014 //
0015 //  PlotHist(histFileName, prefix, text, flagC, etalo, etahi, save)
0016 //
0017 //        This will plot the histograms and save the canvases
0018 //
0019 //  PlotPHist(hisFileName, prefix, pLow, pHigh, isRealData, save)
0020 //
0021 //        This will plot histograms of momenta spectra of types between
0022 //        pLow and pHigh and save the canvases
0023 //
0024 // .L CalibPlotProperties.C+g
0025 //  CalibSplit c1(fname, dirname, outFileName, pmin, pmax, runMin, runMax, debug);
0026 //  c1.Loop(nentries);
0027 //
0028 //        This will split the tree and keep for tacks with momenta between
0029 //        pmin nd pm
0030 //
0031 //   where:
0032 //
0033 //   fname   (const char*)     = file name of the input ROOT tree
0034 //                               or name of the file containing a list of
0035 //                               file names of input ROOT trees
0036 //   dirname (const char*)     = name of the directory where Tree resides
0037 //                               (use "HcalIsoTrkAnalyzer")
0038 //   dupFileName (const char*) = name of the file containing list of entries
0039 //                               of duplicate events or depth dependent weights
0040 //                               or weights coming due to change in gains
0041 //                               (driven by flag)
0042 //   prefix (std::string)      = String to be added to the name of histogram
0043 //                               (usually a 4 character string; default="")
0044 //   corrFileName (const char*)= name of the text file having the correction
0045 //                               factors to be used (default="", no corr.)
0046 //   rcorFileName (const char*)= name of the text file having the correction
0047 //                               factors as a function of run numbers or depth
0048 //                               to be used for raddam/depth/pileup/phisym/
0049 //                               phisym(s) dependent correction
0050 //                               (default="", no corr.)
0051 //   puCorr (int)              = PU correction to be applied or not: 0 no
0052 //                               correction; < 0 use eDelta; > 0 rho dependent
0053 //                               correction (-8)
0054 //   flag (int)                = 7 digit integer (ymlthdo) with control
0055 //                               information (y=3/2/1/0 containing list of
0056 //                               run ranges and ieta, depth for gain changes
0057 //                               (3): list of ieta, iphi of channels to be
0058 //                               selected (2); list containing depth dependent
0059 //                               weights for each ieta (1); list of duplicate
0060 //                               entries (0) in the dupFileName;
0061 //                               m=0/1 for controlling creation of depth
0062 //                               depedendent histograms;
0063 //                               l=5/4/3/2/1/0 for type of rcorFileName ((5
0064 //                               for run-dependent correctons using results
0065 //                               from several phi symmetry studies; 4 for
0066 //                               using results from phi-symmetry; 3 for
0067 //                               pileup correction using machine learning
0068 //                               method; 2 for overall response corrections;
0069 //                               1 for depth dependence corrections; 0 for
0070 //                               raddam corrections);
0071 //                               t = bit information (lower bit set will
0072 //                               apply a cut on L1 closeness; and higher bit
0073 //                               set read correction file with Marina format);
0074 //                               h =0/1 flag to create energy histograms
0075 //                               d =0/1 flag to create basic set of histograms;
0076 //                               o =0/1/2 for tight / loose / flexible
0077 //                               selection). Default = 101111
0078 //   isRealData (bool)         = true/false for data/MC (default true)
0079 //   truncateFlag    (int)     = A two digit flag (dr) with the default value 0.
0080 //                               The digit *r* is used to treat depth values:
0081 //                               (0) treat each depth independently; (1) all
0082 //                               depths of ieta 15, 16 of HB as depth 1; (2)
0083 //                               all depths in HB and HE as depth 1; (3) ignore
0084 //                               depth index in HE (depth index set to 1); (4)
0085 //                               ignore depth index in HB (depth index set 1);
0086 //                               (5) all depths in HB and HE with values > 1
0087 //                               as depth 2; (6) for depth = 1 and 2, depth =
0088 //                               1, else depth = 2; (7) in case of HB, depths
0089 //                               1 and 2 are set to 1, else depth = 2; for HE
0090 //                               ignore depth index; (8) Assign all depths > 4
0091 //                               as depth = 5; (9) Assign all depth = 1 as
0092 //                               depth = 2. The digit *d* is used if zside is
0093 //                               to be ignored (1) or not (0).
0094 //                               (Default 0)
0095 //   useGen (bool)             = true/false to use generator level momentum
0096 //                               or reconstruction level momentum (def false)
0097 //   scale (double)            = energy scale if correction factor to be used
0098 //                               (default = 1.0)
0099 //   useScale (int)            = two digit number (do) with o: as the flag for
0100 //                               application of scale factor (0: nowehere,
0101 //                               1: barrel; 2: endcap, 3: everywhere)
0102 //                               barrel => |ieta| < 16; endcap => |ieta| > 15;
0103 //                               d: as the format for threshold application,
0104 //                               0: no threshold; 1: 2022 prompt data; 2:
0105 //                               2022 reco data; 3: 2023 prompt data; 4: 2025
0106 //                               Begin of Year; 5: Derived from the file
0107 //                               PFCuts_IOV_362975.txt.
0108 //                               (default = 0)
0109 //   etalo/etahi (int,int)     = |eta| ranges (0:30)
0110 //   runlo  (int)              = lower value of run number to be included (+ve)
0111 //                               or excluded (-ve) (default 0)
0112 //   runhi  (int)              = higher value of run number to be included
0113 //                               (+ve) or excluded (-ve) (def 9999999)
0114 //   phimin          (int)     = minimum iphi value (1)
0115 //   phimax          (int)     = maximum iphi value (72)
0116 //   zside           (int)     = the side of the detector if phimin and phimax
0117 //                               differ from 1-72 (1)
0118 //   nvxlo           (int)     = minimum # of vertex in event to be used (0)
0119 //   nvxhi           (int)     = maximum # of vertex in event to be used (1000)
0120 //   rbxFile         (char *)  = Name of the file containing a list of RBX's
0121 //                               to be consdered (default = ""). RBX's are
0122 //                               specified by zside*(Subdet*100+RBX #).
0123 //                               For HEP17 it will be 217
0124 //   badRunFile      (char *)  = Name of the file containing a list of runs
0125 //                               to be excluded
0126 //   exclude         (bool)    = RBX specified by the contents in *rbxFile* to
0127 //                               be exluded or only considered (default = false)
0128 //   etamax          (bool)    = if set and if the corr-factor not found in the
0129 //                               corrFactor table, the corr-factor for the
0130 //                               corresponding zside, depth=1 and maximum ieta
0131 //                               in the table is taken (false)
0132 //   nentries        (int)     = maximum number of entries to be processed,
0133 //                               if -1, all entries to be processed (-1)
0134 //
0135 //   histFileName (std::string)= name of the file containing saved histograms
0136 //   append (bool)             = true/false if the histogram file to be opened
0137 //                               in append/output mode
0138 //   all (bool)                = true/false if all histograms to be saved or
0139 //                               not (def false)
0140 //
0141 //   text  (string)            = string to be put in the title
0142 //   flagC (int)               = 3 digit integer (hdo) with control
0143 //                               information (h=0/1 for plottting the depth
0144 //                               depedendent histograms;
0145 //                               d =0/1 flag to plot energy histograms;
0146 //                               o =0/1 flag to plot basic set of histograms;
0147 //                               Default = 111
0148 //   save (int)                = flag to create or not save the plot in a file
0149 //                               (0 = no save, > 0 pdf file, < 0 hpg file)
0150 //
0151 //   outFileName (std::string)= name of the file containing saved tree
0152 //   pmin (double)            = minimum track momentum (40.0)
0153 //   pmax (double)            = maximum track momentum (60.0)
0154 //   runMin (int)             = minimum run number (-1) | if -1, no check on
0155 //   runMax (int)             = maximum run number (-1) | run number not done
0156 //   debug (bool)             = debug flag (false)
0157 //////////////////////////////////////////////////////////////////////////////
0158 #include <TROOT.h>
0159 #include <TChain.h>
0160 #include <TFile.h>
0161 #include <TH1D.h>
0162 #include <TH2F.h>
0163 #include <TProfile.h>
0164 #include <TFitResult.h>
0165 #include <TFitResultPtr.h>
0166 #include <TStyle.h>
0167 #include <TCanvas.h>
0168 #include <TLegend.h>
0169 #include <TPaveStats.h>
0170 #include <TPaveText.h>
0171 
0172 #include <algorithm>
0173 #include <iomanip>
0174 #include <iostream>
0175 #include <fstream>
0176 #include <sstream>
0177 #include <map>
0178 #include <vector>
0179 #include <string>
0180 
0181 void unpackDetId(unsigned int, int &, int &, int &, int &, int &);
0182 #include "CalibCorr.C"
0183 
0184 namespace CalibPlots {
0185   static const int npbin = 5;
0186   static const int netabin = 6;
0187   static const int ndepth = 7;
0188   static const int ntitles = 5;
0189   static const int npbin0 = (npbin + 1);
0190   int getP(int k) {
0191     const int ipbin[npbin0] = {20, 30, 40, 60, 100, 500};
0192     return ((k >= 0 && k < npbin0) ? ipbin[k] : 0);
0193   }
0194   double getMomentum(int k) { return (double)(getP(k)); }
0195   int getEta(int k) {
0196     int ietas[netabin + 1] = {0, 4, 8, 13, 18, 23, 28};
0197     return ((k >= 0 && k < netabin) ? ietas[k] : -1);
0198   }
0199   std::string getTitle(int k) {
0200     std::string titl[ntitles] = {
0201         "all tracks", "good quality tracks", "selected tracks", "isolated good tracks", "tracks having MIP in ECAL"};
0202     return ((k >= 0 && k < ntitles) ? titl[k] : "");
0203   }
0204 }  // namespace CalibPlots
0205 
0206 class CalibPlotProperties {
0207 public:
0208   TChain *fChain;  //!pointer to the analyzed TTree or TChain
0209   Int_t fCurrent;  //!current Tree number in a TChain
0210 
0211   // Declaration of leaf types
0212   Int_t t_Run;
0213   Int_t t_Event;
0214   Int_t t_DataType;
0215   Int_t t_ieta;
0216   Int_t t_iphi;
0217   Double_t t_EventWeight;
0218   Int_t t_nVtx;
0219   Int_t t_nTrk;
0220   Int_t t_goodPV;
0221   Double_t t_l1pt;
0222   Double_t t_l1eta;
0223   Double_t t_l1phi;
0224   Double_t t_l3pt;
0225   Double_t t_l3eta;
0226   Double_t t_l3phi;
0227   Double_t t_p;
0228   Double_t t_pt;
0229   Double_t t_phi;
0230   Double_t t_mindR1;
0231   Double_t t_mindR2;
0232   Double_t t_eMipDR;
0233   Double_t t_eHcal;
0234   Double_t t_eHcal10;
0235   Double_t t_eHcal30;
0236   Double_t t_hmaxNearP;
0237   Double_t t_rhoh;
0238   Bool_t t_selectTk;
0239   Bool_t t_qltyFlag;
0240   Bool_t t_qltyMissFlag;
0241   Bool_t t_qltyPVFlag;
0242   Double_t t_gentrackP;
0243   std::vector<unsigned int> *t_DetIds;
0244   std::vector<double> *t_HitEnergies;
0245   std::vector<bool> *t_trgbits;
0246   std::vector<unsigned int> *t_DetIds1;
0247   std::vector<unsigned int> *t_DetIds3;
0248   std::vector<double> *t_HitEnergies1;
0249   std::vector<double> *t_HitEnergies3;
0250 
0251   // List of branches
0252   TBranch *b_t_Run;           //!
0253   TBranch *b_t_Event;         //!
0254   TBranch *b_t_DataType;      //!
0255   TBranch *b_t_ieta;          //!
0256   TBranch *b_t_iphi;          //!
0257   TBranch *b_t_EventWeight;   //!
0258   TBranch *b_t_nVtx;          //!
0259   TBranch *b_t_nTrk;          //!
0260   TBranch *b_t_goodPV;        //!
0261   TBranch *b_t_l1pt;          //!
0262   TBranch *b_t_l1eta;         //!
0263   TBranch *b_t_l1phi;         //!
0264   TBranch *b_t_l3pt;          //!
0265   TBranch *b_t_l3eta;         //!
0266   TBranch *b_t_l3phi;         //!
0267   TBranch *b_t_p;             //!
0268   TBranch *b_t_pt;            //!
0269   TBranch *b_t_phi;           //!
0270   TBranch *b_t_mindR1;        //!
0271   TBranch *b_t_mindR2;        //!
0272   TBranch *b_t_eMipDR;        //!
0273   TBranch *b_t_eHcal;         //!
0274   TBranch *b_t_eHcal10;       //!
0275   TBranch *b_t_eHcal30;       //!
0276   TBranch *b_t_hmaxNearP;     //!
0277   TBranch *b_t_rhoh;          //!
0278   TBranch *b_t_selectTk;      //!
0279   TBranch *b_t_qltyFlag;      //!
0280   TBranch *b_t_qltyMissFlag;  //!
0281   TBranch *b_t_qltyPVFlag;    //!
0282   TBranch *b_t_gentrackP;     //!
0283   TBranch *b_t_DetIds;        //!
0284   TBranch *b_t_HitEnergies;   //!
0285   TBranch *b_t_trgbits;       //!
0286   TBranch *b_t_DetIds1;       //!
0287   TBranch *b_t_DetIds3;       //!
0288   TBranch *b_t_HitEnergies1;  //!
0289   TBranch *b_t_HitEnergies3;  //!
0290 
0291   CalibPlotProperties(const char *fname,
0292                       const std::string &dirname,
0293                       const char *dupFileName,
0294                       const std::string &prefix = "",
0295                       const char *corrFileName = "",
0296                       const char *rcorFileName = "",
0297                       int puCorr = -8,
0298                       int flag = 101111,
0299                       bool isRealData = true,
0300                       int truncateFlag = 0,
0301                       bool useGen = false,
0302                       double scale = 1.0,
0303                       int useScale = 0,
0304                       int etalo = 0,
0305                       int etahi = 30,
0306                       int runlo = 0,
0307                       int runhi = 99999999,
0308                       int phimin = 1,
0309                       int phimax = 72,
0310                       int zside = 1,
0311                       int nvxlo = 0,
0312                       int nvxhi = 1000,
0313                       const char *rbxFile = "",
0314                       const char *badRunFile = "",
0315                       bool exclude = false,
0316                       bool etamax = false);
0317   virtual ~CalibPlotProperties();
0318   virtual Int_t Cut(Long64_t entry);
0319   virtual Int_t GetEntry(Long64_t entry);
0320   virtual Long64_t LoadTree(Long64_t entry);
0321   virtual void Init(TChain *);
0322   virtual void Loop(Long64_t nentries = -1);
0323   virtual Bool_t Notify();
0324   virtual void Show(Long64_t entry = -1);
0325   bool goodTrack(double &eHcal, double &cut, bool debug);
0326   bool selectPhi(bool debug);
0327   void savePlot(const std::string &theName, bool append, bool all = false, bool debug = false);
0328   void correctEnergy(double &ener);
0329   std::vector<double> rawEnergy(bool debug);
0330 
0331 private:
0332   static const int kp50 = 2;
0333   CalibCorrFactor *corrFactor_;
0334   CalibCorr *cFactor_;
0335   CalibSelectRBX *cSelect_;
0336   CalibDuplicate *cDuplicate_;
0337   CalibThreshold *cThr_;
0338   CalibExcludeRuns *cRunEx_;
0339   const std::string fname_, dirnm_, prefix_, outFileName_;
0340   const int corrPU_, flag_;
0341   const bool isRealData_, useGen_;
0342   const int truncateFlag_;
0343   const int etalo_, etahi_;
0344   int runlo_, runhi_;
0345   const int phimin_, phimax_, zside_, nvxlo_, nvxhi_;
0346   const char *rbxFile_;
0347   bool exclude_, corrE_, cutL1T_;
0348   bool includeRun_, getHist_;
0349   int flexibleSelect_, ifDepth_, duplicate_, thrForm_;
0350   bool plotBasic_, plotEnergy_, plotHists_;
0351   double log2by18_;
0352   std::ofstream fileout_;
0353   std::vector<std::pair<int, int> > events_;
0354   TH1D *h_p[CalibPlots::ntitles];
0355   TH1D *h_eta[CalibPlots::ntitles], *h_nvtx, *h_nvtxEv, *h_nvtxTk;
0356   std::vector<TH1D *> h_eta0, h_eta1, h_eta2, h_eta3, h_eta4;
0357   std::vector<TH1D *> h_dL1, h_vtx;
0358   std::vector<TH1D *> h_etaEH[CalibPlots::npbin0];
0359   std::vector<TH1D *> h_etaEp[CalibPlots::npbin0];
0360   std::vector<TH1D *> h_etaEE[CalibPlots::npbin0];
0361   std::vector<TH1D *> h_etaEE0[CalibPlots::npbin0];
0362   std::vector<TH1D *> h_mom, h_eEcal, h_eHcal;
0363   std::vector<TH1D *> h_eHdepth[CalibPlots::ndepth];
0364   std::vector<TH1F *> h_bvlist, h_bvlist2, h_evlist, h_evlist2;
0365   std::vector<TH1F *> h_bvlist3, h_evlist3;
0366   TH2F *h_etaE;
0367 };
0368 
0369 CalibPlotProperties::CalibPlotProperties(const char *fname,
0370                                          const std::string &dirnm,
0371                                          const char *dupFileName,
0372                                          const std::string &prefix,
0373                                          const char *corrFileName,
0374                                          const char *rcorFileName,
0375                                          int puCorr,
0376                                          int flag,
0377                                          bool isRealData,
0378                                          int truncate,
0379                                          bool useGen,
0380                                          double scl,
0381                                          int useScale,
0382                                          int etalo,
0383                                          int etahi,
0384                                          int runlo,
0385                                          int runhi,
0386                                          int phimin,
0387                                          int phimax,
0388                                          int zside,
0389                                          int nvxlo,
0390                                          int nvxhi,
0391                                          const char *rbxFile,
0392                                          const char *badRunFile,
0393                                          bool exc,
0394                                          bool etam)
0395     : corrFactor_(nullptr),
0396       cFactor_(nullptr),
0397       cSelect_(nullptr),
0398       cDuplicate_(nullptr),
0399       cThr_(nullptr),
0400       cRunEx_(nullptr),
0401       fname_(fname),
0402       dirnm_(dirnm),
0403       prefix_(prefix),
0404       corrPU_(puCorr),
0405       flag_(flag),
0406       isRealData_(isRealData),
0407       useGen_(useGen),
0408       truncateFlag_(truncate),
0409       etalo_(etalo),
0410       etahi_(etahi),
0411       runlo_(runlo),
0412       runhi_(runhi),
0413       phimin_(phimin),
0414       phimax_(phimax),
0415       zside_(zside),
0416       nvxlo_(nvxlo),
0417       nvxhi_(nvxhi),
0418       rbxFile_(rbxFile),
0419       exclude_(exc),
0420       includeRun_(true) {
0421   // if parameter tree is not specified (or zero), connect the file
0422   // used to generate this class and read the Tree
0423 
0424   flexibleSelect_ = ((flag_ / 1) % 10);
0425   plotBasic_ = (((flag_ / 10) % 10) > 0);
0426   plotEnergy_ = (((flag_ / 100) % 10) > 0);
0427   int oneplace = ((flag_ / 1000) % 10);
0428   cutL1T_ = (oneplace % 2);
0429   bool marina = ((oneplace / 2) % 2);
0430   ifDepth_ = ((flag_ / 10000) % 10);
0431   plotHists_ = (((flag_ / 100000) % 10) > 0);
0432   duplicate_ = ((flag_ / 1000000) % 10);
0433   log2by18_ = std::log(2.5) / 18.0;
0434   if (runlo_ < 0 || runhi_ < 0) {
0435     runlo_ = std::abs(runlo_);
0436     runhi_ = std::abs(runhi_);
0437     includeRun_ = false;
0438   }
0439   int useScale0 = useScale % 10;
0440   thrForm_ = useScale / 10;
0441   char treeName[400];
0442   sprintf(treeName, "%s/CalibTree", dirnm.c_str());
0443   TChain *chain = new TChain(treeName);
0444   std::cout << "Create a chain for " << treeName << " from " << fname << " flags " << flag_ << "|" << flexibleSelect_
0445             << "|" << plotBasic_ << "|"
0446             << "|" << plotEnergy_ << "|" << plotHists_ << "|" << corrPU_ << " cons " << log2by18_ << " eta range "
0447             << etalo_ << ":" << etahi_ << " run range " << runlo_ << ":" << runhi_ << " (inclusion flag " << includeRun_
0448             << ") Vertex Range " << nvxlo_ << ":" << nvxhi_ << " Threshold Flag " << thrForm_ << std::endl;
0449   corrFactor_ = new CalibCorrFactor(corrFileName, useScale0, scl, etam, marina, false);
0450   if (!fillChain(chain, fname)) {
0451     std::cout << "*****No valid tree chain can be obtained*****" << std::endl;
0452   } else {
0453     std::cout << "Proceed with a tree chain with " << chain->GetEntries() << " entries" << std::endl;
0454     Init(chain);
0455     if (std::string(rcorFileName) != "") {
0456       std::cout << "rcorFileName: " << rcorFileName << std::endl;
0457       cFactor_ = new CalibCorr(rcorFileName, ifDepth_, false);
0458       if (cFactor_->absent())
0459         ifDepth_ = -1;
0460     } else {
0461       ifDepth_ = -1;
0462     }
0463     if (std::string(dupFileName) != "") {
0464       std::cout << "dupFileName: " << dupFileName << std::endl;
0465       cDuplicate_ = new CalibDuplicate(dupFileName, duplicate_, false);
0466     }
0467     if (std::string(rbxFile) != "") {
0468       std::cout << "RBX File: " << rbxFile << std::endl;
0469       cSelect_ = new CalibSelectRBX(rbxFile, false);
0470     }
0471     if (thrForm_ > 0)
0472       cThr_ = new CalibThreshold(thrForm_);
0473     if (std::string(badRunFile) != "") {
0474       std::cout << "File with list of excluded runs: " << badRunFile << std::endl;
0475       cRunEx_ = new CalibExcludeRuns(badRunFile, false);
0476     }
0477   }
0478 }
0479 
0480 CalibPlotProperties::~CalibPlotProperties() {
0481   delete corrFactor_;
0482   delete cFactor_;
0483   delete cSelect_;
0484   delete cDuplicate_;
0485   delete cThr_;
0486   delete cRunEx_;
0487   if (!fChain)
0488     return;
0489   delete fChain->GetCurrentFile();
0490 }
0491 
0492 Int_t CalibPlotProperties::GetEntry(Long64_t entry) {
0493   // Read contents of entry.
0494   if (!fChain)
0495     return 0;
0496   return fChain->GetEntry(entry);
0497 }
0498 
0499 Long64_t CalibPlotProperties::LoadTree(Long64_t entry) {
0500   // Set the environment to read one entry
0501   if (!fChain)
0502     return -5;
0503   Long64_t centry = fChain->LoadTree(entry);
0504   if (centry < 0)
0505     return centry;
0506   if (!fChain->InheritsFrom(TChain::Class()))
0507     return centry;
0508   TChain *chain = (TChain *)fChain;
0509   if (chain->GetTreeNumber() != fCurrent) {
0510     fCurrent = chain->GetTreeNumber();
0511     Notify();
0512   }
0513   return centry;
0514 }
0515 
0516 void CalibPlotProperties::Init(TChain *tree) {
0517   // The Init() function is called when the selector needs to initialize
0518   // a new tree or chain. Typically here the branch addresses and branch
0519   // pointers of the tree will be set.
0520   // It is normally not necessary to make changes to the generated
0521   // code, but the routine can be extended by the user if needed.
0522   // Init() will be called many times when running on PROOF
0523   // (once per file to be processed).
0524 
0525   // Set object pointer
0526   t_DetIds = 0;
0527   t_DetIds1 = 0;
0528   t_DetIds3 = 0;
0529   t_HitEnergies = 0;
0530   t_HitEnergies1 = 0;
0531   t_HitEnergies3 = 0;
0532   t_trgbits = 0;
0533   // Set branch addresses and branch pointers
0534   fChain = tree;
0535   fCurrent = -1;
0536   if (!tree)
0537     return;
0538   fChain->SetMakeClass(1);
0539 
0540   fChain->SetBranchAddress("t_Run", &t_Run, &b_t_Run);
0541   fChain->SetBranchAddress("t_Event", &t_Event, &b_t_Event);
0542   fChain->SetBranchAddress("t_DataType", &t_DataType, &b_t_DataType);
0543   fChain->SetBranchAddress("t_ieta", &t_ieta, &b_t_ieta);
0544   fChain->SetBranchAddress("t_iphi", &t_iphi, &b_t_iphi);
0545   fChain->SetBranchAddress("t_EventWeight", &t_EventWeight, &b_t_EventWeight);
0546   fChain->SetBranchAddress("t_nVtx", &t_nVtx, &b_t_nVtx);
0547   fChain->SetBranchAddress("t_nTrk", &t_nTrk, &b_t_nTrk);
0548   fChain->SetBranchAddress("t_goodPV", &t_goodPV, &b_t_goodPV);
0549   fChain->SetBranchAddress("t_l1pt", &t_l1pt, &b_t_l1pt);
0550   fChain->SetBranchAddress("t_l1eta", &t_l1eta, &b_t_l1eta);
0551   fChain->SetBranchAddress("t_l1phi", &t_l1phi, &b_t_l1phi);
0552   fChain->SetBranchAddress("t_l3pt", &t_l3pt, &b_t_l3pt);
0553   fChain->SetBranchAddress("t_l3eta", &t_l3eta, &b_t_l3eta);
0554   fChain->SetBranchAddress("t_l3phi", &t_l3phi, &b_t_l3phi);
0555   fChain->SetBranchAddress("t_p", &t_p, &b_t_p);
0556   fChain->SetBranchAddress("t_pt", &t_pt, &b_t_pt);
0557   fChain->SetBranchAddress("t_phi", &t_phi, &b_t_phi);
0558   fChain->SetBranchAddress("t_mindR1", &t_mindR1, &b_t_mindR1);
0559   fChain->SetBranchAddress("t_mindR2", &t_mindR2, &b_t_mindR2);
0560   fChain->SetBranchAddress("t_eMipDR", &t_eMipDR, &b_t_eMipDR);
0561   fChain->SetBranchAddress("t_eHcal", &t_eHcal, &b_t_eHcal);
0562   fChain->SetBranchAddress("t_eHcal10", &t_eHcal10, &b_t_eHcal10);
0563   fChain->SetBranchAddress("t_eHcal30", &t_eHcal30, &b_t_eHcal30);
0564   fChain->SetBranchAddress("t_hmaxNearP", &t_hmaxNearP, &b_t_hmaxNearP);
0565   fChain->SetBranchAddress("t_rhoh", &t_rhoh, &b_t_rhoh);
0566   fChain->SetBranchAddress("t_selectTk", &t_selectTk, &b_t_selectTk);
0567   fChain->SetBranchAddress("t_qltyFlag", &t_qltyFlag, &b_t_qltyFlag);
0568   fChain->SetBranchAddress("t_qltyMissFlag", &t_qltyMissFlag, &b_t_qltyMissFlag);
0569   fChain->SetBranchAddress("t_qltyPVFlag", &t_qltyPVFlag, &b_t_qltyPVFlag);
0570   fChain->SetBranchAddress("t_gentrackP", &t_gentrackP, &b_t_gentrackP);
0571   fChain->SetBranchAddress("t_DetIds", &t_DetIds, &b_t_DetIds);
0572   fChain->SetBranchAddress("t_HitEnergies", &t_HitEnergies, &b_t_HitEnergies);
0573   fChain->SetBranchAddress("t_trgbits", &t_trgbits, &b_t_trgbits);
0574   fChain->SetBranchAddress("t_DetIds1", &t_DetIds1, &b_t_DetIds1);
0575   fChain->SetBranchAddress("t_DetIds3", &t_DetIds3, &b_t_DetIds3);
0576   fChain->SetBranchAddress("t_HitEnergies1", &t_HitEnergies1, &b_t_HitEnergies1);
0577   fChain->SetBranchAddress("t_HitEnergies3", &t_HitEnergies3, &b_t_HitEnergies3);
0578   Notify();
0579 
0580   char name[20], title[200];
0581   unsigned int kk(0);
0582 
0583   if (plotBasic_) {
0584     std::cout << "Book Basic Histos" << std::endl;
0585     h_nvtx = new TH1D("hnvtx", "Number of vertices (selected entries)", 10, 0, 100);
0586     h_nvtx->Sumw2();
0587     h_nvtxEv = new TH1D("hnvtxEv", "Number of vertices (selected events)", 10, 0, 100);
0588     h_nvtxEv->Sumw2();
0589     h_nvtxTk = new TH1D("hnvtxTk", "Number of vertices (selected tracks)", 10, 0, 100);
0590     h_nvtxTk->Sumw2();
0591     for (int k = 0; k < CalibPlots::ntitles; ++k) {
0592       sprintf(name, "%sp%d", prefix_.c_str(), k);
0593       sprintf(title, "Momentum for %s", CalibPlots::getTitle(k).c_str());
0594       h_p[k] = new TH1D(name, title, 150, 0.0, 150.0);
0595       sprintf(name, "%seta%d", prefix_.c_str(), k);
0596       sprintf(title, "#eta for %s", CalibPlots::getTitle(k).c_str());
0597       h_eta[k] = new TH1D(name, title, 60, -30.0, 30.0);
0598     }
0599     for (int k = 0; k < CalibPlots::npbin; ++k) {
0600       sprintf(name, "%seta0%d", prefix_.c_str(), k);
0601       sprintf(title,
0602               "#eta for %s (p = %d:%d GeV)",
0603               CalibPlots::getTitle(0).c_str(),
0604               CalibPlots::getP(k),
0605               CalibPlots::getP(k + 1));
0606       h_eta0.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0607       kk = h_eta0.size() - 1;
0608       h_eta0[kk]->Sumw2();
0609       sprintf(name, "%seta1%d", prefix_.c_str(), k);
0610       sprintf(title,
0611               "#eta for %s (p = %d:%d GeV)",
0612               CalibPlots::getTitle(1).c_str(),
0613               CalibPlots::getP(k),
0614               CalibPlots::getP(k + 1));
0615       h_eta1.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0616       kk = h_eta1.size() - 1;
0617       h_eta1[kk]->Sumw2();
0618       sprintf(name, "%seta2%d", prefix_.c_str(), k);
0619       sprintf(title,
0620               "#eta for %s (p = %d:%d GeV)",
0621               CalibPlots::getTitle(2).c_str(),
0622               CalibPlots::getP(k),
0623               CalibPlots::getP(k + 1));
0624       h_eta2.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0625       kk = h_eta2.size() - 1;
0626       h_eta2[kk]->Sumw2();
0627       sprintf(name, "%seta3%d", prefix_.c_str(), k);
0628       sprintf(title,
0629               "#eta for %s (p = %d:%d GeV)",
0630               CalibPlots::getTitle(3).c_str(),
0631               CalibPlots::getP(k),
0632               CalibPlots::getP(k + 1));
0633       h_eta3.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0634       kk = h_eta3.size() - 1;
0635       h_eta3[kk]->Sumw2();
0636       sprintf(name, "%seta4%d", prefix_.c_str(), k);
0637       sprintf(title,
0638               "#eta for %s (p = %d:%d GeV)",
0639               CalibPlots::getTitle(4).c_str(),
0640               CalibPlots::getP(k),
0641               CalibPlots::getP(k + 1));
0642       h_eta4.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0643       kk = h_eta4.size() - 1;
0644       h_eta4[kk]->Sumw2();
0645       sprintf(name, "%sdl1%d", prefix_.c_str(), k);
0646       sprintf(title, "Distance from L1 (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
0647       h_dL1.push_back(new TH1D(name, title, 160, 0.0, 8.0));
0648       kk = h_dL1.size() - 1;
0649       h_dL1[kk]->Sumw2();
0650       sprintf(name, "%svtx%d", prefix_.c_str(), k);
0651       sprintf(title, "N_{Vertex} (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
0652       h_vtx.push_back(new TH1D(name, title, 100, 0.0, 100.0));
0653       kk = h_vtx.size() - 1;
0654       h_vtx[kk]->Sumw2();
0655     }
0656   }
0657 
0658   if (plotEnergy_) {
0659     std::cout << "Make plots for good tracks" << std::endl;
0660     for (int k = 0; k < CalibPlots::npbin; ++k) {
0661       for (int j = etalo_; j <= etahi_ + 1; ++j) {
0662         sprintf(name, "%senergyH%d%d", prefix_.c_str(), k, j);
0663         if (j > etahi_)
0664           sprintf(title,
0665                   "HCAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
0666                   CalibPlots::getTitle(3).c_str(),
0667                   CalibPlots::getP(k),
0668                   CalibPlots::getP(k + 1),
0669                   etalo_,
0670                   etahi_);
0671         else
0672           sprintf(title,
0673                   "HCAL energy for %s (p = %d:%d GeV |#eta| = %d)",
0674                   CalibPlots::getTitle(3).c_str(),
0675                   CalibPlots::getP(k),
0676                   CalibPlots::getP(k + 1),
0677                   j);
0678         h_etaEH[k].push_back(new TH1D(name, title, 200, 0, 200));
0679         kk = h_etaEH[k].size() - 1;
0680         h_etaEH[k][kk]->Sumw2();
0681         sprintf(name, "%senergyP%d%d", prefix_.c_str(), k, j);
0682         if (j > etahi_)
0683           sprintf(title,
0684                   "momentum for %s (p = %d:%d GeV |#eta| = %d:%d)",
0685                   CalibPlots::getTitle(3).c_str(),
0686                   CalibPlots::getP(k),
0687                   CalibPlots::getP(k + 1),
0688                   etalo_,
0689                   etahi_);
0690         else
0691           sprintf(title,
0692                   "momentum for %s (p = %d:%d GeV |#eta| = %d)",
0693                   CalibPlots::getTitle(3).c_str(),
0694                   CalibPlots::getP(k),
0695                   CalibPlots::getP(k + 1),
0696                   j);
0697         h_etaEp[k].push_back(new TH1D(name, title, 100, 0, 100));
0698         kk = h_etaEp[k].size() - 1;
0699         h_etaEp[k][kk]->Sumw2();
0700         sprintf(name, "%senergyE%d%d", prefix_.c_str(), k, j);
0701         if (j > etahi_)
0702           sprintf(title,
0703                   "ECAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
0704                   CalibPlots::getTitle(3).c_str(),
0705                   CalibPlots::getP(k),
0706                   CalibPlots::getP(k + 1),
0707                   etalo_,
0708                   etahi_);
0709         else
0710           sprintf(title,
0711                   "ECAL energy for %s (p = %d:%d GeV |#eta| = %d)",
0712                   CalibPlots::getTitle(3).c_str(),
0713                   CalibPlots::getP(k),
0714                   CalibPlots::getP(k + 1),
0715                   j);
0716         h_etaEE[k].push_back(new TH1D(name, title, 100, 0, 10));
0717         kk = h_etaEE[k].size() - 1;
0718         h_etaEE[k][kk]->Sumw2();
0719         sprintf(name, "%senergyER%d%d", prefix_.c_str(), k, j);
0720         h_etaEE0[k].push_back(new TH1D(name, title, 100, 0, 1));
0721         kk = h_etaEE0[k].size() - 1;
0722         h_etaEE0[k][kk]->Sumw2();
0723       }
0724     }
0725 
0726     for (int j = 0; j < CalibPlots::netabin; ++j) {
0727       sprintf(name, "%senergyH%d", prefix_.c_str(), j);
0728       if (j == 0)
0729         sprintf(title, "HCAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
0730       else
0731         sprintf(title,
0732                 "HCAL energy for %s (|#eta| = %d:%d)",
0733                 CalibPlots::getTitle(3).c_str(),
0734                 CalibPlots::getEta(j - 1),
0735                 CalibPlots::getEta(j));
0736       h_eHcal.push_back(new TH1D(name, title, 200, 0, 200));
0737       kk = h_eHcal.size() - 1;
0738       h_eHcal[kk]->Sumw2();
0739       sprintf(name, "%senergyP%d", prefix_.c_str(), j);
0740       if (j == 0)
0741         sprintf(title, "Track momentum for %s (All)", CalibPlots::getTitle(3).c_str());
0742       else
0743         sprintf(title,
0744                 "Track momentum for %s (|#eta| = %d:%d)",
0745                 CalibPlots::getTitle(3).c_str(),
0746                 CalibPlots::getEta(j - 1),
0747                 CalibPlots::getEta(j));
0748       h_mom.push_back(new TH1D(name, title, 100, 0, 100));
0749       kk = h_mom.size() - 1;
0750       h_mom[kk]->Sumw2();
0751       sprintf(name, "%senergyE%d", prefix_.c_str(), j);
0752       if (j == 0)
0753         sprintf(title, "ECAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
0754       else
0755         sprintf(title,
0756                 "ECAL energy for %s (|#eta| = %d:%d)",
0757                 CalibPlots::getTitle(3).c_str(),
0758                 CalibPlots::getEta(j - 1),
0759                 CalibPlots::getEta(j));
0760       h_eEcal.push_back(new TH1D(name, title, 100, 0, 10));
0761       kk = h_eEcal.size() - 1;
0762       h_eEcal[kk]->Sumw2();
0763 
0764       for (int k = 0; k < CalibPlots::ndepth; ++k) {
0765         sprintf(name, "%sEnergyDepth%dEta%d", prefix_.c_str(), k, j);
0766         sprintf(title,
0767                 "HCAL energy for Depth %d (|#eta| = %d:%d)",
0768                 (k + 1),
0769                 CalibPlots::getEta(j - 1),
0770                 CalibPlots::getEta(j));
0771         h_eHdepth[k].push_back(new TH1D(name, title, 100, 0, 50));
0772         kk = h_eHdepth[k].size() - 1;
0773         h_eHdepth[k][kk]->Sumw2();
0774       }
0775     }
0776   }
0777 
0778   if (plotHists_) {
0779     for (int i = 0; i < CalibPlots::ndepth; i++) {
0780       sprintf(name, "b_edepth%d", i);
0781       sprintf(title, "Total RecHit energy in depth %d (Barrel)", i + 1);
0782       h_bvlist.push_back(new TH1F(name, title, 1000, 0, 100));
0783       h_bvlist[i]->Sumw2();
0784       sprintf(name, "b_recedepth%d", i);
0785       sprintf(title, "RecHit energy in depth %d (Barrel)", i + 1);
0786       h_bvlist2.push_back(new TH1F(name, title, 1000, 0, 100));
0787       h_bvlist2[i]->Sumw2();
0788       sprintf(name, "b_nrecdepth%d", i);
0789       sprintf(title, "#RecHits in depth %d (Barrel)", i + 1);
0790       h_bvlist3.push_back(new TH1F(name, title, 1000, 0, 100));
0791       h_bvlist3[i]->Sumw2();
0792       sprintf(name, "e_edepth%d", i);
0793       sprintf(title, "Total RecHit energy in depth %d (Endcap)", i + 1);
0794       h_evlist.push_back(new TH1F(name, title, 1000, 0, 100));
0795       h_evlist[i]->Sumw2();
0796       sprintf(name, "e_recedepth%d", i);
0797       sprintf(title, "RecHit energy in depth %d (Endcap)", i + 1);
0798       h_evlist2.push_back(new TH1F(name, title, 1000, 0, 100));
0799       h_evlist2[i]->Sumw2();
0800       sprintf(name, "e_nrecdepth%d", i);
0801       sprintf(title, "#RecHits in depth %d (Endcap)", i + 1);
0802       h_evlist3.push_back(new TH1F(name, title, 1000, 0, 100));
0803       h_evlist3[i]->Sumw2();
0804     }
0805     h_etaE = new TH2F("heta", "", 60, -30, 30, 100, 0, 100);
0806   }
0807 }
0808 
0809 Bool_t CalibPlotProperties::Notify() {
0810   // The Notify() function is called when a new file is opened. This
0811   // can be either for a new TTree in a TChain or when when a new TTree
0812   // is started when using PROOF. It is normally not necessary to make changes
0813   // to the generated code, but the routine can be extended by the
0814   // user if needed. The return value is currently not used.
0815 
0816   return kTRUE;
0817 }
0818 
0819 void CalibPlotProperties::Show(Long64_t entry) {
0820   // Print contents of entry.
0821   // If entry is not specified, print current entry
0822   if (!fChain)
0823     return;
0824   fChain->Show(entry);
0825 }
0826 
0827 Int_t CalibPlotProperties::Cut(Long64_t) {
0828   // This function may be called from Loop.
0829   // returns  1 if entry is accepted.
0830   // returns -1 otherwise.
0831   return 1;
0832 }
0833 
0834 void CalibPlotProperties::Loop(Long64_t nentries) {
0835   //   In a ROOT session, you can do:
0836   //      Root > .L CalibMonitor.C
0837   //      Root > CalibMonitor t
0838   //      Root > t.GetEntry(12); // Fill t data members with entry number 12
0839   //      Root > t.Show();       // Show values of entry 12
0840   //      Root > t.Show(16);     // Read and show values of entry 16
0841   //      Root > t.Loop();       // Loop on all entries
0842   //
0843 
0844   //   This is the loop skeleton where:
0845   //      jentry is the global entry number in the chain
0846   //      ientry is the entry number in the current Tree
0847   //   Note that the argument to GetEntry must be:
0848   //      jentry for TChain::GetEntry
0849   //      ientry for TTree::GetEntry and TBranch::GetEntry
0850   //
0851   //       To read only selected branches, Insert statements like:
0852   // METHOD1:
0853   //    fChain->SetBranchStatus("*",0);  // disable all branches
0854   //    fChain->SetBranchStatus("branchname",1);  // activate branchname
0855   // METHOD2: replace line
0856   //    fChain->GetEntry(jentry);       //read all branches
0857   //by  b_branchname->GetEntry(ientry); //read only this branch
0858   const bool debug(false);
0859 
0860   if (fChain == 0)
0861     return;
0862 
0863   // Find list of duplicate events
0864   if (nentries < 0)
0865     nentries = fChain->GetEntriesFast();
0866   std::cout << "Total entries " << nentries << ":" << fChain->GetEntriesFast() << std::endl;
0867   std::vector<std::pair<int, int> > runEvent;
0868   Long64_t nbytes(0), nb(0);
0869   unsigned int duplicate(0), good(0), kount(0);
0870   double sel(0), selHB(0), selHE(0);
0871   for (Long64_t jentry = 0; jentry < nentries; jentry++) {
0872     Long64_t ientry = LoadTree(jentry);
0873     if (ientry < 0)
0874       break;
0875     nb = fChain->GetEntry(jentry);
0876     nbytes += nb;
0877     if (jentry % 1000000 == 0)
0878       std::cout << "Entry " << jentry << " Run " << t_Run << " Event " << t_Event << std::endl;
0879     bool select = ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(0))) ? (cDuplicate_->isDuplicate(jentry)) : true;
0880     bool reject = (cRunEx_ != nullptr) ? cRunEx_->exclude(t_Run) : false;
0881     if ((!select) || reject) {
0882       ++duplicate;
0883       if (debug)
0884         std::cout << "Duplicate event " << t_Run << " " << t_Event << " " << t_p << std::endl;
0885       continue;
0886     }
0887     bool selRun = (includeRun_ ? ((t_Run >= runlo_) && (t_Run <= runhi_)) : ((t_Run < runlo_) || (t_Run > runhi_)));
0888     select =
0889         (selRun && (fabs(t_ieta) >= etalo_) && (fabs(t_ieta) <= etahi_) && (t_nVtx >= nvxlo_) && (t_nVtx <= nvxhi_));
0890     if (!select) {
0891       if (debug)
0892         std::cout << "Run # " << t_Run << " out of range of " << runlo_ << ":" << runhi_ << " or ieta " << t_ieta
0893                   << " (" << etalo_ << ":" << etahi_ << ") or nvtx " << t_nVtx << " (" << nvxlo_ << ":" << nvxhi_
0894                   << ") out of range" << std::endl;
0895       continue;
0896     }
0897     if (cSelect_ != nullptr) {
0898       if (exclude_) {
0899         if (cSelect_->isItRBX(t_DetIds))
0900           continue;
0901       } else {
0902         if (!(cSelect_->isItRBX(t_ieta, t_iphi)))
0903           continue;
0904       }
0905     }
0906     if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(2))) {
0907       if (cDuplicate_->select(t_ieta, t_iphi))
0908         continue;
0909     }
0910     select = (!cutL1T_ || (t_mindR1 >= 0.5));
0911     if (!select) {
0912       if (debug)
0913         std::cout << "Reject Run # " << t_Run << " Event # " << t_Event << " too close to L1 trigger " << t_mindR1
0914                   << std::endl;
0915       continue;
0916     }
0917     select = ((events_.size() == 0) ||
0918               (std::find(events_.begin(), events_.end(), std::pair<int, int>(t_Run, t_Event)) != events_.end()));
0919     if (!select) {
0920       if (debug)
0921         std::cout << "Reject Run # " << t_Run << " Event # " << t_Event << " not in the selection list" << std::endl;
0922       continue;
0923     }
0924 
0925     if (plotBasic_) {
0926       h_nvtx->Fill(t_nVtx);
0927       std::pair<int, int> runEv(t_Run, t_Event);
0928       if (std::find(runEvent.begin(), runEvent.end(), runEv) == runEvent.end()) {
0929         h_nvtxEv->Fill(t_nVtx);
0930         runEvent.push_back(runEv);
0931       }
0932     }
0933 
0934     // if (Cut(ientry) < 0) continue;
0935     int kp = CalibPlots::npbin0;
0936     double pmom = (useGen_ && (t_gentrackP > 0)) ? t_gentrackP : t_p;
0937     for (int k = 1; k < CalibPlots::npbin0; ++k) {
0938       if (pmom >= CalibPlots::getMomentum(k - 1) && pmom < CalibPlots::getMomentum(k)) {
0939         kp = k - 1;
0940         break;
0941       }
0942     }
0943     int jp1 = (((std::abs(t_ieta) >= etalo_) && (std::abs(t_ieta) <= etahi_)) ? (std::abs(t_ieta) - etalo_) : -1);
0944     int jp2 = (etahi_ - etalo_ + 1);
0945     int je1 = CalibPlots::netabin;
0946     for (int j = 1; j < CalibPlots::netabin; ++j) {
0947       if (std::abs(t_ieta) > CalibPlots::getEta(j - 1) && std::abs(t_ieta) <= CalibPlots::getEta(j)) {
0948         je1 = j;
0949         break;
0950       }
0951     }
0952     int je2 = (je1 != CalibPlots::netabin) ? 0 : -1;
0953     if (debug)
0954       std::cout << "Bin " << kp << ":" << je1 << ":" << je2 << ":" << jp1 << ":" << jp2 << std::endl;
0955     double cut = (pmom > 20) ? ((flexibleSelect_ == 0) ? 2.0 : 10.0) : 0.0;
0956     double rcut(-1000.0);
0957 
0958     // Selection of good track and energy measured in Hcal
0959     double eHcal(t_eHcal);
0960     if (corrFactor_->doCorr()) {
0961       eHcal = 0;
0962       for (unsigned int k = 0; k < t_HitEnergies->size(); ++k) {
0963         // Apply thresholds if necessary
0964         bool okcell = (thrForm_ == 0) || ((*t_HitEnergies)[k] > (cThr_->threshold((*t_DetIds)[k])));
0965         if (okcell) {
0966           // The masks are defined in DataFormats/HcalDetId/interface/HcalDetId.h
0967           unsigned int id = truncateId((*t_DetIds)[k], truncateFlag_, false);
0968           double cfac = corrFactor_->getCorr(id);
0969           if ((cFactor_ != 0) && (ifDepth_ != 3) && (ifDepth_ > 0))
0970             cfac *= cFactor_->getCorr(t_Run, (*t_DetIds)[k]);
0971           if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(1)))
0972             cfac *= cDuplicate_->getWeight((*t_DetIds)[k]);
0973           if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(3))) {
0974             int subdet, zside, ieta, iphi, depth;
0975             unpackDetId((*t_DetIds)[k], subdet, zside, ieta, iphi, depth);
0976             cfac *= cDuplicate_->getCorr(t_Run, ieta, depth);
0977           }
0978           eHcal += (cfac * ((*t_HitEnergies)[k]));
0979           if (debug) {
0980             int subdet, zside, ieta, iphi, depth;
0981             unpackDetId(id, subdet, zside, ieta, iphi, depth);
0982             std::cout << zside << ":" << ieta << ":" << depth << " Corr " << cfac << " " << (*t_HitEnergies)[k]
0983                       << " Out " << eHcal << std::endl;
0984           }
0985         }
0986       }
0987     }
0988     bool goodTk = goodTrack(eHcal, cut, debug);
0989     bool selPhi = selectPhi(debug);
0990     double rat = (pmom > 0) ? (eHcal / (pmom - t_eMipDR)) : 1.0;
0991     if (debug)
0992       std::cout << "Entry " << jentry << " p|eHcal|ratio " << pmom << "|" << t_eHcal << "|" << eHcal << "|" << rat
0993                 << "|" << kp << " Cuts " << t_qltyFlag << "|" << t_selectTk << "|" << (t_hmaxNearP < cut) << "|"
0994                 << (t_eMipDR < 1.0) << "|" << goodTk << "|" << (rat > rcut) << " Select Phi " << selPhi << std::endl;
0995     std::vector<double> eDepth = rawEnergy(debug);
0996     if (plotEnergy_) {
0997       if ((je1 > 0) && (je1 < CalibPlots::netabin)) {
0998         for (int k = 0; k < CalibPlots::ndepth; ++k)
0999           h_eHdepth[k][je1]->Fill(eDepth[k], t_EventWeight);
1000       }
1001     }
1002     if (plotBasic_) {
1003       h_nvtxTk->Fill(t_nVtx);
1004       h_p[0]->Fill(pmom, t_EventWeight);
1005       h_eta[0]->Fill(t_ieta, t_EventWeight);
1006       if (kp < CalibPlots::npbin0)
1007         h_eta0[kp]->Fill(t_ieta, t_EventWeight);
1008       if (t_qltyFlag) {
1009         h_p[1]->Fill(pmom, t_EventWeight);
1010         h_eta[1]->Fill(t_ieta, t_EventWeight);
1011         if (kp < CalibPlots::npbin0)
1012           h_eta1[kp]->Fill(t_ieta, t_EventWeight);
1013         if (t_selectTk) {
1014           h_p[2]->Fill(pmom, t_EventWeight);
1015           h_eta[2]->Fill(t_ieta, t_EventWeight);
1016           if (kp < CalibPlots::npbin0)
1017             h_eta2[kp]->Fill(t_ieta, t_EventWeight);
1018           if (t_hmaxNearP < cut) {
1019             h_p[3]->Fill(pmom, t_EventWeight);
1020             h_eta[3]->Fill(t_ieta, t_EventWeight);
1021             if (kp < CalibPlots::npbin0)
1022               h_eta3[kp]->Fill(t_ieta, t_EventWeight);
1023             if (t_eMipDR < 1.0) {
1024               h_p[4]->Fill(pmom, t_EventWeight);
1025               h_eta[4]->Fill(t_ieta, t_EventWeight);
1026               if (kp < CalibPlots::npbin0) {
1027                 h_eta4[kp]->Fill(t_ieta, t_EventWeight);
1028                 h_dL1[kp]->Fill(t_mindR1, t_EventWeight);
1029                 h_vtx[kp]->Fill(t_goodPV, t_EventWeight);
1030               }
1031             }
1032           }
1033         }
1034       }
1035     }
1036 
1037     if (goodTk && kp != CalibPlots::npbin0 && selPhi) {
1038       if (rat > rcut) {
1039         if (plotEnergy_) {
1040           if (jp1 >= 0) {
1041             h_etaEH[kp][jp1]->Fill(eHcal, t_EventWeight);
1042             h_etaEH[kp][jp2]->Fill(eHcal, t_EventWeight);
1043             h_etaEp[kp][jp1]->Fill(pmom, t_EventWeight);
1044             h_etaEp[kp][jp2]->Fill(pmom, t_EventWeight);
1045             h_etaEE[kp][jp1]->Fill(t_eMipDR, t_EventWeight);
1046             h_etaEE[kp][jp2]->Fill(t_eMipDR, t_EventWeight);
1047             h_etaEE0[kp][jp1]->Fill(t_eMipDR, t_EventWeight);
1048             h_etaEE0[kp][jp2]->Fill(t_eMipDR, t_EventWeight);
1049           }
1050           if (kp == kp50) {
1051             if (je1 != CalibPlots::netabin) {
1052               h_eHcal[je1]->Fill(eHcal, t_EventWeight);
1053               h_eHcal[je2]->Fill(eHcal, t_EventWeight);
1054               h_mom[je1]->Fill(pmom, t_EventWeight);
1055               h_mom[je2]->Fill(pmom, t_EventWeight);
1056               h_eEcal[je1]->Fill(t_eMipDR, t_EventWeight);
1057               h_eEcal[je2]->Fill(t_eMipDR, t_EventWeight);
1058             }
1059           }
1060         }
1061 
1062         if (plotHists_) {
1063           if ((std::fabs(rat - 1) < 0.15) && (kp == kp50) && ((std::abs(t_ieta) < 15) || (std::abs(t_ieta) > 17))) {
1064             float weight = (isRealData_ ? t_EventWeight : t_EventWeight * puweight(t_nVtx));
1065             h_etaE->Fill(t_ieta, eHcal, weight);
1066             sel += weight;
1067             std::vector<float> bv(7, 0.0f), ev(7, 0.0f);
1068             std::vector<int> bnrec(7, 0), enrec(7, 0);
1069             double eb(0), ee(0);
1070             for (unsigned int k = 0; k < t_HitEnergies->size(); ++k) {
1071               // Apply thresholds if necessary
1072               bool okcell = (thrForm_ == 0) || ((*t_HitEnergies)[k] > (cThr_->threshold((*t_DetIds)[k])));
1073               if (okcell) {
1074                 unsigned int id = truncateId((*t_DetIds)[k], truncateFlag_, false);
1075                 double cfac = corrFactor_->getCorr(id);
1076                 if ((cFactor_ != 0) && (ifDepth_ != 3) && (ifDepth_ > 0))
1077                   cfac *= cFactor_->getCorr(t_Run, (*t_DetIds)[k]);
1078                 if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(1)))
1079                   cfac *= cDuplicate_->getWeight((*t_DetIds)[k]);
1080                 if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(3))) {
1081                   int subdet, zside, ieta, iphi, depth;
1082                   unpackDetId((*t_DetIds)[k], subdet, zside, ieta, iphi, depth);
1083                   cfac *= cDuplicate_->getCorr(t_Run, ieta, depth);
1084                 }
1085                 double ener = cfac * (*t_HitEnergies)[k];
1086                 if (corrPU_)
1087                   correctEnergy(ener);
1088                 unsigned int idx = (unsigned int)((*t_DetIds)[k]);
1089                 int subdet, zside, ieta, iphi, depth;
1090                 unpackDetId(idx, subdet, zside, ieta, iphi, depth);
1091                 if (depth > 0 && depth <= CalibPlots::ndepth) {
1092                   if (subdet == 1) {
1093                     eb += ener;
1094                     bv[depth - 1] += ener;
1095                     h_bvlist2[depth - 1]->Fill(ener, weight);
1096                     ++bnrec[depth - 1];
1097                   } else if (subdet == 2) {
1098                     ee += ener;
1099                     ev[depth - 1] += ener;
1100                     h_evlist2[depth - 1]->Fill(ener, weight);
1101                     ++enrec[depth - 1];
1102                   }
1103                 }
1104               }
1105             }
1106             bool barrel = (eb > ee);
1107             if (barrel)
1108               selHB += weight;
1109             else
1110               selHE += weight;
1111             for (int i = 0; i < CalibPlots::ndepth; i++) {
1112               if (barrel) {
1113                 h_bvlist[i]->Fill(bv[i], weight);
1114                 h_bvlist3[i]->Fill((bnrec[i] + 0.001), weight);
1115               } else {
1116                 h_evlist[i]->Fill(ev[i], weight);
1117                 h_evlist3[i]->Fill((enrec[i] + 0.001), weight);
1118               }
1119             }
1120           }
1121         }
1122       }
1123       good++;
1124     }
1125     ++kount;
1126   }
1127   std::cout << "Finds " << duplicate << " Duplicate events out of " << kount << " events in this file and " << good
1128             << " selected events" << std::endl;
1129   if (plotHists_)
1130     std::cout << "Number of weighted selected events " << sel << " HB " << selHB << " HE " << selHE << std::endl;
1131 }
1132 
1133 bool CalibPlotProperties::goodTrack(double &eHcal, double &cuti, bool debug) {
1134   bool select(true);
1135   double cut(cuti);
1136   if (debug) {
1137     std::cout << "goodTrack input " << eHcal << ":" << cut;
1138   }
1139   if (flexibleSelect_ > 1) {
1140     double eta = (t_ieta > 0) ? t_ieta : -t_ieta;
1141     cut = 8.0 * exp(eta * log2by18_);
1142   }
1143   correctEnergy(eHcal);
1144   select = ((t_qltyFlag) && (t_selectTk) && (t_hmaxNearP < cut) && (t_eMipDR < 100.0));
1145   if (debug) {
1146     std::cout << " output " << eHcal << ":" << cut << ":" << select << std::endl;
1147   }
1148   return select;
1149 }
1150 
1151 bool CalibPlotProperties::selectPhi(bool debug) {
1152   bool select(true);
1153   if (phimin_ > 1 || phimax_ < 72) {
1154     double eTotal(0), eSelec(0);
1155     // The masks are defined in DataFormats/HcalDetId/interface/HcalDetId.h
1156     for (unsigned int k = 0; k < t_HitEnergies->size(); ++k) {
1157       // Apply thresholds if necessary
1158       bool okcell = (thrForm_ == 0) || ((*t_HitEnergies)[k] > (cThr_->threshold((*t_DetIds)[k])));
1159       if (okcell) {
1160         int iphi = ((*t_DetIds)[k]) & (0x3FF);
1161         int zside = ((*t_DetIds)[k] & 0x80000) ? (1) : (-1);
1162         eTotal += ((*t_HitEnergies)[k]);
1163         if (iphi >= phimin_ && iphi <= phimax_ && zside == zside_)
1164           eSelec += ((*t_HitEnergies)[k]);
1165       }
1166     }
1167     if (eSelec < 0.9 * eTotal)
1168       select = false;
1169     if (debug) {
1170       std::cout << "Etotal " << eTotal << " and ESelec " << eSelec << " (phi " << phimin_ << ":" << phimax_ << " z "
1171                 << zside_ << ") Selection " << select << std::endl;
1172     }
1173   }
1174   return select;
1175 }
1176 
1177 void CalibPlotProperties::savePlot(const std::string &theName, bool append, bool all, bool debug) {
1178   TFile *theFile(0);
1179   if (append) {
1180     theFile = new TFile(theName.c_str(), "UPDATE");
1181   } else {
1182     theFile = new TFile(theName.c_str(), "RECREATE");
1183   }
1184 
1185   theFile->cd();
1186 
1187   if (plotBasic_) {
1188     if (debug)
1189       std::cout << "nvtx " << h_nvtx << ":" << h_nvtxEv << ":" << h_nvtxTk << std::endl;
1190     h_nvtx->Write();
1191     h_nvtxEv->Write();
1192     h_nvtxTk->Write();
1193     for (int k = 0; k < CalibPlots::ntitles; ++k) {
1194       if (debug)
1195         std::cout << "[" << k << "] p " << h_p[k] << " eta " << h_eta[k] << std::endl;
1196       h_p[k]->Write();
1197       h_eta[k]->Write();
1198     }
1199     for (int k = 0; k < CalibPlots::npbin; ++k) {
1200       if (debug)
1201         std::cout << "[" << k << "] eta " << h_eta0[k] << ":" << h_eta1[k] << ":" << h_eta2[k] << ":" << h_eta3[k]
1202                   << ":" << h_eta4[k] << " dl " << h_dL1[k] << " vtx " << h_vtx[k] << std::endl;
1203       if (h_eta0[k] != 0) {
1204         TH1D *h1 = (TH1D *)h_eta0[k]->Clone();
1205         h1->Write();
1206       }
1207       if (h_eta1[k] != 0) {
1208         TH1D *h2 = (TH1D *)h_eta1[k]->Clone();
1209         h2->Write();
1210       }
1211       if (h_eta2[k] != 0) {
1212         TH1D *h3 = (TH1D *)h_eta2[k]->Clone();
1213         h3->Write();
1214       }
1215       if (h_eta3[k] != 0) {
1216         TH1D *h4 = (TH1D *)h_eta3[k]->Clone();
1217         h4->Write();
1218       }
1219       if (h_eta4[k] != 0) {
1220         TH1D *h5 = (TH1D *)h_eta4[k]->Clone();
1221         h5->Write();
1222       }
1223       if (h_dL1[k] != 0) {
1224         TH1D *h6 = (TH1D *)h_dL1[k]->Clone();
1225         h6->Write();
1226       }
1227       if (h_vtx[k] != 0) {
1228         TH1D *h7 = (TH1D *)h_vtx[k]->Clone();
1229         h7->Write();
1230       }
1231     }
1232   }
1233 
1234   if (plotEnergy_) {
1235     for (int k = 0; k < CalibPlots::npbin; ++k) {
1236       if (debug)
1237         std::cout << "Energy[" << k << "] "
1238                   << " eta " << etalo_ << ":" << etahi_ << ":" << CalibPlots::netabin << " etaEH " << h_etaEH[k].size()
1239                   << " etaEp " << h_etaEp[k].size() << " etaEE " << h_etaEE[k].size() << " eHcal " << h_eHcal.size()
1240                   << " mom " << h_mom.size() << " eEcal " << h_eEcal.size() << std::endl;
1241       for (unsigned int j = 0; j <= (unsigned int)(etahi_ - etalo_); ++j) {
1242         if (h_etaEH[k].size() > j && h_etaEH[k][j] != nullptr && (all || (k == kp50))) {
1243           TH1D *hist = (TH1D *)h_etaEH[k][j]->Clone();
1244           hist->Write();
1245         }
1246         if (h_etaEp[k].size() > j && h_etaEp[k][j] != nullptr && (all || (k == kp50))) {
1247           TH1D *hist = (TH1D *)h_etaEp[k][j]->Clone();
1248           hist->Write();
1249         }
1250         if (h_etaEE[k].size() > j && h_etaEE[k][j] != nullptr && (all || (k == kp50))) {
1251           TH1D *hist = (TH1D *)h_etaEE[k][j]->Clone();
1252           hist->Write();
1253         }
1254         if (h_etaEE0[k].size() > j && h_etaEE0[k][j] != nullptr && (all || (k == kp50))) {
1255           TH1D *hist = (TH1D *)h_etaEE0[k][j]->Clone();
1256           hist->Write();
1257         }
1258       }
1259     }
1260 
1261     for (int j = 0; j < CalibPlots::netabin; ++j) {
1262       if (h_eHcal.size() > (unsigned int)(j) && (h_eHcal[j] != nullptr)) {
1263         TH1D *hist = (TH1D *)h_eHcal[j]->Clone();
1264         hist->Write();
1265       }
1266       if (h_mom.size() > (unsigned int)(j) && (h_mom[j] != nullptr)) {
1267         TH1D *hist = (TH1D *)h_mom[j]->Clone();
1268         hist->Write();
1269       }
1270       if (h_eEcal.size() > (unsigned int)(j) && (h_eEcal[j] != nullptr)) {
1271         TH1D *hist = (TH1D *)h_eEcal[j]->Clone();
1272         hist->Write();
1273       }
1274     }
1275     for (int j = 1; j < CalibPlots::netabin; ++j) {
1276       for (int k = 0; k < CalibPlots::ndepth; ++k) {
1277         if (h_eHdepth[k].size() > (unsigned int)(j) && (h_eHdepth[k][j] != nullptr)) {
1278           TH1D *hist = (TH1D *)h_eHdepth[k][j]->Clone();
1279           hist->Write();
1280         }
1281       }
1282     }
1283   }
1284 
1285   if (plotHists_) {
1286     if (debug)
1287       std::cout << "etaE " << h_etaE << std::endl;
1288     h_etaE->Write();
1289     for (int i = 0; i < CalibPlots::ndepth; ++i) {
1290       if (debug)
1291         std::cout << "Depth[" << i << "] bv " << h_bvlist[i] << ":" << h_bvlist2[i] << ":" << h_bvlist3[i] << " ev "
1292                   << h_evlist[i] << ":" << h_evlist2[i] << ":" << h_evlist3[i] << std::endl;
1293       h_bvlist[i]->Write();
1294       h_bvlist2[i]->Write();
1295       h_bvlist3[i]->Write();
1296       h_evlist[i]->Write();
1297       h_evlist2[i]->Write();
1298       h_evlist3[i]->Write();
1299     }
1300   }
1301   std::cout << "All done" << std::endl;
1302   theFile->Close();
1303 }
1304 
1305 void CalibPlotProperties::correctEnergy(double &eHcal) {
1306   double pmom = (useGen_ && (t_gentrackP > 0)) ? t_gentrackP : t_p;
1307   if ((corrPU_ < 0) && (pmom > 0)) {
1308     double ediff = (t_eHcal30 - t_eHcal10);
1309     if (t_DetIds1 != 0 && t_DetIds3 != 0) {
1310       double Etot1(0), Etot3(0);
1311       // The masks are defined in DataFormats/HcalDetId/interface/HcalDetId.h
1312       for (unsigned int idet = 0; idet < (*t_DetIds1).size(); idet++) {
1313         // Apply thresholds if necessary
1314         bool okcell = (thrForm_ == 0) || ((*t_HitEnergies1)[idet] > (cThr_->threshold((*t_DetIds1)[idet])));
1315         if (okcell) {
1316           unsigned int id = truncateId((*t_DetIds1)[idet], truncateFlag_, false);
1317           double cfac = corrFactor_->getCorr(id);
1318           if ((cFactor_ != 0) && (ifDepth_ != 3) && (ifDepth_ > 0))
1319             cfac *= cFactor_->getCorr(t_Run, (*t_DetIds1)[idet]);
1320           if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(1)))
1321             cfac *= cDuplicate_->getWeight((*t_DetIds1)[idet]);
1322           if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(3))) {
1323             int subdet, zside, ieta, iphi, depth;
1324             unpackDetId((*t_DetIds1)[idet], subdet, zside, ieta, iphi, depth);
1325             cfac *= cDuplicate_->getCorr(t_Run, ieta, depth);
1326           }
1327           double hitEn = cfac * (*t_HitEnergies1)[idet];
1328           Etot1 += hitEn;
1329         }
1330       }
1331       for (unsigned int idet = 0; idet < (*t_DetIds3).size(); idet++) {
1332         // Apply thresholds if necessary
1333         bool okcell = (thrForm_ == 0) || ((*t_HitEnergies3)[idet] > (cThr_->threshold((*t_DetIds3)[idet])));
1334         if (okcell) {
1335           unsigned int id = truncateId((*t_DetIds3)[idet], truncateFlag_, false);
1336           double cfac = corrFactor_->getCorr(id);
1337           if ((cFactor_ != 0) && (ifDepth_ != 3) && (ifDepth_ > 0))
1338             cfac *= cFactor_->getCorr(t_Run, (*t_DetIds3)[idet]);
1339           if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(1)))
1340             cfac *= cDuplicate_->getWeight((*t_DetIds)[idet]);
1341           if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(3))) {
1342             int subdet, zside, ieta, iphi, depth;
1343             unpackDetId((*t_DetIds3)[idet], subdet, zside, ieta, iphi, depth);
1344             cfac *= cDuplicate_->getCorr(t_Run, ieta, depth);
1345           }
1346           double hitEn = cfac * (*t_HitEnergies3)[idet];
1347           Etot3 += hitEn;
1348         }
1349       }
1350       ediff = (Etot3 - Etot1);
1351     }
1352     double fac = puFactor(-corrPU_, t_ieta, pmom, eHcal, ediff);
1353     eHcal *= fac;
1354   } else if (corrPU_ > 1) {
1355     eHcal = puFactorRho(corrPU_, t_ieta, t_rhoh, eHcal);
1356   }
1357 }
1358 
1359 std::vector<double> CalibPlotProperties::rawEnergy(bool debug) {
1360   std::vector<double> eHcal(CalibPlots::ndepth, 0);
1361   for (unsigned int k = 0; k < t_HitEnergies->size(); ++k) {
1362     // Apply thresholds if necessary
1363     bool okcell = (thrForm_ == 0) || ((*t_HitEnergies)[k] > (cThr_->threshold((*t_DetIds)[k])));
1364     // The masks are defined in DataFormats/HcalDetId/interface/HcalDetId.h
1365     if (okcell) {
1366       int subdet, zside, ieta, iphi, depth;
1367       unpackDetId((*t_DetIds)[k], subdet, zside, ieta, iphi, depth);
1368       double cfac(1.0);
1369       if (corrFactor_->doCorr()) {
1370         unsigned int id = truncateId((*t_DetIds)[k], truncateFlag_, false);
1371         cfac = corrFactor_->getCorr(id);
1372       }
1373       if ((cFactor_ != nullptr) && (ifDepth_ != 3) && (ifDepth_ > 0))
1374         cfac *= cFactor_->getCorr(t_Run, (*t_DetIds)[k]);
1375       if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(1)))
1376         cfac *= cDuplicate_->getWeight((*t_DetIds)[k]);
1377       if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr(3))) {
1378         cfac *= cDuplicate_->getCorr(t_Run, ieta, depth);
1379       }
1380       if (depth <= CalibPlots::ndepth)
1381         eHcal[depth - 1] += (cfac * ((*t_HitEnergies)[k]));
1382     }
1383   }
1384   if (debug) {
1385     std::ostringstream st1;
1386     for (int k = 0; k < CalibPlots::ndepth; ++k)
1387       st1 << " : " << eHcal[k];
1388     std::cout << "Energy deposit in depths " << st1.str() << std::endl;
1389   }
1390   return eHcal;
1391 }
1392 
1393 void PlotThisHist(TH1D *hist, const std::string &text, bool isRealData, int save) {
1394   char namep[120];
1395   sprintf(namep, "c_%s", hist->GetName());
1396   TCanvas *pad = new TCanvas(namep, namep, 700, 500);
1397   pad->SetRightMargin(0.10);
1398   pad->SetTopMargin(0.10);
1399   hist->GetXaxis()->SetTitleSize(0.04);
1400   hist->GetYaxis()->SetTitle("Tracks");
1401   hist->GetYaxis()->SetLabelOffset(0.005);
1402   hist->GetYaxis()->SetTitleSize(0.04);
1403   hist->GetYaxis()->SetLabelSize(0.035);
1404   hist->GetYaxis()->SetTitleOffset(1.10);
1405   hist->SetMarkerStyle(20);
1406   hist->SetMarkerColor(2);
1407   hist->SetLineColor(2);
1408   hist->Draw("Hist");
1409   pad->Modified();
1410   pad->Update();
1411   TPaveStats *st1 = (TPaveStats *)hist->GetListOfFunctions()->FindObject("stats");
1412   TPaveText *txt0 = new TPaveText(0.12, 0.91, 0.49, 0.96, "blNDC");
1413   txt0->SetFillColor(0);
1414   char txt[100];
1415   if (isRealData)
1416     sprintf(txt, "CMS Preliminary");
1417   else
1418     sprintf(txt, "CMS Simulation Preliminary");
1419   txt0->AddText(txt);
1420   txt0->Draw("same");
1421   TPaveText *txt1 = new TPaveText(0.51, 0.91, 0.90, 0.96, "blNDC");
1422   txt1->SetFillColor(0);
1423   sprintf(txt, "%s", text.c_str());
1424   txt1->AddText(txt);
1425   txt1->Draw("same");
1426   if (st1 != nullptr) {
1427     st1->SetY1NDC(0.70);
1428     st1->SetY2NDC(0.90);
1429     st1->SetX1NDC(0.65);
1430     st1->SetX2NDC(0.90);
1431   }
1432   pad->Update();
1433   if (save != 0) {
1434     if (save > 0)
1435       sprintf(namep, "%s.pdf", pad->GetName());
1436     else
1437       sprintf(namep, "%s.jpg", pad->GetName());
1438     pad->Print(namep);
1439   }
1440 }
1441 
1442 void PlotTheseHists(const char *name,
1443                     TH1D *hist1,
1444                     const std::string &prefix1,
1445                     const std::string &text1,
1446                     TH1D *hist2,
1447                     const std::string &prefix2,
1448                     const std::string &text2,
1449                     bool isRealData,
1450                     bool normalize,
1451                     int save) {
1452   int colors[2] = {2, 4};
1453   std::vector<std::string> prefixes, texts;
1454   prefixes.push_back(prefix1);
1455   texts.push_back(text1);
1456   prefixes.push_back(prefix2);
1457   texts.push_back(text2);
1458   double ymax(0.90), dy(0.12);
1459   char namex[100], namep[120];
1460   sprintf(namep, "c_%s", name);
1461   double ymx0 = (ymax - dy * 2 - .01);
1462   TCanvas *pad = new TCanvas(namep, namep, 700, 500);
1463   TLegend *legend = new TLegend(0.64, ymx0 - 0.05 * 2, 0.89, ymx0);
1464   legend->SetFillColor(kWhite);
1465   pad->SetRightMargin(0.10);
1466   pad->SetTopMargin(0.10);
1467   pad->SetLogy();
1468   for (unsigned int k = 0; k < 2; ++k) {
1469     TH1D *hist = (k == 0) ? hist1 : hist2;
1470     hist->GetXaxis()->SetTitleSize(0.040);
1471     hist->GetYaxis()->SetLabelOffset(0.005);
1472     hist->GetYaxis()->SetLabelSize(0.035);
1473     hist->GetYaxis()->SetTitleSize(0.040);
1474     hist->GetYaxis()->SetTitleOffset(1.15);
1475     hist->SetMarkerStyle(20);
1476     hist->SetMarkerColor(colors[k]);
1477     hist->SetLineColor(colors[k]);
1478     if (normalize) {
1479       if (k == 0)
1480         hist->DrawNormalized();
1481       else
1482         hist->DrawNormalized("sames");
1483     } else {
1484       if (k == 0)
1485         hist->Draw();
1486       else
1487         hist->Draw("sames");
1488     }
1489     pad->Modified();
1490     pad->Update();
1491     pad->Update();
1492     TPaveStats *st1 = (TPaveStats *)hist->GetListOfFunctions()->FindObject("stats");
1493     if (st1 != nullptr) {
1494       double ymin = ymax - dy;
1495       st1->SetLineColor(colors[k]);
1496       st1->SetTextColor(colors[k]);
1497       st1->SetY1NDC(ymin);
1498       st1->SetY2NDC(ymax);
1499       st1->SetX1NDC(0.70);
1500       st1->SetX2NDC(0.90);
1501       ymax = ymin;
1502     }
1503     sprintf(namex, "%s", texts[k].c_str());
1504     legend->AddEntry(hist, namex, "lp");
1505   }
1506   legend->Draw("same");
1507   pad->Update();
1508   char txt[100];
1509   double xmi(0.10), xmx(0.895), ymx(0.95);
1510   double ymi = ymx - 0.045;
1511   TPaveText *txt1 = new TPaveText(xmi, ymi, xmx, ymx, "blNDC");
1512   txt1->SetFillColor(0);
1513   if (isRealData)
1514     sprintf(txt, "CMS Preliminary");
1515   else
1516     sprintf(txt, "CMS Simulation Preliminary");
1517   txt1->AddText(txt);
1518   txt1->Draw("same");
1519   pad->Modified();
1520   pad->Update();
1521   if (save > 0) {
1522     sprintf(namep, "%s.pdf", pad->GetName());
1523     pad->Print(namep);
1524   } else if (save < 0) {
1525     sprintf(namep, "%s.C", pad->GetName());
1526     pad->Print(namep);
1527   }
1528 }
1529 
1530 void PlotHist(const char *hisFileName,
1531               const std::string &prefix = "",
1532               const std::string &text = "",
1533               int flagC = 111,
1534               int etalo = 0,
1535               int etahi = 30,
1536               int save = 0) {
1537   gStyle->SetCanvasBorderMode(0);
1538   gStyle->SetCanvasColor(kWhite);
1539   gStyle->SetPadColor(kWhite);
1540   gStyle->SetFillColor(kWhite);
1541   gStyle->SetOptTitle(0);
1542   gStyle->SetOptStat(1110);
1543 
1544   bool isRealData = (((flagC / 1000) % 10) > 0);
1545   bool plotBasic = (((flagC / 1) % 10) > 0);
1546   bool plotEnergy = (((flagC / 10) % 10) > 0);
1547   bool plotHists = (((flagC / 100) % 10) > 0);
1548 
1549   TFile *file = new TFile(hisFileName);
1550   char name[100], title[200];
1551   TH1D *hist;
1552   if ((file != nullptr) && plotBasic) {
1553     std::cout << "Plot Basic Histos" << std::endl;
1554     hist = (TH1D *)(file->FindObjectAny("hnvtx"));
1555     if (hist != nullptr) {
1556       hist->GetXaxis()->SetTitle("Number of vertices (selected entries)");
1557       PlotThisHist(hist, text, isRealData, save);
1558     }
1559     hist = (TH1D *)(file->FindObjectAny("hnvtxEv"));
1560     if (hist != nullptr) {
1561       hist->GetXaxis()->SetTitle("Number of vertices (selected events)");
1562       PlotThisHist(hist, text, isRealData, save);
1563     }
1564     hist = (TH1D *)(file->FindObjectAny("hnvtxTk"));
1565     if (hist != nullptr) {
1566       hist->GetXaxis()->SetTitle("Number of vertices (selected tracks)");
1567       PlotThisHist(hist, text, isRealData, save);
1568     }
1569     for (int k = 0; k < CalibPlots::ntitles; ++k) {
1570       sprintf(name, "%sp%d", prefix.c_str(), k);
1571       hist = (TH1D *)(file->FindObjectAny(name));
1572       if (hist != nullptr) {
1573         sprintf(title, "Momentum for %s (GeV)", CalibPlots::getTitle(k).c_str());
1574         hist->GetXaxis()->SetTitle(title);
1575         PlotThisHist(hist, text, isRealData, save);
1576       }
1577       sprintf(name, "%seta%d", prefix.c_str(), k);
1578       hist = (TH1D *)(file->FindObjectAny(name));
1579       if (hist != nullptr) {
1580         sprintf(title, "#eta for %s", CalibPlots::getTitle(k).c_str());
1581         hist->GetXaxis()->SetTitle(title);
1582         PlotThisHist(hist, text, isRealData, save);
1583       }
1584     }
1585     for (int k = 0; k < CalibPlots::npbin; ++k) {
1586       sprintf(name, "%seta0%d", prefix.c_str(), k);
1587       hist = (TH1D *)(file->FindObjectAny(name));
1588       if (hist != nullptr) {
1589         sprintf(title,
1590                 "#eta for %s (p = %d:%d GeV)",
1591                 CalibPlots::getTitle(0).c_str(),
1592                 CalibPlots::getP(k),
1593                 CalibPlots::getP(k + 1));
1594         hist->GetXaxis()->SetTitle(title);
1595         PlotThisHist(hist, text, isRealData, save);
1596       }
1597       sprintf(name, "%seta1%d", prefix.c_str(), k);
1598       hist = (TH1D *)(file->FindObjectAny(name));
1599       if (hist != nullptr) {
1600         sprintf(title,
1601                 "#eta for %s (p = %d:%d GeV)",
1602                 CalibPlots::getTitle(1).c_str(),
1603                 CalibPlots::getP(k),
1604                 CalibPlots::getP(k + 1));
1605         hist->GetXaxis()->SetTitle(title);
1606         PlotThisHist(hist, text, isRealData, save);
1607       }
1608       sprintf(name, "%seta2%d", prefix.c_str(), k);
1609       hist = (TH1D *)(file->FindObjectAny(name));
1610       if (hist != nullptr) {
1611         sprintf(title,
1612                 "#eta for %s (p = %d:%d GeV)",
1613                 CalibPlots::getTitle(2).c_str(),
1614                 CalibPlots::getP(k),
1615                 CalibPlots::getP(k + 1));
1616         hist->GetXaxis()->SetTitle(title);
1617         PlotThisHist(hist, text, isRealData, save);
1618       }
1619       sprintf(name, "%seta3%d", prefix.c_str(), k);
1620       hist = (TH1D *)(file->FindObjectAny(name));
1621       if (hist != nullptr) {
1622         sprintf(title,
1623                 "#eta for %s (p = %d:%d GeV)",
1624                 CalibPlots::getTitle(3).c_str(),
1625                 CalibPlots::getP(k),
1626                 CalibPlots::getP(k + 1));
1627         hist->GetXaxis()->SetTitle(title);
1628         PlotThisHist(hist, text, isRealData, save);
1629       }
1630       sprintf(name, "%seta4%d", prefix.c_str(), k);
1631       hist = (TH1D *)(file->FindObjectAny(name));
1632       if (hist != nullptr) {
1633         sprintf(title,
1634                 "#eta for %s (p = %d:%d GeV)",
1635                 CalibPlots::getTitle(4).c_str(),
1636                 CalibPlots::getP(k),
1637                 CalibPlots::getP(k + 1));
1638         hist->GetXaxis()->SetTitle(title);
1639         PlotThisHist(hist, text, isRealData, save);
1640       }
1641       sprintf(name, "%sdl1%d", prefix.c_str(), k);
1642       hist = (TH1D *)(file->FindObjectAny(name));
1643       if (hist != nullptr) {
1644         sprintf(title, "Distance from L1 (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
1645         hist->GetXaxis()->SetTitle(title);
1646         PlotThisHist(hist, text, isRealData, save);
1647       }
1648       sprintf(name, "%svtx%d", prefix.c_str(), k);
1649       hist = (TH1D *)(file->FindObjectAny(name));
1650       if (hist != nullptr) {
1651         sprintf(title, "N_{Vertex} (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
1652         hist->GetXaxis()->SetTitle(title);
1653         PlotThisHist(hist, text, isRealData, save);
1654       }
1655     }
1656   }
1657 
1658   if ((file != nullptr) && plotEnergy) {
1659     std::cout << "Make plots for good tracks" << std::endl;
1660     for (int k = 0; k < CalibPlots::npbin; ++k) {
1661       for (int j = etalo; j <= etahi + 1; ++j) {
1662         sprintf(name, "%senergyH%d%d", prefix.c_str(), k, j);
1663         hist = (TH1D *)(file->FindObjectAny(name));
1664         if (hist != nullptr) {
1665           if (j > etahi)
1666             sprintf(title,
1667                     "HCAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
1668                     CalibPlots::getTitle(3).c_str(),
1669                     CalibPlots::getP(k),
1670                     CalibPlots::getP(k + 1),
1671                     etalo,
1672                     etahi);
1673           else
1674             sprintf(title,
1675                     "HCAL energy for %s (p = %d:%d GeV |#eta| = %d)",
1676                     CalibPlots::getTitle(3).c_str(),
1677                     CalibPlots::getP(k),
1678                     CalibPlots::getP(k + 1),
1679                     j);
1680           hist->GetXaxis()->SetTitle(title);
1681           PlotThisHist(hist, text, isRealData, save);
1682         }
1683         sprintf(name, "%senergyP%d%d", prefix.c_str(), k, j);
1684         hist = (TH1D *)(file->FindObjectAny(name));
1685         if (hist != nullptr) {
1686           if (j > etahi)
1687             sprintf(title,
1688                     "momentum for %s (p = %d:%d GeV |#eta| = %d:%d)",
1689                     CalibPlots::getTitle(3).c_str(),
1690                     CalibPlots::getP(k),
1691                     CalibPlots::getP(k + 1),
1692                     etalo,
1693                     etahi);
1694           else
1695             sprintf(title,
1696                     "momentum for %s (p = %d:%d GeV |#eta| = %d)",
1697                     CalibPlots::getTitle(3).c_str(),
1698                     CalibPlots::getP(k),
1699                     CalibPlots::getP(k + 1),
1700                     j);
1701           hist->GetXaxis()->SetTitle(title);
1702           PlotThisHist(hist, text, isRealData, save);
1703         }
1704         sprintf(name, "%senergyE%d%d", prefix.c_str(), k, j);
1705         hist = (TH1D *)(file->FindObjectAny(name));
1706         if (hist != nullptr) {
1707           if (j > etahi)
1708             sprintf(title,
1709                     "ECAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
1710                     CalibPlots::getTitle(3).c_str(),
1711                     CalibPlots::getP(k),
1712                     CalibPlots::getP(k + 1),
1713                     etalo,
1714                     etahi);
1715           else
1716             sprintf(title,
1717                     "ECAL energy for %s (p = %d:%d GeV |#eta| = %d)",
1718                     CalibPlots::getTitle(3).c_str(),
1719                     CalibPlots::getP(k),
1720                     CalibPlots::getP(k + 1),
1721                     j);
1722           hist->GetXaxis()->SetTitle(title);
1723           PlotThisHist(hist, text, isRealData, save);
1724         }
1725         sprintf(name, "%senergyER%d%d", prefix.c_str(), k, j);
1726         hist = (TH1D *)(file->FindObjectAny(name));
1727         if (hist != nullptr) {
1728           std::cout << name << " Mean " << hist->GetMean() << " +- " << hist->GetMeanError() << " Entries "
1729                     << hist->GetEntries() << " RMS " << hist->GetRMS() << std::endl;
1730         }
1731       }
1732     }
1733 
1734     for (int j = 0; j < CalibPlots::netabin; ++j) {
1735       sprintf(name, "%senergyH%d", prefix.c_str(), j);
1736       hist = (TH1D *)(file->FindObjectAny(name));
1737       if (hist != nullptr) {
1738         if (j == 0)
1739           sprintf(title, "HCAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
1740         else
1741           sprintf(title,
1742                   "HCAL energy for %s (|#eta| = %d:%d)",
1743                   CalibPlots::getTitle(3).c_str(),
1744                   CalibPlots::getEta(j - 1),
1745                   CalibPlots::getEta(j));
1746         hist->GetXaxis()->SetTitle(title);
1747         PlotThisHist(hist, text, isRealData, save);
1748       }
1749       sprintf(name, "%senergyP%d", prefix.c_str(), j);
1750       hist = (TH1D *)(file->FindObjectAny(name));
1751       if (hist != nullptr) {
1752         if (j == 0)
1753           sprintf(title, "Track momentum for %s (All)", CalibPlots::getTitle(3).c_str());
1754         else
1755           sprintf(title,
1756                   "Track momentum for %s (|#eta| = %d:%d)",
1757                   CalibPlots::getTitle(3).c_str(),
1758                   CalibPlots::getEta(j - 1),
1759                   CalibPlots::getEta(j));
1760         hist->GetXaxis()->SetTitle(title);
1761         PlotThisHist(hist, text, isRealData, save);
1762       }
1763       sprintf(name, "%senergyE%d", prefix.c_str(), j);
1764       hist = (TH1D *)(file->FindObjectAny(name));
1765       if (hist != nullptr) {
1766         if (j == 0)
1767           sprintf(title, "ECAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
1768         else
1769           sprintf(title,
1770                   "ECAL energy for %s (|#eta| = %d:%d)",
1771                   CalibPlots::getTitle(3).c_str(),
1772                   CalibPlots::getEta(j - 1),
1773                   CalibPlots::getEta(j));
1774         hist->GetXaxis()->SetTitle(title);
1775         PlotThisHist(hist, text, isRealData, save);
1776       }
1777     }
1778     for (int j = 1; j < CalibPlots::netabin; ++j) {
1779       for (int k = 0; k < CalibPlots::ndepth; ++k) {
1780         sprintf(name, "%sEnergyDepth%dEta%d", prefix.c_str(), k, j);
1781         hist = (TH1D *)(file->FindObjectAny(name));
1782         if (hist != nullptr) {
1783           sprintf(title,
1784                   "HCAL energy for depth %d (|#eta| = %d:%d)",
1785                   (k + 1),
1786                   CalibPlots::getEta(j - 1),
1787                   CalibPlots::getEta(j));
1788           hist->GetXaxis()->SetTitle(title);
1789           PlotThisHist(hist, text, isRealData, save);
1790         }
1791       }
1792     }
1793   }
1794 
1795   if ((file != nullptr) && plotHists) {
1796     for (int i = 0; i < CalibPlots::ndepth; i++) {
1797       sprintf(name, "b_edepth%d", i);
1798       hist = (TH1D *)(file->FindObjectAny(name));
1799       if (hist != nullptr) {
1800         sprintf(title, "Total RecHit energy in depth %d (Barrel)", i + 1);
1801         hist->GetXaxis()->SetTitle(title);
1802         PlotThisHist(hist, text, isRealData, save);
1803       }
1804       sprintf(name, "b_recedepth%d", i);
1805       hist = (TH1D *)(file->FindObjectAny(name));
1806       if (hist != nullptr) {
1807         sprintf(title, "RecHit energy in depth %d (Barrel)", i + 1);
1808         hist->GetXaxis()->SetTitle(title);
1809         PlotThisHist(hist, text, isRealData, save);
1810       }
1811       sprintf(name, "b_nrecdepth%d", i);
1812       hist = (TH1D *)(file->FindObjectAny(name));
1813       if (hist != nullptr) {
1814         sprintf(title, "#RecHits in depth %d (Barrel)", i + 1);
1815         hist->GetXaxis()->SetTitle(title);
1816         PlotThisHist(hist, text, isRealData, save);
1817       }
1818       sprintf(name, "e_edepth%d", i);
1819       hist = (TH1D *)(file->FindObjectAny(name));
1820       if (hist != nullptr) {
1821         sprintf(title, "Total RecHit energy in depth %d (Endcap)", i + 1);
1822         hist->GetXaxis()->SetTitle(title);
1823         PlotThisHist(hist, text, isRealData, save);
1824       }
1825       sprintf(name, "e_recedepth%d", i);
1826       hist = (TH1D *)(file->FindObjectAny(name));
1827       if (hist != nullptr) {
1828         sprintf(title, "RecHit energy in depth %d (Endcap)", i + 1);
1829         hist->GetXaxis()->SetTitle(title);
1830         PlotThisHist(hist, text, isRealData, save);
1831       }
1832       sprintf(name, "e_nrecdepth%d", i);
1833       hist = (TH1D *)(file->FindObjectAny(name));
1834       if (hist != nullptr) {
1835         sprintf(title, "#RecHits in depth %d (Endcap)", i + 1);
1836         hist->GetXaxis()->SetTitle(title);
1837         PlotThisHist(hist, text, isRealData, save);
1838       }
1839     }
1840     TH2F *h_etaE = (TH2F *)(file->FindObjectAny("heta"));
1841     if (h_etaE != nullptr) {
1842       h_etaE->GetXaxis()->SetTitle("i#eta");
1843       h_etaE->GetYaxis()->SetTitle("Energy (GeV)");
1844       char namep[120];
1845       sprintf(namep, "c_%s", h_etaE->GetName());
1846       TCanvas *pad = new TCanvas(namep, namep, 700, 700);
1847       pad->SetRightMargin(0.10);
1848       pad->SetTopMargin(0.10);
1849       h_etaE->GetXaxis()->SetTitleSize(0.04);
1850       h_etaE->GetYaxis()->SetLabelOffset(0.005);
1851       h_etaE->GetYaxis()->SetTitleSize(0.04);
1852       h_etaE->GetYaxis()->SetLabelSize(0.035);
1853       h_etaE->GetYaxis()->SetTitleOffset(1.10);
1854       h_etaE->SetMarkerStyle(20);
1855       h_etaE->SetMarkerColor(2);
1856       h_etaE->SetLineColor(2);
1857       h_etaE->Draw();
1858       pad->Update();
1859       TPaveStats *st1 = (TPaveStats *)h_etaE->GetListOfFunctions()->FindObject("stats");
1860       if (st1 != nullptr) {
1861         st1->SetY1NDC(0.70);
1862         st1->SetY2NDC(0.90);
1863         st1->SetX1NDC(0.65);
1864         st1->SetX2NDC(0.90);
1865       }
1866       if (save != 0) {
1867         if (save > 0)
1868           sprintf(namep, "%s.pdf", pad->GetName());
1869         else
1870           sprintf(namep, "%s.jpg", pad->GetName());
1871         pad->Print(namep);
1872       }
1873     }
1874   }
1875 }
1876 
1877 void PlotPHist(const char *hisFileName,
1878                const std::string &prefix = "",
1879                const std::string &text = "",
1880                int pLow = 1,
1881                int pHigh = 5,
1882                bool isRealData = true,
1883                int save = 0) {
1884   gStyle->SetCanvasBorderMode(0);
1885   gStyle->SetCanvasColor(kWhite);
1886   gStyle->SetPadColor(kWhite);
1887   gStyle->SetFillColor(kWhite);
1888   gStyle->SetOptTitle(0);
1889   gStyle->SetOptStat(1110);
1890 
1891   TFile *file = new TFile(hisFileName);
1892   char name[100];
1893   TH1D *hist;
1894   if (file != nullptr) {
1895     for (int ip = pLow; ip <= pHigh; ++ip) {
1896       sprintf(name, "%sp%d", prefix.c_str(), ip);
1897       hist = (TH1D *)(file->FindObjectAny(name));
1898       if (hist != nullptr) {
1899         hist->GetXaxis()->SetTitle(hist->GetTitle());
1900         hist->GetYaxis()->SetTitle("Tracks");
1901         PlotThisHist(hist, text, isRealData, save);
1902       }
1903     }
1904   }
1905 }
1906 
1907 void PlotTwoHists(const char *infile1,
1908                   const std::string &prefix1,
1909                   const std::string &text1,
1910                   const char *infile2,
1911                   const std::string &prefix2,
1912                   const std::string &text2,
1913                   int flagC = 111,
1914                   int drawStatBox = 0,
1915                   bool normalize = true,
1916                   int etalo = 0,
1917                   int etahi = 30,
1918                   int save = 0) {
1919   gStyle->SetCanvasBorderMode(0);
1920   gStyle->SetCanvasColor(kWhite);
1921   gStyle->SetPadColor(kWhite);
1922   gStyle->SetFillColor(kWhite);
1923   gStyle->SetOptTitle(0);
1924   if (drawStatBox > 0)
1925     gStyle->SetOptStat(1110);
1926   else
1927     gStyle->SetOptStat(0);
1928 
1929   bool isRealData = (((flagC / 1000) % 10) > 0);
1930   bool plotBasic = (((flagC / 1) % 10) > 0);
1931   bool plotEnergy = (((flagC / 10) % 10) > 0);
1932   bool plotHists = (((flagC / 100) % 10) > 0);
1933 
1934   char name[100], title[200];
1935   TFile *file1 = new TFile(infile1);
1936   TFile *file2 = new TFile(infile2);
1937   TH1D *hist1, *hist2;
1938   if ((file1 != nullptr) && (file2 != nullptr) && plotBasic) {
1939     std::cout << "Plot Basic Histos" << std::endl;
1940     sprintf(name, "hnvtx");
1941     hist1 = (TH1D *)(file1->FindObjectAny(name));
1942     hist2 = (TH1D *)(file2->FindObjectAny(name));
1943     if ((hist1 != nullptr) && (hist2 != nullptr)) {
1944       hist1->GetXaxis()->SetTitle("Number of vertices (selected entries)");
1945       hist2->GetXaxis()->SetTitle("Number of vertices (selected entries)");
1946       PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
1947     }
1948     sprintf(name, "hnvtxEv");
1949     hist1 = (TH1D *)(file1->FindObjectAny(name));
1950     hist2 = (TH1D *)(file2->FindObjectAny(name));
1951     if ((hist1 != nullptr) && (hist2 != nullptr)) {
1952       hist1->GetXaxis()->SetTitle("Number of vertices (selected events)");
1953       hist2->GetXaxis()->SetTitle("Number of vertices (selected events)");
1954       PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
1955     }
1956     sprintf(name, "hnvtxTk");
1957     hist1 = (TH1D *)(file1->FindObjectAny(name));
1958     hist2 = (TH1D *)(file2->FindObjectAny(name));
1959     if ((hist1 != nullptr) && (hist2 != nullptr)) {
1960       hist1->GetXaxis()->SetTitle("Number of vertices (selected tracks)");
1961       hist2->GetXaxis()->SetTitle("Number of vertices (selected tracks)");
1962       PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
1963     }
1964     for (int k = 0; k < CalibPlots::ntitles; ++k) {
1965       sprintf(name, "%sp%d", prefix1.c_str(), k);
1966       hist1 = (TH1D *)(file1->FindObjectAny(name));
1967       sprintf(name, "%sp%d", prefix2.c_str(), k);
1968       hist2 = (TH1D *)(file2->FindObjectAny(name));
1969       sprintf(name, "p%d", k);
1970       if ((hist1 != nullptr) && (hist2 != nullptr)) {
1971         sprintf(title, "Momentum for %s (GeV)", CalibPlots::getTitle(k).c_str());
1972         hist1->GetXaxis()->SetTitle(title);
1973         hist2->GetXaxis()->SetTitle(title);
1974         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
1975       }
1976       sprintf(name, "%seta%d", prefix1.c_str(), k);
1977       hist1 = (TH1D *)(file1->FindObjectAny(name));
1978       sprintf(name, "%seta%d", prefix2.c_str(), k);
1979       hist2 = (TH1D *)(file2->FindObjectAny(name));
1980       sprintf(name, "eta%d", k);
1981       if ((hist1 != nullptr) && (hist2 != nullptr)) {
1982         sprintf(title, "#eta for %s", CalibPlots::getTitle(k).c_str());
1983         hist1->GetXaxis()->SetTitle(title);
1984         hist2->GetXaxis()->SetTitle(title);
1985         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
1986       }
1987     }
1988     for (int k = 0; k < CalibPlots::npbin; ++k) {
1989       sprintf(name, "%seta0%d", prefix1.c_str(), k);
1990       hist1 = (TH1D *)(file1->FindObjectAny(name));
1991       sprintf(name, "%seta0%d", prefix2.c_str(), k);
1992       hist2 = (TH1D *)(file2->FindObjectAny(name));
1993       sprintf(name, "eta0%d", k);
1994       if ((hist1 != nullptr) && (hist2 != nullptr)) {
1995         sprintf(title,
1996                 "#eta for %s (p = %d:%d GeV)",
1997                 CalibPlots::getTitle(0).c_str(),
1998                 CalibPlots::getP(k),
1999                 CalibPlots::getP(k + 1));
2000         hist1->GetXaxis()->SetTitle(title);
2001         hist2->GetXaxis()->SetTitle(title);
2002         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2003       }
2004       sprintf(name, "%seta1%d", prefix1.c_str(), k);
2005       hist1 = (TH1D *)(file1->FindObjectAny(name));
2006       sprintf(name, "%seta1%d", prefix2.c_str(), k);
2007       hist2 = (TH1D *)(file2->FindObjectAny(name));
2008       sprintf(name, "eta1%d", k);
2009       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2010         sprintf(title,
2011                 "#eta for %s (p = %d:%d GeV)",
2012                 CalibPlots::getTitle(1).c_str(),
2013                 CalibPlots::getP(k),
2014                 CalibPlots::getP(k + 1));
2015         hist1->GetXaxis()->SetTitle(title);
2016         hist2->GetXaxis()->SetTitle(title);
2017         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2018       }
2019       sprintf(name, "%seta2%d", prefix1.c_str(), k);
2020       hist1 = (TH1D *)(file1->FindObjectAny(name));
2021       sprintf(name, "%seta2%d", prefix2.c_str(), k);
2022       hist2 = (TH1D *)(file2->FindObjectAny(name));
2023       sprintf(name, "eta2%d", k);
2024       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2025         sprintf(title,
2026                 "#eta for %s (p = %d:%d GeV)",
2027                 CalibPlots::getTitle(2).c_str(),
2028                 CalibPlots::getP(k),
2029                 CalibPlots::getP(k + 1));
2030         hist1->GetXaxis()->SetTitle(title);
2031         hist2->GetXaxis()->SetTitle(title);
2032         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2033       }
2034       sprintf(name, "%seta3%d", prefix1.c_str(), k);
2035       hist1 = (TH1D *)(file1->FindObjectAny(name));
2036       sprintf(name, "%seta3%d", prefix2.c_str(), k);
2037       hist2 = (TH1D *)(file2->FindObjectAny(name));
2038       sprintf(name, "eta3%d", k);
2039       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2040         sprintf(title,
2041                 "#eta for %s (p = %d:%d GeV)",
2042                 CalibPlots::getTitle(3).c_str(),
2043                 CalibPlots::getP(k),
2044                 CalibPlots::getP(k + 1));
2045         hist1->GetXaxis()->SetTitle(title);
2046         hist2->GetXaxis()->SetTitle(title);
2047         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2048       }
2049       sprintf(name, "%seta4%d", prefix1.c_str(), k);
2050       hist1 = (TH1D *)(file1->FindObjectAny(name));
2051       sprintf(name, "%seta4%d", prefix2.c_str(), k);
2052       hist2 = (TH1D *)(file2->FindObjectAny(name));
2053       sprintf(name, "eta4%d", k);
2054       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2055         sprintf(title,
2056                 "#eta for %s (p = %d:%d GeV)",
2057                 CalibPlots::getTitle(4).c_str(),
2058                 CalibPlots::getP(k),
2059                 CalibPlots::getP(k + 1));
2060         hist1->GetXaxis()->SetTitle(title);
2061         hist2->GetXaxis()->SetTitle(title);
2062         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2063       }
2064       sprintf(name, "%sdl1%d", prefix1.c_str(), k);
2065       hist1 = (TH1D *)(file1->FindObjectAny(name));
2066       sprintf(name, "%sdl1%d", prefix2.c_str(), k);
2067       hist2 = (TH1D *)(file2->FindObjectAny(name));
2068       sprintf(name, "dl1%d", k);
2069       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2070         sprintf(title, "Distance from L1 (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
2071         hist1->GetXaxis()->SetTitle(title);
2072         hist2->GetXaxis()->SetTitle(title);
2073         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2074       }
2075       sprintf(name, "%svtx%d", prefix1.c_str(), k);
2076       hist1 = (TH1D *)(file1->FindObjectAny(name));
2077       sprintf(name, "%svtx%d", prefix2.c_str(), k);
2078       hist2 = (TH1D *)(file2->FindObjectAny(name));
2079       sprintf(name, "vtx%d", k);
2080       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2081         sprintf(title, "N_{Vertex} (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
2082         hist1->GetXaxis()->SetTitle(title);
2083         hist2->GetXaxis()->SetTitle(title);
2084         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2085       }
2086     }
2087   }
2088 
2089   if ((file1 != nullptr) && (file2 != nullptr) && plotEnergy) {
2090     std::cout << "Make plots for good tracks" << std::endl;
2091     if (((flagC / 10) % 10) == 2) {
2092       for (int k = 0; k < CalibPlots::npbin; ++k) {
2093         for (int j = etalo; j <= etahi + 1; ++j) {
2094           sprintf(name, "%senergyH%d%d", prefix1.c_str(), k, j);
2095           hist1 = (TH1D *)(file1->FindObjectAny(name));
2096           sprintf(name, "%senergyH%d%d", prefix2.c_str(), k, j);
2097           hist2 = (TH1D *)(file2->FindObjectAny(name));
2098           sprintf(name, "energyH%d%d", k, j);
2099           if ((hist1 != nullptr) && (hist2 != nullptr)) {
2100             if (j > etahi)
2101               sprintf(title,
2102                       "HCAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
2103                       CalibPlots::getTitle(3).c_str(),
2104                       CalibPlots::getP(k),
2105                       CalibPlots::getP(k + 1),
2106                       etalo,
2107                       etahi);
2108             else
2109               sprintf(title,
2110                       "HCAL energy for %s (p = %d:%d GeV |#eta| = %d)",
2111                       CalibPlots::getTitle(3).c_str(),
2112                       CalibPlots::getP(k),
2113                       CalibPlots::getP(k + 1),
2114                       j);
2115             hist1->GetXaxis()->SetTitle(title);
2116             hist2->GetXaxis()->SetTitle(title);
2117             PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2118           }
2119           sprintf(name, "%senergyP%d%d", prefix1.c_str(), k, j);
2120           hist1 = (TH1D *)(file1->FindObjectAny(name));
2121           sprintf(name, "%senergyP%d%d", prefix2.c_str(), k, j);
2122           hist2 = (TH1D *)(file2->FindObjectAny(name));
2123           sprintf(name, "energyP%d%d", k, j);
2124           if ((hist1 != nullptr) && (hist2 != nullptr)) {
2125             if (j > etahi)
2126               sprintf(title,
2127                       "momentum for %s (p = %d:%d GeV |#eta| = %d:%d)",
2128                       CalibPlots::getTitle(3).c_str(),
2129                       CalibPlots::getP(k),
2130                       CalibPlots::getP(k + 1),
2131                       etalo,
2132                       etahi);
2133             else
2134               sprintf(title,
2135                       "momentum for %s (p = %d:%d GeV |#eta| = %d)",
2136                       CalibPlots::getTitle(3).c_str(),
2137                       CalibPlots::getP(k),
2138                       CalibPlots::getP(k + 1),
2139                       j);
2140             hist1->GetXaxis()->SetTitle(title);
2141             hist2->GetXaxis()->SetTitle(title);
2142             PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2143           }
2144           sprintf(name, "%senergyE%d%d", prefix1.c_str(), k, j);
2145           hist1 = (TH1D *)(file1->FindObjectAny(name));
2146           sprintf(name, "%senergyE%d%d", prefix2.c_str(), k, j);
2147           hist2 = (TH1D *)(file2->FindObjectAny(name));
2148           sprintf(name, "energyE%d%d", k, j);
2149           if ((hist1 != nullptr) && (hist2 != nullptr)) {
2150             if (j > etahi)
2151               sprintf(title,
2152                       "ECAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
2153                       CalibPlots::getTitle(3).c_str(),
2154                       CalibPlots::getP(k),
2155                       CalibPlots::getP(k + 1),
2156                       etalo,
2157                       etahi);
2158             else
2159               sprintf(title,
2160                       "ECAL energy for %s (p = %d:%d GeV |#eta| = %d)",
2161                       CalibPlots::getTitle(3).c_str(),
2162                       CalibPlots::getP(k),
2163                       CalibPlots::getP(k + 1),
2164                       j);
2165             hist1->GetXaxis()->SetTitle(title);
2166             hist2->GetXaxis()->SetTitle(title);
2167             PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2168           }
2169           sprintf(name, "%senergyER%d%d", prefix1.c_str(), k, j);
2170           hist1 = (TH1D *)(file1->FindObjectAny(name));
2171           sprintf(name, "%senergyER%d%d", prefix2.c_str(), k, j);
2172           hist2 = (TH1D *)(file2->FindObjectAny(name));
2173           sprintf(name, "energyER%d%d", k, j);
2174           if ((hist1 != nullptr) && (hist2 != nullptr)) {
2175             std::cout << name << " Mean " << hist1->GetMean() << " +- " << hist1->GetMeanError() << " : "
2176                       << hist2->GetMean() << " +- " << hist2->GetMeanError() << " Entries " << hist1->GetEntries()
2177                       << " : " << hist2->GetEntries() << " RMS " << hist1->GetRMS() << " : " << hist2->GetRMS()
2178                       << std::endl;
2179           }
2180         }
2181       }
2182     }
2183 
2184     if (((flagC / 10) % 10) == 3) {
2185       for (int j = 0; j < CalibPlots::netabin; ++j) {
2186         sprintf(name, "%senergyH%d", prefix1.c_str(), j);
2187         hist1 = (TH1D *)(file1->FindObjectAny(name));
2188         sprintf(name, "%senergyH%d", prefix2.c_str(), j);
2189         hist2 = (TH1D *)(file2->FindObjectAny(name));
2190         sprintf(name, "energyH%d", j);
2191         if ((hist1 != nullptr) && (hist2 != nullptr)) {
2192           if (j == 0)
2193             sprintf(title, "HCAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
2194           else
2195             sprintf(title,
2196                     "HCAL energy for %s (|#eta| = %d:%d)",
2197                     CalibPlots::getTitle(3).c_str(),
2198                     CalibPlots::getEta(j - 1),
2199                     CalibPlots::getEta(j));
2200           hist1->GetXaxis()->SetTitle(title);
2201           hist2->GetXaxis()->SetTitle(title);
2202           PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2203         }
2204         sprintf(name, "%senergyP%d", prefix1.c_str(), j);
2205         hist1 = (TH1D *)(file1->FindObjectAny(name));
2206         sprintf(name, "%senergyP%d", prefix2.c_str(), j);
2207         hist2 = (TH1D *)(file2->FindObjectAny(name));
2208         sprintf(name, "energyP%d", j);
2209         if ((hist1 != nullptr) && (hist2 != nullptr)) {
2210           if (j == 0)
2211             sprintf(title, "Track momentum for %s (All)", CalibPlots::getTitle(3).c_str());
2212           else
2213             sprintf(title,
2214                     "Track momentum for %s (|#eta| = %d:%d)",
2215                     CalibPlots::getTitle(3).c_str(),
2216                     CalibPlots::getEta(j - 1),
2217                     CalibPlots::getEta(j));
2218           hist1->GetXaxis()->SetTitle(title);
2219           hist2->GetXaxis()->SetTitle(title);
2220           PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2221         }
2222         sprintf(name, "%senergyE%d", prefix1.c_str(), j);
2223         hist1 = (TH1D *)(file1->FindObjectAny(name));
2224         sprintf(name, "%senergyE%d", prefix2.c_str(), j);
2225         hist2 = (TH1D *)(file2->FindObjectAny(name));
2226         sprintf(name, "energyE%d", j);
2227         if ((hist1 != nullptr) && (hist2 != nullptr)) {
2228           if (j == 0)
2229             sprintf(title, "ECAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
2230           else
2231             sprintf(title,
2232                     "ECAL energy for %s (|#eta| = %d:%d)",
2233                     CalibPlots::getTitle(3).c_str(),
2234                     CalibPlots::getEta(j - 1),
2235                     CalibPlots::getEta(j));
2236           hist1->GetXaxis()->SetTitle(title);
2237           hist2->GetXaxis()->SetTitle(title);
2238           PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2239         }
2240       }
2241     }
2242 
2243     if (((flagC / 10) % 10) == 1) {
2244       for (int j = 1; j < CalibPlots::netabin; ++j) {
2245         for (int k = 0; k < CalibPlots::ndepth; ++k) {
2246           sprintf(name, "%sEnergyDepth%dEta%d", prefix1.c_str(), k, j);
2247           hist1 = (TH1D *)(file1->FindObjectAny(name));
2248           sprintf(name, "%sEnergyDepth%dEta%d", prefix2.c_str(), k, j);
2249           hist2 = (TH1D *)(file2->FindObjectAny(name));
2250           sprintf(name, "EnergyDepth%dEta%d", k, j);
2251           if ((hist1 != nullptr) && (hist2 != nullptr)) {
2252             sprintf(title,
2253                     "HCAL energy for depth %d (|#eta| = %d:%d)",
2254                     (k + 1),
2255                     CalibPlots::getEta(j - 1),
2256                     CalibPlots::getEta(j));
2257             hist1->GetXaxis()->SetTitle(title);
2258             hist2->GetXaxis()->SetTitle(title);
2259             PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2260           }
2261         }
2262       }
2263     }
2264   }
2265 
2266   if ((file1 != nullptr) && (file2 != nullptr) && plotHists) {
2267     for (int i = 0; i < CalibPlots::ndepth; i++) {
2268       sprintf(name, "b_edepth%d", i);
2269       hist1 = (TH1D *)(file1->FindObjectAny(name));
2270       hist2 = (TH1D *)(file2->FindObjectAny(name));
2271       sprintf(name, "bEdepth%d", i);
2272       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2273         sprintf(title, "Total RecHit energy in depth %d (Barrel)", i + 1);
2274         hist1->GetXaxis()->SetTitle(title);
2275         hist2->GetXaxis()->SetTitle(title);
2276         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2277       }
2278       sprintf(name, "b_recedepth%d", i);
2279       hist1 = (TH1D *)(file1->FindObjectAny(name));
2280       hist2 = (TH1D *)(file2->FindObjectAny(name));
2281       sprintf(name, "bRecedepth%d", i);
2282       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2283         sprintf(title, "RecHit energy in depth %d (Barrel)", i + 1);
2284         hist1->GetXaxis()->SetTitle(title);
2285         hist2->GetXaxis()->SetTitle(title);
2286         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2287       }
2288       sprintf(name, "b_nrecdepth%d", i);
2289       hist1 = (TH1D *)(file1->FindObjectAny(name));
2290       hist2 = (TH1D *)(file2->FindObjectAny(name));
2291       sprintf(name, "bNrecdepth%d", i);
2292       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2293         sprintf(title, "#RecHits in depth %d (Barrel)", i + 1);
2294         hist1->GetXaxis()->SetTitle(title);
2295         hist2->GetXaxis()->SetTitle(title);
2296         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2297       }
2298       sprintf(name, "e_edepth%d", i);
2299       hist1 = (TH1D *)(file1->FindObjectAny(name));
2300       hist2 = (TH1D *)(file2->FindObjectAny(name));
2301       sprintf(name, "eEdepth%d", i);
2302       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2303         sprintf(title, "Total RecHit energy in depth %d (Endcap)", i + 1);
2304         hist1->GetXaxis()->SetTitle(title);
2305         hist2->GetXaxis()->SetTitle(title);
2306         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2307       }
2308       sprintf(name, "e_recedepth%d", i);
2309       hist1 = (TH1D *)(file1->FindObjectAny(name));
2310       hist2 = (TH1D *)(file2->FindObjectAny(name));
2311       sprintf(name, "eRecedepth%d", i);
2312       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2313         sprintf(title, "RecHit energy in depth %d (Endcap)", i + 1);
2314         hist1->GetXaxis()->SetTitle(title);
2315         hist2->GetXaxis()->SetTitle(title);
2316         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2317       }
2318       sprintf(name, "e_nrecdepth%d", i);
2319       hist1 = (TH1D *)(file1->FindObjectAny(name));
2320       hist2 = (TH1D *)(file2->FindObjectAny(name));
2321       sprintf(name, "eNrecdepth%d", i);
2322       if ((hist1 != nullptr) && (hist2 != nullptr)) {
2323         sprintf(title, "#RecHits in depth %d (Endcap)", i + 1);
2324         hist1->GetXaxis()->SetTitle(title);
2325         hist2->GetXaxis()->SetTitle(title);
2326         PlotTheseHists(name, hist1, prefix1, text1, hist2, prefix2, text2, isRealData, normalize, save);
2327       }
2328     }
2329   }
2330 }
2331 
2332 class CalibSplit {
2333 public:
2334   TChain *fChain;  //!pointer to the analyzed TTree or TChain
2335   Int_t fCurrent;  //!current Tree number in a TChain
2336 
2337   // Declaration of leaf types
2338   Int_t t_Run;
2339   Int_t t_Event;
2340   Int_t t_DataType;
2341   Int_t t_ieta;
2342   Int_t t_iphi;
2343   Double_t t_EventWeight;
2344   Int_t t_nVtx;
2345   Int_t t_nTrk;
2346   Int_t t_goodPV;
2347   Double_t t_l1pt;
2348   Double_t t_l1eta;
2349   Double_t t_l1phi;
2350   Double_t t_l3pt;
2351   Double_t t_l3eta;
2352   Double_t t_l3phi;
2353   Double_t t_p;
2354   Double_t t_pt;
2355   Double_t t_phi;
2356   Double_t t_mindR1;
2357   Double_t t_mindR2;
2358   Double_t t_eMipDR;
2359   Double_t t_eHcal;
2360   Double_t t_eHcal10;
2361   Double_t t_eHcal30;
2362   Double_t t_hmaxNearP;
2363   Double_t t_rhoh;
2364   Bool_t t_selectTk;
2365   Bool_t t_qltyFlag;
2366   Bool_t t_qltyMissFlag;
2367   Bool_t t_qltyPVFlag;
2368   Double_t t_gentrackP;
2369   std::vector<unsigned int> *t_DetIds;
2370   std::vector<double> *t_HitEnergies;
2371   std::vector<bool> *t_trgbits;
2372   std::vector<unsigned int> *t_DetIds1;
2373   std::vector<unsigned int> *t_DetIds3;
2374   std::vector<double> *t_HitEnergies1;
2375   std::vector<double> *t_HitEnergies3;
2376 
2377   // List of branches
2378   TBranch *b_t_Run;           //!
2379   TBranch *b_t_Event;         //!
2380   TBranch *b_t_DataType;      //!
2381   TBranch *b_t_ieta;          //!
2382   TBranch *b_t_iphi;          //!
2383   TBranch *b_t_EventWeight;   //!
2384   TBranch *b_t_nVtx;          //!
2385   TBranch *b_t_nTrk;          //!
2386   TBranch *b_t_goodPV;        //!
2387   TBranch *b_t_l1pt;          //!
2388   TBranch *b_t_l1eta;         //!
2389   TBranch *b_t_l1phi;         //!
2390   TBranch *b_t_l3pt;          //!
2391   TBranch *b_t_l3eta;         //!
2392   TBranch *b_t_l3phi;         //!
2393   TBranch *b_t_p;             //!
2394   TBranch *b_t_pt;            //!
2395   TBranch *b_t_phi;           //!
2396   TBranch *b_t_mindR1;        //!
2397   TBranch *b_t_mindR2;        //!
2398   TBranch *b_t_eMipDR;        //!
2399   TBranch *b_t_eHcal;         //!
2400   TBranch *b_t_eHcal10;       //!
2401   TBranch *b_t_eHcal30;       //!
2402   TBranch *b_t_hmaxNearP;     //!
2403   TBranch *b_t_rhoh;          //!
2404   TBranch *b_t_selectTk;      //!
2405   TBranch *b_t_qltyFlag;      //!
2406   TBranch *b_t_qltyMissFlag;  //!
2407   TBranch *b_t_qltyPVFlag;    //!
2408   TBranch *b_t_gentrackP;     //!
2409   TBranch *b_t_DetIds;        //!
2410   TBranch *b_t_HitEnergies;   //!
2411   TBranch *b_t_trgbits;       //!
2412   TBranch *b_t_DetIds1;       //!
2413   TBranch *b_t_DetIds3;       //!
2414   TBranch *b_t_HitEnergies1;  //!
2415   TBranch *b_t_HitEnergies3;  //!
2416 
2417   // Declaration of output leaf types
2418   Int_t tout_Run;
2419   Int_t tout_Event;
2420   Int_t tout_DataType;
2421   Int_t tout_ieta;
2422   Int_t tout_iphi;
2423   Double_t tout_EventWeight;
2424   Int_t tout_nVtx;
2425   Int_t tout_nTrk;
2426   Int_t tout_goodPV;
2427   Double_t tout_l1pt;
2428   Double_t tout_l1eta;
2429   Double_t tout_l1phi;
2430   Double_t tout_l3pt;
2431   Double_t tout_l3eta;
2432   Double_t tout_l3phi;
2433   Double_t tout_p;
2434   Double_t tout_pt;
2435   Double_t tout_phi;
2436   Double_t tout_mindR1;
2437   Double_t tout_mindR2;
2438   Double_t tout_eMipDR;
2439   Double_t tout_eHcal;
2440   Double_t tout_eHcal10;
2441   Double_t tout_eHcal30;
2442   Double_t tout_hmaxNearP;
2443   Double_t tout_rhoh;
2444   Bool_t tout_selectTk;
2445   Bool_t tout_qltyFlag;
2446   Bool_t tout_qltyMissFlag;
2447   Bool_t tout_qltyPVFlag;
2448   Double_t tout_gentrackP;
2449   std::vector<unsigned int> *tout_DetIds;
2450   std::vector<double> *tout_HitEnergies;
2451   std::vector<bool> *tout_trgbits;
2452   std::vector<unsigned int> *tout_DetIds1;
2453   std::vector<unsigned int> *tout_DetIds3;
2454   std::vector<double> *tout_HitEnergies1;
2455   std::vector<double> *tout_HitEnergies3;
2456 
2457   CalibSplit(const char *fname,
2458              const std::string &dirname,
2459              const std::string &outFileName,
2460              double pmin = 40.0,
2461              double pmax = 60.0,
2462              int runMin = -1,
2463              int runMax = -1,
2464              bool debug = false);
2465   virtual ~CalibSplit();
2466   virtual Int_t Cut(Long64_t entry);
2467   virtual Int_t GetEntry(Long64_t entry);
2468   virtual Long64_t LoadTree(Long64_t entry);
2469   virtual void Init(TChain *);
2470   virtual void Loop(Long64_t nentries = -1);
2471   virtual Bool_t Notify();
2472   virtual void Show(Long64_t entry = -1);
2473   void copyTree();
2474   void close();
2475 
2476 private:
2477   const std::string fname_, dirnm_, outFileName_;
2478   const double pmin_, pmax_;
2479   const int runMin_, runMax_;
2480   const bool debug_;
2481   bool checkRun_;
2482   TFile *outputFile_;
2483   TDirectoryFile *outputDir_;
2484   TTree *outputTree_;
2485 };
2486 
2487 CalibSplit::CalibSplit(const char *fname,
2488                        const std::string &dirnm,
2489                        const std::string &outFileName,
2490                        double pmin,
2491                        double pmax,
2492                        int runMin,
2493                        int runMax,
2494                        bool debug)
2495     : fname_(fname),
2496       dirnm_(dirnm),
2497       outFileName_(outFileName),
2498       pmin_(pmin),
2499       pmax_(pmax),
2500       runMin_(runMin),
2501       runMax_(runMax),
2502       debug_(debug),
2503       outputFile_(nullptr),
2504       outputDir_(nullptr),
2505       outputTree_(nullptr) {
2506   checkRun_ = ((runMin_ < 0) || (runMax_ < 0)) ? false : true;
2507   char treeName[400];
2508   sprintf(treeName, "%s/CalibTree", dirnm.c_str());
2509   TChain *chain = new TChain(treeName);
2510   std::cout << "Create a chain for " << treeName << " from " << fname << " to write tracs of momentum in the range "
2511             << pmin_ << ":" << pmax_ << " to file " << outFileName_ << std::endl;
2512   if (!fillChain(chain, fname)) {
2513     std::cout << "*****No valid tree chain can be obtained*****" << std::endl;
2514   } else {
2515     std::cout << "Proceed with a tree chain with " << chain->GetEntries() << " entries" << std::endl;
2516     Init(chain);
2517   }
2518 }
2519 
2520 CalibSplit::~CalibSplit() {
2521   if (!fChain)
2522     return;
2523   delete fChain->GetCurrentFile();
2524 }
2525 
2526 Int_t CalibSplit::GetEntry(Long64_t entry) {
2527   // Read contents of entry.
2528   if (!fChain)
2529     return 0;
2530   return fChain->GetEntry(entry);
2531 }
2532 
2533 Long64_t CalibSplit::LoadTree(Long64_t entry) {
2534   // Set the environment to read one entry
2535   if (!fChain)
2536     return -5;
2537   Long64_t centry = fChain->LoadTree(entry);
2538   if (centry < 0)
2539     return centry;
2540   if (!fChain->InheritsFrom(TChain::Class()))
2541     return centry;
2542   TChain *chain = (TChain *)fChain;
2543   if (chain->GetTreeNumber() != fCurrent) {
2544     fCurrent = chain->GetTreeNumber();
2545     Notify();
2546   }
2547   return centry;
2548 }
2549 
2550 void CalibSplit::Init(TChain *tree) {
2551   // The Init() function is called when the selector needs to initialize
2552   // a new tree or chain. Typically here the branch addresses and branch
2553   // pointers of the tree will be set.
2554   // It is normally not necessary to make changes to the generated
2555   // code, but the routine can be extended by the user if needed.
2556   // Init() will be called many times when running on PROOF
2557   // (once per file to be processed).
2558 
2559   // Set object pointer
2560   t_DetIds = nullptr;
2561   t_DetIds1 = nullptr;
2562   t_DetIds3 = nullptr;
2563   t_HitEnergies = nullptr;
2564   t_HitEnergies1 = nullptr;
2565   t_HitEnergies3 = nullptr;
2566   t_trgbits = nullptr;
2567   // Set branch addresses and branch pointers
2568   fChain = tree;
2569   fCurrent = -1;
2570   if (!tree)
2571     return;
2572   fChain->SetMakeClass(1);
2573 
2574   fChain->SetBranchAddress("t_Run", &t_Run, &b_t_Run);
2575   fChain->SetBranchAddress("t_Event", &t_Event, &b_t_Event);
2576   fChain->SetBranchAddress("t_DataType", &t_DataType, &b_t_DataType);
2577   fChain->SetBranchAddress("t_ieta", &t_ieta, &b_t_ieta);
2578   fChain->SetBranchAddress("t_iphi", &t_iphi, &b_t_iphi);
2579   fChain->SetBranchAddress("t_EventWeight", &t_EventWeight, &b_t_EventWeight);
2580   fChain->SetBranchAddress("t_nVtx", &t_nVtx, &b_t_nVtx);
2581   fChain->SetBranchAddress("t_nTrk", &t_nTrk, &b_t_nTrk);
2582   fChain->SetBranchAddress("t_goodPV", &t_goodPV, &b_t_goodPV);
2583   fChain->SetBranchAddress("t_l1pt", &t_l1pt, &b_t_l1pt);
2584   fChain->SetBranchAddress("t_l1eta", &t_l1eta, &b_t_l1eta);
2585   fChain->SetBranchAddress("t_l1phi", &t_l1phi, &b_t_l1phi);
2586   fChain->SetBranchAddress("t_l3pt", &t_l3pt, &b_t_l3pt);
2587   fChain->SetBranchAddress("t_l3eta", &t_l3eta, &b_t_l3eta);
2588   fChain->SetBranchAddress("t_l3phi", &t_l3phi, &b_t_l3phi);
2589   fChain->SetBranchAddress("t_p", &t_p, &b_t_p);
2590   fChain->SetBranchAddress("t_pt", &t_pt, &b_t_pt);
2591   fChain->SetBranchAddress("t_phi", &t_phi, &b_t_phi);
2592   fChain->SetBranchAddress("t_mindR1", &t_mindR1, &b_t_mindR1);
2593   fChain->SetBranchAddress("t_mindR2", &t_mindR2, &b_t_mindR2);
2594   fChain->SetBranchAddress("t_eMipDR", &t_eMipDR, &b_t_eMipDR);
2595   fChain->SetBranchAddress("t_eHcal", &t_eHcal, &b_t_eHcal);
2596   fChain->SetBranchAddress("t_eHcal10", &t_eHcal10, &b_t_eHcal10);
2597   fChain->SetBranchAddress("t_eHcal30", &t_eHcal30, &b_t_eHcal30);
2598   fChain->SetBranchAddress("t_hmaxNearP", &t_hmaxNearP, &b_t_hmaxNearP);
2599   fChain->SetBranchAddress("t_rhoh", &t_rhoh, &b_t_rhoh);
2600   fChain->SetBranchAddress("t_selectTk", &t_selectTk, &b_t_selectTk);
2601   fChain->SetBranchAddress("t_qltyFlag", &t_qltyFlag, &b_t_qltyFlag);
2602   fChain->SetBranchAddress("t_qltyMissFlag", &t_qltyMissFlag, &b_t_qltyMissFlag);
2603   fChain->SetBranchAddress("t_qltyPVFlag", &t_qltyPVFlag, &b_t_qltyPVFlag);
2604   fChain->SetBranchAddress("t_gentrackP", &t_gentrackP, &b_t_gentrackP);
2605   fChain->SetBranchAddress("t_DetIds", &t_DetIds, &b_t_DetIds);
2606   fChain->SetBranchAddress("t_HitEnergies", &t_HitEnergies, &b_t_HitEnergies);
2607   fChain->SetBranchAddress("t_trgbits", &t_trgbits, &b_t_trgbits);
2608   fChain->SetBranchAddress("t_DetIds1", &t_DetIds1, &b_t_DetIds1);
2609   fChain->SetBranchAddress("t_DetIds3", &t_DetIds3, &b_t_DetIds3);
2610   fChain->SetBranchAddress("t_HitEnergies1", &t_HitEnergies1, &b_t_HitEnergies1);
2611   fChain->SetBranchAddress("t_HitEnergies3", &t_HitEnergies3, &b_t_HitEnergies3);
2612   Notify();
2613 
2614   tout_DetIds = new std::vector<unsigned int>();
2615   tout_HitEnergies = new std::vector<double>();
2616   tout_trgbits = new std::vector<bool>();
2617   tout_DetIds1 = new std::vector<unsigned int>();
2618   tout_DetIds3 = new std::vector<unsigned int>();
2619   tout_HitEnergies1 = new std::vector<double>();
2620   tout_HitEnergies3 = new std::vector<double>();
2621 
2622   outputFile_ = TFile::Open(outFileName_.c_str(), "RECREATE");
2623   outputFile_->cd();
2624   outputDir_ = new TDirectoryFile(dirnm_.c_str(), dirnm_.c_str());
2625   outputDir_->cd();
2626   outputTree_ = new TTree("CalibTree", "CalibTree");
2627   outputTree_->Branch("t_Run", &tout_Run);
2628   outputTree_->Branch("t_Event", &tout_Event);
2629   outputTree_->Branch("t_DataType", &tout_DataType);
2630   outputTree_->Branch("t_ieta", &tout_ieta);
2631   outputTree_->Branch("t_iphi", &tout_iphi);
2632   outputTree_->Branch("t_EventWeight", &tout_EventWeight);
2633   outputTree_->Branch("t_nVtx", &tout_nVtx);
2634   outputTree_->Branch("t_nTrk", &tout_nTrk);
2635   outputTree_->Branch("t_goodPV", &tout_goodPV);
2636   outputTree_->Branch("t_l1pt", &tout_l1pt);
2637   outputTree_->Branch("t_l1eta", &tout_l1eta);
2638   outputTree_->Branch("t_l1phi", &tout_l1phi);
2639   outputTree_->Branch("t_l3pt", &tout_l3pt);
2640   outputTree_->Branch("t_l3eta", &tout_l3eta);
2641   outputTree_->Branch("t_l3phi", &tout_l3phi);
2642   outputTree_->Branch("t_p", &tout_p);
2643   outputTree_->Branch("t_pt", &tout_pt);
2644   outputTree_->Branch("t_phi", &tout_phi);
2645   outputTree_->Branch("t_mindR1", &tout_mindR1);
2646   outputTree_->Branch("t_mindR2", &tout_mindR2);
2647   outputTree_->Branch("t_eMipDR", &tout_eMipDR);
2648   outputTree_->Branch("t_eHcal", &tout_eHcal);
2649   outputTree_->Branch("t_eHcal10", &tout_eHcal10);
2650   outputTree_->Branch("t_eHcal30", &tout_eHcal30);
2651   outputTree_->Branch("t_hmaxNearP", &tout_hmaxNearP);
2652   outputTree_->Branch("t_rhoh", &tout_rhoh);
2653   outputTree_->Branch("t_selectTk", &tout_selectTk);
2654   outputTree_->Branch("t_qltyFlag", &tout_qltyFlag);
2655   outputTree_->Branch("t_qltyMissFlag", &tout_qltyMissFlag);
2656   outputTree_->Branch("t_qltyPVFlag", &tout_qltyPVFlag);
2657   outputTree_->Branch("t_gentrackP", &tout_gentrackP);
2658   outputTree_->Branch("t_DetIds", &tout_DetIds);
2659   outputTree_->Branch("t_HitEnergies", &tout_HitEnergies);
2660   outputTree_->Branch("t_trgbits", &tout_trgbits);
2661   outputTree_->Branch("t_DetIds1", &tout_DetIds1);
2662   outputTree_->Branch("t_DetIds3", &tout_DetIds3);
2663   outputTree_->Branch("t_HitEnergies1", &tout_HitEnergies1);
2664   outputTree_->Branch("t_HitEnergies3", &tout_HitEnergies3);
2665 
2666   std::cout << "Output CalibTree is created in directory " << dirnm_ << " of " << outFileName_ << std::endl;
2667 }
2668 
2669 Bool_t CalibSplit::Notify() {
2670   // The Notify() function is called when a new file is opened. This
2671   // can be either for a new TTree in a TChain or when when a new TTree
2672   // is started when using PROOF. It is normally not necessary to make changes
2673   // to the generated code, but the routine can be extended by the
2674   // user if needed. The return value is currently not used.
2675 
2676   return kTRUE;
2677 }
2678 
2679 void CalibSplit::Show(Long64_t entry) {
2680   // Print contents of entry.
2681   // If entry is not specified, print current entry
2682   if (!fChain)
2683     return;
2684   fChain->Show(entry);
2685 }
2686 
2687 Int_t CalibSplit::Cut(Long64_t) {
2688   // This function may be called from Loop.
2689   // returns  1 if entry is accepted.
2690   // returns -1 otherwise.
2691   return 1;
2692 }
2693 
2694 void CalibSplit::Loop(Long64_t nentries) {
2695   //   In a ROOT session, you can do:
2696   //      Root > .L CalibMonitor.C
2697   //      Root > CalibMonitor t
2698   //      Root > t.GetEntry(12); // Fill t data members with entry number 12
2699   //      Root > t.Show();       // Show values of entry 12
2700   //      Root > t.Show(16);     // Read and show values of entry 16
2701   //      Root > t.Loop();       // Loop on all entries
2702   //
2703 
2704   //   This is the loop skeleton where:
2705   //      jentry is the global entry number in the chain
2706   //      ientry is the entry number in the current Tree
2707   //   Note that the argument to GetEntry must be:
2708   //      jentry for TChain::GetEntry
2709   //      ientry for TTree::GetEntry and TBranch::GetEntry
2710   //
2711   //       To read only selected branches, Insert statements like:
2712   // METHOD1:
2713   //    fChain->SetBranchStatus("*",0);  // disable all branches
2714   //    fChain->SetBranchStatus("branchname",1);  // activate branchname
2715   // METHOD2: replace line
2716   //    fChain->GetEntry(jentry);       //read all branches
2717   //by  b_branchname->GetEntry(ientry); //read only this branch
2718 
2719   if (fChain == 0)
2720     return;
2721 
2722   // Find list of duplicate events
2723   if (nentries < 0)
2724     nentries = fChain->GetEntriesFast();
2725   std::cout << "Total entries " << nentries << ":" << fChain->GetEntriesFast() << std::endl;
2726   Long64_t nbytes(0), nb(0);
2727   unsigned int good(0), reject(0), kount(0);
2728   for (Long64_t jentry = 0; jentry < nentries; jentry++) {
2729     Long64_t ientry = LoadTree(jentry);
2730     if (ientry < 0)
2731       break;
2732     nb = fChain->GetEntry(jentry);
2733     nbytes += nb;
2734     if (jentry % 1000000 == 0)
2735       std::cout << "Entry " << jentry << " Run " << t_Run << " Event " << t_Event << std::endl;
2736     ++kount;
2737     bool select = ((t_p >= pmin_) && (t_p < pmax_));
2738     if (select && checkRun_) {
2739       if ((t_Run < runMin_) || (t_Run > runMax_))
2740         select = false;
2741     }
2742     if (!select) {
2743       ++reject;
2744       if (debug_)
2745         std::cout << "Rejected event " << t_Run << " " << t_Event << " " << t_p << std::endl;
2746       continue;
2747     }
2748     ++good;
2749     copyTree();
2750     outputTree_->Fill();
2751   }
2752   std::cout << "\nSelect " << good << " Reject " << reject << " entries out of a total of " << kount << " counts"
2753             << std::endl;
2754   close();
2755 }
2756 
2757 void CalibSplit::copyTree() {
2758   tout_Run = t_Run;
2759   tout_Event = t_Event;
2760   tout_DataType = t_DataType;
2761   tout_ieta = t_ieta;
2762   tout_iphi = t_iphi;
2763   tout_EventWeight = t_EventWeight;
2764   tout_nVtx = t_nVtx;
2765   tout_nTrk = t_nTrk;
2766   tout_goodPV = t_goodPV;
2767   tout_l1pt = t_l1pt;
2768   tout_l1eta = t_l1eta;
2769   tout_l1phi = t_l1phi;
2770   tout_l3pt = t_l3pt;
2771   tout_l3eta = t_l3eta;
2772   tout_l3phi = t_l3phi;
2773   tout_p = t_p;
2774   tout_pt = t_pt;
2775   tout_phi = t_phi;
2776   tout_mindR1 = t_mindR1;
2777   tout_mindR2 = t_mindR2;
2778   tout_eMipDR = t_eMipDR;
2779   tout_eHcal = t_eHcal;
2780   tout_eHcal10 = t_eHcal10;
2781   tout_eHcal30 = t_eHcal30;
2782   tout_hmaxNearP = t_hmaxNearP;
2783   tout_rhoh = t_rhoh;
2784   tout_selectTk = t_selectTk;
2785   tout_qltyFlag = t_qltyFlag;
2786   tout_qltyMissFlag = t_qltyMissFlag;
2787   tout_qltyPVFlag = t_qltyPVFlag;
2788   tout_gentrackP = t_gentrackP;
2789   tout_DetIds->clear();
2790   if (t_DetIds != nullptr) {
2791     tout_DetIds->reserve(t_DetIds->size());
2792     for (unsigned int i = 0; i < t_DetIds->size(); ++i)
2793       tout_DetIds->push_back((*t_DetIds)[i]);
2794   }
2795   tout_HitEnergies->clear();
2796   if (t_HitEnergies != nullptr) {
2797     tout_HitEnergies->reserve(t_HitEnergies->size());
2798     for (unsigned int i = 0; i < t_HitEnergies->size(); ++i)
2799       tout_HitEnergies->push_back((*t_HitEnergies)[i]);
2800   }
2801   tout_trgbits->clear();
2802   if (t_trgbits != nullptr) {
2803     tout_trgbits->reserve(t_trgbits->size());
2804     for (unsigned int i = 0; i < t_trgbits->size(); ++i)
2805       tout_trgbits->push_back((*t_trgbits)[i]);
2806   }
2807   tout_DetIds1->clear();
2808   if (t_DetIds1 != nullptr) {
2809     tout_DetIds1->reserve(t_DetIds1->size());
2810     for (unsigned int i = 0; i < t_DetIds1->size(); ++i)
2811       tout_DetIds1->push_back((*t_DetIds1)[i]);
2812   }
2813   tout_DetIds3->clear();
2814   if (t_DetIds3 != nullptr) {
2815     tout_DetIds3->reserve(t_DetIds3->size());
2816     for (unsigned int i = 0; i < t_DetIds3->size(); ++i)
2817       tout_DetIds3->push_back((*t_DetIds3)[i]);
2818   }
2819   tout_HitEnergies1->clear();
2820   if (t_HitEnergies1 != nullptr) {
2821     tout_HitEnergies1->reserve(t_HitEnergies1->size());
2822     for (unsigned int i = 0; i < t_HitEnergies1->size(); ++i)
2823       tout_HitEnergies1->push_back((*t_HitEnergies1)[i]);
2824   }
2825   tout_HitEnergies3->clear();
2826   if (t_HitEnergies1 != nullptr) {
2827     tout_HitEnergies3->reserve(t_HitEnergies3->size());
2828     for (unsigned int i = 0; i < t_HitEnergies3->size(); ++i)
2829       tout_HitEnergies3->push_back((*t_HitEnergies3)[i]);
2830   }
2831 }
2832 
2833 void CalibSplit::close() {
2834   if (outputFile_) {
2835     outputDir_->cd();
2836     std::cout << "file yet to be Written" << std::endl;
2837     outputTree_->Write();
2838     std::cout << "file Written" << std::endl;
2839     outputFile_->Close();
2840     std::cout << "now doing return" << std::endl;
2841   }
2842   outputFile_ = nullptr;
2843 }