Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Bessel.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_math
3/// \notebook
4/// Show the different kinds of Bessel functions available in ROOT
5/// To execute the macro type in:
6///
7/// ~~~{.cpp}
8/// root[0] .x Bessel.C
9/// ~~~
10///
11/// It will create one canvas with the representation
12/// of the cylindrical and spherical Bessel functions
13/// regular and modified
14///
15/// \macro_image
16/// \macro_code
17///
18/// \author Magdalena Slawinska
19
20#include "TMath.h"
21#include "TF1.h"
22#include "TCanvas.h"
23
24#include <Riostream.h>
25#include "TLegend.h"
26#include "TLegendEntry.h"
27
28#include "Math/IFunction.h"
29#include <cmath>
30#include "TSystem.h"
31#include "TAxis.h"
32#include "TPaveLabel.h"
33
34void Bessel()
35{
36 TCanvas *DistCanvas = new TCanvas("DistCanvas", "Bessel functions example", 10, 10, 800, 600);
37 DistCanvas->SetFillColor(17);
38 DistCanvas->Divide(2, 2);
39 DistCanvas->cd(1);
40 gPad->SetGrid();
41 gPad->SetFrameFillColor(19);
42 TLegend *leg = new TLegend(0.75, 0.7, 0.89, 0.89);
43
44 int n = 5; //number of functions in each pad
45 //drawing the set of Bessel J functions
46 TF1* JBessel[5];
47 for(int nu = 0; nu < n; nu++)
48 {
49 JBessel[nu]= new TF1("J_0", "ROOT::Math::cyl_bessel_j([0],x)", 0, 10);
50 JBessel[nu]->SetParameters(nu, 0.0);
51 JBessel[nu]->SetTitle(""); //Bessel J functions");
52 JBessel[nu]->SetLineStyle(1);
53 JBessel[nu]->SetLineWidth(3);
54 JBessel[nu]->SetLineColor(nu+1);
55 }
56 JBessel[0]->TF1::GetXaxis()->SetTitle("x");
57 JBessel[0]->GetXaxis()->SetTitleSize(0.06);
58 JBessel[0]->GetXaxis()->SetTitleOffset(.7);
59
60 //setting the title in a label style
61 TPaveLabel *p1 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) , "Bessel J functions", "NDC");
62 p1->SetFillColor(0);
63 p1->SetTextFont(22);
65
66 //setting the legend
67 leg->AddEntry(JBessel[0]->DrawCopy(), " J_0(x)", "l");
68 leg->AddEntry(JBessel[1]->DrawCopy("same"), " J_1(x)", "l");
69 leg->AddEntry(JBessel[2]->DrawCopy("same"), " J_2(x)", "l");
70 leg->AddEntry(JBessel[3]->DrawCopy("same"), " J_3(x)", "l");
71 leg->AddEntry(JBessel[4]->DrawCopy("same"), " J_4(x)", "l");
72
73 leg->Draw();
74 p1->Draw();
75
76 //------------------------------------------------
77 DistCanvas->cd(2);
78 gPad->SetGrid();
79 gPad->SetFrameFillColor(19);
80
81 TLegend *leg2 = new TLegend(0.75, 0.7, 0.89, 0.89);
82 //------------------------------------------------
83 //Drawing Bessel k
84 TF1* KBessel[5];
85 for(int nu = 0; nu < n; nu++){
86 KBessel[nu]= new TF1("J_0", "ROOT::Math::cyl_bessel_k([0],x)", 0, 10);
87 KBessel[nu]->SetParameters(nu, 0.0);
88 KBessel[nu]->SetTitle("Bessel K functions");
89 KBessel[nu]->SetLineStyle(1);
90 KBessel[nu]->SetLineWidth(3);
91 KBessel[nu]->SetLineColor(nu+1);
92 }
93 KBessel[0]->GetXaxis()->SetTitle("x");
94 KBessel[0]->GetXaxis()->SetTitleSize(0.06);
95 KBessel[0]->GetXaxis()->SetTitleOffset(.7);
96
97 //setting title
98 TPaveLabel *p2 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) , "Bessel K functions", "NDC");
99 p2->SetFillColor(0);
100 p2->SetTextFont(22);
101 p2->SetTextColor(kBlack);
102
103 //setting legend
104 leg2->AddEntry(KBessel[0]->DrawCopy(), " K_0(x)", "l");
105 leg2->AddEntry(KBessel[1]->DrawCopy("same"), " K_1(x)", "l");
106 leg2->AddEntry(KBessel[2]->DrawCopy("same"), " K_2(x)", "l");
107 leg2->AddEntry(KBessel[3]->DrawCopy("same"), " K_3(x)", "l");
108 leg2->AddEntry(KBessel[4]->DrawCopy("same"), " K_4(x)", "l");
109 leg2->Draw();
110 p2->Draw();
111 //------------------------------------------------
112 DistCanvas->cd(3);
113 gPad->SetGrid();
114 gPad->SetFrameFillColor(19);
115 TLegend *leg3 = new TLegend(0.75, 0.7, 0.89, 0.89);
116 //------------------------------------------------
117 //Drawing Bessel i
118 TF1* iBessel[5];
119 for(int nu = 0; nu <= 4; nu++){
120 iBessel[nu]= new TF1("J_0", "ROOT::Math::cyl_bessel_i([0],x)", 0, 10);
121 iBessel[nu]->SetParameters(nu, 0.0);
122 iBessel[nu]->SetTitle("Bessel I functions");
123 iBessel[nu]->SetLineStyle(1);
124 iBessel[nu]->SetLineWidth(3);
125 iBessel[nu]->SetLineColor(nu+1);
126 }
127
128 iBessel[0]->GetXaxis()->SetTitle("x");
129 iBessel[0]->GetXaxis()->SetTitleSize(0.06);
130 iBessel[0]->GetXaxis()->SetTitleOffset(.7);
131
132 //setting title
133 TPaveLabel *p3 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) ,"Bessel I functions", "NDC");
134 p3->SetFillColor(0);
135 p3->SetTextFont(22);
136 p3->SetTextColor(kBlack);
137
138 //setting legend
139 leg3->AddEntry(iBessel[0]->DrawCopy(), " I_0", "l");
140 leg3->AddEntry(iBessel[1]->DrawCopy("same"), " I_1(x)", "l");
141 leg3->AddEntry(iBessel[2]->DrawCopy("same"), " I_2(x)", "l");
142 leg3->AddEntry(iBessel[3]->DrawCopy("same"), " I_3(x)", "l");
143 leg3->AddEntry(iBessel[4]->DrawCopy("same"), " I_4(x)", "l");
144 leg3->Draw();
145 p3->Draw();
146 //------------------------------------------------
147 DistCanvas->cd(4);
148 gPad->SetGrid();
149 gPad->SetFrameFillColor(19);
150 TLegend *leg4 = new TLegend(0.75, 0.7, 0.89, 0.89);
151 //------------------------------------------------
152 //Drawing sph_bessel
153 TF1* jBessel[5];
154 for(int nu = 0; nu <= 4; nu++){
155 jBessel[nu]= new TF1("J_0", "ROOT::Math::sph_bessel([0],x)", 0, 10);
156 jBessel[nu]->SetParameters(nu, 0.0);
157 jBessel[nu]->SetTitle("Bessel j functions");
158 jBessel[nu]->SetLineStyle(1);
159 jBessel[nu]->SetLineWidth(3);
160 jBessel[nu]->SetLineColor(nu+1);
161 }
162 jBessel[0]->GetXaxis()->SetTitle("x");
163 jBessel[0]->GetXaxis()->SetTitleSize(0.06);
164 jBessel[0]->GetXaxis()->SetTitleOffset(.7);
165
166 //setting title
167 TPaveLabel *p4 = new TPaveLabel(.0,.90 , (.0+.50),(.90+.10) ,"Bessel j functions", "NDC");
168 p4->SetFillColor(0);
169 p4->SetTextFont(22);
170 p4->SetTextColor(kBlack);
171
172 //setting legend
173
174 leg4->AddEntry(jBessel[0]->DrawCopy(), " j_0(x)", "l");
175 leg4->AddEntry(jBessel[1]->DrawCopy("same"), " j_1(x)", "l");
176 leg4->AddEntry(jBessel[2]->DrawCopy("same"), " j_2(x)", "l");
177 leg4->AddEntry(jBessel[3]->DrawCopy("same"), " j_3(x)", "l");
178 leg4->AddEntry(jBessel[4]->DrawCopy("same"), " j_4(x)", "l");
179
180 leg4->Draw();
181 p4->Draw();
182
183 DistCanvas->cd();
184}
@ kBlack
Definition Rtypes.h:65
#define gPad
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition TAttAxis.cxx:309
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition TAttLine.h:42
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:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:46
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:719
1-Dim function class
Definition TF1.h:233
void SetTitle(const char *title="") override
Set function title if title has the form "fffffff;xxxx;yyyy", it is assumed that the function title i...
Definition TF1.cxx:3558
virtual void SetParameters(const Double_t *params)
Definition TF1.h:677
TAxis * GetXaxis() const
Get x axis of the function.
Definition TF1.cxx:2400
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition TLegend.cxx:320
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:425
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1249
A Pave (see TPave) with a text centered in the Pave.
Definition TPaveLabel.h:20
void Draw(Option_t *option="") override
Draw this pavelabel with its current attributes.
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)=0
const Int_t n
Definition legend1.C:16
leg
Definition legend1.C:34