I’ve been trying to make a 2 dimensional unbinned likelihood fit using a ROOT::Math::bigaussian_pdf. The code seems to work but the minimization does not converge. I always get “Invalid FitResult (status = 4)” even when I set the initial values of the parameters really close to the expected result. Here is the main part of the code that I wrote following the guide.
Hi,
It was not easy, but I found at the end the problem of your code.
You are using this to create a data set for fitting from the TTree:
// Extract data from tree
int nevt = tree->Draw("Xt:Yt","","goff");
double *Xt = tree->GetV1();
double *Yt = tree->GetV2();
ROOT::Fit::UnBinData data(nevt, Xt, Yt);
This is fine, but one should note that in this case the data values are not copied but just used externally and only the pointers are copied in the UnBinData class. The problem is that later you call again TTree::Draw to get the histograms for X and Y and this make the UnBinData class having pointers to wrong values. You can do many things, for example:
don’t make histogram for just the mean and stddev of the data. You can just use TMath::Mean and TMath::StdDev` using the Xt and Yt arrays
copy the data in some local arrays, and use those to pass to UnBinData, like:
int nevt = tree->Draw("Xt:Yt","","goff");
double *Xt = tree->GetV1();
double *Yt = tree->GetV2();
ROOT::Fit::UnBinData data(nevt,2); // 2 is the data dimension
for (int i = 0; i < next; i++)
data.Add(Xt[i], Yt[i]);
Many thanks @moneta !
Your explanation was extremely helpful. I had not fully understood what tree->Draw() was doing.
I followed your first suggestion and everything worked out great