{ "cells": [ { "cell_type": "markdown", "id": "8b72a87e", "metadata": {}, "source": [ "# fit2a\n", "Fitting a 2-D histogram (a variant)\n", "This tutorial illustrates :\n", " - how to create a 2-d function\n", " - fill a 2-d histogram randomly from this function\n", " - fit the histogram\n", " - display the fitted function on top of the histogram (lego-plot)\n", " using a surface plot in a sub-range of the histogram.\n", "\n", "This example can be executed via the interpreter or/and the compiler\n", "\n", "```cpp\n", " root > .x fit2a.C\n", " root > .x fit2a.C++\n", "```\n", "\n", "\n", "\n", "\n", "**Author:** Rene Brun \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": "markdown", "id": "680e4b49", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 1, "id": "17da207e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:43.996549Z", "iopub.status.busy": "2026-05-19T20:24:43.996387Z", "iopub.status.idle": "2026-05-19T20:24:44.001122Z", "shell.execute_reply": "2026-05-19T20:24:44.000502Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "\n", "#include \"TF2.h\"\n", "#include \"TH2.h\"\n", "#include \"TCutG.h\"\n", "#include \"TMath.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TStyle.h\"\n", "\n", "double g2(double *x, double *par) {\n", " double r1 = double((x[0]-par[1])/par[2]);\n", " double r2 = double((x[1]-par[3])/par[4]);\n", " return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2));\n", "}" ] }, { "cell_type": "markdown", "id": "207c30ae", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "a6380d8d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:44.002748Z", "iopub.status.busy": "2026-05-19T20:24:44.002612Z", "iopub.status.idle": "2026-05-19T20:24:44.005713Z", "shell.execute_reply": "2026-05-19T20:24:44.005238Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "double fun2(double *x, double *par) {\n", " double *p1 = &par[0];\n", " double *p2 = &par[5];\n", " double *p3 = &par[10];\n", " double result = g2(x,p1) + g2(x,p2) + g2(x,p3);\n", " return result;\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "id": "5ad11233", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:44.007215Z", "iopub.status.busy": "2026-05-19T20:24:44.007092Z", "iopub.status.idle": "2026-05-19T20:24:44.456438Z", "shell.execute_reply": "2026-05-19T20:24:44.456018Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TCanvas *c = new TCanvas();\n", "gStyle->SetOptStat(true);\n", "gStyle->SetPalette(57);\n", "const int npar = 15;\n", "double f2params[npar] = {100,-3,3,-3,3,160,0,0.8,0,0.9,40,4,0.7,4,0.7};\n", "auto f2 = new TF2(\"f2\",fun2,-10,10,-10,10, npar);\n", "f2->SetParameters(f2params);" ] }, { "cell_type": "markdown", "id": "71976e37", "metadata": {}, "source": [ "Create an histogram and fill it randomly with f2" ] }, { "cell_type": "code", "execution_count": 4, "id": "ad0dd00c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:44.479407Z", "iopub.status.busy": "2026-05-19T20:24:44.479261Z", "iopub.status.idle": "2026-05-19T20:24:44.684495Z", "shell.execute_reply": "2026-05-19T20:24:44.683858Z" } }, "outputs": [], "source": [ "auto h2 = new TH2F(\"h2\",\"From f2\",40,-10,10,40,-10,10);\n", "int nentries = 100000;\n", "h2->FillRandom(\"f2\",nentries);" ] }, { "cell_type": "markdown", "id": "673e8f4b", "metadata": {}, "source": [ "Fit h2 with original function f2" ] }, { "cell_type": "code", "execution_count": 5, "id": "c46b07a7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:44.687821Z", "iopub.status.busy": "2026-05-19T20:24:44.687699Z", "iopub.status.idle": "2026-05-19T20:24:44.894910Z", "shell.execute_reply": "2026-05-19T20:24:44.894548Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 1048.29\n", "NDf = 1139\n", "Edm = 3.90056e-06\n", "NCalls = 519\n", "p0 = 392.558 +/- 2.07088 \n", "p1 = -2.99838 +/- 0.0116072 \n", "p2 = 2.98484 +/- 0.00840711 \n", "p3 = -3.00201 +/- 0.0115172 \n", "p4 = 2.97271 +/- 0.00841038 \n", "p5 = 601.133 +/- 10.5562 \n", "p6 = 0.00614073 +/- 0.0119548 \n", "p7 = 0.81626 +/- 0.0107847 \n", "p8 = -0.000781266 +/- 0.0134062 \n", "p9 = 0.911288 +/- 0.0119899 \n", "p10 = 146.899 +/- 5.12261 \n", "p11 = 3.9882 +/- 0.0182639 \n", "p12 = 0.727561 +/- 0.0142962 \n", "p13 = 4.02637 +/- 0.0175896 \n", "p14 = 0.703077 +/- 0.0140242 \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "input_line_70:2:2: warning: 'ratio' shadows a declaration with the same name in the 'std' namespace; use '::ratio' to reference this declaration\n", " float ratio = 4*nentries/100000;\n", " ^\n" ] } ], "source": [ "float ratio = 4*nentries/100000;\n", "f2params[ 0] *= ratio;\n", "f2params[ 5] *= ratio;\n", "f2params[10] *= ratio;\n", "f2->SetParameters(f2params);\n", "h2->Fit(\"f2\",\"N\");\n", "auto cutg = new TCutG(\"cutg\",5);\n", "cutg->SetPoint(0,-7,-7);\n", "cutg->SetPoint(1, 2,-7);\n", "cutg->SetPoint(2, 2, 2);\n", "cutg->SetPoint(3,-7, 2);\n", "cutg->SetPoint(4,-7,-7);\n", "h2->Draw(\"lego2 0\");\n", "h2->SetFillColor(38);\n", "f2->SetNpx(80);\n", "f2->SetNpy(80);\n", "f2->Draw(\"surf1 same bb [cutg]\");\n", "return c;" ] }, { "cell_type": "markdown", "id": "cd5a8880", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 6, "id": "a1a7be1d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:44.897965Z", "iopub.status.busy": "2026-05-19T20:24:44.897836Z", "iopub.status.idle": "2026-05-19T20:24:45.105024Z", "shell.execute_reply": "2026-05-19T20:24:45.103868Z" } }, "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 }