{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# PyROOT Exercise 2: Graphs and Fits" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "\n", "Write a python macro `ExerciseGraph.py`.\n", "\n", "1. Create a graph with symmetric errors and 5 points.\n", "2. Set the following points:\n", " `(1.0, 2.1), (2.0, 2.9), (3.0, 4.05), (4.0, 5.2), (5.0, 5.95)`\n", "3. Set the errors on `x` to 0.0 and the errors on `y` to 0.1 (all at once).\n", "4. Draw the graph including the axes and error bars.\n", "5. Create a one dimensional function `f(x) = ax + b` and fit it to the graph.\n", "\n", "Bonus questions:\n", "1. Programatically obtain the two parameters `a` and `b` and their estimated uncertainties." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Solution" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to JupyROOT 6.28/00\n" ] } ], "source": [ "import ROOT\n", "%jsroot on" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = 1.000000 +/- 0.031623\n", "b = 1.040000 +/- 0.104881\n", " FCN=5.7 FROM MIGRAD STATUS=CONVERGED 33 CALLS 34 TOTAL\n", " EDM=2.71359e-21 STRATEGY= 1 ERROR MATRIX ACCURATE \n", " EXT PARAMETER STEP FIRST \n", " NO. NAME VALUE ERROR SIZE DERIVATIVE \n", " 1 p0 1.00000e+00 3.16228e-02 1.70422e-05 -2.08465e-09\n", " 2 p1 1.04000e+00 1.04881e-01 5.65226e-05 -1.21781e-09\n" ] }, { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ROOT import TCanvas, TF1, TGraphErrors\n", "\n", "canvas = TCanvas(\"canvas\", \"\", 800, 600)\n", "\n", "graph = TGraphErrors(5)\n", "graph.SetPoint(0, 1.0, 2.1)\n", "graph.SetPoint(1, 2.0, 2.9)\n", "graph.SetPoint(2, 3.0, 4.05)\n", "graph.SetPoint(3, 4.0, 5.2)\n", "graph.SetPoint(4, 5.0, 5.95)\n", "for i in range(5):\n", " graph.SetPointError(i, 0, 0.1)\n", "\n", "funct = TF1(\"funct\", \"[0] * x + [1]\", 0, 6)\n", "graph.Fit(\"funct\")\n", "\n", "graph.Draw()\n", "funct.Draw(\"same\")\n", "canvas.Draw()\n", "\n", "print(f\"a = {funct.GetParameter(0):.6f} +/- {funct.GetParError(0):.6f}\")\n", "print(f\"b = {funct.GetParameter(1):.6f} +/- {funct.GetParError(1):.6f}\")" ] } ], "metadata": { "kernelspec": { "display_name": "LCG 103", "language": "python", "name": "lcg_103" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } } }, "nbformat": 4, "nbformat_minor": 2 }