{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c0e8049b",
   "metadata": {},
   "source": [
    "# rf613_global_observables\n",
    "This tutorial explains the concept of global observables in RooFit, and\n",
    "showcases how their values can be stored either in the model or in the\n",
    "dataset.\n",
    "\n",
    "# Introduction\n",
    "\n",
    "Note: in this tutorial, we are multiplying the likelihood with an additional\n",
    "likelihood to constrain the parameters with auxiliary measurements. This is\n",
    "different from the `rf604_constraints` tutorial, where the likelihood is\n",
    "multiplied with a Bayesian prior to constrain the parameters.\n",
    "\n",
    "\n",
    "With RooFit, you usually optimize some model parameters `p` to maximize the\n",
    "likelihood `L` given the per-event or per-bin observations `x`:\n",
    "\n",
    "\n",
    "Often, the parameters are constrained with some prior likelihood `C`, which\n",
    "doesn't depend on the observables `x`:\n",
    "\n",
    "\n",
    "Usually, these constraint terms depend on some auxiliary measurements of\n",
    "other observables `g`. The constraint term is then the likelihood of the\n",
    "so-called global observables:\n",
    "\n",
    "\n",
    "For example, think of a model where the true luminosity `lumi` is a\n",
    "nuisance parameter that is constrained by an auxiliary measurement\n",
    "`lumi_obs` with uncertainty `lumi_obs_sigma`:\n",
    "\n",
    "\n",
    "As a Gaussian is symmetric under exchange of the observable and the mean\n",
    "parameter, you can also sometimes find this equivalent but less conventional\n",
    "formulation for Gaussian constraints:\n",
    "\n",
    "\n",
    "If you wanted to constrain a parameter that represents event counts, you\n",
    "would use a Poissonian constraint, e.g.:\n",
    "\n",
    "\n",
    "Unlike a Gaussian, a Poissonian is not symmetric under exchange of the\n",
    "observable and the parameter, so here you need to be more careful to follow\n",
    "the global observable prescription correctly.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Jonas Rembser  \n",
    "<i><small>This notebook tutorial was automatically generated with <a href= \"https://github.com/root-project/root/blob/master/documentation/doxygen/converttonotebook.py\">ROOTBOOK-izer</a> from the macro found in the ROOT repository  on Tuesday, May 19, 2026 at 08:33 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8ac28a4e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:47.158320Z",
     "iopub.status.busy": "2026-05-19T20:33:47.158206Z",
     "iopub.status.idle": "2026-05-19T20:33:48.139299Z",
     "shell.execute_reply": "2026-05-19T20:33:48.138588Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5544414e",
   "metadata": {},
   "source": [
    "Silence info output for this tutorial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c75d4ac",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:48.141143Z",
     "iopub.status.busy": "2026-05-19T20:33:48.141000Z",
     "iopub.status.idle": "2026-05-19T20:33:48.302693Z",
     "shell.execute_reply": "2026-05-19T20:33:48.302130Z"
    }
   },
   "outputs": [],
   "source": [
    "ROOT.RooMsgService.instance().getStream(1).removeTopic(ROOT.RooFit.Minimization)\n",
    "ROOT.RooMsgService.instance().getStream(1).removeTopic(ROOT.RooFit.Fitting)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "093e0d48",
   "metadata": {},
   "source": [
    "Setting up the model and creating toy dataset\n",
    "---------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "413a245d",
   "metadata": {},
   "source": [
    "l'(x | mu, sigma) = l(x | mu, sigma) * Gauss(mu_obs | mu, 0.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd6476cd",
   "metadata": {},
   "source": [
    "event observables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "505965c7",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:48.304351Z",
     "iopub.status.busy": "2026-05-19T20:33:48.304220Z",
     "iopub.status.idle": "2026-05-19T20:33:48.462025Z",
     "shell.execute_reply": "2026-05-19T20:33:48.451378Z"
    }
   },
   "outputs": [],
   "source": [
    "x = ROOT.RooRealVar(\"x\", \"x\", -10, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88ca40ea",
   "metadata": {},
   "source": [
    "parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7e5569f3",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:48.463507Z",
     "iopub.status.busy": "2026-05-19T20:33:48.463365Z",
     "iopub.status.idle": "2026-05-19T20:33:48.573291Z",
     "shell.execute_reply": "2026-05-19T20:33:48.572783Z"
    }
   },
   "outputs": [],
   "source": [
    "mu = ROOT.RooRealVar(\"mu\", \"mu\", 0.0, -10, 10)\n",
    "sigma = ROOT.RooRealVar(\"sigma\", \"sigma\", 1.0, 0.1, 2.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fc76bba",
   "metadata": {},
   "source": [
    "Gaussian model for event observables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4f05f7b8",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:48.574927Z",
     "iopub.status.busy": "2026-05-19T20:33:48.574777Z",
     "iopub.status.idle": "2026-05-19T20:33:48.697638Z",
     "shell.execute_reply": "2026-05-19T20:33:48.697102Z"
    }
   },
   "outputs": [],
   "source": [
    "gauss = ROOT.RooGaussian(\"gauss\", \"gauss\", x, mu, sigma)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2a99079",
   "metadata": {},
   "source": [
    "global observables (which are not parameters so they are constant)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4e7c74a0",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:48.699189Z",
     "iopub.status.busy": "2026-05-19T20:33:48.699059Z",
     "iopub.status.idle": "2026-05-19T20:33:48.808699Z",
     "shell.execute_reply": "2026-05-19T20:33:48.808149Z"
    }
   },
   "outputs": [],
   "source": [
    "mu_obs = ROOT.RooRealVar(\"mu_obs\", \"mu_obs\", 1.0, -10, 10)\n",
    "mu_obs.setConstant()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d382fb55",
   "metadata": {},
   "source": [
    "note: alternatively, one can create a constant with default limits using `RooRealVar(\"mu_obs\", \"mu_obs\", 1.0)`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1736656e",
   "metadata": {},
   "source": [
    "constraint pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9db93333",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:48.810178Z",
     "iopub.status.busy": "2026-05-19T20:33:48.809990Z",
     "iopub.status.idle": "2026-05-19T20:33:48.924199Z",
     "shell.execute_reply": "2026-05-19T20:33:48.923705Z"
    }
   },
   "outputs": [],
   "source": [
    "constraint = ROOT.RooGaussian(\"constraint\", \"constraint\", mu_obs, mu, 0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c0df91f",
   "metadata": {},
   "source": [
    "full pdf including constraint pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3b4cf5e9",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:48.925685Z",
     "iopub.status.busy": "2026-05-19T20:33:48.925537Z",
     "iopub.status.idle": "2026-05-19T20:33:49.106170Z",
     "shell.execute_reply": "2026-05-19T20:33:49.105643Z"
    }
   },
   "outputs": [],
   "source": [
    "model = ROOT.RooProdPdf(\"model\", \"model\", [gauss, constraint])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3364c41",
   "metadata": {},
   "source": [
    "Generating toy data with randomized global observables\n",
    "------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6c5cfb8",
   "metadata": {},
   "source": [
    "For most toy-based statistical procedures, it is necessary to also\n",
    "randomize the global observable when generating toy datasets.\n",
    "\n",
    "To that end, let's generate a single event from the model and take the\n",
    "global observable value (the same is done in the RooStats:ToyMCSampler\n",
    "class):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1a4699ac",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:49.107640Z",
     "iopub.status.busy": "2026-05-19T20:33:49.107500Z",
     "iopub.status.idle": "2026-05-19T20:33:49.237803Z",
     "shell.execute_reply": "2026-05-19T20:33:49.237290Z"
    }
   },
   "outputs": [],
   "source": [
    "dataGlob = model.generate({mu_obs}, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2202a8e6",
   "metadata": {},
   "source": [
    "Next, we temporarily set the value of `mu_obs` to the randomized value for\n",
    "generating our toy dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e2c590d9",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:49.239229Z",
     "iopub.status.busy": "2026-05-19T20:33:49.239104Z",
     "iopub.status.idle": "2026-05-19T20:33:49.355568Z",
     "shell.execute_reply": "2026-05-19T20:33:49.355050Z"
    }
   },
   "outputs": [],
   "source": [
    "mu_obs_orig_val = mu_obs.getVal()\n",
    "\n",
    "ROOT.RooArgSet(mu_obs).assign(dataGlob.get(0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72f447b6",
   "metadata": {},
   "source": [
    "Actually generate the toy dataset. We don't generate too many events,\n",
    "otherwise, the constraint will not have much weight in the fit and the result\n",
    "looks like it's unaffected by it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2983b723",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:49.356944Z",
     "iopub.status.busy": "2026-05-19T20:33:49.356820Z",
     "iopub.status.idle": "2026-05-19T20:33:49.460128Z",
     "shell.execute_reply": "2026-05-19T20:33:49.459637Z"
    }
   },
   "outputs": [],
   "source": [
    "data = model.generate({x}, 50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ceeefe5f",
   "metadata": {},
   "source": [
    "When fitting the toy dataset, it is important to set the global\n",
    "observables in the fit to the values that were used to generate the toy\n",
    "dataset. To facilitate the bookkeeping of global observable values, you\n",
    "can attach a snapshot with the current global observable values to the\n",
    "dataset like this (new feature introduced in ROOT 6.26):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d3d39947",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:49.461415Z",
     "iopub.status.busy": "2026-05-19T20:33:49.461295Z",
     "iopub.status.idle": "2026-05-19T20:33:49.576283Z",
     "shell.execute_reply": "2026-05-19T20:33:49.575834Z"
    }
   },
   "outputs": [],
   "source": [
    "data.setGlobalObservables({mu_obs})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a3913cc",
   "metadata": {},
   "source": [
    "reset original mu_obs value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6bae7c9a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:49.594847Z",
     "iopub.status.busy": "2026-05-19T20:33:49.594691Z",
     "iopub.status.idle": "2026-05-19T20:33:49.702576Z",
     "shell.execute_reply": "2026-05-19T20:33:49.702123Z"
    }
   },
   "outputs": [],
   "source": [
    "mu_obs.setVal(mu_obs_orig_val)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "558788f3",
   "metadata": {},
   "source": [
    "Fitting a model with global observables\n",
    "---------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8449ef6",
   "metadata": {},
   "source": [
    "Create snapshot of original parameters to reset parameters after fitting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "228f6fb1",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:49.704815Z",
     "iopub.status.busy": "2026-05-19T20:33:49.704673Z",
     "iopub.status.idle": "2026-05-19T20:33:49.818920Z",
     "shell.execute_reply": "2026-05-19T20:33:49.818319Z"
    }
   },
   "outputs": [],
   "source": [
    "modelParameters = model.getParameters(data.get())\n",
    "origParameters = modelParameters.snapshot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41f6de0c",
   "metadata": {},
   "source": [
    "When you fit a model that includes global observables, you need to\n",
    "specify them in the call to RooAbsPdf::fitTo with the\n",
    "RooFit::GlobalObservables command argument. By default, the global\n",
    "observable values attached to the dataset will be prioritized over the\n",
    "values in the model, so the following fit correctly uses the randomized\n",
    "global observable values from the toy dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3eb82305",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:49.820888Z",
     "iopub.status.busy": "2026-05-19T20:33:49.820745Z",
     "iopub.status.idle": "2026-05-19T20:33:50.043143Z",
     "shell.execute_reply": "2026-05-19T20:33:50.037610Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. model.fitTo(*data, GlobalObservables(mu_obs))\n",
      "------------------------------------------------\n",
      "\n",
      "  RooFitResult: minimized FCN value: 68.2482, estimated distance to minimum: 9.80327e-07\n",
      "                covariance matrix quality: Full, accurate covariance matrix\n",
      "                Status : MINIMIZE=0 HESSE=0 \n",
      "\n",
      "    Floating Parameter    FinalValue +/-  Error   \n",
      "  --------------------  --------------------------\n",
      "                    mu    5.2717e-02 +/-  8.11e-02\n",
      "                 sigma    9.7190e-01 +/-  9.73e-02\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"1. model.fitTo(*data, GlobalObservables(mu_obs))\")\n",
    "print(\"------------------------------------------------\")\n",
    "model.fitTo(data, GlobalObservables=mu_obs, PrintLevel=-1, Save=True).Print()\n",
    "modelParameters.assign(origParameters)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb44520e",
   "metadata": {},
   "source": [
    "In our example, the set of global observables is attached to the toy\n",
    "dataset. In this case, you can actually drop the GlobalObservables()\n",
    "command argument, because the global observables are automatically\n",
    "figured out from the data set (this fit result should be identical to the\n",
    "previous one)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5b97e54e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:50.044559Z",
     "iopub.status.busy": "2026-05-19T20:33:50.044429Z",
     "iopub.status.idle": "2026-05-19T20:33:50.149695Z",
     "shell.execute_reply": "2026-05-19T20:33:50.149035Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2. model.fitTo(*data)\n",
      "---------------------\n",
      "\n",
      "  RooFitResult: minimized FCN value: 68.2482, estimated distance to minimum: 9.80327e-07\n",
      "                covariance matrix quality: Full, accurate covariance matrix\n",
      "                Status : MINIMIZE=0 HESSE=0 \n",
      "\n",
      "    Floating Parameter    FinalValue +/-  Error   \n",
      "  --------------------  --------------------------\n",
      "                    mu    5.2717e-02 +/-  8.11e-02\n",
      "                 sigma    9.7190e-01 +/-  9.73e-02\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"2. model.fitTo(*data)\")\n",
    "print(\"---------------------\")\n",
    "model.fitTo(data, PrintLevel=-1, Save=True).Print()\n",
    "modelParameters.assign(origParameters)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d430ff7",
   "metadata": {},
   "source": [
    "If you want to explicitly ignore the global observables in the dataset,\n",
    "you can do that by specifying GlobalObservablesSource(\"model\"). Keep in\n",
    "mind that now it's also again your responsibility to define the set of\n",
    "global observables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "da0885af",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:50.151030Z",
     "iopub.status.busy": "2026-05-19T20:33:50.150910Z",
     "iopub.status.idle": "2026-05-19T20:33:50.261325Z",
     "shell.execute_reply": "2026-05-19T20:33:50.260712Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3. model.fitTo(*data, GlobalObservables(mu_obs), GlobalObservablesSource(\"model\"))\n",
      "------------------------------------------------\n",
      "\n",
      "  RooFitResult: minimized FCN value: 83.7181, estimated distance to minimum: 6.67911e-07\n",
      "                covariance matrix quality: Full, accurate covariance matrix\n",
      "                Status : MINIMIZE=0 HESSE=0 \n",
      "\n",
      "    Floating Parameter    FinalValue +/-  Error   \n",
      "  --------------------  --------------------------\n",
      "                    mu    7.4744e-01 +/-  9.68e-02\n",
      "                 sigma    1.2451e+00 +/-  1.38e-01\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print('3. model.fitTo(*data, GlobalObservables(mu_obs), GlobalObservablesSource(\"model\"))')\n",
    "print(\"------------------------------------------------\")\n",
    "model.fitTo(data, GlobalObservables=mu_obs, GlobalObservablesSource=\"model\", PrintLevel=-1, Save=True).Print()\n",
    "modelParameters.assign(origParameters)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ec4eb97",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b481a30b",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:33:50.263214Z",
     "iopub.status.busy": "2026-05-19T20:33:50.263092Z",
     "iopub.status.idle": "2026-05-19T20:33:50.385329Z",
     "shell.execute_reply": "2026-05-19T20:33:50.384706Z"
    }
   },
   "outputs": [],
   "source": [
    "%jsroot on\n",
    "from ROOT import gROOT \n",
    "gROOT.GetListOfCanvases().Draw()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
