Logo ROOT   6.16/01
Reference Guide
hsimple.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_pyroot
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## \author Wim Lavrijsen
16
17from ROOT import TCanvas, TFile, TProfile, TNtuple, TH1F, TH2F
18from ROOT import gROOT, gBenchmark, gRandom, gSystem, Double
19
20# Create a new canvas, and customize it.
21c1 = TCanvas( 'c1', 'Dynamic Filling Example', 200, 10, 700, 500 )
22c1.SetFillColor( 42 )
23c1.GetFrame().SetFillColor( 21 )
24c1.GetFrame().SetBorderSize( 6 )
25c1.GetFrame().SetBorderMode( -1 )
26
27# Create a new ROOT binary machine independent file.
28# Note that this file may contain any kind of ROOT objects, histograms,
29# pictures, graphics objects, detector geometries, tracks, events, etc..
30# This file is now becoming the current directory.
31
32hfile = gROOT.FindObject( 'py-hsimple.root' )
33if hfile:
34 hfile.Close()
35hfile = TFile( 'py-hsimple.root', 'RECREATE', 'Demo ROOT file with histograms' )
36
37# Create some histograms, a profile histogram and an ntuple
38hpx = TH1F( 'hpx', 'This is the px distribution', 100, -4, 4 )
39hpxpy = TH2F( 'hpxpy', 'py vs px', 40, -4, 4, 40, -4, 4 )
40hprof = TProfile( 'hprof', 'Profile of pz versus px', 100, -4, 4, 0, 20 )
41ntuple = TNtuple( 'ntuple', 'Demo ntuple', 'px:py:pz:random:i' )
42
43# Set canvas/frame attributes.
44hpx.SetFillColor( 48 )
45
46gBenchmark.Start( 'hsimple' )
47
48# Initialize random number generator.
49gRandom.SetSeed()
50rannor, rndm = gRandom.Rannor, gRandom.Rndm
51
52# For speed, bind and cache the Fill member functions,
53histos = [ 'hpx', 'hpxpy', 'hprof', 'ntuple' ]
54for name in histos:
55 exec('%sFill = %s.Fill' % (name,name))
56
57# Fill histograms randomly.
58px, py = Double(), Double()
59kUPDATE = 1000
60for i in range( 25000 ):
61 # Generate random values.
62 rannor( px, py )
63 pz = px*px + py*py
64 random = rndm(1)
65
66 # Fill histograms.
67 hpx.Fill( px )
68 hpxpy.Fill( px, py )
69 hprof.Fill( px, pz )
70 ntuple.Fill( px, py, pz, random, i )
71
72 # Update display every kUPDATE events.
73 if i and i%kUPDATE == 0:
74 if i == kUPDATE:
75 hpx.Draw()
76
77 c1.Modified()
78 c1.Update()
79
80 if gSystem.ProcessEvents(): # allow user interrupt
81 break
82
83# Destroy member functions cache.
84for name in histos:
85 exec('del %sFill' % name)
86del histos
87
88gBenchmark.Show( 'hsimple' )
89
90# Save all objects in this file.
91hpx.SetFillColor( 0 )
92hfile.Write()
93hpx.SetFillColor( 48 )
94c1.Modified()
95c1.Update()
96
97# Note that the file is automatically closed when application terminates
98# or when the file destructor is called.
The Canvas class.
Definition: TCanvas.h:31
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:250
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
Profile Histogram.
Definition: TProfile.h:32
c SetBorderSize(2)
h1 SetFillColor(kGreen)
const char * Double