Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf609_xychi2fit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Likelihood and minimization: setting up a chi^2 fit to an unbinned dataset with X,Y,err(Y)
5/// values (and optionally err(X) values)
6///
7/// \macro_image
8/// \macro_output
9/// \macro_code
10///
11/// \date July 2008
12/// \author Wouter Verkerke
13
14#include "RooRealVar.h"
15#include "RooDataSet.h"
16#include "RooPolyVar.h"
17#include "RooChi2Var.h"
18#include "TCanvas.h"
19#include "TAxis.h"
20#include "RooPlot.h"
21#include "TRandom.h"
22
23using namespace RooFit;
24
25void rf609_xychi2fit()
26{
27 // 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
28 // -------------------------------------------------------------------
29
30 // Make weighted XY dataset with asymmetric errors stored
31 // The StoreError() argument is essential as it makes
32 // the dataset store the error in addition to the values
33 // of the observables. If errors on one or more observables
34 // are asymmetric, one can store the asymmetric error
35 // using the StoreAsymError() argument
36
37 RooRealVar x("x", "x", -11, 11);
38 RooRealVar y("y", "y", -10, 200);
39 RooDataSet dxy("dxy", "dxy", RooArgSet(x, y), StoreError(RooArgSet(x, y)));
40
41 // Fill an example dataset with X,err(X),Y,err(Y) values
42 for (int i = 0; i <= 10; i++) {
43
44 // Set X value and error
45 x = -10 + 2 * i;
46 x.setError(i < 5 ? 0.5 / 1. : 1.0 / 1.);
47
48 // Set Y value and error
49 y = x.getVal() * x.getVal() + 4 * fabs(gRandom->Gaus());
50 y.setError(sqrt(y.getVal()));
51
52 dxy.add(RooArgSet(x, y));
53 }
54
55 // 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
56 // ---------------------------------------------------------------------------------------
57
58 // Make fit function
59 RooRealVar a("a", "a", 0.0, -10, 10);
60 RooRealVar b("b", "b", 0.0, -100, 100);
61 RooPolyVar f("f", "f", x, RooArgList(b, a, 1.0));
62
63 // Plot dataset in X-Y interpretation
64 RooPlot *frame = x.frame(Title("Chi^2 fit of function set of (X#pmdX,Y#pmdY) values"));
65 dxy.plotOnXY(frame, YVar(y));
66
67 // Fit chi^2 using X and Y errors
68 f.chi2FitTo(dxy, YVar(y));
69
70 // Overlay fitted function
71 f.plotOn(frame);
72
73 // Alternative: fit chi^2 integrating f(x) over ranges defined by X errors, rather
74 // than taking point at center of bin
75 f.chi2FitTo(dxy, YVar(y), Integrate(kTRUE));
76
77 // Overlay alternate fit result
78 f.plotOn(frame, LineStyle(kDashed), LineColor(kRed));
79
80 // Draw the plot on a canvas
81 new TCanvas("rf609_xychi2fit", "rf609_xychi2fit", 600, 600);
82 gPad->SetLeftMargin(0.15);
83 frame->GetYaxis()->SetTitleOffset(1.4);
84 frame->Draw();
85}
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define a(i)
Definition RSha256.hxx:99
const Bool_t kTRUE
Definition RtypesCore.h:100
@ kRed
Definition Rtypes.h:66
@ kDashed
Definition TAttLine.h:48
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#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:35
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:36
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:44
TAxis * GetYaxis() const
Definition RooPlot.cxx:1278
static RooPlot * frame(const RooAbsRealLValue &var, Double_t xmin, Double_t xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:249
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:661
Class RooPolyVar is a RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficie...
Definition RooPolyVar.h:28
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:302
The Canvas class.
Definition TCanvas.h:23
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:274
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg=RooCmdArg::none())
RooCmdArg StoreError(const RooArgSet &aset)
RooCmdArg Integrate(Bool_t flag)
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)
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition Common.h:18
const char * Title
Definition TXMLSetup.cxx:68