{
"cells": [
{
"cell_type": "markdown",
"id": "abec0e58",
"metadata": {},
"source": [
"# FitHistoInFile\n",
"\n",
"This example can be executed as:\n",
" root > .x FitHistoInFile.C\n",
"\n",
"Based on FittingDemo.C by Rene Brun\n",
"\n",
"\n",
"**Author:** Author E. von Toerne \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": "aa9c1f4b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.248226Z",
"iopub.status.busy": "2026-05-19T20:24:53.248116Z",
"iopub.status.idle": "2026-05-19T20:24:53.255281Z",
"shell.execute_reply": "2026-05-19T20:24:53.254720Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \"TH1.h\"\n",
"#include \"TMath.h\"\n",
"#include \"TF1.h\"\n",
"#include \"TLegend.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TFile.h\"\n",
"#include \"TStyle.h\""
]
},
{
"cell_type": "markdown",
"id": "dc348d40",
"metadata": {},
"source": [
"Function parameters are passed as an array to TF1. Here, we\n",
"define the position of each parameter in this array.\n",
"Note: N_PAR will give us the total number of parameters. Make\n",
"sure it is always the last entry!"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f428fa69",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.256814Z",
"iopub.status.busy": "2026-05-19T20:24:53.256693Z",
"iopub.status.idle": "2026-05-19T20:24:53.579103Z",
"shell.execute_reply": "2026-05-19T20:24:53.578450Z"
}
},
"outputs": [],
"source": [
"enum ParIndex_t {\n",
" Bkg0=0, Bkg1=1, Bkg2,\n",
" SigScale, SigSigma, SigMean,\n",
" N_PAR};"
]
},
{
"cell_type": "markdown",
"id": "1573b591",
"metadata": {},
"source": [
" Use this map t\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cd6c29dc",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.580933Z",
"iopub.status.busy": "2026-05-19T20:24:53.580814Z",
"iopub.status.idle": "2026-05-19T20:24:53.584296Z",
"shell.execute_reply": "2026-05-19T20:24:53.583784Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_54:1:4: error: use of undeclared identifier 're'\n",
"o (re-)name parameters for the plot\n",
" ^\n",
"input_line_54:1:7: error: expected expression\n",
"o (re-)name parameters for the plot\n",
" ^\n"
]
}
],
"source": [
"%%cpp -d\n",
"o (re-)name parameters for the plot\n",
"const std::map parNames{\n",
" {Bkg0, \"Bkg0\"}, {Bkg1, \"Bkg1\"}, {Bkg2, \"Bkg2\"},\n",
" {SigScale, \"Gauss scale\"}, {SigSigma, \"Gauss #sigma\"}, {SigMean, \"Gauss #mu\"}\n",
"};"
]
},
{
"cell_type": "markdown",
"id": "cff9ddd7",
"metadata": {},
"source": [
" Quadratic background function\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a87b7b18",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.585626Z",
"iopub.status.busy": "2026-05-19T20:24:53.585509Z",
"iopub.status.idle": "2026-05-19T20:24:53.588543Z",
"shell.execute_reply": "2026-05-19T20:24:53.588061Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"double background(double *x, double *par) {\n",
" return par[Bkg0] + par[Bkg1]*x[0] + par[Bkg2]*x[0]*x[0];\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "43f5b924",
"metadata": {},
"source": [
" Gauss Peak function\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "770351ea",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.589885Z",
"iopub.status.busy": "2026-05-19T20:24:53.589776Z",
"iopub.status.idle": "2026-05-19T20:24:53.592880Z",
"shell.execute_reply": "2026-05-19T20:24:53.592146Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"double signal(double *x, double *par) {\n",
" return par[SigScale]*TMath::Gaus(x[0], par[SigMean], par[SigSigma], true);\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "f9685488",
"metadata": {},
"source": [
" Sum of background and peak function. We pass x and the fit parameters\n",
"down to the signal and background functions.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6655ee7c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.594068Z",
"iopub.status.busy": "2026-05-19T20:24:53.593962Z",
"iopub.status.idle": "2026-05-19T20:24:53.596705Z",
"shell.execute_reply": "2026-05-19T20:24:53.596184Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"double fitFunction(double *x, double *par) {\n",
" return background(x, par) + signal(x, par);\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "d788658d",
"metadata": {},
"source": [
" Fit \"fitFunction\" to the histogram, and draw results on the canvas `c1`.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e1c6eb80",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.597998Z",
"iopub.status.busy": "2026-05-19T20:24:53.597891Z",
"iopub.status.idle": "2026-05-19T20:24:53.610479Z",
"shell.execute_reply": "2026-05-19T20:24:53.608309Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_58:10:26: error: use of undeclared identifier 'parNames'\n",
" for (auto& idx_name : parNames) {\n",
" ^\n"
]
}
],
"source": [
"%%cpp -d\n",
"void FitRoutine(TCanvas* c1, TH1* histo, float fitxmin, float fitxmax, TString filename){\n",
" c1->cd();\n",
" // create a TF1 with the range from 0 to 3 and N_PAR parameters (six by default)\n",
" TF1 fitFcn(\"fitFcn\",fitFunction,fitxmin,fitxmax,N_PAR);\n",
" fitFcn.SetNpx(500);\n",
" fitFcn.SetLineWidth(2);\n",
" fitFcn.SetLineColor(kBlue);\n",
"\n",
" // Assign the names from the map \"parNames\". Optional, but makes a nicer plot.\n",
" for (auto& idx_name : parNames) {\n",
" fitFcn.SetParName(idx_name.first, idx_name.second.c_str());\n",
" }\n",
"\n",
" // Fit. First set ok-ish starting values for the parameters\n",
" fitFcn.SetParameters(30,0,0,50.,0.1,1.);\n",
" histo->GetXaxis()->SetRange(2,40);\n",
" histo->Fit(\"fitFcn\",\"VR+\",\"ep\");\n",
"\n",
" // improve the picture:\n",
" // Draw signal and background functions separately\n",
" TF1 backFcn(\"backFcn\",background,fitxmin,fitxmax,N_PAR);\n",
" backFcn.SetLineColor(kRed);\n",
" TF1 signalFcn(\"signalFcn\",signal,fitxmin,fitxmax,N_PAR);\n",
" signalFcn.SetLineColor(kBlue);\n",
" signalFcn.SetNpx(500);\n",
"\n",
" // Retrieve fit parameters, and copy them to the signal and background functions\n",
" double par[N_PAR];\n",
" fitFcn.GetParameters(par);\n",
"\n",
" backFcn.SetParameters(par);\n",
" backFcn.DrawCopy(\"same\");\n",
"\n",
" signalFcn.SetParameters(par);\n",
" signalFcn.SetLineColor(kGreen);\n",
" signalFcn.DrawCopy(\"same\");\n",
"\n",
" const double binwidth = histo->GetXaxis()->GetBinWidth(1);\n",
" const double integral = signalFcn.Integral(0.,3.);\n",
" std::cout << \"number of signal events = \" << integral/binwidth << \" \" << binwidth<< std::endl;\n",
"\n",
" // draw the legend\n",
" TLegend legend(0.15,0.7,0.28,0.85);\n",
" legend.SetTextFont(72);\n",
" legend.SetTextSize(0.03);\n",
" legend.AddEntry(histo,\"Data\",\"lpe\");\n",
" legend.AddEntry(&backFcn,\"Bgd\",\"l\");\n",
" legend.AddEntry(&signalFcn,\"Sig\",\"l\");\n",
" legend.AddEntry(&fitFcn,\"Sig+Bgd\",\"l\");\n",
" legend.DrawClone();\n",
" histo->Draw(\"esame\");\n",
" c1->SaveAs(filename);\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "0a77a8af",
"metadata": {},
"source": [
" Create a file with example data\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b7212021",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.611732Z",
"iopub.status.busy": "2026-05-19T20:24:53.611598Z",
"iopub.status.idle": "2026-05-19T20:24:53.619897Z",
"shell.execute_reply": "2026-05-19T20:24:53.619370Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void CreateRootFile(){\n",
" // The data in array form\n",
" const int nBins = 60;\n",
" double data[nBins] = { 6, 1,10,12, 6,13,23,22,15,21,\n",
" 23,26,36,25,27,35,40,44,66,81,\n",
" 75,57,43,37,36,31,35,36,43,32,\n",
" 40,37,38,33,36,44,42,37,32,32,\n",
" 43,44,35,33,33,39,29,41,32,44,\n",
" 26,39,29,35,32,21,21,15,25,15};\n",
"\n",
" TFile* f = new TFile(\"exampleRootFile.root\",\"RECREATE\");\n",
" TH1D *histo = new TH1D(\"histo\", \"Gauss Peak on Quadratic Background;x;Events/0.05\",60,0,3);\n",
" for(int i=0; i < nBins; i++) histo->SetBinContent(i+1,data[i]);\n",
" f->Write();\n",
" f->Close();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "6a429e0f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.621349Z",
"iopub.status.busy": "2026-05-19T20:24:53.621233Z",
"iopub.status.idle": "2026-05-19T20:24:53.843298Z",
"shell.execute_reply": "2026-05-19T20:24:53.840441Z"
}
},
"outputs": [],
"source": [
"gStyle->SetOptFit(1111);\n",
"gStyle->SetOptStat(0);"
]
},
{
"cell_type": "markdown",
"id": "8d2ee42c",
"metadata": {},
"source": [
"fit range from fitxmin to fitxmax"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b276791a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:53.847840Z",
"iopub.status.busy": "2026-05-19T20:24:53.847717Z",
"iopub.status.idle": "2026-05-19T20:24:54.054317Z",
"shell.execute_reply": "2026-05-19T20:24:54.053598Z"
}
},
"outputs": [],
"source": [
"float fitxmin=0.2;\n",
"float fitxmax=2.7;\n",
"\n",
"TCanvas *c1 = new TCanvas(\"c1\",\"Fitting Demo of Histogram in File\",10,10,700,500);\n",
"CreateRootFile();\n",
"TFile* f = new TFile(\"exampleRootFile.root\");\n",
"TH1D* histo= nullptr;\n",
"f->GetObject(\"histo\",histo);\n",
"if (!histo){\n",
" std::cout << \"histo not found\" << std::endl;\n",
" return;\n",
"}\n",
"histo->SetMarkerStyle(21);\n",
"histo->SetMarkerSize(0.8);"
]
},
{
"cell_type": "markdown",
"id": "35a87213",
"metadata": {},
"source": [
"now call the fit routine"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "148358b0",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:54.060895Z",
"iopub.status.busy": "2026-05-19T20:24:54.060770Z",
"iopub.status.idle": "2026-05-19T20:24:54.275107Z",
"shell.execute_reply": "2026-05-19T20:24:54.274509Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_67:2:3: error: use of undeclared identifier 'FitRoutine'\n",
" (FitRoutine(((*(TCanvas **)0x7f27c69f1008)), ((*(TH1D **)0x7f27c69f1020)), ((*(float*)0x7f27c69f1000)), ((*(float*)0x7f27c69f1004)), \"FitHistoInFile.pdf\"))\n",
" ^\n",
"Error in : Error evaluating expression (FitRoutine(((*(TCanvas **)0x7f27c69f1008)), ((*(TH1D **)0x7f27c69f1020)), ((*(float*)0x7f27c69f1000)), ((*(float*)0x7f27c69f1004)), \"FitHistoInFile.pdf\"))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"FitRoutine(c1,histo, fitxmin, fitxmax,\"FitHistoInFile.pdf\");"
]
},
{
"cell_type": "markdown",
"id": "75bb81ec",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1f4e595d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:54.276994Z",
"iopub.status.busy": "2026-05-19T20:24:54.276876Z",
"iopub.status.idle": "2026-05-19T20:24:54.483050Z",
"shell.execute_reply": "2026-05-19T20:24:54.482654Z"
}
},
"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
}