{ "cells": [ { "cell_type": "markdown", "id": "f74debd5", "metadata": {}, "source": [ "# gr201_waves\n", "here to draw triangles on top of a 2D function in order to hide parts of it.\n", "\n", "\n", "\n", "**Author:** Otto Schaile \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:38 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "b0b10f94", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:29.553600Z", "iopub.status.busy": "2026-05-19T20:38:29.553490Z", "iopub.status.idle": "2026-05-19T20:38:29.566129Z", "shell.execute_reply": "2026-05-19T20:38:29.565589Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TROOT.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TColor.h\"\n", "#include \"TArc.h\"\n", "#include \"TGraph.h\"\n", "#include \"TF2.h\"\n", "#include \"TLine.h\"\n", "#include \"TLatex.h\"\n", "#include \"TMath.h\"\n", "#include \"TStyle.h\"" ] }, { "cell_type": "markdown", "id": "0ff3e085", "metadata": {}, "source": [ " ______________________________________________________________\n", " " ] }, { "cell_type": "code", "execution_count": 2, "id": "17b02e01", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:29.567947Z", "iopub.status.busy": "2026-05-19T20:38:29.567831Z", "iopub.status.idle": "2026-05-19T20:38:29.571584Z", "shell.execute_reply": "2026-05-19T20:38:29.571105Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "Double_t interference( Double_t *x, Double_t *par)\n", "{\n", " Double_t x_p2 = x[0] * x[0];\n", " Double_t d_2 = 0.5 * par[2];\n", " Double_t ym_p2 = (x[1] - d_2) * (x[1] - d_2);\n", " Double_t yp_p2 = (x[1] + d_2) * (x[1] + d_2);\n", " Double_t tpi_l = TMath::Pi() / par[1];\n", " Double_t amplitude = par[0] * (cos(tpi_l * sqrt(x_p2 + ym_p2))\n", " + par[3] * cos(tpi_l * sqrt(x_p2 + yp_p2)));\n", " return amplitude * amplitude;\n", "}" ] }, { "cell_type": "markdown", "id": "117697af", "metadata": {}, "source": [ " _____________________________________________________________\n", " " ] }, { "cell_type": "code", "execution_count": 3, "id": "2a6d5fa8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:29.573462Z", "iopub.status.busy": "2026-05-19T20:38:29.573347Z", "iopub.status.idle": "2026-05-19T20:38:29.576464Z", "shell.execute_reply": "2026-05-19T20:38:29.576071Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "Double_t result( Double_t *x, Double_t *par)\n", "{\n", " Double_t xint[2];\n", " Double_t maxintens = 0, xcur = 14;\n", " Double_t dlambda = 0.1 * par[1];\n", " for(Int_t i=0; i<10; i++){\n", " xint[0] = xcur;\n", " xint[1] = x[1];\n", " Double_t intens = interference(xint, par);\n", " if(intens > maxintens) maxintens = intens;\n", " xcur -= dlambda;\n", " }\n", " return maxintens;\n", "}" ] }, { "cell_type": "markdown", "id": "1a4b3a59", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 4, "id": "0cb81d6d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:29.578311Z", "iopub.status.busy": "2026-05-19T20:38:29.578124Z", "iopub.status.idle": "2026-05-19T20:38:29.921532Z", "shell.execute_reply": "2026-05-19T20:38:29.921108Z" } }, "outputs": [], "source": [ "Double_t d = 3;\n", "Double_t lambda = 1;\n", "Double_t amp = 10;" ] }, { "cell_type": "code", "execution_count": 5, "id": "946d8c6b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:29.944482Z", "iopub.status.busy": "2026-05-19T20:38:29.944304Z", "iopub.status.idle": "2026-05-19T20:38:30.162912Z", "shell.execute_reply": "2026-05-19T20:38:30.162379Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"gr201_waves\", \"A double slit experiment\", 300, 40, 1004, 759);\n", "c1->Range(0, -10, 30, 10);\n", "c1->SetFillColor(0);\n", "TPad *pad = new TPad(\"pr\", \"pr\", 0.5, 0, 1., 1);\n", "pad->Range(0, -10, 15, 10);\n", "pad->Draw();\n", "\n", "const Int_t colNum = 30;\n", "Int_t palette[colNum];\n", "for (Int_t i = 0; i < colNum; i++) {\n", " Float_t level = 1.*i/colNum;\n", " palette[i] = TColor::GetColor((Float_t) TMath::Power(level,0.3), (Float_t) TMath::Power(level,0.3), (Float_t) 0.5*level);\n", " // palette[i] = 1001+i;\n", "}\n", "gStyle->SetPalette(colNum, palette);\n", "\n", "c1->cd();" ] }, { "cell_type": "markdown", "id": "33ed044a", "metadata": {}, "source": [ "For the incoming waves, on the left side, we use a TF2 and increase the number\n", "of points used for drawing to 200 (default is 100) for better resolution" ] }, { "cell_type": "code", "execution_count": 6, "id": "a3a4b408", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:30.165086Z", "iopub.status.busy": "2026-05-19T20:38:30.164944Z", "iopub.status.idle": "2026-05-19T20:38:30.372740Z", "shell.execute_reply": "2026-05-19T20:38:30.372110Z" } }, "outputs": [], "source": [ "TF2 *f0 = new TF2(\"ray_source\", interference, 0.02, 15, -8, 8, 4);\n", "f0->SetParameters(amp, lambda, 0, 0);\n", "f0->SetNpx(200);\n", "f0->SetNpy(200);\n", "f0->SetContour(colNum-2);\n", "f0->Draw(\"samecol\");\n", "\n", "TLatex title;\n", "title.DrawLatex(1.6, 8.5, \"A double slit experiment\");" ] }, { "cell_type": "markdown", "id": "6c839d2f", "metadata": {}, "source": [ "Draw 2 white triangles to cover parts of f0, simulating a cone\n", "(change the fill color to see these)" ] }, { "cell_type": "code", "execution_count": 7, "id": "cf3c3682", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:30.375113Z", "iopub.status.busy": "2026-05-19T20:38:30.374987Z", "iopub.status.idle": "2026-05-19T20:38:30.582278Z", "shell.execute_reply": "2026-05-19T20:38:30.581733Z" } }, "outputs": [], "source": [ "TGraph *graph = new TGraph(4);\n", "graph->SetFillColor(1);\n", "graph->SetFillStyle(1001);\n", "graph->SetLineWidth(0);\n", "graph->SetPoint(0, 0., 0.1);\n", "graph->SetPoint(1, 14.8, 8);\n", "graph->SetPoint(2, 0, 8);\n", "graph->SetPoint(3, 0, 0.1);\n", "graph->Draw(\"F\");\n", "\n", "graph = new TGraph(4);\n", "graph->SetFillColor(1);\n", "graph->SetFillStyle(1001);\n", "graph->SetLineWidth(0);\n", "graph->SetPoint(0, 0, -0.1);\n", "graph->SetPoint(1, 14.8, -8);\n", "graph->SetPoint(2, 0, -8);\n", "graph->SetPoint(3, 0, -0.1);\n", "graph->Draw(\"F\");" ] }, { "cell_type": "markdown", "id": "56c99e59", "metadata": {}, "source": [ "To represent the wall with 2 openings, we draw 3 black vertical lines" ] }, { "cell_type": "code", "execution_count": 8, "id": "25f1db62", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:30.584790Z", "iopub.status.busy": "2026-05-19T20:38:30.584661Z", "iopub.status.idle": "2026-05-19T20:38:30.796469Z", "shell.execute_reply": "2026-05-19T20:38:30.795928Z" } }, "outputs": [], "source": [ "TLine * line = new TLine(15,-10, 15, 0 - 0.5*d - 0.2);\n", "line->SetLineWidth(10);\n", "line->Draw();\n", "\n", "line = new TLine(15, 0 - 0.5*d + 0.2, 15, 0 + 0.5*d - 0.2);\n", "line->SetLineWidth(10);\n", "line->Draw();\n", "\n", "line = new TLine(15, 0 + 0.5*d + 0.2, 15, 10);\n", "line->SetLineWidth(10);\n", "line->Draw();\n", "\n", "pad->cd();" ] }, { "cell_type": "markdown", "id": "35a25cf7", "metadata": {}, "source": [ "Interference plot, on the centre-right side" ] }, { "cell_type": "code", "execution_count": 9, "id": "086afeb2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:30.798668Z", "iopub.status.busy": "2026-05-19T20:38:30.798531Z", "iopub.status.idle": "2026-05-19T20:38:31.005817Z", "shell.execute_reply": "2026-05-19T20:38:31.005367Z" } }, "outputs": [], "source": [ "TF2 *finter = new TF2(\"interference\",interference, 0.01, 14, -10, 10, 4);\n", "finter->SetParameters(amp, lambda, d, 1);\n", "finter->SetNpx(200);\n", "finter->SetNpy(200);\n", "finter->SetContour(colNum-2);\n", "finter->Draw(\"samecol\");" ] }, { "cell_type": "markdown", "id": "3ae087ee", "metadata": {}, "source": [ "Some lines" ] }, { "cell_type": "code", "execution_count": 10, "id": "a702a738", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:31.019452Z", "iopub.status.busy": "2026-05-19T20:38:31.019291Z", "iopub.status.idle": "2026-05-19T20:38:31.231266Z", "shell.execute_reply": "2026-05-19T20:38:31.230634Z" } }, "outputs": [], "source": [ "TArc arc;\n", "arc.SetFillStyle(0);\n", "arc.SetLineWidth(2);\n", "arc.SetLineColor(5);\n", "Float_t r = 0.5 * lambda, dr = lambda;\n", "for (Int_t i = 0; i < 16; i++) {\n", " arc.DrawArc(0, 0.5*d, r, 0., 360., \"only\");\n", " arc.DrawArc(0, -0.5*d, r, 0., 360., \"only\");\n", " r += dr;\n", "}\n", "\n", "pad->cd();" ] }, { "cell_type": "markdown", "id": "f934f894", "metadata": {}, "source": [ "Result, on the right edge" ] }, { "cell_type": "code", "execution_count": 11, "id": "1cbb5ac7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:31.233184Z", "iopub.status.busy": "2026-05-19T20:38:31.233064Z", "iopub.status.idle": "2026-05-19T20:38:31.440558Z", "shell.execute_reply": "2026-05-19T20:38:31.439956Z" } }, "outputs": [], "source": [ "TF2 *fresult = new TF2(\"result\",result, 14, 15, -10, 10, 4);\n", "fresult->SetParameters(amp, lambda, d, 1);\n", "fresult->SetNpx(300);\n", "fresult->SetNpy(300);\n", "fresult->SetContour(colNum-2);\n", "fresult->Draw(\"samecol\");" ] }, { "cell_type": "markdown", "id": "71535bfe", "metadata": {}, "source": [ "Vertical white line on the right side" ] }, { "cell_type": "code", "execution_count": 12, "id": "1b4478f0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:31.442446Z", "iopub.status.busy": "2026-05-19T20:38:31.442321Z", "iopub.status.idle": "2026-05-19T20:38:32.703733Z", "shell.execute_reply": "2026-05-19T20:38:32.703113Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "