# Integrate a user defined function

Hello,

I have a certain equation, which I need to integrate

In this equation, the variable of integration is Egamma.
I have plotted the functions Txl and Rho(Ex-Eg,J) as graphs as a function of energy. Using these graphs, I plan to interpolate and extract the values for these functions at all values of Eg from 0 to Emax.

How do I within a user defined function, set the parameters for the equation defined above??

thanks

Please read tips for efficient and successful posting and posting code

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

I guess the fit tutorials can help you. May be you saw them already ? @moneta may have more ideas.

yes, I did see the examples.

I just can’t figure out a way to set the parameter within a user defined - such that it always sets its value as a interpolation from a TGraph.
And then integrate the full function for all values of energy.

Any help, thanks a lot Ok, I let @moneta have a look at your problem.

1 Like

Hi,
For integrating you need to create a TF1 or just a simple C++ functor object that you give as input to the `ROOT::Math::IntegratorOneDim` class. If you want to parametrize the integrand function better using a TF1. How did you implement Txl(E) and Rho(E) functions ? Are functions or graphs ?
If there are functions it is easy to create a new functions which is the product of the two. See the different way of creating a TF1 here.
If there are represented as TGraph objects and you evaluate them with TGraph::Eval, you can still do as following:

``````auto integFunction = [&](double x) { return Txl->Eval(x) * rho->Eval(x); };
ROOT::Math::Functor1D f(integFunction);
ROOT::Math::Integrator ig(f);
``````

and then integrate using `Integrator::Integral(x1,x2);`

Cheers

Lorenzo

Thanks for the reply.

Just to understand,

auto integFunction = [&](double x) { return Txl->Eval(x) * rho->Eval(x)};

this is creating a C++ functor.
How does the value of x vary?

what’s ig(f) in the following equation -

ROOT::Math::Integrator ig(f);

In the integral , x1 = 0 and x2 = Emax.

Integrator::Integral(x1,x2);

thanks

Hi,
integFunction is defined as a function of x. You can do for example `integFunction(1.)` or `integFunction(2.)`.
`ig` is an instance of the Integrator class which is built passing the C++ object. It takes an argument an object implementing the `ROOT::Math::IGenFunction` interface, and for this you need to wrap the functor integFunction in that interface. This is done using the `ROOT::Math::Functor1D` class.
After building the integrator you just call : `ig.Integral(0,Emax)`.

Lorenzo

Thanks so much,
I have included it, this way in the program -

auto integFunction = [&](double x) { return gGtransmission->Eval(x) * gld->Eval(x); };
ROOT::Math::Functor1D f(integFunction);
ROOT::Math::Integrator ig(f);

``````Double_t IG = 	ig.Integral(0,ExCurrent);
``````

The integFunction - is defined as a function of x, which is basically energy ranging from 0 to ExCurrent. These are also the limits for the integration.
Does the above snippet work that way?
integFunction will be evaluated for all values of x between 0 to ExCurrent and then the same will be fed into the integration and the integral to be calculated.

Sorry, if my question is naive. I haven’t used C++ Functor previously.

Thanks
Mansi

Hi,
Yes, this should work. If you have any problem with your code, share it here and we can have a look

Cheers

Lorenzo

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