# Fitting a circle in Root

Hi Experts,

I want to fit a few data points with a circle. For this I am following the code fitCircle.C.
https://root.cern/doc/v608/fitCircle_8C.html
My question is when I am using TGraphErrors and setting errors it doesn’t take in to account the errors. Is there any way to access the chi2 of the fit properly taking in to account the errors on the points?

I am using this function below with 9 Points;
ROOT::Math::Functor fcn1(chi2Function,3);
fitter.SetFCN(fcn, pStart1,9,1);

After fitting I accessed Chi2 and status, Chi2 is very small like e.g. 1.0e-5 (after changing the error on points not changing) and status is always 0.

cout << “Fit Result Chi2:”<<fitter.Result().Chi2()<<endl;
cout << “Status:”<<fitter.Result().Status()<<endl;

Is there any way to fix or judge the quality of fit?

// This takes only points not errors
auto chi2Function = [&](const Double_t par1) {
//minimisation function computing the sum of squares of residuals
// looping at the graph points
Int_t np = gr1->GetN();
Double_t f = 0;
Double_t x1 = gr1->GetX();
Double_t y1 = gr1->GetY();
for (Int_t i=0;i<np;i++) {
Double_t u = x1[i] - par1;
Double_t v = y1[i] - par1;
Double_t dr = par1 - std::sqrt(u
u+v
v);
f += dr
dr;
}
return f;
};

Hi,
Here is a modified version of the tutorial taking into account the x and y errors of the data points, using a TGraphErrors.
https://cernbox.cern.ch/index.php/s/THQLnCZ0i2Iv4r6

Cheers

Lorenzo

1 Like

Thank you. It will be very helpful for me.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.