ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ConfidenceIntervals.C
Go to the documentation of this file.
1 
2 #include "TGraphErrors.h"
3 #include "TGraph2DErrors.h"
4 #include "TCanvas.h"
5 #include "TF2.h"
6 #include "TH1.h"
7 #include "TVirtualFitter.h"
8 #include "TRandom.h"
9 
11 {
12 //Illustrates TVirtualFitter::GetConfidenceIntervals
13 //This method computes confidence intervals for the fitted function
14 //Author: Anna Kreshuk
15 
16  TCanvas *myc = new TCanvas("myc",
17  "Confidence intervals on the fitted function",1200, 500);
18  myc->Divide(3,1);
19 
20 /////1. A graph
21  //Create and fill a graph
22  Int_t ngr = 100;
23  TGraph *gr = new TGraph(ngr);
24  gr->SetName("GraphNoError");
25  Double_t x, y;
26  Int_t i;
27  for (i=0; i<ngr; i++){
28  x = gRandom->Uniform(-1, 1);
29  y = -1 + 2*x + gRandom->Gaus(0, 1);
30  gr->SetPoint(i, x, y);
31  }
32  //Create the fitting function
33  TF1 *fpol = new TF1("fpol", "pol1", -1, 1);
34  fpol->SetLineWidth(2);
35  gr->Fit(fpol, "Q");
36 
37  //Create a TGraphErrors to hold the confidence intervals
38  TGraphErrors *grint = new TGraphErrors(ngr);
39  grint->SetTitle("Fitted line with .95 conf. band");
40  for (i=0; i<ngr; i++)
41  grint->SetPoint(i, gr->GetX()[i], 0);
42  //Compute the confidence intervals at the x points of the created graph
44  //Now the "grint" graph contains function values as its y-coordinates
45  //and confidence intervals as the errors on these coordinates
46  //Draw the graph, the function and the confidence intervals
47  myc->cd(1);
48  grint->SetLineColor(kRed);
49  grint->Draw("ap");
50  gr->SetMarkerStyle(5);
51  gr->SetMarkerSize(0.7);
52  gr->Draw("psame");
53 
54 /////2. A histogram
55  myc->cd(2);
56  //Create, fill and fit a histogram
57  Int_t nh=5000;
58  TH1D *h = new TH1D("h",
59  "Fitted gaussian with .95 conf.band", 100, -3, 3);
60  h->FillRandom("gaus", nh);
61  TF1 *f = new TF1("fgaus", "gaus", -3, 3);
62  f->SetLineWidth(2);
63  h->Fit(f, "Q");
64  h->Draw();
65 
66  //Create a histogram to hold the confidence intervals
67  TH1D *hint = new TH1D("hint",
68  "Fitted gaussian with .95 conf.band", 100, -3, 3);
70  //Now the "hint" histogram has the fitted function values as the
71  //bin contents and the confidence intervals as bin errors
72  hint->SetStats(kFALSE);
73  hint->SetFillColor(2);
74  hint->Draw("e3 same");
75 
76 /////3. A 2d graph
77  //Create and fill the graph
78  Int_t ngr2 = 100;
79  Double_t z, rnd, e=0.3;
80  TGraph2D *gr2 = new TGraph2D(ngr2);
81  gr2->SetName("Graph2DNoError");
82  TF2 *f2 = new TF2("f2",
83  "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+250",-6,6,-6,6);
84  f2->SetParameters(1,1);
85  for (i=0; i<ngr2; i++){
86  f2->GetRandom2(x,y);
87  // Generate a random number in [-e,e]
88  rnd = 2*gRandom->Rndm()*e-e;
89  z = f2->Eval(x,y)*(1+rnd);
90  gr2->SetPoint(i,x,y,z);
91  }
92  //Create a graph with errors to store the intervals
93  TGraph2DErrors *grint2 = new TGraph2DErrors(ngr2);
94  for (i=0; i<ngr2; i++)
95  grint2->SetPoint(i, gr2->GetX()[i], gr2->GetY()[i], 0);
96 
97  //Fit the graph
98  f2->SetParameters(0.5,1.5);
99  gr2->Fit(f2, "Q");
100  //Compute the confidence intervals
102  //Now the "grint2" graph contains function values as z-coordinates
103  //and confidence intervals as their errors
104  //draw
105  myc->cd(3);
106  f2->SetNpx(30);
107  f2->SetNpy(30);
108  f2->SetFillColor(kBlue);
109  f2->Draw("surf4");
110  grint2->SetNpx(20);
111  grint2->SetNpy(20);
112  grint2->SetMarkerStyle(24);
113  grint2->SetMarkerSize(0.7);
114  grint2->SetMarkerColor(kRed);
115  grint2->SetLineColor(kRed);
116  grint2->Draw("E0 same");
117  grint2->SetTitle("Fitted 2d function with .95 error bars");
118 
119  myc->cd();
120 
121 }
122 
123 
124 
125 
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:1024
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:432
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:216
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition: TF1.cxx:3116
virtual void Draw(Option_t *option="")
Specific drawing options can be used to paint a TGraph2D:
Definition: TGraph2D.cxx:719
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
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:235
Definition: Rtypes.h:61
Double_t * GetX() const
Definition: TGraph2D.h:128
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
tuple f2
Definition: surfaces.py:24
TH1 * h
Definition: legend2.C:5
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2153
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:772
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:740
TFile * f
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 SetTitle(const char *title="")
Sets graph title.
Definition: TGraph2D.cxx:1747
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
void ConfidenceIntervals()
Float_t z[5]
Definition: Ifit.C:16
virtual void SetName(const char *name)
Changes the name of this 2D graph.
Definition: TGraph2D.cxx:1635
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
Double_t * GetX() const
Definition: TGraph.h:139
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
virtual void SetNpy(Int_t npy=100)
Set the number of points used to draw the function.
Definition: TF2.cxx:907
static TVirtualFitter * GetFitter()
static: return the current Fitter
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
A 2-Dim function with parameters.
Definition: TF2.h:33
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
virtual void SetMarkerSize(Size_t msize=1)
Definition: TAttMarker.h:54
TGraphErrors * gr
Definition: legend1.C:25
The Canvas class.
Definition: TCanvas.h:48
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:1707
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:504
Double_t y[n]
Definition: legend1.C:17
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2127
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:1073
1-Dim function class
Definition: TF1.h:149
void SetNpy(Int_t npx=40)
Sets the number of bins along Y used to draw the function.
Definition: TGraph2D.cxx:1684
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
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:1162
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:28
Double_t * GetY() const
Definition: TGraph2D.h:129
Definition: Rtypes.h:61
void SetNpx(Int_t npx=40)
Sets the number of bins along X used to draw the function.
Definition: TGraph2D.cxx:1663
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition: TGraph2D.h:50
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:3607
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8320
Graph 2D class with errors.