{ "cells": [ { "cell_type": "markdown", "id": "07617254", "metadata": {}, "source": [ "# rf502_wspacewrite\n", "Organisation and simultaneous fits: creating and writing a workspace\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:05 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "d351f040", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:51.526963Z", "iopub.status.busy": "2026-05-19T20:05:51.526844Z", "iopub.status.idle": "2026-05-19T20:05:51.570346Z", "shell.execute_reply": "2026-05-19T20:05:51.548455Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooChebychev.h\"\n", "#include \"RooAddPdf.h\"\n", "#include \"RooWorkspace.h\"\n", "#include \"RooPlot.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"TFile.h\"\n", "#include \"TH1.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "d7152339", "metadata": {}, "source": [ "Create model and dataset\n", "-----------------------------------------------" ] }, { "cell_type": "markdown", "id": "973d6ca6", "metadata": {}, "source": [ "Declare observable x" ] }, { "cell_type": "code", "execution_count": 2, "id": "b1699783", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:51.589592Z", "iopub.status.busy": "2026-05-19T20:05:51.589424Z", "iopub.status.idle": "2026-05-19T20:05:51.819492Z", "shell.execute_reply": "2026-05-19T20:05:51.818626Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", 0, 10);" ] }, { "cell_type": "markdown", "id": "907620d6", "metadata": {}, "source": [ "Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters" ] }, { "cell_type": "code", "execution_count": 3, "id": "11990a88", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:51.832496Z", "iopub.status.busy": "2026-05-19T20:05:51.832349Z", "iopub.status.idle": "2026-05-19T20:05:52.036747Z", "shell.execute_reply": "2026-05-19T20:05:52.036390Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] WARNING:InputArguments -- The parameter 'sigma1' with range [-inf, inf] of the RooGaussian 'sig1' exceeds the safe range of (0, inf). Advise to limit its range.\n", "[#0] WARNING:InputArguments -- The parameter 'sigma2' with range [-inf, inf] of the RooGaussian 'sig2' exceeds the safe range of (0, inf). Advise to limit its range.\n" ] } ], "source": [ "RooRealVar mean(\"mean\", \"mean of gaussians\", 5, 0, 10);\n", "RooRealVar sigma1(\"sigma1\", \"width of gaussians\", 0.5);\n", "RooRealVar sigma2(\"sigma2\", \"width of gaussians\", 1);\n", "\n", "RooGaussian sig1(\"sig1\", \"Signal component 1\", x, mean, sigma1);\n", "RooGaussian sig2(\"sig2\", \"Signal component 2\", x, mean, sigma2);" ] }, { "cell_type": "markdown", "id": "e54b8627", "metadata": {}, "source": [ "Build Chebychev polynomial pdf" ] }, { "cell_type": "code", "execution_count": 4, "id": "ba23e913", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:52.046227Z", "iopub.status.busy": "2026-05-19T20:05:52.046040Z", "iopub.status.idle": "2026-05-19T20:05:52.401680Z", "shell.execute_reply": "2026-05-19T20:05:52.381653Z" } }, "outputs": [], "source": [ "RooRealVar a0(\"a0\", \"a0\", 0.5, 0., 1.);\n", "RooRealVar a1(\"a1\", \"a1\", 0.2, 0, 1.);\n", "RooChebychev bkg(\"bkg\", \"Background\", x, RooArgSet(a0, a1));" ] }, { "cell_type": "markdown", "id": "0caa5f77", "metadata": {}, "source": [ "Sum the signal components into a composite signal pdf" ] }, { "cell_type": "code", "execution_count": 5, "id": "c2733f31", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:52.425253Z", "iopub.status.busy": "2026-05-19T20:05:52.425066Z", "iopub.status.idle": "2026-05-19T20:05:52.669811Z", "shell.execute_reply": "2026-05-19T20:05:52.649370Z" } }, "outputs": [], "source": [ "RooRealVar sig1frac(\"sig1frac\", \"fraction of component 1 in signal\", 0.8, 0., 1.);\n", "RooAddPdf sig(\"sig\", \"Signal\", RooArgList(sig1, sig2), sig1frac);" ] }, { "cell_type": "markdown", "id": "05b943a3", "metadata": {}, "source": [ "Sum the composite signal and background" ] }, { "cell_type": "code", "execution_count": 6, "id": "1c250be2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:52.691048Z", "iopub.status.busy": "2026-05-19T20:05:52.690875Z", "iopub.status.idle": "2026-05-19T20:05:52.954691Z", "shell.execute_reply": "2026-05-19T20:05:52.922708Z" } }, "outputs": [], "source": [ "RooRealVar bkgfrac(\"bkgfrac\", \"fraction of background\", 0.5, 0., 1.);\n", "RooAddPdf model(\"model\", \"g1+g2+a\", RooArgList(bkg, sig), bkgfrac);" ] }, { "cell_type": "markdown", "id": "2a70995f", "metadata": {}, "source": [ "Generate a data sample of 1000 events in x from model" ] }, { "cell_type": "code", "execution_count": 7, "id": "9db35e59", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:52.974510Z", "iopub.status.busy": "2026-05-19T20:05:52.974344Z", "iopub.status.idle": "2026-05-19T20:05:53.204987Z", "shell.execute_reply": "2026-05-19T20:05:53.198206Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54: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{model.generate(x, 1000)};\n", " ^\n" ] } ], "source": [ "std::unique_ptr data{model.generate(x, 1000)};" ] }, { "cell_type": "markdown", "id": "d4fc57cc", "metadata": {}, "source": [ "Create workspace, import data and model\n", "-----------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "3ce81c31", "metadata": {}, "source": [ "Create a new empty workspace" ] }, { "cell_type": "code", "execution_count": 8, "id": "63782511", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:53.216402Z", "iopub.status.busy": "2026-05-19T20:05:53.216235Z", "iopub.status.idle": "2026-05-19T20:05:53.446049Z", "shell.execute_reply": "2026-05-19T20:05:53.444653Z" } }, "outputs": [], "source": [ "RooWorkspace *w = new RooWorkspace(\"w\", \"workspace\");" ] }, { "cell_type": "markdown", "id": "411bf129", "metadata": {}, "source": [ "Import model and all its components into the workspace" ] }, { "cell_type": "code", "execution_count": 9, "id": "fc97e2fd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:53.450937Z", "iopub.status.busy": "2026-05-19T20:05:53.450789Z", "iopub.status.idle": "2026-05-19T20:05:53.681082Z", "shell.execute_reply": "2026-05-19T20:05:53.672262Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooAddPdf::model\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooChebychev::bkg\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::x\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::a0\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::a1\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::bkgfrac\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooAddPdf::sig\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooGaussian::sig1\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::mean\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::sigma1\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::sig1frac\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooGaussian::sig2\n", "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::sigma2\n" ] } ], "source": [ "w->import(model);" ] }, { "cell_type": "markdown", "id": "ae2c3984", "metadata": {}, "source": [ "Import data into the workspace" ] }, { "cell_type": "code", "execution_count": 10, "id": "2625c2d7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:53.683499Z", "iopub.status.busy": "2026-05-19T20:05:53.683364Z", "iopub.status.idle": "2026-05-19T20:05:53.922660Z", "shell.execute_reply": "2026-05-19T20:05:53.902047Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_57:2:13: error: reference to 'data' is ambiguous\n", " w->import(*data);\n", " ^\n", "input_line_54:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{model.generate(x, 1000)};\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": [ "w->import(*data);" ] }, { "cell_type": "markdown", "id": "28fddd24", "metadata": {}, "source": [ "Print workspace contents" ] }, { "cell_type": "code", "execution_count": 11, "id": "6f8214f6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:53.938115Z", "iopub.status.busy": "2026-05-19T20:05:53.937948Z", "iopub.status.idle": "2026-05-19T20:05:54.183675Z", "shell.execute_reply": "2026-05-19T20:05:54.169676Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RooWorkspace(w) workspace contents\n", "\n", "variables\n", "---------\n", "(a0,a1,bkgfrac,mean,sig1frac,sigma1,sigma2,x)\n", "\n", "p.d.f.s\n", "-------\n", "RooChebychev::bkg[ x=x coefList=(a0,a1) ] = 0.8\n", "RooAddPdf::model[ bkgfrac * bkg + [%] * sig ] = 0.9/1\n", "RooAddPdf::sig[ sig1frac * sig1 + [%] * sig2 ] = 1/1\n", "RooGaussian::sig1[ x=x mean=mean sigma=sigma1 ] = 1\n", "RooGaussian::sig2[ x=x mean=mean sigma=sigma2 ] = 1\n", "\n" ] } ], "source": [ "w->Print();" ] }, { "cell_type": "markdown", "id": "d81fe61b", "metadata": {}, "source": [ "Save workspace in file\n", "-------------------------------------------" ] }, { "cell_type": "markdown", "id": "b11aabfd", "metadata": {}, "source": [ "Save the workspace into a ROOT file" ] }, { "cell_type": "code", "execution_count": 12, "id": "cfedb480", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:54.213200Z", "iopub.status.busy": "2026-05-19T20:05:54.213029Z", "iopub.status.idle": "2026-05-19T20:05:54.620726Z", "shell.execute_reply": "2026-05-19T20:05:54.618939Z" } }, "outputs": [], "source": [ "w->writeToFile(\"rf502_workspace.root\");" ] }, { "cell_type": "markdown", "id": "d6542d1e", "metadata": {}, "source": [ "Workspace will remain in memory after macro finishes" ] }, { "cell_type": "code", "execution_count": 13, "id": "d53c406a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:05:54.657124Z", "iopub.status.busy": "2026-05-19T20:05:54.656963Z", "iopub.status.idle": "2026-05-19T20:05:54.860032Z", "shell.execute_reply": "2026-05-19T20:05:54.859598Z" } }, "outputs": [], "source": [ "gDirectory->Add(w);" ] } ], "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 }