CMS 3D CMS Logo

NumericalIntegration.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_NumericalIntegration_h
2 #define PhysicsTools_Utilities_NumericalIntegration_h
3 /*
4  * Numerical integration utilities
5  *
6  * \author: Luca Lista
7  * Gauss Legendre and Gauss algorithms based on ROOT implementation
8  *
9  */
10 #include "Math/Functor.h"
11 #include "Math/Integrator.h"
12 #include "Math/AllIntegrationTypes.h"
13 #include <vector>
14 #include <cmath>
15 #include <memory>
16 
17 namespace funct {
18 
19  template <typename F>
20  double trapezoid_integral(const F& f, double min, double max, unsigned int samples) {
21  const double l = max - min, delta = l / samples;
22  double sum = 0;
23  for (unsigned int i = 0; i < samples; ++i) {
24  sum += f(min + (i + 0.5) * delta);
25  }
26  return sum * delta;
27  }
28 
30  public:
32  explicit TrapezoidIntegrator(unsigned int samples) : samples_(samples) {}
33  template <typename F>
34  double operator()(const F& f, double min, double max) const {
35  return trapezoid_integral(f, min, max, samples_);
36  }
37 
38  private:
39  unsigned int samples_;
40  };
41 
43  public:
45  GaussLegendreIntegrator(unsigned int samples, double epsilon);
46  template <typename F>
47  double operator()(const F& f, double min, double max) const {
48  a0 = 0.5 * (max + min);
49  b0 = 0.5 * (max - min);
50  result = 0.0;
51  for (i = 0; i < samples_; ++i) {
52  result += w[i] * f(a0 + b0 * x[i]);
53  }
54 
55  return result * b0;
56  }
57 
58  private:
59  unsigned int samples_;
60  std::vector<double> x, w;
61  mutable double a0, b0, result;
62  mutable unsigned int i;
63  };
64 
66  public:
69  template <typename F>
70  double operator()(const F& f, double a, double b) const {
71  h = 0;
72  if (b == a)
73  return h;
74  aconst = kCST / std::abs(b - a);
75  bb = a;
76  CASE1:
77  aa = bb;
78  bb = b;
79  CASE2:
80  c1 = kHF * (bb + aa);
81  c2 = kHF * (bb - aa);
82  s8 = 0;
83  for (i = 0; i < 4; ++i) {
84  u = c2 * x[i];
85  xx = c1 + u;
86  f1 = f(xx);
87  xx = c1 - u;
88  f2 = f(xx);
89  s8 += w[i] * (f1 + f2);
90  }
91  s16 = 0;
92  for (i = 4; i < 12; ++i) {
93  u = c2 * x[i];
94  xx = c1 + u;
95  f1 = f(xx);
96  xx = c1 - u;
97  f2 = f(xx);
98  s16 += w[i] * (f1 + f2);
99  }
100  s16 = c2 * s16;
101  if (std::abs(s16 - c2 * s8) <= epsilon_ * (1. + std::abs(s16))) {
102  h += s16;
103  if (bb != b)
104  goto CASE1;
105  } else {
106  bb = c1;
107  if (1. + aconst * std::abs(c2) != 1)
108  goto CASE2;
109  h = s8;
110  }
111 
112  error_ = std::abs(s16 - c2 * s8);
113  return h;
114  }
115  double error() const { return error_; }
116 
117  private:
118  mutable double error_;
119  double epsilon_;
120  static const double x[12], w[12];
121  static const double kHF;
122  static const double kCST;
123  mutable double h, aconst, bb, aa, c1, c2, u, s8, s16, f1, f2, xx;
124  mutable unsigned int i;
125  };
126 
127  struct RootIntegrator {
128  RootIntegrator(ROOT::Math::IntegrationOneDim::Type type = ROOT::Math::IntegrationOneDim::kADAPTIVE,
129  double absTol = 1e-9,
130  double relTol = 1e-6,
131  unsigned int size = 1000,
132  unsigned int rule = 3)
133  : type_(type),
134  absTol_(absTol),
135  relTol_(relTol),
136  size_(size),
137  rule_(rule),
138  integrator_(new ROOT::Math::Integrator(type, absTol, relTol, size, rule)) {}
140  type_ = o.type_;
141  absTol_ = o.absTol_;
142  relTol_ = o.relTol_;
143  size_ = o.size_;
144  rule_ = o.rule_;
145  integrator_.reset(new ROOT::Math::Integrator(type_, absTol_, relTol_, size_, rule_));
146  }
148  type_ = o.type_;
149  absTol_ = o.absTol_;
150  relTol_ = o.relTol_;
151  size_ = o.size_;
152  rule_ = o.rule_;
153  integrator_.reset(new ROOT::Math::Integrator(type_, absTol_, relTol_, size_, rule_));
154  return *this;
155  }
156  template <typename F>
157  double operator()(const F& f, double a, double b) const {
158  ROOT::Math::Functor1D wrapper(&f, &F::operator());
159  integrator_->SetFunction(wrapper);
160  return integrator_->Integral(a, b);
161  }
162 
163  private:
165  double absTol_, relTol_;
166  unsigned int size_, rule_;
167  mutable std::unique_ptr<ROOT::Math::Integrator> integrator_;
168  };
169 } // namespace funct
170 
171 #endif
funct::RootIntegrator::type_
ROOT::Math::IntegrationOneDim::Type type_
Definition: NumericalIntegration.h:164
mps_fire.i
i
Definition: mps_fire.py:428
funct::GaussIntegrator::f1
double f1
Definition: NumericalIntegration.h:123
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
min
T min(T a, T b)
Definition: MathUtil.h:58
funct::GaussIntegrator::h
double h
Definition: NumericalIntegration.h:123
funct::RootIntegrator::size_
unsigned int size_
Definition: NumericalIntegration.h:166
funct::GaussIntegrator::kCST
static const double kCST
Definition: NumericalIntegration.h:122
funct::RootIntegrator
Definition: NumericalIntegration.h:127
funct::GaussLegendreIntegrator::b0
double b0
Definition: NumericalIntegration.h:61
wrapper
static HepMC::HEPEVT_Wrapper wrapper
Definition: BeamHaloProducer.cc:47
EcalTangentSkim_cfg.o
o
Definition: EcalTangentSkim_cfg.py:36
geometryDiff.epsilon
int epsilon
Definition: geometryDiff.py:26
Utilities.operator
operator
Definition: Utilities.py:24
EgammaValidation_cff.samples
samples
Definition: EgammaValidation_cff.py:19
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
funct::GaussIntegrator::bb
double bb
Definition: NumericalIntegration.h:123
funct::GaussIntegrator::f2
double f2
Definition: NumericalIntegration.h:123
susybsm::HSCParticleType::Type
Type
Definition: HSCParticle.h:20
funct::GaussIntegrator::kHF
static const double kHF
Definition: NumericalIntegration.h:121
funct::GaussIntegrator::u
double u
Definition: NumericalIntegration.h:123
h
funct::GaussIntegrator::operator()
double operator()(const F &f, double a, double b) const
Definition: NumericalIntegration.h:70
b
double b
Definition: hdecay.h:118
funct::trapezoid_integral
double trapezoid_integral(const F &f, double min, double max, unsigned int samples)
Definition: NumericalIntegration.h:20
funct::GaussLegendreIntegrator::operator()
double operator()(const F &f, double min, double max) const
Definition: NumericalIntegration.h:47
funct::GaussLegendreIntegrator::result
double result
Definition: NumericalIntegration.h:61
funct::TrapezoidIntegrator::operator()
double operator()(const F &f, double min, double max) const
Definition: NumericalIntegration.h:34
funct::GaussIntegrator::error
double error() const
Definition: NumericalIntegration.h:115
funct::RootIntegrator::operator=
RootIntegrator & operator=(const RootIntegrator &o)
Definition: NumericalIntegration.h:147
a
double a
Definition: hdecay.h:119
funct::GaussLegendreIntegrator::samples_
unsigned int samples_
Definition: NumericalIntegration.h:59
funct::GaussLegendreIntegrator
Definition: NumericalIntegration.h:42
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
funct::RootIntegrator::relTol_
double relTol_
Definition: NumericalIntegration.h:165
funct::GaussLegendreIntegrator::i
unsigned int i
Definition: NumericalIntegration.h:62
dumpMFGeometry_cfg.delta
delta
Definition: dumpMFGeometry_cfg.py:25
funct::RootIntegrator::RootIntegrator
RootIntegrator(ROOT::Math::IntegrationOneDim::Type type=ROOT::Math::IntegrationOneDim::kADAPTIVE, double absTol=1e-9, double relTol=1e-6, unsigned int size=1000, unsigned int rule=3)
Definition: NumericalIntegration.h:128
funct::GaussIntegrator::xx
double xx
Definition: NumericalIntegration.h:123
A
funct::TrapezoidIntegrator
Definition: NumericalIntegration.h:29
funct::GaussIntegrator::i
unsigned int i
Definition: NumericalIntegration.h:124
funct::GaussIntegrator::w
static const double w[12]
Definition: NumericalIntegration.h:120
funct::GaussIntegrator::GaussIntegrator
GaussIntegrator()
Definition: NumericalIntegration.h:67
funct::GaussIntegrator::s16
double s16
Definition: NumericalIntegration.h:123
funct::GaussIntegrator::s8
double s8
Definition: NumericalIntegration.h:123
cmsLHEtoEOSManager.l
l
Definition: cmsLHEtoEOSManager.py:204
funct::GaussLegendreIntegrator::GaussLegendreIntegrator
GaussLegendreIntegrator()
Definition: NumericalIntegration.h:44
funct::RootIntegrator::operator()
double operator()(const F &f, double a, double b) const
Definition: NumericalIntegration.h:157
funct::GaussIntegrator::c2
double c2
Definition: NumericalIntegration.h:123
funct::TrapezoidIntegrator::TrapezoidIntegrator
TrapezoidIntegrator(unsigned int samples)
Definition: NumericalIntegration.h:32
funct::GaussIntegrator::x
static const double x[12]
Definition: NumericalIntegration.h:120
funct::GaussIntegrator::c1
double c1
Definition: NumericalIntegration.h:123
funct::RootIntegrator::rule_
unsigned int rule_
Definition: NumericalIntegration.h:166
funct::GaussIntegrator::epsilon_
double epsilon_
Definition: NumericalIntegration.h:119
funct::GaussIntegrator::aa
double aa
Definition: NumericalIntegration.h:123
funct::RootIntegrator::integrator_
std::unique_ptr< ROOT::Math::Integrator > integrator_
Definition: NumericalIntegration.h:167
funct::RootIntegrator::RootIntegrator
RootIntegrator(const RootIntegrator &o)
Definition: NumericalIntegration.h:139
funct::TrapezoidIntegrator::samples_
unsigned int samples_
Definition: NumericalIntegration.h:39
funct::TrapezoidIntegrator::TrapezoidIntegrator
TrapezoidIntegrator()
Definition: NumericalIntegration.h:31
funct::GaussLegendreIntegrator::a0
double a0
Definition: NumericalIntegration.h:61
funct::GaussLegendreIntegrator::w
std::vector< double > w
Definition: NumericalIntegration.h:60
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
funct::RootIntegrator::absTol_
double absTol_
Definition: NumericalIntegration.h:165
funct::GaussLegendreIntegrator::x
std::vector< double > x
Definition: NumericalIntegration.h:60
ROOT
Definition: Transform3DPJ.h:35
funct
Definition: Abs.h:5
funct::GaussIntegrator::aconst
double aconst
Definition: NumericalIntegration.h:123
funct::GaussIntegrator::GaussIntegrator
GaussIntegrator(double epsilon)
Definition: NumericalIntegration.h:68
funct::GaussIntegrator
Definition: NumericalIntegration.h:65
funct::GaussIntegrator::error_
double error_
Definition: NumericalIntegration.h:118
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37