Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
fillrandom.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_pyroot
3## \notebook
4## FillRandom example
5##
6## \macro_image
7## \macro_output
8## \macro_code
9##
10## \author Wim Lavrijsen
11
12from ROOT import TCanvas, TPad, TFormula, TF1, TPaveLabel, TH1F, TFile
13from ROOT import gROOT, gBenchmark
14
15
16
17c1 = TCanvas( 'c1', 'The FillRandom example', 200, 10, 700, 900 )
18c1.SetFillColor( 18 )
19
20pad1 = TPad( 'pad1', 'The pad with the function', 0.05, 0.50, 0.95, 0.95, 21 )
21pad2 = TPad( 'pad2', 'The pad with the histogram', 0.05, 0.05, 0.95, 0.45, 21 )
22pad1.Draw()
23pad2.Draw()
24pad1.cd()
25
26gBenchmark.Start( 'fillrandom' )
27#
28# A function (any dimension) or a formula may reference
29# an already defined formula
30#
31form1 = TFormula( 'form1', 'abs(sin(x)/x)' )
32sqroot = TF1( 'sqroot', 'x*gaus(0) + [3]*form1', 0, 10 )
33sqroot.SetParameters( 10, 4, 1, 20 )
34pad1.SetGridx()
35pad1.SetGridy()
36pad1.GetFrame().SetFillColor( 42 )
37pad1.GetFrame().SetBorderMode( -1 )
38pad1.GetFrame().SetBorderSize( 5 )
39sqroot.SetLineColor( 4 )
40sqroot.SetLineWidth( 6 )
41sqroot.Draw()
42lfunction = TPaveLabel( 5, 39, 9.8, 46, 'The sqroot function' )
43lfunction.SetFillColor( 41 )
44lfunction.Draw()
45c1.Update()
46
47#
48# Create a one dimensional histogram (one float per bin)
49# and fill it following the distribution in function sqroot.
50#
51pad2.cd();
52pad2.GetFrame().SetFillColor( 42 )
53pad2.GetFrame().SetBorderMode( -1 )
54pad2.GetFrame().SetBorderSize( 5 )
55h1f = TH1F( 'h1f', 'Test random numbers', 200, 0, 10 )
56h1f.SetFillColor( 45 )
57h1f.FillRandom( 'sqroot', 10000 )
58h1f.Draw()
59c1.Update()
60#
61# Open a ROOT file and save the formula, function and histogram
62#
63myfile = TFile( 'py-fillrandom.root', 'RECREATE' )
64form1.Write()
65sqroot.Write()
66h1f.Write()
67myfile.Close()
68gBenchmark.Show( 'fillrandom' )
Option_t Option_t SetFillColor
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:233
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
The Formula class.
Definition TFormula.h:89
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
The most important graphics class in the ROOT system.
Definition TPad.h:28
A Pave (see TPave) with a text centered in the Pave.
Definition TPaveLabel.h:20
c SetBorderSize(2)