Logo ROOT   6.14/05
Reference Guide
ConfidenceIntervals.C
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
6 ///
7 /// \macro_image
8 /// \macro_output
9 /// \macro_code
10 ///
11 /// \author Rene Brun
12 
13 #include "TGraphErrors.h"
14 #include "TGraph2DErrors.h"
15 #include "TCanvas.h"
16 #include "TF2.h"
17 #include "TH1.h"
18 #include "TVirtualFitter.h"
19 #include "TRandom.h"
20 
21 void ConfidenceIntervals()
22 {
23  TCanvas *myc = new TCanvas("myc",
24  "Confidence intervals on the fitted function",1200, 500);
25  myc->Divide(3,1);
26 
27 //### 1. A graph
28  //Create and fill a graph
29  Int_t ngr = 100;
30  TGraph *gr = new TGraph(ngr);
31  gr->SetName("GraphNoError");
32  Double_t x, y;
33  Int_t i;
34  for (i=0; i<ngr; i++){
35  x = gRandom->Uniform(-1, 1);
36  y = -1 + 2*x + gRandom->Gaus(0, 1);
37  gr->SetPoint(i, x, y);
38  }
39  //Create the fitting function
40  TF1 *fpol = new TF1("fpol", "pol1", -1, 1);
41  fpol->SetLineWidth(2);
42  gr->Fit(fpol, "Q");
43 
44  /*Create a TGraphErrors to hold the confidence intervals*/
45  TGraphErrors *grint = new TGraphErrors(ngr);
46  grint->SetTitle("Fitted line with .95 conf. band");
47  for (i=0; i<ngr; i++)
48  grint->SetPoint(i, gr->GetX()[i], 0);
49  /*Compute the confidence intervals at the x points of the created graph*/
51  //Now the "grint" graph contains function values as its y-coordinates
52  //and confidence intervals as the errors on these coordinates
53  //Draw the graph, the function and the confidence intervals
54  myc->cd(1);
55  grint->SetLineColor(kRed);
56  grint->Draw("ap");
57  gr->SetMarkerStyle(5);
58  gr->SetMarkerSize(0.7);
59  gr->Draw("psame");
60 
61 //### 2. A histogram
62  myc->cd(2);
63  //Create, fill and fit a histogram
64  Int_t nh=5000;
65  TH1D *h = new TH1D("h",
66  "Fitted gaussian with .95 conf.band", 100, -3, 3);
67  h->FillRandom("gaus", nh);
68  TF1 *f = new TF1("fgaus", "gaus", -3, 3);
69  f->SetLineWidth(2);
70  h->Fit(f, "Q");
71  h->Draw();
72 
73  /*Create a histogram to hold the confidence intervals*/
74  TH1D *hint = new TH1D("hint",
75  "Fitted gaussian with .95 conf.band", 100, -3, 3);
77  //Now the "hint" histogram has the fitted function values as the
78  //bin contents and the confidence intervals as bin errors
79  hint->SetStats(kFALSE);
80  hint->SetFillColor(2);
81  hint->Draw("e3 same");
82 
83 //### 3. A 2d graph
84  //Create and fill the graph
85  Int_t ngr2 = 100;
86  Double_t z, rnd, e=0.3;
87  TGraph2D *gr2 = new TGraph2D(ngr2);
88  gr2->SetName("Graph2DNoError");
89  TF2 *f2 = new TF2("f2",
90  "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+250",-6,6,-6,6);
91  f2->SetParameters(1,1);
92  for (i=0; i<ngr2; i++){
93  f2->GetRandom2(x,y);
94  // Generate a random number in [-e,e]
95  rnd = 2*gRandom->Rndm()*e-e;
96  z = f2->Eval(x,y)*(1+rnd);
97  gr2->SetPoint(i,x,y,z);
98  }
99  //Create a graph with errors to store the intervals
100  TGraph2DErrors *grint2 = new TGraph2DErrors(ngr2);
101  for (i=0; i<ngr2; i++)
102  grint2->SetPoint(i, gr2->GetX()[i], gr2->GetY()[i], 0);
103 
104  //Fit the graph
105  f2->SetParameters(0.5,1.5);
106  gr2->Fit(f2, "Q");
107  /*Compute the confidence intervals*/
109  //Now the "grint2" graph contains function values as z-coordinates
110  //and confidence intervals as their errors
111  //draw
112  myc->cd(3);
113  f2->SetNpx(30);
114  f2->SetNpy(30);
115  f2->SetFillColor(kBlue);
116  f2->Draw("surf4");
117  grint2->SetNpx(20);
118  grint2->SetNpy(20);
119  grint2->SetMarkerStyle(24);
120  grint2->SetMarkerSize(0.7);
121  grint2->SetMarkerColor(kRed);
122  grint2->SetLineColor(kRed);
123  grint2->Draw("E0 same");
124  grint2->SetTitle("Fitted 2d function with .95 error bars");
125 
126  myc->cd();
127 
128 }
129 
130 
131 
132 
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:1050
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:628
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:241
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition: TF1.cxx:3339
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:256
Definition: Rtypes.h:59
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
virtual void Draw(Option_t *option="P0")
Specific drawing options can be used to paint a TGraph2D:
Definition: TGraph2D.cxx:704
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2216
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:757
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
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 ...
Double_t x[n]
Definition: legend1.C:17
virtual void SetName(const char *name="")
Set graph name.
Definition: TGraph.cxx:2207
virtual void SetTitle(const char *title="")
Sets the 2D graph title.
Definition: TGraph2D.cxx:1753
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:533
virtual void SetName(const char *name)
Changes the name of this 2D graph.
Definition: TGraph2D.cxx:1634
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3421
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetNpy(Int_t npy=100)
Set the number of points used to draw the function.
Definition: TF2.cxx:932
static TVirtualFitter * GetFitter()
static: return the current Fitter
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
A 2-Dim function with parameters.
Definition: TF2.h:29
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:610
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition: TAttMarker.h:41
TGraphErrors * gr
Definition: legend1.C:25
#define h(i)
Definition: RSha256.hxx:106
const Bool_t kFALSE
Definition: RtypesCore.h:88
Double_t * GetY() const
Definition: TGraph2D.h:119
Double_t * GetX() const
Definition: TGraph.h:129
The Canvas class.
Definition: TCanvas.h:31
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:1336
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.
virtual void SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)
Sets point number n.
Definition: TGraph2D.cxx:1706
double Double_t
Definition: RtypesCore.h:55
virtual void GetRandom2(Double_t &xrandom, Double_t &yrandom)
Return 2 random numbers following this function shape.
Definition: TF2.cxx:529
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:627
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2184
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:1162
1-Dim function class
Definition: TF1.h:211
void SetNpy(Int_t npx=40)
Sets the number of bins along Y used to draw the function.
Definition: TGraph2D.cxx:1683
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:26
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:284
Definition: Rtypes.h:59
void SetNpx(Int_t npx=40)
Sets the number of bins along X used to draw the function.
Definition: TGraph2D.cxx:1662
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition: TGraph2D.h:40
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3695
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8284
Double_t * GetX() const
Definition: TGraph2D.h:118
Graph 2D class with errors.