{ "cells": [ { "cell_type": "markdown", "id": "b58d8771", "metadata": {}, "source": [ "# rf108_plotbinning\n", "Basic functionality: plotting unbinned data with alternate and variable binnings\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": "a82aeace", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:11.343844Z", "iopub.status.busy": "2026-05-19T20:29:11.343731Z", "iopub.status.idle": "2026-05-19T20:29:11.356653Z", "shell.execute_reply": "2026-05-19T20:29:11.356061Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussModel.h\"\n", "#include \"RooDecay.h\"\n", "#include \"RooBMixDecay.h\"\n", "#include \"RooCategory.h\"\n", "#include \"RooBinning.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": "e1c9ac6c", "metadata": {}, "source": [ "Setup model\n", "---------------------" ] }, { "cell_type": "markdown", "id": "da284b56", "metadata": {}, "source": [ "Build a B decay pdf with mixing" ] }, { "cell_type": "code", "execution_count": 2, "id": "2bcaa774", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:11.357903Z", "iopub.status.busy": "2026-05-19T20:29:11.357788Z", "iopub.status.idle": "2026-05-19T20:29:11.711137Z", "shell.execute_reply": "2026-05-19T20:29:11.710323Z" } }, "outputs": [], "source": [ "RooRealVar dt(\"dt\", \"dt\", -20, 20);\n", "RooRealVar dm(\"dm\", \"dm\", 0.472);\n", "RooRealVar tau(\"tau\", \"tau\", 1.547);\n", "RooRealVar w(\"w\", \"mistag rate\", 0.1);\n", "RooRealVar dw(\"dw\", \"delta mistag rate\", 0.);\n", "\n", "RooCategory mixState(\"mixState\", \"B0/B0bar mixing state\");\n", "mixState.defineType(\"mixed\", -1);\n", "mixState.defineType(\"unmixed\", 1);\n", "RooCategory tagFlav(\"tagFlav\", \"Flavour of the tagged B0\");\n", "tagFlav.defineType(\"B0\", 1);\n", "tagFlav.defineType(\"B0bar\", -1);" ] }, { "cell_type": "markdown", "id": "dae89a6d", "metadata": {}, "source": [ "Build a gaussian resolution model" ] }, { "cell_type": "code", "execution_count": 3, "id": "28319b83", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:11.712877Z", "iopub.status.busy": "2026-05-19T20:29:11.712761Z", "iopub.status.idle": "2026-05-19T20:29:11.920295Z", "shell.execute_reply": "2026-05-19T20:29:11.919623Z" } }, "outputs": [], "source": [ "RooRealVar bias1(\"bias1\", \"bias1\", 0);\n", "RooRealVar sigma1(\"sigma1\", \"sigma1\", 0.1);\n", "RooGaussModel gm1(\"gm1\", \"gauss model 1\", dt, bias1, sigma1);" ] }, { "cell_type": "markdown", "id": "85d0ae3e", "metadata": {}, "source": [ "Construct Bdecay (x) gauss" ] }, { "cell_type": "code", "execution_count": 4, "id": "bc94858b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:11.922363Z", "iopub.status.busy": "2026-05-19T20:29:11.922246Z", "iopub.status.idle": "2026-05-19T20:29:12.129713Z", "shell.execute_reply": "2026-05-19T20:29:12.129123Z" } }, "outputs": [], "source": [ "RooBMixDecay bmix(\"bmix\", \"decay\", dt, mixState, tagFlav, tau, dm, w, dw, gm1, RooBMixDecay::DoubleSided);" ] }, { "cell_type": "markdown", "id": "3afd690e", "metadata": {}, "source": [ "Sample data from model\n", "--------------------------------------------" ] }, { "cell_type": "markdown", "id": "fbaf058a", "metadata": {}, "source": [ "Sample 2000 events in (dt,mixState,tagFlav) from bmix" ] }, { "cell_type": "code", "execution_count": 5, "id": "e8a010e3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:12.131893Z", "iopub.status.busy": "2026-05-19T20:29:12.131775Z", "iopub.status.idle": "2026-05-19T20:29:12.339580Z", "shell.execute_reply": "2026-05-19T20:29:12.338963Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_64: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{bmix.generate(RooArgSet(dt, mixState, tagFlav), 2000)};\n", " ^\n" ] } ], "source": [ "std::unique_ptr data{bmix.generate(RooArgSet(dt, mixState, tagFlav), 2000)};" ] }, { "cell_type": "markdown", "id": "4e12b20f", "metadata": {}, "source": [ "Show dt distribution with custom binning\n", "-------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "f3e4105c", "metadata": {}, "source": [ "Make plot of dt distribution of data in range (-15,15) with fine binning for dt>0 and coarse binning for dt<0" ] }, { "cell_type": "markdown", "id": "0fafdb65", "metadata": {}, "source": [ "Create binning object with range (-15,15)" ] }, { "cell_type": "code", "execution_count": 6, "id": "1a319dea", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:12.341449Z", "iopub.status.busy": "2026-05-19T20:29:12.341333Z", "iopub.status.idle": "2026-05-19T20:29:12.549038Z", "shell.execute_reply": "2026-05-19T20:29:12.548297Z" } }, "outputs": [], "source": [ "RooBinning tbins(-15, 15);" ] }, { "cell_type": "markdown", "id": "1dc26fdf", "metadata": {}, "source": [ "Add 60 bins with uniform spacing in range (-15,0)" ] }, { "cell_type": "code", "execution_count": 7, "id": "872b09df", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:12.550979Z", "iopub.status.busy": "2026-05-19T20:29:12.550863Z", "iopub.status.idle": "2026-05-19T20:29:12.758470Z", "shell.execute_reply": "2026-05-19T20:29:12.757736Z" } }, "outputs": [], "source": [ "tbins.addUniform(60, -15, 0);" ] }, { "cell_type": "markdown", "id": "87bb5dc0", "metadata": {}, "source": [ "Add 15 bins with uniform spacing in range (0,15)" ] }, { "cell_type": "code", "execution_count": 8, "id": "06480f75", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:12.760353Z", "iopub.status.busy": "2026-05-19T20:29:12.760239Z", "iopub.status.idle": "2026-05-19T20:29:12.968051Z", "shell.execute_reply": "2026-05-19T20:29:12.967331Z" } }, "outputs": [], "source": [ "tbins.addUniform(15, 0, 15);" ] }, { "cell_type": "markdown", "id": "8d8e8a85", "metadata": {}, "source": [ "Make plot with specified binning" ] }, { "cell_type": "code", "execution_count": 9, "id": "2615767d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:12.969956Z", "iopub.status.busy": "2026-05-19T20:29:12.969841Z", "iopub.status.idle": "2026-05-19T20:29:13.178346Z", "shell.execute_reply": "2026-05-19T20:29:13.177595Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_68:3:1: error: reference to 'data' is ambiguous\n", "data->plotOn(dtframe, Binning(tbins));\n", "^\n", "input_line_64:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{bmix.generate(RooArgSet(dt, mixState, tagFlav), 2000)};\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 *dtframe = dt.frame(Range(-15, 15), Title(\"dt distribution with custom binning\"));\n", "data->plotOn(dtframe, Binning(tbins));\n", "bmix.plotOn(dtframe);" ] }, { "cell_type": "markdown", "id": "50744ea8", "metadata": {}, "source": [ "NB: Note that bin density for each bin is adjusted to that of default frame binning as shown\n", "in Y axis label (100 bins --> Events/0.4*Xaxis-dim) so that all bins represent a consistent density distribution" ] }, { "cell_type": "markdown", "id": "e007b368", "metadata": {}, "source": [ "Show mixstate asymmetry with custom binning\n", "------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "6c6c89af", "metadata": {}, "source": [ "Make plot of dt distribution of data asymmetry in 'mixState' with variable binning" ] }, { "cell_type": "markdown", "id": "81cfe7aa", "metadata": {}, "source": [ "Create binning object with range (-10,10)" ] }, { "cell_type": "code", "execution_count": 10, "id": "f72fdfe9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:13.179942Z", "iopub.status.busy": "2026-05-19T20:29:13.179828Z", "iopub.status.idle": "2026-05-19T20:29:13.387653Z", "shell.execute_reply": "2026-05-19T20:29:13.386845Z" } }, "outputs": [], "source": [ "RooBinning abins(-10, 10);" ] }, { "cell_type": "markdown", "id": "efbdaf10", "metadata": {}, "source": [ "Add boundaries at 0, (-1,1), (-2,2), (-3,3), (-4,4) and (-6,6)" ] }, { "cell_type": "code", "execution_count": 11, "id": "5cda331c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:13.389292Z", "iopub.status.busy": "2026-05-19T20:29:13.389170Z", "iopub.status.idle": "2026-05-19T20:29:13.596777Z", "shell.execute_reply": "2026-05-19T20:29:13.596046Z" } }, "outputs": [], "source": [ "abins.addBoundary(0);\n", "abins.addBoundaryPair(1);\n", "abins.addBoundaryPair(2);\n", "abins.addBoundaryPair(3);\n", "abins.addBoundaryPair(4);\n", "abins.addBoundaryPair(6);" ] }, { "cell_type": "markdown", "id": "0f7f9119", "metadata": {}, "source": [ "Create plot frame in dt" ] }, { "cell_type": "code", "execution_count": 12, "id": "3e246d19", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:13.606318Z", "iopub.status.busy": "2026-05-19T20:29:13.606195Z", "iopub.status.idle": "2026-05-19T20:29:13.811506Z", "shell.execute_reply": "2026-05-19T20:29:13.810808Z" } }, "outputs": [], "source": [ "RooPlot *aframe = dt.frame(Range(-10, 10), Title(\"mixState asymmetry distribution with custom binning\"));" ] }, { "cell_type": "markdown", "id": "26622de7", "metadata": {}, "source": [ "Plot mixState asymmetry of data with specified custom binning" ] }, { "cell_type": "code", "execution_count": 13, "id": "f91be7e2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:13.813393Z", "iopub.status.busy": "2026-05-19T20:29:13.813276Z", "iopub.status.idle": "2026-05-19T20:29:14.021771Z", "shell.execute_reply": "2026-05-19T20:29:14.021141Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_72:2:2: error: reference to 'data' is ambiguous\n", " data->plotOn(aframe, Asymmetry(mixState), Binning(abins));\n", " ^\n", "input_line_64:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{bmix.generate(RooArgSet(dt, mixState, tagFlav), 2000)};\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": [ "data->plotOn(aframe, Asymmetry(mixState), Binning(abins));" ] }, { "cell_type": "markdown", "id": "a068ab5c", "metadata": {}, "source": [ "Plot corresponding property of pdf" ] }, { "cell_type": "code", "execution_count": 14, "id": "84316005", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:14.023239Z", "iopub.status.busy": "2026-05-19T20:29:14.023124Z", "iopub.status.idle": "2026-05-19T20:29:14.230480Z", "shell.execute_reply": "2026-05-19T20:29:14.230044Z" } }, "outputs": [], "source": [ "bmix.plotOn(aframe, Asymmetry(mixState));" ] }, { "cell_type": "markdown", "id": "0904589b", "metadata": {}, "source": [ "Adjust vertical range of plot to sensible values for an asymmetry" ] }, { "cell_type": "code", "execution_count": 15, "id": "3aa8b066", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:14.246410Z", "iopub.status.busy": "2026-05-19T20:29:14.246268Z", "iopub.status.idle": "2026-05-19T20:29:14.453935Z", "shell.execute_reply": "2026-05-19T20:29:14.453232Z" } }, "outputs": [], "source": [ "aframe->SetMinimum(-1.1);\n", "aframe->SetMaximum(1.1);" ] }, { "cell_type": "markdown", "id": "8253c857", "metadata": {}, "source": [ "NB: For asymmetry distributions no density corrects are needed (and are thus not applied)" ] }, { "cell_type": "markdown", "id": "ac010da5", "metadata": {}, "source": [ "Draw plots on canvas" ] }, { "cell_type": "code", "execution_count": 16, "id": "e144741c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:14.455784Z", "iopub.status.busy": "2026-05-19T20:29:14.455666Z", "iopub.status.idle": "2026-05-19T20:29:14.663674Z", "shell.execute_reply": "2026-05-19T20:29:14.663025Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_82:2:3: error: use of undeclared identifier 'dtframe'\n", " (dtframe->GetYaxis()->SetTitleOffset(1.6000000000000001))\n", " ^\n", "Error in : Error evaluating expression (dtframe->GetYaxis()->SetTitleOffset(1.6000000000000001))\n", "Execution of your code was aborted.\n" ] } ], "source": [ "TCanvas *c = new TCanvas(\"rf108_plotbinning\", \"rf108_plotbinning\", 800, 400);\n", "c->Divide(2);\n", "c->cd(1);\n", "gPad->SetLeftMargin(0.15);\n", "dtframe->GetYaxis()->SetTitleOffset(1.6);\n", "dtframe->Draw();\n", "c->cd(2);\n", "gPad->SetLeftMargin(0.15);\n", "aframe->GetYaxis()->SetTitleOffset(1.6);\n", "aframe->Draw();" ] }, { "cell_type": "markdown", "id": "9ca8635c", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 17, "id": "1676b606", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:14.665435Z", "iopub.status.busy": "2026-05-19T20:29:14.665317Z", "iopub.status.idle": "2026-05-19T20:29:14.892963Z", "shell.execute_reply": "2026-05-19T20:29:14.892347Z" } }, "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 }