{ "cells": [ { "cell_type": "markdown", "id": "cb4c3bc5", "metadata": {}, "source": [ "# gr011_graph2d_errorsfit\n", "\n", "\n", "\n", "**Author:** Olivier Couet \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:37 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "c2df9519", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:56.278412Z", "iopub.status.busy": "2026-05-19T20:37:56.278286Z", "iopub.status.idle": "2026-05-19T20:37:56.627810Z", "shell.execute_reply": "2026-05-19T20:37:56.627150Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"c1\");\n", "\n", "Double_t rnd, x, y, z, ex, ey, ez;\n", "Double_t e = 0.3;\n", "Int_t nd = 500;" ] }, { "cell_type": "markdown", "id": "4b21a054", "metadata": {}, "source": [ "To generate some random data to put into the graph" ] }, { "cell_type": "code", "execution_count": 2, "id": "7ef28265", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:56.629276Z", "iopub.status.busy": "2026-05-19T20:37:56.629156Z", "iopub.status.idle": "2026-05-19T20:37:56.841022Z", "shell.execute_reply": "2026-05-19T20:37:56.840331Z" } }, "outputs": [], "source": [ "TRandom r;\n", "TF2 *f2 = new TF2(\"f2\",\"1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200\",-6,6,-6,6);\n", "f2->SetParameters(1,1);\n", "\n", "TGraph2DErrors *dte = new TGraph2DErrors(nd);" ] }, { "cell_type": "markdown", "id": "61fce038", "metadata": {}, "source": [ "Fill the 2D graph. It was created only specifying the number of points, so all\n", "elements are empty. We now \"fill\" the values and errors with SetPoint and SetPointError.\n", "Note that the first point has index zero" ] }, { "cell_type": "code", "execution_count": 3, "id": "cfe2966c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:56.842480Z", "iopub.status.busy": "2026-05-19T20:37:56.842363Z", "iopub.status.idle": "2026-05-19T20:37:57.045615Z", "shell.execute_reply": "2026-05-19T20:37:57.045080Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Warning in : function:f2 has 24 negative values: abs assumed\n" ] } ], "source": [ "Double_t zmax = 0;\n", "for (Int_t i=0; iGetRandom2(x,y);\n", " rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]\n", " z = f2->Eval(x,y)*(1+rnd);\n", " if (z>zmax) zmax = z;\n", " dte->SetPoint(i,x,y,z);\n", " ex = 0.05*r.Rndm();\n", " ey = 0.05*r.Rndm();\n", " ez = TMath::Abs(z*rnd);\n", " dte->SetPointError(i,ex,ey,ez);\n", "}" ] }, { "cell_type": "markdown", "id": "c6f741f6", "metadata": {}, "source": [ "If the fit is not needed, just draw dte here and skip the lines below\n", "dte->Draw(\"A p0\");" ] }, { "cell_type": "markdown", "id": "609061be", "metadata": {}, "source": [ "To do the fit we use a function, in this example the same f2 from above" ] }, { "cell_type": "code", "execution_count": 4, "id": "e0b49fd9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:57.052754Z", "iopub.status.busy": "2026-05-19T20:37:57.052593Z", "iopub.status.idle": "2026-05-19T20:37:57.266510Z", "shell.execute_reply": "2026-05-19T20:37:57.265697Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 515.752\n", "NDf = 498\n", "Edm = 8.12059e-06\n", "NCalls = 35\n", "p0 = 0.576226 +/- 0.19029 \n", "p1 = 1.72868 +/- 0.57087 \n" ] } ], "source": [ "f2->SetParameters(0.5,1.5);\n", "dte->Fit(f2);\n", "TF2 *fit2 = (TF2*)dte->FindObject(\"f2\");\n", "fit2->SetTitle(\"Minuit fit result on the Graph2DErrors points\");\n", "fit2->SetMaximum(zmax);\n", "gStyle->SetHistTopMargin(0);\n", "fit2->SetLineColor(1);\n", "fit2->SetLineWidth(1);\n", "fit2->Draw(\"surf1\");\n", "dte->Draw(\"same p0\");" ] }, { "cell_type": "markdown", "id": "c7ade5e5", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "f1dde2a3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:57.268051Z", "iopub.status.busy": "2026-05-19T20:37:57.267927Z", "iopub.status.idle": "2026-05-19T20:37:57.587760Z", "shell.execute_reply": "2026-05-19T20:37:57.587105Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "gROOT->GetListOfCanvases()->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": 5 }