Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
11void fillrandom(int plot=1) {
12 gBenchmark->Start("fillrandom");
13
14 // A function (any dimension) or a formula may reference
15 // an already defined formula
16 auto form1 = new TFormula("form1","abs(sin(x)/x)");
17 auto sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1",0,10);
18 sqroot->SetLineColor(4);
19 sqroot->SetLineWidth(6);
20 sqroot->SetParameters(10,4,1,20);
21
22 // Create a one dimensional histogram (one float per bin)
23 // and fill it following the distribution in function sqroot.
24 auto h1f = new TH1F("h1f","Test random numbers",200,0,10);
25 h1f->FillRandom("sqroot",10000);
26
27 // Open a ROOT file and save the formula, function and histogram
28 TFile myfile("fillrandom.root","RECREATE");
29 form1->Write();
30 sqroot->Write();
31 h1f->Write();
32
33 // Produce a plot if requested
34 if (plot) {
35 TCanvas *c1 = new TCanvas("c1","The FillRandom example",200,10,700,900);
36
37 auto pad1 = new TPad("pad1","The pad with the function",0.05,0.50,0.95,0.95);
38 auto pad2 = new TPad("pad2","The pad with the histogram",0.05,0.05,0.95,0.45);
39 pad1->Draw();
40 pad2->Draw();
41 pad1->cd();
42
43 pad1->SetGridx();
44 pad1->SetGridy();
45 pad1->GetFrame()->SetBorderMode(-1);
46 pad1->GetFrame()->SetBorderSize(5);
47
48 sqroot->Draw();
49 auto lfunction = new TPaveLabel(5,39,9.8,46,"The sqroot function");
50 lfunction->Draw();
51 c1->Update();
52
53 pad2->cd();
54 pad2->GetFrame()->SetBorderMode(-1);
55 pad2->GetFrame()->SetBorderSize(5);
56
57 h1f->SetFillColor(45);
58 h1f->Draw();
59 c1->Update();
60 }
61
62 gBenchmark->Show("fillrandom");
63}
R__EXTERN TBenchmark * gBenchmark
Definition TBenchmark.h:59
winID h TVirtualViewer3D TVirtualGLPainter char TVirtualGLPainter plot
virtual void Start(const char *name)
Starts Benchmark with the specified name.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
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:623
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
return c1
Definition legend1.C:41