ROOT
v6-36
Reference Guide
Loading...
Searching...
No Matches
tree201_histograms.C File Reference
Tutorials
»
IO tutorials
»
TTree tutorials
Detailed Description
Save histograms in Tree branches
To run this example, do
root > .L
tree201_histograms
.C
root >
htw
()
root >
htr1
()
root >
htr2
()
root >
htr3
()
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
void
write_histograms
()
{
// Create a Tree with a few branches of type histogram
// 25000 entries are filled in the Tree
// For each entry, the copy of 3 histograms is written
// The data base will contain 75000 histograms.
gBenchmark
->
Start
(
"write_histograms"
);
TFile
f
(
"ht.root"
,
"recreate"
);
auto
T =
new
TTree
(
"T"
,
"test"
);
auto
hpx
=
new
TH1F
(
"hpx"
,
"This is the px distribution"
, 100, -4, 4);
auto
hpxpy
=
new
TH2F
(
"hpxpy"
,
"py vs px"
, 40, -4, 4, 40, -4, 4);
auto
hprof
=
new
TProfile
(
"hprof"
,
"Profile of pz versus px"
, 100, -4, 4, 0, 20);
T->Branch(
"hpx"
,
"TH1F"
, &
hpx
, 32000, 0);
T->Branch(
"hpxpy"
,
"TH2F"
, &
hpxpy
, 32000, 0);
T->Branch(
"hprof"
,
"TProfile"
, &
hprof
, 32000, 0);
Float_t
px, py, pz;
for
(
Int_t
i = 0; i < 25000; i++) {
if
(i % 1000 == 0)
printf
(
"at entry: %d\n"
, i);
gRandom
->
Rannor
(px, py);
pz = px * px + py * py;
hpx
->Fill(px);
hpxpy
->Fill(px ,py);
hprof
->Fill(px, pz);
T->Fill();
}
T->Print();
f
.Write();
gBenchmark
->
Show
(
"write_histograms"
);
}
void
read_histogram1
()
{
// Connect Tree generated by htw and show histograms for entry 12345
auto
f
=
TFile::Open
(
"ht.root"
);
auto
T =
f
->Get<
TTree
>(
"T"
);
TH1F
*
hpx
=
nullptr
;
TH2F
*
hpxpy
=
nullptr
;
TProfile
*
hprof
=
nullptr
;
T->SetBranchAddress(
"hpx"
, &
hpx
);
T->SetBranchAddress(
"hpxpy"
, &
hpxpy
);
T->SetBranchAddress(
"hprof"
, &
hprof
);
T->GetEntry(12345);
auto
c1
=
new
TCanvas
(
"c1"
,
"test"
, 10, 10, 600, 1000);
c1
->Divide(1, 3);
c1
->cd(1);
hpx
->Draw();
c1
->cd(2);
hpxpy
->Draw();
c1
->cd(3);
hprof
->Draw();
c1
->Print(
"htr1.png"
);
}
void
read_histogram2
()
{
// Connect Tree generated by htw and show histograms for entry 12345
// a variant of read_histogram1
auto
f
=
TFile::Open
(
"ht.root"
);
auto
T =
f
->Get<
TTree
>(
"T"
);
auto
c1
=
new
TCanvas
(
"c1"
,
"test"
, 10, 10, 600, 1000);
c1
->Divide(1, 3);
c1
->cd(1);
T->Draw(
"hpx.Draw()"
,
""
,
"goff"
, 1, 12345);
c1
->cd(2);
T->Draw(
"hpxpy.Draw()"
,
""
,
"goff"
, 1, 12345);
c1
->cd(3);
T->Draw(
"hprof.Draw()"
,
""
,
"goff"
, 1, 12345);
c1
->Print(
"htr2.png"
);
}
void
read_histogram3
()
{
// Connect Tree generated by htw
// read all histograms and plot the RMS of hpx versus the Mean of hprof
// for each of the 25000 entries
auto
f
=
TFile::Open
(
"ht.root"
);
auto
T =
f
->Get<
TTree
>(
"T"
);
auto
c1
=
new
TCanvas
(
"c1"
,
"test"
, 10, 10, 600, 400);
T->Draw(
"hpx.GetRMS():hprof.GetMean()"
);
c1
->Print(
"htr3.png"
);
}
void
tree201_histograms
()
{
write_histograms
();
read_histogram1
();
read_histogram2
();
read_histogram3
();
}
f
#define f(i)
Definition
RSha256.hxx:104
Int_t
int Int_t
Definition
RtypesCore.h:45
Float_t
float Float_t
Definition
RtypesCore.h:57
gBenchmark
R__EXTERN TBenchmark * gBenchmark
Definition
TBenchmark.h:59
gRandom
R__EXTERN TRandom * gRandom
Definition
TRandom.h:62
TBenchmark::Start
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition
TBenchmark.cxx:172
TBenchmark::Show
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition
TBenchmark.cxx:155
TCanvas
The Canvas class.
Definition
TCanvas.h:23
TFile
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition
TFile.h:131
TFile::Open
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:4131
TH1F
1-D histogram with a float per channel (see TH1 documentation)
Definition
TH1.h:877
TH2F
2-D histogram with a float per channel (see TH1 documentation)
Definition
TH2.h:307
TProfile
Profile Histogram.
Definition
TProfile.h:32
TRandom::Rannor
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
TTree
A TTree represents a columnar dataset.
Definition
TTree.h:84
c1
return c1
Definition
legend1.C:41
Author
Rene Brun
Definition in file
tree201_histograms.C
.
tutorials
io
tree
tree201_histograms.C
ROOT v6-36 - Reference Guide Generated on Thu Aug 21 2025 04:30:53 (GVA Time) using Doxygen 1.10.0