ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
htest.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 ///
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 /// \macro_image
15 /// \macro_code
16 /// \author Rene Brun
17 
18 void htw() {
19  //create a Tree with a few branches of type histogram
20  //25000 entries are filled in the Tree
21  //For each entry, the copy of 3 histograms is written
22  //The data base will contain 75000 histograms.
23  gBenchmark->Start("hsimple");
24  TFile f("ht.root","recreate");
25  auto T = new TTree("T","test");
26  auto hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
27  auto hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
28  auto hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
29  T->Branch("hpx","TH1F",&hpx,32000,0);
30  T->Branch("hpxpy","TH2F",&hpxpy,32000,0);
31  T->Branch("hprof","TProfile",&hprof,32000,0);
32  Float_t px, py, pz;
33  for (Int_t i = 0; i < 25000; i++) {
34  if (i%1000 == 0) printf("at entry: %d\n",i);
35  gRandom->Rannor(px,py);
36  pz = px*px + py*py;
37  hpx->Fill(px);
38  hpxpy->Fill(px,py);
39  hprof->Fill(px,pz);
40  T->Fill();
41  }
42  T->Print();
43  f.Write();
44  gBenchmark->Show("hsimple");
45 }
46 void htr1() {
47  //connect Tree generated by htw and show histograms for entry 12345
48  auto f = new TFile("ht.root");
49  auto T = (TTree*)f->Get("T");
50  TH1F *hpx = nullptr;
51  TH2F *hpxpy = nullptr;
52  TProfile *hprof = nullptr;
53  T->SetBranchAddress("hpx",&hpx);
54  T->SetBranchAddress("hpxpy",&hpxpy);
55  T->SetBranchAddress("hprof",&hprof);
56  T->GetEntry(12345);
57  auto c1 = new TCanvas("c1","test",10,10,600,1000);
58  c1->Divide(1,3);
59  c1->cd(1);
60  hpx->Draw();
61  c1->cd(2);
62  hpxpy->Draw();
63  c1->cd(3);
64  hprof->Draw();
65  c1->Print("htr1.png");
66 }
67 void htr2() {
68  //connect Tree generated by htw and show histograms for entry 12345
69  // a variant of htr1
70  auto f = new TFile("ht.root");
71  auto T = (TTree*)f->Get("T");
72  auto c1 = new TCanvas("c1","test",10,10,600,1000);
73  c1->Divide(1,3);
74  c1->cd(1);
75  T->Draw("hpx.Draw()","","goff",1,12345);
76  c1->cd(2);
77  T->Draw("hpxpy.Draw()","","goff",1,12345);
78  c1->cd(3);
79  T->Draw("hprof.Draw()","","goff",1,12345);
80  c1->Print("htr2.png");
81 }
82 void htr3() {
83  //connect Tree generated by htw
84  //read all histograms and plot the RMS of hpx versus the Mean of hprof
85  //for each of the 25000 entries
86  auto f = new TFile("ht.root");
87  auto T = (TTree*)f->Get("T");
88  T->Draw("hpx.GetRMS():hprof.GetMean()");
89  gPad->Print("htr3.png");
90 }
91 void htest() {
92  htw();
93  htr1();
94  htr2();
95  htr3();
96 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
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:460
Float_t pz
Definition: hprod.C:33
float Float_t
Definition: RtypesCore.h:53
TCanvas * c1
Definition: legend1.C:2
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4306
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5144
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
virtual void Print(const char *filename="") const
Save Pad contents in a file in one of various formats.
Definition: TPad.cxx:4134
Float_t py
Definition: hprod.C:33
Profile Historam.
Definition: TProfile.h:34
TFile * f
TTree * T
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6495
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7510
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2248
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
Int_t Fill(const Double_t *v)
Definition: TProfile.h:56
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
The Canvas class.
Definition: TCanvas.h:48
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:360
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1623
Float_t px
Definition: hprod.C:33
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
#define gPad
Definition: TVirtualPad.h:288
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:287