CMS 3D CMS Logo

Integral.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_Integral_h
2 #define PhysicsTools_Utilities_Integral_h
5 
6 namespace funct {
7 
8  struct no_var;
9 
10  template <typename F, typename X = no_var>
11  struct IntegralStruct {
12  IntegralStruct(const F& f) : p(primitive<X>(f)) {}
13  double operator()(double min, double max) const {
14  X::set(min);
15  double pMin = p();
16  X::set(max);
17  double pMax = p();
18  return pMax - pMin;
19  }
20 
21  private:
23  };
24 
25  template <typename F>
26  struct IntegralStruct<F> {
27  IntegralStruct(const F& f) : p(primitive(f)) {}
28  double operator()(double min, double max) const { return p(max) - p(min); }
29 
30  private:
32  };
33 
34  template <typename Integrator, typename F, typename X = no_var>
36  NumericalIntegral(const F& f, const Integrator& integrator) : f_(f), integrator_(integrator) {}
37  inline double operator()(double min, double max) const { return integrator_(f_, min, max); }
38 
39  private:
40  struct function {
41  function(const F& f) : f_(f) {}
42  inline double operator()(double x) const {
43  X::set(x);
44  return f_();
45  }
46 
47  private:
48  F f_;
49  };
50  function f_;
51  Integrator integrator_;
52  };
53 
54  template <typename Integrator, typename F>
55  struct NumericalIntegral<Integrator, F, no_var> {
56  NumericalIntegral(const F& f, const Integrator& integrator) : f_(f), integrator_(integrator) {}
57  double operator()(double min, double max) const { return integrator_(f_, min, max); }
58 
59  private:
60  F f_;
61  Integrator integrator_;
62  };
63 
64  template <typename F, typename X = no_var>
65  struct Integral {
67  };
68 
69  template <typename X, typename F>
70  typename Integral<F, X>::type integral(const F& f) {
71  return typename Integral<F, X>::type(f);
72  }
73 
74  template <typename X, typename F, typename Integrator>
75  typename Integral<F, X>::type integral(const F& f, const Integrator& integrator) {
76  return typename Integral<F, X>::type(f, integrator);
77  }
78 
79  template <typename F, typename Integrator>
80  typename Integral<F>::type integral_f(const F& f, const Integrator& integrator) {
81  return typename Integral<F>::type(f, integrator);
82  }
83 
84  template <typename X, typename F>
85  double integral(const F& f, double min, double max) {
86  return integral<X>(f)(min, max);
87  }
88 
89  template <typename X, typename F, typename Integrator>
90  double integral(const F& f, double min, double max, const Integrator& integrator) {
91  return integral<X>(f, integrator)(min, max);
92  }
93 
94  template <typename F>
95  typename Integral<F>::type integral_f(const F& f) {
96  return typename Integral<F>::type(f);
97  }
98 
99  template <typename F>
100  double integral_f(const F& f, double min, double max) {
101  return integral_f(f)(min, max);
102  }
103 
104  template <typename F, typename Integrator>
105  double integral_f(const F& f, double min, double max, const Integrator& integrator) {
106  return integral_f(f, integrator)(min, max);
107  }
108 
109  template <typename F, typename MIN, typename MAX, typename Integrator = no_var, typename X = no_var>
110  struct DefIntegral {
111  DefIntegral(const F& f, const MIN& min, const MAX& max, const Integrator& integrator)
112  : f_(f), min_(min), max_(max), integrator_(integrator) {}
113  double operator()() const { return integral<X>(f_, min_(), max_(), integrator_); }
114 
115  private:
116  F f_;
117  MIN min_;
118  MAX max_;
119  Integrator integrator_;
120  };
121 
122  template <typename F, typename MIN, typename MAX, typename X>
123  struct DefIntegral<F, MIN, MAX, no_var, X> {
124  DefIntegral(const F& f, const MIN& min, const MAX& max) : f_(f), min_(min), max_(max) {}
125  double operator()(double x) const { return integral<X>(f_, min_(x), max_(x)); }
126 
127  private:
128  F f_;
129  MIN min_;
130  MAX max_;
131  };
132 
133  template <typename F, typename MIN, typename MAX, typename Integrator>
134  struct DefIntegral<F, MIN, MAX, Integrator, no_var> {
135  DefIntegral(const F& f, const MIN& min, const MAX& max, const Integrator& integrator)
136  : f_(f), min_(min), max_(max), integrator_(integrator) {}
137  double operator()(double x) const { return integral_f(f_, min_(x), max_(x), integrator_); }
138 
139  private:
140  F f_;
141  MIN min_;
142  MAX max_;
143  Integrator integrator_;
144  };
145 
146  template <typename F, typename MIN, typename MAX>
147  struct DefIntegral<F, MIN, MAX, no_var, no_var> {
148  DefIntegral(const F& f, const MIN& min, const MAX& max) : f_(f), min_(min), max_(max) {}
149  double operator()(double x) const { return integral_f(f_, min_(x), max_(x)); }
150 
151  private:
152  F f_;
153  MIN min_;
154  MAX max_;
155  };
156 
157 } // namespace funct
158 
159 #define NUMERICAL_INTEGRAL(X, F, INTEGRATOR) \
160  namespace funct { \
161  template <typename X> \
162  struct Integral<F, X> { \
163  typedef NumericalIntegral<INTEGRATOR, F, X> type; \
164  }; \
165  } \
166  struct __useless_ignoreme
167 
168 #define NUMERICAL_FUNCT_INTEGRAL(F, INTEGRATOR) \
169  namespace funct { \
170  template <> \
171  struct Integral<F, no_var> { \
172  typedef NumericalIntegral<INTEGRATOR, F> type; \
173  }; \
174  } \
175  struct __useless_ignoreme
176 
177 #endif
funct::IntegralStruct::operator()
double operator()(double min, double max) const
Definition: Integral.h:13
funct::DefIntegral< F, MIN, MAX, Integrator, no_var >::operator()
double operator()(double x) const
Definition: Integral.h:137
funct::DefIntegral< F, MIN, MAX, Integrator, no_var >::min_
MIN min_
Definition: Integral.h:141
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
min
T min(T a, T b)
Definition: MathUtil.h:58
funct::DefIntegral< F, MIN, MAX, no_var, no_var >::operator()
double operator()(double x) const
Definition: Integral.h:149
funct::NumericalIntegral::integrator_
Integrator integrator_
Definition: Integral.h:51
funct::NumericalIntegral< Integrator, F, no_var >::operator()
double operator()(double min, double max) const
Definition: Integral.h:57
funct::integral_f
Integral< F >::type integral_f(const F &f, const Integrator &integrator)
Definition: Integral.h:80
funct::Integral
Definition: Integral.h:65
funct::X
Definition: Variables.h:51
funct::DefIntegral::f_
F f_
Definition: Integral.h:116
funct::DefIntegral< F, MIN, MAX, Integrator, no_var >::f_
F f_
Definition: Integral.h:140
funct::DefIntegral< F, MIN, MAX, no_var, no_var >::min_
MIN min_
Definition: Integral.h:153
funct::IntegralStruct< F >::p
Primitive< F >::type p
Definition: Integral.h:31
funct::DefIntegral< F, MIN, MAX, no_var, X >::operator()
double operator()(double x) const
Definition: Integral.h:125
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
funct::UndefinedIntegral
Definition: Primitive.h:17
AlignmentTrackSelector_cfi.pMax
pMax
Definition: AlignmentTrackSelector_cfi.py:14
funct::DefIntegral< F, MIN, MAX, no_var, X >::min_
MIN min_
Definition: Integral.h:129
funct::NumericalIntegral< Integrator, F, no_var >::NumericalIntegral
NumericalIntegral(const F &f, const Integrator &integrator)
Definition: Integral.h:56
funct::integral
Integral< F, X >::type integral(const F &f)
Definition: Integral.h:70
funct::DefIntegral::DefIntegral
DefIntegral(const F &f, const MIN &min, const MAX &max, const Integrator &integrator)
Definition: Integral.h:111
Primitive.h
funct::DefIntegral< F, MIN, MAX, no_var, no_var >::DefIntegral
DefIntegral(const F &f, const MIN &min, const MAX &max)
Definition: Integral.h:148
funct::Integral::type
IntegralStruct< F, X > type
Definition: Integral.h:66
funct::IntegralStruct::IntegralStruct
IntegralStruct(const F &f)
Definition: Integral.h:12
funct::DefIntegral::min_
MIN min_
Definition: Integral.h:117
funct::DefIntegral< F, MIN, MAX, no_var, X >::max_
MAX max_
Definition: Integral.h:130
funct::IntegralStruct< F >::IntegralStruct
IntegralStruct(const F &f)
Definition: Integral.h:27
funct::DefIntegral::operator()
double operator()() const
Definition: Integral.h:113
funct::NumericalIntegral< Integrator, F, no_var >::integrator_
Integrator integrator_
Definition: Integral.h:61
funct::DefIntegral< F, MIN, MAX, Integrator, no_var >::DefIntegral
DefIntegral(const F &f, const MIN &min, const MAX &max, const Integrator &integrator)
Definition: Integral.h:135
funct::IntegralStruct
Definition: Integral.h:11
funct::primitive
Primitive< F, X >::type primitive(const F &f)
Definition: Primitive.h:41
funct::X::set
static void set(const double &x)
Definition: Variables.h:51
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
ALCARECOTkAlMinBias_cff.pMin
pMin
GeV.
Definition: ALCARECOTkAlMinBias_cff.py:35
funct::NumericalIntegral::function::operator()
double operator()(double x) const
Definition: Integral.h:42
funct::NumericalIntegral::function
Definition: Integral.h:40
funct::IntegralStruct::p
Primitive< F, X >::type p
Definition: Integral.h:22
funct::DefIntegral< F, MIN, MAX, Integrator, no_var >::integrator_
Integrator integrator_
Definition: Integral.h:143
funct::DefIntegral::max_
MAX max_
Definition: Integral.h:118
funct::NumericalIntegral< Integrator, F, no_var >::f_
F f_
Definition: Integral.h:60
NumericalIntegration.h
funct::DefIntegral< F, MIN, MAX, no_var, X >::DefIntegral
DefIntegral(const F &f, const MIN &min, const MAX &max)
Definition: Integral.h:124
funct::NumericalIntegral
Definition: Integral.h:35
funct::IntegralStruct< F >::operator()
double operator()(double min, double max) const
Definition: Integral.h:28
funct::DefIntegral< F, MIN, MAX, no_var, X >::f_
F f_
Definition: Integral.h:128
funct::NumericalIntegral::f_
function f_
Definition: Integral.h:50
funct::NumericalIntegral::function::f_
F f_
Definition: Integral.h:48
funct::DefIntegral::integrator_
Integrator integrator_
Definition: Integral.h:119
funct
Definition: Abs.h:5
funct::DefIntegral< F, MIN, MAX, no_var, no_var >::max_
MAX max_
Definition: Integral.h:154
funct::NumericalIntegral::NumericalIntegral
NumericalIntegral(const F &f, const Integrator &integrator)
Definition: Integral.h:36
funct::DefIntegral< F, MIN, MAX, no_var, no_var >::f_
F f_
Definition: Integral.h:152
funct::NumericalIntegral::operator()
double operator()(double min, double max) const
Definition: Integral.h:37
funct::DefIntegral< F, MIN, MAX, Integrator, no_var >::max_
MAX max_
Definition: Integral.h:142
funct::DefIntegral
Definition: Integral.h:110