{
"cells": [
{
"cell_type": "markdown",
"id": "5a4d3825",
"metadata": {},
"source": [
"# fit2\n",
"Fitting a 2-D histogram\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\n",
"\n",
"This example can be executed via the interpreter or ACLIC\n",
"\n",
"```cpp\n",
" root > .x fit2.C\n",
" root > .x fit2.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": "a498603f",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2e5cf80a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:40.124534Z",
"iopub.status.busy": "2026-05-19T20:24:40.124423Z",
"iopub.status.idle": "2026-05-19T20:24:40.128245Z",
"shell.execute_reply": "2026-05-19T20:24:40.127829Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \"TF2.h\"\n",
"#include \"TH2.h\"\n",
"#include \"TMath.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": "c95348a0",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4545cf52",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:40.151436Z",
"iopub.status.busy": "2026-05-19T20:24:40.151281Z",
"iopub.status.idle": "2026-05-19T20:24:40.158616Z",
"shell.execute_reply": "2026-05-19T20:24:40.158098Z"
}
},
"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": "ee091c77",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:40.160164Z",
"iopub.status.busy": "2026-05-19T20:24:40.160049Z",
"iopub.status.idle": "2026-05-19T20:24:40.488598Z",
"shell.execute_reply": "2026-05-19T20:24:40.487511Z"
}
},
"outputs": [],
"source": [
"const int npar = 15;\n",
"double f2params[npar] =\n",
" {100,-3,3,-3,3,160,0,0.8,0,0.9,40,4,0.7,4,0.7};\n",
"TF2 *f2 = new TF2(\"f2\",fun2,-10,10,-10,10, npar);\n",
"f2->SetParameters(f2params);"
]
},
{
"cell_type": "markdown",
"id": "64d08815",
"metadata": {},
"source": [
"Create an histogram and fill it randomly with f2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f2ce186b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:40.490248Z",
"iopub.status.busy": "2026-05-19T20:24:40.490125Z",
"iopub.status.idle": "2026-05-19T20:24:40.695235Z",
"shell.execute_reply": "2026-05-19T20:24:40.694061Z"
}
},
"outputs": [],
"source": [
"TH2F *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": "0dfdf1a4",
"metadata": {},
"source": [
"Fit h2 with original function f2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "50b92f8a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:40.697098Z",
"iopub.status.busy": "2026-05-19T20:24:40.696974Z",
"iopub.status.idle": "2026-05-19T20:24:40.911932Z",
"shell.execute_reply": "2026-05-19T20:24:40.906952Z"
}
},
"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_56: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",
"Info in : created default TCanvas with name c1\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\");"
]
},
{
"cell_type": "markdown",
"id": "8bc17d53",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "34324d05",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:40.918312Z",
"iopub.status.busy": "2026-05-19T20:24:40.918182Z",
"iopub.status.idle": "2026-05-19T20:24:41.125032Z",
"shell.execute_reply": "2026-05-19T20:24:41.124652Z"
}
},
"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
}