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