Define new fitting function with external functions

Please provide the following information:

ROOT Version (e.g. 6.12/02): 6.06/01
Platform, compiler (e.g. CentOS 7.3, gcc6.2): gcc6.4

I would like to call complex functions from other file and merge them.
So I made some external function and called it at the main function.
But there is an issue about arguments.
So could somebody please solve this problem?

Following is sample of my code

Double_t MyFunc(Double_t* x, Double_t* p, TF1* f1, TF1* f2, TF1* f3)
return p[0]*f1->Eval(x[0]) + p[1]*f2->Eval(x[0]) + p[3]*f3->Eval(x[0]);
TFile * fin = new TFile(“file.root”);
TF1 * tf1S = (TF1 *) fin->Get(“func1”);
TF1 * tf2S = (TF1 *) fin->Get(“func2”);
TF1 * tf3S = (TF1 *) fin->Get(“func3”);
TF1 * tf1SC = (TF1 *) tf1S->Clone(“tf1SC”);
TF1 * tf2SC = (TF1 *) tf2S->Clone(“tf2SC”);
TF1 * tf3SC = (TF1 *) tf3S->Clone(“tf3SC”);
TF1 * fadd = new TF1(“fadd”, MyFunc(tf1SC, tf2SC, tf3SC), 0, 10);

This code does not work due to the arguments of the MyFunc.
How can I solve this?

Thank you.


That’s what lambdas were invented for!

TF1 * fadd = new TF1(“fadd”,
  [&](double*x, double*p) {
    return MyFunc(x, p, tf1SC, tf2SC, tf3SC);
  0, 10);

This creates a TF1 on a function that takes two double arrays and returns a double. It forwards the call to MyFunc, specifying the parameters that TF1 cannot provide. Makes sense?

Cheers, Axel.

Thank you,

Honestly I don’t understand fully how lambda works.
But this form works!!!

Thank you!!!

Basically it is an unnamed function you define when you need it. You can find many simple introduction tutorials to it on the web. For instance this one:

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