Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf502_wspacewrite.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook -nodraw
4## Organization and simultaneous fits: creating and writing a workspace
5##
6## \macro_code
7## \macro_output
8##
9## \date February 2018
10## \authors Clemens Lange, Wouter Verkerke (C++ version)
11
12import ROOT
13
14
15# Create model and dataset
16# -----------------------------------------------
17
18# Declare observable x
19x = ROOT.RooRealVar("x", "x", 0, 10)
20
21# Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
22# their parameters
23mean = ROOT.RooRealVar("mean", "mean of gaussians", 5, 0, 10)
24sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
25sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
26
27sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
28sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
29
30# Build Chebychev polynomial pdf
31a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0.0, 1.0)
32a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0.0, 1.0)
33bkg = ROOT.RooChebychev("bkg", "Background", x, [a0, a1])
34
35# Sum the signal components into a composite signal pdf
36sig1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0.0, 1.0)
37sig = ROOT.RooAddPdf("sig", "Signal", [sig1, sig2], [sig1frac])
38
39# Sum the composite signal and background
40bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0.0, 1.0)
41model = ROOT.RooAddPdf("model", "g1+g2+a", [bkg, sig], [bkgfrac])
42
43# Generate a data sample of 1000 events in x from model
44data = model.generate({x}, 1000)
45
46# Create workspace, import data and model
47# -----------------------------------------------------------------------------
48
49# Create a empty workspace
50w = ROOT.RooWorkspace("w", "workspace")
51
52# Import model and all its components into the workspace
53w.Import(model)
54
55# Import data into the workspace
56w.Import(data)
57
58# Print workspace contents
59w.Print()
60
61# Save workspace in file
62# -------------------------------------------
63
64# Save the workspace into a ROOT file
65w.writeToFile("rf502_workspace_py.root")