Logo ROOT  
Reference Guide
Integration.C File Reference

Detailed Description

View in nbviewer Open in SWAN Numerical integration using R passing the function from ROOT

#include<TMath.h>
#include<TRInterface.h>
#include<TF1.h>
//To integrate using R the function must be vectorized
//The idea is just to receive a vector like an argument,to evaluate
//every element saving the result in another vector
//and return the resultant vector.
std::vector<Double_t> BreitWignerVectorized(std::vector<Double_t> xx)
{
std::vector<Double_t> result(xx.size());
for(Int_t i=0;i<xx.size();i++)
{
result[i]=TMath::BreitWigner(xx[i]);
}
return result;
}
double BreitWignerWrap( double x){
}
{
r["BreitWigner"]=ROOT::R::TRFunctionExport(BreitWignerVectorized);
Double_t value=r.Eval("integrate(BreitWigner, lower = -2, upper = 2)$value");
std::cout.precision(18);
std::cout<<"Integral of the BreitWigner Function in the interval [-2, 2] R = "<<value<<std::endl;
ROOT::Math::WrappedFunction<> wf(BreitWignerWrap);
value=i.Integral(-2,2);
std::cout<<"Integral of the BreitWigner Function in the interval [-2, 2] MathMore = "<<value<<std::endl;
TF1 f1("BreitWigner","BreitWignerWrap(x)");
value=f1.Integral(-2,2);
std::cout<<"Integral of the BreitWigner Function in the interval [-2, 2] TF1 = "<<value<<std::endl;
// infinite limits
value=r.Eval("integrate(BreitWigner, lower = -Inf, upper = Inf)$value");
std::cout<<"Integral of BreitWigner Function in the interval [-Inf, Inf] R = "<<value<<std::endl;
}
Author

Definition in file Integration.C.

ROOT::R::TRFunctionExport
Definition: TRFunctionExport.h:153
ROOT::Math::WrappedFunction
Template class to wrap any C++ callable object which takes one argument i.e.
Definition: WrappedFunction.h:94
Integrator.h
TF1::Integral
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition: TF1.cxx:2522
r
ROOT::R::TRInterface & r
Definition: Object.C:4
ROOT::R::TRInterface::Eval
Int_t Eval(const TString &code, TRObject &ans)
Method to eval R code and you get the result in a reference to TRObject.
Definition: TRInterface.cxx:78
ROOT::R::TRInterface
Definition: TRInterface.h:137
Int_t
int Int_t
Definition: RtypesCore.h:45
x
Double_t x[n]
Definition: legend1.C:17
TRInterface.h
TMath::BreitWigner
Double_t BreitWigner(Double_t x, Double_t mean=0, Double_t gamma=1)
Calculate a Breit Wigner function with mean and gamma.
Definition: TMath.cxx:437
f1
TF1 * f1
Definition: legend1.C:11
Double_t
double Double_t
Definition: RtypesCore.h:59
TF1.h
ROOT::R::TRInterface::Instance
static TRInterface & Instance()
static method to get an TRInterface instance reference
Definition: TRInterface.cxx:185
ROOT::Math::IntegratorOneDim
User Class for performing numerical integration of a function in one dimension.
Definition: Integrator.h:95
TF1
1-Dim function class
Definition: TF1.h:212
RooFit::Integration
@ Integration
Definition: RooGlobalFunc.h:67
TMath.h