Logo ROOT   6.10/09
Reference Guide
testGraphFit.cxx
Go to the documentation of this file.
1 // @(#)root/minuit2:$Id$
2 // Author: L. Moneta 10/2005
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2005 ROOT Foundation, CERN/PH-SFT *
7  * *
8  **********************************************************************/
9 
10 #include "TApplication.h"
11 #include "TGraphErrors.h"
12 #include "TF1.h"
13 #include "TRandom3.h"
14 #include "TCanvas.h"
15 #include "TLegend.h"
16 #include "TPaveLabel.h"
17 #include "TStopwatch.h"
18 #include "TVirtualFitter.h"
19 #include "TMath.h"
20 #include "TStyle.h"
21 
22 #include <vector>
23 #include <iterator>
24 #include <cassert>
25 
26 int verbose = 0;
27 
28 double fitFunc( double *x , double * p) {
29 
30  double A = p[0];
31  double B = p[1];
32  double C = p[2];
33  return A*TMath::Sin(C*x[0]) + B*TMath::Sin(2*C*x[0]);
34 }
35 
36 
37 
38 void makePoints(Int_t n, std::vector<double> & x, std::vector<double> & y, std::vector<double> & e)
39 {
40  Int_t i;
41  TRandom3 r;
42 
43  double A = 1;
44  double B = 2;
45  double C = 1;
46 
47  for (i=0; i<n; i++) {
48  x[i] = r.Uniform(-2, 2);
49  y[i]=A*TMath::Sin(C*x[i]) + B*TMath::Sin(2*C*x[i]) + r.Gaus()*0.3;
50  e[i] = 0.1;
51  }
52 
53 }
54 
55 
56 void doFit(int n,const char * fitter)
57 {
58 
59 
60  std::vector<double> x(n);
61  std::vector<double> y(n);
62  std::vector<double> e(n);
63 
64  double initPar[3] = { 1, 1, 2 };
65 
66  //Generate points along a sin(x)+sin(2x) function
67  makePoints(n, x , y, e);
68 
69  TGraphErrors *gre3 = new TGraphErrors(n, &x.front(), &y.front(), 0, &e.front());
70  gre3->SetMarkerStyle(24);
71  gre3->SetMarkerSize(0.3);
72  gre3->Draw("ap");
73 
74 
75  //Fit the graph with the predefined "pol3" function
76  TF1 *f = new TF1("f2",fitFunc, -2, 2, 3);
77 
78  printf("fitting with %s",fitter);
79 
80  // should change test to use Minimizer interface
82 
83 
84  int npass = (verbose) ? 1 : 100;
86  timer.Start();
87  for (int i = 0; i < npass; ++i) {
88  f->SetParameters(initPar);
89  //f->FixParameter(1,2.);
90  if (verbose)
91  gre3->Fit(f,"v");
92  else
93  gre3->Fit(f,"q");
94  }
95  timer.Stop();
96  printf("%s,: RT=%7.3f s, Cpu=%7.3f s\n",fitter,timer.RealTime(),timer.CpuTime());
97 
98  // get covariance matrix
100  int np = theFitter->GetNumberFreeParameters();
101  std::cout << "Number of free parameters " << np << "\nCovariance Matrix :\n";
102  double * cv = theFitter->GetCovarianceMatrix();
103  assert(cv != 0);
104  for (int i = 0; i < np ; ++i) {
105  for (int j = 0; j < np ; ++j)
106  std::cout << cv[j + i*np] << "\t";
107  std::cout << std::endl;
108  }
109 
110 
111 
112  //Access the fit results
113  TF1 *f3 = gre3->GetFunction("f2");
114  //std::cout << "draw function" << f3 << std::endl;
115  if (f3) {
116  f3->SetLineWidth(1);
117  f3->SetLineColor(kRed);
118  f3->Draw("same");
119  }
120 
121 
122  TLegend *leg = new TLegend(0.1, 0.8, 0.35, 0.9);
123  leg->AddEntry(gre3, "sin(x) + sin(2*x)", "p");
124  leg->Draw();
125  leg->SetFillColor(42);
126 
127  TPaveLabel *pl = new TPaveLabel(0.5,0.7,0.85,0.8,Form("%s CPU= %g s",fitter,timer.CpuTime()),"brNDC");
128  pl->Draw();
129 
130 
131 }
132 
133 void testGraphFit(int n = 500) {
134  TCanvas *myc = new TCanvas("myc", "Fitting 3 TGraphErrors with linear functions");
135  myc->Divide(1,2);
136  myc->SetFillColor(42);
137  myc->SetGrid();
138  gStyle->SetOptFit();
139 
140  myc->cd(1);
141  doFit(n,"Minuit");
142  myc->Update();
143 
144  myc->cd(2);
145  doFit(n,"Minuit2");
146  myc->Update();
147 
148 }
149 
150 #ifndef __CINT__
151 int main(int argc, char **argv)
152 {
153 
154  bool showGraphics = false;
155 
156  // Parse command line arguments
157  for (Int_t i = 1 ; i < argc ; i++) {
158  std::string arg = argv[i] ;
159 
160  if (arg == "-v") {
161  std::cout << "Running in verbose mode" << std::endl;
162  verbose = 1;
163  showGraphics = true;
164  }
165 
166  if (arg == "-g") {
167  std::cout << "Running showing the graphics" << std::endl;
168  showGraphics = true;
169  }
170  }
171 
172  TApplication* theApp = 0;
173  if ( showGraphics )
174  theApp = new TApplication("App",&argc,argv);
175 
176  testGraphFit(500);
177 
178  if ( showGraphics )
179  {
180  theApp->Run();
181  delete theApp;
182  theApp = 0;
183  }
184 
185  return 0;
186 }
187 #endif
188 
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
const int npass
Definition: testPermute.cxx:21
static double B[]
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:588
Random number generator class based on M.
Definition: TRandom3.h:27
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
void doFit(int n, const char *fitter)
TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TGraph.cxx:1457
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:56
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:452
static void SetDefaultFitter(const char *name="")
static: set name of default fitter
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
double fitFunc(double *x, double *p)
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:125
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF1.cxx:1087
int main(int argc, char **argv)
static double A[]
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
TStopwatch timer
Definition: pirndm.C:37
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
Double_t x[n]
Definition: legend1.C:17
virtual void Run(Bool_t retrn=kFALSE)
Main application eventloop. Calls system dependent eventloop via gSystem.
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:323
void makePoints(Int_t n, std::vector< double > &x, std::vector< double > &y, std::vector< double > &e)
A Pave (see TPave) with a text centered in the Pave.
Definition: TPaveLabel.h:20
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
static double C[]
TRandom2 r(17)
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
void SetOptFit(Int_t fit=1)
The type of information about fit parameters printed in the histogram statistics box can be selected ...
Definition: TStyle.cxx:1219
char * Form(const char *fmt,...)
static TVirtualFitter * GetFitter()
static: return the current Fitter
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
int verbose
The Canvas class.
Definition: TCanvas.h:31
virtual void Draw(Option_t *option="")
Draw this pavelabel with its current attributes.
Definition: TPaveLabel.cxx:77
double f(double x)
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:359
leg
Definition: legend1.C:34
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:606
Abstract Base Class for Fitting.
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:1135
1-Dim function class
Definition: TF1.h:150
Double_t Sin(Double_t)
Definition: TMath.h:548
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:26
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:39
void testGraphFit(int n=500)
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208
bool showGraphics
const Int_t n
Definition: legend1.C:16
Stopwatch class.
Definition: TStopwatch.h:28