{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial: Drawing the interference pattern of light falling on a grid with n slits and a ratio of r of slit width over distance between slits in C++\n", "
\n", "Original translation to notebook by: Theis Hansen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this tutorial, we are going to learn to code the aforementioned grid in C++\n", "To begin with, we need to import ROOT so that we can use the toolkit. We also set our Notebook to C++" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Writing the Function\n", "We use ROOT Namespace TMath to access the mathimatical toolkit of ROOT. In this first line we are setting the value for our constant \"pi\", and we use the math toolkit to state that our \"pi\" is the mathimatical constant \"Pi\"." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%jsroot on" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(double) 3.14159\n" ] } ], "source": [ "auto pi = TMath::Pi()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we are writing the code for our functions that we will call on later. The \"*\"s are pointers, that access a certain area of the memory (only in the first set of parentheses of each line, the others are multiplications).\n", "\n", "The \"%%cpp -d\" marks a cell as used for declaration of functions. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "double single(double *x, double *par){return pow(sin(pi*par[0]*x[0])/(pi*par[0]*x[0]),2);};\n", "double nslit0(double *x, double *par){return pow(sin(pi*par[1]*x[0])/sin(pi*x[0]),2);};\n", "double nslit(double *x, double *par){return single(x,par) * nslit0(x,par);};" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We set the number of slits to 4, and the ration of slit width to distance between slits to 2." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(float) 2.00000f\n" ] } ], "source": [ "float r=.2,ns=2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we are using the ROOT Type TF1 to write our function and we add the \"*\" pointer before our Fnslit object so that the information can be saved to the computer's memory. \n", "We set the options for our function with the command SetNpx. Here we use the \"->\" because we are setting a command to an object with a pointer." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TF1 Fnslit(\"Fnslit\",nslit,-5.001,5.,2);\n", "Fnslit.SetNpx(500);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here you can see that we are setting the parameters for the code, so that we can apply them as fixed variables that we state at the beginning of our code. This saves us the trouble of entering long numbers over and over again. Note that the parameter number starts at 0, not 1." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Fnslit.SetParameters(r,ns)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we've got our code nicely written out, we can use the \"Draw\" command so that we can see the graph." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TCanvas c;\n", "Fnslit.Draw();\n", "c.Draw();" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 0 }