MathUtil.h
Go to the documentation of this file.
1 #ifndef MATHUTIL_H
2 #define MATHUTIL_H
3
4 //////////////////////////////////////////////////////////////////////////
5 // //
6 // \file MathUtil.h //
7 // //
8 // Simple mathematical functions, initially to prevent the abuse of //
9 // pow() //
10 // <bckhouse@caltech.edu> //
11 // //
12 //////////////////////////////////////////////////////////////////////////
13
14 #include <cmath>
15 #include <vector>
16
17 namespace util{
18
19  /// \name Simple mathematical functions
20  //@{
21
22  /// More efficient square function than pow(x,2)
23  template<class T> inline T sqr(T x){return x*x;}
24
25  /// More efficient cube function than pow(x,3)
26  template<class T> inline T cube(T x){return x*x*x;}
27
28  /// More efficient exponentiation function than pow(x,n) for small n
29  template<class T> inline T ipow(T x, unsigned int n)
30  {
31  T ret = 1;
32  if (n == 0) return ret;
33  for(unsigned int i = 1; i <= n; ++i) ret *= x;
34  return ret;
35  }
36
37
38  /// 2D Euclidean distance
39  inline double pythag(double x, double y)
40  {
41  return sqrt(sqr(x)+sqr(y));
42  }
43
44  /// 3D Euclidean distance
45  inline double pythag(double x, double y, double z)
46  {
47  return sqrt(sqr(x)+sqr(y)+sqr(z));
48  }
49
50  /// \brief Simplified version of \ref LinFit
51  ///
52  /// All weights are assumed 1. In the absence of ability to weight, the
53  /// returned chi-square doesn't mean much and is omitted.
54  void LinFitUnweighted(const std::vector<double>& x,
55  const std::vector<double>& y,
56  double& m, double &c);
57
58  /// \brief Find the best-fit line to a collection of points in 2-D by
59  /// minimizing the squared vertical distance from the points to the line.
60  ///
61  /// In the common case of unweighted data with Gaussian errors, you should
62  /// set the points weights to 1/sigma^2 for each point.
63  ///
64  /// \param x - input vector of x coordinates
65  /// \param y - input vector of y coordinates
66  /// \param w - input vector of weights for the points
67  /// \param m - output gradient of the fit line
68  /// \param c - output y-intercept of the fit line
69  /// \return The chi^2 value defined by chi^2 = sum_i[w_i d^2_i]
70  double LinFit(const std::vector<double>& x,
71  const std::vector<double>& y,
72  const std::vector<double>& w,
73  double& m, double &c);
74
75  /// \brief Best fit line to points using Theil-Sens median method
76  ///
77  /// An implementation of the Theil-Sens straight line estimator
78  /// (http://en.wikipedia.org/wiki/Theil-Sen_estimator)
79  ///
80  /// Theil-Sens sets the slope to be the median slope of data-point
81  /// pairs and the intercept to the median value for data points
82  /// computed using the median slope. Theil-Sens is more robust
83  /// against the presence of noise.
84  ///
85  /// The weighting method is controlled by "wmode":
86  ///
87  /// * wmode=0 : pairs of points will be assigned the same weights
88  /// (that is, unweighted)
89  ///
90  /// * wmode=1 : pairs of points will be assigned weights equal to
91  /// sqrt(w[i]*w[j])
92  ///
93  /// * wmode=2 : pairs of points will be assigned weights sqrt(w1*w1*d^2)
94  /// where d is the distance between the points.
95  ///
96  /// \param x - input vector of x coordinates
97  /// \param y - input vector of y coordinates
98  /// \param w - input vector of weights
99  /// \param m - on return, the slope value
100  /// \param b - on return, the intercept value
101  /// \param wmode - weight mode
102  ///
103  void LinFitTS(const std::vector<double>& x,
104  const std::vector<double>& y,
105  const std::vector<double>& w,
106  double& m,
107  double& b,
108  int wmode);
109
110  //@}
111
112 } // end namespace
113
114 #endif // MATHUTIL_H
Filter events based on their run/event numbers.
T sqrt(T number)
Definition: d0nt_math.hpp:156
T cube(T x)
More efficient cube function than pow(x,3)
Definition: MathUtil.h:26
T sqr(T x)
More efficient square function than pow(x,2)
Definition: MathUtil.h:23
T ipow(T x, unsigned int n)
More efficient exponentiation function than pow(x,n) for small n.
Definition: MathUtil.h:29
z
Definition: test.py:28
void LinFitUnweighted(const std::vector< double > &x, const std::vector< double > &y, double &m, double &c)
Simplified version of LinFit.
Definition: MathUtil.cxx:8
double pythag(double x, double y)
2D Euclidean distance
Definition: MathUtil.h:29
const hit & b
Definition: hits.cxx:21
double LinFit(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &w, double &m, double &c)
Find the best-fit line to a collection of points in 2-D by minimizing the squared vertical distance f...
Definition: MathUtil.cxx:36
double T
Definition: Xdiff_gwt.C:5
Float_t w
Definition: plot.C:20
void LinFitTS(const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &wt, double &m, double &b, int wmode)
Best fit line to points using Theil-Sens median method.
Definition: MathUtil.cxx:104