{ "cells": [ { "cell_type": "markdown", "id": "2d156aaa", "metadata": {}, "source": [ "# rf111_derivatives\n", "Basic functionality: numerical 1st,2nd and 3rd order derivatives w.r.t. observables and parameters\n", "\n", "```\n", " pdf = gauss(x,m,s)\n", "```\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": "499457a0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:26.090692Z", "iopub.status.busy": "2026-05-19T20:29:26.090563Z", "iopub.status.idle": "2026-05-19T20:29:26.104292Z", "shell.execute_reply": "2026-05-19T20:29:26.103653Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooDerivative.h\"\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooPlot.h\"\n", "\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "1d33ac62", "metadata": {}, "source": [ "Setup model\n", "---------------------" ] }, { "cell_type": "markdown", "id": "54f8dafa", "metadata": {}, "source": [ "Declare variables x,mean,sigma with associated name, title, initial value and allowed range" ] }, { "cell_type": "code", "execution_count": 2, "id": "0080c044", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:26.105890Z", "iopub.status.busy": "2026-05-19T20:29:26.105765Z", "iopub.status.idle": "2026-05-19T20:29:26.420537Z", "shell.execute_reply": "2026-05-19T20:29:26.419797Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", -10, 10);\n", "RooRealVar mean(\"mean\", \"mean of gaussian\", 1, -10, 10);\n", "RooRealVar sigma(\"sigma\", \"width of gaussian\", 1, 0.1, 10);" ] }, { "cell_type": "markdown", "id": "09b39a54", "metadata": {}, "source": [ "Build gaussian pdf in terms of x,mean and sigma" ] }, { "cell_type": "code", "execution_count": 3, "id": "8cab8f63", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:26.422457Z", "iopub.status.busy": "2026-05-19T20:29:26.422334Z", "iopub.status.idle": "2026-05-19T20:29:26.630193Z", "shell.execute_reply": "2026-05-19T20:29:26.629397Z" } }, "outputs": [], "source": [ "RooGaussian gauss(\"gauss\", \"gaussian PDF\", x, mean, sigma);" ] }, { "cell_type": "markdown", "id": "e7034356", "metadata": {}, "source": [ "Create and plot derivatives w.r.t. x\n", "----------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "3788f43e", "metadata": {}, "source": [ "Derivative of normalized gauss(x) w.r.t. observable x" ] }, { "cell_type": "code", "execution_count": 4, "id": "0c661bdd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:26.635782Z", "iopub.status.busy": "2026-05-19T20:29:26.635655Z", "iopub.status.idle": "2026-05-19T20:29:26.843409Z", "shell.execute_reply": "2026-05-19T20:29:26.842669Z" } }, "outputs": [], "source": [ "RooAbsReal *dgdx = gauss.derivative(x, 1);" ] }, { "cell_type": "markdown", "id": "acbd88df", "metadata": {}, "source": [ "Second and third derivative of normalized gauss(x) w.r.t. observable x" ] }, { "cell_type": "code", "execution_count": 5, "id": "0f1f96fd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:26.845387Z", "iopub.status.busy": "2026-05-19T20:29:26.845265Z", "iopub.status.idle": "2026-05-19T20:29:27.052757Z", "shell.execute_reply": "2026-05-19T20:29:27.052114Z" } }, "outputs": [], "source": [ "RooAbsReal *d2gdx2 = gauss.derivative(x, 2);\n", "RooAbsReal *d3gdx3 = gauss.derivative(x, 3);" ] }, { "cell_type": "markdown", "id": "47367af0", "metadata": {}, "source": [ "Construct plot frame in 'x'" ] }, { "cell_type": "code", "execution_count": 6, "id": "e95aca6b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:27.054893Z", "iopub.status.busy": "2026-05-19T20:29:27.054769Z", "iopub.status.idle": "2026-05-19T20:29:27.257002Z", "shell.execute_reply": "2026-05-19T20:29:27.256426Z" } }, "outputs": [], "source": [ "RooPlot *xframe = x.frame(Title(\"d(Gauss)/dx\"));" ] }, { "cell_type": "markdown", "id": "3f83fe26", "metadata": {}, "source": [ "Plot gauss in frame (i.e. in x)" ] }, { "cell_type": "code", "execution_count": 7, "id": "d287493b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:27.259144Z", "iopub.status.busy": "2026-05-19T20:29:27.259030Z", "iopub.status.idle": "2026-05-19T20:29:27.466409Z", "shell.execute_reply": "2026-05-19T20:29:27.465886Z" } }, "outputs": [], "source": [ "gauss.plotOn(xframe);" ] }, { "cell_type": "markdown", "id": "8d4733f0", "metadata": {}, "source": [ "Plot derivatives in same frame" ] }, { "cell_type": "code", "execution_count": 8, "id": "56a6787f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:27.468450Z", "iopub.status.busy": "2026-05-19T20:29:27.468338Z", "iopub.status.idle": "2026-05-19T20:29:27.675839Z", "shell.execute_reply": "2026-05-19T20:29:27.675147Z" } }, "outputs": [], "source": [ "dgdx->plotOn(xframe, LineColor(kMagenta));\n", "d2gdx2->plotOn(xframe, LineColor(kRed));\n", "d3gdx3->plotOn(xframe, LineColor(kOrange));" ] }, { "cell_type": "markdown", "id": "10670e8a", "metadata": {}, "source": [ "Create and plot derivatives w.r.t. sigma\n", "------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "8141ac9a", "metadata": {}, "source": [ "Derivative of normalized gauss(x) w.r.t. parameter sigma" ] }, { "cell_type": "code", "execution_count": 9, "id": "035d8008", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:27.677788Z", "iopub.status.busy": "2026-05-19T20:29:27.677676Z", "iopub.status.idle": "2026-05-19T20:29:27.885137Z", "shell.execute_reply": "2026-05-19T20:29:27.884473Z" } }, "outputs": [], "source": [ "RooAbsReal *dgds = gauss.derivative(sigma, 1);" ] }, { "cell_type": "markdown", "id": "3af65265", "metadata": {}, "source": [ "Second and third derivative of normalized gauss(x) w.r.t. parameter sigma" ] }, { "cell_type": "code", "execution_count": 10, "id": "82059f6a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:27.888360Z", "iopub.status.busy": "2026-05-19T20:29:27.888249Z", "iopub.status.idle": "2026-05-19T20:29:28.095573Z", "shell.execute_reply": "2026-05-19T20:29:28.095011Z" } }, "outputs": [], "source": [ "RooAbsReal *d2gds2 = gauss.derivative(sigma, 2);\n", "RooAbsReal *d3gds3 = gauss.derivative(sigma, 3);" ] }, { "cell_type": "markdown", "id": "6acf917a", "metadata": {}, "source": [ "Construct plot frame in 'sigma'" ] }, { "cell_type": "code", "execution_count": 11, "id": "429f5f59", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:28.097630Z", "iopub.status.busy": "2026-05-19T20:29:28.097493Z", "iopub.status.idle": "2026-05-19T20:29:28.304988Z", "shell.execute_reply": "2026-05-19T20:29:28.304299Z" } }, "outputs": [], "source": [ "RooPlot *sframe = sigma.frame(Title(\"d(Gauss)/d(sigma)\"), Range(0., 2.));" ] }, { "cell_type": "markdown", "id": "5ad7a47b", "metadata": {}, "source": [ "Plot gauss in frame (i.e. in x)" ] }, { "cell_type": "code", "execution_count": 12, "id": "7689de48", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:28.306856Z", "iopub.status.busy": "2026-05-19T20:29:28.306744Z", "iopub.status.idle": "2026-05-19T20:29:28.514434Z", "shell.execute_reply": "2026-05-19T20:29:28.513731Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:NumericIntegration -- RooRealIntegral::init(gauss_Int[sigma]) using numeric integrator RooIntegrator1D to calculate Int(sigma)\n" ] } ], "source": [ "gauss.plotOn(sframe);" ] }, { "cell_type": "markdown", "id": "1139184f", "metadata": {}, "source": [ "Plot derivatives in same frame" ] }, { "cell_type": "code", "execution_count": 13, "id": "c72d9e72", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:28.516131Z", "iopub.status.busy": "2026-05-19T20:29:28.516018Z", "iopub.status.idle": "2026-05-19T20:29:28.723972Z", "shell.execute_reply": "2026-05-19T20:29:28.723509Z" } }, "outputs": [], "source": [ "dgds->plotOn(sframe, LineColor(kMagenta));\n", "d2gds2->plotOn(sframe, LineColor(kRed));\n", "d3gds3->plotOn(sframe, LineColor(kOrange));" ] }, { "cell_type": "markdown", "id": "eb6f9a75", "metadata": {}, "source": [ "Draw all frames on a canvas" ] }, { "cell_type": "code", "execution_count": 14, "id": "824fceac", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:28.725414Z", "iopub.status.busy": "2026-05-19T20:29:28.725307Z", "iopub.status.idle": "2026-05-19T20:29:28.932559Z", "shell.execute_reply": "2026-05-19T20:29:28.932085Z" } }, "outputs": [], "source": [ "TCanvas *c = new TCanvas(\"rf111_derivatives\", \"rf111_derivatives\", 800, 400);\n", "c->Divide(2);\n", "c->cd(1);\n", "gPad->SetLeftMargin(0.15);\n", "xframe->GetYaxis()->SetTitleOffset(1.6);\n", "xframe->Draw();\n", "c->cd(2);\n", "gPad->SetLeftMargin(0.15);\n", "sframe->GetYaxis()->SetTitleOffset(1.6);\n", "sframe->Draw();" ] }, { "cell_type": "markdown", "id": "69bdcdf2", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 15, "id": "cde03116", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:28.934152Z", "iopub.status.busy": "2026-05-19T20:29:28.934045Z", "iopub.status.idle": "2026-05-19T20:29:29.161701Z", "shell.execute_reply": "2026-05-19T20:29:29.161249Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "