Logo ROOT  
Reference Guide
fitCircle.C File Reference

Detailed Description

View in nbviewer Open in SWAN Generate points distributed with some errors around a circle Fit a circle through the points and draw To run the script, do, eg

root > .x fitCircle.C (10000 points by default)
root > .x fitCircle.C(100); (with only 100 points
root > .x fitCircle.C++(100000); with ACLIC
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t points
****************************************
Minimizer is Minuit / Migrad
MinFCN = 903.174
NDf = 0
Edm = 2.25062e-09
NCalls = 72
x0 = 0.00667777 +/- 0.0141774
y0 = 0.00536467 +/- 0.0141954
R = 3.995 +/- 0.0100048
#include "TCanvas.h"
#include "TRandom3.h"
#include "TGraph.h"
#include "TMath.h"
#include "TArc.h"
#include "Fit/Fitter.h"
#include <Math/Functor.h>
//____________________________________________________________________
void fitCircle(int n=10000) {
//generates n points around a circle and fit them
TCanvas *c1 = new TCanvas("c1","c1",600,600);
c1->SetGrid();
TGraph* gr = new TGraph(n);
if (n> 999) gr->SetMarkerStyle(1);
else gr->SetMarkerStyle(3);
double x,y;
for (int i=0;i<n;i++) {
r.Circle(x,y,r.Gaus(4,0.3));
gr->SetPoint(i,x,y);
}
c1->DrawFrame(-5,-5,5,5);
gr->Draw("p");
auto chi2Function = [&](const double *par) {
//minimisation function computing the sum of squares of residuals
// looping at the graph points
int np = gr->GetN();
double f = 0;
double *x = gr->GetX();
double *y = gr->GetY();
for (int i=0;i<np;i++) {
double u = x[i] - par[0];
double v = y[i] - par[1];
double dr = par[2] - std::sqrt(u*u+v*v);
f += dr*dr;
}
return f;
};
// wrap chi2 function in a function object for the fit
// 3 is the number of fit parameters (size of array par)
ROOT::Math::Functor fcn(chi2Function,3);
double pStart[3] = {0,0,1};
fitter.SetFCN(fcn, pStart);
fitter.Config().ParSettings(0).SetName("x0");
fitter.Config().ParSettings(1).SetName("y0");
fitter.Config().ParSettings(2).SetName("R");
// do the fit
bool ok = fitter.FitFCN();
if (!ok) {
Error("line3Dfit","Line3D Fit failed");
}
const ROOT::Fit::FitResult & result = fitter.Result();
result.Print(std::cout);
//Draw the circle on top of the points
TArc *arc = new TArc(result.Parameter(0),result.Parameter(1),result.Parameter(2));
arc->SetLineWidth(4);
arc->SetFillStyle(0);
arc->Draw();
}
#define f(i)
Definition: RSha256.hxx:104
@ kRed
Definition: Rtypes.h:66
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
const ParameterSettings & ParSettings(unsigned int i) const
get the parameter settings for the i-th parameter (const method)
Definition: FitConfig.h:76
class containing the result of the fit and all the related information (fitted parameter values,...
Definition: FitResult.h:47
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:77
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:625
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:632
const FitResult & Result() const
get fit result
Definition: Fitter.h:382
const FitConfig & Config() const
access to the fit configuration (const method)
Definition: Fitter.h:410
void SetName(const std::string &name)
interaction
Documentation for class Functor class.
Definition: Functor.h:401
Create an Arc.
Definition: TArc.h:26
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
The Canvas class.
Definition: TCanvas.h:23
void Draw(Option_t *option="") override
Draw this ellipse with its current attributes.
Definition: TEllipse.cxx:168
A TGraph is an object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2281
Double_t * GetY() const
Definition: TGraph.h:134
Int_t GetN() const
Definition: TGraph.h:126
Double_t * GetX() const
Definition: TGraph.h:133
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition: TGraph.cxx:806
Random number generator class based on M.
Definition: TRandom3.h:27
Double_t y[n]
Definition: legend1.C:17
return c1
Definition: legend1.C:41
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
TGraphErrors * gr
Definition: legend1.C:25
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
Author
Rene Brun

Definition in file fitCircle.C.