{ "cells": [ { "cell_type": "markdown", "id": "35ff507e", "metadata": {}, "source": [ "# rf306_condpereventerrors\n", "Multidimensional models: conditional pdf with per-event errors\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:30 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "394df506", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:47.191146Z", "iopub.status.busy": "2026-05-19T20:30:47.191034Z", "iopub.status.idle": "2026-05-19T20:30:47.204483Z", "shell.execute_reply": "2026-05-19T20:30:47.203951Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooGaussModel.h\"\n", "#include \"RooDecay.h\"\n", "#include \"RooLandau.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": "d84cbfe2", "metadata": {}, "source": [ "B-physics pdf with per-event Gaussian resolution\n", "----------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "82e17918", "metadata": {}, "source": [ "Observables" ] }, { "cell_type": "code", "execution_count": 2, "id": "bd832c46", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:47.206204Z", "iopub.status.busy": "2026-05-19T20:30:47.206088Z", "iopub.status.idle": "2026-05-19T20:30:47.522407Z", "shell.execute_reply": "2026-05-19T20:30:47.521694Z" } }, "outputs": [], "source": [ "RooRealVar dt(\"dt\", \"dt\", -10, 10);\n", "RooRealVar dterr(\"dterr\", \"per-event error on dt\", 0.01, 10);" ] }, { "cell_type": "markdown", "id": "a29d4870", "metadata": {}, "source": [ "Build a gaussian resolution model scaled by the per-event error = gauss(dt,bias,sigma*dterr)" ] }, { "cell_type": "code", "execution_count": 3, "id": "c451bcab", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:47.524800Z", "iopub.status.busy": "2026-05-19T20:30:47.524683Z", "iopub.status.idle": "2026-05-19T20:30:47.732505Z", "shell.execute_reply": "2026-05-19T20:30:47.731937Z" } }, "outputs": [], "source": [ "RooRealVar bias(\"bias\", \"bias\", 0, -10, 10);\n", "RooRealVar sigma(\"sigma\", \"per-event error scale factor\", 1, 0.1, 10);\n", "RooGaussModel gm(\"gm1\", \"gauss model scaled bt per-event error\", dt, bias, sigma, dterr);" ] }, { "cell_type": "markdown", "id": "50f9329a", "metadata": {}, "source": [ "Construct decay(dt) (x) gauss1(dt|dterr)" ] }, { "cell_type": "code", "execution_count": 4, "id": "52090f85", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:47.734709Z", "iopub.status.busy": "2026-05-19T20:30:47.734567Z", "iopub.status.idle": "2026-05-19T20:30:47.942191Z", "shell.execute_reply": "2026-05-19T20:30:47.941644Z" } }, "outputs": [], "source": [ "RooRealVar tau(\"tau\", \"tau\", 1.548);\n", "RooDecay decay_gm(\"decay_gm\", \"decay\", dt, tau, gm, RooDecay::DoubleSided);" ] }, { "cell_type": "markdown", "id": "5296ba7d", "metadata": {}, "source": [ "Construct fake 'external' data with per-event error\n", "------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "bcd1e890", "metadata": {}, "source": [ "Use landau pdf to get somewhat realistic distribution with long tail" ] }, { "cell_type": "code", "execution_count": 5, "id": "a1af4619", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:47.944584Z", "iopub.status.busy": "2026-05-19T20:30:47.944459Z", "iopub.status.idle": "2026-05-19T20:30:48.152412Z", "shell.execute_reply": "2026-05-19T20:30:48.151767Z" } }, "outputs": [], "source": [ "RooLandau pdfDtErr(\"pdfDtErr\", \"pdfDtErr\", dterr, 1.0, 0.25);\n", "std::unique_ptr expDataDterr{pdfDtErr.generate(dterr, 10000)};" ] }, { "cell_type": "markdown", "id": "53d8e317", "metadata": {}, "source": [ "Sample data from conditional decay_gm(dt|dterr)\n", "---------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "16833a90", "metadata": {}, "source": [ "Specify external dataset with dterr values to use decay_dm as conditional pdf" ] }, { "cell_type": "code", "execution_count": 6, "id": "2247e954", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:48.154277Z", "iopub.status.busy": "2026-05-19T20:30:48.154159Z", "iopub.status.idle": "2026-05-19T20:30:48.362244Z", "shell.execute_reply": "2026-05-19T20:30:48.361745Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_59: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{decay_gm.generate(dt, ProtoData(*expDataDterr))};\n", " ^\n" ] } ], "source": [ "std::unique_ptr data{decay_gm.generate(dt, ProtoData(*expDataDterr))};" ] }, { "cell_type": "markdown", "id": "a4e309c6", "metadata": {}, "source": [ "Fit conditional decay_dm(dt|dterr)\n", "---------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "bc07745e", "metadata": {}, "source": [ "Specify dterr as conditional observable" ] }, { "cell_type": "code", "execution_count": 7, "id": "ca5fb46e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:48.363897Z", "iopub.status.busy": "2026-05-19T20:30:48.363777Z", "iopub.status.idle": "2026-05-19T20:30:48.572619Z", "shell.execute_reply": "2026-05-19T20:30:48.572031Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_63:2:18: error: reference to 'data' is ambiguous\n", " decay_gm.fitTo(*data, ConditionalObservables(dterr), PrintLevel(-1));\n", " ^\n", "input_line_59:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{decay_gm.generate(dt, ProtoData(*expDataDterr))};\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n", " data(initializer_list<_Tp> __il) noexcept\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n", " data(_Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n", " data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n", " data(_Tp (&__array)[_Nm]) noexcept\n", " ^\n" ] } ], "source": [ "decay_gm.fitTo(*data, ConditionalObservables(dterr), PrintLevel(-1));" ] }, { "cell_type": "markdown", "id": "93d0388a", "metadata": {}, "source": [ "Plot conditional decay_dm(dt|dterr)\n", "---------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "842d4880", "metadata": {}, "source": [ "Make two-dimensional plot of conditional pdf in (dt,dterr)" ] }, { "cell_type": "code", "execution_count": 8, "id": "05c4206a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:48.574145Z", "iopub.status.busy": "2026-05-19T20:30:48.574029Z", "iopub.status.idle": "2026-05-19T20:30:48.782050Z", "shell.execute_reply": "2026-05-19T20:30:48.781538Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:NumericIntegration -- RooRealIntegral::init(gm1_conv_exp(-abs(@0)/@1)_dt_tau_[decay_gm]_Int[dt,dterr]) using numeric integrator RooIntegrator1D to calculate Int(dterr)\n" ] } ], "source": [ "TH1 *hh_decay = decay_gm.createHistogram(\"hh_decay\", dt, Binning(50), YVar(dterr, Binning(50)));\n", "hh_decay->SetLineColor(kBlue);" ] }, { "cell_type": "markdown", "id": "d6b25e95", "metadata": {}, "source": [ "Plot decay_gm(dt|dterr) at various values of dterr" ] }, { "cell_type": "code", "execution_count": 9, "id": "72291703", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:48.783813Z", "iopub.status.busy": "2026-05-19T20:30:48.783694Z", "iopub.status.idle": "2026-05-19T20:30:48.985865Z", "shell.execute_reply": "2026-05-19T20:30:48.985278Z" } }, "outputs": [], "source": [ "RooPlot *frame = dt.frame(Title(\"Slices of decay(dt|dterr) at various dterr\"));\n", "for (Int_t ibin = 0; ibin < 100; ibin += 20) {\n", " dterr.setBin(ibin);\n", " decay_gm.plotOn(frame, Normalization(5.));\n", "}" ] }, { "cell_type": "markdown", "id": "fd00c096", "metadata": {}, "source": [ "Make projection of data an dt" ] }, { "cell_type": "code", "execution_count": 10, "id": "b8367ffb", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:48.987972Z", "iopub.status.busy": "2026-05-19T20:30:48.987854Z", "iopub.status.idle": "2026-05-19T20:30:49.197477Z", "shell.execute_reply": "2026-05-19T20:30:49.195952Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_66:3:1: error: reference to 'data' is ambiguous\n", "data->plotOn(frame2);\n", "^\n", "input_line_59:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{decay_gm.generate(dt, ProtoData(*expDataDterr))};\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n", " data(initializer_list<_Tp> __il) noexcept\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n", " data(_Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n", " data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n", " data(_Tp (&__array)[_Nm]) noexcept\n", " ^\n" ] } ], "source": [ "RooPlot *frame2 = dt.frame(Title(\"Projection of decay(dt|dterr) on dt\"));\n", "data->plotOn(frame2);" ] }, { "cell_type": "markdown", "id": "6eabe466", "metadata": {}, "source": [ "Make projection of decay(dt|dterr) on dt.\n", "\n", "Instead of integrating out dterr, make a weighted average of curves\n", "at values dterr_i as given in the external dataset.\n", "(The true argument bins the data before projection to speed up the process)" ] }, { "cell_type": "code", "execution_count": 11, "id": "6cb7a109", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:49.207108Z", "iopub.status.busy": "2026-05-19T20:30:49.206974Z", "iopub.status.idle": "2026-05-19T20:30:49.414977Z", "shell.execute_reply": "2026-05-19T20:30:49.414590Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_67:2:26: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)(const RooAbsData &, bool)'\n", " decay_gm.plotOn(frame2, ProjWData(*expDataDterr, true));\n", " ^~~~~~~~~\n" ] } ], "source": [ "decay_gm.plotOn(frame2, ProjWData(*expDataDterr, true));" ] }, { "cell_type": "markdown", "id": "15e5f383", "metadata": {}, "source": [ "Draw all frames on canvas" ] }, { "cell_type": "code", "execution_count": 12, "id": "8b85b546", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:49.424290Z", "iopub.status.busy": "2026-05-19T20:30:49.424167Z", "iopub.status.idle": "2026-05-19T20:30:49.781248Z", "shell.execute_reply": "2026-05-19T20:30:49.780780Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN12__cling_N5291cE, _ZN12__cling_N52916__cling_Un1Qu328EPv, __orc_init_func.cling-module-292, $.cling-module-292.__inits.0, _ZN7TObjectnwEm, cling_module_292_, _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb, _ZN5cling7runtime8internal15DynamicExprInfoC2EPKcPPvb, _ZN3TH18GetZaxisEv, _GLOBAL__sub_I_cling_module_292 }) }\n", "IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!\n", "You are probably missing the definition of void cling::runtime::internal::EvaluateT(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)\n", "Maybe you need to load the corresponding shared library?\n" ] } ], "source": [ "TCanvas *c = new TCanvas(\"rf306_condpereventerrors\", \"rf306_condperventerrors\", 1200, 400);\n", "c->Divide(3);\n", "c->cd(1);\n", "gPad->SetLeftMargin(0.20);\n", "hh_decay->GetZaxis()->SetTitleOffset(2.5);\n", "hh_decay->Draw(\"surf\");\n", "c->cd(2);\n", "gPad->SetLeftMargin(0.15);\n", "frame->GetYaxis()->SetTitleOffset(1.6);\n", "frame->Draw();\n", "c->cd(3);\n", "gPad->SetLeftMargin(0.15);\n", "frame2->GetYaxis()->SetTitleOffset(1.6);\n", "frame2->Draw();" ] }, { "cell_type": "markdown", "id": "ce93692d", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 13, "id": "e4538d04", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:49.783077Z", "iopub.status.busy": "2026-05-19T20:30:49.782959Z", "iopub.status.idle": "2026-05-19T20:30:49.997947Z", "shell.execute_reply": "2026-05-19T20:30:49.994857Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-292 }) }\n" ] } ], "source": [ "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 }