Rf 5 1 1_Wsfactory_Basic

Organization and simultaneous fits: basic use of the 'object factory' associated with a workspace to rapidly build pdfs functions and their parameter components

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Friday, May 13, 2022 at 09:36 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooChebychev.h"
#include "RooAddPdf.h"
#include "RooWorkspace.h"
#include "RooPlot.h"
#include "TCanvas.h"
#include "TAxis.h"
In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;

Arguments are defined.

In [3]:
Bool_t compact = kFALSE;
In [4]:
RooWorkspace *w = new RooWorkspace("w");

Creating and adding basic p.d.f.s

Remake example pdf of tutorial rs502_wspacewrite.c:

Basic pdf construction: ClassName::ObjectName(constructor arguments) Variable construction : VarName[x,xlo,xhi], VarName[xlo,xhi], VarName[x] P.d.f. addition : SUM::ObjectName(coef1pdf1,...coefMpdfM,pdfN)

In [5]:
if (!compact) {

   // Use object factory to build pdf of tutorial rs502_wspacewrite

} else {

   // Use object factory to build pdf of tutorial rs502_wspacewrite but
   //  - Contracted to a single line recursive expression,
   //  - Omitting explicit names for components that are not referred to explicitly later

              "SUM(sig1frac[0.8,0.,1.]*Gaussian(x,mean[5,0,10],0.5), Gaussian(x,mean,1)))");

Advanced p.d.f. constructor arguments

P.d.f. constructor arguments may by any type of RooAbsArg, but also

Double_t --> converted to RooConst(...) {a,b,c} --> converted to RooArgSet() or RooArgList() depending on required ctor arg dataset name --> converted to RooAbsData reference for any dataset residing in the workspace enum --> Any enum label that belongs to an enum defined in the (base) class

Make a dummy dataset pdf 'model' and import it in the workspace

In [6]:
RooDataSet *data = w->pdf("model")->generate(*w->var("x"), 1000);
w->import(*data, Rename("data"));
[#0] WARNING:Eval -- Evaluating RooAddPdf without a defined normalization set. This can lead to ambiguos coefficients definition and incorrect results. Use RooAddPdf::fixCoefNormalization(nset) to provide a normalization set for defining uniquely RooAddPdf coefficients!
[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing dataset modelData
[#1] INFO:ObjectHandling -- RooWorkSpace::import(w) changing name of dataset from  modelData to data
input_line_55:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration
 RooDataSet *data = w->pdf("model")->generate(*w->var("x"), 1000);

Construct a keys pdf passing a dataset name and an enum type defining the mirroring strategy

In [7]:

Print workspace contents

In [8]:
RooWorkspace(w) w contents


RooChebychev::bkg[ x=x coefList=(a0,a1) ] = 0.8
RooKeysPdf::k[ x=x ] = 0.0210129
RooAddPdf::model[ bkgfrac * bkg + [%] * sig ] = 0.4
RooAddPdf::sig[ sig1frac * sig1 + [%] * sig2 ] = 7.45331e-07
RooGaussian::sig1[ x=x mean=mean sigma=0.5 ] = 1.92875e-22
RooGaussian::sig2[ x=x mean=mean sigma=1 ] = 3.72665e-06