User-defined fit function with unspecified parameters?


i am trying to create a custom fit function (two gaussians plus a constant), but i seem to be running into errors. my fit is defined as

and then it gets fit to the histogram with

where h1 is the histogram.

the problem is that all the fit parameters converge to zero, except par0 and par3 and par6. the result is a constant horizontal line

ideas? i am trying to force in an exact expression for the gaussian and seeing if that makes a difference, but maybe my general approach is incorrect (which is why i’m here :smiley:)



In my understanding, if you use a user-defined function you need to explicitly set the initial values for your parameters. (The actual values you set don’t matter, since they’re just going to be optimized anyway. I don’t know why ROOT makes you set the initial values, but whatever…) You can do

[code]TFormula *function = new TFormula(“function”, “[0]TMath::Exp(-.5((x-[1])/[2])^2) + [3]TMath::Exp(-.5((x-[4])/[5])^2) + [6]”);

const int n = 7; // the number of parameters to your function
for (int i = 0; i < n; i++)
function->SetParameter(i, 1.0);

TF1 *gaus1 = new TF1(“gaus1”, “function”, 2000, 2700);
h1->Fit(“gaus1”, “R”);[/code]