ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fillrandom.py
Go to the documentation of this file.
1 #
2 # To see the output of this macro, click begin_html <a href="gif/fillrandom.gif">here</a>. end_html
3 #
4 
5 from ROOT import TCanvas, TPad, TFormula, TF1, TPaveLabel, TH1F, TFile
6 from ROOT import gROOT, gBenchmark
7 
8 
9 
10 c1 = TCanvas( 'c1', 'The FillRandom example', 200, 10, 700, 900 )
11 c1.SetFillColor( 18 )
12 
13 pad1 = TPad( 'pad1', 'The pad with the function', 0.05, 0.50, 0.95, 0.95, 21 )
14 pad2 = TPad( 'pad2', 'The pad with the histogram', 0.05, 0.05, 0.95, 0.45, 21 )
15 pad1.Draw()
16 pad2.Draw()
17 pad1.cd()
18 
19 gBenchmark.Start( 'fillrandom' )
20 #
21 # A function (any dimension) or a formula may reference
22 # an already defined formula
23 #
24 form1 = TFormula( 'form1', 'abs(sin(x)/x)' )
25 sqroot = TF1( 'sqroot', 'x*gaus(0) + [3]*form1', 0, 10 )
26 sqroot.SetParameters( 10, 4, 1, 20 )
27 pad1.SetGridx()
28 pad1.SetGridy()
29 pad1.GetFrame().SetFillColor( 42 )
30 pad1.GetFrame().SetBorderMode( -1 )
31 pad1.GetFrame().SetBorderSize( 5 )
32 sqroot.SetLineColor( 4 )
33 sqroot.SetLineWidth( 6 )
34 sqroot.Draw()
35 lfunction = TPaveLabel( 5, 39, 9.8, 46, 'The sqroot function' )
36 lfunction.SetFillColor( 41 )
37 lfunction.Draw()
38 c1.Update()
39 
40 #
41 # Create a one dimensional histogram (one float per bin)
42 # and fill it following the distribution in function sqroot.
43 #
44 pad2.cd();
45 pad2.GetFrame().SetFillColor( 42 )
46 pad2.GetFrame().SetBorderMode( -1 )
47 pad2.GetFrame().SetBorderSize( 5 )
48 h1f = TH1F( 'h1f', 'Test random numbers', 200, 0, 10 )
49 h1f.SetFillColor( 45 )
50 h1f.FillRandom( 'sqroot', 10000 )
51 h1f.Draw()
52 c1.Update()
53 #
54 # Open a ROOT file and save the formula, function and histogram
55 #
56 myfile = TFile( 'py-fillrandom.root', 'RECREATE' )
57 form1.Write()
58 sqroot.Write()
59 h1f.Write()
60 myfile.Close()
61 gBenchmark.Show( 'fillrandom' )
c SetBorderSize(2)
h1 SetFillColor(kGreen)