Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf609_xychi2fit.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Likelihood and minimization: setting up a chi^2 fit to an unbinned dataset with X,Y,err(Y) values (and optionally err(X) values)

#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooPolyVar.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "TRandom.h"
using namespace RooFit;
{
// C r e a t e d a t a s e t w i t h X a n d Y v a l u e s
// -------------------------------------------------------------------
// Make weighted XY dataset with asymmetric errors stored
// The StoreError() argument is essential as it makes
// the dataset store the error in addition to the values
// of the observables. If errors on one or more observables
// are asymmetric, one can store the asymmetric error
// using the StoreAsymError() argument
RooRealVar x("x", "x", -11, 11);
RooRealVar y("y", "y", -10, 200);
RooDataSet dxy("dxy", "dxy", {x, y}, StoreError({x, y}));
TRandom3 trnd{};
// Fill an example dataset with X,err(X),Y,err(Y) values
RooArgSet vars{x, y};
for (int i = 0; i <= 10; i++) {
// Set X value and error
x = -10 + 2 * i;
x.setError(i < 5 ? 0.5 / 1. : 1.0 / 1.);
// Set Y value and error
y = x.getVal() * x.getVal() + 4 * std::abs(trnd.Gaus());
y.setError(sqrt(y.getVal()));
dxy.add(vars);
}
// P e r f o r m c h i 2 f i t t o X + / - d x a n d Y + / - d Y v a l u e s
// ---------------------------------------------------------------------------------------
// Make fit function
RooRealVar a("a", "a", 0.0, -10, 10);
RooRealVar b("b", "b", 0.0, -100, 100);
RooRealVar c("c", "c", 0.0, -100, 100);
RooPolyVar f("f", "f", x, RooArgList(b, a, c));
// Plot dataset in X-Y interpretation
RooPlot *frame = x.frame(Title("Chi^2 fit of function set of (X#pmdX,Y#pmdY) values"));
dxy.plotOnXY(frame, YVar(y));
// Fit chi^2 using X and Y errors
std::unique_ptr<RooFitResult> fit1{f.chi2FitTo(dxy, YVar(y), Save(), PrintLevel(-1))};
fit1->Print();
// Overlay fitted function
f.plotOn(frame);
// Alternative: fit chi^2 integrating f(x) over ranges defined by X errors, rather
// than taking point at center of bin
std::unique_ptr<RooFitResult> fit2{f.chi2FitTo(dxy, YVar(y), Save(), PrintLevel(-1), Integrate(true))};
fit2->Print();
// Overlay alternate fit result
f.plotOn(frame, LineStyle(kDashed), LineColor(kRed));
// Draw the plot on a canvas
new TCanvas("rf609_xychi2fit", "rf609_xychi2fit", 600, 600);
gPad->SetLeftMargin(0.15);
frame->GetYaxis()->SetTitleOffset(1.4);
frame->Draw();
}
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
@ kRed
Definition Rtypes.h:66
@ kDashed
Definition TAttLine.h:48
#define gPad
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Container class to hold unbinned data.
Definition RooDataSet.h:34
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:185
TAxis * GetYaxis() const
Definition RooPlot.cxx:1224
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:597
A RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficients.
Definition RooPolyVar.h:25
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
The Canvas class.
Definition TCanvas.h:23
Random number generator class based on M.
Definition TRandom3.h:27
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg={})
RooCmdArg Save(bool flag=true)
RooCmdArg Integrate(bool flag)
RooCmdArg PrintLevel(Int_t code)
RooCmdArg LineColor(Color_t color)
RooCmdArg LineStyle(Style_t style)
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:64
Definition fit1.py:1
const char * Title
Definition TXMLSetup.cxx:68
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization
RooFitResult: minimized FCN value: 1.17354, estimated distance to minimum: 3.83787e-05
covariance matrix quality: Full, accurate covariance matrix
Status : MINIMIZE=0 HESSE=0
Floating Parameter FinalValue +/- Error
-------------------- --------------------------
a -6.4539e-02 +/- 5.85e-01
b 2.9760e-01 +/- 4.74e-01
c 1.0442e+00 +/- 9.06e-02
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization
RooFitResult: minimized FCN value: 2.81819, estimated distance to minimum: 5.77434e-06
covariance matrix quality: Full, accurate covariance matrix
Status : MINIMIZE=0 HESSE=0
Floating Parameter FinalValue +/- Error
-------------------- --------------------------
a -5.5529e-02 +/- 3.37e-01
b 3.6741e-02 +/- 4.64e-01
c 1.0407e+00 +/- 4.98e-02
Date
July 2008
Author
Wouter Verkerke

Definition in file rf609_xychi2fit.C.