Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_fit
3/// \notebook
4/// Illustrates TVirtualFitter::GetConfidenceIntervals
5/// This method computes confidence intervals for the fitted function
7/// \macro_image
8/// \macro_code
10/// \author Rene Brun
12#include "TGraphErrors.h"
13#include "TGraph2DErrors.h"
14#include "TCanvas.h"
15#include "TF2.h"
16#include "TH1.h"
17#include "TVirtualFitter.h"
18#include "TRandom.h"
20void ConfidenceIntervals()
22 TCanvas *myc = new TCanvas("myc",
23 "Confidence intervals on the fitted function",1000, 500);
24 myc->Divide(3,1);
26//### 1. A graph
27 //Create and fill a graph
28 int ngr = 100;
29 TGraph *gr = new TGraph(ngr);
30 gr->SetName("GraphNoError");
31 double x, y;
32 int i;
33 for (i=0; i<ngr; i++){
34 x = gRandom->Uniform(-1, 1);
35 y = -1 + 2*x + gRandom->Gaus(0, 1);
36 gr->SetPoint(i, x, y);
37 }
38 //Create the fitting function
39 TF1 *fpol = new TF1("fpol", "pol1", -1, 1);
40 fpol->SetLineWidth(2);
41 gr->Fit(fpol, "Q");
43 /*Create a TGraphErrors to hold the confidence intervals*/
44 TGraphErrors *grint = new TGraphErrors(ngr);
45 grint->SetTitle("Fitted line with .95 conf. band");
46 for (i=0; i<ngr; i++)
47 grint->SetPoint(i, gr->GetX()[i], 0);
48 /*Compute the confidence intervals at the x points of the created graph*/
50 //Now the "grint" graph contains function values as its y-coordinates
51 //and confidence intervals as the errors on these coordinates
52 //Draw the graph, the function and the confidence intervals
53 myc->cd(1);
54 grint->SetLineColor(kRed);
55 grint->Draw("ap");
57 gr->SetMarkerSize(0.7);
58 gr->Draw("psame");
60//### 2. A histogram
61 myc->cd(2);
62 //Create, fill and fit a histogram
63 int nh=5000;
64 TH1D *h = new TH1D("h",
65 "Fitted Gaussian with .95 conf.band", 100, -3, 3);
66 h->FillRandom("gaus", nh);
67 TF1 *f = new TF1("fgaus", "gaus", -3, 3);
68 f->SetLineWidth(2);
69 h->Fit(f, "Q");
70 h->Draw();
72 /*Create a histogram to hold the confidence intervals*/
73 TH1D *hint = new TH1D("hint",
74 "Fitted Gaussian with .95 conf.band", 100, -3, 3);
76 //Now the "hint" histogram has the fitted function values as the
77 //bin contents and the confidence intervals as bin errors
78 hint->SetStats(false);
79 hint->SetFillColor(2);
80 hint->Draw("e3 same");
82//### 3. A 2d graph
83 //Create and fill the graph
84 int ngr2 = 100;
85 double z, rnd, e=0.3;
86 TGraph2D *gr2 = new TGraph2D(ngr2);
87 gr2->SetName("Graph2DNoError");
88 TF2 *f2 = new TF2("f2",
89 "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+250",-6,6,-6,6);
90 f2->SetParameters(1,1);
91 for (i=0; i<ngr2; i++){
92 f2->GetRandom2(x,y);
93 // Generate a random number in [-e,e]
94 rnd = 2*gRandom->Rndm()*e-e;
95 z = f2->Eval(x,y)*(1+rnd);
96 gr2->SetPoint(i,x,y,z);
97 }
98 //Create a graph with errors to store the intervals
99 TGraph2DErrors *grint2 = new TGraph2DErrors(ngr2);
100 for (i=0; i<ngr2; i++)
101 grint2->SetPoint(i, gr2->GetX()[i], gr2->GetY()[i], 0);
103 //Fit the graph
104 f2->SetParameters(0.5,1.5);
105 gr2->Fit(f2, "Q");
106 /*Compute the confidence intervals*/
108 //Now the "grint2" graph contains function values as z-coordinates
109 //and confidence intervals as their errors
110 //draw
111 myc->cd(3);
112 f2->SetNpx(30);
113 f2->SetNpy(30);
114 f2->SetFillColor(kBlue);
115 f2->Draw("surf4");
116 grint2->SetNpx(20);
117 grint2->SetNpy(20);
118 grint2->SetMarkerStyle(24);
119 grint2->SetMarkerSize(0.7);
120 grint2->SetMarkerColor(kRed);
121 grint2->SetLineColor(kRed);
122 grint2->Draw("E0 same");
123 grint2->SetTitle("Fitted 2d function with .95 error bars");
125 myc->cd();
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:45
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:716
1-Dim function class
Definition TF1.h:214
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition TF1.cxx:3433
virtual void SetParameters(const Double_t *params)
Definition TF1.h:650
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
Evaluate this function.
Definition TF1.cxx:1441
A 2-Dim function with parameters.
Definition TF2.h:29
virtual void GetRandom2(Double_t &xrandom, Double_t &yrandom, TRandom *rng=nullptr)
Return 2 random numbers following this function shape.
Definition TF2.cxx:547
virtual void SetNpy(Int_t npy=100)
Set the number of points used to draw the function.
Definition TF2.cxx:927
void Draw(Option_t *option="") override
Draw this function with its current attributes.
Definition TF2.cxx:259
Graph 2D class with errors.
void SetPoint(Int_t i, Double_t x, Double_t y, Double_t z) override
Set x, y and z values for point number i.
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition TGraph2D.h:41
Double_t * GetY() const
Definition TGraph2D.h:122
Double_t * GetX() const
Definition TGraph2D.h:121
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="")
Fits this graph with function with name fname Predefined functions such as gaus, expo and poln are au...
Definition TGraph2D.cxx:791
void SetName(const char *name) override
Changes the name of this 2D graph.
void SetNpy(Int_t npx=40)
Sets the number of bins along Y used to draw the function.
void SetTitle(const char *title="") override
Sets the 2D graph title.
virtual void SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)
Sets point number n.
void Draw(Option_t *option="P0") override
Specific drawing options can be used to paint a TGraph2D:
Definition TGraph2D.cxx:738
void SetNpx(Int_t npx=40)
Sets the number of bins along X used to draw the function.
A TGraphErrors is a TGraph with error bars.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2315
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Axis_t xmin=0, Axis_t xmax=0)
Fit this graph with function with name fname.
Definition TGraph.cxx:1228
Double_t * GetX() const
Definition TGraph.h:137
void SetName(const char *name="") override
Set graph name.
Definition TGraph.cxx:2354
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:809
void SetTitle(const char *title="") override
Change (i.e.
Definition TGraph.cxx:2370
1-D histogram with a double per channel (see TH1 documentation)}
Definition TH1.h:620
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3067
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:8907
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1153
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
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:552
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:672
static TVirtualFitter * GetFitter()
static: return the current Fitter
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
TGraphErrors * gr
Definition legend1.C:25
bool GetConfidenceIntervals(const TH1 *h1, const ROOT::Fit::FitResult &r, TGraphErrors *gr, double cl=0.95)
compute confidence intervals at level cl for a fitted histogram h1 in a TGraphErrors gr