Logo ROOT   6.12/07
Reference Guide
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 
11 from ROOT import TCanvas, TH1F, TSlider
12 from ROOT import gROOT, gBenchmark, gRandom
13 
14 # Create a new canvas, and customize it.
15 c1 = TCanvas( 'c1', 'The HSUM example', 200, 10, 600, 400 )
16 c1.SetGrid();
17 
18 gBenchmark.Start( 'hsum' )
19 
20 # Create some histograms.
21 total = TH1F( 'total', 'This is the total distribution', 100, -4, 4 )
22 main = TH1F( 'main', 'Main contributor', 100, -4, 4 )
23 s1 = TH1F( 's1', 'This is the first signal', 100, -4, 4 )
24 s2 = TH1F( 's2', 'This is the second signal', 100, -4, 4 )
25 total.Sumw2() # this makes sure that the sum of squares of weights will be stored
26 
27 # Set canvas/frame attributes.
28 total.SetMarkerStyle( 21 )
29 total.SetMarkerSize( 0.7 )
30 main.SetFillColor( 16 )
31 s1.SetFillColor( 42 )
32 s2.SetFillColor( 46 )
33 
34 # Initialize random number generator.
35 gRandom.SetSeed()
36 gauss, landau = gRandom.Gaus, gRandom.Landau
37 
38 # for speed, bind and cache the Fill member functions
39 histos = [ 'total', 'main', 's1', 's2' ]
40 for name in histos:
41  exec('%sFill = %s.Fill' % (name,name))
42 
43 # Fill histograms randomly
44 kUPDATE = 500
45 for 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.
77 for name in histos:
78  exec('del %sFill' % name)
79 del histos
80 
81 # Done, finalized and trigger an update.
82 slider.SetRange( 0, 1 )
83 total.Draw( 'sameaxis' ) # to redraw axis hidden by the fill area
84 c1.Modified()
85 c1.Update()
86 
87 gBenchmark.Show( 'hsum' )
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
A specialized TPad including a TSliderBox object.
Definition: TSlider.h:18
The Canvas class.
Definition: TCanvas.h:31
static constexpr double gauss