{ "cells": [ { "cell_type": "markdown", "id": "f876507b", "metadata": {}, "source": [ "# rf314_paramfitrange\n", "Multidimensional models: working with parametrized ranges in a fit.\n", "This an example of a fit with an acceptance that changes per-event\n", "\n", "`pdf = exp(-t/tau)` with `t[tmin,5]`\n", "\n", "where `t` and `tmin` are both observables in the dataset\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:31 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "325f24de", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:16.835038Z", "iopub.status.busy": "2026-05-19T20:31:16.834869Z", "iopub.status.idle": "2026-05-19T20:31:16.854495Z", "shell.execute_reply": "2026-05-19T20:31:16.854037Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooConstVar.h\"\n", "#include \"RooExponential.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"RooPlot.h\"\n", "#include \"RooFitResult.h\"\n", "\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "75b0d1ca", "metadata": {}, "source": [ "Define observables and decay pdf\n", "---------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "6d1168e6", "metadata": {}, "source": [ "Declare observables" ] }, { "cell_type": "code", "execution_count": 2, "id": "77b5cd57", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:16.856243Z", "iopub.status.busy": "2026-05-19T20:31:16.856121Z", "iopub.status.idle": "2026-05-19T20:31:17.173047Z", "shell.execute_reply": "2026-05-19T20:31:17.172544Z" } }, "outputs": [], "source": [ "RooRealVar t(\"t\", \"t\", 0, 5);\n", "RooRealVar tmin(\"tmin\", \"tmin\", 0, 0, 5);" ] }, { "cell_type": "markdown", "id": "f0969f0a", "metadata": {}, "source": [ "Make parametrized range in t : [tmin,5]" ] }, { "cell_type": "code", "execution_count": 3, "id": "8fc29713", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:17.174938Z", "iopub.status.busy": "2026-05-19T20:31:17.174817Z", "iopub.status.idle": "2026-05-19T20:31:17.403052Z", "shell.execute_reply": "2026-05-19T20:31:17.402276Z" } }, "outputs": [], "source": [ "t.setRange(tmin, RooConst(t.getMax()));" ] }, { "cell_type": "markdown", "id": "e040eb6b", "metadata": {}, "source": [ "Make pdf" ] }, { "cell_type": "code", "execution_count": 4, "id": "208a166e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:17.415412Z", "iopub.status.busy": "2026-05-19T20:31:17.415272Z", "iopub.status.idle": "2026-05-19T20:31:17.623113Z", "shell.execute_reply": "2026-05-19T20:31:17.622642Z" } }, "outputs": [], "source": [ "RooRealVar tau(\"tau\", \"tau\", -1.54, -10, -0.1);\n", "RooExponential model(\"model\", \"model\", t, tau);" ] }, { "cell_type": "markdown", "id": "88ede56c", "metadata": {}, "source": [ "Create input data\n", "------------------------------------" ] }, { "cell_type": "markdown", "id": "bbd75d5c", "metadata": {}, "source": [ "Generate complete dataset without acceptance cuts (for reference)" ] }, { "cell_type": "code", "execution_count": 5, "id": "927be15e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:17.626867Z", "iopub.status.busy": "2026-05-19T20:31:17.626735Z", "iopub.status.idle": "2026-05-19T20:31:17.834741Z", "shell.execute_reply": "2026-05-19T20:31:17.834203Z" } }, "outputs": [], "source": [ "std::unique_ptr dall{model.generate(t, 10000)};" ] }, { "cell_type": "markdown", "id": "c6dd7c07", "metadata": {}, "source": [ "Generate a (fake) prototype dataset for acceptance limit values" ] }, { "cell_type": "code", "execution_count": 6, "id": "685fa99f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:17.836387Z", "iopub.status.busy": "2026-05-19T20:31:17.836264Z", "iopub.status.idle": "2026-05-19T20:31:18.070201Z", "shell.execute_reply": "2026-05-19T20:31:18.068661Z" } }, "outputs": [], "source": [ "std::unique_ptr tmp{RooGaussian(\"gmin\", \"gmin\", tmin, 0.0, 0.5).generate(tmin, 5000)};" ] }, { "cell_type": "markdown", "id": "b90d93aa", "metadata": {}, "source": [ "Generate dataset with t values that observe (t>tmin)" ] }, { "cell_type": "code", "execution_count": 7, "id": "e963c88b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:18.077366Z", "iopub.status.busy": "2026-05-19T20:31:18.077213Z", "iopub.status.idle": "2026-05-19T20:31:18.290189Z", "shell.execute_reply": "2026-05-19T20:31:18.289648Z" } }, "outputs": [], "source": [ "std::unique_ptr dacc{model.generate(t, ProtoData(*tmp))};" ] }, { "cell_type": "markdown", "id": "f0c6b71b", "metadata": {}, "source": [ "Fit pdf to data in acceptance region\n", "-----------------------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": 8, "id": "1a74b148", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:18.291864Z", "iopub.status.busy": "2026-05-19T20:31:18.291742Z", "iopub.status.idle": "2026-05-19T20:31:18.500143Z", "shell.execute_reply": "2026-05-19T20:31:18.499696Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:Fitting -- RooAbsPdf::fitTo(model_over_model_Int[t]) 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 797.414 μs\n", "[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_over_model_Int[t]_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{model.fitTo(*dacc, Save(), PrintLevel(-1))};" ] }, { "cell_type": "markdown", "id": "af5c92fa", "metadata": {}, "source": [ "Plot fitted pdf on full and accepted data\n", "---------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "8216e03a", "metadata": {}, "source": [ "Make plot frame, add datasets and overlay model" ] }, { "cell_type": "code", "execution_count": 9, "id": "b0614a3e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:18.501562Z", "iopub.status.busy": "2026-05-19T20:31:18.501440Z", "iopub.status.idle": "2026-05-19T20:31:18.719593Z", "shell.execute_reply": "2026-05-19T20:31:18.719151Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:Plotting -- RooPlot::updateFitRangeNorm: New event count of 5000 will supersede previous event count of 10000 for normalization of PDF projections\n" ] } ], "source": [ "RooPlot *frame = t.frame(Title(\"Fit to data with per-event acceptance\"));\n", "dall->plotOn(frame, MarkerColor(kRed), LineColor(kRed));\n", "model.plotOn(frame);\n", "dacc->plotOn(frame);" ] }, { "cell_type": "markdown", "id": "1f621194", "metadata": {}, "source": [ "Print fit results to demonstrate absence of bias" ] }, { "cell_type": "code", "execution_count": 10, "id": "e459e5a0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:18.721025Z", "iopub.status.busy": "2026-05-19T20:31:18.720904Z", "iopub.status.idle": "2026-05-19T20:31:18.929303Z", "shell.execute_reply": "2026-05-19T20:31:18.928864Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " RooFitResult: minimized FCN value: 2823.97, estimated distance to minimum: 3.17108e-08\n", " covariance matrix quality: Full, accurate covariance matrix\n", " Status : MINIMIZE=0 HESSE=0 \n", "\n", " Floating Parameter InitialValue FinalValue +/- Error GblCorr.\n", " -------------------- ------------ -------------------------- --------\n", " tau -1.5400e+00 -1.5335e+00 +/- 2.22e-02 \n", "\n" ] } ], "source": [ "r->Print(\"v\");\n", "\n", "new TCanvas(\"rf314_paramranges\", \"rf314_paramranges\", 600, 600);\n", "gPad->SetLeftMargin(0.15);\n", "frame->GetYaxis()->SetTitleOffset(1.6);\n", "frame->Draw();\n", "\n", "return;" ] }, { "cell_type": "markdown", "id": "f9d853ff", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 11, "id": "b7272f2e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:18.931252Z", "iopub.status.busy": "2026-05-19T20:31:18.931127Z", "iopub.status.idle": "2026-05-19T20:31:19.159410Z", "shell.execute_reply": "2026-05-19T20:31:19.158845Z" } }, "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 }