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

Namespaces

 hsum
 

Detailed Description

View in nbviewer Open in SWAN Simple example illustrating how to use the C++ interpreter

pict1_hsum.py.png
1 
2 from ROOT import TCanvas, TH1F, TSlider
3 from ROOT import gROOT, gBenchmark, gRandom
4 
5 # Create a new canvas, and customize it.
6 c1 = TCanvas( 'c1', 'The HSUM example', 200, 10, 600, 400 )
7 c1.SetGrid();
8 
9 gBenchmark.Start( 'hsum' )
10 
11 # Create some histograms.
12 total = TH1F( 'total', 'This is the total distribution', 100, -4, 4 )
13 main = TH1F( 'main', 'Main contributor', 100, -4, 4 )
14 s1 = TH1F( 's1', 'This is the first signal', 100, -4, 4 )
15 s2 = TH1F( 's2', 'This is the second signal', 100, -4, 4 )
16 total.Sumw2() # this makes sure that the sum of squares of weights will be stored
17 
18 # Set canvas/frame attributes.
19 total.SetMarkerStyle( 21 )
20 total.SetMarkerSize( 0.7 )
21 main.SetFillColor( 16 )
22 s1.SetFillColor( 42 )
23 s2.SetFillColor( 46 )
24 
25 # Initialize random number generator.
26 gRandom.SetSeed()
27 gauss, landau = gRandom.Gaus, gRandom.Landau
28 
29 # for speed, bind and cache the Fill member functions
30 histos = [ 'total', 'main', 's1', 's2' ]
31 for name in histos:
32  exec('%sFill = %s.Fill' % (name,name))
33 
34 # Fill histograms randomly
35 kUPDATE = 500
36 for i in range( 10000 ):
37  # Generate random values.
38  xmain = gauss( -1, 1.5 )
39  xs1 = gauss( -0.5, 0.5 )
40  xs2 = landau( 1, 0.15 )
41  mainFill( xmain )
42 
43  # Fill histograms.
44  s1Fill( xs1, 0.3 )
45  s2Fill( xs2, 0.2 )
46  totalFill( xmain )
47  totalFill( xs1, 0.3 )
48  totalFill( xs2, 0.2 )
49 
50  # Update display every kUPDATE events.
51  if i and (i%kUPDATE) == 0 :
52  if i == kUPDATE :
53  total.Draw( 'e1p' )
54  main.Draw( 'same' )
55  s1.Draw( 'same' )
56  s2.Draw( 'same' )
57  c1.Update()
58  slider = TSlider( 'slider', 'test', 4.2, 0, 4.6, total.GetMaximum(), 38 )
59  slider.SetFillColor( 46 )
60 
61  if slider:
62  slider.SetRange( 0, float(i) / 10000. )
63 
64  c1.Modified()
65  c1.Update()
66 
67 # Destroy member functions cache.
68 for name in histos:
69  exec('del %sFill' % name)
70 del histos
71 
72 # Done, finalized and trigger an update.
73 slider.SetRange( 0, 1 )
74 total.Draw( 'sameaxis' ) # to redraw axis hidden by the fill area
75 c1.Modified()
76 c1.Update()
77 
78 gBenchmark.Show( 'hsum' )
Author
Wim Lavrijsen

Definition in file hsum.py.