{
"cells": [
{
"cell_type": "markdown",
"id": "9a7547f7",
"metadata": {},
"source": [
"# rf202_extendedmlfit\n",
"Setting up an extended maximum likelihood fit.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Wouter Verkerke \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:15 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "399c8bed",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:16.456817Z",
"iopub.status.busy": "2024-03-19T19:15:16.456459Z",
"iopub.status.idle": "2024-03-19T19:15:16.487702Z",
"shell.execute_reply": "2024-03-19T19:15:16.485676Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"RooChebychev.h\"\n",
"#include \"RooAddPdf.h\"\n",
"#include \"RooExtendPdf.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "f5ecc5c1",
"metadata": {},
"source": [
"Setup component pdfs\n",
"---------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "9941ba9a",
"metadata": {},
"source": [
"Declare observable x"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4042444c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:16.493934Z",
"iopub.status.busy": "2024-03-19T19:15:16.493379Z",
"iopub.status.idle": "2024-03-19T19:15:17.046289Z",
"shell.execute_reply": "2024-03-19T19:15:17.044976Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", 0, 10);"
]
},
{
"cell_type": "markdown",
"id": "142e62b4",
"metadata": {},
"source": [
"Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0551e092",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:17.052958Z",
"iopub.status.busy": "2024-03-19T19:15:17.052611Z",
"iopub.status.idle": "2024-03-19T19:15:17.277125Z",
"shell.execute_reply": "2024-03-19T19:15:17.275900Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#0] WARNING:InputArguments -- The parameter 'sigma1' with range [-inf, inf] of the RooGaussian 'sig1' exceeds the safe range of (0, inf). Advise to limit its range.\n",
"[#0] WARNING:InputArguments -- The parameter 'sigma2' with range [-inf, inf] of the RooGaussian 'sig2' exceeds the safe range of (0, inf). Advise to limit its range.\n"
]
}
],
"source": [
"RooRealVar mean(\"mean\", \"mean of gaussians\", 5);\n",
"RooRealVar sigma1(\"sigma1\", \"width of gaussians\", 0.5);\n",
"RooRealVar sigma2(\"sigma2\", \"width of gaussians\", 1);\n",
"\n",
"RooGaussian sig1(\"sig1\", \"Signal component 1\", x, mean, sigma1);\n",
"RooGaussian sig2(\"sig2\", \"Signal component 2\", x, mean, sigma2);"
]
},
{
"cell_type": "markdown",
"id": "aa7daa18",
"metadata": {},
"source": [
"Build Chebychev polynomial pdf"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e4d2a38b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:17.289479Z",
"iopub.status.busy": "2024-03-19T19:15:17.289044Z",
"iopub.status.idle": "2024-03-19T19:15:17.515845Z",
"shell.execute_reply": "2024-03-19T19:15:17.514268Z"
}
},
"outputs": [],
"source": [
"RooRealVar a0(\"a0\", \"a0\", 0.5, 0., 1.);\n",
"RooRealVar a1(\"a1\", \"a1\", 0.2, 0., 1.);\n",
"RooChebychev bkg(\"bkg\", \"Background\", x, RooArgSet(a0, a1));"
]
},
{
"cell_type": "markdown",
"id": "1ab1f810",
"metadata": {},
"source": [
"Sum the signal components into a composite signal pdf"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c5c6a89b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:17.520967Z",
"iopub.status.busy": "2024-03-19T19:15:17.520637Z",
"iopub.status.idle": "2024-03-19T19:15:17.740631Z",
"shell.execute_reply": "2024-03-19T19:15:17.732775Z"
}
},
"outputs": [],
"source": [
"RooRealVar sig1frac(\"sig1frac\", \"fraction of component 1 in signal\", 0.8, 0., 1.);\n",
"RooAddPdf sig(\"sig\", \"Signal\", RooArgList(sig1, sig2), sig1frac);"
]
},
{
"cell_type": "markdown",
"id": "19ff7c1e",
"metadata": {},
"source": [
"----------------\n",
"METHOD 1\n",
"================"
]
},
{
"cell_type": "markdown",
"id": "fca85524",
"metadata": {},
"source": [
"Construct extended composite model\n",
"-------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "d71d8b9f",
"metadata": {},
"source": [
"Sum the composite signal and background into an extended pdf nsig*sig+nbkg*bkg"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "821c1190",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:17.747907Z",
"iopub.status.busy": "2024-03-19T19:15:17.747504Z",
"iopub.status.idle": "2024-03-19T19:15:17.961805Z",
"shell.execute_reply": "2024-03-19T19:15:17.960566Z"
}
},
"outputs": [],
"source": [
"RooRealVar nsig(\"nsig\", \"number of signal events\", 500, 0., 10000);\n",
"RooRealVar nbkg(\"nbkg\", \"number of background events\", 500, 0, 10000);\n",
"RooAddPdf model(\"model\", \"(g1+g2)+a\", RooArgList(bkg, sig), RooArgList(nbkg, nsig));"
]
},
{
"cell_type": "markdown",
"id": "8ef0a631",
"metadata": {},
"source": [
"Sample, fit and plot extended model\n",
"---------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "d663e39d",
"metadata": {},
"source": [
"Generate a data sample of expected number events in x from model\n",
"= model.expectedEvents() = nsig+nbkg"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "456f5ea4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:17.966351Z",
"iopub.status.busy": "2024-03-19T19:15:17.966033Z",
"iopub.status.idle": "2024-03-19T19:15:18.183240Z",
"shell.execute_reply": "2024-03-19T19:15:18.182082Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_53:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n",
" std::unique_ptr data{model.generate(x)};\n",
" ^\n"
]
}
],
"source": [
"std::unique_ptr data{model.generate(x)};"
]
},
{
"cell_type": "markdown",
"id": "3f91d5b9",
"metadata": {},
"source": [
"Fit model to data, extended ML term automatically included"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "666ae87a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:18.192543Z",
"iopub.status.busy": "2024-03-19T19:15:18.192111Z",
"iopub.status.idle": "2024-03-19T19:15:18.419634Z",
"shell.execute_reply": "2024-03-19T19:15:18.414574Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_54:2:15: error: reference to 'data' is ambiguous\n",
" model.fitTo(*data, PrintLevel(-1));\n",
" ^\n",
"input_line_53:2:30: note: candidate found by name lookup is 'data'\n",
" std::unique_ptr data{model.generate(x)};\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'\n",
" data(initializer_list<_Tp> __il) noexcept\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'\n",
" data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Tp (&__array)[_Nm]) noexcept\n",
" ^\n"
]
}
],
"source": [
"model.fitTo(*data, PrintLevel(-1));"
]
},
{
"cell_type": "markdown",
"id": "8210165d",
"metadata": {},
"source": [
"Plot data and PDF overlaid, use expected number of events for pdf projection normalization\n",
"rather than observed number of events (==data->numEntries())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b0c19287",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:18.430843Z",
"iopub.status.busy": "2024-03-19T19:15:18.430425Z",
"iopub.status.idle": "2024-03-19T19:15:18.646997Z",
"shell.execute_reply": "2024-03-19T19:15:18.645257Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_55:3:1: error: reference to 'data' is ambiguous\n",
"data->plotOn(xframe);\n",
"^\n",
"input_line_53:2:30: note: candidate found by name lookup is 'data'\n",
" std::unique_ptr data{model.generate(x)};\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'\n",
" data(initializer_list<_Tp> __il) noexcept\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'\n",
" data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Tp (&__array)[_Nm]) noexcept\n",
" ^\n"
]
}
],
"source": [
"RooPlot *xframe = x.frame(Title(\"extended ML fit example\"));\n",
"data->plotOn(xframe);\n",
"model.plotOn(xframe, Normalization(1.0, RooAbsReal::RelativeExpected));"
]
},
{
"cell_type": "markdown",
"id": "018ed5b2",
"metadata": {},
"source": [
"Overlay the background component of model with a dashed line"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a2f9168b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:18.651230Z",
"iopub.status.busy": "2024-03-19T19:15:18.650847Z",
"iopub.status.idle": "2024-03-19T19:15:18.871102Z",
"shell.execute_reply": "2024-03-19T19:15:18.869716Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_56:2:50: error: cannot take the address of an rvalue of type 'ELineStyle'\n",
" model.plotOn(xframe, Components(bkg), LineStyle(kDashed), Normalization(1.0, RooAbsReal::RelativeExpected));\n",
" ^~~~~~~\n",
"Error while creating dynamic expression for:\n",
" model.plotOn(xframe, Components(bkg), LineStyle(kDashed), Normalization(1., RooAbsReal::RelativeExpected))\n"
]
}
],
"source": [
"model.plotOn(xframe, Components(bkg), LineStyle(kDashed), Normalization(1.0, RooAbsReal::RelativeExpected));"
]
},
{
"cell_type": "markdown",
"id": "cfbe1a9e",
"metadata": {},
"source": [
"Overlay the background+sig2 components of model with a dotted line"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "b488ad32",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:18.877004Z",
"iopub.status.busy": "2024-03-19T19:15:18.876080Z",
"iopub.status.idle": "2024-03-19T19:15:19.091871Z",
"shell.execute_reply": "2024-03-19T19:15:19.090477Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_57:2:67: error: cannot take the address of an rvalue of type 'ELineStyle'\n",
" model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted),\n",
" ^~~~~~~\n",
"Error while creating dynamic expression for:\n",
" model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted), Normalization(1., RooAbsReal::RelativeExpected))\n"
]
}
],
"source": [
"model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted),\n",
" Normalization(1.0, RooAbsReal::RelativeExpected));"
]
},
{
"cell_type": "markdown",
"id": "308ff62b",
"metadata": {},
"source": [
"Print structure of composite pdf"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "7e7fd4f2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:19.105010Z",
"iopub.status.busy": "2024-03-19T19:15:19.104624Z",
"iopub.status.idle": "2024-03-19T19:15:19.317714Z",
"shell.execute_reply": "2024-03-19T19:15:19.316617Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0x7feff002d7d0 RooAddPdf::model = 0.9/1 [Auto,Clean] \n",
" 0x7feff00387d0/V- RooChebychev::bkg = 0.8 [Auto,Dirty] \n",
" 0x7feff0bdc000/V- RooRealVar::x = 5\n",
" 0x7feff0038000/V- RooRealVar::a0 = 0.5\n",
" 0x7feff00383e8/V- RooRealVar::a1 = 0.2\n",
" 0x7feff002d3e8/V- RooRealVar::nbkg = 500\n",
" 0x7feff00303e8/V- RooAddPdf::sig = 1/1 [Auto,Clean] \n",
" 0x7feff003bbb8/V- RooGaussian::sig1 = 1 [Auto,Dirty] \n",
" 0x7feff0bdc000/V- RooRealVar::x = 5\n",
" 0x7feff003b000/V- RooRealVar::mean = 5\n",
" 0x7feff003b3e8/V- RooRealVar::sigma1 = 0.5\n",
" 0x7feff0030000/V- RooRealVar::sig1frac = 0.8\n",
" 0x7feff003c110/V- RooGaussian::sig2 = 1 [Auto,Dirty] \n",
" 0x7feff0bdc000/V- RooRealVar::x = 5\n",
" 0x7feff003b000/V- RooRealVar::mean = 5\n",
" 0x7feff003b7d0/V- RooRealVar::sigma2 = 1\n",
" 0x7feff002d000/V- RooRealVar::nsig = 500\n"
]
}
],
"source": [
"model.Print(\"t\");"
]
},
{
"cell_type": "markdown",
"id": "f41f2c67",
"metadata": {},
"source": [
"----------------\n",
"METHOD 2\n",
"================"
]
},
{
"cell_type": "markdown",
"id": "2bba0c7a",
"metadata": {},
"source": [
"Construct extended components first\n",
"---------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "e4cb96a5",
"metadata": {},
"source": [
"Associated nsig/nbkg as expected number of events with sig/bkg"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a179260f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:19.323911Z",
"iopub.status.busy": "2024-03-19T19:15:19.323504Z",
"iopub.status.idle": "2024-03-19T19:15:19.559866Z",
"shell.execute_reply": "2024-03-19T19:15:19.549636Z"
}
},
"outputs": [],
"source": [
"RooExtendPdf esig(\"esig\", \"extended signal pdf\", sig, nsig);\n",
"RooExtendPdf ebkg(\"ebkg\", \"extended background pdf\", bkg, nbkg);"
]
},
{
"cell_type": "markdown",
"id": "6ef13f87",
"metadata": {},
"source": [
"Sum extended components without coefs\n",
"-------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "dbdad627",
"metadata": {},
"source": [
"Construct sum of two extended pdf (no coefficients required)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "0d72eebe",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:19.584014Z",
"iopub.status.busy": "2024-03-19T19:15:19.583586Z",
"iopub.status.idle": "2024-03-19T19:15:19.797460Z",
"shell.execute_reply": "2024-03-19T19:15:19.796359Z"
}
},
"outputs": [],
"source": [
"RooAddPdf model2(\"model2\", \"(g1+g2)+a\", RooArgList(ebkg, esig));"
]
},
{
"cell_type": "markdown",
"id": "333d1284",
"metadata": {},
"source": [
"Draw the frame on the canvas"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ce11a520",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:19.801975Z",
"iopub.status.busy": "2024-03-19T19:15:19.801659Z",
"iopub.status.idle": "2024-03-19T19:15:20.415610Z",
"shell.execute_reply": "2024-03-19T19:15:20.414227Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!\n",
"You are probably missing the definition of void cling::runtime::internal::EvaluateT(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)\n",
"Maybe you need to load the corresponding shared library?\n"
]
}
],
"source": [
"new TCanvas(\"rf202_composite\", \"rf202_composite\", 600, 600);\n",
"gPad->SetLeftMargin(0.15);\n",
"xframe->GetYaxis()->SetTitleOffset(1.4);\n",
"xframe->Draw();"
]
},
{
"cell_type": "markdown",
"id": "8bc67af5",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "788e4275",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:20.421656Z",
"iopub.status.busy": "2024-03-19T19:15:20.421252Z",
"iopub.status.idle": "2024-03-19T19:15:20.685566Z",
"shell.execute_reply": "2024-03-19T19:15:20.684084Z"
}
},
"outputs": [],
"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
}