/
AnalyzeTree.C
67 lines (58 loc) · 2.25 KB
/
AnalyzeTree.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
#include "TH1F.h"
const Int_t kMaxfParticles = 1293;
void AnalyzeTree()
{
// Variables used to store the data
Int_t totalSize = 0; // Sum of data size (in bytes) of all events
Int_t eventSize = 0; // Size of the current event
TH1F *hPosX; // X position of the particles
// List of branches
TBranch *nParticlesBranch;
TBranch *particlesPosXBranch;
TBranch *particlesMomentumBranch;
// Declaration of leaf types
Int_t nParticles;
Double_t particlesPosX[kMaxfParticles];
Double_t particlesMomentum[kMaxfParticles];
// open the file
TFile *f = TFile::Open("http://lcg-heppkg.web.cern.ch/lcg-heppkg/ROOT/eventdata.root");
if (f == 0) {
// if we cannot open the file, print an error message and return immediatly
printf("Error: cannot open http://lcg-heppkg.web.cern.ch/lcg-heppkg/ROOT/eventdata.root!\n");
return;
}
// get a pointer to the tree
TTree *tree = (TTree *)f->Get("EventTree");
// To use SetBranchAddress() with simple types (e.g. double, int)
// instead of objects (e.g. std::vector<Particle>).
tree->SetMakeClass(1);
// Connect the branches with their member variables.
tree->SetBranchAddress("fParticles", &nParticles, &nParticlesBranch);
tree->SetBranchAddress("fParticles.fPosX", particlesPosX, &particlesPosXBranch);
tree->SetBranchAddress("fParticles.fMomentum", particlesMomentum, &particlesMomentumBranch);
// create the TH1F histogram
hPosX = new TH1F("hPosX", "Position in X", 20, -5, 5);
// enable bin errors:
hPosX->Sumw2();
Long64_t nentries = tree->GetEntries();
for (Long64_t i=0;i<nentries;i++) {
// We only need the number of particles...
nParticlesBranch->GetEntry(i);
// ... and their position in X...
particlesPosXBranch->GetEntry(i);
// ... and their momentum
particlesMomentumBranch->GetEntry(i);
// *** 2. *** Do the actual analysis
for (int iParticle = 0; iParticle < nParticles; ++iParticle) {
if (particlesMomentum[iParticle] > 40.0)
hPosX->Fill(particlesPosX[iParticle]);
}
}
// Fit the histogram:
hPosX->Fit("pol2");
// and draw it:
hPosX->Draw();
}