|
8 | 8 | /// |
9 | 9 | /// \author Rene Brun |
10 | 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 | | - |
| 11 | +void fillrandom(int plot=1) { |
20 | 12 | gBenchmark->Start("fillrandom"); |
21 | | - // |
| 13 | + |
22 | 14 | // A function (any dimension) or a formula may reference |
23 | 15 | // an already defined formula |
24 | | - // |
25 | 16 | auto form1 = new TFormula("form1","abs(sin(x)/x)"); |
26 | 17 | 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 | 18 | sqroot->SetLineColor(4); |
33 | 19 | 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(); |
| 20 | + sqroot->SetParameters(10,4,1,20); |
38 | 21 |
|
39 | | - // |
40 | 22 | // Create a one dimensional histogram (one float per bin) |
41 | 23 | // and fill it following the distribution in function sqroot. |
42 | | - // |
43 | | - pad2->cd(); |
44 | | - pad2->GetFrame()->SetBorderMode(-1); |
45 | | - pad2->GetFrame()->SetBorderSize(5); |
46 | 24 | auto h1f = new TH1F("h1f","Test random numbers",200,0,10); |
47 | | - h1f->SetFillColor(45); |
48 | 25 | h1f->FillRandom("sqroot",10000); |
49 | | - h1f->Draw(); |
50 | | - c1->Update(); |
51 | | - // |
| 26 | + |
52 | 27 | // Open a ROOT file and save the formula, function and histogram |
53 | | - // |
54 | 28 | TFile myfile("fillrandom.root","RECREATE"); |
55 | 29 | form1->Write(); |
56 | 30 | sqroot->Write(); |
57 | 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 | + |
58 | 62 | gBenchmark->Show("fillrandom"); |
59 | 63 | } |
0 commit comments