Logo ROOT   6.18/05
Reference Guide
ConfidenceIntervals.C File Reference

Detailed Description

View in nbviewer Open in SWAN Illustrates TVirtualFitter::GetConfidenceIntervals This method computes confidence intervals for the fitted function

#include "TGraphErrors.h"
#include "TGraph2DErrors.h"
#include "TCanvas.h"
#include "TF2.h"
#include "TH1.h"
#include "TVirtualFitter.h"
#include "TRandom.h"
void ConfidenceIntervals()
{
TCanvas *myc = new TCanvas("myc",
"Confidence intervals on the fitted function",1200, 500);
myc->Divide(3,1);
//### 1. A graph
//Create and fill a graph
Int_t ngr = 100;
TGraph *gr = new TGraph(ngr);
gr->SetName("GraphNoError");
Int_t i;
for (i=0; i<ngr; i++){
x = gRandom->Uniform(-1, 1);
y = -1 + 2*x + gRandom->Gaus(0, 1);
gr->SetPoint(i, x, y);
}
//Create the fitting function
TF1 *fpol = new TF1("fpol", "pol1", -1, 1);
fpol->SetLineWidth(2);
gr->Fit(fpol, "Q");
/*Create a TGraphErrors to hold the confidence intervals*/
TGraphErrors *grint = new TGraphErrors(ngr);
grint->SetTitle("Fitted line with .95 conf. band");
for (i=0; i<ngr; i++)
grint->SetPoint(i, gr->GetX()[i], 0);
/*Compute the confidence intervals at the x points of the created graph*/
//Now the "grint" graph contains function values as its y-coordinates
//and confidence intervals as the errors on these coordinates
//Draw the graph, the function and the confidence intervals
myc->cd(1);
grint->SetLineColor(kRed);
grint->Draw("ap");
gr->Draw("psame");
//### 2. A histogram
myc->cd(2);
//Create, fill and fit a histogram
Int_t nh=5000;
TH1D *h = new TH1D("h",
"Fitted gaussian with .95 conf.band", 100, -3, 3);
h->FillRandom("gaus", nh);
TF1 *f = new TF1("fgaus", "gaus", -3, 3);
f->SetLineWidth(2);
h->Fit(f, "Q");
h->Draw();
/*Create a histogram to hold the confidence intervals*/
TH1D *hint = new TH1D("hint",
"Fitted gaussian with .95 conf.band", 100, -3, 3);
//Now the "hint" histogram has the fitted function values as the
//bin contents and the confidence intervals as bin errors
hint->SetStats(kFALSE);
hint->SetFillColor(2);
hint->Draw("e3 same");
//### 3. A 2d graph
//Create and fill the graph
Int_t ngr2 = 100;
Double_t z, rnd, e=0.3;
TGraph2D *gr2 = new TGraph2D(ngr2);
gr2->SetName("Graph2DNoError");
TF2 *f2 = new TF2("f2",
"1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+250",-6,6,-6,6);
f2->SetParameters(1,1);
for (i=0; i<ngr2; i++){
f2->GetRandom2(x,y);
// Generate a random number in [-e,e]
rnd = 2*gRandom->Rndm()*e-e;
z = f2->Eval(x,y)*(1+rnd);
gr2->SetPoint(i,x,y,z);
}
//Create a graph with errors to store the intervals
TGraph2DErrors *grint2 = new TGraph2DErrors(ngr2);
for (i=0; i<ngr2; i++)
grint2->SetPoint(i, gr2->GetX()[i], gr2->GetY()[i], 0);
//Fit the graph
f2->SetParameters(0.5,1.5);
gr2->Fit(f2, "Q");
/*Compute the confidence intervals*/
//Now the "grint2" graph contains function values as z-coordinates
//and confidence intervals as their errors
//draw
myc->cd(3);
f2->SetNpx(30);
f2->SetNpy(30);
f2->Draw("surf4");
grint2->SetNpx(20);
grint2->SetNpy(20);
grint2->SetMarkerStyle(24);
grint2->SetMarkerSize(0.7);
grint2->SetMarkerColor(kRed);
grint2->SetLineColor(kRed);
grint2->Draw("E0 same");
grint2->SetTitle("Fitted 2d function with .95 error bars");
myc->cd();
}
#define f(i)
Definition: RSha256.hxx:104
#define h(i)
Definition: RSha256.hxx:106
#define e(i)
Definition: RSha256.hxx:103
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
double Double_t
Definition: RtypesCore.h:55
@ kRed
Definition: Rtypes.h:64
@ kBlue
Definition: Rtypes.h:64
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:41
The Canvas class.
Definition: TCanvas.h:31
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:693
1-Dim function class
Definition: TF1.h:211
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition: TF1.cxx:3432
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:638
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:1429
A 2-Dim function with parameters.
Definition: TF2.h:29
virtual void SetNpy(Int_t npy=100)
Set the number of points used to draw the function.
Definition: TF2.cxx:932
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:241
virtual void GetRandom2(Double_t &xrandom, Double_t &yrandom)
Return 2 random numbers following this function shape.
Definition: TF2.cxx:529
Graph 2D class with errors.
virtual void SetPoint(Int_t i, Double_t x, Double_t y, Double_t z)
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:40
Double_t * GetY() const
Definition: TGraph2D.h:120
Double_t * GetX() const
Definition: TGraph2D.h:119
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:760
void SetNpy(Int_t npx=40)
Sets the number of bins along Y used to draw the function.
Definition: TGraph2D.cxx:1712
virtual void SetTitle(const char *title="")
Sets the 2D graph title.
Definition: TGraph2D.cxx:1783
virtual void SetName(const char *name)
Changes the name of this 2D graph.
Definition: TGraph2D.cxx:1662
virtual void SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)
Sets point number n.
Definition: TGraph2D.cxx:1736
virtual void Draw(Option_t *option="P0")
Specific drawing options can be used to paint a TGraph2D:
Definition: TGraph2D.cxx:707
void SetNpx(Int_t npx=40)
Sets the number of bins along X used to draw the function.
Definition: TGraph2D.cxx:1690
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:26
A Graph is a graphics 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:2198
virtual void SetName(const char *name="")
Set graph name.
Definition: TGraph.cxx:2221
virtual void SetTitle(const char *title="")
Change (i.e.
Definition: TGraph.cxx:2237
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:1051
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:747
Double_t * GetX() const
Definition: TGraph.h:130
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:614
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2981
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8404
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1166
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
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:635
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:541
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
Author
Rene Brun

Definition in file ConfidenceIntervals.C.