from __future__ import print_function
import ROOT
x = ROOT.RooRealVar("x", "x", -10, 10)
m = ROOT.RooRealVar("m", "m", 0, -10, 10)
s = ROOT.RooRealVar("s", "s", 2, 0.1, 10)
gauss = ROOT.RooGaussian("gauss", "gauss(x,m,s)", x, m, s)
poly = ROOT.RooPolynomial("poly", "poly(x)", x)
f = ROOT.RooRealVar("f", "f", 0.5, 0., 1.)
model = ROOT.RooAddPdf(
"model",
"model",
ROOT.RooArgList(
gauss,
poly),
ROOT.RooArgList(f))
d = model.generate(ROOT.RooArgSet(x), 50)
fconstraint = ROOT.RooGaussian(
"fconstraint",
"fconstraint",
f,
ROOT.RooFit.RooConst(0.8),
ROOT.RooFit.RooConst(0.1))
modelc = ROOT.RooProdPdf(
"modelc", "model with constraint", ROOT.RooArgList(model, fconstraint))
r1 = model.fitTo(d, ROOT.RooFit.Save())
r2 = modelc.fitTo(
d,
ROOT.RooFit.Constrain(
ROOT.RooArgSet(f)),
ROOT.RooFit.Save())
fconstext = ROOT.RooGaussian("fconstext", "fconstext", f, ROOT.RooFit.RooConst(
0.2), ROOT.RooFit.RooConst(0.1))
r3 = model.fitTo(d, ROOT.RooFit.ExternalConstraints(
ROOT.RooArgSet(fconstext)), ROOT.RooFit.Save())
print("fit result without constraint (data generated at f=0.5)")
r1.Print("v")
print("fit result with internal constraint (data generated at f=0.5, is f=0.8+/-0.2)")
r2.Print("v")
print("fit result with (another) external constraint (data generated at f=0.5, is f=0.2+/-0.1)")
r3.Print("v")