{ "cells": [ { "cell_type": "markdown", "id": "e7f8aa30", "metadata": {}, "source": [ "# rf203_ranges\n", "Fitting and plotting in sub ranges.\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, May 19, 2026 at 08:29 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "b5fc787e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:43.554232Z", "iopub.status.busy": "2026-05-19T20:29:43.554120Z", "iopub.status.idle": "2026-05-19T20:29:43.567083Z", "shell.execute_reply": "2026-05-19T20:29:43.566638Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooPolynomial.h\"\n", "#include \"RooAddPdf.h\"\n", "#include \"RooFitResult.h\"\n", "#include \"RooPlot.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"TH1.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "c7bd8552", "metadata": {}, "source": [ "Setup model\n", "---------------------" ] }, { "cell_type": "markdown", "id": "2a4507a4", "metadata": {}, "source": [ "Construct observables x" ] }, { "cell_type": "code", "execution_count": 2, "id": "af560dc2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:43.568744Z", "iopub.status.busy": "2026-05-19T20:29:43.568619Z", "iopub.status.idle": "2026-05-19T20:29:43.780697Z", "shell.execute_reply": "2026-05-19T20:29:43.779992Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", -10, 10);" ] }, { "cell_type": "markdown", "id": "b1b72c59", "metadata": {}, "source": [ "Construct gaussx(x,mx,1)" ] }, { "cell_type": "code", "execution_count": 3, "id": "2647d4b9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:43.782645Z", "iopub.status.busy": "2026-05-19T20:29:43.782506Z", "iopub.status.idle": "2026-05-19T20:29:43.989951Z", "shell.execute_reply": "2026-05-19T20:29:43.989489Z" } }, "outputs": [], "source": [ "RooRealVar mx(\"mx\", \"mx\", 0, -10, 10);\n", "RooGaussian gx(\"gx\", \"gx\", x, mx, 1.0);" ] }, { "cell_type": "markdown", "id": "c1a52a62", "metadata": {}, "source": [ "Construct px = 1 (flat in x)" ] }, { "cell_type": "code", "execution_count": 4, "id": "7759c1bb", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:44.002380Z", "iopub.status.busy": "2026-05-19T20:29:44.002244Z", "iopub.status.idle": "2026-05-19T20:29:44.216195Z", "shell.execute_reply": "2026-05-19T20:29:44.215548Z" } }, "outputs": [], "source": [ "RooPolynomial px(\"px\", \"px\", x);" ] }, { "cell_type": "markdown", "id": "c7c9d5f0", "metadata": {}, "source": [ "Construct model = f*gx + (1-f)px" ] }, { "cell_type": "code", "execution_count": 5, "id": "4407d98c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:44.218129Z", "iopub.status.busy": "2026-05-19T20:29:44.218010Z", "iopub.status.idle": "2026-05-19T20:29:44.430442Z", "shell.execute_reply": "2026-05-19T20:29:44.429832Z" } }, "outputs": [], "source": [ "RooRealVar f(\"f\", \"f\", 0., 1.);\n", "RooAddPdf model(\"model\", \"model\", RooArgList(gx, px), f);" ] }, { "cell_type": "markdown", "id": "1a15633e", "metadata": {}, "source": [ "Generated 10000 events in (x,y) from pdf model" ] }, { "cell_type": "code", "execution_count": 6, "id": "1bd759fe", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:44.451105Z", "iopub.status.busy": "2026-05-19T20:29:44.450944Z", "iopub.status.idle": "2026-05-19T20:29:44.669336Z", "shell.execute_reply": "2026-05-19T20:29:44.668907Z" } }, "outputs": [], "source": [ "std::unique_ptr modelData{model.generate(x, 10000)};" ] }, { "cell_type": "markdown", "id": "390f9c8a", "metadata": {}, "source": [ "Fit full range\n", "---------------------------" ] }, { "cell_type": "markdown", "id": "dc54fcb2", "metadata": {}, "source": [ "Fit pdf to all data" ] }, { "cell_type": "code", "execution_count": 7, "id": "82bae8c7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:44.671425Z", "iopub.status.busy": "2026-05-19T20:29:44.671305Z", "iopub.status.idle": "2026-05-19T20:29:44.879565Z", "shell.execute_reply": "2026-05-19T20:29:44.878969Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:Fitting -- RooAbsPdf::fitTo(model) 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 893.893 μs\n", "[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData) Summation contains a RooNLLVar, using its error level\n", "[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n" ] } ], "source": [ "std::unique_ptr r_full{model.fitTo(*modelData, Save(true), PrintLevel(-1))};" ] }, { "cell_type": "markdown", "id": "14e59fba", "metadata": {}, "source": [ "Fit partial range\n", "----------------------------------" ] }, { "cell_type": "markdown", "id": "e4e0d820", "metadata": {}, "source": [ "Define \"signal\" range in x as [-3,3]" ] }, { "cell_type": "code", "execution_count": 8, "id": "0479779e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:44.881257Z", "iopub.status.busy": "2026-05-19T20:29:44.881140Z", "iopub.status.idle": "2026-05-19T20:29:45.088856Z", "shell.execute_reply": "2026-05-19T20:29:45.088331Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'signal' created with bounds [-3,3]\n" ] } ], "source": [ "x.setRange(\"signal\", -3, 3);" ] }, { "cell_type": "markdown", "id": "84458ec9", "metadata": {}, "source": [ "Fit pdf only to data in \"signal\" range" ] }, { "cell_type": "code", "execution_count": 9, "id": "7bbbf3fe", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:45.090593Z", "iopub.status.busy": "2026-05-19T20:29:45.090474Z", "iopub.status.idle": "2026-05-19T20:29:45.298663Z", "shell.execute_reply": "2026-05-19T20:29:45.297930Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'fit_nll_model_modelData' created with bounds [-3,3]\n", "[#1] INFO:Fitting -- RooAbsPdf::fitTo(model) fixing normalization set for coefficient determination to observables in data\n", "[#1] INFO:Fitting -- Creation of NLL object took 461.242 μs\n", "[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData) Summation contains a RooNLLVar, using its error level\n", "[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n" ] } ], "source": [ "std::unique_ptr r_sig{model.fitTo(*modelData, Save(true), Range(\"signal\"), PrintLevel(-1))};" ] }, { "cell_type": "markdown", "id": "34c39f18", "metadata": {}, "source": [ "Plot / print results\n", "---------------------------------------" ] }, { "cell_type": "markdown", "id": "ddd4da93", "metadata": {}, "source": [ "Make plot frame in x and add data and fitted model" ] }, { "cell_type": "code", "execution_count": 10, "id": "acc44e20", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:45.300137Z", "iopub.status.busy": "2026-05-19T20:29:45.300021Z", "iopub.status.idle": "2026-05-19T20:29:45.506760Z", "shell.execute_reply": "2026-05-19T20:29:45.506063Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) p.d.f was fitted in a subrange and no explicit NormRange() was specified. Plotting / normalising in fit range. To override, do one of the following\n", "\t- Clear the automatic fit range attribute: .removeStringAttribute(\"fitrange\");\n", "\t- Explicitly specify the plotting range: Range(\"\").\n", "\t- Explicitly specify where to compute the normalisation: NormRange(\"\").\n", "\tThe default (full) range can be denoted with Range(\"\") / NormRange(\"\").\n", "[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) only plotting range ''\n", "[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) p.d.f. curve is normalized using explicit choice of ranges 'fit_nll_model_modelData'\n", "[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) p.d.f was fitted in a subrange and no explicit Range() and NormRange() was specified. Plotting / normalising in fit range. To override, do one of the following\n", "\t- Clear the automatic fit range attribute: .removeStringAttribute(\"fitrange\");\n", "\t- Explicitly specify the plotting range: Range(\"\").\n", "\t- Explicitly specify where to compute the normalisation: NormRange(\"\").\n", "\tThe default (full) range can be denoted with Range(\"\") / NormRange(\"\").\n", "[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) only plotting range 'fit_nll_model_modelData'\n", "[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) p.d.f. curve is normalized using explicit choice of ranges 'fit_nll_model_modelData'\n" ] } ], "source": [ "RooPlot *frame = x.frame(Title(\"Fitting a sub range\"));\n", "modelData->plotOn(frame);\n", "model.plotOn(frame, Range(\"\"), LineStyle(kDashed), LineColor(kRed)); // Add shape in full ranged dashed\n", "model.plotOn(frame); // By default only fitted range is shown" ] }, { "cell_type": "markdown", "id": "6f04cfe3", "metadata": {}, "source": [ "Print fit results" ] }, { "cell_type": "code", "execution_count": 11, "id": "a8f68a13", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:45.508177Z", "iopub.status.busy": "2026-05-19T20:29:45.508060Z", "iopub.status.idle": "2026-05-19T20:29:45.716546Z", "shell.execute_reply": "2026-05-19T20:29:45.715931Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "result of fit on all data \n", "\n", " RooFitResult: minimized FCN value: 25939.4, estimated distance to minimum: 3.77183e-06\n", " covariance matrix quality: Full, accurate covariance matrix\n", " Status : MINIMIZE=0 HESSE=0 \n", "\n", " Floating Parameter FinalValue +/- Error \n", " -------------------- --------------------------\n", " f 5.0441e-01 +/- 6.32e-03\n", " mx -2.1605e-02 +/- 1.77e-02\n", "\n", "result of fit in in signal region (note increased error on signal fraction)\n", "\n", " RooFitResult: minimized FCN value: 10339.5, estimated distance to minimum: 0.000279216\n", " covariance matrix quality: Full, accurate covariance matrix\n", " Status : MINIMIZE=0 HESSE=0 \n", "\n", " Floating Parameter FinalValue +/- Error \n", " -------------------- --------------------------\n", " f 4.8979e-01 +/- 1.62e-02\n", " mx -2.1518e-02 +/- 1.79e-02\n", "\n" ] } ], "source": [ "cout << \"result of fit on all data \" << endl;\n", "r_full->Print();\n", "cout << \"result of fit in in signal region (note increased error on signal fraction)\" << endl;\n", "r_sig->Print();" ] }, { "cell_type": "markdown", "id": "88eb733f", "metadata": {}, "source": [ "Draw frame on canvas" ] }, { "cell_type": "code", "execution_count": 12, "id": "0b13c60d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:45.718241Z", "iopub.status.busy": "2026-05-19T20:29:45.718127Z", "iopub.status.idle": "2026-05-19T20:29:45.925354Z", "shell.execute_reply": "2026-05-19T20:29:45.924974Z" } }, "outputs": [], "source": [ "new TCanvas(\"rf203_ranges\", \"rf203_ranges\", 600, 600);\n", "gPad->SetLeftMargin(0.15);\n", "frame->GetYaxis()->SetTitleOffset(1.4);\n", "frame->Draw();\n", "\n", "return;" ] }, { "cell_type": "markdown", "id": "ab3d65c0", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 13, "id": "24ca85d5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:45.933987Z", "iopub.status.busy": "2026-05-19T20:29:45.933857Z", "iopub.status.idle": "2026-05-19T20:29:46.161056Z", "shell.execute_reply": "2026-05-19T20:29:46.160434Z" } }, "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 }