User defined linear fit in TH2D for correlation

Hi All,

I want to fit my histogram, I made a user defined fomula as the following, and everything in this formula defined before TF1 declare.

TF1 *f = new TF1("Correlation","(TMath::Log10(GAIN)*TMath::Log10(n+1))/(TMath::Log10(HV[hv])*n)");

However, I got this error !

input_line_134:1:82: error: use of undeclared identifier 'GAIN'
Double_t TFormula____id7729400694293341383(){ return (TMath::Power(TMath::Log10({GAIN}),TMath::Log10({n}+1)))//(TMath::Power(TMath::Log10({HV[hv]}),{n})) ; }
input_line_134:1:103: error: use of undeclared identifier 'n'
Double_t TFormula____id7729400694293341383(){ return (TMath::Power(TMath::Log10({GAIN}),TMath::Log10({n}+1)))//(TMath::Power(TMath::Log10({HV[hv]}),{n})) ; }
<<< cling interactive line includer >>>:1:1: error: expected '}'
input_line_134:1:45: note: to match this '{'
Double_t TFormula____id7729400694293341383(){ return (TMath::Power(TMath::Log10({GAIN}),TMath::Log10({n}+1)))//(TMath::Power(TMath::Log10({HV[hv]}),{n})) ; }
Error in <TFormula::Eval>: Can't find TFormula____id7729400694293341383 function prototype with arguments
Error in <TFormula::ProcessFormula>: "HV[hv]" has not been matched in the formula expression
Error in <TFormula::ProcessFormula>: "GAIN" has not been matched in the formula expression
Error in <TFormula::ProcessFormula>: "n" has not been matched in the formula expression
Error in <TFormula::ProcessFormula>: Formula "(pow(TMath::Log10(GAIN),TMath::Log10(n+1)))//(pow(TMath::Log10(HV[hv]),n))" is invalid !
Error in <Fit>: function Correlation has illegal number of parameters = 0

What’s wrong with this formula ?
Can anybody help me about this problem ?


Dear Ersel,

There are some rules to follow for the variable and the parameters: see TF2 and TF1.
In particular, the variables on the axis are named ‘x’ and ‘y’, the parameters vector par[…] .

Hope it helps,
G Ganis

When I try to fit my data, i’ve got this following error.

What does it mean ?

Error in <TSystem::ExpandFileName>: input: $/usr/local/lib/libCling, output: $/usr/local/lib/libCling
Error in <TSystem::ExpandFileName>: input: $/usr/local/lib/libMathCore, output: $/usr/local/lib/libMathCore



the formula trying to fit my data is this ;

G = P0 * V ^(P1*10) 

( G= y-axis, V= x-axis, P1=constant (i don’t know what it is;that is, just constant) and P2 = slope ( which is thing i need to find ) )

So, how might i fit my data with this user defined formula ?

For example, I tried to this one ;

I create TH2D with G=y-axis and V=x-axis and then fitted my data with the following TF1;

TF1 *f = new TF1("fit", [p0] * TMath::Power ( x , ( [p1]*10 ) ) ); 

[p0] : the constant in this formula and [p1] : slope which is that i need to figure out what it’s value !

However, It wasn’t correct fit i want to !!!

Could you help me about this issue ?


You try to fit a 2-dimensional histogram with a 1-dimensional function?

BTW. Do remember that you need to set “reasonable” initial values for all parameters of your function, before you try to fit your histogram (otherwise the fitting procedure may easily misbehave).

1 Like

Yes, because i need to find the parameter [p1] ,which is the slope of the fitted plot G vs V

Ok i have done it :wink:

I have just written the values below, randomly, on my script , as you said !


Thank you very much,

I have tried some initial values for fitting, which i chose them by visual inspection for a specific range according to theoretical formula, but, of course, the results of fit are different from each other for each initial value ! Is this a problem ?

Why doesn’t ROOT choose the best ‘optimised’ initial value for fitting ?

So, How might i choose the best optimised initial value for fitting with an user-defined function ?


Well, the user is responsible for choosing good initial values for the user-defined function.

If you really get significantly different “best fit” values then:

  1. maybe your function is ill-defined (e.g. some parameters are correlated / redundant),
  2. maybe your function is not able to describe your data well,
  3. maybe the fit has many “local minima” and you land in one or another, depending on the “starting point”.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.