Fitting x,y,z with errors to a function

OK, let me try to figure out what I’m doing:

I have x,y,z points with errors ex,ey,ez in a file.
I need to fit this function to the points with errors:

void func(Double_t *x, Double_t *par) {

Double_t u = x[0] - par[0];
Double_t v = x[1] - par[1];
Double_t w = x[2] - par[2];
Double_t dr =TMath::Exp(uu+vv+w*w) - par[3] ;
return dr;

TF3 *expfit = new TF3(“expfit”,func,-10.,10.,-10.,10.,-10.,10.,4);

I tried with TGraph2DErrors, using TF3, but gives:
Error in : function expfit dimension, 3, is greater than fit object dimension, 2

This happens because I have a 2D, so how do I fit func to x,y,z points with errors?
If I fill an histogram (TH3F) the fit is wrong and errors are not included for the fit, how do I include them and why the fit is wrong?



the TGraph2D is representing the z values of (x,y) points. When fitting you need a 2D function, since the chi2 built from the residuals of z - f(x,y) is minimized.
It is not clear to me if you have (x,y) points associated with a z value or
(x,y,z) points plus a 4-th v value. In this last case you would need a TGraph3D which does not exist yet.
However, you should be able to use a TH3, if you don’t use the errors in the coordinates (x,y,z). If you really need
to fit 3D points with coordinate errors you can use directly the fitter class.
It is quite easy to do it and eventually I could send you a macro doing it, if you provide me your data points.

Best Regards


Hi Lorenzo, I need to fit 3D points with coordinate errors ( parameter-f(x,y,z), as you pointed out ).
I’ll just have an ascii file with the coordinates and the errors (6 columns). I’m still waiting for the data…
I’d really appreciate if you have a macro for this and a manual for the fitter class.
Cheers, Eric

I think you should have also the 4-th measurement and possibly also its error, so the ascii file should have 7 or 8 columns.


Hi, I’ll only have x,y,z + their errors and a free parameter.
The function to minimize will be more or less like this:
exp(x+y+z) - par[0]
I already tried with TGraph2Derrors but doesn’t work (I’m doing something wrong, but I can’t figure out what)
It works with TVirtualFitter but without errors (I don’t know how to include the errors).
This is a simple example for a sphere (I’ve been testing with this function and an ascii file with points over the sphere):

void myfcn(Int_t &, Double_t *, Double_t &f, Double_t *par, Int_t) {

Int_t np = gr->GetN();
f = 0;
Double_t xc = gr->GetX();
Double_t yc = gr->GetY();
Double_t zc = gr->GetZ();
for (Int_t i=0;i<np;i++) {
Double_t u = xc[i] - par[0];
Double_t v = yc[i] - par[1];
Double_t w = zc[i] - par[2];
Double_t dr = TMath::Sqrt(u
w) - par[3] ;
f += dr*dr;

Cheers, Eric


you are doing not a standard least square (chi2) fit, but something different.
You must write yourself the function to minimize and use directly the fitter.

An example using directly TMinuit is


Best Regards