Logo ROOT   6.16/01
Reference Guide
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
20void 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
49void 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
71void 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
87void 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
98void htest() {
99 htw();
100 htr1();
101 htr2();
102 htr3();
103}
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
float Float_t
Definition: RtypesCore.h:53
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
The Canvas class.
Definition: TCanvas.h:31
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:250
Profile Histogram.
Definition: TProfile.h:32
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition: TRandom.cxx:481
A TTree object has a header with a name and a title.
Definition: TTree.h:71
return c1
Definition: legend1.C:41
double T(double x)
Definition: ChebyshevPol.h:34