ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hsimple.py
Go to the documentation of this file.
1 #*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2 #*-*
3 #*-* This program creates :
4 #*-* - a one dimensional histogram
5 #*-* - a two dimensional histogram
6 #*-* - a profile histogram
7 #*-* - a memory-resident ntuple
8 #*-*
9 #*-* These objects are filled with some random numbers and saved on a file.
10 #*-*
11 #*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
12 
13 from ROOT import TCanvas, TFile, TProfile, TNtuple, TH1F, TH2F
14 from ROOT import gROOT, gBenchmark, gRandom, gSystem, Double
15 
16 
17 
18 # Create a new canvas, and customize it.
19 c1 = TCanvas( 'c1', 'Dynamic Filling Example', 200, 10, 700, 500 )
20 c1.SetFillColor( 42 )
21 c1.GetFrame().SetFillColor( 21 )
22 c1.GetFrame().SetBorderSize( 6 )
23 c1.GetFrame().SetBorderMode( -1 )
24 
25 # Create a new ROOT binary machine independent file.
26 # Note that this file may contain any kind of ROOT objects, histograms,
27 # pictures, graphics objects, detector geometries, tracks, events, etc..
28 # This file is now becoming the current directory.
29 
30 hfile = gROOT.FindObject( 'py-hsimple.root' )
31 if hfile:
32  hfile.Close()
33 hfile = TFile( 'py-hsimple.root', 'RECREATE', 'Demo ROOT file with histograms' )
34 
35 # Create some histograms, a profile histogram and an ntuple
36 hpx = TH1F( 'hpx', 'This is the px distribution', 100, -4, 4 )
37 hpxpy = TH2F( 'hpxpy', 'py vs px', 40, -4, 4, 40, -4, 4 )
38 hprof = TProfile( 'hprof', 'Profile of pz versus px', 100, -4, 4, 0, 20 )
39 ntuple = TNtuple( 'ntuple', 'Demo ntuple', 'px:py:pz:random:i' )
40 
41 # Set canvas/frame attributes.
42 hpx.SetFillColor( 48 )
43 
44 gBenchmark.Start( 'hsimple' )
45 
46 # Initialize random number generator.
47 gRandom.SetSeed()
48 rannor, rndm = gRandom.Rannor, gRandom.Rndm
49 
50 # For speed, bind and cache the Fill member functions,
51 histos = [ 'hpx', 'hpxpy', 'hprof', 'ntuple' ]
52 for name in histos:
53  exec '%sFill = %s.Fill' % (name,name)
54 
55 # Fill histograms randomly.
56 px, py = Double(), Double()
57 kUPDATE = 1000
58 for i in xrange( 25000 ):
59  # Generate random values.
60  rannor( px, py )
61  pz = px*px + py*py
62  random = rndm(1)
63 
64  # Fill histograms.
65  hpxFill( px )
66  hpxpyFill( px, py )
67  hprofFill( px, pz )
68  ntupleFill( px, py, pz, random, i )
69 
70  # Update display every kUPDATE events.
71  if i and i%kUPDATE == 0:
72  if i == kUPDATE:
73  hpx.Draw()
74 
75  c1.Modified()
76  c1.Update()
77 
78  if gSystem.ProcessEvents(): # allow user interrupt
79  break
80 
81 # Destroy member functions cache.
82 for name in histos:
83  exec 'del %sFill' % name
84 del histos
85 
86 gBenchmark.Show( 'hsimple' )
87 
88 # Save all objects in this file.
89 hpx.SetFillColor( 0 )
90 hfile.Write()
91 hpx.SetFillColor( 48 )
92 c1.Modified()
93 c1.Update()
94 
95 # Note that the file is automatically closed when application terminates
96 # or when the file destructor is called.
c SetBorderSize(2)
h1 SetFillColor(kGreen)
const char * Double
TRandom3 rndm
Definition: fit2dHist.C:44