Logo ROOT   6.12/07
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 
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 void htr1() {
49  //connect Tree generated by htw and show histograms for entry 12345
50  auto f = new TFile("ht.root");
51  auto T = (TTree*)f->Get("T");
52  TH1F *hpx = nullptr;
53  TH2F *hpxpy = nullptr;
54  TProfile *hprof = nullptr;
55  T->SetBranchAddress("hpx",&hpx);
56  T->SetBranchAddress("hpxpy",&hpxpy);
57  T->SetBranchAddress("hprof",&hprof);
58  T->GetEntry(12345);
59  auto c1 = new TCanvas("c1","test",10,10,600,1000);
60  c1->Divide(1,3);
61  c1->cd(1);
62  hpx->Draw();
63  c1->cd(2);
64  hpxpy->Draw();
65  c1->cd(3);
66  hprof->Draw();
67  c1->Print("htr1.png");
68 }
69 void htr2() {
70  //connect Tree generated by htw and show histograms for entry 12345
71  // a variant of htr1
72  auto f = new TFile("ht.root");
73  auto T = (TTree*)f->Get("T");
74  auto c1 = new TCanvas("c1","test",10,10,600,1000);
75  c1->Divide(1,3);
76  c1->cd(1);
77  T->Draw("hpx.Draw()","","goff",1,12345);
78  c1->cd(2);
79  T->Draw("hpxpy.Draw()","","goff",1,12345);
80  c1->cd(3);
81  T->Draw("hprof.Draw()","","goff",1,12345);
82  c1->Print("htr2.png");
83 }
84 void htr3() {
85  //connect Tree generated by htw
86  //read all histograms and plot the RMS of hpx versus the Mean of hprof
87  //for each of the 25000 entries
88  auto f = new TFile("ht.root");
89  auto T = (TTree*)f->Get("T");
90  T->Draw("hpx.GetRMS():hprof.GetMean()");
91  gPad->Print("htr3.png");
92 }
93 void htest() {
94  htw();
95  htr1();
96  htr2();
97  htr3();
98 }
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
float Float_t
Definition: RtypesCore.h:53
return c1
Definition: legend1.C:41
double T(double x)
Definition: ChebyshevPol.h:34
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:285
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
int Int_t
Definition: RtypesCore.h:41
Profile Histogram.
Definition: TProfile.h:32
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2969
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:249
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
The Canvas class.
Definition: TCanvas.h:31
#define gPad
Definition: TVirtualPad.h:285
A TTree object has a header with a name and a title.
Definition: TTree.h:70
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:291