 ROOT   Reference Guide Integration.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_r
3/// \notebook -nodraw
4/// Numerical integration using R passing the function from ROOT
5///
6/// \macro_code
7///
8/// \author
9
10#include<TMath.h>
11#include<TRInterface.h>
12#include<Math/Integrator.h>
13#include<TF1.h>
14
15//To integrate using R the function must be vectorized
16//The idea is just to receive a vector like an argument,to evaluate
17//every element saving the result in another vector
18//and return the resultant vector.
19std::vector<Double_t> BreitWignerVectorized(std::vector<Double_t> xx)
20{
21 std::vector<Double_t> result(xx.size());
22 for(Int_t i=0;i<xx.size();i++)
23 {
24 result[i]=TMath::BreitWigner(xx[i]);
25 }
26 return result;
27}
28
29double BreitWignerWrap( double x){
30 return TMath::BreitWigner(x);
31}
32
33
34void Integration()
35{
37
38 r["BreitWigner"]=ROOT::R::TRFunctionExport(BreitWignerVectorized);
39
40 Double_t value=r.Eval("integrate(BreitWigner, lower = -2, upper = 2)$value"); 41 42 std::cout.precision(18); 43 std::cout<<"Integral of the BreitWigner Function in the interval [-2, 2] R = "<<value<<std::endl; 44 45 46 ROOT::Math::WrappedFunction<> wf(BreitWignerWrap); 48 value=i.Integral(-2,2); 49 std::cout<<"Integral of the BreitWigner Function in the interval [-2, 2] MathMore = "<<value<<std::endl; 50 51 52 TF1 f1("BreitWigner","BreitWignerWrap(x)"); 53 value=f1.Integral(-2,2); 54 std::cout<<"Integral of the BreitWigner Function in the interval [-2, 2] TF1 = "<<value<<std::endl; 55 56 // infinite limits 57 value=r.Eval("integrate(BreitWigner, lower = -Inf, upper = Inf)$value");
58 std::cout<<"Integral of BreitWigner Function in the interval [-Inf, Inf] R = "<<value<<std::endl;
59}
ROOT::R::TRInterface & r
Definition: Object.C:4
int Int_t
Definition: RtypesCore.h:45
double Double_t
Definition: RtypesCore.h:59
User Class for performing numerical integration of a function in one dimension.
Definition: Integrator.h:98
Template class to wrap any C++ callable object which takes one argument i.e.
This is a class to pass functions from ROOT to R.
ROOT R was implemented using the R Project library and the modules Rcpp and RInside
Definition: TRInterface.h:136
static TRInterface & Instance()
static method to get an TRInterface instance reference
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:80
1-Dim function class
Definition: TF1.h:213
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition: TF1.cxx:2521
Double_t x[n]
Definition: legend1.C:17
TF1 * f1
Definition: legend1.C:11
@ Integration
Definition: RooGlobalFunc.h:60
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