from __future__ import print_function
import ROOT
x = ROOT.RooRealVar("x", "x", -10, 10)
mx = ROOT.RooRealVar("mx", "mx", 0, -10, 10)
gx = ROOT.RooGaussian("gx", "gx", x, mx, ROOT.RooFit.RooConst(1))
px = ROOT.RooPolynomial("px", "px", x)
f = ROOT.RooRealVar("f", "f", 0., 1.)
model = ROOT.RooAddPdf(
"model", "model", ROOT.RooArgList(gx, px), ROOT.RooArgList(f))
modelData = model.generate(ROOT.RooArgSet(x), 10000)
r_full = model.fitTo(modelData, ROOT.RooFit.Save(ROOT.kTRUE))
x.setRange("signal", -3, 3)
r_sig = model.fitTo(modelData, ROOT.RooFit.Save(
ROOT.kTRUE), ROOT.RooFit.Range("signal"))
frame = x.frame(ROOT.RooFit.Title("Fitting a sub range"))
modelData.plotOn(frame)
model.plotOn(
frame, ROOT.RooFit.Range("Full"), ROOT.RooFit.LineStyle(
ROOT.kDashed), ROOT.RooFit.LineColor(
ROOT.kRed))
model.plotOn(frame)
print("result of fit on all data ")
r_full.Print()
print("result of fit in in signal region (note increased error on signal fraction)")
r_sig.Print()
c = ROOT.TCanvas("rf203_ranges", "rf203_ranges", 600, 600)
ROOT.gPad.SetLeftMargin(0.15)
frame.GetYaxis().SetTitleOffset(1.4)
frame.Draw()
c.SaveAs("rf203_ranges.png")