Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf511_wsfactory_basic.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook -nodraw
4 ##
5 ## Organization and simultaneous fits: basic use of the 'object factory' associated with a workspace to rapidly build p.d.f.s functions and their parameter components
6 ##
7 ## \macro_code
8 ##
9 ## \date February 2018
10 ## \author Clemens Lange, Wouter Verkerke (C++ version)
11 
12 import ROOT
13 
14 
15 compact = ROOT.kFALSE
16 w = ROOT.RooWorkspace("w")
17 
18 # Creating and adding basic pdfs
19 # ----------------------------------------------------------------
20 
21 # Remake example p.d.f. of tutorial rs502_wspacewrite.C:
22 #
23 # Basic p.d.f. 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 
28 if not compact:
29  # Use object factory to build p.d.f. 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 
36 else:
37 
38  # Use object factory to build p.d.f. 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 # Advanced pdf constructor arguments
47 # ----------------------------------------------------------------
48 #
49 # P.d.f. constructor arguments may by any type of ROOT.RooAbsArg, also
50 #
51 # Double_t -. converted to ROOT.RooConst(...)
52 # {a,b,c} -. converted to ROOT.RooArgSet() or ROOT.RooArgList() depending on required ctor arg
53 # dataset name -. convered to ROOT.RooAbsData reference for any dataset residing in the workspace
54 # enum -. Any enum label that belongs to an enum defined in the (base)
55 # class
56 
57 # Make a dummy dataset p.d.f. 'model' and import it in the workspace
58 data = w.pdf("model").generate(ROOT.RooArgSet(w.var("x")), 1000)
59 # Cannot call 'import' directly because this is a python keyword:
60 getattr(w, 'import')(data, ROOT.RooFit.Rename("data"))
61 
62 # Construct a KEYS p.d.f. passing a dataset name and an enum type defining the
63 # mirroring strategy
64 # w.factory("KeysPdf::k(x,data,NoMirror,0.2)")
65 # Workaround for pyROOT
66 x = w.var("x")
67 k = ROOT.RooKeysPdf("k", "k", x, data, ROOT.RooKeysPdf.NoMirror, 0.2)
68 getattr(w, 'import')(k, ROOT.RooFit.RenameAllNodes("workspace"))
69 
70 # Print workspace contents
71 w.Print()
72