{ "cells": [ { "cell_type": "markdown", "id": "7925f212", "metadata": {}, "source": [ "# rf503_wspaceread\n", "Organisation and simultaneous fits: reading and using a workspace\n", "\n", " The input file for this macro is generated by rf502_wspacewrite.C\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": "ccf7e352", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:07.039037Z", "iopub.status.busy": "2026-05-19T20:32:07.038923Z", "iopub.status.idle": "2026-05-19T20:32:07.053330Z", "shell.execute_reply": "2026-05-19T20:32:07.052802Z" } }, "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": "3fcc93ae", "metadata": {}, "source": [ "Read workspace from file\n", "-----------------------------------------------" ] }, { "cell_type": "markdown", "id": "addaf9ba", "metadata": {}, "source": [ "Open input file with workspace (generated by rf503_wspacewrite)" ] }, { "cell_type": "code", "execution_count": 2, "id": "9cd916c0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:07.055141Z", "iopub.status.busy": "2026-05-19T20:32:07.055027Z", "iopub.status.idle": "2026-05-19T20:32:07.260292Z", "shell.execute_reply": "2026-05-19T20:32:07.259515Z" } }, "outputs": [], "source": [ "TFile *f = new TFile(\"rf502_workspace.root\");" ] }, { "cell_type": "markdown", "id": "b05e04db", "metadata": {}, "source": [ "Retrieve workspace from file" ] }, { "cell_type": "code", "execution_count": 3, "id": "d17b3cf4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:07.261772Z", "iopub.status.busy": "2026-05-19T20:32:07.261644Z", "iopub.status.idle": "2026-05-19T20:32:07.467304Z", "shell.execute_reply": "2026-05-19T20:32:07.466618Z" } }, "outputs": [], "source": [ "RooWorkspace *w = (RooWorkspace *)f->Get(\"w\");" ] }, { "cell_type": "markdown", "id": "dd1d3fe1", "metadata": {}, "source": [ "Retrieve pdf, data from workspace\n", "-----------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "2a7a1fa9", "metadata": {}, "source": [ "Retrieve x,model and data from workspace" ] }, { "cell_type": "code", "execution_count": 4, "id": "ba794cfc", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:07.468854Z", "iopub.status.busy": "2026-05-19T20:32:07.468734Z", "iopub.status.idle": "2026-05-19T20:32:07.675843Z", "shell.execute_reply": "2026-05-19T20:32:07.674840Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_59:4:1: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n", "RooAbsData *data = w->data(\"modelData\");\n", "^\n" ] } ], "source": [ "RooRealVar *x = w->var(\"x\");\n", "RooAbsPdf *model = w->pdf(\"model\");\n", "RooAbsData *data = w->data(\"modelData\");" ] }, { "cell_type": "markdown", "id": "58587689", "metadata": {}, "source": [ "Print structure of composite pdf" ] }, { "cell_type": "code", "execution_count": 5, "id": "6ad76876", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:07.677250Z", "iopub.status.busy": "2026-05-19T20:32:07.677099Z", "iopub.status.idle": "2026-05-19T20:32:07.883171Z", "shell.execute_reply": "2026-05-19T20:32:07.882659Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0x7f16891d3820 RooAddPdf::model = 0.9/1 [Auto,Clean] \n", " 0x7f16893b6860/V- RooChebychev::bkg = 0.8 [Auto,Dirty] \n", " 0x7f16891afab0/V- RooRealVar::x = 5\n", " 0x7f16883ff250/V- RooRealVar::a0 = 0.5\n", " 0x562be4ac6b00/V- RooRealVar::a1 = 0.2\n", " 0x7f1689461110/V- RooRealVar::bkgfrac = 0.5\n", " 0x7f1689453fc0/V- RooAddPdf::sig = 1/1 [Auto,Clean] \n", " 0x7f1689417750/V- RooGaussian::sig1 = 1 [Auto,Dirty] \n", " 0x7f16891afab0/V- RooRealVar::x = 5\n", " 0x7f1689109d30/V- RooRealVar::mean = 5\n", " 0x7f16884b63b0/V- RooRealVar::sigma1 = 0.5\n", " 0x7f1688550cd0/V- RooRealVar::sig1frac = 0.8\n", " 0x7f1689424ec0/V- RooGaussian::sig2 = 1 [Auto,Dirty] \n", " 0x7f16891afab0/V- RooRealVar::x = 5\n", " 0x7f1689109d30/V- RooRealVar::mean = 5\n", " 0x7f16885c52d0/V- RooRealVar::sigma2 = 1\n" ] } ], "source": [ "model->Print(\"t\");" ] }, { "cell_type": "markdown", "id": "f920823b", "metadata": {}, "source": [ "Fit model to data, plot model\n", "---------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "003d455c", "metadata": {}, "source": [ "Fit model to data" ] }, { "cell_type": "code", "execution_count": 6, "id": "eb64fb34", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:07.884430Z", "iopub.status.busy": "2026-05-19T20:32:07.884315Z", "iopub.status.idle": "2026-05-19T20:32:08.093272Z", "shell.execute_reply": "2026-05-19T20:32:08.092836Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_61:2:16: error: reference to 'data' is ambiguous\n", " model->fitTo(*data, PrintLevel(-1));\n", " ^\n", "input_line_59:4:13: note: candidate found by name lookup is 'data'\n", "RooAbsData *data = w->data(\"modelData\");\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": [ "model->fitTo(*data, PrintLevel(-1));" ] }, { "cell_type": "markdown", "id": "e56db08b", "metadata": {}, "source": [ "Plot data and PDF overlaid" ] }, { "cell_type": "code", "execution_count": 7, "id": "dae88753", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:08.094555Z", "iopub.status.busy": "2026-05-19T20:32:08.094447Z", "iopub.status.idle": "2026-05-19T20:32:08.303368Z", "shell.execute_reply": "2026-05-19T20:32:08.302766Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_62:3:1: error: reference to 'data' is ambiguous\n", "data->plotOn(xframe);\n", "^\n", "input_line_59:4:13: note: candidate found by name lookup is 'data'\n", "RooAbsData *data = w->data(\"modelData\");\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 *xframe = x->frame(Title(\"Model and data read from workspace\"));\n", "data->plotOn(xframe);\n", "model->plotOn(xframe);" ] }, { "cell_type": "markdown", "id": "7d6dd21b", "metadata": {}, "source": [ "Overlay the background component of model with a dashed line" ] }, { "cell_type": "code", "execution_count": 8, "id": "b7c3e325", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:08.304779Z", "iopub.status.busy": "2026-05-19T20:32:08.304585Z", "iopub.status.idle": "2026-05-19T20:32:08.515593Z", "shell.execute_reply": "2026-05-19T20:32:08.515111Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_63:2:53: error: cannot take the address of an rvalue of type 'ELineStyle'\n", " model->plotOn(xframe, Components(\"bkg\"), LineStyle(kDashed));\n", " ^~~~~~~\n", "Error while creating dynamic expression for:\n", " model->plotOn(xframe, Components(\"bkg\"), LineStyle(kDashed))\n" ] } ], "source": [ "model->plotOn(xframe, Components(\"bkg\"), LineStyle(kDashed));" ] }, { "cell_type": "markdown", "id": "cda0a083", "metadata": {}, "source": [ "Overlay the background+sig2 components of model with a dotted line" ] }, { "cell_type": "code", "execution_count": 9, "id": "031da990", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:08.517490Z", "iopub.status.busy": "2026-05-19T20:32:08.517377Z", "iopub.status.idle": "2026-05-19T20:32:08.727169Z", "shell.execute_reply": "2026-05-19T20:32:08.726742Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_64:2:58: error: cannot take the address of an rvalue of type 'ELineStyle'\n", " model->plotOn(xframe, Components(\"bkg,sig2\"), LineStyle(kDotted));\n", " ^~~~~~~\n", "Error while creating dynamic expression for:\n", " model->plotOn(xframe, Components(\"bkg,sig2\"), LineStyle(kDotted))\n" ] } ], "source": [ "model->plotOn(xframe, Components(\"bkg,sig2\"), LineStyle(kDotted));" ] }, { "cell_type": "markdown", "id": "9597862a", "metadata": {}, "source": [ "Draw the frame on the canvas" ] }, { "cell_type": "code", "execution_count": 10, "id": "c1cab8e9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:08.728736Z", "iopub.status.busy": "2026-05-19T20:32:08.728617Z", "iopub.status.idle": "2026-05-19T20:32:09.077175Z", "shell.execute_reply": "2026-05-19T20:32:09.076726Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!\n", "You are probably missing the definition of void cling::runtime::internal::EvaluateT(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)\n", "Maybe you need to load the corresponding shared library?\n" ] } ], "source": [ "new TCanvas(\"rf503_wspaceread\", \"rf503_wspaceread\", 600, 600);\n", "gPad->SetLeftMargin(0.15);\n", "xframe->GetYaxis()->SetTitleOffset(1.4);\n", "xframe->Draw();" ] }, { "cell_type": "markdown", "id": "3e546160", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 11, "id": "b7b430bd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:09.078672Z", "iopub.status.busy": "2026-05-19T20:32:09.078528Z", "iopub.status.idle": "2026-05-19T20:32:09.308304Z", "shell.execute_reply": "2026-05-19T20:32:09.307826Z" } }, "outputs": [], "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 }