Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 #include "Riostream.h"
22 
23 TF2 * finter;
24 
25 //______________________________________________________________
26 Double_t interference( Double_t *x, Double_t *par)
27 {
28  Double_t x_p2 = x[0] * x[0];
29  Double_t d_2 = 0.5 * par[2];
30  Double_t ym_p2 = (x[1] - d_2) * (x[1] - d_2);
31  Double_t yp_p2 = (x[1] + d_2) * (x[1] + d_2);
32  Double_t tpi_l = TMath::Pi() / par[1];
33  Double_t amplitude = par[0] * (cos(tpi_l * sqrt(x_p2 + ym_p2))
34  + par[3] * cos(tpi_l * sqrt(x_p2 + yp_p2)));
35  return amplitude * amplitude;
36 }
37 
38 
39 //_____________________________________________________________
40 Double_t result( Double_t *x, Double_t *par)
41 {
42  Double_t xint[2];
43  Double_t maxintens = 0, xcur = 14;
44  Double_t dlambda = 0.1 * par[1];
45  for(Int_t i=0; i<10; i++){
46  xint[0] = xcur;
47  xint[1] = x[1];
48  Double_t intens = interference(xint, par);
49  if(intens > maxintens) maxintens = intens;
50  xcur -= dlambda;
51  }
52  return maxintens;
53 }
54 
55 
56 //_____________________________________________________________
57 void waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10)
58 {
59  TCanvas *c1 = new TCanvas("waves", "A double slit experiment",
60  300,40, 1004, 759);
61  c1->Range(0, -10, 30, 10);
62  c1->SetFillColor(0);
63  TPad *pad = new TPad("pr","pr", 0.5, 0 , 1., 1);
64  pad->Range(0, -10, 15, 10);
65  pad->Draw();
66 
67  const Int_t colNum = 30;
68  Int_t palette[colNum];
69  for (Int_t i=0;i<colNum;i++) {
70  TColor *color = new TColor(1001+i
71  , pow(i/((colNum)*1.0),0.3)
72  , pow(i/((colNum)*1.0),0.3)
73  ,0.5*(i/((colNum)*1.0)),"");
74  palette[i] = 1001+i;
75  }
76  gStyle->SetPalette(colNum,palette);
77  c1->cd();
78  TF2 * f0 = new TF2("ray_source",interference, 0.02, 15, -8, 8, 4);
79 
80  f0->SetParameters(amp, lambda, 0, 0);
81  f0->SetNpx(200);
82  f0->SetNpy(200);
83  f0->SetContour(colNum-2);
84  f0->Draw("samecolz");
85 
86  TLatex title;
87  title.DrawLatex(1.6, 8.5, "A double slit experiment");
88 
89  TGraph *graph = new TGraph(4);
90  graph->SetFillColor(0);
91  graph->SetFillStyle(1001);
92  graph->SetLineWidth(0);
93  graph->SetPoint(0, 0., 0.1);
94  graph->SetPoint(1, 14.8, 8);
95  graph->SetPoint(2, 0, 8);
96  graph->SetPoint(3, 0, 0.1);
97  graph->Draw("F");
98 
99  graph = new TGraph(4);
100  graph->SetFillColor(0);
101  graph->SetFillStyle(1001);
102  graph->SetLineWidth(0);
103  graph->SetPoint(0, 0, -0.1);
104  graph->SetPoint(1, 14.8, -8);
105  graph->SetPoint(2, 0, -8);
106  graph->SetPoint(3, 0, -0.1);
107  graph->Draw("F");
108 
109  TLine * line;
110  line = new TLine(15,-10, 15, 0 - 0.5*d -0.2);
111  line->SetLineWidth(10); line->Draw();
112  line = new TLine(15, 0 - 0.5*d +0.2 ,15, 0 + 0.5*d -0.2);
113  line->SetLineWidth(10); line->Draw();
114 
115  line = new TLine(15,0 + 0.5*d + 0.2,15, 10);
116  line->SetLineWidth(10); line->Draw();
117 
118  pad ->cd();
119  finter = new TF2("interference",interference, 0.01, 14, -10, 10, 4);
120 
121  finter->SetParameters(amp, lambda, d, 1);
122  finter->SetNpx(200);
123  finter->SetNpy(200);
124  finter->SetContour(colNum-2);
125  finter->Draw("samecolorz");
126 
127  TArc *arc = new TArc();;
128  arc->SetFillStyle(0);
129  arc->SetLineWidth(2);
130  arc->SetLineColor(5);
131  Float_t r = 0.5 * lambda, dr = lambda;
132  for (Int_t i = 0; i < 16; i++) {
133  arc->DrawArc(0, 0.5*d, r, 0., 360., "only");
134  arc->DrawArc(0, -0.5*d, r, 0., 360., "only");
135  r += dr;
136  }
137 
138  pad ->cd();
139  TF2 * fresult = new TF2("result",result, 14, 15, -10, 10, 4);
140 
141  fresult->SetParameters(amp, lambda, d, 1);
142  fresult->SetNpx(300);
143  fresult->SetNpy(300);
144  fresult->SetContour(colNum-2);
145  fresult->Draw("samecolor");
146  line = new TLine(13.8,-10, 14, 10);
147  line->SetLineWidth(10); line->SetLineColor(0); line->Draw();
148  c1->Modified(kTRUE);
149  c1->Update();
150  c1->SetEditable(kTRUE);
151 }