///////////////////////////////////////////////////////////////////////// // // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #203 // // Fitting and plotting in sub ranges // // // 07/2008 - Wouter Verkerke // ///////////////////////////////////////////////////////////////////////// #ifndef __CINT__ #include "RooGlobalFunc.h" #endif #include "RooRealVar.h" #include "RooDataSet.h" #include "RooGaussian.h" #include "RooExponential.h" #include "RooConstVar.h" #include "RooPolynomial.h" #include "RooBernstein.h" #include "RooAddPdf.h" #include "RooFitResult.h" #include "RooPlot.h" #include "TCanvas.h" #include "TAxis.h" #include "TH1.h" using namespace RooFit ; void rf203_ranges() { // S e t u p m o d e l // --------------------- // Construct observables x RooRealVar x("x","x",-10,10) ; //RooRealVar x("x","x",-3,3) ; // Make the x range the same as the signal range // Construct exponential(x,-0.1) RooExponential gx("gx","gx",x,RooConst(-0.1)) ; // Construct px = 1 (flat in x) RooRealVar a0("a0","a_{0}",1); RooRealVar a1("a1","a_{1}",0,1); RooBernstein px("px","px",x,RooArgList(a0,a1)) ; // Generated 10000 events in (x,y) from p.d.f. model RooDataSet* modelData = gx.generate(x,100000) ; // F i t f u l l r a n g e // --------------------------- // Fit p.d.f to all data RooFitResult* r_full = px.fitTo(*modelData,Save(kTRUE)) ; // F i t p a r t i a l r a n g e // ---------------------------------- // Define "signal" range in x as [-3,3] x.setRange("signal",-3,3) ; // Fit p.d.f only to data in "signal" range RooFitResult* r_sig = px.fitTo(*modelData,Save(kTRUE),Range("signal")) ; // P l o t / p r i n t r e s u l t s // --------------------------------------- // Make plot frame in x and add data and fitted model RooPlot* frame = x.frame(Title("Fitting a sub range")) ; modelData->plotOn(frame) ; px.plotOn(frame,Range("Full"),LineStyle(kDashed),LineColor(kRed)) ; // Add shape in full ranged dashed px.plotOn(frame) ; // By default only fitted range is shown // Print fit results cout << "result of fit on all data " << endl ; r_full->Print() ; cout << "result of fit in in signal region (note increased error on signal fraction)" << endl ; r_sig->Print() ; // Draw frame on canvas new TCanvas("rf203_ranges","rf203_ranges",600,600) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ; cout << "a0 = " << a0.getVal() << ", a1 = " << a1.getVal() << endl; return ; }