Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:28:07

0001 #ifndef NtupleHelper_h
0002 #define NtupleHelper_h
0003 
0004 #include <TROOT.h>
0005 #include <TChain.h>
0006 #include <TFile.h>
0007 #include <TH1.h>
0008 #include <TH2.h>
0009 #include <TH3.h>
0010 #include <vector>
0011 #include <iostream>
0012 #include "TObject.h"
0013 
0014 #include "global.h"
0015 
0016 class NtupleHelper {
0017 public:
0018   TH1F *hsx;
0019   TH2F *hd;
0020   TH1F *hsw;
0021   TH1F *hsd;
0022   TH1F *hsigma;
0023   //  TH1F           *hsw;
0024   TH2F *hvxy;
0025   TH2F *hvxz;
0026   TH2F *hdphi;
0027   TH3F *hvxyz;
0028   TH1F *hpt;
0029   TTree *fChain;   //!pointer to the analyzed TTree or TChain
0030   Int_t fCurrent;  //!current Tree number in a TChain
0031 
0032   // Declaration of leave types
0033   Float_t run;
0034   Float_t event;
0035   Double_t pt;
0036   Double_t d0;
0037   Double_t phi;
0038   Double_t sigmaD;
0039   Double_t z0;
0040   Double_t sigmaz0;
0041   Float_t x;
0042   Float_t y;
0043   //   UInt_t nPixelHit;
0044   Double_t normchi2;
0045   Double_t eta;
0046   Double_t theta;
0047   Int_t charge;
0048   Int_t nTotLayerMeas;
0049   Int_t nStripLayerMeas;
0050   Int_t nPixelLayerMeas;
0051   Bool_t quality;
0052   Bool_t algo;
0053 
0054   // List of branches
0055   TBranch *b_run;      //!
0056   TBranch *b_event;    //!
0057   TBranch *b_pt;       //!
0058   TBranch *b_d0;       //!
0059   TBranch *b_phi;      //!
0060   TBranch *b_sigmaD;   //!
0061   TBranch *b_z0;       //!
0062   TBranch *b_sigmaz0;  //!
0063   TBranch *b_x;        //!
0064   TBranch *b_y;        //!
0065   TBranch *b_nTotLayerMeas;
0066   TBranch *b_nStripLayerMeas;
0067   TBranch *b_nPixelLayerMeas;
0068   TBranch *b_normchi2;
0069   TBranch *b_eta;
0070   TBranch *b_theta;
0071   TBranch *b_charge;
0072   TBranch *b_quality;
0073   TBranch *b_algo;
0074 
0075   NtupleHelper(const char *fname, TTree *tree = 0);
0076   virtual ~NtupleHelper();
0077   virtual Int_t Cut(Long64_t entry);
0078   virtual Int_t GetEntry(Long64_t entry);
0079   virtual Long64_t LoadTree(Long64_t entry);
0080   virtual void Init(TTree *tree);
0081   virtual zData Loop(int maxEvents = 0);
0082   virtual Bool_t Notify();
0083   virtual void Show(Long64_t entry = -1);
0084   virtual void Book();
0085   ClassDef(NtupleHelper, 1)
0086 };
0087 
0088 #endif
0089 #ifdef NtupleHelper_cc
0090 NtupleHelper::NtupleHelper(const char *fname, TTree *tree) {
0091   // if parameter tree is not specified (or zero), connect the file
0092   // used to generate this class and read the Tree.
0093   if (tree == 0) {
0094     TFile *f = (TFile *)gROOT->GetListOfFiles()->FindObject(fname);
0095     if (!f) {
0096       f = new TFile(fname);
0097     }
0098     tree = (TTree *)gDirectory->Get("mytree");
0099     std::cout << "[NtupleHelper] got tree " << std::endl;
0100     //tree = (TTree*)gDirectory->Get("mytree"); // for reco ntuple
0101   }
0102   Init(tree);
0103 }
0104 
0105 NtupleHelper::~NtupleHelper() {
0106   if (!fChain)
0107     return;
0108   //delete fChain->GetCurrentFile();
0109 }
0110 
0111 Int_t NtupleHelper::GetEntry(Long64_t entry) {
0112   // Read contents of entry.
0113   if (!fChain)
0114     return 0;
0115   return fChain->GetEntry(entry);
0116 }
0117 Long64_t NtupleHelper::LoadTree(Long64_t entry) {
0118   // Set the environment to read one entry
0119   if (!fChain)
0120     return -5;
0121   Long64_t centry = fChain->LoadTree(entry);
0122   if (centry < 0)
0123     return centry;
0124   if (fChain->IsA() != TChain::Class())
0125     return centry;
0126   TChain *chain = (TChain *)fChain;
0127   if (chain->GetTreeNumber() != fCurrent) {
0128     fCurrent = chain->GetTreeNumber();
0129     Notify();
0130   }
0131   return centry;
0132 }
0133 
0134 void NtupleHelper::Init(TTree *tree) {
0135   // The Init() function is called when the selector needs to initialize
0136   // a new tree or chain. Typically here the branch addresses of the tree
0137   // will be set. It is normaly not necessary to make changes to the
0138   // generated code, but the routine can be extended by the user if needed.
0139   // Init() will be called many times when running with PROOF.
0140 
0141   // Set branch addresses
0142   if (tree == 0)
0143     return;
0144   fChain = tree;
0145   fCurrent = -1;
0146   fChain->SetMakeClass(1);
0147 
0148   //fChain->SetBranchAddress("run",&run);
0149   //fChain->SetBranchAddress("event",&event);
0150   fChain->SetBranchAddress("pt", &pt);
0151   fChain->SetBranchAddress("d0", &d0);
0152   //fChain->SetBranchAddress("phi",&phi);
0153   fChain->SetBranchAddress("phi0", &phi);        // for reco ntuple
0154   fChain->SetBranchAddress("sigmad0", &sigmaD);  // for reco ntuple
0155   fChain->SetBranchAddress("z0", &z0);
0156   fChain->SetBranchAddress("sigmaz0", &sigmaz0);
0157   //fChain->SetBranchAddress("x",&x);
0158   //fChain->SetBranchAddress("y",&y);
0159   fChain->SetBranchAddress("nTotLayerMeas", &nTotLayerMeas);
0160   fChain->SetBranchAddress("nStripLayerMeas", &nStripLayerMeas);
0161   fChain->SetBranchAddress("nPixelLayerMeas", &nPixelLayerMeas);
0162   fChain->SetBranchAddress("normchi2", &normchi2);
0163   fChain->SetBranchAddress("eta", &eta);
0164   fChain->SetBranchAddress("theta", &theta);
0165   fChain->SetBranchAddress("charge", &charge);
0166   fChain->SetBranchAddress("quality", &quality);
0167   fChain->SetBranchAddress("algo", &algo);
0168 
0169   std::cout << "[NtupleHelper] tree initialized " << std::endl;
0170   Notify();
0171 }
0172 
0173 Bool_t NtupleHelper::Notify() {
0174   // The Notify() function is called when a new file is opened. This
0175   // can be either for a new TTree in a TChain or when when a new TTree
0176   // is started when using PROOF. Typically here the branch pointers
0177   // will be retrieved. It is normaly not necessary to make changes
0178   // to the generated code, but the routine can be extended by the
0179   // user if needed.
0180 
0181   // Get branch pointers
0182   //b_run = fChain->GetBranch("run");
0183   //b_event = fChain->GetBranch("event");
0184   b_pt = fChain->GetBranch("pt");
0185   b_d0 = fChain->GetBranch("d0");
0186   //b_phi = fChain->GetBranch("phi");
0187   //b_sigmaD = fChain->GetBranch("sigmaD");
0188   b_phi = fChain->GetBranch("phi0");
0189   b_sigmaD = fChain->GetBranch("sigmad0");
0190 
0191   b_z0 = fChain->GetBranch("z0");
0192   b_sigmaz0 = fChain->GetBranch("sigmaz0");
0193   //b_x = fChain->GetBranch("x");
0194   //b_y = fChain->GetBranch("y");
0195 
0196   b_nTotLayerMeas = fChain->GetBranch("nTotLayerMeas");
0197   b_nPixelLayerMeas = fChain->GetBranch("nPixelLayerMeas");
0198   b_nStripLayerMeas = fChain->GetBranch("nStripLayerMeas");
0199   b_normchi2 = fChain->GetBranch("normchi2");
0200   b_eta = fChain->GetBranch("eta");
0201   b_quality = fChain->GetBranch("quality");
0202   b_algo = fChain->GetBranch("algo");
0203 
0204   std::cout << "[NtupleHelper] branches notified" << std::endl;
0205   return kTRUE;
0206 }
0207 
0208 void NtupleHelper::Show(Long64_t entry) {
0209   // Print contents of entry.
0210   // If entry is not specified, print current entry
0211   if (!fChain)
0212     return;
0213   fChain->Show(entry);
0214 }
0215 Int_t NtupleHelper::Cut(Long64_t entry) {
0216   // This function may be called from Loop.
0217   // returns  1 if entry is accepted.
0218   // returns -1 otherwise.
0219   return 1;
0220 }
0221 #endif  // #ifdef NtupleHelper_cc