ROOT
6.30.04
Reference Guide
All
Namespaces
Files
Pages
htest.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_tree
3
/// \notebook
4
/// Save histograms in Tree branches
5
///
6
/// To run this example, do
7
/// ~~~{.cpp}
8
/// root > .L htest.C
9
/// root > htw()
10
/// root > htr1()
11
/// root > htr2()
12
/// root > htr3()
13
/// ~~~
14
///
15
/// \macro_image
16
/// \macro_code
17
///
18
/// \author Rene Brun
19
20
void
htw() {
21
// Create a Tree with a few branches of type histogram
22
// 25000 entries are filled in the Tree
23
// For each entry, the copy of 3 histograms is written
24
// The data base will contain 75000 histograms.
25
gBenchmark->Start(
"hsimple"
);
26
TFile f(
"ht.root"
,
"recreate"
);
27
auto
T =
new
TTree(
"T"
,
"test"
);
28
auto
hpx =
new
TH1F(
"hpx"
,
"This is the px distribution"
,100,-4,4);
29
auto
hpxpy =
new
TH2F(
"hpxpy"
,
"py vs px"
,40,-4,4,40,-4,4);
30
auto
hprof =
new
TProfile(
"hprof"
,
"Profile of pz versus px"
,100,-4,4,0,20);
31
T->Branch(
"hpx"
,
"TH1F"
,&hpx,32000,0);
32
T->Branch(
"hpxpy"
,
"TH2F"
,&hpxpy,32000,0);
33
T->Branch(
"hprof"
,
"TProfile"
,&hprof,32000,0);
34
Float_t px, py, pz;
35
for
(Int_t i = 0; i < 25000; i++) {
36
if
(i%1000 == 0) printf(
"at entry: %d\n"
,i);
37
gRandom->Rannor(px,py);
38
pz = px*px + py*py;
39
hpx->Fill(px);
40
hpxpy->Fill(px,py);
41
hprof->Fill(px,pz);
42
T->Fill();
43
}
44
T->Print();
45
f.Write();
46
gBenchmark->Show(
"hsimple"
);
47
}
48
49
void
htr1() {
50
// Connect Tree generated by htw and show histograms for entry 12345
51
auto
f =
new
TFile(
"ht.root"
);
52
auto
T = (TTree*)f->Get(
"T"
);
53
TH1F *hpx =
nullptr
;
54
TH2F *hpxpy =
nullptr
;
55
TProfile *hprof =
nullptr
;
56
T->SetBranchAddress(
"hpx"
,&hpx);
57
T->SetBranchAddress(
"hpxpy"
,&hpxpy);
58
T->SetBranchAddress(
"hprof"
,&hprof);
59
T->GetEntry(12345);
60
auto
c1 =
new
TCanvas(
"c1"
,
"test"
,10,10,600,1000);
61
c1->Divide(1,3);
62
c1->cd(1);
63
hpx->Draw();
64
c1->cd(2);
65
hpxpy->Draw();
66
c1->cd(3);
67
hprof->Draw();
68
c1->Print(
"htr1.png"
);
69
}
70
71
void
htr2() {
72
// Connect Tree generated by htw and show histograms for entry 12345
73
// a variant of htr1
74
auto
f =
new
TFile(
"ht.root"
);
75
auto
T = (TTree*)f->Get(
"T"
);
76
auto
c1 =
new
TCanvas(
"c1"
,
"test"
,10,10,600,1000);
77
c1->Divide(1,3);
78
c1->cd(1);
79
T->Draw(
"hpx.Draw()"
,
""
,
"goff"
,1,12345);
80
c1->cd(2);
81
T->Draw(
"hpxpy.Draw()"
,
""
,
"goff"
,1,12345);
82
c1->cd(3);
83
T->Draw(
"hprof.Draw()"
,
""
,
"goff"
,1,12345);
84
c1->Print(
"htr2.png"
);
85
}
86
87
void
htr3() {
88
// Connect Tree generated by htw
89
// read all histograms and plot the RMS of hpx versus the Mean of hprof
90
// for each of the 25000 entries
91
auto
f =
new
TFile(
"ht.root"
);
92
auto
T = (TTree*)f->Get(
"T"
);
93
auto
c1 =
new
TCanvas(
"c1"
,
"test"
,10,10,600,400);
94
T->Draw(
"hpx.GetRMS():hprof.GetMean()"
);
95
c1->Print(
"htr3.png"
);
96
}
97
98
void
htest() {
99
htw();
100
htr1();
101
htr2();
102
htr3();
103
}
tutorials
tree
htest.C
Generated on Tue May 5 2020 14:03:51 for ROOT by
1.8.5