File indexing completed on 2024-04-06 12:29:06
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
0024 TH2F *hvxy;
0025 TH2F *hvxz;
0026 TH2F *hdphi;
0027 TH3F *hvxyz;
0028 TH1F *hpt;
0029 TTree *fChain;
0030 Int_t fCurrent;
0031
0032
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
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
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
0092
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
0101 }
0102 Init(tree);
0103 }
0104
0105 NtupleHelper::~NtupleHelper() {
0106 if (!fChain)
0107 return;
0108
0109 }
0110
0111 Int_t NtupleHelper::GetEntry(Long64_t entry) {
0112
0113 if (!fChain)
0114 return 0;
0115 return fChain->GetEntry(entry);
0116 }
0117 Long64_t NtupleHelper::LoadTree(Long64_t entry) {
0118
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
0136
0137
0138
0139
0140
0141
0142 if (tree == 0)
0143 return;
0144 fChain = tree;
0145 fCurrent = -1;
0146 fChain->SetMakeClass(1);
0147
0148
0149
0150 fChain->SetBranchAddress("pt", &pt);
0151 fChain->SetBranchAddress("d0", &d0);
0152
0153 fChain->SetBranchAddress("phi0", &phi);
0154 fChain->SetBranchAddress("sigmad0", &sigmaD);
0155 fChain->SetBranchAddress("z0", &z0);
0156 fChain->SetBranchAddress("sigmaz0", &sigmaz0);
0157
0158
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
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 b_pt = fChain->GetBranch("pt");
0185 b_d0 = fChain->GetBranch("d0");
0186
0187
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
0194
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
0210
0211 if (!fChain)
0212 return;
0213 fChain->Show(entry);
0214 }
0215 Int_t NtupleHelper::Cut(Long64_t entry) {
0216
0217
0218
0219 return 1;
0220 }
0221 #endif