Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Basic functionality: Calculating chi^2 from histograms and curves in RooPlots, making
5/// histogram of residual and pull distributions
7/// \macro_image
8/// \macro_code
9/// \macro_output
11/// \date July 2008
12/// \author Wouter Verkerke
14#include "RooRealVar.h"
15#include "RooDataSet.h"
16#include "RooGaussian.h"
17#include "TCanvas.h"
18#include "TAxis.h"
19#include "RooPlot.h"
20#include "RooHist.h"
21using namespace RooFit;
26 // S e t u p m o d e l
27 // ---------------------
29 // Create observables
30 RooRealVar x("x", "x", -10, 10);
32 // Create Gaussian
33 RooRealVar sigma("sigma", "sigma", 3, 0.1, 10);
34 RooRealVar mean("mean", "mean", 0, -10, 10);
35 RooGaussian gauss("gauss", "gauss", x, 0.0, sigma);
37 // Generate a sample of 1000 events with sigma=3
38 std::unique_ptr<RooDataSet> data{gauss.generate(x, 10000)};
40 // Change sigma to 3.15
41 sigma.setVal(3.15);
43 // P l o t d a t a a n d s l i g h t l y d i s t o r t e d m o d e l
44 // ---------------------------------------------------------------------------
46 // Overlay projection of gauss with sigma=3.15 on data with sigma=3.0
47 RooPlot *frame1 = x.frame(Title("Data with distorted Gaussian pdf"), Bins(40));
48 data->plotOn(frame1, DataError(RooAbsData::SumW2));
49 gauss.plotOn(frame1);
51 // C a l c u l a t e c h i ^ 2
52 // ------------------------------
54 // Show the chi^2 of the curve w.r.t. the histogram
55 // If multiple curves or datasets live in the frame you can specify
56 // the name of the relevant curve and/or dataset in chiSquare()
57 cout << "chi^2 = " << frame1->chiSquare() << endl;
59 // S h o w r e s i d u a l a n d p u l l d i s t s
60 // -------------------------------------------------------
62 // Construct a histogram with the residuals of the data w.r.t. the curve
63 RooHist *hresid = frame1->residHist();
65 // Construct a histogram with the pulls of the data w.r.t the curve
66 RooHist *hpull = frame1->pullHist();
68 // Create a new frame to draw the residual distribution and add the distribution to the frame
69 RooPlot *frame2 = x.frame(Title("Residual Distribution"));
70 frame2->addPlotable(hresid, "P");
72 // Create a new frame to draw the pull distribution and add the distribution to the frame
73 RooPlot *frame3 = x.frame(Title("Pull Distribution"));
74 frame3->addPlotable(hpull, "P");
76 TCanvas *c = new TCanvas("rf109_chi2residpull", "rf109_chi2residpull", 900, 300);
77 c->Divide(3);
78 c->cd(1);
79 gPad->SetLeftMargin(0.15);
80 frame1->GetYaxis()->SetTitleOffset(1.6);
81 frame1->Draw();
82 c->cd(2);
83 gPad->SetLeftMargin(0.15);
84 frame2->GetYaxis()->SetTitleOffset(1.6);
85 frame2->Draw();
86 c->cd(3);
87 gPad->SetLeftMargin(0.15);
88 frame3->GetYaxis()->SetTitleOffset(1.6);
89 frame3->Draw();
#define c(i)
Definition RSha256.hxx:101
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gPad
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Graphical representation of binned data based on the TGraphAsymmErrors class.
Definition RooHist.h:29
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:237
double chiSquare(int nFitParam=0) const
Shortcut for RooPlot::chiSquare(const char* pdfname, const char* histname, int nFitParam=nullptr)
Definition RooPlot.h:176
TAxis * GetYaxis() const
Definition RooPlot.cxx:1276
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:649
RooHist * residHist(const char *histname=nullptr, const char *pdfname=nullptr, bool normalize=false, bool useAverage=true) const
Return a RooHist (derives from TGraphAsymErrors) containing the residuals of a histogram.
Definition RooPlot.cxx:1113
RooHist * pullHist(const char *histname=nullptr, const char *pdfname=nullptr, bool useAverage=true) const
Uses residHist() and sets normalize=true.
Definition RooPlot.h:181
void addPlotable(RooPlotable *plotable, Option_t *drawOptions="", bool invisible=false, bool refreshNorm=false)
Add the specified plotable object to our plot.
Definition RooPlot.cxx:528
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
RooCmdArg Bins(Int_t nbin)
RooCmdArg DataError(Int_t)
const Double_t sigma
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
const char * Title
Definition TXMLSetup.cxx:68