Logo ROOT  
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
17import numpy
18from ROOT import TH1F, TH2F, TCanvas, TFile, TNtuple, TProfile, gBenchmark, gSystem
19
20# Create a new canvas, and customize it.
21c1 = TCanvas("c1", "Dynamic Filling Example", 200, 10, 700, 500)
23frame = c1.GetFrame()
27
28# Create some histograms, a profile histogram and an ntuple
29hpx = TH1F("hpx", "This is the px distribution", 100, -4, 4)
30hpxpy = TH2F("hpxpy", "py vs px", 40, -4, 4, 40, -4, 4)
31hprof = TProfile("hprof", "Profile of pz versus px", 100, -4, 4, 0, 20)
32ntuple = TNtuple("ntuple", "Demo ntuple", "px:py:pz:random:i")
33
34# Set canvas/frame attributes.
36
37gBenchmark.Start("hsimple")
38
39# Fill histograms randomly.
40for 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
58 c1.Update()
59
60 if gSystem.ProcessEvents(): # allow user interrupt
61 break
62
63gBenchmark.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.
68with TFile("py-hsimple.root", "RECREATE", "Demo ROOT file with histograms") as hfile:
69 # Save all created objects in the file
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.
78
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
The Canvas class.
Definition TCanvas.h:23
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Profile Histogram.
Definition TProfile.h:32