{
"cells": [
{
"cell_type": "markdown",
"id": "a610fb96",
"metadata": {},
"source": [
"# rf512_wsfactory_oper\n",
"Organization and simultaneous fits: operator expressions and expression-based basic\n",
"pdfs in the workspace factory syntax\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": "3243213e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:39.564144Z",
"iopub.status.busy": "2026-05-19T20:32:39.564019Z",
"iopub.status.idle": "2026-05-19T20:32:39.578571Z",
"shell.execute_reply": "2026-05-19T20:32:39.578057Z"
}
},
"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": "code",
"execution_count": 2,
"id": "c08dc298",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:39.589472Z",
"iopub.status.busy": "2026-05-19T20:32:39.589345Z",
"iopub.status.idle": "2026-05-19T20:32:39.794539Z",
"shell.execute_reply": "2026-05-19T20:32:39.793902Z"
}
},
"outputs": [],
"source": [
"RooWorkspace *w = new RooWorkspace(\"w\");"
]
},
{
"cell_type": "markdown",
"id": "383e7136",
"metadata": {},
"source": [
"You can define typedefs for even shorter construction semantics"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "741adb15",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:39.797005Z",
"iopub.status.busy": "2026-05-19T20:32:39.796884Z",
"iopub.status.idle": "2026-05-19T20:32:40.002511Z",
"shell.execute_reply": "2026-05-19T20:32:40.001847Z"
}
},
"outputs": [],
"source": [
"w->factory(\"$Typedef(Gaussian,Gaus)\");\n",
"w->factory(\"$Typedef(Chebychev,Cheby)\");"
]
},
{
"cell_type": "markdown",
"id": "59990a65",
"metadata": {},
"source": [
"Operator p.d.f. examples\n",
"------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "1a81a40e",
"metadata": {},
"source": [
"PDF addition is done with SUM (coef1*pdf1,pdf2)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9ed3b231",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:40.004839Z",
"iopub.status.busy": "2026-05-19T20:32:40.004716Z",
"iopub.status.idle": "2026-05-19T20:32:40.207125Z",
"shell.execute_reply": "2026-05-19T20:32:40.206343Z"
}
},
"outputs": [],
"source": [
"w->factory(\"SUM::summodel( f[0,1]*Gaussian::gx(x[-10,10],m[0],1.0), Chebychev::ch(x,{0.1,0.2,-0.3}) )\");"
]
},
{
"cell_type": "markdown",
"id": "a96deca2",
"metadata": {},
"source": [
"Extended PDF addition is done with SUM (yield1*pdf1,yield2*pdf2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5f8cfea2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:40.219729Z",
"iopub.status.busy": "2026-05-19T20:32:40.219519Z",
"iopub.status.idle": "2026-05-19T20:32:40.421973Z",
"shell.execute_reply": "2026-05-19T20:32:40.421275Z"
}
},
"outputs": [],
"source": [
"w->factory(\"SUM::extsummodel( Nsig[0,1000]*gx, Nbkg[0,1000]*ch )\");"
]
},
{
"cell_type": "markdown",
"id": "fbd2045c",
"metadata": {},
"source": [
"PDF multiplication is done with PROD ( pdf1, pdf2 )"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c9d9fe40",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:40.424751Z",
"iopub.status.busy": "2026-05-19T20:32:40.424616Z",
"iopub.status.idle": "2026-05-19T20:32:40.626992Z",
"shell.execute_reply": "2026-05-19T20:32:40.626317Z"
}
},
"outputs": [],
"source": [
"w->factory(\"PROD::gxz( gx, Gaussian::gz(z[-10,10],0,1) )\");"
]
},
{
"cell_type": "markdown",
"id": "8d2bc97a",
"metadata": {},
"source": [
"Conditional pdf multiplication is done with PROD ( pdf1|obs, pdf2 )"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7e500714",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:40.628919Z",
"iopub.status.busy": "2026-05-19T20:32:40.628802Z",
"iopub.status.idle": "2026-05-19T20:32:40.831172Z",
"shell.execute_reply": "2026-05-19T20:32:40.830441Z"
}
},
"outputs": [],
"source": [
"w->factory(\"Gaussian::gy( y[-10,10], x, 1.0 )\");\n",
"w->factory(\"PROD::gxycond( gy|x, gx )\");"
]
},
{
"cell_type": "markdown",
"id": "dda3c14e",
"metadata": {},
"source": [
"Convolution (numeric/ fft) is done with NCONV/FCONV (obs,pdf1,pdf2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "9b248e55",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:40.832892Z",
"iopub.status.busy": "2026-05-19T20:32:40.832773Z",
"iopub.status.idle": "2026-05-19T20:32:41.036400Z",
"shell.execute_reply": "2026-05-19T20:32:41.035792Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Caching -- Changing internal binning of variable 'x' in FFT 'lxg' from 100 to 930 to improve the precision of the numerical FFT. This can be done manually by setting an additional binning named 'cache'.\n"
]
}
],
"source": [
"w->factory(\"FCONV::lxg( x, Gaussian::g(x,mg[0],1), Landau::lc(x,0,1) )\");"
]
},
{
"cell_type": "markdown",
"id": "7a2f217e",
"metadata": {},
"source": [
"Simultaneous pdfs are constructed with SIMUL( index, state1=pdf1, state2=pdf2,...)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3a86c602",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:41.037957Z",
"iopub.status.busy": "2026-05-19T20:32:41.037840Z",
"iopub.status.idle": "2026-05-19T20:32:41.240452Z",
"shell.execute_reply": "2026-05-19T20:32:41.239367Z"
}
},
"outputs": [],
"source": [
"w->factory(\"SIMUL::smodel( c[A=0,B=1], A=Gaussian::gs(x,m,s[1.0, 0.01, 10.0]), B=Landau::ls(x,0,1) )\");"
]
},
{
"cell_type": "markdown",
"id": "a1a12297",
"metadata": {},
"source": [
"Operator function examples\n",
"---------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "cadeb938",
"metadata": {},
"source": [
"Function multiplication is done with prod (func1, func2,...)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b88c121e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:41.241908Z",
"iopub.status.busy": "2026-05-19T20:32:41.241788Z",
"iopub.status.idle": "2026-05-19T20:32:41.447411Z",
"shell.execute_reply": "2026-05-19T20:32:41.446799Z"
}
},
"outputs": [],
"source": [
"w->factory(\"prod::uv(u[10],v[10])\");"
]
},
{
"cell_type": "markdown",
"id": "533280e3",
"metadata": {},
"source": [
"Function addition is done with sum(func1,func2)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c0f88242",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:41.449265Z",
"iopub.status.busy": "2026-05-19T20:32:41.449131Z",
"iopub.status.idle": "2026-05-19T20:32:41.654734Z",
"shell.execute_reply": "2026-05-19T20:32:41.654012Z"
}
},
"outputs": [],
"source": [
"w->factory(\"sum::uv2(u,v)\");"
]
},
{
"cell_type": "markdown",
"id": "ba0b18e8",
"metadata": {},
"source": [
"Lagrangian morphing function for the example shown in rf711_lagrangianmorph"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5716029a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:41.656434Z",
"iopub.status.busy": "2026-05-19T20:32:41.656315Z",
"iopub.status.idle": "2026-05-19T20:32:41.863386Z",
"shell.execute_reply": "2026-05-19T20:32:41.862540Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#0] PROGRESS:InputArguments -- initializing physics inputs from file /github/home/ROOT-CI/build/tutorials/roofit/roofit/input_histos_rf_lagrangianmorph.root with object name(s) 'pTV'\n",
"[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing dataset dh_SM_NPsq0_morph\n",
"[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing dataset dh_cHq3_NPsq1_morph\n",
"[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing dataset dh_cHq3_NPsq2_morph\n"
]
}
],
"source": [
"std::string infilename = std::string(gROOT->GetTutorialDir()) + \"/roofit/roofit/input_histos_rf_lagrangianmorph.root\";\n",
"w->factory((\"lagrangianmorph::morph($fileName('\"+infilename+\"'),$observableName('pTV'),$couplings({cHq3[0,1],SM[1]}),$NewPhysics(cHq3=1,SM=0),$folders({'SM_NPsq0','cHq3_NPsq1','cHq3_NPsq2'}))\").c_str());"
]
},
{
"cell_type": "markdown",
"id": "5c6a3056",
"metadata": {},
"source": [
"Taylor expansion is done with taylorexpand(func,{var1,var2,...},val,order)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "31a813f6",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:41.864692Z",
"iopub.status.busy": "2026-05-19T20:32:41.864557Z",
"iopub.status.idle": "2026-05-19T20:32:42.070173Z",
"shell.execute_reply": "2026-05-19T20:32:42.069513Z"
}
},
"outputs": [],
"source": [
"w->factory(\"taylorexpand::te(expr::poly('x^4+5*x^3+2*x^2+x+1',x),{x},0.0,2)\");"
]
},
{
"cell_type": "markdown",
"id": "8183c5ef",
"metadata": {},
"source": [
"Interpreted and compiled expression based p.d.f.s.\n",
"---------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "d044b4b8",
"metadata": {},
"source": [
"Create a RooGenericPdf interpreted pdf You can use single quotes to pass the expression string argument"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "8e7413bf",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:42.071822Z",
"iopub.status.busy": "2026-05-19T20:32:42.071700Z",
"iopub.status.idle": "2026-05-19T20:32:42.277775Z",
"shell.execute_reply": "2026-05-19T20:32:42.276623Z"
}
},
"outputs": [],
"source": [
"w->factory(\"EXPR::G('x*x+1',x)\");"
]
},
{
"cell_type": "markdown",
"id": "ac9c2aa8",
"metadata": {},
"source": [
"Create a custom compiled pdf similar to the above interpreted pdf\n",
"The code required to make this pdf is automatically embedded in the workspace"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "986a9a45",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:42.279153Z",
"iopub.status.busy": "2026-05-19T20:32:42.279036Z",
"iopub.status.idle": "2026-05-19T20:32:47.454546Z",
"shell.execute_reply": "2026-05-19T20:32:47.453908Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#0] ERROR:ObjectHandling -- RooWorkspace::CodeRepo(w) ERROR: cannot retrieve code file names for class RooCFAuto000Pdf through ROOT TClass interface, unable to import code\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : creating shared library /github/home/master/notebooks/RooCFAuto000Pdf_cxx.so\n"
]
}
],
"source": [
"w->factory(\"CEXPR::GC('x*x+a',{x,a[1]})\");"
]
},
{
"cell_type": "markdown",
"id": "0aaeea50",
"metadata": {},
"source": [
"Compiled and interpreted functions (rather than pdfs) can be made with the lower case\n",
"'expr' and 'cexpr' types"
]
},
{
"cell_type": "markdown",
"id": "74acaf59",
"metadata": {},
"source": [
"Print workspace contents"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "0b766840",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:47.456384Z",
"iopub.status.busy": "2026-05-19T20:32:47.456261Z",
"iopub.status.idle": "2026-05-19T20:32:47.666880Z",
"shell.execute_reply": "2026-05-19T20:32:47.665972Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"RooWorkspace(w) w contents\n",
"\n",
"variables\n",
"---------\n",
"(Nbkg,Nsig,SM,a,binWidth_pTV,c,cHq3,f,m,mg,nNP0,nNP1,nNP2,nNP3,nNP4,pTV,s,te_c0,te_c1,te_c2,te_x^0,te_x^1,te_x^2,u,v,x,y,z)\n",
"\n",
"p.d.f.s\n",
"-------\n",
"RooGenericPdf::G[ actualVars=(x) formula=\"x[0]*x[0]+1\" ] = 1\n",
"RooCFAuto000Pdf::GC[ x=x a=a ] = 1\n",
"RooChebychev::ch[ x=x coefList=(0.1,0.2,-0.3) ] = 0.8\n",
"RooAddPdf::extsummodel[ Nsig * gx + Nbkg * ch ] = 0.9/1\n",
"RooGaussian::g[ x=x mean=mg sigma=1 ] = 1\n",
"RooGaussian::gs[ x=x mean=m sigma=s ] = 1\n",
"RooGaussian::gx[ x=x mean=m sigma=1 ] = 1\n",
"RooProdPdf::gxycond[ gx * gy|x ] = 1\n",
"RooProdPdf::gxz[ gx * gz ] = 1\n",
"RooGaussian::gy[ x=y mean=x sigma=1 ] = 1\n",
"RooGaussian::gz[ x=z mean=0 sigma=1 ] = 1\n",
"RooLandau::lc[ x=x mean=0 sigma=1 ] = 0.178854\n",
"RooLandau::ls[ x=x mean=0 sigma=1 ] = 0.178854\n",
"RooFFTConvPdf::lxg[ g(x) (*) lc(x) ] = 380336\n",
"RooSimultaneous::smodel[ indexCat=c A=gs B=ls ] = 1\n",
"RooAddPdf::summodel[ f * gx + [%] * ch ] = 0.9/1\n",
"\n",
"functions\n",
"--------\n",
"RooLagrangianMorphFunc::morph[ physics=(phys_SM_NPsq0_morph,phys_cHq3_NPsq1_morph,phys_cHq3_NPsq2_morph) operators=(cHq3,SM) observables=(pTV) binWidths=(binWidth_pTV) flags=(nNP0,nNP1,nNP2,nNP3,nNP4) binWidth_pTV * SM_NPsq0_morph + binWidth_pTV * cHq3_NPsq1_morph + binWidth_pTV * cHq3_NPsq2_morph ] = 35.9835\n",
"RooHistFunc::phys_SM_NPsq0_morph[ depList=(pTV) ] = 65.1309\n",
"RooHistFunc::phys_cHq3_NPsq1_morph[ depList=(pTV) ] = 544.966\n",
"RooHistFunc::phys_cHq3_NPsq2_morph[ depList=(pTV) ] = 1528.22\n",
"RooFormulaVar::poly[ actualVars=(x) formula=\"x^4+5*x^3+2*x^2+x+1\" ] = 1\n",
"RooPolyFunc::te[ vars=(x) te_t0=(te_x^0,te_c0) te_t1=(te_x^1,te_c1) te_t2=(te_x^2,te_c2) ] = 1\n",
"RooProduct::uv[ u * v ] = 100\n",
"RooAddition::uv2[ u + v ] = 20\n",
"\n",
"embedded datasets (in pdfs and functions)\n",
"-----------------------------------------\n",
"RooDataHist::dh_SM_NPsq0_morph(pTV)\n",
"RooDataHist::dh_cHq3_NPsq1_morph(pTV)\n",
"RooDataHist::dh_cHq3_NPsq2_morph(pTV)\n",
"\n"
]
}
],
"source": [
"w->Print();"
]
},
{
"cell_type": "markdown",
"id": "52774301",
"metadata": {},
"source": [
"Make workspace visible on command line"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "1f5e6b4a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:32:47.668322Z",
"iopub.status.busy": "2026-05-19T20:32:47.668201Z",
"iopub.status.idle": "2026-05-19T20:32:47.876471Z",
"shell.execute_reply": "2026-05-19T20:32:47.875842Z"
}
},
"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
}