{
"cells": [
{
"cell_type": "markdown",
"id": "c108268a",
"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 Monday, December 22, 2025 at 04:26 AM."
]
},
{
"cell_type": "markdown",
"id": "2f1b9f63",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "26414023",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2025-12-22T03:26:32.888975Z",
"iopub.status.busy": "2025-12-22T03:26:32.888818Z",
"iopub.status.idle": "2025-12-22T03:26:32.893754Z",
"shell.execute_reply": "2025-12-22T03:26:32.893079Z"
}
},
"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": "736f25b9",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3b27ec99",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2025-12-22T03:26:32.897021Z",
"iopub.status.busy": "2025-12-22T03:26:32.896874Z",
"iopub.status.idle": "2025-12-22T03:26:32.936304Z",
"shell.execute_reply": "2025-12-22T03:26:32.931201Z"
}
},
"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": "ed9bccb3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2025-12-22T03:26:32.951851Z",
"iopub.status.busy": "2025-12-22T03:26:32.951640Z",
"iopub.status.idle": "2025-12-22T03:26:33.312557Z",
"shell.execute_reply": "2025-12-22T03:26:33.311867Z"
}
},
"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": "7935b741",
"metadata": {},
"source": [
"Create an histogram and fill it randomly with f2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "efef9dc2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2025-12-22T03:26:33.320343Z",
"iopub.status.busy": "2025-12-22T03:26:33.320145Z",
"iopub.status.idle": "2025-12-22T03:26:33.540252Z",
"shell.execute_reply": "2025-12-22T03:26:33.534328Z"
}
},
"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": "38a119a9",
"metadata": {},
"source": [
"Fit h2 with original function f2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4b9a8dc1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2025-12-22T03:26:33.548634Z",
"iopub.status.busy": "2025-12-22T03:26:33.548462Z",
"iopub.status.idle": "2025-12-22T03:26:33.760624Z",
"shell.execute_reply": "2025-12-22T03:26:33.760209Z"
}
},
"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_60: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": "387c1aaa",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f1580644",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2025-12-22T03:26:33.763841Z",
"iopub.status.busy": "2025-12-22T03:26:33.763705Z",
"iopub.status.idle": "2025-12-22T03:26:33.977428Z",
"shell.execute_reply": "2025-12-22T03:26:33.976861Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\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
}