# Symbolic summation in function with parameters definition

Hello dear Rooters,

I’ve written a program to identify pulse overlap. What I want is to separate the pulses overlapping and then extract the pulse’s features individually (rise time, maximum, area, etc). To do that, I fit a TGraph instance containing the isolated pulse overlap. The TF1 function that fits a single pulse has two parameters, f(t; [0], [1]), so when I want to fit the pulse overlap the fitting function has a form like `F(t; [0], ..., [2n-1]) = f(t; [0], [1]) + f(t-t1; [2], [3]) + ... + f(t-tn; [2n-2], [2n-1])`, where n is the number of pulses overlapping. n is unknown a priori.

I’d like to write that sum as a sigma summation with n as parameter to be provided, `F(t; [0],...,[2n-1]) = summation( f(t-ti); [2i-2], [2i-1], i = 1..n)`, so that the fitting method creates the necessary number of f() functions every time that it’s called, without having to define a function for every n case ( n up to 5 in practical cases). This would save me a lot of repetitive lines and actually would cover many more n cases (as I won’t write down fitting functions for every n case).

I already looked it up on the ROOT User Guide but I didn’t find anything. Hope I was just no exhaustive in my search and you can help me by posting any relevant/important documentation.

Daniel

Hi,

The way to implement this is to build a new function object, a functor, implementing the summation in the evaluation method ( double operator() (const double * x, const double *p) using the original TF1.
The functor object will contain in its state (as data member) the original TF1.

From the functor object you can then create a TF1 for fitting, see

root.cern.ch/root/htmldoc/TF1.html#F4

Best Regards

Lorenzo

Thanks for the suggestion!

Did you mean something like this?

``````class  exp_functor {
public:

exp_functor(int nn){ nHits = nn;}

double operator() (double *x, double *p) {
double fit = 0.;
for(int i = 0; i < nHits; i++)  fit += hit(x[0], p[3*i], p[3*i+1], p[3*i+2]);
return fit;
}

//this is the fit function for one hit
double hit(double x, double p0, double p1, double p2){

double h = p0*(x-p1)*exp(-(x-p1)/p2);
if(x < p1) return 0.;
else return h;
}

private:
int nHits;
};``````

I didn’t use a TF1 member though. How would you have done it?

Thanks!

A small note. You need:
double operator() (const double *x, const double *p)

Hi,

Yes, I meant something like this. Yes, there is no need to have a TF1 member inside.

Lorenzo