Logo ROOT   6.07/09
Reference Guide
Namespaces
hsimple.py File Reference

Namespaces

 hsimple
 

Detailed Description

View in nbviewer Open in SWAN This program creates :

These objects are filled with some random numbers and saved on a file.

pict1_hsimple.py.png
1 
2 from ROOT import TCanvas, TFile, TProfile, TNtuple, TH1F, TH2F
3 from ROOT import gROOT, gBenchmark, gRandom, gSystem, Double
4 
5 # Create a new canvas, and customize it.
6 c1 = TCanvas( 'c1', 'Dynamic Filling Example', 200, 10, 700, 500 )
7 c1.SetFillColor( 42 )
8 c1.GetFrame().SetFillColor( 21 )
9 c1.GetFrame().SetBorderSize( 6 )
10 c1.GetFrame().SetBorderMode( -1 )
11 
12 # Create a new ROOT binary machine independent file.
13 # Note that this file may contain any kind of ROOT objects, histograms,
14 # pictures, graphics objects, detector geometries, tracks, events, etc..
15 # This file is now becoming the current directory.
16 
17 hfile = gROOT.FindObject( 'py-hsimple.root' )
18 if hfile:
19  hfile.Close()
20 hfile = TFile( 'py-hsimple.root', 'RECREATE', 'Demo ROOT file with histograms' )
21 
22 # Create some histograms, a profile histogram and an ntuple
23 hpx = TH1F( 'hpx', 'This is the px distribution', 100, -4, 4 )
24 hpxpy = TH2F( 'hpxpy', 'py vs px', 40, -4, 4, 40, -4, 4 )
25 hprof = TProfile( 'hprof', 'Profile of pz versus px', 100, -4, 4, 0, 20 )
26 ntuple = TNtuple( 'ntuple', 'Demo ntuple', 'px:py:pz:random:i' )
27 
28 # Set canvas/frame attributes.
29 hpx.SetFillColor( 48 )
30 
31 gBenchmark.Start( 'hsimple' )
32 
33 # Initialize random number generator.
34 gRandom.SetSeed()
35 rannor, rndm = gRandom.Rannor, gRandom.Rndm
36 
37 # For speed, bind and cache the Fill member functions,
38 histos = [ 'hpx', 'hpxpy', 'hprof', 'ntuple' ]
39 for name in histos:
40  exec('%sFill = %s.Fill' % (name,name))
41 
42 # Fill histograms randomly.
43 px, py = Double(), Double()
44 kUPDATE = 1000
45 for i in range( 25000 ):
46  # Generate random values.
47  rannor( px, py )
48  pz = px*px + py*py
49  random = rndm(1)
50 
51  # Fill histograms.
52  hpx.Fill( px )
53  hpxpy.Fill( px, py )
54  hprof.Fill( px, pz )
55  ntuple.Fill( px, py, pz, random, i )
56 
57  # Update display every kUPDATE events.
58  if i and i%kUPDATE == 0:
59  if i == kUPDATE:
60  hpx.Draw()
61 
62  c1.Modified()
63  c1.Update()
64 
65  if gSystem.ProcessEvents(): # allow user interrupt
66  break
67 
68 # Destroy member functions cache.
69 for name in histos:
70  exec('del %sFill' % name)
71 del histos
72 
73 gBenchmark.Show( 'hsimple' )
74 
75 # Save all objects in this file.
76 hpx.SetFillColor( 0 )
77 hfile.Write()
78 hpx.SetFillColor( 48 )
79 c1.Modified()
80 c1.Update()
81 
82 # Note that the file is automatically closed when application terminates
83 # or when the file destructor is called.
Author
Wim Lavrijsen

Definition in file hsimple.py.