Logo ROOT   6.14/05
Reference Guide
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 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:294
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:241
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:390
static double p3(double t, double a, double b, double c, double d)
virtual TF1 * DrawCopy(Option_t *option="") const
Draw a copy of this function with its current attributes.
Definition: TF1.cxx:1254
return c1
Definition: legend1.C:41
Definition: Rtypes.h:59
R__EXTERN TStyle * gStyle
Definition: TStyle.h:406
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:285
Definition: Rtypes.h:58
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
Definition: TF1.cxx:3425
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:391
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
TAxis * GetXaxis() const
Get x axis of the function.
Definition: TF1.cxx:2289
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF1.cxx:1224
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:45
TAxis * GetYaxis() const
Get y axis of the function.
Definition: TF1.cxx:2300
static double p2(double t, double a, double b, double c)
A Pave (see TPave) with a text centered in the Pave.
Definition: TPaveLabel.h:20
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
static double p1(double t, double a, double b)
A 2-Dim function with parameters.
Definition: TF2.h:29
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title The size is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:304
#define h(i)
Definition: RSha256.hxx:106
virtual void SetTitle(const char *title="")
Set function title if title has the form "fffffff;xxxx;yyyy", it is assumed that the function title i...
Definition: TF1.cxx:3455
The Canvas class.
Definition: TCanvas.h:31
virtual void Draw(Option_t *option="")
Draw this pavelabel with its current attributes.
Definition: TPaveLabel.cxx:77
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1162
1-Dim function class
Definition: TF1.h:211
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1444
#define gPad
Definition: TVirtualPad.h:285
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:43
Definition: Rtypes.h:59
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:315