I’m confused. I’ve got a class member function that I would like to use as a fit function. So I created this external (C style) function as a wrapper around it and I wanted to use the SetObjectFit()/GetObjectFit() methods to have that wrapper access the object that contains the fit function and information.
To do this I create a fitter (fitter = TVirtualFitter::Fitter(histo,npar), with histo the histogram to be fitted) and then call fitter->SetFCN(wrapper) and fitter->SetObjectFit(this). (This is called from inside the object that contains the fitting info so now the fitters fObjectFit points to that object. Then I call histo->Fit(“fitFun”,“U”) to perform the fit.
The problem is that somewhere deep inside TVirtualFitter::Fitter() called from TH1::Fit() another call is made to fitter->SetObjectFit(this) so now it points to the histogram instead of to the object that contains my fitting information.
Is this the intended behaviour? In that case I would appreciate some help with the correct way to tackle this problem.
I know I can get around this by using a global pointer to my object but that’s soooo ugly…
You do not indicate the most important call to create the TF1 object.
If you fit via TH1::Fit, you do not have to create a Fitter yourself.
See example in the tutorial FittingDemo.C
I know the TF1 object is fine because it gets called correctly. I also do have to create the fitter myself since after that I want to call fitter->SetObjectFit(myObject) to store a pointer to the calling object and fitter->SetFCN(myMinFunction) to set a custom minimisation function. Then in the extern ‘C style’ function I want to extract that pointer to be able to extract some information from the calling object.
But, and this is the problem, when I fit by calling TH1::Fit(fitFun,“U”), it correctly calls the user minimisation function and the correct user fit function but the pointer extracted by fitter->GetObjectFit() is now the pointer to the histogram being fitted instead of the pointer that I put in.
So I was wondering if this is the desired behaviour. I don’t think so, right? But if it is, is the only way around it to fit by calling minuit directly?
Okay, I uploaded a small example to show what I mean. I want to pass a pointer to an object into the fitter using SetFCN but when I call the TH1::Fit method this pointer gets reset. fit_problem.C (2.33 KB)