{ "cells": [ { "cell_type": "markdown", "id": "6218fd99", "metadata": {}, "source": [ "# rf508_listsetmanip\n", "Organization and simultaneous fits: RooArgSet and RooArgList tools and tricks\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:32 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "a84a663c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:26.012870Z", "iopub.status.busy": "2026-05-19T20:32:26.012757Z", "iopub.status.idle": "2026-05-19T20:32:26.027195Z", "shell.execute_reply": "2026-05-19T20:32:26.026757Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"RooPlot.h\"\n", "#include \"RooArgSet.h\"\n", "#include \"RooArgList.h\"\n", "#include \"RooCategory.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "d69c62bb", "metadata": {}, "source": [ "Create dummy objects\n", "---------------------------------------" ] }, { "cell_type": "markdown", "id": "67728efa", "metadata": {}, "source": [ "Create some variables" ] }, { "cell_type": "code", "execution_count": 2, "id": "5a4d45d8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:26.028594Z", "iopub.status.busy": "2026-05-19T20:32:26.028480Z", "iopub.status.idle": "2026-05-19T20:32:26.353763Z", "shell.execute_reply": "2026-05-19T20:32:26.353163Z" } }, "outputs": [], "source": [ "RooRealVar a(\"a\", \"a\", 1, -10, 10);\n", "RooRealVar b(\"b\", \"b\", 2, -10, 10);\n", "RooRealVar c(\"c\", \"c\", 3, -10, 10);\n", "RooRealVar d(\"d\", \"d\", 4, -10, 10);\n", "RooRealVar x(\"x\", \"x\", 0, -10, 10);\n", "c.setError(0.5);\n", "a.setConstant();\n", "b.setConstant();" ] }, { "cell_type": "markdown", "id": "40e253f1", "metadata": {}, "source": [ "Create a category" ] }, { "cell_type": "code", "execution_count": 3, "id": "9dfa77c5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:26.355722Z", "iopub.status.busy": "2026-05-19T20:32:26.355576Z", "iopub.status.idle": "2026-05-19T20:32:26.561198Z", "shell.execute_reply": "2026-05-19T20:32:26.560587Z" } }, "outputs": [], "source": [ "RooCategory e(\"e\", \"e\");\n", "e.defineType(\"sig\");\n", "e.defineType(\"bkg\");" ] }, { "cell_type": "markdown", "id": "2393945f", "metadata": {}, "source": [ "Create a pdf" ] }, { "cell_type": "code", "execution_count": 4, "id": "9ec8d909", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:26.563169Z", "iopub.status.busy": "2026-05-19T20:32:26.563030Z", "iopub.status.idle": "2026-05-19T20:32:26.771968Z", "shell.execute_reply": "2026-05-19T20:32:26.771492Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] WARNING:InputArguments -- The parameter 'b' with range [-10, 10] of the RooGaussian 'g' exceeds the safe range of (0, inf). Advise to limit its range.\n" ] } ], "source": [ "RooGaussian g(\"g\", \"g\", x, a, b);" ] }, { "cell_type": "markdown", "id": "96716b85", "metadata": {}, "source": [ "Creating, filling RooArgSets\n", "-------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "7f5563c9", "metadata": {}, "source": [ "A RooArgSet is a set of RooAbsArg objects. Each object in the set must have\n", "a unique name" ] }, { "cell_type": "markdown", "id": "74ceecf4", "metadata": {}, "source": [ "Set constructors exists with up to 9 initial arguments" ] }, { "cell_type": "code", "execution_count": 5, "id": "3dd72a12", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:26.773574Z", "iopub.status.busy": "2026-05-19T20:32:26.773438Z", "iopub.status.idle": "2026-05-19T20:32:26.982235Z", "shell.execute_reply": "2026-05-19T20:32:26.981676Z" } }, "outputs": [], "source": [ "RooArgSet s(a, b);" ] }, { "cell_type": "markdown", "id": "c8009d77", "metadata": {}, "source": [ "At any time objects can be added with add()" ] }, { "cell_type": "code", "execution_count": 6, "id": "6578ddda", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:26.984100Z", "iopub.status.busy": "2026-05-19T20:32:26.983980Z", "iopub.status.idle": "2026-05-19T20:32:27.192199Z", "shell.execute_reply": "2026-05-19T20:32:27.191615Z" } }, "outputs": [], "source": [ "s.add(e);" ] }, { "cell_type": "markdown", "id": "011b1640", "metadata": {}, "source": [ "Add up to 9 additional arguments in one call" ] }, { "cell_type": "code", "execution_count": 7, "id": "8c062aac", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:27.194225Z", "iopub.status.busy": "2026-05-19T20:32:27.194106Z", "iopub.status.idle": "2026-05-19T20:32:27.402435Z", "shell.execute_reply": "2026-05-19T20:32:27.401914Z" } }, "outputs": [], "source": [ "s.add(RooArgSet(c, d));" ] }, { "cell_type": "markdown", "id": "bcde3f21", "metadata": {}, "source": [ "Sets can contain any type of RooAbsArg, also pdf and functions" ] }, { "cell_type": "code", "execution_count": 8, "id": "b9a0d0b1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:27.404418Z", "iopub.status.busy": "2026-05-19T20:32:27.404291Z", "iopub.status.idle": "2026-05-19T20:32:27.612756Z", "shell.execute_reply": "2026-05-19T20:32:27.612181Z" } }, "outputs": [], "source": [ "s.add(g);" ] }, { "cell_type": "markdown", "id": "f5d709c7", "metadata": {}, "source": [ "Remove element d" ] }, { "cell_type": "code", "execution_count": 9, "id": "3aac7f69", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:27.614512Z", "iopub.status.busy": "2026-05-19T20:32:27.614379Z", "iopub.status.idle": "2026-05-19T20:32:27.822792Z", "shell.execute_reply": "2026-05-19T20:32:27.822224Z" } }, "outputs": [], "source": [ "s.remove(d);" ] }, { "cell_type": "markdown", "id": "9c3f05d7", "metadata": {}, "source": [ "Accessing RooArgSet contents\n", "-------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "fe141c26", "metadata": {}, "source": [ "You can look up objects by name" ] }, { "cell_type": "code", "execution_count": 10, "id": "c7b21428", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:27.824562Z", "iopub.status.busy": "2026-05-19T20:32:27.824442Z", "iopub.status.idle": "2026-05-19T20:32:28.036297Z", "shell.execute_reply": "2026-05-19T20:32:28.035694Z" } }, "outputs": [], "source": [ "RooAbsArg *aptr = s.find(\"a\");" ] }, { "cell_type": "markdown", "id": "05f2f396", "metadata": {}, "source": [ "Construct a subset by name" ] }, { "cell_type": "code", "execution_count": 11, "id": "f5e919d1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:28.038020Z", "iopub.status.busy": "2026-05-19T20:32:28.037875Z", "iopub.status.idle": "2026-05-19T20:32:28.244173Z", "shell.execute_reply": "2026-05-19T20:32:28.243698Z" } }, "outputs": [], "source": [ "RooArgSet *subset1 = (RooArgSet *)s.selectByName(\"a,b,c\");" ] }, { "cell_type": "markdown", "id": "aeede4c6", "metadata": {}, "source": [ "Construct asubset by attribute" ] }, { "cell_type": "code", "execution_count": 12, "id": "75fa3888", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:28.245804Z", "iopub.status.busy": "2026-05-19T20:32:28.245676Z", "iopub.status.idle": "2026-05-19T20:32:28.455167Z", "shell.execute_reply": "2026-05-19T20:32:28.454680Z" } }, "outputs": [], "source": [ "RooArgSet *subset2 = (RooArgSet *)s.selectByAttrib(\"Constant\", true);" ] }, { "cell_type": "markdown", "id": "222fa000", "metadata": {}, "source": [ "Construct the subset of overlapping contents with another set" ] }, { "cell_type": "code", "execution_count": 13, "id": "f8cf1c9f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:28.466037Z", "iopub.status.busy": "2026-05-19T20:32:28.465897Z", "iopub.status.idle": "2026-05-19T20:32:28.674549Z", "shell.execute_reply": "2026-05-19T20:32:28.674113Z" } }, "outputs": [], "source": [ "RooArgSet s1(a, b, c);\n", "RooArgSet s2(c, d, e);\n", "RooArgSet *subset3 = (RooArgSet *)s1.selectCommon(s2);" ] }, { "cell_type": "markdown", "id": "841e3440", "metadata": {}, "source": [ "Owning RooArgSets\n", "---------------------------------" ] }, { "cell_type": "markdown", "id": "d74f08f8", "metadata": {}, "source": [ "Create a RooArgSet that owns its components\n", "A set either owns all of its components or none,\n", "so once addOwned() is used, add() can no longer be\n", "used and will result in an error message" ] }, { "cell_type": "code", "execution_count": 14, "id": "65d4b6d3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:28.684362Z", "iopub.status.busy": "2026-05-19T20:32:28.684223Z", "iopub.status.idle": "2026-05-19T20:32:28.892599Z", "shell.execute_reply": "2026-05-19T20:32:28.891968Z" } }, "outputs": [], "source": [ "RooRealVar *ac = (RooRealVar *)a.clone(\"a\");\n", "RooRealVar *bc = (RooRealVar *)b.clone(\"b\");\n", "RooRealVar *cc = (RooRealVar *)c.clone(\"c\");\n", "\n", "RooArgSet s3;\n", "s3.addOwned(RooArgSet(*ac, *bc, *cc));" ] }, { "cell_type": "markdown", "id": "58c5317d", "metadata": {}, "source": [ "Another possibility is to add an owned clone\n", "of an object instead of the original" ] }, { "cell_type": "code", "execution_count": 15, "id": "0efa29f8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:28.894662Z", "iopub.status.busy": "2026-05-19T20:32:28.894527Z", "iopub.status.idle": "2026-05-19T20:32:29.102849Z", "shell.execute_reply": "2026-05-19T20:32:29.102154Z" } }, "outputs": [], "source": [ "s3.addClone(RooArgSet(d, e, g));" ] }, { "cell_type": "markdown", "id": "8c168167", "metadata": {}, "source": [ "A clone of a owning set is non-owning and its\n", "contents is owned by the originating owning set" ] }, { "cell_type": "code", "execution_count": 16, "id": "e5ed8c79", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:29.104973Z", "iopub.status.busy": "2026-05-19T20:32:29.104858Z", "iopub.status.idle": "2026-05-19T20:32:29.327009Z", "shell.execute_reply": "2026-05-19T20:32:29.323374Z" } }, "outputs": [], "source": [ "RooArgSet *sclone = (RooArgSet *)s3.Clone(\"sclone\");" ] }, { "cell_type": "markdown", "id": "e15062f7", "metadata": {}, "source": [ "To make a clone of a set and its contents use\n", "the snapshot method" ] }, { "cell_type": "code", "execution_count": 17, "id": "95486954", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:29.328740Z", "iopub.status.busy": "2026-05-19T20:32:29.328601Z", "iopub.status.idle": "2026-05-19T20:32:29.537253Z", "shell.execute_reply": "2026-05-19T20:32:29.536470Z" } }, "outputs": [], "source": [ "RooArgSet *sclone2 = (RooArgSet *)s3.snapshot();" ] }, { "cell_type": "markdown", "id": "a9ddbb01", "metadata": {}, "source": [ "If a set contains function objects, only the head node\n", "is cloned in a snapshot. To make a snapshot of all\n", "servers of a function object do as follows. The result\n", "of a RooArgSet snapshot with deepCloning option is a set\n", "of cloned objects, and all their clone (recursive) server\n", "dependencies, that together form a self-consistent\n", "set that is free of external dependencies" ] }, { "cell_type": "code", "execution_count": 18, "id": "9333c5af", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:29.538901Z", "iopub.status.busy": "2026-05-19T20:32:29.538783Z", "iopub.status.idle": "2026-05-19T20:32:29.747460Z", "shell.execute_reply": "2026-05-19T20:32:29.746703Z" } }, "outputs": [], "source": [ "RooArgSet *sclone3 = (RooArgSet *)s3.snapshot(true);" ] }, { "cell_type": "markdown", "id": "3e1752a0", "metadata": {}, "source": [ "Set printing\n", "------------------------" ] }, { "cell_type": "markdown", "id": "104f7505", "metadata": {}, "source": [ "Inline printing only show list of names of contained objects" ] }, { "cell_type": "code", "execution_count": 19, "id": "615cabfb", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:29.749260Z", "iopub.status.busy": "2026-05-19T20:32:29.749138Z", "iopub.status.idle": "2026-05-19T20:32:29.957428Z", "shell.execute_reply": "2026-05-19T20:32:29.957057Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sclone = (a,b,c,d,e,g)\n" ] } ], "source": [ "cout << \"sclone = \" << (*sclone) << endl;" ] }, { "cell_type": "markdown", "id": "b666b6b2", "metadata": {}, "source": [ "Plain print shows the same, prefixed by name of the set" ] }, { "cell_type": "code", "execution_count": 20, "id": "c75e2903", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:29.965390Z", "iopub.status.busy": "2026-05-19T20:32:29.965239Z", "iopub.status.idle": "2026-05-19T20:32:30.173956Z", "shell.execute_reply": "2026-05-19T20:32:30.173361Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "RooArgSet::sclone = (a,b,c,d,e,g)\n" ] } ], "source": [ "sclone->Print();" ] }, { "cell_type": "markdown", "id": "21da0171", "metadata": {}, "source": [ "Standard printing shows one line for each item with the items name, class name and value" ] }, { "cell_type": "code", "execution_count": 21, "id": "05a46383", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:30.180810Z", "iopub.status.busy": "2026-05-19T20:32:30.180677Z", "iopub.status.idle": "2026-05-19T20:32:30.386591Z", "shell.execute_reply": "2026-05-19T20:32:30.385981Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1) RooRealVar:: a = 1\n", " 2) RooRealVar:: b = 2\n", " 3) RooRealVar:: c = 3 +/- 0.5\n", " 4) RooRealVar:: d = 4\n", " 5) RooCategory:: e = sig(idx = 0)\n", "\n", " 6) RooGaussian:: g = 0.882497\n" ] } ], "source": [ "sclone->Print(\"s\");" ] }, { "cell_type": "markdown", "id": "00a515b5", "metadata": {}, "source": [ "Verbose printing adds each items arguments, address and 'extras' as defined by the object" ] }, { "cell_type": "code", "execution_count": 22, "id": "4ef2ac51", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:30.388458Z", "iopub.status.busy": "2026-05-19T20:32:30.388337Z", "iopub.status.idle": "2026-05-19T20:32:30.595204Z", "shell.execute_reply": "2026-05-19T20:32:30.594563Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1) 0x7fb5f1717c00 RooRealVar:: a = 1 C L(-10 - 10) \"a\"\n", " 2) 0x7fb5f1742fb0 RooRealVar:: b = 2 C L(-10 - 10) \"b\"\n", " 3) 0x7fb5f168e030 RooRealVar:: c = 3 +/- 0.5 L(-10 - 10) \"c\"\n", " 4) 0x7fb5f1612710 RooRealVar:: d = 4 L(-10 - 10) \"d\"\n", " 5) 0x7fb5f172a600 RooCategory:: e = sig(idx = 0)\n", " \"e\"\n", " 6) 0x7fb5f17bc460 RooGaussian:: g[ x=x mean=a sigma=b ] = 0.882497 \"g\"\n" ] } ], "source": [ "sclone->Print(\"v\");" ] }, { "cell_type": "markdown", "id": "d14c34de", "metadata": {}, "source": [ "Using RooArgLists\n", "---------------------------------" ] }, { "cell_type": "markdown", "id": "f31cfad7", "metadata": {}, "source": [ "List constructors exists with up to 9 initial arguments" ] }, { "cell_type": "code", "execution_count": 23, "id": "139f1fc1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:30.597116Z", "iopub.status.busy": "2026-05-19T20:32:30.596996Z", "iopub.status.idle": "2026-05-19T20:32:30.807004Z", "shell.execute_reply": "2026-05-19T20:32:30.806277Z" } }, "outputs": [], "source": [ "RooArgList l(a, b, c, d);" ] }, { "cell_type": "markdown", "id": "261d3bc4", "metadata": {}, "source": [ "Lists have an explicit order and allow multiple arguments with the same name" ] }, { "cell_type": "code", "execution_count": 24, "id": "8c1c6e48", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:30.809302Z", "iopub.status.busy": "2026-05-19T20:32:30.809181Z", "iopub.status.idle": "2026-05-19T20:32:31.017592Z", "shell.execute_reply": "2026-05-19T20:32:31.016857Z" } }, "outputs": [], "source": [ "l.add(RooArgList(a, b, c, d));" ] }, { "cell_type": "markdown", "id": "63115236", "metadata": {}, "source": [ "Access by index is provided" ] }, { "cell_type": "code", "execution_count": 25, "id": "f90d9f69", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:31.019714Z", "iopub.status.busy": "2026-05-19T20:32:31.019575Z", "iopub.status.idle": "2026-05-19T20:32:31.227904Z", "shell.execute_reply": "2026-05-19T20:32:31.227209Z" } }, "outputs": [], "source": [ "RooAbsArg *arg4 = l.at(4);" ] } ], "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 }