Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
gr201_waves.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// \preview Draw spherical waves interference. Two closed TGraphs filled with white are used
5/// here to draw triangles on top of a 2D function in order to hide parts of it.
6///
7/// \macro_image
8/// \macro_code
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//______________________________________________________________
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))
31 + par[3] * cos(tpi_l * sqrt(x_p2 + yp_p2)));
32 return amplitude * amplitude;
33}
34
35
36//_____________________________________________________________
38{
39 Double_t xint[2];
40 Double_t maxintens = 0, xcur = 14;
41 Double_t dlambda = 0.1 * par[1];
42 for(Int_t i=0; i<10; i++){
43 xint[0] = xcur;
44 xint[1] = x[1];
47 xcur -= dlambda;
48 }
49 return maxintens;
50}
51
52
53//_____________________________________________________________
54void gr201_waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10)
55{
56 TCanvas *c1 = new TCanvas("gr201_waves", "A double slit experiment", 300, 40, 1004, 759);
57 c1->Range(0, -10, 30, 10);
58 c1->SetFillColor(0);
59 TPad *pad = new TPad("pr", "pr", 0.5, 0, 1., 1);
60 pad->Range(0, -10, 15, 10);
61 pad->Draw();
62
63 const Int_t colNum = 30;
65 for (Int_t i = 0; i < colNum; i++) {
66 Float_t level = 1.*i/colNum;
67 palette[i] = TColor::GetColor((Float_t) TMath::Power(level,0.3), (Float_t) TMath::Power(level,0.3), (Float_t) 0.5*level);
68 // palette[i] = 1001+i;
69 }
71
72 c1->cd();
73
74 // For the incoming waves, on the left side, we use a TF2 and increase the number
75 // of points used for drawing to 200 (default is 100) for better resolution
76 TF2 *f0 = new TF2("ray_source", interference, 0.02, 15, -8, 8, 4);
77 f0->SetParameters(amp, lambda, 0, 0);
78 f0->SetNpx(200);
79 f0->SetNpy(200);
80 f0->SetContour(colNum-2);
81 f0->Draw("samecol");
82
83 TLatex title;
84 title.DrawLatex(1.6, 8.5, "A double slit experiment");
85
86 // Draw 2 white triangles to cover parts of f0, simulating a cone
87 // (change the fill color to see these)
88 TGraph *graph = new TGraph(4);
89 graph->SetFillColor(1);
90 graph->SetFillStyle(1001);
91 graph->SetLineWidth(0);
92 graph->SetPoint(0, 0., 0.1);
93 graph->SetPoint(1, 14.8, 8);
94 graph->SetPoint(2, 0, 8);
95 graph->SetPoint(3, 0, 0.1);
96 graph->Draw("F");
97
98 graph = new TGraph(4);
99 graph->SetFillColor(1);
100 graph->SetFillStyle(1001);
101 graph->SetLineWidth(0);
102 graph->SetPoint(0, 0, -0.1);
103 graph->SetPoint(1, 14.8, -8);
104 graph->SetPoint(2, 0, -8);
105 graph->SetPoint(3, 0, -0.1);
106 graph->Draw("F");
107
108 // To represent the wall with 2 openings, we draw 3 black vertical lines
109 TLine * line = new TLine(15,-10, 15, 0 - 0.5*d - 0.2);
110 line->SetLineWidth(10);
111 line->Draw();
112
113 line = new TLine(15, 0 - 0.5*d + 0.2, 15, 0 + 0.5*d - 0.2);
114 line->SetLineWidth(10);
115 line->Draw();
116
117 line = new TLine(15, 0 + 0.5*d + 0.2, 15, 10);
118 line->SetLineWidth(10);
119 line->Draw();
120
121 pad->cd();
122
123 // Interference plot, on the centre-right side
124 TF2 *finter = new TF2("interference",interference, 0.01, 14, -10, 10, 4);
125 finter->SetParameters(amp, lambda, d, 1);
126 finter->SetNpx(200);
127 finter->SetNpy(200);
128 finter->SetContour(colNum-2);
129 finter->Draw("samecol");
130
131 // Some lines
132 TArc arc;
133 arc.SetFillStyle(0);
134 arc.SetLineWidth(2);
135 arc.SetLineColor(5);
136 Float_t r = 0.5 * lambda, dr = lambda;
137 for (Int_t i = 0; i < 16; i++) {
138 arc.DrawArc(0, 0.5*d, r, 0., 360., "only");
139 arc.DrawArc(0, -0.5*d, r, 0., 360., "only");
140 r += dr;
141 }
142
143 pad->cd();
144
145 // Result, on the right edge
146 TF2 *fresult = new TF2("result",result, 14, 15, -10, 10, 4);
147 fresult->SetParameters(amp, lambda, d, 1);
148 fresult->SetNpx(300);
149 fresult->SetNpy(300);
150 fresult->SetContour(colNum-2);
151 fresult->Draw("samecol");
152
153 // Vertical white line on the right side
154 line = new TLine(14,-10, 14, 10);
156 c1->Modified(kTRUE);
157 c1->Update();
158 c1->SetEditable(kTRUE);
159}
#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:442
Create an Arc.
Definition TArc.h:26
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:38
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:40
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:45
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:42
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:1924
A 2-Dim function with parameters.
Definition TF2.h:29
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2347
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:833
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 SetPalette(Int_t ncolors=kBird, Int_t *colors=nullptr, Float_t alpha=1.)
See TColor::SetPalette.
Definition TStyle.cxx:1890
TLine * line
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
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