ROOT logo

From $ROOTSYS/tutorials/graphs/waves.C

//Hint: Spherical waves
//Author; Otto Schaile

#include "TROOT.h"
#include "TCanvas.h"
#include "TColor.h"
#include "TArc.h"
#include "TGraph.h"
#include "TF2.h"
#include "TLine.h"
#include "TLatex.h"
#include "TMath.h"
#include "TStyle.h"
#include "Riostream.h"

TF2 * finter;


//______________________________________________________________
Double_t interference( Double_t *x, Double_t *par)
{
   Double_t x_p2 = x[0] * x[0];
   Double_t d_2 = 0.5 * par[2];
   Double_t ym_p2 = (x[1] - d_2) * (x[1] - d_2);
   Double_t yp_p2 = (x[1] + d_2) * (x[1] + d_2);
   Double_t  tpi_l = TMath::Pi() /  par[1];
   Double_t amplitude = par[0] * (cos(tpi_l  * sqrt(x_p2 + ym_p2))
                      + par[3] * cos(tpi_l  * sqrt(x_p2 + yp_p2)));
   return amplitude * amplitude;
}


//_____________________________________________________________
Double_t result( Double_t *x, Double_t *par)
{
   Double_t xint[2];
   Double_t  maxintens = 0, xcur = 14;
   Double_t dlambda = 0.1 * par[1];
   for(Int_t i=0; i<10; i++){
      xint[0] = xcur;
      xint[1] = x[1];
      Double_t  intens = interference(xint, par);
      if(intens > maxintens) maxintens = intens;
      xcur -= dlambda;
   }
   return maxintens;
}


//_________________________________________________________________
void waves( Double_t d = 3, Double_t lambda = 1, Double_t amp = 10)
{
   TCanvas *c1 = new TCanvas("waves", "A double slit experiment", 
      300,40, 1004, 759);
   c1->Range(0, -10,  30, 10);
   c1->SetFillColor(0);
   TPad *pad = new TPad("pr","pr",  0.5, 0 , 1., 1);
   pad->Range(0, -10,  15, 10);
   pad->Draw();

   const Int_t colNum = 30;
   Int_t palette[colNum];
   for (Int_t i=0;i<colNum;i++) {
      TColor *color = new TColor(1001+i
      ,    pow(i/((colNum)*1.0),0.3)
      ,    pow(i/((colNum)*1.0),0.3)
      ,0.5*(i/((colNum)*1.0)),"");
      palette[i] = 1001+i;
      if(color);
   }
   gStyle->SetPalette(colNum,palette);
   c1->cd();
   TF2 * f0 = new TF2("ray_source",interference, 0.02, 15, -8, 8, 4);

   f0->SetParameters(amp, lambda, 0, 0);
   f0->SetNpx(200);
   f0->SetNpy(200);
   f0->SetContour(colNum-2);
   f0->Draw("samecolz");

   TLatex title;
   title.DrawLatex(1.6, 8.5, "A double slit experiment");

   TGraph *graph = new TGraph(4);
   graph->SetFillColor(0);
   graph->SetFillStyle(1001);
   graph->SetLineWidth(0);
   graph->SetPoint(0, 0., 0.1);
   graph->SetPoint(1, 14.8, 8);
   graph->SetPoint(2, 0, 8);
   graph->SetPoint(3, 0, 0.1);
   graph->Draw("F");

   graph = new TGraph(4);
   graph->SetFillColor(0);
   graph->SetFillStyle(1001);
   graph->SetLineWidth(0);
   graph->SetPoint(0, 0, -0.1);
   graph->SetPoint(1, 14.8, -8);
   graph->SetPoint(2, 0, -8);
   graph->SetPoint(3, 0, -0.1);
   graph->Draw("F");

   TLine * line;
   line = new TLine(15,-10, 15, 0 - 0.5*d -0.2);
   line->SetLineWidth(10); line->Draw();
   line = new TLine(15, 0 - 0.5*d +0.2 ,15, 0 + 0.5*d -0.2);
   line->SetLineWidth(10); line->Draw();

   line = new TLine(15,0 + 0.5*d + 0.2,15, 10);
   line->SetLineWidth(10); line->Draw();

   pad ->cd();
   finter = new TF2("interference",interference, 0.01, 14, -10, 10, 4);

   finter->SetParameters(amp, lambda, d, 1);
   finter->SetNpx(200);
   finter->SetNpy(200);
   finter->SetContour(colNum-2);
   finter->Draw("samecolorz");

   TArc *arc = new TArc();;
   arc->SetFillStyle(0);
   arc->SetLineWidth(2);
   arc->SetLineColor(5);
   Float_t r = 0.5 * lambda, dr = lambda;
   for (Int_t i = 0; i < 16; i++) {
      arc->DrawArc(0,  0.5*d, r, 0., 360., "only");
      arc->DrawArc(0, -0.5*d, r, 0., 360., "only");
      r += dr;
   }

   pad ->cd();
   TF2 * fresult = new TF2("result",result, 14, 15, -10, 10, 4);

   fresult->SetParameters(amp, lambda, d, 1);
   fresult->SetNpx(300);
   fresult->SetNpy(300);
   fresult->SetContour(colNum-2);
   fresult->Draw("samecolor");
   line = new TLine(13.8,-10, 14, 10);
   line->SetLineWidth(10); line->SetLineColor(0); line->Draw();
   c1->Modified(kTRUE);
   c1->Update();
   c1->SetEditable(kTRUE);
}
 waves.C:1
 waves.C:2
 waves.C:3
 waves.C:4
 waves.C:5
 waves.C:6
 waves.C:7
 waves.C:8
 waves.C:9
 waves.C:10
 waves.C:11
 waves.C:12
 waves.C:13
 waves.C:14
 waves.C:15
 waves.C:16
 waves.C:17
 waves.C:18
 waves.C:19
 waves.C:20
 waves.C:21
 waves.C:22
 waves.C:23
 waves.C:24
 waves.C:25
 waves.C:26
 waves.C:27
 waves.C:28
 waves.C:29
 waves.C:30
 waves.C:31
 waves.C:32
 waves.C:33
 waves.C:34
 waves.C:35
 waves.C:36
 waves.C:37
 waves.C:38
 waves.C:39
 waves.C:40
 waves.C:41
 waves.C:42
 waves.C:43
 waves.C:44
 waves.C:45
 waves.C:46
 waves.C:47
 waves.C:48
 waves.C:49
 waves.C:50
 waves.C:51
 waves.C:52
 waves.C:53
 waves.C:54
 waves.C:55
 waves.C:56
 waves.C:57
 waves.C:58
 waves.C:59
 waves.C:60
 waves.C:61
 waves.C:62
 waves.C:63
 waves.C:64
 waves.C:65
 waves.C:66
 waves.C:67
 waves.C:68
 waves.C:69
 waves.C:70
 waves.C:71
 waves.C:72
 waves.C:73
 waves.C:74
 waves.C:75
 waves.C:76
 waves.C:77
 waves.C:78
 waves.C:79
 waves.C:80
 waves.C:81
 waves.C:82
 waves.C:83
 waves.C:84
 waves.C:85
 waves.C:86
 waves.C:87
 waves.C:88
 waves.C:89
 waves.C:90
 waves.C:91
 waves.C:92
 waves.C:93
 waves.C:94
 waves.C:95
 waves.C:96
 waves.C:97
 waves.C:98
 waves.C:99
 waves.C:100
 waves.C:101
 waves.C:102
 waves.C:103
 waves.C:104
 waves.C:105
 waves.C:106
 waves.C:107
 waves.C:108
 waves.C:109
 waves.C:110
 waves.C:111
 waves.C:112
 waves.C:113
 waves.C:114
 waves.C:115
 waves.C:116
 waves.C:117
 waves.C:118
 waves.C:119
 waves.C:120
 waves.C:121
 waves.C:122
 waves.C:123
 waves.C:124
 waves.C:125
 waves.C:126
 waves.C:127
 waves.C:128
 waves.C:129
 waves.C:130
 waves.C:131
 waves.C:132
 waves.C:133
 waves.C:134
 waves.C:135
 waves.C:136
 waves.C:137
 waves.C:138
 waves.C:139
 waves.C:140
 waves.C:141
 waves.C:142
 waves.C:143
 waves.C:144
 waves.C:145
 waves.C:146
 waves.C:147
thumb