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