TF1 from fit vs TF1 with SetParameters

dear rooters,

using root 5.18 i obseve a very strange problem: i created my own function (see attached MyFitFunction.C):

Double_t MyFitFunction(Double_t* x, Double_t* parameter) {

	Double_t funtionValue;
	
	if (x[0] <= 24.77) {
		functionValue = parameter[0] + 
							 parameter[1] * x[0] ;
	} else {
		functionValue = parameter[2] + 
										parameter[3] * x[0] +  
										parameter[4] * x[0] * x[0] +  
										parameter[5] * x[0] * x[0] * x[0] + 
										parameter[6] * x[0] * x[0] * x[0] * x[0];
	}
	
	return functionValue;
}

to fit a histogram (stored in attached test.root) with a root macro (attached fitTest.C):

{
 TFile* _file0 = TFile::Open("test.root");
 TH1D* ElecERatio = (TH1D*) _file0->Get("ScatElecEnergy");

 gInterpreter->Load("MyFitFunction.C");

 TF1 FitFunction("myFitFunction",MyFitFunction,10.,30.,7);
 FitFunction.SetParameters(1.9223668, -0.04884, 3.998807e+04, -6.356760e+03, 3.790429e+02, -1.004786e+01, 9.991058e-02);

 ElecERatio->SetYTitle("data/mc");
 ElecERatio->Fit(&myFitFunction);
 FitFunction.Draw("same");
	
}

the fit works fine, but if i run a second script (attached plotTest.C) in which i set the parameters resulting from the fit by hand:

{
	gInterpreter->Load("MyFitFunctionForElecERatio.C");

	TF1 FitFunction("myFitFunction",MyFitFunction,10.,30.,7);
	FitFunction.SetParameters(2.042, -0.05528, 3.998e+04, -6357., 379.1, -10.05, 0.09989);
	
	FitFunction.Draw();
}

the curve looks very, very different from the one resulting from the fit.

does anyone have an idea why this happens?

thanks in advance for help!
cheers,
axel.
test.root (4.33 KB)
plotTest.C (231 Bytes)
fitTest.C (427 Bytes)
MyFitFunction.C (420 Bytes)

Could you clarify the following?
in fitTest.C you have

gInterpreter->Load("MyFitFunction.C"); in plotTest.C you have:

gInterpreter->Load("MyFitFunctionForElecERatio.C");
What is the difference between the 2 functions?
Could you post the second dile?

Rene

hi rene,

sorry that was a mistake. for posting the macros i changed their names, so MyFitFunctionForElecERatio.C and MyFitFunction.C are exactly the same. i just forgot to change gInterpreter->Load("MyFitFunctionForElecERatio.C"); to gInterpreter->Load("MyFitFunction.C"); in plotTest.C.

so just replace that statement in plotTest.C. (to make sure i attached the changed version).

cheers,
axel.
plotTest.C (218 Bytes)

hi,

in the function in MyFitFunction.C i am creating a funtion which up to x=24.77 is a line and a polynomial of 4th order for x>24.77. running the scripts i attached in the previous posts will give you the results (for completion i also attached the resulting plot in “fitResult.ps”).

using gnuplot i then plotted the line and the polynomial separately, which you can see in the attached “gnuFunction.ps”.

the latter clearly shows that the polynomial with the fit parameters as input looks different from the polynomial that root shows after the fit, which confirms that my previously attached macro “plotTest.c” seems to work fine, but the fit seems to go awry.

cheers,
axel.
gnuFunction.ps (17.5 KB)
fitResult.ps (18.8 KB)

Hum, you are not plotting the same function !!! The values given when plotting are slightly different than the results of the fit (typycal problems when computing polynomials with
variables that are not in [-1,1]. In your case, in plotTest.C simply change the line

and you will see the huge difference ::slight_smile:

Rene

oh man! :smiley: thanks very much, Rene!

didn’t realise the numerical precision was THAT important!

sorry for stealing your time!

you never stop to learn :slight_smile:

cheers,
axel.