ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hsum.py
Go to the documentation of this file.
1 #
2 # To see the output of this macro, click begin_html <a href="gif/hsum.gif" >here</a> end_html
3 # Simple example illustrating how to use the C++ interpreter
4 # to fill histograms in a loop and show the graphics results
5 #
6 
7 from ROOT import TCanvas, TH1F, TSlider
8 from ROOT import gROOT, gBenchmark, gRandom
9 
10 
11 
12 # Create a new canvas, and customize it.
13 c1 = TCanvas( 'c1', 'The HSUM example', 200, 10, 600, 400 )
14 c1.SetGrid();
15 
16 gBenchmark.Start( 'hsum' )
17 
18 # Create some histograms.
19 total = TH1F( 'total', 'This is the total distribution', 100, -4, 4 )
20 main = TH1F( 'main', 'Main contributor', 100, -4, 4 )
21 s1 = TH1F( 's1', 'This is the first signal', 100, -4, 4 )
22 s2 = TH1F( 's2', 'This is the second signal', 100, -4, 4 )
23 total.Sumw2() # this makes sure that the sum of squares of weights will be stored
24 
25 # Set canvas/frame attributes.
26 total.SetMarkerStyle( 21 )
27 total.SetMarkerSize( 0.7 )
28 main.SetFillColor( 16 )
29 s1.SetFillColor( 42 )
30 s2.SetFillColor( 46 )
31 
32 # Initialize random number generator.
33 gRandom.SetSeed()
34 gauss, landau = gRandom.Gaus, gRandom.Landau
35 
36 # for speed, bind and cache the Fill member functions
37 histos = [ 'total', 'main', 's1', 's2' ]
38 for name in histos:
39  exec '%sFill = %s.Fill' % (name,name)
40 
41 # Fill histograms randomly
42 kUPDATE = 500
43 for i in xrange( 10000 ):
44  # Generate random values.
45  xmain = gauss( -1, 1.5 )
46  xs1 = gauss( -0.5, 0.5 )
47  xs2 = landau( 1, 0.15 )
48  mainFill( xmain )
49 
50  # Fill histograms.
51  s1Fill( xs1, 0.3 )
52  s2Fill( xs2, 0.2 )
53  totalFill( xmain )
54  totalFill( xs1, 0.3 )
55  totalFill( xs2, 0.2 )
56 
57  # Update display every kUPDATE events.
58  if i and (i%kUPDATE) == 0 :
59  if i == kUPDATE :
60  total.Draw( 'e1p' )
61  main.Draw( 'same' )
62  s1.Draw( 'same' )
63  s2.Draw( 'same' )
64  c1.Update()
65  slider = TSlider( 'slider', 'test', 4.2, 0, 4.6, total.GetMaximum(), 38 )
66  slider.SetFillColor( 46 )
67 
68  if slider:
69  slider.SetRange( 0, float(i) / 10000. )
70 
71  c1.Modified()
72  c1.Update()
73 
74 # Destroy member functions cache.
75 for name in histos:
76  exec 'del %sFill' % name
77 del histos
78 
79 # Done, finalized and trigger an update.
80 slider.SetRange( 0, 1 )
81 total.Draw( 'sameaxis' ) # to redraw axis hidden by the fill area
82 c1.Modified()
83 c1.Update()
84 
85 gBenchmark.Show( 'hsum' )