{
"cells": [
{
"cell_type": "markdown",
"id": "f238782a",
"metadata": {},
"source": [
"# fithist\n",
"Example of fit where the model is histogram + function\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, March 19, 2024 at 07:08 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e72ecce8",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:43.311405Z",
"iopub.status.busy": "2024-03-19T19:08:43.311032Z",
"iopub.status.idle": "2024-03-19T19:08:43.324896Z",
"shell.execute_reply": "2024-03-19T19:08:43.323686Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \n",
"#include \n",
"#include \n",
"\n",
"TH1F *background;"
]
},
{
"cell_type": "markdown",
"id": "94b48043",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e4828283",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:43.329584Z",
"iopub.status.busy": "2024-03-19T19:08:43.329234Z",
"iopub.status.idle": "2024-03-19T19:08:43.377627Z",
"shell.execute_reply": "2024-03-19T19:08:43.376447Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void histgen() {\n",
" //generate the histogram background and save it to a file\n",
" //background taken as linearly decreasing\n",
"\n",
" TF1 f1(\"f1\",\"pol1\",0,10);\n",
" f1.SetParameters(5,-0.5);\n",
" TH1F h(\"background\",\"linear background\",100,0,10);\n",
" h.FillRandom(\"f1\",10000);\n",
" TFile f(\"background.root\",\"recreate\");\n",
" //save the background histogram\n",
" h.Write();\n",
" //superimpose a Gaussian signal to the background histogram\n",
" TF1 f2(\"f2\",\"gaus\",0,10);\n",
" f2.SetParameters(1,6,0.5);\n",
" h.FillRandom(\"f2\",2000);\n",
" h.SetName(\"result\");\n",
" h.Write();\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "665d6367",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b326bedd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:43.381799Z",
"iopub.status.busy": "2024-03-19T19:08:43.381444Z",
"iopub.status.idle": "2024-03-19T19:08:43.392797Z",
"shell.execute_reply": "2024-03-19T19:08:43.390941Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"double ftotal(double *x, double *par) {\n",
" double xx = x[0];\n",
" int bin = background->GetXaxis()->FindBin(xx);\n",
" double br = par[3]*background->GetBinContent(bin);\n",
" double arg = (xx-par[1])/par[2];\n",
" double sr = par[0]*TMath::Exp(-0.5*arg*arg);\n",
" return sr + br;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "07381bca",
"metadata": {},
"source": [
"fit function ftotal to signal + background"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c7aa6934",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:43.398630Z",
"iopub.status.busy": "2024-03-19T19:08:43.398274Z",
"iopub.status.idle": "2024-03-19T19:08:44.420287Z",
"shell.execute_reply": "2024-03-19T19:08:44.419269Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"****************************************\n",
"Minimizer is Minuit2 / Migrad\n",
"Chi2 = 7.84157\n",
"NDf = 96\n",
"Edm = 3.7399e-06\n",
"NCalls = 174\n",
"p0 = 156.638 +/- 5.91061 \t (limited)\n",
"p1 = 5.98183 +/- 0.0189043 \n",
"p2 = -0.506659 +/- 0.0180481 \n",
"p3 = 1.0003 +/- 0.0112955 \n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_46:9:1: warning: 'norm' shadows a declaration with the same name in the 'std' namespace; use '::norm' to reference this declaration\n",
"double norm = result->GetMaximum();\n",
"^\n",
"Info in : created default TCanvas with name c1\n"
]
}
],
"source": [
"histgen();\n",
"\n",
"TFile *f = new TFile(\"background.root\");\n",
"background = (TH1F*)f->Get(\"background\"); //pointer used in ftotal\n",
"TH1F *result = (TH1F*)f->Get(\"result\");\n",
"\n",
"TF1 *ftot = new TF1(\"ftot\",ftotal,0,10,4);\n",
"double norm = result->GetMaximum();\n",
"ftot->SetParameters(0.5*norm,5,.2,norm);\n",
"ftot->SetParLimits(0,.3*norm,norm);\n",
"\n",
"result->Fit(\"ftot\",\"b\");"
]
},
{
"cell_type": "markdown",
"id": "daaf2eea",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8b0d8728",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:08:44.425111Z",
"iopub.status.busy": "2024-03-19T19:08:44.424757Z",
"iopub.status.idle": "2024-03-19T19:08:44.851766Z",
"shell.execute_reply": "2024-03-19T19:08:44.850744Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%jsroot on\n",
"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
}