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 
17 from ROOT import TCanvas, TFile, TProfile, TNtuple, TH1F, TH2F
18 from ROOT import gROOT, gBenchmark, gRandom, gSystem
19 import ctypes
20 
21 # Create a new canvas, and customize it.
22 c1 = TCanvas( 'c1', 'Dynamic Filling Example', 200, 10, 700, 500 )
23 c1.SetFillColor( 42 )
24 c1.GetFrame().SetFillColor( 21 )
25 c1.GetFrame().SetBorderSize( 6 )
26 c1.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 
33 hfile = gROOT.FindObject( 'py-hsimple.root' )
34 if hfile:
35  hfile.Close()
36 hfile = TFile( 'py-hsimple.root', 'RECREATE', 'Demo ROOT file with histograms' )
37 
38 # Create some histograms, a profile histogram and an ntuple
39 hpx = TH1F( 'hpx', 'This is the px distribution', 100, -4, 4 )
40 hpxpy = TH2F( 'hpxpy', 'py vs px', 40, -4, 4, 40, -4, 4 )
41 hprof = TProfile( 'hprof', 'Profile of pz versus px', 100, -4, 4, 0, 20 )
42 ntuple = TNtuple( 'ntuple', 'Demo ntuple', 'px:py:pz:random:i' )
43 
44 # Set canvas/frame attributes.
45 hpx.SetFillColor( 48 )
46 
47 gBenchmark.Start( 'hsimple' )
48 
49 # Initialize random number generator.
50 gRandom.SetSeed()
51 rannor, rndm = gRandom.Rannor, gRandom.Rndm
52 
53 # For speed, bind and cache the Fill member functions,
54 histos = [ 'hpx', 'hpxpy', 'hprof', 'ntuple' ]
55 for name in histos:
56  exec('%sFill = %s.Fill' % (name,name))
57 
58 # Fill histograms randomly.
59 px_ref, py_ref = ctypes.c_double(), ctypes.c_double()
60 kUPDATE = 1000
61 for 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.
89 for name in histos:
90  exec('del %sFill' % name)
91 del histos
92 
93 gBenchmark.Show( 'hsimple' )
94 
95 # Save all objects in this file.
96 hpx.SetFillColor( 0 )
97 hfile.Write()
98 hpx.SetFillColor( 48 )
99 c1.Modified()
100 c1.Update()
101 
102 # Note that the file is automatically closed when application terminates
103 # or when the file destructor is called.
TNtuple
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
SetBorderSize
c SetBorderSize(2)
TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
SetFillColor
h1 SetFillColor(kGreen)
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
TProfile
Profile Histogram.
Definition: TProfile.h:32
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:573