%%cpp -d #include "RooRealVar.h" #include "RooDataSet.h" #include "RooDataHist.h" #include "RooGaussian.h" #include "RooFormulaVar.h" #include "RooGenericPdf.h" #include "RooPolynomial.h" #include "RooMinimizer.h" #include "TCanvas.h" #include "TAxis.h" #include "RooPlot.h" #include "RooFitResult.h" using namespace RooFit; RooRealVar x("x", "x", -10, 10); x.setBins(40); RooPolynomial p0("px", "px", x); std::unique_ptr data{p0.generate(x, 1000)}; RooFormulaVar wFunc("w", "event weight", "(x*x+10)", x); RooRealVar *w = (RooRealVar *)data->addColumn(wFunc); data->Print(); RooDataSet wdata(data->GetName(), data->GetTitle(), data.get(), *data->get(), 0, w->GetName()); wdata.Print(); RooRealVar a0("a0", "a0", 1); RooRealVar a1("a1", "a1", 0, -1, 1); RooRealVar a2("a2", "a2", 1, 0, 10); RooPolynomial p2("p2", "p2", x, RooArgList(a0, a1, a2), 0); std::unique_ptr r_ml_wgt{p2.fitTo(wdata, Save(), PrintLevel(-1))}; std::unique_ptr r_ml_wgt_corr{p2.fitTo(wdata, Save(), SumW2Error(true), PrintLevel(-1))}; RooPlot *frame = x.frame(Title("Unbinned ML fit, binned chi^2 fit to weighted data")); wdata.plotOn(frame, DataError(RooAbsData::SumW2)); p2.plotOn(frame); RooGenericPdf genPdf("genPdf", "x*x+10", x); std::unique_ptr data2{genPdf.generate(x, 1000)}; std::unique_ptr data3{genPdf.generate(x, 43000)}; std::unique_ptr r_ml_unw10{p2.fitTo(*data2, Save(), PrintLevel(-1))}; std::unique_ptr r_ml_unw43{p2.fitTo(*data3, Save(), PrintLevel(-1))}; std::unique_ptr binnedData{wdata.binnedClone()}; binnedData->Print("v"); std::unique_ptr chi2{ p2.createChi2(static_cast(*binnedData), DataError(RooAbsData::SumW2))}; RooMinimizer m(*chi2); m.migrad(); m.hesse(); std::unique_ptr r_chi2_wgt{m.save()}; p2.plotOn(frame, LineStyle(kDashed), LineColor(kRed)); cout << "==> ML Fit results on 1K unweighted events" << endl; r_ml_unw10->Print(); cout << "==> ML Fit results on 43K unweighted events" << endl; r_ml_unw43->Print(); cout << "==> ML Fit results on 1K weighted events with a summed weight of 43K" << endl; r_ml_wgt->Print(); cout << "==> Corrected ML Fit results on 1K weighted events with a summed weight of 43K" << endl; r_ml_wgt_corr->Print(); cout << "==> Chi2 Fit results on 1K weighted events with a summed weight of 43K" << endl; r_chi2_wgt->Print(); new TCanvas("rf403_weightedevts", "rf403_weightedevts", 600, 600); gPad->SetLeftMargin(0.15); frame->GetYaxis()->SetTitleOffset(1.8); frame->Draw(); %jsroot on gROOT->GetListOfCanvases()->Draw()