Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:52

0001 #include <TStyle.h>
0002 #include <TCanvas.h>
0003 #include <TROOT.h>
0004 #include <TChain.h>
0005 #include <TFile.h>
0006 #include <TFitResult.h>
0007 #include <TFitResultPtr.h>
0008 #include <TTree.h>
0009 #include <TH1.h>
0010 #include <TGraph.h>
0011 #include <TProfile.h>
0012 #include <algorithm>
0013 #include <vector>
0014 #include <string>
0015 #include <iomanip>
0016 #include <iostream>
0017 #include <fstream>
0018 #include <sstream>
0019 
0020 class CalibTreeExtended {
0021 public :
0022 
0023   CalibTreeExtended(const char* infile, const char* outfile,
0024             bool debug=false);
0025   virtual ~CalibTreeExtended();
0026   virtual Int_t    Cut(Long64_t entry);
0027   virtual Int_t    GetEntry(Long64_t entry);
0028   virtual Long64_t LoadTree(Long64_t entry);
0029   virtual void     Init(TTree *tree);
0030   virtual void     Loop();
0031   virtual Bool_t   Notify();
0032   virtual void     Show(Long64_t entry = -1);
0033 
0034 private:
0035   void             BookHisto(const char* outfile);
0036   void             Close();
0037 
0038   TTree                     *fChain;  //!pointer to the analyzed TTree or TChain
0039   Int_t                      fCurrent;//!current Tree number in a TChain
0040 
0041   // Fixed size dimensions of array or collections stored in the TTree if any.
0042   // Declaration of leaf types
0043   Int_t                      t_Run;
0044   Int_t                      t_Event;
0045   Int_t                      t_DataType;
0046   Int_t                      t_ieta;
0047   Int_t                      t_iphi;
0048   Double_t                   t_EventWeight;
0049   Int_t                      t_nVtx;
0050   Int_t                      t_nTrk;
0051   Int_t                      t_goodPV;
0052   Double_t                   t_l1pt;
0053   Double_t                   t_l1eta;
0054   Double_t                   t_l1phi;
0055   Double_t                   t_l3pt;
0056   Double_t                   t_l3eta;
0057   Double_t                   t_l3phi;
0058   Double_t                   t_p;
0059   Double_t                   t_pt;
0060   Double_t                   t_phi;
0061   std::vector<double>       *t_mapP;
0062   std::vector<double>       *t_mapPt;
0063   std::vector<double>       *t_mapEta;
0064   std::vector<double>       *t_mapPhi;
0065   Double_t                   t_mindR1;
0066   Double_t                   t_mindR2;
0067   Double_t                   t_eMipDR;
0068   Double_t                   t_eHcal;
0069   Double_t                   t_eHcal10;
0070   Double_t                   t_eHcal30;
0071   Double_t                   t_hmaxNearP;
0072   Double_t                   t_emaxNearP;
0073   Double_t                   t_eAnnular;
0074   Double_t                   t_hAnnular;
0075   Double_t                   t_rhoh;
0076   Bool_t                     t_selectTk;
0077   Bool_t                     t_qltyFlag;
0078   Bool_t                     t_qltyMissFlag;
0079   Bool_t                     t_qltyPVFlag;
0080   Double_t                   t_gentrackP;
0081   Double_t                   t_gentrackE;
0082   std::vector<unsigned int> *t_DetIds;
0083   std::vector<double>       *t_HitEnergies;
0084   std::vector<bool>         *t_trgbits;
0085   std::vector<unsigned int> *t_DetIds1;
0086   std::vector<double>       *t_HitEnergies1;
0087   std::vector<unsigned int> *t_DetIds3;
0088   std::vector<double>       *t_HitEnergies3;
0089   std::vector<unsigned int> *t_DetIdEC;
0090   std::vector<double>       *t_HitEnergyEC;
0091   std::vector<double>       *t_HitDistEC;
0092   std::vector<unsigned int> *t_DetIdHC;
0093   std::vector<double>       *t_HitEnergyHC;
0094   std::vector<double>       *t_HitDistHC;
0095   
0096   // List of branches
0097   TBranch                   *b_t_Run;
0098   TBranch                   *b_t_Event;
0099   TBranch                   *b_t_DataType;
0100   TBranch                   *b_t_ieta;
0101   TBranch                   *b_t_iphi;
0102   TBranch                   *b_t_EventWeight;
0103   TBranch                   *b_t_nVtx;
0104   TBranch                   *b_t_nTrk;
0105   TBranch                   *b_t_goodPV;
0106   TBranch                   *b_t_l1pt;
0107   TBranch                   *b_t_l1eta;
0108   TBranch                   *b_t_l1phi;
0109   TBranch                   *b_t_l3pt;
0110   TBranch                   *b_t_l3eta;
0111   TBranch                   *b_t_l3phi;
0112   TBranch                   *b_t_p;
0113   TBranch                   *b_t_pt;
0114   TBranch                   *b_t_phi;
0115   TBranch                   *b_t_mapP;
0116   TBranch                   *b_t_mapPt;
0117   TBranch                   *b_t_mapEta;
0118   TBranch                   *b_t_mapPhi;
0119   TBranch                   *b_t_mindR1;
0120   TBranch                   *b_t_mindR2;
0121   TBranch                   *b_t_eMipDR;
0122   TBranch                   *b_t_eHcal;
0123   TBranch                   *b_t_eHcal10;
0124   TBranch                   *b_t_eHcal30;
0125   TBranch                   *b_t_hmaxNearP;
0126   TBranch                   *b_t_emaxNearP;
0127   TBranch                   *b_t_eAnnular;
0128   TBranch                   *b_t_hAnnular;
0129   TBranch                   *b_t_rhoh;
0130   TBranch                   *b_t_selectTk;
0131   TBranch                   *b_t_qltyFlag;
0132   TBranch                   *b_t_qltyMissFlag;
0133   TBranch                   *b_t_qltyPVFlag;
0134   TBranch                   *b_t_gentrackP;
0135   TBranch                   *b_t_gentrackE;
0136   TBranch                   *b_t_DetIds;
0137   TBranch                   *b_t_HitEnergies;
0138   TBranch                   *b_t_trgbits;
0139   TBranch                   *b_t_DetIds1;
0140   TBranch                   *b_t_HitEnergies1;
0141   TBranch                   *b_t_DetIds3;
0142   TBranch                   *b_t_HitEnergies3;
0143   TBranch                   *b_t_DetIdEC;
0144   TBranch                   *b_t_HitEnergyEC;
0145   TBranch                   *b_t_HitDistEC;
0146   TBranch                   *b_t_DetIdHC;
0147   TBranch                   *b_t_HitEnergyHC;
0148   TBranch                   *b_t_HitDistHC;
0149 
0150   bool                       debug_;
0151   TFile                     *output_file;
0152 };
0153 
0154 CalibTreeExtended::CalibTreeExtended(const char *infile, const char *outfile,
0155                      const bool debug) : debug_(debug) {
0156   TFile      *file = new TFile(infile);
0157   TDirectory *dir  = (TDirectory*)(file->FindObjectAny("hcalIsoTrackStudy"));
0158   TTree      *tree = (TTree*)(dir->FindObjectAny("CalibTreeExtended"));
0159   std::cout << "Attaches tree CalibTreeExtended at " << tree << " in file " 
0160         << infile << std::endl;
0161   
0162   BookHisto(outfile);
0163   Init(tree);
0164 }
0165 
0166 CalibTreeExtended::~CalibTreeExtended() {
0167   Close();
0168   if (!fChain) return;
0169   delete fChain->GetCurrentFile();
0170 }
0171 
0172 Int_t CalibTreeExtended::GetEntry(Long64_t entry) {
0173   // Read contents of entry.
0174   if (!fChain) return 0;
0175   return fChain->GetEntry(entry);
0176 }
0177 
0178 Long64_t CalibTreeExtended::LoadTree(Long64_t entry) {
0179   // Set the environment to read one entry
0180   if (!fChain) return -5;
0181   Long64_t centry = fChain->LoadTree(entry);
0182   if (centry < 0) return centry;
0183   if (fChain->GetTreeNumber() != fCurrent) {
0184     fCurrent = fChain->GetTreeNumber();
0185     Notify();
0186   }
0187   return centry;
0188 }
0189 
0190 void CalibTreeExtended::Init(TTree *tree) {
0191   // The Init() function is called when the selector needs to initialize
0192   // a new tree or chain. Typically here the branch addresses and branch
0193   // pointers of the tree will be set.
0194   // It is normally not necessary to make changes to the generated
0195   // code, but the routine can be extended by the user if needed.
0196   // Init() will be called many times when running on PROOF
0197   // (once per file to be processed).
0198 
0199   // Set object pointer
0200   t_mapP = 0;
0201   t_mapPt = 0;
0202   t_mapEta = 0;
0203   t_mapPhi = 0;
0204   t_DetIds = 0;
0205   t_HitEnergies = 0;
0206   t_trgbits = 0;
0207   t_DetIds1 = 0;
0208   t_HitEnergies1 = 0;
0209   t_DetIds3 = 0;
0210   t_HitEnergies3 = 0;
0211   t_DetIdEC = 0;
0212   t_HitEnergyEC = 0;
0213   t_HitDistEC = 0;
0214   t_DetIdHC = 0;
0215   t_HitEnergyHC = 0;
0216   t_HitDistHC = 0;
0217   // Set branch addresses and branch pointers
0218   if (!tree) return;
0219   fChain = tree;
0220   fCurrent = -1;
0221   fChain->SetMakeClass(1);
0222 
0223   fChain->SetBranchAddress("t_Run", &t_Run, &b_t_Run);
0224   fChain->SetBranchAddress("t_Event", &t_Event, &b_t_Event);
0225   fChain->SetBranchAddress("t_DataType", &t_DataType, &b_t_DataType);
0226   fChain->SetBranchAddress("t_ieta", &t_ieta, &b_t_ieta);
0227   fChain->SetBranchAddress("t_iphi", &t_iphi, &b_t_iphi);
0228   fChain->SetBranchAddress("t_EventWeight", &t_EventWeight, &b_t_EventWeight);
0229   fChain->SetBranchAddress("t_nVtx", &t_nVtx, &b_t_nVtx);
0230   fChain->SetBranchAddress("t_nTrk", &t_nTrk, &b_t_nTrk);
0231   fChain->SetBranchAddress("t_goodPV", &t_goodPV, &b_t_goodPV);
0232   fChain->SetBranchAddress("t_l1pt", &t_l1pt, &b_t_l1pt);
0233   fChain->SetBranchAddress("t_l1eta", &t_l1eta, &b_t_l1eta);
0234   fChain->SetBranchAddress("t_l1phi", &t_l1phi, &b_t_l1phi);
0235   fChain->SetBranchAddress("t_l3pt", &t_l3pt, &b_t_l3pt);
0236   fChain->SetBranchAddress("t_l3eta", &t_l3eta, &b_t_l3eta);
0237   fChain->SetBranchAddress("t_l3phi", &t_l3phi, &b_t_l3phi);
0238   fChain->SetBranchAddress("t_p", &t_p, &b_t_p);
0239   fChain->SetBranchAddress("t_pt", &t_pt, &b_t_pt);
0240   fChain->SetBranchAddress("t_phi", &t_phi, &b_t_phi);
0241   fChain->SetBranchAddress("t_mapP", &t_mapP, &b_t_mapP);
0242   fChain->SetBranchAddress("t_mapPt", &t_mapPt, &b_t_mapPt);
0243   fChain->SetBranchAddress("t_mapEta", &t_mapEta, &b_t_mapEta);
0244   fChain->SetBranchAddress("t_mapPhi", &t_mapPhi, &b_t_mapPhi);
0245   fChain->SetBranchAddress("t_mindR1", &t_mindR1, &b_t_mindR1);
0246   fChain->SetBranchAddress("t_mindR2", &t_mindR2, &b_t_mindR2);
0247   fChain->SetBranchAddress("t_eMipDR", &t_eMipDR, &b_t_eMipDR);
0248   fChain->SetBranchAddress("t_eHcal", &t_eHcal, &b_t_eHcal);
0249   fChain->SetBranchAddress("t_eHcal10", &t_eHcal10, &b_t_eHcal10);
0250   fChain->SetBranchAddress("t_eHcal30", &t_eHcal30, &b_t_eHcal30);
0251   fChain->SetBranchAddress("t_hmaxNearP", &t_hmaxNearP, &b_t_hmaxNearP);
0252   fChain->SetBranchAddress("t_emaxNearP", &t_emaxNearP, &b_t_emaxNearP);
0253   fChain->SetBranchAddress("t_eAnnular", &t_eAnnular, &b_t_eAnnular);
0254   fChain->SetBranchAddress("t_hAnnular", &t_hAnnular, &b_t_hAnnular);
0255   fChain->SetBranchAddress("t_rhoh", &t_rhoh, &b_t_rhoh);
0256   fChain->SetBranchAddress("t_selectTk", &t_selectTk, &b_t_selectTk);
0257   fChain->SetBranchAddress("t_qltyFlag", &t_qltyFlag, &b_t_qltyFlag);
0258   fChain->SetBranchAddress("t_qltyMissFlag", &t_qltyMissFlag, &b_t_qltyMissFlag);
0259   fChain->SetBranchAddress("t_qltyPVFlag", &t_qltyPVFlag, &b_t_qltyPVFlag);
0260   fChain->SetBranchAddress("t_gentrackP", &t_gentrackP, &b_t_gentrackP);
0261   fChain->SetBranchAddress("t_gentrackE", &t_gentrackE, &b_t_gentrackE);
0262   fChain->SetBranchAddress("t_DetIds", &t_DetIds, &b_t_DetIds);
0263   fChain->SetBranchAddress("t_HitEnergies", &t_HitEnergies, &b_t_HitEnergies);
0264   fChain->SetBranchAddress("t_trgbits", &t_trgbits, &b_t_trgbits);
0265   fChain->SetBranchAddress("t_DetIds1", &t_DetIds1, &b_t_DetIds1);
0266   fChain->SetBranchAddress("t_HitEnergies1", &t_HitEnergies1, &b_t_HitEnergies1);
0267   fChain->SetBranchAddress("t_DetIds3", &t_DetIds3, &b_t_DetIds3);
0268   fChain->SetBranchAddress("t_HitEnergies3", &t_HitEnergies3, &b_t_HitEnergies3);
0269   fChain->SetBranchAddress("t_DetIdEC", &t_DetIdEC, &b_t_DetIdEC);
0270   fChain->SetBranchAddress("t_HitEnergyEC", &t_HitEnergyEC, &b_t_HitEnergyEC);
0271   fChain->SetBranchAddress("t_HitDistEC", &t_HitDistEC, &b_t_HitDistEC);
0272   fChain->SetBranchAddress("t_DetIdHC", &t_DetIdHC, &b_t_DetIdHC);
0273   fChain->SetBranchAddress("t_HitEnergyHC", &t_HitEnergyHC, &b_t_HitEnergyHC);
0274   fChain->SetBranchAddress("t_HitDistHC", &t_HitDistHC, &b_t_HitDistHC);
0275   Notify();
0276 }
0277 
0278 Bool_t CalibTreeExtended::Notify() {
0279   // The Notify() function is called when a new file is opened. This
0280   // can be either for a new TTree in a TChain or when when a new TTree
0281   // is started when using PROOF. It is normally not necessary to make changes
0282   // to the generated code, but the routine can be extended by the
0283   // user if needed. The return value is currently not used.
0284 
0285   return kTRUE;
0286 }
0287 
0288 void CalibTreeExtended::Show(Long64_t entry) {
0289   // Print contents of entry.
0290   // If entry is not specified, print current entry
0291   if (!fChain) return;
0292   fChain->Show(entry);
0293 }
0294 
0295 Int_t CalibTreeExtended::Cut(Long64_t) {
0296   // This function may be called from Loop.
0297   // returns  1 if entry is accepted.
0298   // returns -1 otherwise.
0299   return 1;
0300 }
0301 
0302 void CalibTreeExtended::Loop() {
0303   //   In a ROOT session, you can do:
0304   //      root> .L CalibTreeExtended.C
0305   //      root> CalibTreeExtended t
0306   //      root> t.GetEntry(12); // Fill t data members with entry number 12
0307   //      root> t.Show();       // Show values of entry 12
0308   //      root> t.Show(16);     // Read and show values of entry 16
0309   //      root> t.Loop();       // Loop on all entries
0310   //
0311   
0312   //     This is the loop skeleton where:
0313   //    jentry is the global entry number in the chain
0314   //    ientry is the entry number in the current Tree
0315   //  Note that the argument to GetEntry must be:
0316   //    jentry for TChain::GetEntry
0317   //    ientry for TTree::GetEntry and TBranch::GetEntry
0318   //
0319   //       To read only selected branches, Insert statements like:
0320   // METHOD1:
0321   //    fChain->SetBranchStatus("*",0);  // disable all branches
0322   //    fChain->SetBranchStatus("branchname",1);  // activate branchname
0323   // METHOD2: replace line
0324   //    fChain->GetEntry(jentry);       //read all branches
0325   //by  b_branchname->GetEntry(ientry); //read only this branch
0326   if (fChain == 0) return;
0327 
0328   Long64_t nentries = fChain->GetEntriesFast();
0329   
0330   Long64_t nbytes = 0, nb = 0;
0331   for (Long64_t jentry=0; jentry<nentries;jentry++) {
0332     Long64_t ientry = LoadTree(jentry);
0333     if (ientry < 0) break;
0334     nb = fChain->GetEntry(jentry);   nbytes += nb;
0335     // if (Cut(ientry) < 0) continue;
0336   }
0337 }
0338 
0339 void CalibTreeExtended::BookHisto(const char* fname) {
0340   output_file = TFile::Open(fname,"RECREATE");
0341 }
0342 
0343 void CalibTreeExtended::Close() {
0344   output_file->cd();
0345   if (debug_) std::cout << "file yet to be Written" << std::endl;
0346   output_file->Write();
0347   std::cout << "output file Written" << std::endl;
0348   output_file->Close();
0349   if (debug_) std::cout << "now doing return" << std::endl;
0350 }