Logo ROOT  
Reference Guide
rf511_wsfactory_basic.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook -nodraw
4## Organization and simultaneous fits: basic use of the 'object factory' associated with a
5## workspace to rapidly build pdfs functions and their parameter components
6##
7## \macro_code
8##
9## \date February 2018
10## \authors Clemens Lange, Wouter Verkerke (C++ version)
11
12import ROOT
13
14
15compact = False
16w = ROOT.RooWorkspace("w")
17
18# Creating and adding basic pdfs
19# ----------------------------------------------------------------
20
21# Remake example pdf of tutorial rs502_wspacewrite.C:
22#
23# Basic pdf construction: ClassName.ObjectName(constructor arguments)
24# Variable construction : VarName[x,xlo,xhi], VarName[xlo,xhi], VarName[x]
25# P.d.f. addition : SUM.ObjectName(coef1*pdf1,...coefM*pdfM,pdfN)
26#
27
28if not compact:
29 # Use object factory to build pdf of tutorial rs502_wspacewrite
30 w.factory("Gaussian::sig1(x[-10,10],mean[5,0,10],0.5)")
31 w.factory("Gaussian::sig2(x,mean,1)")
32 w.factory("Chebychev::bkg(x,{a0[0.5,0.,1],a1[-0.2,0.,1.]})")
33 w.factory("SUM::sig(sig1frac[0.8,0.,1.]*sig1,sig2)")
34 w.factory("SUM::model(bkgfrac[0.5,0.,1.]*bkg,sig)")
35
36else:
37
38 # Use object factory to build pdf of tutorial rs502_wspacewrite but
39 # - Contracted to a single line recursive expression,
40 # - Omitting explicit names for components that are not referred to explicitly later
41
42 w.factory(
43 "SUM::model(bkgfrac[0.5,0.,1.]*Chebychev::bkg(x[-10,10],{a0[0.5,0.,1],a1[-0.2,0.,1.]}), "
44 "SUM(sig1frac[0.8,0.,1.]*Gaussian(x,mean[5,0,10],0.5), Gaussian(x,mean,1)))"
45 )
46
47# Advanced pdf constructor arguments
48# ----------------------------------------------------------------
49#
50# P.d.f. constructor arguments may by any type of ROOT.RooAbsArg, also
51#
52# Double_t -. converted to ROOT.RooConst(...)
53# {a,b,c} -. converted to ROOT.RooArgSet() or ROOT.RooArgList() depending on required ctor arg
54# dataset name -. convered to ROOT.RooAbsData reference for any dataset residing in the workspace
55# enum -. Any enum label that belongs to an enum defined in the (base)
56# class
57
58# Make a dummy dataset pdf 'model' and import it in the workspace
59data = w["model"].generate({w["x"]}, 1000)
60# Cannot call 'import' directly because this is a python keyword:
61w.Import(data, Rename="data")
62
63# Construct a KEYS pdf passing a dataset name and an enum type defining the
64# mirroring strategy
65# w.factory("KeysPdf::k(x,data,NoMirror,0.2)")
66# Workaround for pyROOT
67x = w["x"]
68k = ROOT.RooKeysPdf("k", "k", x, data, ROOT.RooKeysPdf.NoMirror, 0.2)
69w.Import(k, RenameAllNodes="workspace")
70
71# Print workspace contents
72w.Print()