Fork me on GitHub

Ticket #45: AddFriendExample.cpp

File AddFriendExample.cpp, 3.8 KB (added by Pavel Demin, 13 years ago)
Line 
1#include <iostream>
2#include <utility>
3#include <vector>
4
5#include "TROOT.h"
6#include "TApplication.h"
7
8#include "TChain.h"
9#include "TString.h"
10
11#include "TClonesArray.h"
12
13#include "ExRootAnalysis/interface/ExRootTreeReader.h"
14#include "ExRootAnalysis/interface/ExRootTreeWriter.h"
15#include "ExRootAnalysis/interface/ExRootTreeBranch.h"
16
17#include "ExRootAnalysis/interface/BlockClasses.h"
18
19using namespace std;
20
21//------------------------------------------------------------------------------
22
23void AnalyseEvents(ExRootTreeReader *treeReader)
24{
25 TClonesArray *branchElectron = treeReader->UseBranch("Electron");
26 TClonesArray *branchGenParticle = treeReader->UseBranch("GEN.Particle");
27
28 TIter itGen(branchGenParticle);
29 TRootC::GenParticle *gen;
30
31 TIter itElec(branchElectron);
32 TRootElectron *elec;
33
34 // general information
35 float E,Px,Py,Pz;
36 float PT,Eta,Phi;
37
38 // lepton information
39 bool IsolFlag;
40
41 // particle level information
42 int PID, Status, M1,M2,D1,D2;
43 float Charge, T, X, Y, Z, M;
44
45 Long64_t allEntries = treeReader->GetEntries();
46
47 cout << "** Chain contains " << allEntries << " events" << endl;
48
49
50 // Loop over all events
51 for(Long64_t entry = 0; entry < allEntries; ++entry)
52 {
53 // Load selected branches with data from specified event
54 treeReader->ReadEntry(entry);
55 cout << "** Event contains " << branchElectron->GetEntriesFast() << " electrons" << endl;
56
57 itGen.Reset();
58 while( (gen = (TRootC::GenParticle*) itGen.Next()) )
59 {
60 PID = gen->PID; // particle HEP ID number
61 Status = gen->Status; // particle status
62 M1 = gen->M1; // particle 1st mother
63 M2 = gen->M2; // particle 2nd mother
64 D1 = gen->D1; // particle 1st daughter
65 D2 = gen->D2; // particle 2nd daughter
66 Charge = gen->Charge; // electrical charge
67
68 T = gen->T; // particle vertex position (t component)
69 X = gen->X; // particle vertex position (x component)
70 Y = gen->Y; // particle vertex position (y component)
71 Z = gen->Z; // particle vertex position (z component)
72 M = gen->M; // particle mass
73 }
74
75 // access the Electron branch
76 itElec.Reset();
77 while( (elec = (TRootElectron*) itElec.Next()) )
78 {
79 E = elec->E; // particle energy in GeV
80 Px = elec->Px; // particle momentum vector (x component) in GeV
81 Py = elec->Py; // particle momentum vector (y component) in GeV
82 Pz = elec->Pz; // particle momentum vector (z component) in GeV
83
84 PT = elec->PT; // particle transverse momentum in GeV
85 Eta = elec->Eta; // particle pseudorapidity
86 Phi = elec->Phi; // particle azimuthal angle in rad
87 IsolFlag = elec->IsolFlag; // is the particule isolated?
88 }
89 }
90}
91
92//------------------------------------------------------------------------------
93
94void AddFriendExample()
95{
96 TChain *chain1 = new TChain("Analysis");
97 TChain *chain2 = new TChain("GEN");
98 chain1->Add("tt_jj_small.root");
99 chain2->Add("tt_jj_small.root");
100 chain1->AddFriend(chain2, "GEN");
101
102 ExRootTreeReader *treeReader = new ExRootTreeReader(chain1);
103
104 AnalyseEvents(treeReader);
105
106 cout << "** Exiting..." << endl;
107
108 delete treeReader;
109 delete chain1;
110 delete chain2;
111}
112
113//------------------------------------------------------------------------------
114
115int main(int argc, char *argv[])
116{
117 char *appName = "AddFriendExample";
118
119 gROOT->SetBatch();
120
121 int appargc = 1;
122 char *appargv[] = {appName};
123 TApplication app(appName, &appargc, appargv);
124
125 AddFriendExample();
126
127}
128