Logo ROOT   6.10/09
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  gSystem->Load("libMathCore");
32 
33  gStyle->SetOptStat(0);
34 
35  TF1 *f1a = new TF1("Gamma(x)","ROOT::Math::tgamma(x)",-2,5);
36  TF1 *f2a = new TF1("f2a","ROOT::Math::lgamma(x)",0,10);
37  TF2 *f3a = new TF2("Beta(x)","ROOT::Math::beta(x, y)",0,0.1, 0, 0.1);
38  TF1 *f4a = new TF1("erf(x)","ROOT::Math::erf(x)",0,5);
39  TF1 *f4b = new TF1("erfc(x)","ROOT::Math::erfc(x)",0,5);
40 
41  TCanvas *c1 = new TCanvas("c1", "Gamma and related functions",800,700);
42 
43  c1->Divide(2,2);
44 
45  c1->cd(1);
46  gPad->SetGrid();
47 
48  //setting the title in a label style
49  TPaveLabel *p1 = new TPaveLabel(.1,.90 , (.1+.50),(.90+.10) ,"ROOT::Math::tgamma(x)", "NDC");
50  p1->SetFillColor(0);
51  p1->SetTextFont(22);
52  p1->SetTextColor(kBlack);
53 
54  //setting graph
55  // draw axis first (use TH1 to draw the frame)
56  TH1F * h = new TH1F("htmp","",500,-2,5);
57  h->SetMinimum(-20);
58  h->SetMaximum(20);
59  h->GetXaxis()->SetTitleSize(0.06);
60  h->GetXaxis()->SetTitleOffset(.7);
61  h->GetXaxis()->SetTitle("x");
62 
63  h->Draw();
64 
65  // draw the functions 3 times in the separate ranges to avoid singularities
66  f1a->SetLineWidth(2);
67  f1a->SetLineColor(kBlue);
68 
69  f1a->SetRange(-2,-1);
70  f1a->DrawCopy("same");
71 
72  f1a->SetRange(-1,0);
73  f1a->DrawCopy("same");
74 
75  f1a->SetRange(0,5);
76  f1a->DrawCopy("same");
77 
78  p1->Draw();
79 
80  c1->cd(2);
81  gPad->SetGrid();
82  TPaveLabel *p2 = new TPaveLabel(.1,.90 , (.1+.50),(.90+.10) ,"ROOT::Math::lgamma(x)", "NDC");
83  p2->SetFillColor(0);
84  p2->SetTextFont(22);
85  p2->SetTextColor(kBlack);
86  f2a->SetLineColor(kBlue);
87  f2a->SetLineWidth(2);
88  f2a->GetXaxis()->SetTitle("x");
89  f2a->GetXaxis()->SetTitleSize(0.06);
90  f2a->GetXaxis()->SetTitleOffset(.7);
91  f2a->SetTitle("");
92  f2a->Draw();
93  p2->Draw();
94 
95  c1->cd(3);
96  gPad->SetGrid();
97 
98  TPaveLabel *p3 = new TPaveLabel(.1,.90 , (.1+.50),(.90+.10) ,"ROOT::Math::beta(x, y)", "NDC");
99  p3->SetFillColor(0);
100  p3->SetTextFont(22);
101  p3->SetTextColor(kBlack);
102  f3a->SetLineWidth(2);
103  f3a->GetXaxis()->SetTitle("x");
104  f3a->GetXaxis()->SetTitleOffset(1.2);
105  f3a->GetXaxis()->SetTitleSize(0.06);
106  f3a->GetYaxis()->SetTitle("y");
107  f3a->GetYaxis()->SetTitleSize(0.06);
108  f3a->GetYaxis()->SetTitleOffset(1.5);
109  f3a->SetTitle("");
110  f3a->Draw("surf1");//option for a 3-dim plot
111  p3->Draw();
112 
113  c1->cd(4);
114  gPad->SetGrid();
115  TPaveLabel *p4 = new TPaveLabel(.1,.90 , (.1+.50),(.90+.10) ,"erf(x) and erfc(x)", "NDC");
116  p4->SetFillColor(0);
117  p4->SetTextFont(22);
118  p4->SetTextColor(kBlack);
119  f4a->SetTitle("erf(x) and erfc(x)");
120  f4a->SetLineWidth(2);
121  f4b->SetLineWidth(2);
122  f4a->SetLineColor(kBlue);
123  f4b->SetLineColor(kRed);
124  f4a->GetXaxis()->SetTitleSize(.06);
125  f4a->GetXaxis()->SetTitleOffset(.7);
126  f4a->GetXaxis()->SetTitle("x");
127  f4a->Draw();
128  f4b->Draw("same");//option for a multiple graph plot
129  f4a->SetTitle("");
130  p4->Draw();
131 }
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:262
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:217
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:375
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:1115
return c1
Definition: legend1.C:41
Definition: Rtypes.h:56
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
TH1 * h
Definition: legend2.C:5
Definition: Rtypes.h:55
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
virtual void SetRange(Double_t xmin, Double_t xmax)
Initialize the upper and lower bounds to draw the function.
Definition: TF1.cxx:3288
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:376
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1825
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
TAxis * GetXaxis() const
Get x axis of the function.
Definition: TF1.cxx:2131
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF1.cxx:1087
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:2142
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
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
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:272
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:3315
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:1135
1-Dim function class
Definition: TF1.h:150
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:1267
#define gPad
Definition: TVirtualPad.h:284
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:43
Definition: Rtypes.h:56
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
TAxis * GetXaxis()
Definition: TH1.h:300