Logo ROOT   6.10/09
Reference Guide
fitCircle.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook
4 /// Generate points distributed with some errors around a circle
5 /// Fit a circle through the points and draw
6 /// To run the script, do, eg
7 ///
8 /// ~~~{.cpp}
9 /// root > .x fitCircle.C (10000 points by default)
10 /// root > .x fitCircle.C(100); (with only 100 points
11 /// root > .x fitCircle.C(100000); with ACLIC
12 /// ~~~
13 ///
14 /// \macro_image
15 /// \macro_output
16 /// \macro_code
17 ///
18 /// \author Rene Brun
19 
20 #include "TCanvas.h"
21 #include "TRandom3.h"
22 #include "TGraph.h"
23 #include "TMath.h"
24 #include "TArc.h"
25 #include "Fit/Fitter.h"
26 
27 //____________________________________________________________________
28 void fitCircle(Int_t n=10000) {
29  //generates n points around a circle and fit them
30  TCanvas *c1 = new TCanvas("c1","c1",600,600);
31  c1->SetGrid();
32  TGraph* gr = new TGraph(n);
33  if (n> 999) gr->SetMarkerStyle(1);
34  else gr->SetMarkerStyle(3);
35  TRandom3 r;
36  Double_t x,y;
37  for (Int_t i=0;i<n;i++) {
38  r.Circle(x,y,r.Gaus(4,0.3));
39  gr->SetPoint(i,x,y);
40  }
41  c1->DrawFrame(-5,-5,5,5);
42  gr->Draw("p");
43 
44 
45  auto chi2Function = [&](const Double_t *par) {
46  //minimisation function computing the sum of squares of residuals
47  // looping at the graph points
48  Int_t np = gr->GetN();
49  Double_t f = 0;
50  Double_t *x = gr->GetX();
51  Double_t *y = gr->GetY();
52  for (Int_t i=0;i<np;i++) {
53  Double_t u = x[i] - par[0];
54  Double_t v = y[i] - par[1];
55  Double_t dr = par[2] - std::sqrt(u*u+v*v);
56  f += dr*dr;
57  }
58  return f;
59  };
60 
61  // wrap chi2 funciton in a function object for the fit
62  // 3 is the number of fit parameters (size of array par)
63  ROOT::Math::Functor fcn(chi2Function,3);
64  ROOT::Fit::Fitter fitter;
65 
66 
67  double pStart[3] = {0,0,1};
68  fitter.SetFCN(fcn, pStart);
69  fitter.Config().ParSettings(0).SetName("x0");
70  fitter.Config().ParSettings(1).SetName("y0");
71  fitter.Config().ParSettings(2).SetName("R");
72 
73  // do the fit
74  bool ok = fitter.FitFCN();
75  if (!ok) {
76  Error("line3Dfit","Line3D Fit failed");
77  }
78 
79  const ROOT::Fit::FitResult & result = fitter.Result();
80  result.Print(std::cout);
81 
82  //Draw the circle on top of the points
83  TArc *arc = new TArc(result.Parameter(0),result.Parameter(1),result.Parameter(2));
84  arc->SetLineColor(kRed);
85  arc->SetLineWidth(4);
86  arc->Draw();
87 }
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
double par[1]
Definition: unuranDistr.cxx:38
Random number generator class based on M.
Definition: TRandom3.h:27
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
return c1
Definition: legend1.C:41
Definition: Rtypes.h:56
Documentation for class Functor class.
Definition: Functor.h:392
TH1F * DrawFrame(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax, const char *title="")
Draw an empty pad frame with X and Y axis.
Definition: TPad.cxx:1537
bool FitFCN(unsigned int npar, Function &fcn, const double *params=0, unsigned int dataSize=0, bool chi2fit=false)
Fit using the a generic FCN function as a C++ callable object implementing double () (const double *)...
Definition: Fitter.h:518
int Int_t
Definition: RtypesCore.h:41
double Parameter(unsigned int i) const
parameter value by index
Definition: FitResult.h:182
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
Definition: FitConfig.h:74
const FitResult & Result() const
get fit result
Definition: Fitter.h:337
double sqrt(double)
Double_t x[n]
Definition: legend1.C:17
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:323
void SetName(const std::string &name)
interaction
const FitConfig & Config() const
access to the fit configuration (const method)
Definition: Fitter.h:365
void Error(const char *location, const char *msgfmt,...)
virtual void Draw(Option_t *option="")
Draw this ellipse with its current attributes.
Definition: TEllipse.cxx:167
Create an Arc.
Definition: TArc.h:28
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
TRandom2 r(17)
SVector< double, 2 > v
Definition: Dict.h:5
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:77
Int_t GetN() const
Definition: TGraph.h:122
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
virtual void Circle(Double_t &x, Double_t &y, Double_t r)
Generates random vectors, uniformly distributed over a circle of given radius.
Definition: TRandom.cxx:200
TGraphErrors * gr
Definition: legend1.C:25
Double_t * GetX() const
Definition: TGraph.h:129
The Canvas class.
Definition: TCanvas.h:31
class containg the result of the fit and all the related information (fitted parameter values...
Definition: FitResult.h:48
double f(double x)
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
bool SetFCN(unsigned int npar, Function &fcn, const double *params=0, unsigned int dataSize=0, bool chi2fit=false)
Set a generic FCN function as a C++ callable object implementing double () (const double *) Note that...
Definition: Fitter.h:523
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2156
Double_t * GetY() const
Definition: TGraph.h:130
void Print(std::ostream &os, bool covmat=false) const
print the result and optionaly covariance matrix and correlations
Definition: FitResult.cxx:446
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
double result[121]
const Int_t n
Definition: legend1.C:16