{
"cells": [
{
"cell_type": "markdown",
"id": "c842a433",
"metadata": {},
"source": [
"# rf511_wsfactory_basic\n",
"Organization and simultaneous fits: basic use of the 'object factory' associated with\n",
"a workspace to rapidly build pdfs functions and their parameter components\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": "cd12ad9a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:34.609986Z",
"iopub.status.busy": "2026-05-19T20:32:34.609873Z",
"iopub.status.idle": "2026-05-19T20:32:34.624218Z",
"shell.execute_reply": "2026-05-19T20:32:34.623735Z"
}
},
"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",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "a7625b40",
"metadata": {},
"source": [
" Arguments are defined. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "572b45b4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:34.625768Z",
"iopub.status.busy": "2026-05-19T20:32:34.625650Z",
"iopub.status.idle": "2026-05-19T20:32:34.945065Z",
"shell.execute_reply": "2026-05-19T20:32:34.944350Z"
}
},
"outputs": [],
"source": [
"bool compact = false;"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6f1ea7f3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:34.947254Z",
"iopub.status.busy": "2026-05-19T20:32:34.947136Z",
"iopub.status.idle": "2026-05-19T20:32:35.152386Z",
"shell.execute_reply": "2026-05-19T20:32:35.152004Z"
}
},
"outputs": [],
"source": [
"RooWorkspace *w = new RooWorkspace(\"w\");"
]
},
{
"cell_type": "markdown",
"id": "1d494273",
"metadata": {},
"source": [
"Creating and adding basic p.d.f.s\n",
"----------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "664c0dca",
"metadata": {},
"source": [
"Remake example pdf of tutorial rs502_wspacewrite.C:\n",
"\n",
"Basic pdf construction: ClassName::ObjectName(constructor arguments)\n",
"Variable construction : VarName[x,xlo,xhi], VarName[xlo,xhi], VarName[x]\n",
"P.d.f. addition : SUM::ObjectName(coef1*pdf1,...coefM*pdfM,pdfN)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "db4831e0",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:35.163253Z",
"iopub.status.busy": "2026-05-19T20:32:35.163100Z",
"iopub.status.idle": "2026-05-19T20:32:35.375914Z",
"shell.execute_reply": "2026-05-19T20:32:35.369399Z"
}
},
"outputs": [],
"source": [
"if (!compact) {\n",
"\n",
" // Use object factory to build pdf of tutorial rs502_wspacewrite\n",
" w->factory(\"Gaussian::sig1(x[-10,10],mean[5,0,10],0.5)\");\n",
" w->factory(\"Gaussian::sig2(x,mean,1)\");\n",
" w->factory(\"Chebychev::bkg(x,{a0[0.5,0.,1],a1[0.2,0.,1.]})\");\n",
" w->factory(\"SUM::sig(sig1frac[0.8,0.,1.]*sig1,sig2)\");\n",
" w->factory(\"SUM::model(bkgfrac[0.5,0.,1.]*bkg,sig)\");\n",
"\n",
"} else {\n",
"\n",
" // Use object factory to build pdf of tutorial rs502_wspacewrite but\n",
" // - Contracted to a single line recursive expression,\n",
" // - Omitting explicit names for components that are not referred to explicitly later\n",
"\n",
" w->factory(\"SUM::model(bkgfrac[0.5,0.,1.]*Chebychev::bkg(x[-10,10],{a0[0.5,0.,1],a1[0.2,0.,1.]}),\"\n",
" \"SUM(sig1frac[0.8,0.,1.]*Gaussian(x,mean[5,0,10],0.5), Gaussian(x,mean,1)))\");\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "94092592",
"metadata": {},
"source": [
"Advanced p.d.f. constructor arguments\n",
"----------------------------------------------------------------\n",
"\n",
"P.d.f. constructor arguments may by any type of RooAbsArg, but also\n",
"\n",
"double --> converted to RooConst(...)\n",
"{a,b,c} --> converted to RooArgSet() or RooArgList() depending on required ctor arg\n",
"dataset name --> converted to RooAbsData reference for any dataset residing in the workspace\n",
"enum --> Any enum label that belongs to an enum defined in the (base) class"
]
},
{
"cell_type": "markdown",
"id": "60114005",
"metadata": {},
"source": [
"Make a dummy dataset pdf 'model' and import it in the workspace"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0631efc8",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:35.377642Z",
"iopub.status.busy": "2026-05-19T20:32:35.377510Z",
"iopub.status.idle": "2026-05-19T20:32:35.583295Z",
"shell.execute_reply": "2026-05-19T20:32:35.582848Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing dataset modelData\n",
"[#1] INFO:ObjectHandling -- RooWorkSpace::import(w) changing name of dataset from modelData to data\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_55: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{w->pdf(\"model\")->generate(*w->var(\"x\"), 1000)};\n",
" ^\n"
]
}
],
"source": [
"std::unique_ptr data{w->pdf(\"model\")->generate(*w->var(\"x\"), 1000)};\n",
"w->import(*data, Rename(\"data\"));"
]
},
{
"cell_type": "markdown",
"id": "f7305591",
"metadata": {},
"source": [
"Construct a KEYS pdf passing a dataset name and an enum type defining the\n",
"mirroring strategy"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b82568e9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:35.584726Z",
"iopub.status.busy": "2026-05-19T20:32:35.584576Z",
"iopub.status.idle": "2026-05-19T20:32:35.789913Z",
"shell.execute_reply": "2026-05-19T20:32:35.789383Z"
}
},
"outputs": [],
"source": [
"w->factory(\"KeysPdf::k(x,data,NoMirror,0.2)\");"
]
},
{
"cell_type": "markdown",
"id": "c338309c",
"metadata": {},
"source": [
"Print workspace contents"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3fa636fd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:35.791637Z",
"iopub.status.busy": "2026-05-19T20:32:35.791501Z",
"iopub.status.idle": "2026-05-19T20:32:36.000122Z",
"shell.execute_reply": "2026-05-19T20:32:35.999689Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"RooWorkspace(w) w contents\n",
"\n",
"variables\n",
"---------\n",
"(a0,a1,bkgfrac,mean,sig1frac,x)\n",
"\n",
"p.d.f.s\n",
"-------\n",
"RooChebychev::bkg[ x=x coefList=(a0,a1) ] = 0.8\n",
"RooKeysPdf::k[ x=x ] = 0.0210129\n",
"RooAddPdf::model[ bkgfrac * bkg + [%] * sig ] = 0.4/1\n",
"RooAddPdf::sig[ sig1frac * sig1 + [%] * sig2 ] = 7.45331e-07/1\n",
"RooGaussian::sig1[ x=x mean=mean sigma=0.5 ] = 1.92875e-22\n",
"RooGaussian::sig2[ x=x mean=mean sigma=1 ] = 3.72665e-06\n",
"\n",
"datasets\n",
"--------\n",
"RooDataSet::data(x)\n",
"\n"
]
}
],
"source": [
"w->Print();"
]
}
],
"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
}