{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Building a Graph with Errors\n", "
\n", "\n", "In this example we will build a graph with errors, display it and save it as an image." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First declare the values and the errors on the Y axis." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "std::vector x_vals {1,2,3,4,5,6,7,8,9,10};\n", "std::vector y_vals {6,12,14,20,22,24,35,45,44,53};\n", "std::vector y_errs {5,5,4.7,4.5,4.2,5.1,2.9,4.1,4.8,5.43};\n", "auto n_points = x_vals.size();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the instance of the graph. The errors along the x axis are set to zero passing a *nullptr*." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TGraphErrors graph(n_points, x_vals.data(), y_vals.data(), nullptr, y_errs.data());\n", "graph.SetTitle(\"Measurement XYZ;lenght [cm];Arb.Units\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make the plot look better." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "graph.SetMarkerStyle(kOpenCircle);\n", "graph.SetMarkerColor(kBlue);\n", "graph.SetLineColor(kBlue);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the canvas and draw the graph." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TCanvas mycanvas;\n", "graph.Draw(\"APE\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a linear function." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TF1 f(\"Linear law\", \"[0]+x*[1]\", .5, 10.5); " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make the funcion line nicer." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "f.SetLineColor(kRed);\n", "f.SetLineStyle(2);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now fit the function to the graph and draw it." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " FCN=3.84883 FROM MIGRAD STATUS=CONVERGED 31 CALLS 32 TOTAL\n", " EDM=5.96982e-22 STRATEGY= 1 ERROR MATRIX ACCURATE \n", " EXT PARAMETER STEP FIRST \n", " NO. NAME VALUE ERROR SIZE DERIVATIVE \n", " 1 p0 -1.01604e+00 3.33409e+00 1.48321e-03 -8.98235e-12\n", " 2 p1 5.18756e+00 5.30717e-01 2.36095e-04 9.40487e-12\n" ] } ], "source": [ "graph.Fit(&f);\n", "f.Draw(\"Same\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build and draw a legend." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TLegend leg(.1, .7, .3, .9, \"Lab. Lesson 1\");\n", "leg.SetFillColor(0);\n", "graph.SetFillColor(0);\n", "leg.AddEntry(&graph, \"Exp. Points\");\n", "leg.AddEntry(&f, \"Th. Law\");\n", "leg.Draw(\"Same\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Draw an arrow on the canvas." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TArrow arrow(8, 8, 6.2, 23, 0.02, \"|>\");\n", "arrow.SetLineWidth(2);\n", "arrow.Draw();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add some text to the plot." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TLatex text(8.2, 7.5, \"#splitline{Maximum}{Deviation}\");\n", "text.Draw();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display in the notebook what is in your canvas. Note how we first activate the interactive JavaScript visualisation." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%jsroot on\n", "mycanvas.Draw();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the content of your canvas to an image file on disk." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Info in : pdf file graph_with_law.pdf has been created\n" ] } ], "source": [ "mycanvas.Print(\"graph_with_law.pdf\");" ] } ], "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 }