Hello all,
I’m trying to fit my histograms with user defined function. For physical reasons it has to stay positive in the fitting range. I have used SetMinimun(), but it’s not working.
I am doing the following:

Function->SetMinimum(0.0); // I have tried to set function minimum before fit
MyHistogram->Fit(Function,"EML");
Function->GetParameters(par);
Function->SetMinimum(0.0); // I have tried to set it after the fit and I tried to use it in both places

But when I draw the histograms with fitted function, I still get the function that is negative sometimes (depending on the content of the bins).
Is there any other way to do this?
I know there are ways to put constrains of function parameters, like SetParLimits() and SetParameter(), but is there a way to set the minimal possible value of the function and then let thet function fit my data obeying the minimum limit?
Thank you!
All the best,
Dragan

Double_t myfunction(Double_t *x, Double_t *par)
{
Float_t xx =x[0];
Double_t f = xx*xx*xx; //replace by your real expression
return f;
}
you can add a line such as

True, I could do that. But that would mean I’ll get unnaturally positive value of the function in the region where it would be negative based on the fit without that limit (or in region with empty bins). So, it’s possible that I get a function that is not smooth. Not to mention that it could be problematic to get derivate of the function. I think that’s not a good way to fit background.
I would rather impose limits to the values that function may have in general and then let it go through fitting procedure, instead of changing the definition of the function for areas where it would be negative based on the fit. If that’s possible…

You can impose limits on the parameters with TF1::SetParLimits(). Below an example. It just fills a histogram, turns the entries negative and fits it with a constant. Without the constraint you end up with a negative fit value, with the constraint your ‘background’ stays positive.

Hello Hans,
Thank you for your reply. I understand how I may use SetParLimits().
But it won’t work for a bit more realistic example. Imagine that I have a linear function for fitting (kx+n). If I set a limit on one parameter to be positive, the function can still be negative. I can force the function to be positive if I set the limit on both parameters to be positive (k>0 and n>0). However, if I do that I ask the function to be increasing. Sometimes my background is increasing, sometimes it’s decreasing, so the fit won’t be always good.
Not to mention that I’ll get the same problem with functions of higher order. For example, k(x^2)+mx+n and I ask for k>0. With this kind of limit I’m forcing the function to be convex, when I need it to be convex or concave depending on the shape of my background.
I don’t want to impose such limits. I want the function to be able to be increasing, decreasing, convex or concave; whatever gives the best fit. I just don’t want it to go into negative range, because then it doesn’t have physical sense.
Cheers,
Dragan