ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fillrandom.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Fill a 1-D histogram from a parametric function.
4 ///
5 /// \macro_image
6 /// \macro_code
7 ///
8 /// \author Rene Brun
9 
10 void fillrandom() {
11  TCanvas *c1 = new TCanvas("c1","The FillRandom example",200,10,700,900);
12  c1->SetFillColor(18);
13 
14  auto pad1 = new TPad("pad1","The pad with the function",0.05,0.50,0.95,0.95,21);
15  auto pad2 = new TPad("pad2","The pad with the histogram",0.05,0.05,0.95,0.45,21);
16  pad1->Draw();
17  pad2->Draw();
18  pad1->cd();
19 
20  gBenchmark->Start("fillrandom");
21  //
22  // A function (any dimension) or a formula may reference
23  // an already defined formula
24  //
25  auto form1 = new TFormula("form1","abs(sin(x)/x)");
26  auto sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1",0,10);
27  sqroot->SetParameters(10,4,1,20);
28  pad1->SetGridx();
29  pad1->SetGridy();
30  pad1->GetFrame()->SetFillColor(42);
31  pad1->GetFrame()->SetBorderMode(-1);
33  sqroot->SetLineColor(4);
34  sqroot->SetLineWidth(6);
35  sqroot->Draw();
36  auto lfunction = new TPaveLabel(5,39,9.8,46,"The sqroot function");
37  lfunction->SetFillColor(41);
38  lfunction->Draw();
39  c1->Update();
40 
41  //
42  // Create a one dimensional histogram (one float per bin)
43  // and fill it following the distribution in function sqroot.
44  //
45  pad2->cd();
46  pad2->GetFrame()->SetFillColor(42);
47  pad2->GetFrame()->SetBorderMode(-1);
49  auto h1f = new TH1F("h1f","Test random numbers",200,0,10);
50  h1f->SetFillColor(45);
51  h1f->FillRandom("sqroot",10000);
52  h1f->Draw();
53  c1->Update();
54  //
55  // Open a ROOT file and save the formula, function and histogram
56  //
57  TFile myfile("fillrandom.root","RECREATE");
58  form1->Write();
59  sqroot->Write();
60  h1f->Write();
61  gBenchmark->Show("fillrandom");
62 }
virtual void SetGridx(Int_t value=1)
Definition: TPad.h:327
tuple myfile
Definition: fillrandom.py:56
virtual void SetBorderMode(Short_t bordermode)
Definition: TWbox.h:62
tuple lfunction
Definition: fillrandom.py:35
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
TPad * pad1
Definition: hcons.C:13
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
tuple h1f
Definition: fillrandom.py:48
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:514
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2729
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:63
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1192
A Pave (see TPave) with a text centered in the Pave.
Definition: TPaveLabel.h:32
tuple form1
Definition: fillrandom.py:24
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
The F O R M U L A class.
Definition: TFormula.h:89
The most important graphics class in the ROOT system.
Definition: TPad.h:46
The Canvas class.
Definition: TCanvas.h:48
virtual void SetGridy(Int_t value=1)
Definition: TPad.h:328
TPad * pad2
Definition: hcons.C:13
1-Dim function class
Definition: TF1.h:149
tuple sqroot
Definition: fillrandom.py:25
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179