Use interpolation as a function in TF1 for fitting

Hi all,
I have a set of data points, say probability vs energy, that I interpolated with TSpline3. Now, I want to use this interpolation as a function so that I can multiply it with another TF1 function, say power law, and use this combined function to fit a set of another data points. But I don’t know how to use interpolation as a function. I tried to use sp->Eval(x) inside TF1 like “sp->Eval(x) * [0] * x**[1]” but it’s not working? Any help is greatly appreciated.


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Try:

TF1 *f = new TF1("f", [=](double *x, double *p){ return (sp->Eval(x[0]) * p[0] * TMath::Power(x[0], p[1])); }, sp->GetXmin(), sp->GetXmax(), 2);

Thanks for your prompt reply @Wile_E_Coyote. As I understand, you defined a function using ‘[=]’ instead of a name, passed pointers x and p as arguments and used min and max x values of the interpolation. Have I understood it correctly? And, does the number 2 in the last stands for two parameters?

TF1 → 5 - A general C++ function object (functor) with parameters → Using a lambda function as a general C++ functor object

Thanks. I have a doubt! I think we need to use the min and max of the data points that I want to fit with this combined function, instead of min and max of the interpolation function as you suggested with sp->GetXmin() and sp->GetXmax(). Is it so?

The function’s range is usually unimportant (unless, e.g., you use the “R” fit option).

@Wile_E_Coyote Could you please see if what I did is correct? Although the fitted parameters value is not what I was expecting!
Thank you in advance.


Try:

@Wile_E_Coyote I tried your suggestion. the errors on the parameters looks fine, I guess. But the value of P2 is very large, 10^7 !! (Image1)
Also, I tried fitting it with one more function but it seems I can’t (image 2)! As a comparison I used fitting in python using ODR (image 3), but the error in the parameter P2 is very large (image 4)!
Any suggestions?
Also, is it necessary to consider errors on x axis. How does it affect the fit if we neglect it?


Fig1

When playing with fit functions, I would use "EX0" or "W EX0".

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