{
"cells": [
{
"cell_type": "markdown",
"id": "31c352c0",
"metadata": {},
"source": [
"# rf212_plottingInRanges_blinding\n",
"Plot a PDF in disjunct ranges, and get normalisation right.\n",
"\n",
"Usually, when comparing a fit to data, one should first plot the data, and then the PDF.\n",
"In this case, the PDF is automatically normalised to match the number of data events in the plot.\n",
"However, when plotting only a sub-range, when e.g. a signal region has to be blinded,\n",
"one has to exclude the blinded region from the computation of the normalisation.\n",
"\n",
"In this tutorial, we show how to explicitly choose the normalisation when plotting using `NormRange()`.\n",
"\n",
"Thanks to Marc Escalier for asking how to do this correctly.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Stephan Hageboeck \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:30 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "832071ea",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:24.878012Z",
"iopub.status.busy": "2026-05-19T20:30:24.877907Z",
"iopub.status.idle": "2026-05-19T20:30:24.892371Z",
"shell.execute_reply": "2026-05-19T20:30:24.891472Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "97e8f965",
"metadata": {},
"source": [
"Make a fit model"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d8a30868",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:24.893804Z",
"iopub.status.busy": "2026-05-19T20:30:24.893687Z",
"iopub.status.idle": "2026-05-19T20:30:25.242712Z",
"shell.execute_reply": "2026-05-19T20:30:25.241957Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"The observable\", 1, 30);\n",
"RooRealVar tau(\"tau\", \"The exponent\", -0.1337, -10., -0.1);\n",
"RooExponential expo(\"expo\", \"A falling exponential function\", x, tau);"
]
},
{
"cell_type": "markdown",
"id": "b735f1cf",
"metadata": {},
"source": [
"Define the sidebands (e.g. background regions)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "34ecd712",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:25.244826Z",
"iopub.status.busy": "2026-05-19T20:30:25.244704Z",
"iopub.status.idle": "2026-05-19T20:30:25.453034Z",
"shell.execute_reply": "2026-05-19T20:30:25.452291Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'full' created with bounds [1,30]\n",
"[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'left' created with bounds [1,10]\n",
"[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'right' created with bounds [20,30]\n"
]
}
],
"source": [
"x.setRange(\"full\", 1, 30);\n",
"x.setRange(\"left\", 1, 10);\n",
"x.setRange(\"right\", 20, 30);"
]
},
{
"cell_type": "markdown",
"id": "cb8ea560",
"metadata": {},
"source": [
"Generate toy data, and cut out the blinded region."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c3104df6",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:25.454746Z",
"iopub.status.busy": "2026-05-19T20:30:25.454620Z",
"iopub.status.idle": "2026-05-19T20:30:25.660313Z",
"shell.execute_reply": "2026-05-19T20:30:25.659650Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_51: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{expo.generate(x, 1000)};\n",
" ^\n"
]
}
],
"source": [
"std::unique_ptr data{expo.generate(x, 1000)};\n",
"std::unique_ptr blindedData{data->reduce(CutRange(\"left,right\"))};"
]
},
{
"cell_type": "markdown",
"id": "1bcfd279",
"metadata": {},
"source": [
"Kick tau a bit, and run an unbinned fit where the blinded data are missing.\n",
"----------------------------------------------------------------------------------------------------------\n",
"The fit should be done only in the unblinded regions, otherwise it would\n",
"try to make the model adapt to the empty bins in the blinded region."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "02662c09",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:25.662270Z",
"iopub.status.busy": "2026-05-19T20:30:25.662154Z",
"iopub.status.idle": "2026-05-19T20:30:25.883229Z",
"shell.execute_reply": "2026-05-19T20:30:25.882551Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'fit_nll_expo_expoData_left' created with bounds [1,10]\n",
"[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'fit_nll_expo_expoData_right' created with bounds [20,30]\n",
"[#1] INFO:Fitting -- RooAbsPdf::fitTo(expo_over_expo_Int[x|left,right]) fixing normalization set for coefficient determination to observables in data\n",
"[#1] INFO:Fitting -- using generic CPU library compiled with no vectorizations\n",
"[#1] INFO:Fitting -- Creation of NLL object took 880.564 μs\n",
"[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_expo_over_expo_Int[x|left,right]_expoData) Summation contains a RooNLLVar, using its error level\n",
"[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n"
]
}
],
"source": [
"tau.setVal(-2.);\n",
"expo.fitTo(*blindedData, Range(\"left,right\"), PrintLevel(-1));"
]
},
{
"cell_type": "markdown",
"id": "9b0a74f0",
"metadata": {},
"source": [
"Clear the \"fitrange\" attribute of the PDF. Otherwise, the fitrange would\n",
"be automatically taken as the NormRange() for plotting. We want to avoid\n",
"this, because the point of this tutorial is to show what can go wrong when\n",
"the NormRange() is not specified."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6b3ac461",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:25.884782Z",
"iopub.status.busy": "2026-05-19T20:30:25.884659Z",
"iopub.status.idle": "2026-05-19T20:30:26.089841Z",
"shell.execute_reply": "2026-05-19T20:30:26.089362Z"
}
},
"outputs": [],
"source": [
"expo.removeStringAttribute(\"fitrange\");"
]
},
{
"cell_type": "markdown",
"id": "ec745954",
"metadata": {},
"source": [
"Here we will plot the results"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "95820840",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:26.100399Z",
"iopub.status.busy": "2026-05-19T20:30:26.100267Z",
"iopub.status.idle": "2026-05-19T20:30:26.308052Z",
"shell.execute_reply": "2026-05-19T20:30:26.307449Z"
}
},
"outputs": [],
"source": [
"TCanvas *canvas=new TCanvas(\"canvas\",\"canvas\",800,600);\n",
"canvas->Divide(2,1);"
]
},
{
"cell_type": "markdown",
"id": "92d090eb",
"metadata": {},
"source": [
"Wrong:\n",
"----------------------------------------------------------------------------------------------------------\n",
"Plotting each slice on its own normalises the PDF over its plotting range. For the full curve, that means\n",
"that the blinded region where data is missing is included in the normalisation calculation. The PDF therefore\n",
"comes out too low, and doesn't match up with the slices in the side bands, which are normalised to \"their\" data."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "05f1b460",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:26.310260Z",
"iopub.status.busy": "2026-05-19T20:30:26.310139Z",
"iopub.status.idle": "2026-05-19T20:30:26.515660Z",
"shell.execute_reply": "2026-05-19T20:30:26.515153Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Now plotting with unique normalisation for each slice.\n"
]
}
],
"source": [
"std::cout << \"Now plotting with unique normalisation for each slice.\" << std::endl;\n",
"canvas->cd(1);\n",
"RooPlot* plotFrame = x.frame(RooFit::Title(\"Wrong: Each slice normalised over its plotting range\"));"
]
},
{
"cell_type": "markdown",
"id": "bc8a51d9",
"metadata": {},
"source": [
"Plot only the blinded data, and then plot the PDF over the full range as well as both sidebands"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "f5e85315",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:26.517083Z",
"iopub.status.busy": "2026-05-19T20:30:26.516956Z",
"iopub.status.idle": "2026-05-19T20:30:26.725141Z",
"shell.execute_reply": "2026-05-19T20:30:26.724637Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Plotting -- RooAbsPdf::plotOn(expo) only plotting range 'full', curve is normalized to data in given range\n",
"[#1] INFO:Plotting -- RooAbsPdf::plotOn(expo) only plotting range 'left', curve is normalized to data in given range\n",
"[#1] INFO:Plotting -- RooAbsPdf::plotOn(expo) only plotting range 'right', curve is normalized to data in given range\n"
]
}
],
"source": [
"blindedData->plotOn(plotFrame);\n",
"expo.plotOn(plotFrame, LineColor(kRed), Range(\"full\"));\n",
"expo.plotOn(plotFrame, LineColor(kBlue), Range(\"left\"));\n",
"expo.plotOn(plotFrame, LineColor(kGreen), Range(\"right\"));\n",
"\n",
"plotFrame->Draw();"
]
},
{
"cell_type": "markdown",
"id": "cac99128",
"metadata": {},
"source": [
"Right:\n",
"----------------------------------------------------------------------------------------------------------\n",
"Make the same plot, but normalise each piece with respect to the regions \"left\" AND \"right\". This requires setting\n",
"a \"NormRange\", which tells RooFit over which range the PDF has to be integrated to normalise.\n",
"This means that the normalisation of the blue and green curves is slightly different from the left plot,\n",
"because they get a common scale factor."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "e75bb504",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:26.726657Z",
"iopub.status.busy": "2026-05-19T20:30:26.726515Z",
"iopub.status.idle": "2026-05-19T20:30:26.932196Z",
"shell.execute_reply": "2026-05-19T20:30:26.931707Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Now plotting with correct norm ranges:\n"
]
}
],
"source": [
"std::cout << \"\\n\\nNow plotting with correct norm ranges:\" << std::endl;\n",
"canvas->cd(2);\n",
"RooPlot* plotFrameWithNormRange = x.frame(RooFit::Title(\"Right: All slices have common normalisation\"));"
]
},
{
"cell_type": "markdown",
"id": "f231f8ca",
"metadata": {},
"source": [
"Plot only the blinded data, and then plot the PDF over the full range as well as both sidebands"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "aad6c782",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:26.938316Z",
"iopub.status.busy": "2026-05-19T20:30:26.938190Z",
"iopub.status.idle": "2026-05-19T20:30:27.253307Z",
"shell.execute_reply": "2026-05-19T20:30:27.252840Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Plotting -- RooAbsPdf::plotOn(expo) only plotting range 'left'\n",
"[#1] INFO:Plotting -- RooAbsPdf::plotOn(expo) p.d.f. curve is normalized using explicit choice of ranges 'left,right'\n",
"[#1] INFO:Plotting -- RooAbsPdf::plotOn(expo) only plotting range 'right'\n",
"[#1] INFO:Plotting -- RooAbsPdf::plotOn(expo) p.d.f. curve is normalized using explicit choice of ranges 'left,right'\n",
"[#1] INFO:Plotting -- RooAbsPdf::plotOn(expo) only plotting range 'full'\n",
"[#1] INFO:Plotting -- RooAbsPdf::plotOn(expo) p.d.f. curve is normalized using explicit choice of ranges 'left,right'\n"
]
}
],
"source": [
"blindedData->plotOn(plotFrameWithNormRange);\n",
"expo.plotOn(plotFrameWithNormRange, LineColor(kBlue), Range(\"left\"), RooFit::NormRange(\"left,right\"));\n",
"expo.plotOn(plotFrameWithNormRange, LineColor(kGreen), Range(\"right\"), RooFit::NormRange(\"left,right\"));\n",
"expo.plotOn(plotFrameWithNormRange, LineColor(kRed), Range(\"full\"), RooFit::NormRange(\"left,right\"), LineStyle(10));\n",
"\n",
"plotFrameWithNormRange->Draw();\n",
"\n",
"canvas->Draw();"
]
},
{
"cell_type": "markdown",
"id": "5e6fd0ad",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5433c8c3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:30:27.260737Z",
"iopub.status.busy": "2026-05-19T20:30:27.260594Z",
"iopub.status.idle": "2026-05-19T20:30:27.473617Z",
"shell.execute_reply": "2026-05-19T20:30:27.472977Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\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
}