Logo ROOT   6.18/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
29void GammaFun() {
30
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);
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);
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);
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}
#define h(i)
Definition: RSha256.hxx:106
@ kRed
Definition: Rtypes.h:64
@ kBlack
Definition: Rtypes.h:63
@ kBlue
Definition: Rtypes.h:64
R__EXTERN TStyle * gStyle
Definition: TStyle.h:406
#define gPad
Definition: TVirtualPad.h:286
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 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
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:43
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:45
The Canvas class.
Definition: TCanvas.h:31
1-Dim function class
Definition: TF1.h:211
TAxis * GetYaxis() const
Get y axis of the function.
Definition: TF1.cxx:2393
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:3548
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
Definition: TF1.cxx:3518
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF1.cxx:1317
virtual TF1 * DrawCopy(Option_t *option="") const
Draw a copy of this function with its current attributes.
Definition: TF1.cxx:1347
TAxis * GetXaxis() const
Get x axis of the function.
Definition: TF1.cxx:2382
A 2-Dim function with parameters.
Definition: TF2.h:29
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:241
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
A Pave (see TPave) with a text centered in the Pave.
Definition: TPaveLabel.h:20
virtual void Draw(Option_t *option="")
Draw this pavelabel with its current attributes.
Definition: TPaveLabel.cxx:77
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
return c1
Definition: legend1.C:41