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

Namespaces

 fillrandom
 

Detailed Description

View in nbviewer Open in SWAN FillRandom example

pict1_fillrandom.py.png
fillrandom: Real Time = 0.54 seconds Cpu Time = 0.25 seconds
1 
2 from ROOT import TCanvas, TPad, TFormula, TF1, TPaveLabel, TH1F, TFile
3 from ROOT import gROOT, gBenchmark
4 
5 
6 
7 c1 = TCanvas( 'c1', 'The FillRandom example', 200, 10, 700, 900 )
8 c1.SetFillColor( 18 )
9 
10 pad1 = TPad( 'pad1', 'The pad with the function', 0.05, 0.50, 0.95, 0.95, 21 )
11 pad2 = TPad( 'pad2', 'The pad with the histogram', 0.05, 0.05, 0.95, 0.45, 21 )
12 pad1.Draw()
13 pad2.Draw()
14 pad1.cd()
15 
16 gBenchmark.Start( 'fillrandom' )
17 #
18 # A function (any dimension) or a formula may reference
19 # an already defined formula
20 #
21 form1 = TFormula( 'form1', 'abs(sin(x)/x)' )
22 sqroot = TF1( 'sqroot', 'x*gaus(0) + [3]*form1', 0, 10 )
23 sqroot.SetParameters( 10, 4, 1, 20 )
24 pad1.SetGridx()
25 pad1.SetGridy()
26 pad1.GetFrame().SetFillColor( 42 )
27 pad1.GetFrame().SetBorderMode( -1 )
28 pad1.GetFrame().SetBorderSize( 5 )
29 sqroot.SetLineColor( 4 )
30 sqroot.SetLineWidth( 6 )
31 sqroot.Draw()
32 lfunction = TPaveLabel( 5, 39, 9.8, 46, 'The sqroot function' )
33 lfunction.SetFillColor( 41 )
34 lfunction.Draw()
35 c1.Update()
36 
37 #
38 # Create a one dimensional histogram (one float per bin)
39 # and fill it following the distribution in function sqroot.
40 #
41 pad2.cd();
42 pad2.GetFrame().SetFillColor( 42 )
43 pad2.GetFrame().SetBorderMode( -1 )
44 pad2.GetFrame().SetBorderSize( 5 )
45 h1f = TH1F( 'h1f', 'Test random numbers', 200, 0, 10 )
46 h1f.SetFillColor( 45 )
47 h1f.FillRandom( 'sqroot', 10000 )
48 h1f.Draw()
49 c1.Update()
50 #
51 # Open a ROOT file and save the formula, function and histogram
52 #
53 myfile = TFile( 'py-fillrandom.root', 'RECREATE' )
54 form1.Write()
55 sqroot.Write()
56 h1f.Write()
57 myfile.Close()
58 gBenchmark.Show( 'fillrandom' )
Author
Wim Lavrijsen

Definition in file fillrandom.py.