Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
waves.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// Hint: Spherical waves
5///
6/// \macro_image
7/// \macro_code
8///
9/// \author Otto Schaile
10
11#include "TROOT.h"
12#include "TCanvas.h"
13#include "TColor.h"
14#include "TArc.h"
15#include "TGraph.h"
16#include "TF2.h"
17#include "TLine.h"
18#include "TLatex.h"
19#include "TMath.h"
20#include "TStyle.h"
21
22//______________________________________________________________
23Double_t interference(Double_t *x, Double_t *par)
24{
25 Double_t x_p2 = x[0] * x[0];
26 Double_t d_2 = 0.5 * par[2];
27 Double_t ym_p2 = (x[1] - d_2) * (x[1] - d_2);
28 Double_t yp_p2 = (x[1] + d_2) * (x[1] + d_2);
29 Double_t tpi_l = TMath::Pi() / par[1];
30 Double_t amplitude = par[0] * (cos(tpi_l * sqrt(x_p2 + ym_p2)) + par[3] * cos(tpi_l * sqrt(x_p2 + yp_p2)));
31 return amplitude * amplitude;
32}
33
34//_____________________________________________________________
36{
37 Double_t xint[2];
38 Double_t maxintens = 0, xcur = 14;
39 Double_t dlambda = 0.1 * par[1];
40 for (Int_t i = 0; i < 10; i++) {
41 xint[0] = xcur;
42 xint[1] = x[1];
43 Double_t intens = interference(xint, par);
44 if (intens > maxintens)
45 maxintens = intens;
46 xcur -= dlambda;
47 }
48 return maxintens;
49}
50
51//_____________________________________________________________
52void waves(Double_t d = 3, Double_t lambda = 1, Double_t amp = 10)
53{
54 TCanvas *c1 = new TCanvas("waves", "A double slit experiment", 300, 40, 1004, 759);
55 c1->Range(0, -10, 30, 10);
56 c1->SetFillColor(0);
57 TPad *pad = new TPad("pr", "pr", 0.5, 0, 1., 1);
58 pad->Range(0, -10, 15, 10);
59 pad->Draw();
60
61 const Int_t colNum = 30;
62 Int_t palette[colNum];
63 for (Int_t i = 0; i < colNum; i++) {
64 Float_t level = 1. * i / colNum;
65 palette[i] =
66 TColor::GetColor((Float_t)TMath::Power(level, 0.3), (Float_t)TMath::Power(level, 0.3), (Float_t)0.5 * level);
67 // palette[i] = 1001+i;
68 }
69 gStyle->SetPalette(colNum, palette);
70
71 c1->cd();
72
73 TF2 *f0 = new TF2("ray_source", interference, 0.02, 15, -8, 8, 4);
74 f0->SetParameters(amp, lambda, 0, 0);
75 f0->SetNpx(200);
76 f0->SetNpy(200);
77 f0->SetContour(colNum - 2);
78 f0->Draw("samecol");
79
80 TLatex title;
81 title.DrawLatex(1.6, 8.5, "A double slit experiment");
82
83 TGraph *graph = new TGraph(4);
84 graph->SetFillColor(0);
85 graph->SetFillStyle(1001);
86 graph->SetLineWidth(0);
87 graph->SetPoint(0, 0., 0.1);
88 graph->SetPoint(1, 14.8, 8);
89 graph->SetPoint(2, 0, 8);
90 graph->SetPoint(3, 0, 0.1);
91 graph->Draw("F");
92
93 graph = new TGraph(4);
94 graph->SetFillColor(0);
95 graph->SetFillStyle(1001);
96 graph->SetLineWidth(0);
97 graph->SetPoint(0, 0, -0.1);
98 graph->SetPoint(1, 14.8, -8);
99 graph->SetPoint(2, 0, -8);
100 graph->SetPoint(3, 0, -0.1);
101 graph->Draw("F");
102
103 TLine *line = new TLine(15, -10, 15, 0 - 0.5 * d - 0.2);
104 line->SetLineWidth(10);
105 line->Draw();
106
107 line = new TLine(15, 0 - 0.5 * d + 0.2, 15, 0 + 0.5 * d - 0.2);
108 line->SetLineWidth(10);
109 line->Draw();
110
111 line = new TLine(15, 0 + 0.5 * d + 0.2, 15, 10);
112 line->SetLineWidth(10);
113 line->Draw();
114
115 pad->cd();
116
117 TF2 *finter = new TF2("interference", interference, 0.01, 14, -10, 10, 4);
118 finter->SetParameters(amp, lambda, d, 1);
119 finter->SetNpx(200);
120 finter->SetNpy(200);
121 finter->SetContour(colNum - 2);
122 finter->Draw("samecol");
123
124 TArc arc;
125 arc.SetFillStyle(0);
126 arc.SetLineWidth(2);
127 arc.SetLineColor(5);
128 Float_t r = 0.5 * lambda, dr = lambda;
129 for (Int_t i = 0; i < 16; i++) {
130 arc.DrawArc(0, 0.5 * d, r, 0., 360., "only");
131 arc.DrawArc(0, -0.5 * d, r, 0., 360., "only");
132 r += dr;
133 }
134
135 pad->cd();
136
137 TF2 *fresult = new TF2("result", result, 14, 15, -10, 10, 4);
138 fresult->SetParameters(amp, lambda, d, 1);
139 fresult->SetNpx(300);
140 fresult->SetNpy(300);
141 fresult->SetContour(colNum - 2);
142 fresult->Draw("samecol");
143
144 line = new TLine(13.8, -10, 14, 10);
145 line->SetLineWidth(10);
146 line->SetLineColor(0);
147 line->Draw();
148 c1->Modified(kTRUE);
149 c1->Update();
150 c1->SetEditable(kTRUE);
151}
#define d(i)
Definition RSha256.hxx:102
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
Create an Arc.
Definition TArc.h:26
virtual TArc * DrawArc(Double_t x1, Double_t y1, Double_t radius, Double_t phimin=0, Double_t phimax=360, Option_t *option="")
Draw this arc with new coordinates.
Definition TArc.cxx:79
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:39
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
The Canvas class.
Definition TCanvas.h:23
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1921
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition TF1.cxx:3433
virtual void SetParameters(const Double_t *params)
Definition TF1.h:677
A 2-Dim function with parameters.
Definition TF2.h:29
virtual void SetContour(Int_t nlevels=20, const Double_t *levels=nullptr)
Set the number and values of contour levels.
Definition TF2.cxx:892
virtual void SetNpy(Int_t npy=100)
Set the number of points used to draw the function.
Definition TF2.cxx:927
void Draw(Option_t *option="") override
Draw this function with its current attributes.
Definition TF2.cxx:259
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
To draw Mathematical Formula.
Definition TLatex.h:18
TLatex * DrawLatex(Double_t x, Double_t y, const char *text)
Make a copy of this object with the new parameters And copy object attributes.
Definition TLatex.cxx:1943
Use the TLine constructor to create a simple line.
Definition TLine.h:22
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:292
The most important graphics class in the ROOT system.
Definition TPad.h:28
void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2) override
Set world coordinate system for the pad.
Definition TPad.cxx:5331
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition TPad.cxx:693
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition TPad.cxx:1364
void SetPalette(Int_t ncolors=kBird, Int_t *colors=nullptr, Float_t alpha=1.)
See TColor::SetPalette.
Definition TStyle.cxx:1888
TLine * line
RVec< PromoteType< T > > cos(const RVec< T > &v)
Definition RVec.hxx:1852
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition TMath.h:725
constexpr Double_t Pi()
Definition TMath.h:37
Definition graph.py:1