Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-06-07 02:29:08

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, 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 heistograms 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, 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
0040 //   prefix (std::string)      = String to be added to the name of histogram
0041 //                               (usually a 4 character string; default="")
0042 //   corrFileName (const char*)= name of the text file having the correction
0043 //                               factors to be used (default="", no corr.)
0044 //   rcorFileName (const char*)= name of the text file having the correction
0045 //                               factors as a function of run numbers or depth
0046 //                               to be used for raddam/depth/pileup/phisym
0047 //                               dependent correction  (default="", no corr.)
0048 //   puCorr (int)              = PU correction to be applied or not: 0 no
0049 //                               correction; < 0 use eDelta; > 0 rho dependent
0050 //                               correction (-8)
0051 //   flag (int)                = 7 digit integer (ymlthdo) with control
0052 //                               information (y=2/1/0 containing list of
0053 //                               ieta, iphi of channels to be selected (2);
0054 //                               list containing depth dependent weights for
0055 //                               each ieta (1); list of duplicate entries
0056 //                               (0) in dupFileName; m=0/1 for controlling
0057 //                               creation of depth depedendent histograms;
0058 //                               l=4/3/2/1/0 for type of rcorFileName (4 for
0059 //                               using results from phi-symmetry; 3 for
0060 //                               pileup correction using machine learning
0061 //                               method; 2 for overall response corrections;
0062 //                               1 for depth dependence corrections; 0 for
0063 //                               raddam corrections);
0064 //                               t = bit information (lower bit set will
0065 //                               apply a cut on L1 closeness; and higher bit
0066 //                               set read correction file with Marina format);
0067 //                               h =0/1 flag to create energy histograms
0068 //                               d =0/1 flag to create basic set of histograms;
0069 //                               o =0/1/2 for tight / loose / flexible
0070 //                               selection). Default = 101111
0071 //   isRealData (bool)         = true/false for data/MC (default true)
0072 //   truncateFlag    (int)     = Flag to treat different depths differently (0)
0073 //                               both depths of ieta 15, 16 of HB as depth 1 (1)
0074 //                               all depths as depth 1 (2), ignore all depth
0075 //                               index in HE (depth index set 1) (3); ignore
0076 //                               depth index in HB (depth index set 1) (4); all
0077 //                               all depths in HB and HE with values > 1 as
0078 //                               depth 2 (5); for depth = 1 and 2, depth = 1,
0079 //                               else depth = 2 (6); in case of HB, depths 1
0080 //                               and 2 are set to 1, else depth = 2; for HE
0081 //                               ignore depth index (7); in case of HE, depths 1
0082 //                               and 2 are set to 1, else depth =2; for HB
0083 //                               ignore depth index (8); ignore depth index for
0084 //                               depth > 1 in HB and all depth index for HE (9).
0085 //                               (Default 0)
0086 //   useGen (bool)             = true/false to use generator level momentum
0087 //                               or reconstruction level momentum (def false)
0088 //   scale (double)            = energy scale if correction factor to be used
0089 //                               (default = 1.0)
0090 //   useScale (int)            = two digit number (do) with o: as the flag for
0091 //                               application of scale factor (0: nowehere,
0092 //                               1: barrel; 2: endcap, 3: everywhere)
0093 //                               barrel => |ieta| < 16; endcap => |ieta| > 15;
0094 //                               d: as the format for threshold application,
0095 //                               0: no threshold; 1: 2022 prompt data; 2:
0096 //                               2022 reco data; 3: 2023 prompt data
0097 //                               (default = 0)
0098 //   etalo/etahi (int,int)     = |eta| ranges (0:30)
0099 //   runlo  (int)              = lower value of run number to be included (+ve)
0100 //                               or excluded (-ve) (default 0)
0101 //   runhi  (int)              = higher value of run number to be included
0102 //                               (+ve) or excluded (-ve) (def 9999999)
0103 //   phimin          (int)     = minimum iphi value (1)
0104 //   phimax          (int)     = maximum iphi value (72)
0105 //   zside           (int)     = the side of the detector if phimin and phimax
0106 //                               differ from 1-72 (1)
0107 //   nvxlo           (int)     = minimum # of vertex in event to be used (0)
0108 //   nvxhi           (int)     = maximum # of vertex in event to be used (1000)
0109 //   rbxFile         (char *)  = Name of the file containing a list of RBX's
0110 //                               to be consdered (default = ""). RBX's are
0111 //                               specified by zside*(Subdet*100+RBX #).
0112 //                               For HEP17 it will be 217
0113 //   exclude         (bool)    = RBX specified by the contents in *rbxFile* to
0114 //                               be exluded or only considered (default = false)
0115 //   etamax          (bool)    = if set and if the corr-factor not found in the
0116 //                               corrFactor table, the corr-factor for the
0117 //                               corresponding zside, depth=1 and maximum ieta
0118 //                               in the table is taken (false)
0119 //   nentries        (int)     = maximum number of entries to be processed,
0120 //                               if -1, all entries to be processed (-1)
0121 //
0122 //   histFileName (std::string)= name of the file containing saved histograms
0123 //   append (bool)             = true/false if the histogram file to be opened
0124 //                               in append/output mode
0125 //   all (bool)                = true/false if all histograms to be saved or
0126 //                               not (def false)
0127 //
0128 //   text  (string)            = string to be put in the title
0129 //   flagC (int)               = 3 digit integer (hdo) with control
0130 //                               information (h=0/1 for plottting the depth
0131 //                               depedendent histograms;
0132 //                               d =0/1 flag to plot energy histograms;
0133 //                               o =0/1 flag to plot basic set of histograms;
0134 //                               Default = 111
0135 //   save (int)                = flag to create or not save the plot in a file
0136 //                               (0 = no save, > 0 pdf file, < 0 hpg file)
0137 //
0138 //   outFileName (std::string)= name of the file containing saved tree
0139 //   pmin (double)            = minimum track momentum (40.0)
0140 //   pmax (double)            = maximum track momentum (60.0)
0141 //   debug (bool)             = debug flag (false)
0142 //////////////////////////////////////////////////////////////////////////////
0143 #include <TROOT.h>
0144 #include <TChain.h>
0145 #include <TFile.h>
0146 #include <TH1D.h>
0147 #include <TH2F.h>
0148 #include <TProfile.h>
0149 #include <TFitResult.h>
0150 #include <TFitResultPtr.h>
0151 #include <TStyle.h>
0152 #include <TCanvas.h>
0153 #include <TLegend.h>
0154 #include <TPaveStats.h>
0155 #include <TPaveText.h>
0156 
0157 #include <algorithm>
0158 #include <iomanip>
0159 #include <iostream>
0160 #include <fstream>
0161 #include <sstream>
0162 #include <map>
0163 #include <vector>
0164 #include <string>
0165 
0166 void unpackDetId(unsigned int, int &, int &, int &, int &, int &);
0167 #include "CalibCorr.C"
0168 
0169 namespace CalibPlots {
0170   static const int npbin = 5;
0171   static const int netabin = 4;
0172   static const int ndepth = 7;
0173   static const int ntitles = 5;
0174   static const int npbin0 = (npbin + 1);
0175   int getP(int k) {
0176     const int ipbin[npbin0] = {20, 30, 40, 60, 100, 500};
0177     return ((k >= 0 && k < npbin0) ? ipbin[k] : 0);
0178   }
0179   double getMomentum(int k) { return (double)(getP(k)); }
0180   int getEta(int k) {
0181     int ietas[netabin] = {0, 13, 18, 23};
0182     return ((k >= 0 && k < netabin) ? ietas[k] : -1);
0183   }
0184   std::string getTitle(int k) {
0185     std::string titl[ntitles] = {
0186         "all tracks", "good quality tracks", "selected tracks", "isolated good tracks", "tracks having MIP in ECAL"};
0187     return ((k >= 0 && k < ntitles) ? titl[k] : "");
0188   }
0189 }  // namespace CalibPlots
0190 
0191 class CalibPlotProperties {
0192 public:
0193   TChain *fChain;  //!pointer to the analyzed TTree or TChain
0194   Int_t fCurrent;  //!current Tree number in a TChain
0195 
0196   // Declaration of leaf types
0197   Int_t t_Run;
0198   Int_t t_Event;
0199   Int_t t_DataType;
0200   Int_t t_ieta;
0201   Int_t t_iphi;
0202   Double_t t_EventWeight;
0203   Int_t t_nVtx;
0204   Int_t t_nTrk;
0205   Int_t t_goodPV;
0206   Double_t t_l1pt;
0207   Double_t t_l1eta;
0208   Double_t t_l1phi;
0209   Double_t t_l3pt;
0210   Double_t t_l3eta;
0211   Double_t t_l3phi;
0212   Double_t t_p;
0213   Double_t t_pt;
0214   Double_t t_phi;
0215   Double_t t_mindR1;
0216   Double_t t_mindR2;
0217   Double_t t_eMipDR;
0218   Double_t t_eHcal;
0219   Double_t t_eHcal10;
0220   Double_t t_eHcal30;
0221   Double_t t_hmaxNearP;
0222   Double_t t_rhoh;
0223   Bool_t t_selectTk;
0224   Bool_t t_qltyFlag;
0225   Bool_t t_qltyMissFlag;
0226   Bool_t t_qltyPVFlag;
0227   Double_t t_gentrackP;
0228   std::vector<unsigned int> *t_DetIds;
0229   std::vector<double> *t_HitEnergies;
0230   std::vector<bool> *t_trgbits;
0231   std::vector<unsigned int> *t_DetIds1;
0232   std::vector<unsigned int> *t_DetIds3;
0233   std::vector<double> *t_HitEnergies1;
0234   std::vector<double> *t_HitEnergies3;
0235 
0236   // List of branches
0237   TBranch *b_t_Run;           //!
0238   TBranch *b_t_Event;         //!
0239   TBranch *b_t_DataType;      //!
0240   TBranch *b_t_ieta;          //!
0241   TBranch *b_t_iphi;          //!
0242   TBranch *b_t_EventWeight;   //!
0243   TBranch *b_t_nVtx;          //!
0244   TBranch *b_t_nTrk;          //!
0245   TBranch *b_t_goodPV;        //!
0246   TBranch *b_t_l1pt;          //!
0247   TBranch *b_t_l1eta;         //!
0248   TBranch *b_t_l1phi;         //!
0249   TBranch *b_t_l3pt;          //!
0250   TBranch *b_t_l3eta;         //!
0251   TBranch *b_t_l3phi;         //!
0252   TBranch *b_t_p;             //!
0253   TBranch *b_t_pt;            //!
0254   TBranch *b_t_phi;           //!
0255   TBranch *b_t_mindR1;        //!
0256   TBranch *b_t_mindR2;        //!
0257   TBranch *b_t_eMipDR;        //!
0258   TBranch *b_t_eHcal;         //!
0259   TBranch *b_t_eHcal10;       //!
0260   TBranch *b_t_eHcal30;       //!
0261   TBranch *b_t_hmaxNearP;     //!
0262   TBranch *b_t_rhoh;          //!
0263   TBranch *b_t_selectTk;      //!
0264   TBranch *b_t_qltyFlag;      //!
0265   TBranch *b_t_qltyMissFlag;  //!
0266   TBranch *b_t_qltyPVFlag;    //!
0267   TBranch *b_t_gentrackP;     //!
0268   TBranch *b_t_DetIds;        //!
0269   TBranch *b_t_HitEnergies;   //!
0270   TBranch *b_t_trgbits;       //!
0271   TBranch *b_t_DetIds1;       //!
0272   TBranch *b_t_DetIds3;       //!
0273   TBranch *b_t_HitEnergies1;  //!
0274   TBranch *b_t_HitEnergies3;  //!
0275 
0276   CalibPlotProperties(const char *fname,
0277                       const std::string &dirname,
0278                       const char *dupFileName,
0279                       const std::string &prefix = "",
0280                       const char *corrFileName = "",
0281                       const char *rcorFileName = "",
0282                       int puCorr = -8,
0283                       int flag = 101111,
0284                       bool isRealData = true,
0285                       int truncateFlag = 0,
0286                       bool useGen = false,
0287                       double scale = 1.0,
0288                       int useScale = 0,
0289                       int etalo = 0,
0290                       int etahi = 30,
0291                       int runlo = 0,
0292                       int runhi = 99999999,
0293                       int phimin = 1,
0294                       int phimax = 72,
0295                       int zside = 1,
0296                       int nvxlo = 0,
0297                       int nvxhi = 1000,
0298                       const char *rbxFile = "",
0299                       bool exclude = false,
0300                       bool etamax = false);
0301   virtual ~CalibPlotProperties();
0302   virtual Int_t Cut(Long64_t entry);
0303   virtual Int_t GetEntry(Long64_t entry);
0304   virtual Long64_t LoadTree(Long64_t entry);
0305   virtual void Init(TChain *);
0306   virtual void Loop(Long64_t nentries = -1);
0307   virtual Bool_t Notify();
0308   virtual void Show(Long64_t entry = -1);
0309   bool goodTrack(double &eHcal, double &cut, bool debug);
0310   bool selectPhi(bool debug);
0311   void savePlot(const std::string &theName, bool append, bool all = false, bool debug = false);
0312   void correctEnergy(double &ener);
0313 
0314 private:
0315   static const int kp50 = 2;
0316   CalibCorrFactor *corrFactor_;
0317   CalibCorr *cFactor_;
0318   CalibSelectRBX *cSelect_;
0319   CalibDuplicate *cDuplicate_;
0320   const std::string fname_, dirnm_, prefix_, outFileName_;
0321   const int corrPU_, flag_;
0322   const bool isRealData_, useGen_;
0323   const int truncateFlag_;
0324   const int etalo_, etahi_;
0325   int runlo_, runhi_;
0326   const int phimin_, phimax_, zside_, nvxlo_, nvxhi_;
0327   const char *rbxFile_;
0328   bool exclude_, corrE_, cutL1T_;
0329   bool includeRun_, getHist_;
0330   int flexibleSelect_, ifDepth_, duplicate_, thrForm_;
0331   bool plotBasic_, plotEnergy_, plotHists_;
0332   double log2by18_;
0333   std::ofstream fileout_;
0334   std::vector<std::pair<int, int> > events_;
0335   TH1D *h_p[CalibPlots::ntitles];
0336   TH1D *h_eta[CalibPlots::ntitles], *h_nvtx, *h_nvtxEv, *h_nvtxTk;
0337   std::vector<TH1D *> h_eta0, h_eta1, h_eta2, h_eta3, h_eta4;
0338   std::vector<TH1D *> h_dL1, h_vtx;
0339   std::vector<TH1D *> h_etaEH[CalibPlots::npbin0];
0340   std::vector<TH1D *> h_etaEp[CalibPlots::npbin0];
0341   std::vector<TH1D *> h_etaEE[CalibPlots::npbin0];
0342   std::vector<TH1D *> h_etaEE0[CalibPlots::npbin0];
0343   std::vector<TH1D *> h_mom, h_eEcal, h_eHcal;
0344   std::vector<TH1F *> h_bvlist, h_bvlist2, h_evlist, h_evlist2;
0345   std::vector<TH1F *> h_bvlist3, h_evlist3;
0346   TH2F *h_etaE;
0347 };
0348 
0349 CalibPlotProperties::CalibPlotProperties(const char *fname,
0350                                          const std::string &dirnm,
0351                                          const char *dupFileName,
0352                                          const std::string &prefix,
0353                                          const char *corrFileName,
0354                                          const char *rcorFileName,
0355                                          int puCorr,
0356                                          int flag,
0357                                          bool isRealData,
0358                                          int truncate,
0359                                          bool useGen,
0360                                          double scl,
0361                                          int useScale,
0362                                          int etalo,
0363                                          int etahi,
0364                                          int runlo,
0365                                          int runhi,
0366                                          int phimin,
0367                                          int phimax,
0368                                          int zside,
0369                                          int nvxlo,
0370                                          int nvxhi,
0371                                          const char *rbxFile,
0372                                          bool exc,
0373                                          bool etam)
0374     : corrFactor_(nullptr),
0375       cFactor_(nullptr),
0376       cSelect_(nullptr),
0377       cDuplicate_(nullptr),
0378       fname_(fname),
0379       dirnm_(dirnm),
0380       prefix_(prefix),
0381       corrPU_(puCorr),
0382       flag_(flag),
0383       isRealData_(isRealData),
0384       useGen_(useGen),
0385       truncateFlag_(truncate),
0386       etalo_(etalo),
0387       etahi_(etahi),
0388       runlo_(runlo),
0389       runhi_(runhi),
0390       phimin_(phimin),
0391       phimax_(phimax),
0392       zside_(zside),
0393       nvxlo_(nvxlo),
0394       nvxhi_(nvxhi),
0395       rbxFile_(rbxFile),
0396       exclude_(exc),
0397       includeRun_(true) {
0398   // if parameter tree is not specified (or zero), connect the file
0399   // used to generate this class and read the Tree
0400 
0401   flexibleSelect_ = ((flag_ / 1) % 10);
0402   plotBasic_ = (((flag_ / 10) % 10) > 0);
0403   plotEnergy_ = (((flag_ / 10) % 10) > 0);
0404   int oneplace = ((flag_ / 1000) % 10);
0405   cutL1T_ = (oneplace % 2);
0406   bool marina = ((oneplace / 2) % 2);
0407   ifDepth_ = ((flag_ / 10000) % 10);
0408   plotHists_ = (((flag_ / 100000) % 10) > 0);
0409   duplicate_ = ((flag_ / 1000000) % 10);
0410   log2by18_ = std::log(2.5) / 18.0;
0411   if (runlo_ < 0 || runhi_ < 0) {
0412     runlo_ = std::abs(runlo_);
0413     runhi_ = std::abs(runhi_);
0414     includeRun_ = false;
0415   }
0416   int useScale0 = useScale % 10;
0417   thrForm_ = useScale / 10;
0418   char treeName[400];
0419   sprintf(treeName, "%s/CalibTree", dirnm.c_str());
0420   TChain *chain = new TChain(treeName);
0421   std::cout << "Create a chain for " << treeName << " from " << fname << " flags " << flexibleSelect_ << "|"
0422             << plotBasic_ << "|"
0423             << "|" << plotEnergy_ << "|" << plotHists_ << "|" << corrPU_ << " cons " << log2by18_ << " eta range "
0424             << etalo_ << ":" << etahi_ << " run range " << runlo_ << ":" << runhi_ << " (inclusion flag " << includeRun_
0425             << ") Vertex Range " << nvxlo_ << ":" << nvxhi_ << " Threshold Flag " << thrForm_ << std::endl;
0426   corrFactor_ = new CalibCorrFactor(corrFileName, useScale0, scl, etam, marina, false);
0427   if (!fillChain(chain, fname)) {
0428     std::cout << "*****No valid tree chain can be obtained*****" << std::endl;
0429   } else {
0430     std::cout << "Proceed with a tree chain with " << chain->GetEntries() << " entries" << std::endl;
0431     Init(chain);
0432     if (std::string(rcorFileName) != "") {
0433       cFactor_ = new CalibCorr(rcorFileName, ifDepth_, false);
0434       if (cFactor_->absent())
0435         ifDepth_ = -1;
0436     } else {
0437       ifDepth_ = -1;
0438     }
0439     if (std::string(dupFileName) != "")
0440       cDuplicate_ = new CalibDuplicate(dupFileName, duplicate_, false);
0441     if (std::string(rbxFile) != "")
0442       cSelect_ = new CalibSelectRBX(rbxFile, false);
0443   }
0444 }
0445 
0446 CalibPlotProperties::~CalibPlotProperties() {
0447   delete corrFactor_;
0448   delete cFactor_;
0449   delete cSelect_;
0450   if (!fChain)
0451     return;
0452   delete fChain->GetCurrentFile();
0453 }
0454 
0455 Int_t CalibPlotProperties::GetEntry(Long64_t entry) {
0456   // Read contents of entry.
0457   if (!fChain)
0458     return 0;
0459   return fChain->GetEntry(entry);
0460 }
0461 
0462 Long64_t CalibPlotProperties::LoadTree(Long64_t entry) {
0463   // Set the environment to read one entry
0464   if (!fChain)
0465     return -5;
0466   Long64_t centry = fChain->LoadTree(entry);
0467   if (centry < 0)
0468     return centry;
0469   if (!fChain->InheritsFrom(TChain::Class()))
0470     return centry;
0471   TChain *chain = (TChain *)fChain;
0472   if (chain->GetTreeNumber() != fCurrent) {
0473     fCurrent = chain->GetTreeNumber();
0474     Notify();
0475   }
0476   return centry;
0477 }
0478 
0479 void CalibPlotProperties::Init(TChain *tree) {
0480   // The Init() function is called when the selector needs to initialize
0481   // a new tree or chain. Typically here the branch addresses and branch
0482   // pointers of the tree will be set.
0483   // It is normally not necessary to make changes to the generated
0484   // code, but the routine can be extended by the user if needed.
0485   // Init() will be called many times when running on PROOF
0486   // (once per file to be processed).
0487 
0488   // Set object pointer
0489   t_DetIds = 0;
0490   t_DetIds1 = 0;
0491   t_DetIds3 = 0;
0492   t_HitEnergies = 0;
0493   t_HitEnergies1 = 0;
0494   t_HitEnergies3 = 0;
0495   t_trgbits = 0;
0496   // Set branch addresses and branch pointers
0497   fChain = tree;
0498   fCurrent = -1;
0499   if (!tree)
0500     return;
0501   fChain->SetMakeClass(1);
0502 
0503   fChain->SetBranchAddress("t_Run", &t_Run, &b_t_Run);
0504   fChain->SetBranchAddress("t_Event", &t_Event, &b_t_Event);
0505   fChain->SetBranchAddress("t_DataType", &t_DataType, &b_t_DataType);
0506   fChain->SetBranchAddress("t_ieta", &t_ieta, &b_t_ieta);
0507   fChain->SetBranchAddress("t_iphi", &t_iphi, &b_t_iphi);
0508   fChain->SetBranchAddress("t_EventWeight", &t_EventWeight, &b_t_EventWeight);
0509   fChain->SetBranchAddress("t_nVtx", &t_nVtx, &b_t_nVtx);
0510   fChain->SetBranchAddress("t_nTrk", &t_nTrk, &b_t_nTrk);
0511   fChain->SetBranchAddress("t_goodPV", &t_goodPV, &b_t_goodPV);
0512   fChain->SetBranchAddress("t_l1pt", &t_l1pt, &b_t_l1pt);
0513   fChain->SetBranchAddress("t_l1eta", &t_l1eta, &b_t_l1eta);
0514   fChain->SetBranchAddress("t_l1phi", &t_l1phi, &b_t_l1phi);
0515   fChain->SetBranchAddress("t_l3pt", &t_l3pt, &b_t_l3pt);
0516   fChain->SetBranchAddress("t_l3eta", &t_l3eta, &b_t_l3eta);
0517   fChain->SetBranchAddress("t_l3phi", &t_l3phi, &b_t_l3phi);
0518   fChain->SetBranchAddress("t_p", &t_p, &b_t_p);
0519   fChain->SetBranchAddress("t_pt", &t_pt, &b_t_pt);
0520   fChain->SetBranchAddress("t_phi", &t_phi, &b_t_phi);
0521   fChain->SetBranchAddress("t_mindR1", &t_mindR1, &b_t_mindR1);
0522   fChain->SetBranchAddress("t_mindR2", &t_mindR2, &b_t_mindR2);
0523   fChain->SetBranchAddress("t_eMipDR", &t_eMipDR, &b_t_eMipDR);
0524   fChain->SetBranchAddress("t_eHcal", &t_eHcal, &b_t_eHcal);
0525   fChain->SetBranchAddress("t_eHcal10", &t_eHcal10, &b_t_eHcal10);
0526   fChain->SetBranchAddress("t_eHcal30", &t_eHcal30, &b_t_eHcal30);
0527   fChain->SetBranchAddress("t_hmaxNearP", &t_hmaxNearP, &b_t_hmaxNearP);
0528   fChain->SetBranchAddress("t_rhoh", &t_rhoh, &b_t_rhoh);
0529   fChain->SetBranchAddress("t_selectTk", &t_selectTk, &b_t_selectTk);
0530   fChain->SetBranchAddress("t_qltyFlag", &t_qltyFlag, &b_t_qltyFlag);
0531   fChain->SetBranchAddress("t_qltyMissFlag", &t_qltyMissFlag, &b_t_qltyMissFlag);
0532   fChain->SetBranchAddress("t_qltyPVFlag", &t_qltyPVFlag, &b_t_qltyPVFlag);
0533   fChain->SetBranchAddress("t_gentrackP", &t_gentrackP, &b_t_gentrackP);
0534   fChain->SetBranchAddress("t_DetIds", &t_DetIds, &b_t_DetIds);
0535   fChain->SetBranchAddress("t_HitEnergies", &t_HitEnergies, &b_t_HitEnergies);
0536   fChain->SetBranchAddress("t_trgbits", &t_trgbits, &b_t_trgbits);
0537   fChain->SetBranchAddress("t_DetIds1", &t_DetIds1, &b_t_DetIds1);
0538   fChain->SetBranchAddress("t_DetIds3", &t_DetIds3, &b_t_DetIds3);
0539   fChain->SetBranchAddress("t_HitEnergies1", &t_HitEnergies1, &b_t_HitEnergies1);
0540   fChain->SetBranchAddress("t_HitEnergies3", &t_HitEnergies3, &b_t_HitEnergies3);
0541   Notify();
0542 
0543   char name[20], title[200];
0544   unsigned int kk(0);
0545 
0546   if (plotBasic_) {
0547     std::cout << "Book Basic Histos" << std::endl;
0548     h_nvtx = new TH1D("hnvtx", "Number of vertices (selected entries)", 10, 0, 100);
0549     h_nvtx->Sumw2();
0550     h_nvtxEv = new TH1D("hnvtxEv", "Number of vertices (selected events)", 10, 0, 100);
0551     h_nvtxEv->Sumw2();
0552     h_nvtxTk = new TH1D("hnvtxTk", "Number of vertices (selected tracks)", 10, 0, 100);
0553     h_nvtxTk->Sumw2();
0554     for (int k = 0; k < CalibPlots::ntitles; ++k) {
0555       sprintf(name, "%sp%d", prefix_.c_str(), k);
0556       sprintf(title, "Momentum for %s", CalibPlots::getTitle(k).c_str());
0557       h_p[k] = new TH1D(name, title, 150, 0.0, 150.0);
0558       sprintf(name, "%seta%d", prefix_.c_str(), k);
0559       sprintf(title, "#eta for %s", CalibPlots::getTitle(k).c_str());
0560       h_eta[k] = new TH1D(name, title, 60, -30.0, 30.0);
0561     }
0562     for (int k = 0; k < CalibPlots::npbin; ++k) {
0563       sprintf(name, "%seta0%d", prefix_.c_str(), k);
0564       sprintf(title,
0565               "#eta for %s (p = %d:%d GeV)",
0566               CalibPlots::getTitle(0).c_str(),
0567               CalibPlots::getP(k),
0568               CalibPlots::getP(k + 1));
0569       h_eta0.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0570       kk = h_eta0.size() - 1;
0571       h_eta0[kk]->Sumw2();
0572       sprintf(name, "%seta1%d", prefix_.c_str(), k);
0573       sprintf(title,
0574               "#eta for %s (p = %d:%d GeV)",
0575               CalibPlots::getTitle(1).c_str(),
0576               CalibPlots::getP(k),
0577               CalibPlots::getP(k + 1));
0578       h_eta1.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0579       kk = h_eta1.size() - 1;
0580       h_eta1[kk]->Sumw2();
0581       sprintf(name, "%seta2%d", prefix_.c_str(), k);
0582       sprintf(title,
0583               "#eta for %s (p = %d:%d GeV)",
0584               CalibPlots::getTitle(2).c_str(),
0585               CalibPlots::getP(k),
0586               CalibPlots::getP(k + 1));
0587       h_eta2.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0588       kk = h_eta2.size() - 1;
0589       h_eta2[kk]->Sumw2();
0590       sprintf(name, "%seta3%d", prefix_.c_str(), k);
0591       sprintf(title,
0592               "#eta for %s (p = %d:%d GeV)",
0593               CalibPlots::getTitle(3).c_str(),
0594               CalibPlots::getP(k),
0595               CalibPlots::getP(k + 1));
0596       h_eta3.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0597       kk = h_eta3.size() - 1;
0598       h_eta3[kk]->Sumw2();
0599       sprintf(name, "%seta4%d", prefix_.c_str(), k);
0600       sprintf(title,
0601               "#eta for %s (p = %d:%d GeV)",
0602               CalibPlots::getTitle(4).c_str(),
0603               CalibPlots::getP(k),
0604               CalibPlots::getP(k + 1));
0605       h_eta4.push_back(new TH1D(name, title, 60, -30.0, 30.0));
0606       kk = h_eta4.size() - 1;
0607       h_eta4[kk]->Sumw2();
0608       sprintf(name, "%sdl1%d", prefix_.c_str(), k);
0609       sprintf(title, "Distance from L1 (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
0610       h_dL1.push_back(new TH1D(name, title, 160, 0.0, 8.0));
0611       kk = h_dL1.size() - 1;
0612       h_dL1[kk]->Sumw2();
0613       sprintf(name, "%svtx%d", prefix_.c_str(), k);
0614       sprintf(title, "N_{Vertex} (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
0615       h_vtx.push_back(new TH1D(name, title, 100, 0.0, 100.0));
0616       kk = h_vtx.size() - 1;
0617       h_vtx[kk]->Sumw2();
0618     }
0619   }
0620 
0621   if (plotEnergy_) {
0622     std::cout << "Make plots for good tracks" << std::endl;
0623     for (int k = 0; k < CalibPlots::npbin; ++k) {
0624       for (int j = etalo_; j <= etahi_ + 1; ++j) {
0625         sprintf(name, "%senergyH%d%d", prefix_.c_str(), k, j);
0626         if (j > etahi_)
0627           sprintf(title,
0628                   "HCAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
0629                   CalibPlots::getTitle(3).c_str(),
0630                   CalibPlots::getP(k),
0631                   CalibPlots::getP(k + 1),
0632                   etalo_,
0633                   etahi_);
0634         else
0635           sprintf(title,
0636                   "HCAL energy for %s (p = %d:%d GeV |#eta| = %d)",
0637                   CalibPlots::getTitle(3).c_str(),
0638                   CalibPlots::getP(k),
0639                   CalibPlots::getP(k + 1),
0640                   j);
0641         h_etaEH[k].push_back(new TH1D(name, title, 200, 0, 200));
0642         kk = h_etaEH[k].size() - 1;
0643         h_etaEH[k][kk]->Sumw2();
0644         sprintf(name, "%senergyP%d%d", prefix_.c_str(), k, j);
0645         if (j > etahi_)
0646           sprintf(title,
0647                   "momentum for %s (p = %d:%d GeV |#eta| = %d:%d)",
0648                   CalibPlots::getTitle(3).c_str(),
0649                   CalibPlots::getP(k),
0650                   CalibPlots::getP(k + 1),
0651                   etalo_,
0652                   etahi_);
0653         else
0654           sprintf(title,
0655                   "momentum for %s (p = %d:%d GeV |#eta| = %d)",
0656                   CalibPlots::getTitle(3).c_str(),
0657                   CalibPlots::getP(k),
0658                   CalibPlots::getP(k + 1),
0659                   j);
0660         h_etaEp[k].push_back(new TH1D(name, title, 100, 0, 100));
0661         kk = h_etaEp[k].size() - 1;
0662         h_etaEp[k][kk]->Sumw2();
0663         sprintf(name, "%senergyE%d%d", prefix_.c_str(), k, j);
0664         if (j > etahi_)
0665           sprintf(title,
0666                   "ECAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
0667                   CalibPlots::getTitle(3).c_str(),
0668                   CalibPlots::getP(k),
0669                   CalibPlots::getP(k + 1),
0670                   etalo_,
0671                   etahi_);
0672         else
0673           sprintf(title,
0674                   "ECAL energy for %s (p = %d:%d GeV |#eta| = %d)",
0675                   CalibPlots::getTitle(3).c_str(),
0676                   CalibPlots::getP(k),
0677                   CalibPlots::getP(k + 1),
0678                   j);
0679         h_etaEE[k].push_back(new TH1D(name, title, 100, 0, 10));
0680         kk = h_etaEE[k].size() - 1;
0681         h_etaEE[k][kk]->Sumw2();
0682         sprintf(name, "%senergyER%d%d", prefix_.c_str(), k, j);
0683         h_etaEE0[k].push_back(new TH1D(name, title, 100, 0, 1));
0684         kk = h_etaEE0[k].size() - 1;
0685         h_etaEE0[k][kk]->Sumw2();
0686       }
0687     }
0688 
0689     for (int j = 0; j < CalibPlots::netabin; ++j) {
0690       sprintf(name, "%senergyH%d", prefix_.c_str(), j);
0691       if (j == 0)
0692         sprintf(title, "HCAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
0693       else
0694         sprintf(title,
0695                 "HCAL energy for %s (|#eta| = %d:%d)",
0696                 CalibPlots::getTitle(3).c_str(),
0697                 CalibPlots::getEta(j - 1),
0698                 CalibPlots::getEta(j));
0699       h_eHcal.push_back(new TH1D(name, title, 200, 0, 200));
0700       kk = h_eHcal.size() - 1;
0701       h_eHcal[kk]->Sumw2();
0702       sprintf(name, "%senergyP%d", prefix_.c_str(), j);
0703       if (j == 0)
0704         sprintf(title, "Track momentum for %s (All)", CalibPlots::getTitle(3).c_str());
0705       else
0706         sprintf(title,
0707                 "Track momentum for %s (|#eta| = %d:%d)",
0708                 CalibPlots::getTitle(3).c_str(),
0709                 CalibPlots::getEta(j - 1),
0710                 CalibPlots::getEta(j));
0711       h_mom.push_back(new TH1D(name, title, 100, 0, 100));
0712       kk = h_mom.size() - 1;
0713       h_mom[kk]->Sumw2();
0714       sprintf(name, "%senergyE%d", prefix_.c_str(), j);
0715       if (j == 0)
0716         sprintf(title, "ECAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
0717       else
0718         sprintf(title,
0719                 "ECAL energy for %s (|#eta| = %d:%d)",
0720                 CalibPlots::getTitle(3).c_str(),
0721                 CalibPlots::getEta(j - 1),
0722                 CalibPlots::getEta(j));
0723       h_eEcal.push_back(new TH1D(name, title, 100, 0, 10));
0724       kk = h_eEcal.size() - 1;
0725       h_eEcal[kk]->Sumw2();
0726     }
0727   }
0728 
0729   if (plotHists_) {
0730     for (int i = 0; i < CalibPlots::ndepth; i++) {
0731       sprintf(name, "b_edepth%d", i);
0732       sprintf(title, "Total RecHit energy in depth %d (Barrel)", i + 1);
0733       h_bvlist.push_back(new TH1F(name, title, 1000, 0, 100));
0734       h_bvlist[i]->Sumw2();
0735       sprintf(name, "b_recedepth%d", i);
0736       sprintf(title, "RecHit energy in depth %d (Barrel)", i + 1);
0737       h_bvlist2.push_back(new TH1F(name, title, 1000, 0, 100));
0738       h_bvlist2[i]->Sumw2();
0739       sprintf(name, "b_nrecdepth%d", i);
0740       sprintf(title, "#RecHits in depth %d (Barrel)", i + 1);
0741       h_bvlist3.push_back(new TH1F(name, title, 1000, 0, 100));
0742       h_bvlist3[i]->Sumw2();
0743       sprintf(name, "e_edepth%d", i);
0744       sprintf(title, "Total RecHit energy in depth %d (Endcap)", i + 1);
0745       h_evlist.push_back(new TH1F(name, title, 1000, 0, 100));
0746       h_evlist[i]->Sumw2();
0747       sprintf(name, "e_recedepth%d", i);
0748       sprintf(title, "RecHit energy in depth %d (Endcap)", i + 1);
0749       h_evlist2.push_back(new TH1F(name, title, 1000, 0, 100));
0750       h_evlist2[i]->Sumw2();
0751       sprintf(name, "e_nrecdepth%d", i);
0752       sprintf(title, "#RecHits in depth %d (Endcap)", i + 1);
0753       h_evlist3.push_back(new TH1F(name, title, 1000, 0, 100));
0754       h_evlist3[i]->Sumw2();
0755     }
0756     h_etaE = new TH2F("heta", "", 60, -30, 30, 100, 0, 100);
0757   }
0758 }
0759 
0760 Bool_t CalibPlotProperties::Notify() {
0761   // The Notify() function is called when a new file is opened. This
0762   // can be either for a new TTree in a TChain or when when a new TTree
0763   // is started when using PROOF. It is normally not necessary to make changes
0764   // to the generated code, but the routine can be extended by the
0765   // user if needed. The return value is currently not used.
0766 
0767   return kTRUE;
0768 }
0769 
0770 void CalibPlotProperties::Show(Long64_t entry) {
0771   // Print contents of entry.
0772   // If entry is not specified, print current entry
0773   if (!fChain)
0774     return;
0775   fChain->Show(entry);
0776 }
0777 
0778 Int_t CalibPlotProperties::Cut(Long64_t) {
0779   // This function may be called from Loop.
0780   // returns  1 if entry is accepted.
0781   // returns -1 otherwise.
0782   return 1;
0783 }
0784 
0785 void CalibPlotProperties::Loop(Long64_t nentries) {
0786   //   In a ROOT session, you can do:
0787   //      Root > .L CalibMonitor.C
0788   //      Root > CalibMonitor t
0789   //      Root > t.GetEntry(12); // Fill t data members with entry number 12
0790   //      Root > t.Show();       // Show values of entry 12
0791   //      Root > t.Show(16);     // Read and show values of entry 16
0792   //      Root > t.Loop();       // Loop on all entries
0793   //
0794 
0795   //   This is the loop skeleton where:
0796   //      jentry is the global entry number in the chain
0797   //      ientry is the entry number in the current Tree
0798   //   Note that the argument to GetEntry must be:
0799   //      jentry for TChain::GetEntry
0800   //      ientry for TTree::GetEntry and TBranch::GetEntry
0801   //
0802   //       To read only selected branches, Insert statements like:
0803   // METHOD1:
0804   //    fChain->SetBranchStatus("*",0);  // disable all branches
0805   //    fChain->SetBranchStatus("branchname",1);  // activate branchname
0806   // METHOD2: replace line
0807   //    fChain->GetEntry(jentry);       //read all branches
0808   //by  b_branchname->GetEntry(ientry); //read only this branch
0809   const bool debug(false);
0810 
0811   if (fChain == 0)
0812     return;
0813 
0814   // Find list of duplicate events
0815   if (nentries < 0)
0816     nentries = fChain->GetEntriesFast();
0817   std::cout << "Total entries " << nentries << ":" << fChain->GetEntriesFast() << std::endl;
0818   std::vector<std::pair<int, int> > runEvent;
0819   Long64_t nbytes(0), nb(0);
0820   unsigned int duplicate(0), good(0), kount(0);
0821   double sel(0), selHB(0), selHE(0);
0822   for (Long64_t jentry = 0; jentry < nentries; jentry++) {
0823     Long64_t ientry = LoadTree(jentry);
0824     if (ientry < 0)
0825       break;
0826     nb = fChain->GetEntry(jentry);
0827     nbytes += nb;
0828     if (jentry % 1000000 == 0)
0829       std::cout << "Entry " << jentry << " Run " << t_Run << " Event " << t_Event << std::endl;
0830     bool select = ((cDuplicate_ != nullptr) && (duplicate_ == 0)) ? (cDuplicate_->isDuplicate(jentry)) : true;
0831     if (!select) {
0832       ++duplicate;
0833       if (debug)
0834         std::cout << "Duplicate event " << t_Run << " " << t_Event << " " << t_p << std::endl;
0835       continue;
0836     }
0837     bool selRun = (includeRun_ ? ((t_Run >= runlo_) && (t_Run <= runhi_)) : ((t_Run < runlo_) || (t_Run > runhi_)));
0838     select =
0839         (selRun && (fabs(t_ieta) >= etalo_) && (fabs(t_ieta) <= etahi_) && (t_nVtx >= nvxlo_) && (t_nVtx <= nvxhi_));
0840     if (!select) {
0841       if (debug)
0842         std::cout << "Run # " << t_Run << " out of range of " << runlo_ << ":" << runhi_ << " or ieta " << t_ieta
0843                   << " (" << etalo_ << ":" << etahi_ << ") or nvtx " << t_nVtx << " (" << nvxlo_ << ":" << nvxhi_
0844                   << ") out of range" << std::endl;
0845       continue;
0846     }
0847     if (cSelect_ != nullptr) {
0848       if (exclude_) {
0849         if (cSelect_->isItRBX(t_DetIds))
0850           continue;
0851       } else {
0852         if (!(cSelect_->isItRBX(t_ieta, t_iphi)))
0853           continue;
0854       }
0855     }
0856     if (cDuplicate_ != nullptr) {
0857       if (cDuplicate_->select(t_ieta, t_iphi))
0858         continue;
0859     }
0860     select = (!cutL1T_ || (t_mindR1 >= 0.5));
0861     if (!select) {
0862       if (debug)
0863         std::cout << "Reject Run # " << t_Run << " Event # " << t_Event << " too close to L1 trigger " << t_mindR1
0864                   << std::endl;
0865       continue;
0866     }
0867     select = ((events_.size() == 0) ||
0868               (std::find(events_.begin(), events_.end(), std::pair<int, int>(t_Run, t_Event)) != events_.end()));
0869     if (!select) {
0870       if (debug)
0871         std::cout << "Reject Run # " << t_Run << " Event # " << t_Event << " not in the selection list" << std::endl;
0872       continue;
0873     }
0874 
0875     if (plotBasic_) {
0876       h_nvtx->Fill(t_nVtx);
0877       std::pair<int, int> runEv(t_Run, t_Event);
0878       if (std::find(runEvent.begin(), runEvent.end(), runEv) == runEvent.end()) {
0879         h_nvtxEv->Fill(t_nVtx);
0880         runEvent.push_back(runEv);
0881       }
0882     }
0883 
0884     // if (Cut(ientry) < 0) continue;
0885     int kp = CalibPlots::npbin0;
0886     double pmom = (useGen_ && (t_gentrackP > 0)) ? t_gentrackP : t_p;
0887     for (int k = 1; k < CalibPlots::npbin0; ++k) {
0888       if (pmom >= CalibPlots::getMomentum(k - 1) && pmom < CalibPlots::getMomentum(k)) {
0889         kp = k - 1;
0890         break;
0891       }
0892     }
0893     int jp1 = (((std::abs(t_ieta) >= etalo_) && (std::abs(t_ieta) <= etahi_)) ? (std::abs(t_ieta) - etalo_) : -1);
0894     int jp2 = (etahi_ - etalo_ + 1);
0895     int je1 = CalibPlots::netabin;
0896     for (int j = 1; j < CalibPlots::netabin; ++j) {
0897       if (std::abs(t_ieta) > CalibPlots::getEta(j - 1) && std::abs(t_ieta) <= CalibPlots::getEta(j)) {
0898         je1 = j;
0899         break;
0900       }
0901     }
0902     int je2 = (je1 != CalibPlots::netabin) ? 0 : -1;
0903     if (debug)
0904       std::cout << "Bin " << kp << ":" << je1 << ":" << je2 << ":" << jp1 << ":" << jp2 << std::endl;
0905     double cut = (pmom > 20) ? ((flexibleSelect_ == 0) ? 2.0 : 10.0) : 0.0;
0906     double rcut(-1000.0);
0907 
0908     // Selection of good track and energy measured in Hcal
0909     double eHcal(t_eHcal);
0910     if (corrFactor_->doCorr()) {
0911       eHcal = 0;
0912       for (unsigned int k = 0; k < t_HitEnergies->size(); ++k) {
0913         // Apply thresholds if necessary
0914         bool okcell = (thrForm_ == 0) || ((*t_HitEnergies)[k] > threshold((*t_DetIds)[k], thrForm_));
0915         if (okcell) {
0916           // The masks are defined in DataFormats/HcalDetId/interface/HcalDetId.h
0917           unsigned int id = truncateId((*t_DetIds)[k], truncateFlag_, false);
0918           double cfac = corrFactor_->getCorr(id);
0919           if ((cFactor_ != 0) && (ifDepth_ != 3) && (ifDepth_ > 0))
0920             cfac *= cFactor_->getCorr(t_Run, (*t_DetIds)[k]);
0921           if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr()))
0922             cfac *= cDuplicate_->getWeight((*t_DetIds)[k]);
0923           eHcal += (cfac * ((*t_HitEnergies)[k]));
0924           if (debug) {
0925             int subdet, zside, ieta, iphi, depth;
0926             unpackDetId(id, subdet, zside, ieta, iphi, depth);
0927             std::cout << zside << ":" << ieta << ":" << depth << " Corr " << cfac << " " << (*t_HitEnergies)[k]
0928                       << " Out " << eHcal << std::endl;
0929           }
0930         }
0931       }
0932     }
0933     bool goodTk = goodTrack(eHcal, cut, debug);
0934     bool selPhi = selectPhi(debug);
0935     double rat = (pmom > 0) ? (eHcal / (pmom - t_eMipDR)) : 1.0;
0936     if (debug)
0937       std::cout << "Entry " << jentry << " p|eHcal|ratio " << pmom << "|" << t_eHcal << "|" << eHcal << "|" << rat
0938                 << "|" << kp << " Cuts " << t_qltyFlag << "|" << t_selectTk << "|" << (t_hmaxNearP < cut) << "|"
0939                 << (t_eMipDR < 1.0) << "|" << goodTk << "|" << (rat > rcut) << " Select Phi " << selPhi << std::endl;
0940     if (plotBasic_) {
0941       h_nvtxTk->Fill(t_nVtx);
0942       h_p[0]->Fill(pmom, t_EventWeight);
0943       h_eta[0]->Fill(t_ieta, t_EventWeight);
0944       if (kp < CalibPlots::npbin0)
0945         h_eta0[kp]->Fill(t_ieta, t_EventWeight);
0946       if (t_qltyFlag) {
0947         h_p[1]->Fill(pmom, t_EventWeight);
0948         h_eta[1]->Fill(t_ieta, t_EventWeight);
0949         if (kp < CalibPlots::npbin0)
0950           h_eta1[kp]->Fill(t_ieta, t_EventWeight);
0951         if (t_selectTk) {
0952           h_p[2]->Fill(pmom, t_EventWeight);
0953           h_eta[2]->Fill(t_ieta, t_EventWeight);
0954           if (kp < CalibPlots::npbin0)
0955             h_eta2[kp]->Fill(t_ieta, t_EventWeight);
0956           if (t_hmaxNearP < cut) {
0957             h_p[3]->Fill(pmom, t_EventWeight);
0958             h_eta[3]->Fill(t_ieta, t_EventWeight);
0959             if (kp < CalibPlots::npbin0)
0960               h_eta3[kp]->Fill(t_ieta, t_EventWeight);
0961             if (t_eMipDR < 1.0) {
0962               h_p[4]->Fill(pmom, t_EventWeight);
0963               h_eta[4]->Fill(t_ieta, t_EventWeight);
0964               if (kp < CalibPlots::npbin0) {
0965                 h_eta4[kp]->Fill(t_ieta, t_EventWeight);
0966                 h_dL1[kp]->Fill(t_mindR1, t_EventWeight);
0967                 h_vtx[kp]->Fill(t_goodPV, t_EventWeight);
0968               }
0969             }
0970           }
0971         }
0972       }
0973     }
0974 
0975     if (goodTk && kp != CalibPlots::npbin0 && selPhi) {
0976       if (rat > rcut) {
0977         if (plotEnergy_) {
0978           if (jp1 >= 0) {
0979             h_etaEH[kp][jp1]->Fill(eHcal, t_EventWeight);
0980             h_etaEH[kp][jp2]->Fill(eHcal, t_EventWeight);
0981             h_etaEp[kp][jp1]->Fill(pmom, t_EventWeight);
0982             h_etaEp[kp][jp2]->Fill(pmom, t_EventWeight);
0983             h_etaEE[kp][jp1]->Fill(t_eMipDR, t_EventWeight);
0984             h_etaEE[kp][jp2]->Fill(t_eMipDR, t_EventWeight);
0985             h_etaEE0[kp][jp1]->Fill(t_eMipDR, t_EventWeight);
0986             h_etaEE0[kp][jp2]->Fill(t_eMipDR, t_EventWeight);
0987           }
0988           if (kp == kp50) {
0989             if (je1 != CalibPlots::netabin) {
0990               h_eHcal[je1]->Fill(eHcal, t_EventWeight);
0991               h_eHcal[je2]->Fill(eHcal, t_EventWeight);
0992               h_mom[je1]->Fill(pmom, t_EventWeight);
0993               h_mom[je2]->Fill(pmom, t_EventWeight);
0994               h_eEcal[je1]->Fill(t_eMipDR, t_EventWeight);
0995               h_eEcal[je2]->Fill(t_eMipDR, t_EventWeight);
0996             }
0997           }
0998         }
0999 
1000         if (plotHists_) {
1001           if ((std::fabs(rat - 1) < 0.15) && (kp == kp50) && ((std::abs(t_ieta) < 15) || (std::abs(t_ieta) > 17))) {
1002             float weight = (isRealData_ ? t_EventWeight : t_EventWeight * puweight(t_nVtx));
1003             h_etaE->Fill(t_ieta, eHcal, weight);
1004             sel += weight;
1005             std::vector<float> bv(7, 0.0f), ev(7, 0.0f);
1006             std::vector<int> bnrec(7, 0), enrec(7, 0);
1007             double eb(0), ee(0);
1008             for (unsigned int k = 0; k < t_HitEnergies->size(); ++k) {
1009               // Apply thresholds if necessary
1010               bool okcell = (thrForm_ == 0) || ((*t_HitEnergies)[k] > threshold((*t_DetIds)[k], thrForm_));
1011               if (okcell) {
1012                 unsigned int id = truncateId((*t_DetIds)[k], truncateFlag_, false);
1013                 double cfac = corrFactor_->getCorr(id);
1014                 if ((cFactor_ != 0) && (ifDepth_ != 3) && (ifDepth_ > 0))
1015                   cfac *= cFactor_->getCorr(t_Run, (*t_DetIds)[k]);
1016                 if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr()))
1017                   cfac *= cDuplicate_->getWeight((*t_DetIds)[k]);
1018                 double ener = cfac * (*t_HitEnergies)[k];
1019                 if (corrPU_)
1020                   correctEnergy(ener);
1021                 unsigned int idx = (unsigned int)((*t_DetIds)[k]);
1022                 int subdet, zside, ieta, iphi, depth;
1023                 unpackDetId(idx, subdet, zside, ieta, iphi, depth);
1024                 if (depth > 0 && depth <= CalibPlots::ndepth) {
1025                   if (subdet == 1) {
1026                     eb += ener;
1027                     bv[depth - 1] += ener;
1028                     h_bvlist2[depth - 1]->Fill(ener, weight);
1029                     ++bnrec[depth - 1];
1030                   } else if (subdet == 2) {
1031                     ee += ener;
1032                     ev[depth - 1] += ener;
1033                     h_evlist2[depth - 1]->Fill(ener, weight);
1034                     ++enrec[depth - 1];
1035                   }
1036                 }
1037               }
1038             }
1039             bool barrel = (eb > ee);
1040             if (barrel)
1041               selHB += weight;
1042             else
1043               selHE += weight;
1044             for (int i = 0; i < CalibPlots::ndepth; i++) {
1045               if (barrel) {
1046                 h_bvlist[i]->Fill(bv[i], weight);
1047                 h_bvlist3[i]->Fill((bnrec[i] + 0.001), weight);
1048               } else {
1049                 h_evlist[i]->Fill(ev[i], weight);
1050                 h_evlist3[i]->Fill((enrec[i] + 0.001), weight);
1051               }
1052             }
1053           }
1054         }
1055       }
1056       good++;
1057     }
1058     ++kount;
1059   }
1060   std::cout << "Finds " << duplicate << " Duplicate events out of " << kount << " events in this file and " << good
1061             << " selected events" << std::endl;
1062   if (plotHists_)
1063     std::cout << "Number of weighted selected events " << sel << " HB " << selHB << " HE " << selHE << std::endl;
1064 }
1065 
1066 bool CalibPlotProperties::goodTrack(double &eHcal, double &cuti, bool debug) {
1067   bool select(true);
1068   double cut(cuti);
1069   if (debug) {
1070     std::cout << "goodTrack input " << eHcal << ":" << cut;
1071   }
1072   if (flexibleSelect_ > 1) {
1073     double eta = (t_ieta > 0) ? t_ieta : -t_ieta;
1074     cut = 8.0 * exp(eta * log2by18_);
1075   }
1076   correctEnergy(eHcal);
1077   select = ((t_qltyFlag) && (t_selectTk) && (t_hmaxNearP < cut) && (t_eMipDR < 100.0));
1078   if (debug) {
1079     std::cout << " output " << eHcal << ":" << cut << ":" << select << std::endl;
1080   }
1081   return select;
1082 }
1083 
1084 bool CalibPlotProperties::selectPhi(bool debug) {
1085   bool select(true);
1086   if (phimin_ > 1 || phimax_ < 72) {
1087     double eTotal(0), eSelec(0);
1088     // The masks are defined in DataFormats/HcalDetId/interface/HcalDetId.h
1089     for (unsigned int k = 0; k < t_HitEnergies->size(); ++k) {
1090       // Apply thresholds if necessary
1091       bool okcell = (thrForm_ == 0) || ((*t_HitEnergies)[k] > threshold((*t_DetIds)[k], thrForm_));
1092       if (okcell) {
1093         int iphi = ((*t_DetIds)[k]) & (0x3FF);
1094         int zside = ((*t_DetIds)[k] & 0x80000) ? (1) : (-1);
1095         eTotal += ((*t_HitEnergies)[k]);
1096         if (iphi >= phimin_ && iphi <= phimax_ && zside == zside_)
1097           eSelec += ((*t_HitEnergies)[k]);
1098       }
1099     }
1100     if (eSelec < 0.9 * eTotal)
1101       select = false;
1102     if (debug) {
1103       std::cout << "Etotal " << eTotal << " and ESelec " << eSelec << " (phi " << phimin_ << ":" << phimax_ << " z "
1104                 << zside_ << ") Selection " << select << std::endl;
1105     }
1106   }
1107   return select;
1108 }
1109 
1110 void CalibPlotProperties::savePlot(const std::string &theName, bool append, bool all, bool debug) {
1111   TFile *theFile(0);
1112   if (append) {
1113     theFile = new TFile(theName.c_str(), "UPDATE");
1114   } else {
1115     theFile = new TFile(theName.c_str(), "RECREATE");
1116   }
1117 
1118   theFile->cd();
1119 
1120   if (plotBasic_) {
1121     if (debug)
1122       std::cout << "nvtx " << h_nvtx << ":" << h_nvtxEv << ":" << h_nvtxTk << std::endl;
1123     h_nvtx->Write();
1124     h_nvtxEv->Write();
1125     h_nvtxTk->Write();
1126     for (int k = 0; k < CalibPlots::ntitles; ++k) {
1127       if (debug)
1128         std::cout << "[" << k << "] p " << h_p[k] << " eta " << h_eta[k] << std::endl;
1129       h_p[k]->Write();
1130       h_eta[k]->Write();
1131     }
1132     for (int k = 0; k < CalibPlots::npbin; ++k) {
1133       if (debug)
1134         std::cout << "[" << k << "] eta " << h_eta0[k] << ":" << h_eta1[k] << ":" << h_eta2[k] << ":" << h_eta3[k]
1135                   << ":" << h_eta4[k] << " dl " << h_dL1[k] << " vtx " << h_vtx[k] << std::endl;
1136       if (h_eta0[k] != 0) {
1137         TH1D *h1 = (TH1D *)h_eta0[k]->Clone();
1138         h1->Write();
1139       }
1140       if (h_eta1[k] != 0) {
1141         TH1D *h2 = (TH1D *)h_eta1[k]->Clone();
1142         h2->Write();
1143       }
1144       if (h_eta2[k] != 0) {
1145         TH1D *h3 = (TH1D *)h_eta2[k]->Clone();
1146         h3->Write();
1147       }
1148       if (h_eta3[k] != 0) {
1149         TH1D *h4 = (TH1D *)h_eta3[k]->Clone();
1150         h4->Write();
1151       }
1152       if (h_eta4[k] != 0) {
1153         TH1D *h5 = (TH1D *)h_eta4[k]->Clone();
1154         h5->Write();
1155       }
1156       if (h_dL1[k] != 0) {
1157         TH1D *h6 = (TH1D *)h_dL1[k]->Clone();
1158         h6->Write();
1159       }
1160       if (h_vtx[k] != 0) {
1161         TH1D *h7 = (TH1D *)h_vtx[k]->Clone();
1162         h7->Write();
1163       }
1164     }
1165   }
1166 
1167   if (plotEnergy_) {
1168     for (int k = 0; k < CalibPlots::npbin; ++k) {
1169       if (debug)
1170         std::cout << "Energy[" << k << "] "
1171                   << " eta " << etalo_ << ":" << etahi_ << ":" << CalibPlots::netabin << " etaEH " << h_etaEH[k].size()
1172                   << " etaEp " << h_etaEp[k].size() << " etaEE " << h_etaEE[k].size() << " eHcal " << h_eHcal.size()
1173                   << " mom " << h_mom.size() << " eEcal " << h_eEcal.size() << std::endl;
1174       for (unsigned int j = 0; j <= (unsigned int)(etahi_ - etalo_); ++j) {
1175         if (h_etaEH[k].size() > j && h_etaEH[k][j] != nullptr && (all || (k == kp50))) {
1176           TH1D *hist = (TH1D *)h_etaEH[k][j]->Clone();
1177           hist->Write();
1178         }
1179         if (h_etaEp[k].size() > j && h_etaEp[k][j] != nullptr && (all || (k == kp50))) {
1180           TH1D *hist = (TH1D *)h_etaEp[k][j]->Clone();
1181           hist->Write();
1182         }
1183         if (h_etaEE[k].size() > j && h_etaEE[k][j] != nullptr && (all || (k == kp50))) {
1184           TH1D *hist = (TH1D *)h_etaEE[k][j]->Clone();
1185           hist->Write();
1186         }
1187         if (h_etaEE0[k].size() > j && h_etaEE0[k][j] != nullptr && (all || (k == kp50))) {
1188           TH1D *hist = (TH1D *)h_etaEE0[k][j]->Clone();
1189           hist->Write();
1190         }
1191       }
1192     }
1193 
1194     for (int j = 0; j < CalibPlots::netabin; ++j) {
1195       if (h_eHcal.size() > (unsigned int)(j) && (h_eHcal[j] != nullptr)) {
1196         TH1D *hist = (TH1D *)h_eHcal[j]->Clone();
1197         hist->Write();
1198       }
1199       if (h_mom.size() > (unsigned int)(j) && (h_mom[j] != nullptr)) {
1200         TH1D *hist = (TH1D *)h_mom[j]->Clone();
1201         hist->Write();
1202       }
1203       if (h_eEcal.size() > (unsigned int)(j) && (h_eEcal[j] != nullptr)) {
1204         TH1D *hist = (TH1D *)h_eEcal[j]->Clone();
1205         hist->Write();
1206       }
1207     }
1208   }
1209 
1210   if (plotHists_) {
1211     if (debug)
1212       std::cout << "etaE " << h_etaE << std::endl;
1213     h_etaE->Write();
1214     for (int i = 0; i < CalibPlots::ndepth; ++i) {
1215       if (debug)
1216         std::cout << "Depth[" << i << "] bv " << h_bvlist[i] << ":" << h_bvlist2[i] << ":" << h_bvlist3[i] << " ev "
1217                   << h_evlist[i] << ":" << h_evlist2[i] << ":" << h_evlist3[i] << std::endl;
1218       h_bvlist[i]->Write();
1219       h_bvlist2[i]->Write();
1220       h_bvlist3[i]->Write();
1221       h_evlist[i]->Write();
1222       h_evlist2[i]->Write();
1223       h_evlist3[i]->Write();
1224     }
1225   }
1226   std::cout << "All done" << std::endl;
1227   theFile->Close();
1228 }
1229 
1230 void CalibPlotProperties::correctEnergy(double &eHcal) {
1231   double pmom = (useGen_ && (t_gentrackP > 0)) ? t_gentrackP : t_p;
1232   if ((corrPU_ < 0) && (pmom > 0)) {
1233     double ediff = (t_eHcal30 - t_eHcal10);
1234     if (t_DetIds1 != 0 && t_DetIds3 != 0) {
1235       double Etot1(0), Etot3(0);
1236       // The masks are defined in DataFormats/HcalDetId/interface/HcalDetId.h
1237       for (unsigned int idet = 0; idet < (*t_DetIds1).size(); idet++) {
1238         // Apply thresholds if necessary
1239         bool okcell = (thrForm_ == 0) || ((*t_HitEnergies1)[idet] > threshold((*t_DetIds1)[idet], thrForm_));
1240         if (okcell) {
1241           unsigned int id = truncateId((*t_DetIds1)[idet], truncateFlag_, false);
1242           double cfac = corrFactor_->getCorr(id);
1243           if ((cFactor_ != 0) && (ifDepth_ != 3) && (ifDepth_ > 0))
1244             cfac *= cFactor_->getCorr(t_Run, (*t_DetIds1)[idet]);
1245           if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr()))
1246             cfac *= cDuplicate_->getWeight((*t_DetIds1)[idet]);
1247           double hitEn = cfac * (*t_HitEnergies1)[idet];
1248           Etot1 += hitEn;
1249         }
1250       }
1251       for (unsigned int idet = 0; idet < (*t_DetIds3).size(); idet++) {
1252         // Apply thresholds if necessary
1253         bool okcell = (thrForm_ == 0) || ((*t_HitEnergies3)[idet] > threshold((*t_DetIds3)[idet], thrForm_));
1254         if (okcell) {
1255           unsigned int id = truncateId((*t_DetIds3)[idet], truncateFlag_, false);
1256           double cfac = corrFactor_->getCorr(id);
1257           if ((cFactor_ != 0) && (ifDepth_ != 3) && (ifDepth_ > 0))
1258             cfac *= cFactor_->getCorr(t_Run, (*t_DetIds3)[idet]);
1259           if ((cDuplicate_ != nullptr) && (cDuplicate_->doCorr()))
1260             cfac *= cDuplicate_->getWeight((*t_DetIds)[idet]);
1261           double hitEn = cfac * (*t_HitEnergies3)[idet];
1262           Etot3 += hitEn;
1263         }
1264       }
1265       ediff = (Etot3 - Etot1);
1266     }
1267     double fac = puFactor(-corrPU_, t_ieta, pmom, eHcal, ediff);
1268     eHcal *= fac;
1269   } else if (corrPU_ > 1) {
1270     eHcal = puFactorRho(corrPU_, t_ieta, t_rhoh, eHcal);
1271   }
1272 }
1273 
1274 void PlotThisHist(TH1D *hist, const std::string &text, bool isRealData, int save) {
1275   char namep[120];
1276   sprintf(namep, "c_%s", hist->GetName());
1277   TCanvas *pad = new TCanvas(namep, namep, 700, 500);
1278   pad->SetRightMargin(0.10);
1279   pad->SetTopMargin(0.10);
1280   hist->GetXaxis()->SetTitleSize(0.04);
1281   hist->GetYaxis()->SetTitle("Tracks");
1282   hist->GetYaxis()->SetLabelOffset(0.005);
1283   hist->GetYaxis()->SetTitleSize(0.04);
1284   hist->GetYaxis()->SetLabelSize(0.035);
1285   hist->GetYaxis()->SetTitleOffset(1.10);
1286   hist->SetMarkerStyle(20);
1287   hist->SetMarkerColor(2);
1288   hist->SetLineColor(2);
1289   hist->Draw("Hist");
1290   pad->Modified();
1291   pad->Update();
1292   TPaveStats *st1 = (TPaveStats *)hist->GetListOfFunctions()->FindObject("stats");
1293   TPaveText *txt0 = new TPaveText(0.12, 0.91, 0.49, 0.96, "blNDC");
1294   txt0->SetFillColor(0);
1295   char txt[100];
1296   if (isRealData)
1297     sprintf(txt, "CMS Preliminary");
1298   else
1299     sprintf(txt, "CMS Simulation Preliminary");
1300   txt0->AddText(txt);
1301   txt0->Draw("same");
1302   TPaveText *txt1 = new TPaveText(0.51, 0.91, 0.90, 0.96, "blNDC");
1303   txt1->SetFillColor(0);
1304   sprintf(txt, "%s", text.c_str());
1305   txt1->AddText(txt);
1306   txt1->Draw("same");
1307   if (st1 != nullptr) {
1308     st1->SetY1NDC(0.70);
1309     st1->SetY2NDC(0.90);
1310     st1->SetX1NDC(0.65);
1311     st1->SetX2NDC(0.90);
1312   }
1313   pad->Update();
1314   if (save != 0) {
1315     if (save > 0)
1316       sprintf(namep, "%s.pdf", pad->GetName());
1317     else
1318       sprintf(namep, "%s.jpg", pad->GetName());
1319     pad->Print(namep);
1320   }
1321 }
1322 
1323 void PlotHist(const char *hisFileName,
1324               const std::string &prefix = "",
1325               const std::string &text = "",
1326               int flagC = 111,
1327               int etalo = 0,
1328               int etahi = 30,
1329               int save = 0) {
1330   gStyle->SetCanvasBorderMode(0);
1331   gStyle->SetCanvasColor(kWhite);
1332   gStyle->SetPadColor(kWhite);
1333   gStyle->SetFillColor(kWhite);
1334   gStyle->SetOptTitle(0);
1335   gStyle->SetOptStat(1110);
1336 
1337   bool isRealData = false;
1338   bool plotBasic = (((flagC / 1) % 10) > 0);
1339   bool plotEnergy = (((flagC / 10) % 10) > 0);
1340   bool plotHists = (((flagC / 100) % 10) > 0);
1341 
1342   TFile *file = new TFile(hisFileName);
1343   char name[100], title[200];
1344   TH1D *hist;
1345   if ((file != nullptr) && plotBasic) {
1346     std::cout << "Plot Basic Histos" << std::endl;
1347     hist = (TH1D *)(file->FindObjectAny("hnvtx"));
1348     if (hist != nullptr) {
1349       hist->GetXaxis()->SetTitle("Number of vertices (selected entries)");
1350       PlotThisHist(hist, text, isRealData, save);
1351     }
1352     hist = (TH1D *)(file->FindObjectAny("hnvtxEv"));
1353     if (hist != nullptr) {
1354       hist->GetXaxis()->SetTitle("Number of vertices (selected events)");
1355       PlotThisHist(hist, text, isRealData, save);
1356     }
1357     hist = (TH1D *)(file->FindObjectAny("hnvtxTk"));
1358     if (hist != nullptr) {
1359       hist->GetXaxis()->SetTitle("Number of vertices (selected tracks)");
1360       PlotThisHist(hist, text, isRealData, save);
1361     }
1362     for (int k = 0; k < CalibPlots::ntitles; ++k) {
1363       sprintf(name, "%sp%d", prefix.c_str(), k);
1364       hist = (TH1D *)(file->FindObjectAny(name));
1365       if (hist != nullptr) {
1366         sprintf(title, "Momentum for %s (GeV)", CalibPlots::getTitle(k).c_str());
1367         hist->GetXaxis()->SetTitle(title);
1368         PlotThisHist(hist, text, isRealData, save);
1369       }
1370       sprintf(name, "%seta%d", prefix.c_str(), k);
1371       hist = (TH1D *)(file->FindObjectAny(name));
1372       if (hist != nullptr) {
1373         sprintf(title, "#eta for %s", CalibPlots::getTitle(k).c_str());
1374         hist->GetXaxis()->SetTitle(title);
1375         PlotThisHist(hist, text, isRealData, save);
1376       }
1377     }
1378     for (int k = 0; k < CalibPlots::npbin; ++k) {
1379       sprintf(name, "%seta0%d", prefix.c_str(), k);
1380       hist = (TH1D *)(file->FindObjectAny(name));
1381       if (hist != nullptr) {
1382         sprintf(title,
1383                 "#eta for %s (p = %d:%d GeV)",
1384                 CalibPlots::getTitle(0).c_str(),
1385                 CalibPlots::getP(k),
1386                 CalibPlots::getP(k + 1));
1387         hist->GetXaxis()->SetTitle(title);
1388         PlotThisHist(hist, text, isRealData, save);
1389       }
1390       sprintf(name, "%seta1%d", prefix.c_str(), k);
1391       hist = (TH1D *)(file->FindObjectAny(name));
1392       if (hist != nullptr) {
1393         sprintf(title,
1394                 "#eta for %s (p = %d:%d GeV)",
1395                 CalibPlots::getTitle(1).c_str(),
1396                 CalibPlots::getP(k),
1397                 CalibPlots::getP(k + 1));
1398         hist->GetXaxis()->SetTitle(title);
1399         PlotThisHist(hist, text, isRealData, save);
1400       }
1401       sprintf(name, "%seta2%d", prefix.c_str(), k);
1402       hist = (TH1D *)(file->FindObjectAny(name));
1403       if (hist != nullptr) {
1404         sprintf(title,
1405                 "#eta for %s (p = %d:%d GeV)",
1406                 CalibPlots::getTitle(2).c_str(),
1407                 CalibPlots::getP(k),
1408                 CalibPlots::getP(k + 1));
1409         hist->GetXaxis()->SetTitle(title);
1410         PlotThisHist(hist, text, isRealData, save);
1411       }
1412       sprintf(name, "%seta3%d", prefix.c_str(), k);
1413       hist = (TH1D *)(file->FindObjectAny(name));
1414       if (hist != nullptr) {
1415         sprintf(title,
1416                 "#eta for %s (p = %d:%d GeV)",
1417                 CalibPlots::getTitle(3).c_str(),
1418                 CalibPlots::getP(k),
1419                 CalibPlots::getP(k + 1));
1420         hist->GetXaxis()->SetTitle(title);
1421         PlotThisHist(hist, text, isRealData, save);
1422       }
1423       sprintf(name, "%seta4%d", prefix.c_str(), k);
1424       hist = (TH1D *)(file->FindObjectAny(name));
1425       if (hist != nullptr) {
1426         sprintf(title,
1427                 "#eta for %s (p = %d:%d GeV)",
1428                 CalibPlots::getTitle(4).c_str(),
1429                 CalibPlots::getP(k),
1430                 CalibPlots::getP(k + 1));
1431         hist->GetXaxis()->SetTitle(title);
1432         PlotThisHist(hist, text, isRealData, save);
1433       }
1434       sprintf(name, "%sdl1%d", prefix.c_str(), k);
1435       hist = (TH1D *)(file->FindObjectAny(name));
1436       if (hist != nullptr) {
1437         sprintf(title, "Distance from L1 (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
1438         hist->GetXaxis()->SetTitle(title);
1439         PlotThisHist(hist, text, isRealData, save);
1440       }
1441       sprintf(name, "%svtx%d", prefix.c_str(), k);
1442       hist = (TH1D *)(file->FindObjectAny(name));
1443       if (hist != nullptr) {
1444         sprintf(title, "N_{Vertex} (p = %d:%d GeV)", CalibPlots::getP(k), CalibPlots::getP(k + 1));
1445         hist->GetXaxis()->SetTitle(title);
1446         PlotThisHist(hist, text, isRealData, save);
1447       }
1448     }
1449   }
1450 
1451   if ((file != nullptr) && plotEnergy) {
1452     std::cout << "Make plots for good tracks" << std::endl;
1453     for (int k = 0; k < CalibPlots::npbin; ++k) {
1454       for (int j = etalo; j <= etahi + 1; ++j) {
1455         sprintf(name, "%senergyH%d%d", prefix.c_str(), k, j);
1456         hist = (TH1D *)(file->FindObjectAny(name));
1457         if (hist != nullptr) {
1458           if (j > etahi)
1459             sprintf(title,
1460                     "HCAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
1461                     CalibPlots::getTitle(3).c_str(),
1462                     CalibPlots::getP(k),
1463                     CalibPlots::getP(k + 1),
1464                     etalo,
1465                     etahi);
1466           else
1467             sprintf(title,
1468                     "HCAL energy for %s (p = %d:%d GeV |#eta| = %d)",
1469                     CalibPlots::getTitle(3).c_str(),
1470                     CalibPlots::getP(k),
1471                     CalibPlots::getP(k + 1),
1472                     j);
1473           hist->GetXaxis()->SetTitle(title);
1474           PlotThisHist(hist, text, isRealData, save);
1475         }
1476         sprintf(name, "%senergyP%d%d", prefix.c_str(), k, j);
1477         hist = (TH1D *)(file->FindObjectAny(name));
1478         if (hist != nullptr) {
1479           if (j > etahi)
1480             sprintf(title,
1481                     "momentum for %s (p = %d:%d GeV |#eta| = %d:%d)",
1482                     CalibPlots::getTitle(3).c_str(),
1483                     CalibPlots::getP(k),
1484                     CalibPlots::getP(k + 1),
1485                     etalo,
1486                     etahi);
1487           else
1488             sprintf(title,
1489                     "momentum for %s (p = %d:%d GeV |#eta| = %d)",
1490                     CalibPlots::getTitle(3).c_str(),
1491                     CalibPlots::getP(k),
1492                     CalibPlots::getP(k + 1),
1493                     j);
1494           hist->GetXaxis()->SetTitle(title);
1495           PlotThisHist(hist, text, isRealData, save);
1496         }
1497         sprintf(name, "%senergyE%d%d", prefix.c_str(), k, j);
1498         hist = (TH1D *)(file->FindObjectAny(name));
1499         if (hist != nullptr) {
1500           if (j > etahi)
1501             sprintf(title,
1502                     "ECAL energy for %s (p = %d:%d GeV |#eta| = %d:%d)",
1503                     CalibPlots::getTitle(3).c_str(),
1504                     CalibPlots::getP(k),
1505                     CalibPlots::getP(k + 1),
1506                     etalo,
1507                     etahi);
1508           else
1509             sprintf(title,
1510                     "ECAL energy for %s (p = %d:%d GeV |#eta| = %d)",
1511                     CalibPlots::getTitle(3).c_str(),
1512                     CalibPlots::getP(k),
1513                     CalibPlots::getP(k + 1),
1514                     j);
1515           hist->GetXaxis()->SetTitle(title);
1516           PlotThisHist(hist, text, isRealData, save);
1517         }
1518         sprintf(name, "%senergyER%d%d", prefix.c_str(), k, j);
1519         hist = (TH1D *)(file->FindObjectAny(name));
1520         if (hist != nullptr) {
1521           std::cout << name << " Mean " << hist->GetMean() << " +- " << hist->GetMeanError() << " Entries "
1522                     << hist->GetEntries() << " RMS " << hist->GetRMS() << std::endl;
1523         }
1524       }
1525     }
1526 
1527     for (int j = 0; j < CalibPlots::netabin; ++j) {
1528       sprintf(name, "%senergyH%d", prefix.c_str(), j);
1529       hist = (TH1D *)(file->FindObjectAny(name));
1530       if (hist != nullptr) {
1531         if (j == 0)
1532           sprintf(title, "HCAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
1533         else
1534           sprintf(title,
1535                   "HCAL energy for %s (|#eta| = %d:%d)",
1536                   CalibPlots::getTitle(3).c_str(),
1537                   CalibPlots::getEta(j - 1),
1538                   CalibPlots::getEta(j));
1539         hist->GetXaxis()->SetTitle(title);
1540         PlotThisHist(hist, text, isRealData, save);
1541       }
1542       sprintf(name, "%senergyP%d", prefix.c_str(), j);
1543       hist = (TH1D *)(file->FindObjectAny(name));
1544       if (hist != nullptr) {
1545         if (j == 0)
1546           sprintf(title, "Track momentum for %s (All)", CalibPlots::getTitle(3).c_str());
1547         else
1548           sprintf(title,
1549                   "Track momentum for %s (|#eta| = %d:%d)",
1550                   CalibPlots::getTitle(3).c_str(),
1551                   CalibPlots::getEta(j - 1),
1552                   CalibPlots::getEta(j));
1553         hist->GetXaxis()->SetTitle(title);
1554         PlotThisHist(hist, text, isRealData, save);
1555       }
1556       sprintf(name, "%senergyE%d", prefix.c_str(), j);
1557       hist = (TH1D *)(file->FindObjectAny(name));
1558       if (hist != nullptr) {
1559         if (j == 0)
1560           sprintf(title, "ECAL energy for %s (All)", CalibPlots::getTitle(3).c_str());
1561         else
1562           sprintf(title,
1563                   "ECAL energy for %s (|#eta| = %d:%d)",
1564                   CalibPlots::getTitle(3).c_str(),
1565                   CalibPlots::getEta(j - 1),
1566                   CalibPlots::getEta(j));
1567         hist->GetXaxis()->SetTitle(title);
1568         PlotThisHist(hist, text, isRealData, save);
1569       }
1570     }
1571   }
1572 
1573   if (plotHists) {
1574     for (int i = 0; i < CalibPlots::ndepth; i++) {
1575       sprintf(name, "b_edepth%d", i);
1576       hist = (TH1D *)(file->FindObjectAny(name));
1577       if (hist != nullptr) {
1578         sprintf(title, "Total RecHit energy in depth %d (Barrel)", i + 1);
1579         hist->GetXaxis()->SetTitle(title);
1580         PlotThisHist(hist, text, isRealData, save);
1581       }
1582       sprintf(name, "b_recedepth%d", i);
1583       hist = (TH1D *)(file->FindObjectAny(name));
1584       if (hist != nullptr) {
1585         sprintf(title, "RecHit energy in depth %d (Barrel)", i + 1);
1586         hist->GetXaxis()->SetTitle(title);
1587         PlotThisHist(hist, text, isRealData, save);
1588       }
1589       sprintf(name, "b_nrecdepth%d", i);
1590       hist = (TH1D *)(file->FindObjectAny(name));
1591       if (hist != nullptr) {
1592         sprintf(title, "#RecHits in depth %d (Barrel)", i + 1);
1593         hist->GetXaxis()->SetTitle(title);
1594         PlotThisHist(hist, text, isRealData, save);
1595       }
1596       sprintf(name, "e_edepth%d", i);
1597       hist = (TH1D *)(file->FindObjectAny(name));
1598       if (hist != nullptr) {
1599         sprintf(title, "Total RecHit energy in depth %d (Endcap)", i + 1);
1600         hist->GetXaxis()->SetTitle(title);
1601         PlotThisHist(hist, text, isRealData, save);
1602       }
1603       sprintf(name, "e_recedepth%d", i);
1604       hist = (TH1D *)(file->FindObjectAny(name));
1605       if (hist != nullptr) {
1606         sprintf(title, "RecHit energy in depth %d (Endcap)", i + 1);
1607         hist->GetXaxis()->SetTitle(title);
1608         PlotThisHist(hist, text, isRealData, save);
1609       }
1610       sprintf(name, "e_nrecdepth%d", i);
1611       hist = (TH1D *)(file->FindObjectAny(name));
1612       if (hist != nullptr) {
1613         sprintf(title, "#RecHits in depth %d (Endcap)", i + 1);
1614         hist->GetXaxis()->SetTitle(title);
1615         PlotThisHist(hist, text, isRealData, save);
1616       }
1617     }
1618     TH2F *h_etaE = (TH2F *)(file->FindObjectAny("heta"));
1619     if (h_etaE != nullptr) {
1620       h_etaE->GetXaxis()->SetTitle("i#eta");
1621       h_etaE->GetYaxis()->SetTitle("Energy (GeV)");
1622       char namep[120];
1623       sprintf(namep, "c_%s", h_etaE->GetName());
1624       TCanvas *pad = new TCanvas(namep, namep, 700, 700);
1625       pad->SetRightMargin(0.10);
1626       pad->SetTopMargin(0.10);
1627       h_etaE->GetXaxis()->SetTitleSize(0.04);
1628       h_etaE->GetYaxis()->SetLabelOffset(0.005);
1629       h_etaE->GetYaxis()->SetTitleSize(0.04);
1630       h_etaE->GetYaxis()->SetLabelSize(0.035);
1631       h_etaE->GetYaxis()->SetTitleOffset(1.10);
1632       h_etaE->SetMarkerStyle(20);
1633       h_etaE->SetMarkerColor(2);
1634       h_etaE->SetLineColor(2);
1635       h_etaE->Draw();
1636       pad->Update();
1637       TPaveStats *st1 = (TPaveStats *)h_etaE->GetListOfFunctions()->FindObject("stats");
1638       if (st1 != nullptr) {
1639         st1->SetY1NDC(0.70);
1640         st1->SetY2NDC(0.90);
1641         st1->SetX1NDC(0.65);
1642         st1->SetX2NDC(0.90);
1643       }
1644       if (save != 0) {
1645         if (save > 0)
1646           sprintf(namep, "%s.pdf", pad->GetName());
1647         else
1648           sprintf(namep, "%s.jpg", pad->GetName());
1649         pad->Print(namep);
1650       }
1651     }
1652   }
1653 }
1654 
1655 void PlotPHist(const char *hisFileName,
1656                const std::string &prefix = "",
1657                const std::string &text = "",
1658                int pLow = 1,
1659                int pHigh = 5,
1660                bool isRealData = true,
1661                int save = 0) {
1662   gStyle->SetCanvasBorderMode(0);
1663   gStyle->SetCanvasColor(kWhite);
1664   gStyle->SetPadColor(kWhite);
1665   gStyle->SetFillColor(kWhite);
1666   gStyle->SetOptTitle(0);
1667   gStyle->SetOptStat(1110);
1668 
1669   TFile *file = new TFile(hisFileName);
1670   char name[100];
1671   TH1D *hist;
1672   if (file != nullptr) {
1673     for (int ip = pLow; ip <= pHigh; ++ip) {
1674       sprintf(name, "%sp%d", prefix.c_str(), ip);
1675       hist = (TH1D *)(file->FindObjectAny(name));
1676       if (hist != nullptr) {
1677         hist->GetXaxis()->SetTitle(hist->GetTitle());
1678         hist->GetYaxis()->SetTitle("Tracks");
1679         PlotThisHist(hist, text, isRealData, save);
1680       }
1681     }
1682   }
1683 }
1684 
1685 class CalibSplit {
1686 public:
1687   TChain *fChain;  //!pointer to the analyzed TTree or TChain
1688   Int_t fCurrent;  //!current Tree number in a TChain
1689 
1690   // Declaration of leaf types
1691   Int_t t_Run;
1692   Int_t t_Event;
1693   Int_t t_DataType;
1694   Int_t t_ieta;
1695   Int_t t_iphi;
1696   Double_t t_EventWeight;
1697   Int_t t_nVtx;
1698   Int_t t_nTrk;
1699   Int_t t_goodPV;
1700   Double_t t_l1pt;
1701   Double_t t_l1eta;
1702   Double_t t_l1phi;
1703   Double_t t_l3pt;
1704   Double_t t_l3eta;
1705   Double_t t_l3phi;
1706   Double_t t_p;
1707   Double_t t_pt;
1708   Double_t t_phi;
1709   Double_t t_mindR1;
1710   Double_t t_mindR2;
1711   Double_t t_eMipDR;
1712   Double_t t_eHcal;
1713   Double_t t_eHcal10;
1714   Double_t t_eHcal30;
1715   Double_t t_hmaxNearP;
1716   Double_t t_rhoh;
1717   Bool_t t_selectTk;
1718   Bool_t t_qltyFlag;
1719   Bool_t t_qltyMissFlag;
1720   Bool_t t_qltyPVFlag;
1721   Double_t t_gentrackP;
1722   std::vector<unsigned int> *t_DetIds;
1723   std::vector<double> *t_HitEnergies;
1724   std::vector<bool> *t_trgbits;
1725   std::vector<unsigned int> *t_DetIds1;
1726   std::vector<unsigned int> *t_DetIds3;
1727   std::vector<double> *t_HitEnergies1;
1728   std::vector<double> *t_HitEnergies3;
1729 
1730   // List of branches
1731   TBranch *b_t_Run;           //!
1732   TBranch *b_t_Event;         //!
1733   TBranch *b_t_DataType;      //!
1734   TBranch *b_t_ieta;          //!
1735   TBranch *b_t_iphi;          //!
1736   TBranch *b_t_EventWeight;   //!
1737   TBranch *b_t_nVtx;          //!
1738   TBranch *b_t_nTrk;          //!
1739   TBranch *b_t_goodPV;        //!
1740   TBranch *b_t_l1pt;          //!
1741   TBranch *b_t_l1eta;         //!
1742   TBranch *b_t_l1phi;         //!
1743   TBranch *b_t_l3pt;          //!
1744   TBranch *b_t_l3eta;         //!
1745   TBranch *b_t_l3phi;         //!
1746   TBranch *b_t_p;             //!
1747   TBranch *b_t_pt;            //!
1748   TBranch *b_t_phi;           //!
1749   TBranch *b_t_mindR1;        //!
1750   TBranch *b_t_mindR2;        //!
1751   TBranch *b_t_eMipDR;        //!
1752   TBranch *b_t_eHcal;         //!
1753   TBranch *b_t_eHcal10;       //!
1754   TBranch *b_t_eHcal30;       //!
1755   TBranch *b_t_hmaxNearP;     //!
1756   TBranch *b_t_rhoh;          //!
1757   TBranch *b_t_selectTk;      //!
1758   TBranch *b_t_qltyFlag;      //!
1759   TBranch *b_t_qltyMissFlag;  //!
1760   TBranch *b_t_qltyPVFlag;    //!
1761   TBranch *b_t_gentrackP;     //!
1762   TBranch *b_t_DetIds;        //!
1763   TBranch *b_t_HitEnergies;   //!
1764   TBranch *b_t_trgbits;       //!
1765   TBranch *b_t_DetIds1;       //!
1766   TBranch *b_t_DetIds3;       //!
1767   TBranch *b_t_HitEnergies1;  //!
1768   TBranch *b_t_HitEnergies3;  //!
1769 
1770   // Declaration of output leaf types
1771   Int_t tout_Run;
1772   Int_t tout_Event;
1773   Int_t tout_DataType;
1774   Int_t tout_ieta;
1775   Int_t tout_iphi;
1776   Double_t tout_EventWeight;
1777   Int_t tout_nVtx;
1778   Int_t tout_nTrk;
1779   Int_t tout_goodPV;
1780   Double_t tout_l1pt;
1781   Double_t tout_l1eta;
1782   Double_t tout_l1phi;
1783   Double_t tout_l3pt;
1784   Double_t tout_l3eta;
1785   Double_t tout_l3phi;
1786   Double_t tout_p;
1787   Double_t tout_pt;
1788   Double_t tout_phi;
1789   Double_t tout_mindR1;
1790   Double_t tout_mindR2;
1791   Double_t tout_eMipDR;
1792   Double_t tout_eHcal;
1793   Double_t tout_eHcal10;
1794   Double_t tout_eHcal30;
1795   Double_t tout_hmaxNearP;
1796   Double_t tout_rhoh;
1797   Bool_t tout_selectTk;
1798   Bool_t tout_qltyFlag;
1799   Bool_t tout_qltyMissFlag;
1800   Bool_t tout_qltyPVFlag;
1801   Double_t tout_gentrackP;
1802   std::vector<unsigned int> *tout_DetIds;
1803   std::vector<double> *tout_HitEnergies;
1804   std::vector<bool> *tout_trgbits;
1805   std::vector<unsigned int> *tout_DetIds1;
1806   std::vector<unsigned int> *tout_DetIds3;
1807   std::vector<double> *tout_HitEnergies1;
1808   std::vector<double> *tout_HitEnergies3;
1809 
1810   CalibSplit(const char *fname,
1811              const std::string &dirname,
1812              const std::string &outFileName,
1813              double pmin = 40.0,
1814              double pmax = 60.0,
1815              bool debug = false);
1816   virtual ~CalibSplit();
1817   virtual Int_t Cut(Long64_t entry);
1818   virtual Int_t GetEntry(Long64_t entry);
1819   virtual Long64_t LoadTree(Long64_t entry);
1820   virtual void Init(TChain *);
1821   virtual void Loop(Long64_t nentries = -1);
1822   virtual Bool_t Notify();
1823   virtual void Show(Long64_t entry = -1);
1824   void copyTree();
1825   void close();
1826 
1827 private:
1828   const std::string fname_, dirnm_, outFileName_;
1829   const double pmin_, pmax_;
1830   const bool debug_;
1831   TFile *outputFile_;
1832   TDirectoryFile *outputDir_;
1833   TTree *outputTree_;
1834 };
1835 
1836 CalibSplit::CalibSplit(
1837     const char *fname, const std::string &dirnm, const std::string &outFileName, double pmin, double pmax, bool debug)
1838     : fname_(fname),
1839       dirnm_(dirnm),
1840       outFileName_(outFileName),
1841       pmin_(pmin),
1842       pmax_(pmax),
1843       debug_(debug),
1844       outputFile_(nullptr),
1845       outputDir_(nullptr),
1846       outputTree_(nullptr) {
1847   char treeName[400];
1848   sprintf(treeName, "%s/CalibTree", dirnm.c_str());
1849   TChain *chain = new TChain(treeName);
1850   std::cout << "Create a chain for " << treeName << " from " << fname << " to write tracs of momentum in the range "
1851             << pmin_ << ":" << pmax_ << " to file " << outFileName_ << std::endl;
1852   if (!fillChain(chain, fname)) {
1853     std::cout << "*****No valid tree chain can be obtained*****" << std::endl;
1854   } else {
1855     std::cout << "Proceed with a tree chain with " << chain->GetEntries() << " entries" << std::endl;
1856     Init(chain);
1857   }
1858 }
1859 
1860 CalibSplit::~CalibSplit() {
1861   if (!fChain)
1862     return;
1863   delete fChain->GetCurrentFile();
1864 }
1865 
1866 Int_t CalibSplit::GetEntry(Long64_t entry) {
1867   // Read contents of entry.
1868   if (!fChain)
1869     return 0;
1870   return fChain->GetEntry(entry);
1871 }
1872 
1873 Long64_t CalibSplit::LoadTree(Long64_t entry) {
1874   // Set the environment to read one entry
1875   if (!fChain)
1876     return -5;
1877   Long64_t centry = fChain->LoadTree(entry);
1878   if (centry < 0)
1879     return centry;
1880   if (!fChain->InheritsFrom(TChain::Class()))
1881     return centry;
1882   TChain *chain = (TChain *)fChain;
1883   if (chain->GetTreeNumber() != fCurrent) {
1884     fCurrent = chain->GetTreeNumber();
1885     Notify();
1886   }
1887   return centry;
1888 }
1889 
1890 void CalibSplit::Init(TChain *tree) {
1891   // The Init() function is called when the selector needs to initialize
1892   // a new tree or chain. Typically here the branch addresses and branch
1893   // pointers of the tree will be set.
1894   // It is normally not necessary to make changes to the generated
1895   // code, but the routine can be extended by the user if needed.
1896   // Init() will be called many times when running on PROOF
1897   // (once per file to be processed).
1898 
1899   // Set object pointer
1900   t_DetIds = nullptr;
1901   t_DetIds1 = nullptr;
1902   t_DetIds3 = nullptr;
1903   t_HitEnergies = nullptr;
1904   t_HitEnergies1 = nullptr;
1905   t_HitEnergies3 = nullptr;
1906   t_trgbits = nullptr;
1907   // Set branch addresses and branch pointers
1908   fChain = tree;
1909   fCurrent = -1;
1910   if (!tree)
1911     return;
1912   fChain->SetMakeClass(1);
1913 
1914   fChain->SetBranchAddress("t_Run", &t_Run, &b_t_Run);
1915   fChain->SetBranchAddress("t_Event", &t_Event, &b_t_Event);
1916   fChain->SetBranchAddress("t_DataType", &t_DataType, &b_t_DataType);
1917   fChain->SetBranchAddress("t_ieta", &t_ieta, &b_t_ieta);
1918   fChain->SetBranchAddress("t_iphi", &t_iphi, &b_t_iphi);
1919   fChain->SetBranchAddress("t_EventWeight", &t_EventWeight, &b_t_EventWeight);
1920   fChain->SetBranchAddress("t_nVtx", &t_nVtx, &b_t_nVtx);
1921   fChain->SetBranchAddress("t_nTrk", &t_nTrk, &b_t_nTrk);
1922   fChain->SetBranchAddress("t_goodPV", &t_goodPV, &b_t_goodPV);
1923   fChain->SetBranchAddress("t_l1pt", &t_l1pt, &b_t_l1pt);
1924   fChain->SetBranchAddress("t_l1eta", &t_l1eta, &b_t_l1eta);
1925   fChain->SetBranchAddress("t_l1phi", &t_l1phi, &b_t_l1phi);
1926   fChain->SetBranchAddress("t_l3pt", &t_l3pt, &b_t_l3pt);
1927   fChain->SetBranchAddress("t_l3eta", &t_l3eta, &b_t_l3eta);
1928   fChain->SetBranchAddress("t_l3phi", &t_l3phi, &b_t_l3phi);
1929   fChain->SetBranchAddress("t_p", &t_p, &b_t_p);
1930   fChain->SetBranchAddress("t_pt", &t_pt, &b_t_pt);
1931   fChain->SetBranchAddress("t_phi", &t_phi, &b_t_phi);
1932   fChain->SetBranchAddress("t_mindR1", &t_mindR1, &b_t_mindR1);
1933   fChain->SetBranchAddress("t_mindR2", &t_mindR2, &b_t_mindR2);
1934   fChain->SetBranchAddress("t_eMipDR", &t_eMipDR, &b_t_eMipDR);
1935   fChain->SetBranchAddress("t_eHcal", &t_eHcal, &b_t_eHcal);
1936   fChain->SetBranchAddress("t_eHcal10", &t_eHcal10, &b_t_eHcal10);
1937   fChain->SetBranchAddress("t_eHcal30", &t_eHcal30, &b_t_eHcal30);
1938   fChain->SetBranchAddress("t_hmaxNearP", &t_hmaxNearP, &b_t_hmaxNearP);
1939   fChain->SetBranchAddress("t_rhoh", &t_rhoh, &b_t_rhoh);
1940   fChain->SetBranchAddress("t_selectTk", &t_selectTk, &b_t_selectTk);
1941   fChain->SetBranchAddress("t_qltyFlag", &t_qltyFlag, &b_t_qltyFlag);
1942   fChain->SetBranchAddress("t_qltyMissFlag", &t_qltyMissFlag, &b_t_qltyMissFlag);
1943   fChain->SetBranchAddress("t_qltyPVFlag", &t_qltyPVFlag, &b_t_qltyPVFlag);
1944   fChain->SetBranchAddress("t_gentrackP", &t_gentrackP, &b_t_gentrackP);
1945   fChain->SetBranchAddress("t_DetIds", &t_DetIds, &b_t_DetIds);
1946   fChain->SetBranchAddress("t_HitEnergies", &t_HitEnergies, &b_t_HitEnergies);
1947   fChain->SetBranchAddress("t_trgbits", &t_trgbits, &b_t_trgbits);
1948   fChain->SetBranchAddress("t_DetIds1", &t_DetIds1, &b_t_DetIds1);
1949   fChain->SetBranchAddress("t_DetIds3", &t_DetIds3, &b_t_DetIds3);
1950   fChain->SetBranchAddress("t_HitEnergies1", &t_HitEnergies1, &b_t_HitEnergies1);
1951   fChain->SetBranchAddress("t_HitEnergies3", &t_HitEnergies3, &b_t_HitEnergies3);
1952   Notify();
1953 
1954   tout_DetIds = new std::vector<unsigned int>();
1955   tout_HitEnergies = new std::vector<double>();
1956   tout_trgbits = new std::vector<bool>();
1957   tout_DetIds1 = new std::vector<unsigned int>();
1958   tout_DetIds3 = new std::vector<unsigned int>();
1959   tout_HitEnergies1 = new std::vector<double>();
1960   tout_HitEnergies3 = new std::vector<double>();
1961 
1962   outputFile_ = TFile::Open(outFileName_.c_str(), "RECREATE");
1963   outputFile_->cd();
1964   outputDir_ = new TDirectoryFile(dirnm_.c_str(), dirnm_.c_str());
1965   outputDir_->cd();
1966   outputTree_ = new TTree("CalibTree", "CalibTree");
1967   outputTree_->Branch("t_Run", &tout_Run);
1968   outputTree_->Branch("t_Event", &tout_Event);
1969   outputTree_->Branch("t_DataType", &tout_DataType);
1970   outputTree_->Branch("t_ieta", &tout_ieta);
1971   outputTree_->Branch("t_iphi", &tout_iphi);
1972   outputTree_->Branch("t_EventWeight", &tout_EventWeight);
1973   outputTree_->Branch("t_nVtx", &tout_nVtx);
1974   outputTree_->Branch("t_nTrk", &tout_nTrk);
1975   outputTree_->Branch("t_goodPV", &tout_goodPV);
1976   outputTree_->Branch("t_l1pt", &tout_l1pt);
1977   outputTree_->Branch("t_l1eta", &tout_l1eta);
1978   outputTree_->Branch("t_l1phi", &tout_l1phi);
1979   outputTree_->Branch("t_l3pt", &tout_l3pt);
1980   outputTree_->Branch("t_l3eta", &tout_l3eta);
1981   outputTree_->Branch("t_l3phi", &tout_l3phi);
1982   outputTree_->Branch("t_p", &tout_p);
1983   outputTree_->Branch("t_pt", &tout_pt);
1984   outputTree_->Branch("t_phi", &tout_phi);
1985   outputTree_->Branch("t_mindR1", &tout_mindR1);
1986   outputTree_->Branch("t_mindR2", &tout_mindR2);
1987   outputTree_->Branch("t_eMipDR", &tout_eMipDR);
1988   outputTree_->Branch("t_eHcal", &tout_eHcal);
1989   outputTree_->Branch("t_eHcal10", &tout_eHcal10);
1990   outputTree_->Branch("t_eHcal30", &tout_eHcal30);
1991   outputTree_->Branch("t_hmaxNearP", &tout_hmaxNearP);
1992   outputTree_->Branch("t_rhoh", &tout_rhoh);
1993   outputTree_->Branch("t_selectTk", &tout_selectTk);
1994   outputTree_->Branch("t_qltyFlag", &tout_qltyFlag);
1995   outputTree_->Branch("t_qltyMissFlag", &tout_qltyMissFlag);
1996   outputTree_->Branch("t_qltyPVFlag", &tout_qltyPVFlag);
1997   outputTree_->Branch("t_gentrackP", &tout_gentrackP);
1998   outputTree_->Branch("t_DetIds", &tout_DetIds);
1999   outputTree_->Branch("t_HitEnergies", &tout_HitEnergies);
2000   outputTree_->Branch("t_trgbits", &tout_trgbits);
2001   outputTree_->Branch("t_DetIds1", &tout_DetIds1);
2002   outputTree_->Branch("t_DetIds3", &tout_DetIds3);
2003   outputTree_->Branch("t_HitEnergies1", &tout_HitEnergies1);
2004   outputTree_->Branch("t_HitEnergies3", &tout_HitEnergies3);
2005 
2006   std::cout << "Output CalibTree is created in directory " << dirnm_ << " of " << outFileName_ << std::endl;
2007 }
2008 
2009 Bool_t CalibSplit::Notify() {
2010   // The Notify() function is called when a new file is opened. This
2011   // can be either for a new TTree in a TChain or when when a new TTree
2012   // is started when using PROOF. It is normally not necessary to make changes
2013   // to the generated code, but the routine can be extended by the
2014   // user if needed. The return value is currently not used.
2015 
2016   return kTRUE;
2017 }
2018 
2019 void CalibSplit::Show(Long64_t entry) {
2020   // Print contents of entry.
2021   // If entry is not specified, print current entry
2022   if (!fChain)
2023     return;
2024   fChain->Show(entry);
2025 }
2026 
2027 Int_t CalibSplit::Cut(Long64_t) {
2028   // This function may be called from Loop.
2029   // returns  1 if entry is accepted.
2030   // returns -1 otherwise.
2031   return 1;
2032 }
2033 
2034 void CalibSplit::Loop(Long64_t nentries) {
2035   //   In a ROOT session, you can do:
2036   //      Root > .L CalibMonitor.C
2037   //      Root > CalibMonitor t
2038   //      Root > t.GetEntry(12); // Fill t data members with entry number 12
2039   //      Root > t.Show();       // Show values of entry 12
2040   //      Root > t.Show(16);     // Read and show values of entry 16
2041   //      Root > t.Loop();       // Loop on all entries
2042   //
2043 
2044   //   This is the loop skeleton where:
2045   //      jentry is the global entry number in the chain
2046   //      ientry is the entry number in the current Tree
2047   //   Note that the argument to GetEntry must be:
2048   //      jentry for TChain::GetEntry
2049   //      ientry for TTree::GetEntry and TBranch::GetEntry
2050   //
2051   //       To read only selected branches, Insert statements like:
2052   // METHOD1:
2053   //    fChain->SetBranchStatus("*",0);  // disable all branches
2054   //    fChain->SetBranchStatus("branchname",1);  // activate branchname
2055   // METHOD2: replace line
2056   //    fChain->GetEntry(jentry);       //read all branches
2057   //by  b_branchname->GetEntry(ientry); //read only this branch
2058 
2059   if (fChain == 0)
2060     return;
2061 
2062   // Find list of duplicate events
2063   if (nentries < 0)
2064     nentries = fChain->GetEntriesFast();
2065   std::cout << "Total entries " << nentries << ":" << fChain->GetEntriesFast() << std::endl;
2066   Long64_t nbytes(0), nb(0);
2067   unsigned int good(0), reject(0), kount(0);
2068   for (Long64_t jentry = 0; jentry < nentries; jentry++) {
2069     Long64_t ientry = LoadTree(jentry);
2070     if (ientry < 0)
2071       break;
2072     nb = fChain->GetEntry(jentry);
2073     nbytes += nb;
2074     if (jentry % 1000000 == 0)
2075       std::cout << "Entry " << jentry << " Run " << t_Run << " Event " << t_Event << std::endl;
2076     ++kount;
2077     bool select = ((t_p >= pmin_) && (t_p < pmax_));
2078     if (!select) {
2079       ++reject;
2080       if (debug_)
2081         std::cout << "Rejected event " << t_Run << " " << t_Event << " " << t_p << std::endl;
2082       continue;
2083     }
2084     ++good;
2085     copyTree();
2086     outputTree_->Fill();
2087   }
2088   std::cout << "\nSelect " << good << " Reject " << reject << " entries out of a total of " << kount << " counts"
2089             << std::endl;
2090   close();
2091 }
2092 
2093 void CalibSplit::copyTree() {
2094   tout_Run = t_Run;
2095   tout_Event = t_Event;
2096   tout_DataType = t_DataType;
2097   tout_ieta = t_ieta;
2098   tout_iphi = t_iphi;
2099   tout_EventWeight = t_EventWeight;
2100   tout_nVtx = t_nVtx;
2101   tout_nTrk = t_nTrk;
2102   tout_goodPV = t_goodPV;
2103   tout_l1pt = t_l1pt;
2104   tout_l1eta = t_l1eta;
2105   tout_l1phi = t_l1phi;
2106   tout_l3pt = t_l3pt;
2107   tout_l3eta = t_l3eta;
2108   tout_l3phi = t_l3phi;
2109   tout_p = t_p;
2110   tout_pt = t_pt;
2111   tout_phi = t_phi;
2112   tout_mindR1 = t_mindR1;
2113   tout_mindR2 = t_mindR2;
2114   tout_eMipDR = t_eMipDR;
2115   tout_eHcal = t_eHcal;
2116   tout_eHcal10 = t_eHcal10;
2117   tout_eHcal30 = t_eHcal30;
2118   tout_hmaxNearP = t_hmaxNearP;
2119   tout_rhoh = t_rhoh;
2120   tout_selectTk = t_selectTk;
2121   tout_qltyFlag = t_qltyFlag;
2122   tout_qltyMissFlag = t_qltyMissFlag;
2123   tout_qltyPVFlag = t_qltyPVFlag;
2124   tout_gentrackP = t_gentrackP;
2125   tout_DetIds->clear();
2126   if (t_DetIds != nullptr) {
2127     tout_DetIds->reserve(t_DetIds->size());
2128     for (unsigned int i = 0; i < t_DetIds->size(); ++i)
2129       tout_DetIds->push_back((*t_DetIds)[i]);
2130   }
2131   tout_HitEnergies->clear();
2132   if (t_HitEnergies != nullptr) {
2133     tout_HitEnergies->reserve(t_HitEnergies->size());
2134     for (unsigned int i = 0; i < t_HitEnergies->size(); ++i)
2135       tout_HitEnergies->push_back((*t_HitEnergies)[i]);
2136   }
2137   tout_trgbits->clear();
2138   if (t_trgbits != nullptr) {
2139     tout_trgbits->reserve(t_trgbits->size());
2140     for (unsigned int i = 0; i < t_trgbits->size(); ++i)
2141       tout_trgbits->push_back((*t_trgbits)[i]);
2142   }
2143   tout_DetIds1->clear();
2144   if (t_DetIds1 != nullptr) {
2145     tout_DetIds1->reserve(t_DetIds1->size());
2146     for (unsigned int i = 0; i < t_DetIds1->size(); ++i)
2147       tout_DetIds1->push_back((*t_DetIds1)[i]);
2148   }
2149   tout_DetIds3->clear();
2150   if (t_DetIds3 != nullptr) {
2151     tout_DetIds3->reserve(t_DetIds3->size());
2152     for (unsigned int i = 0; i < t_DetIds3->size(); ++i)
2153       tout_DetIds3->push_back((*t_DetIds3)[i]);
2154   }
2155   tout_HitEnergies1->clear();
2156   if (t_HitEnergies1 != nullptr) {
2157     tout_HitEnergies1->reserve(t_HitEnergies1->size());
2158     for (unsigned int i = 0; i < t_HitEnergies1->size(); ++i)
2159       tout_HitEnergies1->push_back((*t_HitEnergies1)[i]);
2160   }
2161   tout_HitEnergies3->clear();
2162   if (t_HitEnergies1 != nullptr) {
2163     tout_HitEnergies3->reserve(t_HitEnergies3->size());
2164     for (unsigned int i = 0; i < t_HitEnergies3->size(); ++i)
2165       tout_HitEnergies3->push_back((*t_HitEnergies3)[i]);
2166   }
2167 }
2168 
2169 void CalibSplit::close() {
2170   if (outputFile_) {
2171     outputDir_->cd();
2172     std::cout << "file yet to be Written" << std::endl;
2173     outputTree_->Write();
2174     std::cout << "file Written" << std::endl;
2175     outputFile_->Close();
2176     std::cout << "now doing return" << std::endl;
2177   }
2178   outputFile_ = nullptr;
2179 }