forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegration.C
More file actions
59 lines (46 loc) · 1.7 KB
/
Integration.C
File metadata and controls
59 lines (46 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// \file
/// \ingroup tutorial_r
/// \notebook -nodraw
/// Numerical integration using R passing the function from ROOT
///
/// \macro_code
///
/// \author
#include<TMath.h>
#include<TRInterface.h>
#include<Math/Integrator.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){
return TMath::BreitWigner(x);
}
void Integration()
{
ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
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);
ROOT::Math::Integrator i(wf);
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;
}