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