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