{
"cells": [
{
"cell_type": "markdown",
"id": "c805916b",
"metadata": {},
"source": [
"# rf103_interprfuncs\n",
"Basic functionality: interpreted functions and PDFs.\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:14 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c50db484",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:37.833019Z",
"iopub.status.busy": "2024-03-19T19:14:37.832628Z",
"iopub.status.idle": "2024-03-19T19:14:37.861642Z",
"shell.execute_reply": "2024-03-19T19:14:37.857331Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"#include \"RooFitResult.h\"\n",
"#include \"RooGenericPdf.h\"\n",
"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "8390195f",
"metadata": {},
"source": [
"----------------------------------------------------\n",
"Generic interpreted p.d.f.\n",
"===================================================="
]
},
{
"cell_type": "markdown",
"id": "cd6d4c2a",
"metadata": {},
"source": [
"Declare observable x"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ff424e74",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:37.878965Z",
"iopub.status.busy": "2024-03-19T19:14:37.878096Z",
"iopub.status.idle": "2024-03-19T19:14:38.683505Z",
"shell.execute_reply": "2024-03-19T19:14:38.660499Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", -20, 20);"
]
},
{
"cell_type": "markdown",
"id": "35ff0f71",
"metadata": {},
"source": [
"Construct generic pdf from interpreted expression\n",
"-------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "2ac9a168",
"metadata": {},
"source": [
"To construct a proper pdf, the formula expression is explicitly normalized internally by dividing\n",
"it by a numeric integral of the expression over x in the range [-20,20]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "eb730e48",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:38.690777Z",
"iopub.status.busy": "2024-03-19T19:14:38.690398Z",
"iopub.status.idle": "2024-03-19T19:14:38.916769Z",
"shell.execute_reply": "2024-03-19T19:14:38.915401Z"
}
},
"outputs": [],
"source": [
"RooRealVar alpha(\"alpha\", \"alpha\", 5, 0.1, 10);\n",
"RooGenericPdf genpdf(\"genpdf\", \"genpdf\", \"(1+0.1*abs(x)+sin(sqrt(abs(x*alpha+0.1))))\", RooArgSet(x, alpha));"
]
},
{
"cell_type": "markdown",
"id": "b221981d",
"metadata": {},
"source": [
"Sample, fit and plot generic pdf\n",
"---------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "424fe010",
"metadata": {},
"source": [
"Generate a toy dataset from the interpreted pdf"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "62ec1150",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:38.922728Z",
"iopub.status.busy": "2024-03-19T19:14:38.922342Z",
"iopub.status.idle": "2024-03-19T19:14:39.317398Z",
"shell.execute_reply": "2024-03-19T19:14:39.316176Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:NumericIntegration -- RooRealIntegral::init(genpdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)\n",
"[#1] INFO:NumericIntegration -- RooRealIntegral::init(genpdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)\n"
]
},
{
"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{genpdf.generate(x, 10000)};\n",
" ^\n"
]
}
],
"source": [
"std::unique_ptr data{genpdf.generate(x, 10000)};"
]
},
{
"cell_type": "markdown",
"id": "dd5fb078",
"metadata": {},
"source": [
"Fit the interpreted pdf to the generated data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "dba59c6c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:39.334183Z",
"iopub.status.busy": "2024-03-19T19:14:39.333810Z",
"iopub.status.idle": "2024-03-19T19:14:39.558645Z",
"shell.execute_reply": "2024-03-19T19:14:39.557768Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_54:2:16: error: reference to 'data' is ambiguous\n",
" genpdf.fitTo(*data, PrintLevel(-1));\n",
" ^\n",
"input_line_53:2:30: note: candidate found by name lookup is 'data'\n",
" std::unique_ptr data{genpdf.generate(x, 10000)};\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": [
"genpdf.fitTo(*data, PrintLevel(-1));"
]
},
{
"cell_type": "markdown",
"id": "bb2391af",
"metadata": {},
"source": [
"Make a plot of the data and the pdf overlaid"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3fd283a4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:39.562948Z",
"iopub.status.busy": "2024-03-19T19:14:39.562558Z",
"iopub.status.idle": "2024-03-19T19:14:39.800374Z",
"shell.execute_reply": "2024-03-19T19:14:39.789462Z"
}
},
"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{genpdf.generate(x, 10000)};\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(\"Interpreted expression pdf\"));\n",
"data->plotOn(xframe);\n",
"genpdf.plotOn(xframe);"
]
},
{
"cell_type": "markdown",
"id": "ceb2215e",
"metadata": {},
"source": [
"-----------------------------------------------------------------------------------------------------------\n",
"Standard p.d.f adjust with interpreted helper function\n",
"==========================================================================================================\n",
"Make a gauss(x,sqrt(mean2),sigma) from a standard RooGaussian"
]
},
{
"cell_type": "markdown",
"id": "5c8fb29a",
"metadata": {},
"source": [
"Construct standard pdf with formula replacing parameter\n",
"------------------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "4caa579a",
"metadata": {},
"source": [
"Construct parameter mean2 and sigma"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "084f1755",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:39.814463Z",
"iopub.status.busy": "2024-03-19T19:14:39.814063Z",
"iopub.status.idle": "2024-03-19T19:14:40.039549Z",
"shell.execute_reply": "2024-03-19T19:14:40.037477Z"
}
},
"outputs": [],
"source": [
"RooRealVar mean2(\"mean2\", \"mean^2\", 10, 0, 200);\n",
"RooRealVar sigma(\"sigma\", \"sigma\", 3, 0.1, 10);"
]
},
{
"cell_type": "markdown",
"id": "2d49c00a",
"metadata": {},
"source": [
"Construct interpreted function mean = sqrt(mean^2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d153d407",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:40.054270Z",
"iopub.status.busy": "2024-03-19T19:14:40.053907Z",
"iopub.status.idle": "2024-03-19T19:14:40.297360Z",
"shell.execute_reply": "2024-03-19T19:14:40.295839Z"
}
},
"outputs": [],
"source": [
"RooFormulaVar mean(\"mean\", \"mean\", \"sqrt(mean2)\", mean2);"
]
},
{
"cell_type": "markdown",
"id": "e832f77e",
"metadata": {},
"source": [
"Construct a gaussian g2(x,sqrt(mean2),sigma) ;"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "85564ac9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:40.302273Z",
"iopub.status.busy": "2024-03-19T19:14:40.301881Z",
"iopub.status.idle": "2024-03-19T19:14:40.533214Z",
"shell.execute_reply": "2024-03-19T19:14:40.530422Z"
}
},
"outputs": [],
"source": [
"RooGaussian g2(\"g2\", \"h2\", x, mean, sigma);"
]
},
{
"cell_type": "markdown",
"id": "841521bc",
"metadata": {},
"source": [
"Generate toy data\n",
"---------------------------------"
]
},
{
"cell_type": "markdown",
"id": "f2028b9c",
"metadata": {},
"source": [
"Construct a separate gaussian g1(x,10,3) to generate a toy Gaussian dataset with mean 10 and width 3"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a2d8bb0f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:40.545839Z",
"iopub.status.busy": "2024-03-19T19:14:40.545452Z",
"iopub.status.idle": "2024-03-19T19:14:40.774055Z",
"shell.execute_reply": "2024-03-19T19:14:40.772585Z"
}
},
"outputs": [],
"source": [
"RooGaussian g1(\"g1\", \"g1\", x, 10.0, 3.0);\n",
"std::unique_ptr data2{g1.generate(x, 1000)};"
]
},
{
"cell_type": "markdown",
"id": "ae74a276",
"metadata": {},
"source": [
"Fit and plot tailored standard pdf\n",
"-------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "9631ca8a",
"metadata": {},
"source": [
"Fit g2 to data from g1"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2e649e53",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:40.778991Z",
"iopub.status.busy": "2024-03-19T19:14:40.778615Z",
"iopub.status.idle": "2024-03-19T19:14:41.253215Z",
"shell.execute_reply": "2024-03-19T19:14:41.244397Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Fitting -- RooAbsPdf::fitTo(g2_over_g2_Int[x]) fixing normalization set for coefficient determination to observables in data\n",
"[#1] INFO:Fitting -- using CPU computation library compiled with -mavx2\n",
"[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_g2_over_g2_Int[x]_g1Data) Summation contains a RooNLLVar, using its error level\n",
"[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization\n",
"[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization\n",
"\n",
" RooFitResult: minimized FCN value: 2551.39, estimated distance to minimum: 4.39288e-06\n",
" covariance matrix quality: Full, accurate covariance matrix\n",
" Status : MINIMIZE=0 HESSE=0 \n",
"\n",
" Floating Parameter FinalValue +/- Error \n",
" -------------------- --------------------------\n",
" mean2 1.0010e+02 +/- 1.98e+00\n",
" sigma 3.1172e+00 +/- 7.12e-02\n",
"\n"
]
}
],
"source": [
"std::unique_ptr fitResult{g2.fitTo(*data2, Save(), PrintLevel(-1))};\n",
"fitResult->Print();"
]
},
{
"cell_type": "markdown",
"id": "442b072d",
"metadata": {},
"source": [
"Plot data on frame and overlay projection of g2"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5ae9a88f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:41.297762Z",
"iopub.status.busy": "2024-03-19T19:14:41.297367Z",
"iopub.status.idle": "2024-03-19T19:14:41.543608Z",
"shell.execute_reply": "2024-03-19T19:14:41.535880Z"
}
},
"outputs": [],
"source": [
"RooPlot *xframe2 = x.frame(Title(\"Tailored Gaussian pdf\"));\n",
"data2->plotOn(xframe2);\n",
"g2.plotOn(xframe2);"
]
},
{
"cell_type": "markdown",
"id": "42bac60f",
"metadata": {},
"source": [
"Draw all frames on a canvas"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "f306f960",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:41.576787Z",
"iopub.status.busy": "2024-03-19T19:14:41.576390Z",
"iopub.status.idle": "2024-03-19T19:14:41.923054Z",
"shell.execute_reply": "2024-03-19T19:14:41.921496Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_85:2:3: error: use of undeclared identifier 'xframe'\n",
" (xframe->GetYaxis()->SetTitleOffset(1.3999999999999999))\n",
" ^\n",
"Error in : Error evaluating expression (xframe->GetYaxis()->SetTitleOffset(1.3999999999999999))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"TCanvas *c = new TCanvas(\"rf103_interprfuncs\", \"rf103_interprfuncs\", 800, 400);\n",
"c->Divide(2);\n",
"c->cd(1);\n",
"gPad->SetLeftMargin(0.15);\n",
"xframe->GetYaxis()->SetTitleOffset(1.4);\n",
"xframe->Draw();\n",
"c->cd(2);\n",
"gPad->SetLeftMargin(0.15);\n",
"xframe2->GetYaxis()->SetTitleOffset(1.4);\n",
"xframe2->Draw();"
]
},
{
"cell_type": "markdown",
"id": "767bd12a",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "71c767c9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:14:41.933028Z",
"iopub.status.busy": "2024-03-19T19:14:41.932415Z",
"iopub.status.idle": "2024-03-19T19:14:42.341106Z",
"shell.execute_reply": "2024-03-19T19:14:42.340074Z"
}
},
"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
}