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 ##
5 ## Organization and simultaneous fits: basic use of the 'object factory' associated with a
6 ## workspace to rapidly build p.d.f.s functions and their parameter components
7 ##
8 ## \macro_code
9 ##
10 ## \date February 2018
11 ## \authors Clemens Lange, Wouter Verkerke (C++ version)
12 
13 import ROOT
14 
15 
16 compact = ROOT.kFALSE
17 w = ROOT.RooWorkspace("w")
18 
19 # Creating and adding basic pdfs
20 # ----------------------------------------------------------------
21 
22 # Remake example p.d.f. of tutorial rs502_wspacewrite.C:
23 #
24 # Basic p.d.f. construction: ClassName.ObjectName(constructor arguments)
25 # Variable construction : VarName[x,xlo,xhi], VarName[xlo,xhi], VarName[x]
26 # P.d.f. addition : SUM.ObjectName(coef1*pdf1,...coefM*pdfM,pdfN)
27 #
28 
29 if not compact:
30  # Use object factory to build p.d.f. of tutorial rs502_wspacewrite
31  w.factory("Gaussian::sig1(x[-10,10],mean[5,0,10],0.5)")
32  w.factory("Gaussian::sig2(x,mean,1)")
33  w.factory("Chebychev::bkg(x,{a0[0.5,0.,1],a1[-0.2,0.,1.]})")
34  w.factory("SUM::sig(sig1frac[0.8,0.,1.]*sig1,sig2)")
35  w.factory("SUM::model(bkgfrac[0.5,0.,1.]*bkg,sig)")
36 
37 else:
38 
39  # Use object factory to build p.d.f. of tutorial rs502_wspacewrite but
40  # - Contracted to a single line recursive expression,
41  # - Omitting explicit names for components that are not referred to explicitly later
42 
43  w.factory(
44  "SUM::model(bkgfrac[0.5,0.,1.]*Chebychev::bkg(x[-10,10],{a0[0.5,0.,1],a1[-0.2,0.,1.]}), "
45  "SUM(sig1frac[0.8,0.,1.]*Gaussian(x,mean[5,0,10],0.5), Gaussian(x,mean,1)))")
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 p.d.f. 'model' and import it in the workspace
59 data = w.pdf("model").generate(ROOT.RooArgSet(w.var("x")), 1000)
60 # Cannot call 'import' directly because this is a python keyword:
61 w.Import(data, ROOT.RooFit.Rename("data"))
62 
63 # Construct a KEYS p.d.f. 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
67 x = w.var("x")
68 k = ROOT.RooKeysPdf("k", "k", x, data, ROOT.RooKeysPdf.NoMirror, 0.2)
69 w.Import(k, ROOT.RooFit.RenameAllNodes("workspace"))
70 
71 # Print workspace contents
72 w.Print()
73