ROOT
master
Reference Guide
Loading...
Searching...
No Matches
hsimple.py
Go to the documentation of this file.
1
## \file
2
## \ingroup Tutorials
3
## \notebook -js
4
## This program creates :
5
## - a one dimensional histogram
6
## - a two dimensional histogram
7
## - a profile histogram
8
## - a memory-resident ntuple
9
##
10
## These objects are filled with some random numbers and saved on a file.
11
##
12
## \macro_image
13
## \macro_code
14
##
15
## \authors Wim Lavrijsen, Enric Tejedor, Vincenzo Eduardo Padulano (CERN), 09.2025
16
17
import
numpy
18
from
ROOT
import
TH1F, TH2F, TCanvas, TFile, TNtuple, TProfile, gBenchmark, gSystem
19
20
# Create a new canvas, and customize it.
21
c1 =
TCanvas
(
"c1"
,
"Dynamic Filling Example"
, 200, 10, 700, 500)
22
c1.SetFillColor
(42)
23
frame =
c1.GetFrame
()
24
frame.SetFillColor
(21)
25
frame.SetBorderSize
(6)
26
frame.SetBorderMode
(-1)
27
28
# Create some histograms, a profile histogram and an ntuple
29
hpx =
TH1F
(
"hpx"
,
"This is the px distribution"
, 100, -4, 4)
30
hpxpy =
TH2F
(
"hpxpy"
,
"py vs px"
, 40, -4, 4, 40, -4, 4)
31
hprof =
TProfile
(
"hprof"
,
"Profile of pz versus px"
, 100, -4, 4, 0, 20)
32
ntuple =
TNtuple
(
"ntuple"
,
"Demo ntuple"
,
"px:py:pz:random:i"
)
33
34
# Set canvas/frame attributes.
35
hpx.SetFillColor
(48)
36
37
gBenchmark.Start
(
"hsimple"
)
38
39
# Fill histograms randomly.
40
for
i
in
range
(25000):
41
# Retrieve the generated values
42
px, py =
numpy.random.standard_normal
(size=2)
43
44
pz = px * px + py * py
45
random =
numpy.random.rand
()
46
47
# Fill histograms.
48
hpx.Fill
(px)
49
hpxpy.Fill
(px, py)
50
hprof.Fill
(px, pz)
51
ntuple.Fill
(px, py, pz, random, i)
52
53
# Update display every 1000 events.
54
if
i > 0
and
i % 1000 == 0:
55
hpx.Draw
()
56
57
c1.Modified
()
58
c1.Update
()
59
60
if
gSystem.ProcessEvents
():
# allow user interrupt
61
break
62
63
gBenchmark.Show
(
"hsimple"
)
64
65
# Create a new ROOT binary machine independent file.
66
# Note that this file may contain any kind of ROOT objects, histograms,
67
# pictures, graphics objects, detector geometries, tracks, events, etc.
68
with
TFile
(
"py-hsimple.root"
,
"RECREATE"
,
"Demo ROOT file with histograms"
)
as
hfile:
69
# Save all created objects in the file
70
hfile.WriteObject
(hpx)
71
hfile.WriteObject
(hpxpy)
72
hfile.WriteObject
(hprof)
73
# TNTuple is special because it is a TTree-derived class. To make sure all the
74
# dataset is properly written to disk, we connect it to the file and then
75
# we ask the ntuple to write all the information to the file itself.
76
ntuple.SetDirectory
(hfile)
77
ntuple.Write
()
78
79
c1.Modified
()
80
c1.Update
()
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:359
ROOT::Detail::TRangeCast
Definition
TCollection.h:312
TCanvas
The Canvas class.
Definition
TCanvas.h:23
TFile
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition
TFile.h:130
TH1F
1-D histogram with a float per channel (see TH1 documentation)
Definition
TH1.h:878
TH2F
2-D histogram with a float per channel (see TH1 documentation)
Definition
TH2.h:345
TNtuple
A simple TTree restricted to a list of float variables only.
Definition
TNtuple.h:28
TProfile
Profile Histogram.
Definition
TProfile.h:32
tutorials
hsimple.py
ROOTmaster - Reference Guide Generated on Mon Aug 3 2026 15:57:13 (GVA Time) using Doxygen 1.10.0