Instantly share code, notes, and snippets.

# petrstepanov/rf403_weightedevts_modified.C

Last active March 12, 2019 22:01
Show Gist options
• Save petrstepanov/132701b0c27f319fc326ef2d1e2bddde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 /// \file /// \ingroup tutorial_roofit /// \notebook -js /// 'DATA AND CATEGORIES' RooFit tutorial macro #403 /// /// Using weights in unbinned datasets /// /// \macro_image /// \macro_output /// \macro_code /// \author 07/2008 - Wouter Verkerke #include "RooRealVar.h" #include "RooDataSet.h" #include "RooDataHist.h" #include "RooGaussian.h" #include "RooConstVar.h" #include "RooFormulaVar.h" #include "RooGenericPdf.h" #include "RooPolynomial.h" #include "RooChi2Var.h" #include "RooMinimizer.h" #include "TCanvas.h" #include "TAxis.h" #include "RooPlot.h" #include "RooFitResult.h" using namespace RooFit ; void rf403_weightedevts() { // C r e a t e o b s e r v a b l e a n d u n w e i g h t e d d a t a s e t // ------------------------------------------------------------------------------- // Declare observable RooRealVar x("x","x",-10,10) ; x.setBins(40) ; x.setRange("LEFT",-10,-6); x.setRange("RIGHT",6,10); // Construction quadratic polynomial pdf for fitting RooRealVar a0("a0","a0",1) ; RooRealVar a1("a1","a1",0,-1,1) ; RooRealVar a2("a2","a2",1,0,10) ; RooPolynomial p1("p1","p1",x,RooArgList(a0,a1,a2),0) ; RooRealVar b0("b0","b0",1) ; RooRealVar b1("b1","b1",0,-1,1) ; RooRealVar b2("b2","b2",1,0,10) ; RooPolynomial p2("p2","p2",x,RooArgList(b0,b1,b2),0) ; RooRealVar c0("c0","c0",1) ; RooRealVar c1("c1","c1",0,-1,1) ; RooRealVar c2("c2","c2",1,0,10) ; RooPolynomial p3("p3","p3",x,RooArgList(c0,c1,c2),0) ; // Sample 1000 events from pdf RooDataHist* data = p1.generateBinned(x,1000) ; // Construct plot frame RooPlot* frame1 = x.frame(Title("RooChi2Var(Range(\"LEFT,RIGHT\") and RooMinimizer()")) ; RooPlot* frame2 = x.frame(Title("chi2FitTo(Range(\"LEFT,RIGHT\")")) ; RooPlot* frame3 = x.frame(Title("fitTo(Range(\"LEFT,RIGHT\")")) ; // Fit and plot first frame RooChi2Var chi2("chi2","chi2",p1,*data,Range("LEFT,RIGHT")) ; RooMinimizer m(chi2) ; m.migrad() ; m.hesse() ; data->plotOn(frame1) ; p1.plotOn(frame1,LineStyle(kDashed),LineColor(kRed), NormRange("LEFT,RIGHT")) ; // Fit and plot second frame p2.chi2FitTo(*data, Range("LEFT,RIGHT")); data->plotOn(frame2) ; p2.plotOn(frame2,LineStyle(kDashed),LineColor(kRed), NormRange("LEFT,RIGHT")) ; // Fit and plot third frame p3.fitTo(*data, Range("LEFT,RIGHT")); data->plotOn(frame3) ; p3.plotOn(frame3,LineStyle(kDashed),LineColor(kRed), NormRange("LEFT,RIGHT")) ; // Draw TCanvas* c = new TCanvas("rf403_weightedevts","rf403_weightedevts",1200,400) ; c->Divide(3); c->cd(1); frame1->Draw() ; c->cd(2); frame2->Draw() ; c->cd(3); frame3->Draw() ; }