Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
GammaFun.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_math
3 /// \notebook
4 /// Example showing the usage of the major special math functions (gamma, beta, erf) in ROOT
5 /// To execute the macro type in:
6 ///
7 /// ~~~{.cpp}
8 /// root[0]: .x GammaFun.C
9 /// ~~~
10 ///
11 /// It will create one canvas with the representation
12 /// of the tgamma, lgamma, beta, erf and erfc functions
13 ///
14 /// \macro_image
15 /// \macro_code
16 ///
17 /// \author Magdalena Slawinska
18 
19 #include "TMath.h"
20 #include "TF1.h"
21 #include "TF2.h"
22 #include "TSystem.h"
23 #include "TCanvas.h"
24 #include "TStyle.h"
25 #include "TPaveLabel.h"
26 #include "TAxis.h"
27 #include "TH1.h"
28 
29 void GammaFun() {
30 
31  gStyle->SetOptStat(0);
32 
33  TF1 *f1a = new TF1("Gamma(x)","ROOT::Math::tgamma(x)",-2,5);
34  TF1 *f2a = new TF1("f2a","ROOT::Math::lgamma(x)",0,10);
35  TF2 *f3a = new TF2("Beta(x)","ROOT::Math::beta(x, y)",0,0.1, 0, 0.1);
36  TF1 *f4a = new TF1("erf(x)","ROOT::Math::erf(x)",0,5);
37  TF1 *f4b = new TF1("erfc(x)","ROOT::Math::erfc(x)",0,5);
38 
39  TCanvas *c1 = new TCanvas("c1", "Gamma and related functions",800,700);
40 
41  c1->Divide(2,2);
42 
43  c1->cd(1);
44  gPad->SetGrid();
45 
46  //setting the title in a label style
47  TPaveLabel *p1 = new TPaveLabel(.1,.90 , (.1+.50),(.90+.10) ,"ROOT::Math::tgamma(x)", "NDC");
48  p1->SetFillColor(0);
49  p1->SetTextFont(22);
50  p1->SetTextColor(kBlack);
51 
52  //setting graph
53  // draw axis first (use TH1 to draw the frame)
54  TH1F * h = new TH1F("htmp","",500,-2,5);
55  h->SetMinimum(-20);
56  h->SetMaximum(20);
57  h->GetXaxis()->SetTitleSize(0.06);
58  h->GetXaxis()->SetTitleOffset(.7);
59  h->GetXaxis()->SetTitle("x");
60 
61  h->Draw();
62 
63  // draw the functions 3 times in the separate ranges to avoid singularities
64  f1a->SetLineWidth(2);
65  f1a->SetLineColor(kBlue);
66 
67  f1a->SetRange(-2,-1);
68  f1a->DrawCopy("same");
69 
70  f1a->SetRange(-1,0);
71  f1a->DrawCopy("same");
72 
73  f1a->SetRange(0,5);
74  f1a->DrawCopy("same");
75 
76  p1->Draw();
77 
78  c1->cd(2);
79  gPad->SetGrid();
80  TPaveLabel *p2 = new TPaveLabel(.1,.90 , (.1+.50),(.90+.10) ,"ROOT::Math::lgamma(x)", "NDC");
81  p2->SetFillColor(0);
82  p2->SetTextFont(22);
83  p2->SetTextColor(kBlack);
84  f2a->SetLineColor(kBlue);
85  f2a->SetLineWidth(2);
86  f2a->GetXaxis()->SetTitle("x");
87  f2a->GetXaxis()->SetTitleSize(0.06);
88  f2a->GetXaxis()->SetTitleOffset(.7);
89  f2a->SetTitle("");
90  f2a->Draw();
91  p2->Draw();
92 
93  c1->cd(3);
94  gPad->SetGrid();
95 
96  TPaveLabel *p3 = new TPaveLabel(.1,.90 , (.1+.50),(.90+.10) ,"ROOT::Math::beta(x, y)", "NDC");
97  p3->SetFillColor(0);
98  p3->SetTextFont(22);
99  p3->SetTextColor(kBlack);
100  f3a->SetLineWidth(2);
101  f3a->GetXaxis()->SetTitle("x");
102  f3a->GetXaxis()->SetTitleOffset(1.2);
103  f3a->GetXaxis()->SetTitleSize(0.06);
104  f3a->GetYaxis()->SetTitle("y");
105  f3a->GetYaxis()->SetTitleSize(0.06);
106  f3a->GetYaxis()->SetTitleOffset(1.5);
107  f3a->SetTitle("");
108  f3a->Draw("surf1");//option for a 3-dim plot
109  p3->Draw();
110 
111  c1->cd(4);
112  gPad->SetGrid();
113  TPaveLabel *p4 = new TPaveLabel(.1,.90 , (.1+.50),(.90+.10) ,"erf(x) and erfc(x)", "NDC");
114  p4->SetFillColor(0);
115  p4->SetTextFont(22);
116  p4->SetTextColor(kBlack);
117  f4a->SetTitle("erf(x) and erfc(x)");
118  f4a->SetLineWidth(2);
119  f4b->SetLineWidth(2);
120  f4a->SetLineColor(kBlue);
121  f4b->SetLineColor(kRed);
122  f4a->GetXaxis()->SetTitleSize(.06);
123  f4a->GetXaxis()->SetTitleOffset(.7);
124  f4a->GetXaxis()->SetTitle("x");
125  f4a->Draw();
126  f4b->Draw("same");//option for a multiple graph plot
127  f4a->SetTitle("");
128  p4->Draw();
129 }