16from __future__
import print_function
24x = ROOT.RooRealVar(
"x",
"x", -10, 10)
28p0 = ROOT.RooPolynomial(
"px",
"px", x)
31data = p0.generate({x}, 1000)
37wFunc = ROOT.RooFormulaVar(
"w",
"event weight",
"(x*x+10)", [x])
40w = data.addColumn(wFunc)
47wdata = ROOT.RooDataSet(data.GetName(), data.GetTitle(), data, data.get(),
"", w.GetName())
57a0 = ROOT.RooRealVar(
"a0",
"a0", 1)
58a1 = ROOT.RooRealVar(
"a1",
"a1", 0, -1, 1)
59a2 = ROOT.RooRealVar(
"a2",
"a2", 1, 0, 10)
60p2 = ROOT.RooPolynomial(
"p2",
"p2", x, [a0, a1, a2], 0)
69r_ml_wgt = p2.fitTo(wdata, Save=
True, PrintLevel=-1)
84r_ml_wgt_corr = p2.fitTo(wdata, Save=
True, SumW2Error=
True, PrintLevel=-1)
90frame = x.frame(Title=
"Unbinned ML fit, chi^2 fit to weighted data")
93wdata.plotOn(frame, DataError=
"SumW2")
102genPdf = ROOT.RooGenericPdf(
"genPdf",
"x*x+10", [x])
105data2 = genPdf.generate({x}, 1000)
108data3 = genPdf.generate({x}, 43000)
112r_ml_unw10 = p2.fitTo(data2, Save=
True, PrintLevel=-1)
113r_ml_unw43 = p2.fitTo(data3, Save=
True, PrintLevel=-1)
119binnedData = wdata.binnedClone()
127chi2 = p2.createChi2(binnedData, ROOT.RooFit.DataError(
"SumW2"))
128m = ROOT.RooMinimizer(chi2)
134p2.plotOn(frame, LineStyle=
"--", LineColor=
"r")
143print(
"==> ML Fit results on 1K unweighted events")
145print(
"==> ML Fit results on 43K unweighted events")
147print(
"==> ML Fit results on 1K weighted events with a summed weight of 43K")
149print(
"==> Corrected ML Fit results on 1K weighted events with a summed weight of 43K")
151print(
"==> Chi2 Fit results on 1K weighted events with a summed weight of 43K")
154c = ROOT.TCanvas(
"rf403_weightedevts",
"rf403_weightedevts", 600, 600)
155ROOT.gPad.SetLeftMargin(0.15)
156frame.GetYaxis().SetTitleOffset(1.8)
159c.SaveAs(
"rf403_weightedevts.png")