# Complicated TGraph2DErrors fit

We made following measurements: at different y_i we measured countrate z_ij at different x_j (x_j are different for each y_i). There are errors for z_ij and y_i.
So, as a result there is TGraph2DErrors object. Each distribution of z via x at concrete y_i is a Gaussian signal with linear background.
We can easily make 1D fit of each that distribution at every y_i (gaus+pol1). After that we draw a distribution of X_c (gaus center) via y and fit it by linear function and extract the slope.
We want to make two consequent fit together. We think that for each y_i (i>=0) the function should look like this:
bg = par[4i+2] + par[4i+3]x;
sig = par[4
i+4]TMath::Exp(-(x-par-parx)(x-par-parx)/(2par[4i+5]par[4i+5]));
par should be extracted as a slope.
But how to build this function? The main criterium is to match this global function at points y_i to 1D gaus+pol1.
Or, maybe I can work directly with ChiSquared (TMinuit or TFumili)? But there I should provide a function again.

Any suggestions or questions are welcome!

Hi,

if I have understood well the problem, you would like to perform a global Least square fit to your z_ij data using different functions f_i(x_j) for every y_i point.

You can do that in ROOT, either using directly Minuit or the TVirtualFitter.
In that case you need to express your least square (chi2) function, implementing a function of type

void Chi2Func(Int_t &npar, Double_t *gin, Double_t &chi2, Double_t *par, Int_t flag); {
chi2 = Sum_i Sum_j ( z_ij - f_i ( x_j) )/sigma(z_ij) ) **2
}

which must be passed to Minuit or the TVirtualFitter to be minimized.

Best Regards

Lorenzo

Yes, this is what I need. Can you give me a starting point in this tree: root.cern.ch/root/html514/ClassIndex.html ?

you can look at the TVirtualFitter documentation:

root.cern.ch/root/htmldoc/TVirtualFitter.html