{ "cells": [ { "cell_type": "markdown", "id": "b7bed22d", "metadata": {}, "source": [ "# rf505_asciicfg\n", "Organisation and simultaneous fits: reading and writing ASCII configuration files\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": "ef72980f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:15.914707Z", "iopub.status.busy": "2026-05-19T20:32:15.914568Z", "iopub.status.idle": "2026-05-19T20:32:15.928942Z", "shell.execute_reply": "2026-05-19T20:32:15.928356Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooPolynomial.h\"\n", "#include \"RooAddPdf.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"RooPlot.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "79d37109", "metadata": {}, "source": [ "Create pdf\n", "------------------" ] }, { "cell_type": "markdown", "id": "318a7825", "metadata": {}, "source": [ "Construct gauss(x,m,s)" ] }, { "cell_type": "code", "execution_count": 2, "id": "9986c476", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:15.930412Z", "iopub.status.busy": "2026-05-19T20:32:15.930293Z", "iopub.status.idle": "2026-05-19T20:32:16.300236Z", "shell.execute_reply": "2026-05-19T20:32:16.299846Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] WARNING:InputArguments -- The parameter 's' with range [-10, 10] of the RooGaussian 'g' exceeds the safe range of (0, inf). Advise to limit its range.\n" ] } ], "source": [ "RooRealVar x(\"x\", \"x\", -10, 10);\n", "RooRealVar m(\"m\", \"m\", 0, -10, 10);\n", "RooRealVar s(\"s\", \"s\", 1, -10, 10);\n", "RooGaussian gauss(\"g\", \"g\", x, m, s);" ] }, { "cell_type": "markdown", "id": "8a9c12ec", "metadata": {}, "source": [ "Construct poly(x,p0)" ] }, { "cell_type": "code", "execution_count": 3, "id": "877c4e8b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:16.302199Z", "iopub.status.busy": "2026-05-19T20:32:16.302080Z", "iopub.status.idle": "2026-05-19T20:32:16.510447Z", "shell.execute_reply": "2026-05-19T20:32:16.509740Z" } }, "outputs": [], "source": [ "RooRealVar p0(\"p0\", \"p0\", 0.01, 0., 1.);\n", "RooPolynomial poly(\"p\", \"p\", x, p0);" ] }, { "cell_type": "markdown", "id": "8d962cce", "metadata": {}, "source": [ "Construct model = f*gauss(x) + (1-f)*poly(x)" ] }, { "cell_type": "code", "execution_count": 4, "id": "362dc798", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:16.512291Z", "iopub.status.busy": "2026-05-19T20:32:16.512169Z", "iopub.status.idle": "2026-05-19T20:32:16.720663Z", "shell.execute_reply": "2026-05-19T20:32:16.719849Z" } }, "outputs": [], "source": [ "RooRealVar f(\"f\", \"f\", 0.5, 0., 1.);\n", "RooAddPdf model(\"model\", \"model\", RooArgSet(gauss, poly), f);" ] }, { "cell_type": "markdown", "id": "09783976", "metadata": {}, "source": [ "Fit model to toy data\n", "-----------------------------------------" ] }, { "cell_type": "code", "execution_count": 5, "id": "860733c0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:16.722444Z", "iopub.status.busy": "2026-05-19T20:32:16.722324Z", "iopub.status.idle": "2026-05-19T20:32:16.931211Z", "shell.execute_reply": "2026-05-19T20:32:16.930576Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:Fitting -- RooAbsPdf::fitTo(model) fixing normalization set for coefficient determination to observables in data\n", "[#1] INFO:Fitting -- using generic CPU library compiled with no vectorizations\n", "[#1] INFO:Fitting -- Creation of NLL object took 607.182 μs\n", "[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData) Summation contains a RooNLLVar, using its error level\n", "[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n" ] } ], "source": [ "std::unique_ptr d{model.generate(x, 1000)};\n", "model.fitTo(*d, PrintLevel(-1));" ] }, { "cell_type": "markdown", "id": "b7e6924f", "metadata": {}, "source": [ "Write parameters to ascii file\n", "-----------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "dfd770cc", "metadata": {}, "source": [ "Obtain set of parameters" ] }, { "cell_type": "code", "execution_count": 6, "id": "00dbbca4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:16.932880Z", "iopub.status.busy": "2026-05-19T20:32:16.932751Z", "iopub.status.idle": "2026-05-19T20:32:17.141134Z", "shell.execute_reply": "2026-05-19T20:32:17.140397Z" } }, "outputs": [], "source": [ "std::unique_ptr params{model.getParameters(x)};" ] }, { "cell_type": "markdown", "id": "db61f2ec", "metadata": {}, "source": [ "Write parameters to file" ] }, { "cell_type": "code", "execution_count": 7, "id": "69382822", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:17.143547Z", "iopub.status.busy": "2026-05-19T20:32:17.143425Z", "iopub.status.idle": "2026-05-19T20:32:17.349152Z", "shell.execute_reply": "2026-05-19T20:32:17.348678Z" } }, "outputs": [], "source": [ "params->writeToFile(\"rf505_asciicfg_example.txt\");\n", "\n", "TString dir1 = gROOT->GetTutorialDir() ;\n", "dir1.Append(\"/roofit/rf505_asciicfg.txt\") ;\n", "TString dir2 = \"rf505_asciicfg_example.txt\";" ] }, { "cell_type": "markdown", "id": "6276ffe1", "metadata": {}, "source": [ "Read parameters from ascii file\n", "----------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "df257e4b", "metadata": {}, "source": [ "Read parameters from file" ] }, { "cell_type": "code", "execution_count": 8, "id": "038363b6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:17.361343Z", "iopub.status.busy": "2026-05-19T20:32:17.361210Z", "iopub.status.idle": "2026-05-19T20:32:17.567227Z", "shell.execute_reply": "2026-05-19T20:32:17.566734Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1) 0x7f4012f2e000 RooRealVar:: f = 0.50733 +/- 0.020971 L(0 - 1) \"f\"\n", " 2) 0x7f40130023e8 RooRealVar:: m = 0.0064018 +/- 0.053686 L(-10 - 10) \"m\"\n", " 3) 0x7f4012f36000 RooRealVar:: p0 = 0.0073509 +/- 0.0078312 L(0 - 1) \"p0\"\n", " 4) 0x7f40130027d0 RooRealVar:: s = 0.96516 +/- 0.047052 L(-10 - 10) \"s\"\n" ] } ], "source": [ "params->readFromFile(dir2);\n", "params->Print(\"v\");" ] }, { "cell_type": "markdown", "id": "1d971d9e", "metadata": {}, "source": [ "Read parameters from section 'Section2' of file" ] }, { "cell_type": "code", "execution_count": 9, "id": "d9547bef", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:17.568809Z", "iopub.status.busy": "2026-05-19T20:32:17.568681Z", "iopub.status.idle": "2026-05-19T20:32:17.774743Z", "shell.execute_reply": "2026-05-19T20:32:17.774240Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] ERROR:InputArguments -- RooArgSet::readFromFile(parameters) error opening file /github/home/ROOT-CI/build/tutorials/roofit/rf505_asciicfg.txt\n", " 1) 0x7f4012f2e000 RooRealVar:: f = 0.50733 +/- 0.020971 L(0 - 1) \"f\"\n", " 2) 0x7f40130023e8 RooRealVar:: m = 0.0064018 +/- 0.053686 L(-10 - 10) \"m\"\n", " 3) 0x7f4012f36000 RooRealVar:: p0 = 0.0073509 +/- 0.0078312 L(0 - 1) \"p0\"\n", " 4) 0x7f40130027d0 RooRealVar:: s = 0.96516 +/- 0.047052 L(-10 - 10) \"s\"\n" ] } ], "source": [ "params->readFromFile(dir1, 0, \"Section2\");\n", "params->Print(\"v\");" ] }, { "cell_type": "markdown", "id": "f5c9d939", "metadata": {}, "source": [ "Read parameters from section 'Section3' of file. Mark all\n", "variables that were processed with the \"READ\" attribute" ] }, { "cell_type": "code", "execution_count": 10, "id": "5706076b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:17.776251Z", "iopub.status.busy": "2026-05-19T20:32:17.776126Z", "iopub.status.idle": "2026-05-19T20:32:17.981975Z", "shell.execute_reply": "2026-05-19T20:32:17.981517Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] ERROR:InputArguments -- RooArgSet::readFromFile(parameters) error opening file /github/home/ROOT-CI/build/tutorials/roofit/rf505_asciicfg.txt\n" ] } ], "source": [ "params->readFromFile(dir1, \"READ\", \"Section3\");" ] }, { "cell_type": "markdown", "id": "825b4aab", "metadata": {}, "source": [ "Print the list of parameters that were not read from Section3" ] }, { "cell_type": "code", "execution_count": 11, "id": "ce49c074", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:17.983470Z", "iopub.status.busy": "2026-05-19T20:32:17.983348Z", "iopub.status.idle": "2026-05-19T20:32:18.191850Z", "shell.execute_reply": "2026-05-19T20:32:18.191374Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The following parameters of the were _not_ read from Section3: (f,m,p0,s)\n" ] } ], "source": [ "cout << \"The following parameters of the were _not_ read from Section3: \"\n", " << (*params->selectByAttrib(\"READ\", false)) << endl;" ] }, { "cell_type": "markdown", "id": "a036db59", "metadata": {}, "source": [ "Read parameters from section 'Section4' of file, which contains\n", "'include file' statement of rf505_asciicfg_example.txt\n", "so that we effective read the same" ] }, { "cell_type": "code", "execution_count": 12, "id": "246a611d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:32:18.193390Z", "iopub.status.busy": "2026-05-19T20:32:18.193270Z", "iopub.status.idle": "2026-05-19T20:32:18.399181Z", "shell.execute_reply": "2026-05-19T20:32:18.398747Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] ERROR:InputArguments -- RooArgSet::readFromFile(parameters) error opening file /github/home/ROOT-CI/build/tutorials/roofit/rf505_asciicfg.txt\n", " 1) 0x7f4012f2e000 RooRealVar:: f = 0.50733 +/- 0.020971 L(0 - 1) \"f\"\n", " 2) 0x7f40130023e8 RooRealVar:: m = 0.0064018 +/- 0.053686 L(-10 - 10) \"m\"\n", " 3) 0x7f4012f36000 RooRealVar:: p0 = 0.0073509 +/- 0.0078312 L(0 - 1) \"p0\"\n", " 4) 0x7f40130027d0 RooRealVar:: s = 0.96516 +/- 0.047052 L(-10 - 10) \"s\"\n" ] } ], "source": [ "params->readFromFile(dir1, 0, \"Section4\");\n", "params->Print(\"v\");" ] } ], "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 }