ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fitCircle.C
Go to the documentation of this file.
1 //Generate points distributed with some errors around a circle
2 //Fit a circle through the points and draw
3 //To run the script, do, eg
4 // root > .x fitCircle.C (10000 points by default)
5 // root > .x fitCircle.C(100); (with only 100 points
6 // root > .x fitCircle.C(100000); with ACLIC
7 //
8 //Author: Rene Brun
9 
10 #include "TCanvas.h"
11 #include "TRandom3.h"
12 #include "TGraph.h"
13 #include "TMath.h"
14 #include "TArc.h"
15 #include "TVirtualFitter.h"
16 
18 
19 //____________________________________________________________________
21  //minimisation function computing the sum of squares of residuals
22  Int_t np = gr->GetN();
23  f = 0;
24  Double_t *x = gr->GetX();
25  Double_t *y = gr->GetY();
26  for (Int_t i=0;i<np;i++) {
27  Double_t u = x[i] - par[0];
28  Double_t v = y[i] - par[1];
29  Double_t dr = par[2] - TMath::Sqrt(u*u+v*v);
30  f += dr*dr;
31  }
32 }
33 
34 //____________________________________________________________________
35 void fitCircle(Int_t n=10000) {
36  //generates n points around a circle and fit them
37  TCanvas *c1 = new TCanvas("c1","c1",600,600);
38  c1->SetGrid();
39  gr = new TGraph(n);
40  if (n> 999) gr->SetMarkerStyle(1);
41  else gr->SetMarkerStyle(3);
42  TRandom3 r;
43  Double_t x,y;
44  for (Int_t i=0;i<n;i++) {
45  r.Circle(x,y,r.Gaus(4,0.3));
46  gr->SetPoint(i,x,y);
47  }
48  c1->DrawFrame(-5,-5,5,5);
49  gr->Draw("p");
50 
51  //Fit a circle to the graph points
52  TVirtualFitter::SetDefaultFitter("Minuit"); //default is Minuit
53  TVirtualFitter *fitter = TVirtualFitter::Fitter(0, 3);
54  fitter->SetFCN(myfcn);
55 
56  fitter->SetParameter(0, "x0", 0, 0.1, 0,0);
57  fitter->SetParameter(1, "y0", 0, 0.1, 0,0);
58  fitter->SetParameter(2, "R", 1, 0.1, 0,0);
59 
60  Double_t arglist[1] = {0};
61  fitter->ExecuteCommand("MIGRAD", arglist, 0);
62 
63  //Draw the circle on top of the points
64  TArc *arc = new TArc(fitter->GetParameter(0),
65  fitter->GetParameter(1),fitter->GetParameter(2));
66  arc->SetLineColor(kRed);
67  arc->SetLineWidth(4);
68  arc->Draw();
69 }
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
double par[1]
Definition: unuranDistr.cxx:38
Random number generator class based on M.
Definition: TRandom3.h:29
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
TCanvas * c1
Definition: legend1.C:2
Definition: Rtypes.h:61
virtual Int_t SetParameter(Int_t ipar, const char *parname, Double_t value, Double_t verr, Double_t vlow, Double_t vhigh)=0
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:1475
static void SetDefaultFitter(const char *name="")
static: set name of default fitter
virtual Double_t GetParameter(Int_t ipar) const =0
int Int_t
Definition: RtypesCore.h:41
virtual void SetFCN(void *fcn)
To set the address of the minimization objective function.
Int_t GetN() const
Definition: TGraph.h:132
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:740
TFile * f
void myfcn(Int_t &, Double_t *, Double_t &f, Double_t *par, Int_t)
Definition: fitCircle.C:20
Double_t * GetY() const
Definition: TGraph.h:140
Double_t x[n]
Definition: legend1.C:17
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:326
tuple np
Definition: multifit.py:30
virtual void Draw(Option_t *option="")
Draw this ellipse with its current attributes.
Definition: TEllipse.cxx:167
Create an Arc.
Definition: TArc.h:29
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
Double_t * GetX() const
Definition: TGraph.h:139
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
virtual Int_t ExecuteCommand(const char *command, Double_t *args, Int_t nargs)=0
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
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
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
TGraph * gr
Definition: fitCircle.C:17
Double_t y[n]
Definition: legend1.C:17
void fitCircle(Int_t n=10000)
Definition: fitCircle.C:35
Abstract Base Class for Fitting.
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
static TVirtualFitter * Fitter(TObject *obj, Int_t maxpar=25)
Static function returning a pointer to the current fitter.
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
Double_t dr
Definition: parallelcoord.C:14
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
const Int_t n
Definition: legend1.C:16