Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
fillrandom.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// \notebook
4 /// Fill a 1-D histogram from a parametric function.
5 ///
6 /// \macro_image
7 /// \macro_code
8 ///
9 /// \author Rene Brun
10 
11 void fillrandom() {
12  TCanvas *c1 = new TCanvas("c1","The FillRandom example",200,10,700,900);
13 
14  auto pad1 = new TPad("pad1","The pad with the function",0.05,0.50,0.95,0.95);
15  auto pad2 = new TPad("pad2","The pad with the histogram",0.05,0.05,0.95,0.45);
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()->SetBorderMode(-1);
31  pad1->GetFrame()->SetBorderSize(5);
32  sqroot->SetLineColor(4);
33  sqroot->SetLineWidth(6);
34  sqroot->Draw();
35  auto lfunction = new TPaveLabel(5,39,9.8,46,"The sqroot function");
36  lfunction->Draw();
37  c1->Update();
38 
39  //
40  // Create a one dimensional histogram (one float per bin)
41  // and fill it following the distribution in function sqroot.
42  //
43  pad2->cd();
44  pad2->GetFrame()->SetBorderMode(-1);
45  pad2->GetFrame()->SetBorderSize(5);
46  auto h1f = new TH1F("h1f","Test random numbers",200,0,10);
47  h1f->SetFillColor(45);
48  h1f->FillRandom("sqroot",10000);
49  h1f->Draw();
50  c1->Update();
51  //
52  // Open a ROOT file and save the formula, function and histogram
53  //
54  TFile myfile("fillrandom.root","RECREATE");
55  form1->Write();
56  sqroot->Write();
57  h1f->Write();
58  gBenchmark->Show("fillrandom");
59 }