Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree201_histograms.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 tree201_histograms.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
21{
22 // Create a Tree with a few branches of type histogram
23 // 25000 entries are filled in the Tree
24 // For each entry, the copy of 3 histograms is written
25 // The data base will contain 75000 histograms.
26 gBenchmark->Start("write_histograms");
27 TFile f("ht.root", "recreate");
28 auto T = new TTree("T", "test");
29 auto hpx = new TH1F("hpx", "This is the px distribution", 100, -4, 4);
30 auto hpxpy = new TH2F("hpxpy", "py vs px", 40, -4, 4, 40, -4, 4);
31 auto hprof = new TProfile("hprof", "Profile of pz versus px", 100, -4, 4, 0, 20);
32 T->Branch("hpx", "TH1F", &hpx, 32000, 0);
33 T->Branch("hpxpy", "TH2F", &hpxpy, 32000, 0);
34 T->Branch("hprof", "TProfile", &hprof, 32000, 0);
35 Float_t px, py, pz;
36 for (Int_t i = 0; i < 25000; i++) {
37 if (i % 1000 == 0)
38 printf("at entry: %d\n", i);
39 gRandom->Rannor(px, py);
40 pz = px * px + py * py;
41 hpx->Fill(px);
42 hpxpy->Fill(px ,py);
43 hprof->Fill(px, pz);
44 T->Fill();
45 }
46 T->Print();
47 f.Write();
48 gBenchmark->Show("write_histograms");
49}
50
51void read_histogram1()
52{
53 // Connect Tree generated by htw and show histograms for entry 12345
54 auto f = TFile::Open("ht.root");
55 auto T = f->Get<TTree>("T");
56 TH1F *hpx = nullptr;
57 TH2F *hpxpy = nullptr;
58 TProfile *hprof = nullptr;
59 T->SetBranchAddress("hpx", &hpx);
60 T->SetBranchAddress("hpxpy", &hpxpy);
61 T->SetBranchAddress("hprof", &hprof);
62 T->GetEntry(12345);
63 auto c1 = new TCanvas("c1", "test", 10, 10, 600, 1000);
64 c1->Divide(1, 3);
65 c1->cd(1);
66 hpx->Draw();
67 c1->cd(2);
68 hpxpy->Draw();
69 c1->cd(3);
70 hprof->Draw();
71 c1->Print("htr1.png");
72}
73
74void read_histogram2()
75{
76 // Connect Tree generated by htw and show histograms for entry 12345
77 // a variant of read_histogram1
78 auto f = TFile::Open("ht.root");
79 auto T = f->Get<TTree>("T");
80 auto c1 = new TCanvas("c1", "test", 10, 10, 600, 1000);
81 c1->Divide(1, 3);
82 c1->cd(1);
83 T->Draw("hpx.Draw()", "", "goff", 1, 12345);
84 c1->cd(2);
85 T->Draw("hpxpy.Draw()", "", "goff", 1, 12345);
86 c1->cd(3);
87 T->Draw("hprof.Draw()", "", "goff", 1, 12345);
88 c1->Print("htr2.png");
89}
90
91void read_histogram3()
92{
93 // Connect Tree generated by htw
94 // read all histograms and plot the RMS of hpx versus the Mean of hprof
95 // for each of the 25000 entries
96 auto f = TFile::Open("ht.root");
97 auto T = f->Get<TTree>("T");
98 auto c1 = new TCanvas("c1", "test", 10, 10, 600, 400);
99 T->Draw("hpx.GetRMS():hprof.GetMean()");
100 c1->Print("htr3.png");
101}
102
104{
109}
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
R__EXTERN TBenchmark * gBenchmark
Definition TBenchmark.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
virtual void Start(const char *name)
Starts Benchmark with the specified name.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
The Canvas class.
Definition TCanvas.h:23
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4130
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:647
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
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:507
A TTree represents a columnar dataset.
Definition TTree.h:79
return c1
Definition legend1.C:41