{ "cells": [ { "cell_type": "markdown", "id": "76708d62", "metadata": {}, "source": [ "# graph2dfit\n", "Fitting a TGraph2D\n", "\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:24 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "b5c39bc5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:02.433082Z", "iopub.status.busy": "2026-05-19T20:25:02.432951Z", "iopub.status.idle": "2026-05-19T20:25:02.789551Z", "shell.execute_reply": "2026-05-19T20:25:02.788834Z" } }, "outputs": [], "source": [ "gStyle->SetOptStat(0);\n", "gStyle->SetOptFit();\n", "\n", "auto c = new TCanvas(\"c\",\"Graph2D example\",0,0,600,800);\n", "c->Divide(2,3);\n", "\n", "double rnd, x, y, z;\n", "double e = 0.3;\n", "int nd = 400;\n", "int np = 10000;\n", "\n", "TRandom r;\n", "double fl = 6;\n", "auto f2 = new TF2(\"f2\",\"1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200\",\n", " -fl,fl,-fl,fl);\n", "f2->SetParameters(1,1);\n", "auto dt = new TGraph2D();" ] }, { "cell_type": "markdown", "id": "1e7ee86a", "metadata": {}, "source": [ "Fill the 2D graph" ] }, { "cell_type": "code", "execution_count": 2, "id": "14622e1d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:02.791548Z", "iopub.status.busy": "2026-05-19T20:25:02.791424Z", "iopub.status.idle": "2026-05-19T20:25:03.144052Z", "shell.execute_reply": "2026-05-19T20:25:03.143414Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 4.25425e+06\n", "NDf = 398\n", "Edm = 5.98608e-13\n", "NCalls = 45\n", "p0 = 0.574556 +/- 0.109977 \n", "p1 = 1.72367 +/- 0.329932 \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning in : function:f2 has 24 negative values: abs assumed\n", "Warning in : function:f2 has 24 negative values: abs assumed\n" ] } ], "source": [ "double zmax = 0;\n", "for (int N=0; NGetRandom2(x,y);\n", " // Generate a random number in [-e,e]\n", " rnd = 2*r.Rndm()*e-e;\n", " z = f2->Eval(x,y)*(1+rnd);\n", " if (z>zmax) zmax = z;\n", " dt->SetPoint(N,x,y,z);\n", "}\n", "\n", "double hr = 350;\n", "auto h1 = new TH1D(\"h1\",\n", "\"#splitline{Difference between Original}{#splitline{function and Function}{with noise}}\",\n", "100, -hr, hr);\n", "auto h2 = new TH1D(\"h2\",\n", "\"#splitline{Difference between Original}{#splitline{function and Delaunay triangles}{interpolation}}\",\n", "100, -hr, hr);\n", "auto h3 = new TH1D(\"h3\",\n", "\"#splitline{Difference between Original}{function and Minuit fit}\",\n", "500, -hr, hr);\n", "\n", "f2->SetParameters(0.5,1.5);\n", "dt->Fit(f2);\n", "auto fit2 = (TF2*)dt->FindObject(\"f2\");\n", "\n", "f2->SetParameters(1,1);\n", "\n", "for (int N=0; NGetRandom2(x,y);\n", " // Generate a random number in [-e,e]\n", " rnd = 2*r.Rndm()*e-e;\n", " z = f2->Eval(x,y)*(1+rnd);\n", " h1->Fill(f2->Eval(x,y)-z);\n", " z = dt->Interpolate(x,y);\n", " h2->Fill(f2->Eval(x,y)-z);\n", " z = fit2->Eval(x,y);\n", " h3->Fill(f2->Eval(x,y)-z);\n", "}\n", "\n", "c->cd(1);\n", "f2->SetTitle(\"Original function with Graph2D points on top\");\n", "f2->SetMaximum(zmax);\n", "gStyle->SetHistTopMargin(0);\n", "f2->Draw(\"surf1\");\n", "dt->Draw(\"same p0\");\n", "\n", "c->cd(3);\n", "dt->SetMargin(0.1);\n", "dt->SetFillColor(36);\n", "dt->SetTitle(\"Histogram produced with Delaunay interpolation\");\n", "dt->Draw(\"surf4\");\n", "\n", "c->cd(5);\n", "fit2->SetTitle(\"Minuit fit result on the Graph2D points\");\n", "fit2->Draw(\"surf1\");\n", "\n", "h1->SetFillColor(47);\n", "h2->SetFillColor(38);\n", "h3->SetFillColor(29);\n", "\n", "c->cd(2); h1->Fit(\"gaus\",\"Q\") ; h1->Draw();\n", "c->cd(4); h2->Fit(\"gaus\",\"Q\") ; h2->Draw();\n", "c->cd(6); h3->Fit(\"gaus\",\"Q\") ; h3->Draw();\n", "c->cd();" ] }, { "cell_type": "markdown", "id": "647cb270", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 3, "id": "b0fd3632", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:03.145892Z", "iopub.status.busy": "2026-05-19T20:25:03.145768Z", "iopub.status.idle": "2026-05-19T20:25:03.464175Z", "shell.execute_reply": "2026-05-19T20:25:03.463563Z" } }, "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 }