{
"cells": [
{
"cell_type": "markdown",
"id": "2ef2dc7e",
"metadata": {},
"source": [
"# rf311_rangeplot\n",
"Multidimensional models: projecting pdf and data ranges in continuous observables\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": "d37b81e1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:57.111707Z",
"iopub.status.busy": "2024-03-19T19:15:57.111326Z",
"iopub.status.idle": "2024-03-19T19:15:57.140423Z",
"shell.execute_reply": "2024-03-19T19:15:57.139167Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"RooConstVar.h\"\n",
"#include \"RooProdPdf.h\"\n",
"#include \"RooAddPdf.h\"\n",
"#include \"RooPolynomial.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "9938227c",
"metadata": {},
"source": [
"Create 3D pdf and data\n",
"-------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "c5502ad9",
"metadata": {},
"source": [
"Create observables"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "46b0788b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:57.159508Z",
"iopub.status.busy": "2024-03-19T19:15:57.159101Z",
"iopub.status.idle": "2024-03-19T19:15:57.833417Z",
"shell.execute_reply": "2024-03-19T19:15:57.832350Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", -5, 5);\n",
"RooRealVar y(\"y\", \"y\", -5, 5);\n",
"RooRealVar z(\"z\", \"z\", -5, 5);"
]
},
{
"cell_type": "markdown",
"id": "ba48285a",
"metadata": {},
"source": [
"Create signal pdf gauss(x)*gauss(y)*gauss(z)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f8732c0a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:57.839244Z",
"iopub.status.busy": "2024-03-19T19:15:57.838851Z",
"iopub.status.idle": "2024-03-19T19:15:58.195490Z",
"shell.execute_reply": "2024-03-19T19:15:58.179516Z"
}
},
"outputs": [],
"source": [
"RooGaussian gx(\"gx\", \"gx\", x, 0.0, 1.0);\n",
"RooGaussian gy(\"gy\", \"gy\", y, 0.0, 1.0);\n",
"RooGaussian gz(\"gz\", \"gz\", z, 0.0, 1.0);\n",
"RooProdPdf sig(\"sig\", \"sig\", RooArgSet(gx, gy, gz));"
]
},
{
"cell_type": "markdown",
"id": "6ad876be",
"metadata": {},
"source": [
"Create background pdf poly(x)*poly(y)*poly(z)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b198fad6",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:58.220955Z",
"iopub.status.busy": "2024-03-19T19:15:58.220403Z",
"iopub.status.idle": "2024-03-19T19:15:58.627329Z",
"shell.execute_reply": "2024-03-19T19:15:58.626150Z"
}
},
"outputs": [],
"source": [
"RooPolynomial px(\"px\", \"px\", x, RooArgSet(-0.1, 0.004));\n",
"RooPolynomial py(\"py\", \"py\", y, RooArgSet(0.1, -0.004));\n",
"RooPolynomial pz(\"pz\", \"pz\", z);\n",
"RooProdPdf bkg(\"bkg\", \"bkg\", RooArgSet(px, py, pz));"
]
},
{
"cell_type": "markdown",
"id": "f8d14ed9",
"metadata": {},
"source": [
"Create composite pdf sig+bkg"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6c5d6c31",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:58.632356Z",
"iopub.status.busy": "2024-03-19T19:15:58.632030Z",
"iopub.status.idle": "2024-03-19T19:15:59.030532Z",
"shell.execute_reply": "2024-03-19T19:15:59.028256Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_51:5:1: 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, y, z}, 20000)};\n",
"^\n"
]
}
],
"source": [
"RooRealVar fsig(\"fsig\", \"signal fraction\", 0.1, 0., 1.);\n",
"RooAddPdf model(\"model\", \"model\", RooArgList(sig, bkg), fsig);\n",
"\n",
"std::unique_ptr data{model.generate({x, y, z}, 20000)};"
]
},
{
"cell_type": "markdown",
"id": "6c802ac0",
"metadata": {},
"source": [
"Project pdf and data on x\n",
"-------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "280df34b",
"metadata": {},
"source": [
"Make plain projection of data and pdf on x observable"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a3bb4f32",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:59.041972Z",
"iopub.status.busy": "2024-03-19T19:15:59.041584Z",
"iopub.status.idle": "2024-03-19T19:15:59.269203Z",
"shell.execute_reply": "2024-03-19T19:15:59.267819Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_52:3:1: error: reference to 'data' is ambiguous\n",
"data->plotOn(frame);\n",
"^\n",
"input_line_51:5:29: note: candidate found by name lookup is 'data'\n",
"std::unique_ptr data{model.generate({x, y, z}, 20000)};\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 *frame = x.frame(Title(\"Projection of 3D data and pdf on X\"), Bins(40));\n",
"data->plotOn(frame);\n",
"model.plotOn(frame);"
]
},
{
"cell_type": "markdown",
"id": "0a85afb8",
"metadata": {},
"source": [
"Project pdf and data on x in signal range\n",
"----------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "37f61d81",
"metadata": {},
"source": [
"Define signal region in y and z observables"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7703f561",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:59.275105Z",
"iopub.status.busy": "2024-03-19T19:15:59.274708Z",
"iopub.status.idle": "2024-03-19T19:15:59.490197Z",
"shell.execute_reply": "2024-03-19T19:15:59.489109Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Eval -- RooRealVar::setRange(y) new range named 'sigRegion' created with bounds [-1,1]\n",
"[#1] INFO:Eval -- RooRealVar::setRange(z) new range named 'sigRegion' created with bounds [-1,1]\n"
]
}
],
"source": [
"y.setRange(\"sigRegion\", -1, 1);\n",
"z.setRange(\"sigRegion\", -1, 1);"
]
},
{
"cell_type": "markdown",
"id": "24a9036d",
"metadata": {},
"source": [
"Make plot frame"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "869a531e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:59.494073Z",
"iopub.status.busy": "2024-03-19T19:15:59.493720Z",
"iopub.status.idle": "2024-03-19T19:15:59.718149Z",
"shell.execute_reply": "2024-03-19T19:15:59.712369Z"
}
},
"outputs": [],
"source": [
"RooPlot *frame2 = x.frame(Title(\"Same projection on X in signal range of (Y,Z)\"), Bins(40));"
]
},
{
"cell_type": "markdown",
"id": "453c31a4",
"metadata": {},
"source": [
"Plot subset of data in which all observables are inside \"sigRegion\"\n",
"For observables that do not have an explicit \"sigRegion\" range defined (e.g. observable)\n",
"an implicit definition is used that is identical to the full range (i.e. [-5,5] for x)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b48b8ed8",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:59.723485Z",
"iopub.status.busy": "2024-03-19T19:15:59.723093Z",
"iopub.status.idle": "2024-03-19T19:15:59.951833Z",
"shell.execute_reply": "2024-03-19T19:15:59.936693Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_55:2:2: error: reference to 'data' is ambiguous\n",
" data->plotOn(frame2, CutRange(\"sigRegion\"));\n",
" ^\n",
"input_line_51:5:29: note: candidate found by name lookup is 'data'\n",
"std::unique_ptr data{model.generate({x, y, z}, 20000)};\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": [
"data->plotOn(frame2, CutRange(\"sigRegion\"));"
]
},
{
"cell_type": "markdown",
"id": "ae73f693",
"metadata": {},
"source": [
"Project model on x, integrating projected observables (y,z) only in \"sigRegion\""
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "1bcb8884",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:15:59.963479Z",
"iopub.status.busy": "2024-03-19T19:15:59.962790Z",
"iopub.status.idle": "2024-03-19T19:16:00.290190Z",
"shell.execute_reply": "2024-03-19T19:16:00.289079Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_57:2:3: error: use of undeclared identifier 'frame'\n",
" (frame->GetYaxis()->SetTitleOffset(1.3999999999999999))\n",
" ^\n",
"Error in : Error evaluating expression (frame->GetYaxis()->SetTitleOffset(1.3999999999999999))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"model.plotOn(frame2, ProjectionRange(\"sigRegion\"));\n",
"\n",
"TCanvas *c = new TCanvas(\"rf311_rangeplot\", \"rf310_rangeplot\", 800, 400);\n",
"c->Divide(2);\n",
"c->cd(1);\n",
"gPad->SetLeftMargin(0.15);\n",
"frame->GetYaxis()->SetTitleOffset(1.4);\n",
"frame->Draw();\n",
"c->cd(2);\n",
"gPad->SetLeftMargin(0.15);\n",
"frame2->GetYaxis()->SetTitleOffset(1.4);\n",
"frame2->Draw();"
]
},
{
"cell_type": "markdown",
"id": "dc6ae811",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "e1df9c89",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:16:00.298765Z",
"iopub.status.busy": "2024-03-19T19:16:00.298374Z",
"iopub.status.idle": "2024-03-19T19:16:00.692947Z",
"shell.execute_reply": "2024-03-19T19:16:00.691874Z"
}
},
"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
}