|
13 | 13 | #ifndef ROOT_Math_TDataPoint |
14 | 14 | #define ROOT_Math_TDataPoint |
15 | 15 |
|
16 | | -//ROOT include(s) |
| 16 | +// ROOT include(s) |
17 | 17 | #include "RtypesCore.h" |
18 | 18 |
|
| 19 | +#include <cassert> |
| 20 | +#include <math.h> |
19 | 21 |
|
20 | | -namespace ROOT |
21 | | -{ |
22 | | -namespace Math |
23 | | -{ |
| 22 | +/// \brief class representing a data point |
| 23 | +/// |
| 24 | +/// This class can be used for describing data points in a high-dimensional space. |
| 25 | +/// The (positive) dimension is specified by the first template parameter. The second |
| 26 | +/// template parameter can be used to tweak the precision of the stored coordinates. By |
| 27 | +/// default all coordinates are stored with 4 byte float precision. In addition to the |
| 28 | +/// coordinates a weight can be assigned to each data point allowing the representation |
| 29 | +/// of fields in high dimensions. |
| 30 | +/// Basic functionality for accessing/modifying the coordinates/weight are provided |
| 31 | +/// as well as a comparison method and the basic euclidean metric. |
24 | 32 |
|
| 33 | +namespace ROOT { |
| 34 | +namespace Math { |
25 | 35 |
|
26 | | -template<unsigned int K,typename _val_type = float> |
27 | | -class TDataPoint |
28 | | -{ |
| 36 | +template <unsigned int K, typename _val_type = float> |
| 37 | +class TDataPoint { |
29 | 38 | public: |
30 | 39 | typedef _val_type value_type; |
31 | 40 | enum { |
32 | | - kDimension = K //the dimensionality of this data point |
| 41 | + kDimension = K // the dimensionality of this data point |
33 | 42 | }; |
34 | | - static UInt_t Dimension() {return kDimension;} |
35 | | - TDataPoint(); |
36 | | -#ifndef __MAKECINT__ |
37 | | - template<typename _coord_typ> |
38 | | - TDataPoint(const _coord_typ* pData,_val_type fWeight = 1); |
39 | | -#endif |
40 | | - //virtual ~TDataPoint() {} |
| 43 | + static UInt_t Dimension() { return kDimension; } |
| 44 | + /// standard constructor |
| 45 | + /// sets the weight to 1 and initialises all coordinates with 0 |
| 46 | + TDataPoint() |
| 47 | + { |
| 48 | + // at least one dimension |
| 49 | + assert(kDimension > 0); |
| 50 | + |
| 51 | + for (UInt_t k = 0; k < K; ++k) |
| 52 | + m_vCoordinates[k] = 0; |
| 53 | + } |
41 | 54 | #ifndef __MAKECINT__ |
42 | | - template<typename _val> |
43 | | - value_type Distance(const TDataPoint<K,_val>& rPoint) const; |
| 55 | + /// constructor initialising the data point from an array |
| 56 | + /// |
| 57 | + /// Input: pData - array with kDimension coordinates |
| 58 | + /// fWeight - weight (default = 1) |
| 59 | + template <typename _coord_typ> |
| 60 | + TDataPoint(const _coord_typ *pData, _val_type fWeight = 1) |
| 61 | + { |
| 62 | + // at least one dimension |
| 63 | + assert(kDimension > 0); |
| 64 | + // fill coordinates |
| 65 | + for (unsigned int i = 0; i < kDimension; ++i) |
| 66 | + m_vCoordinates[i] = pData[i]; |
| 67 | + } |
| 68 | + /// euclidean distance |
| 69 | + /// |
| 70 | + /// returns the euclidean distance to the given data point |
| 71 | + /// |
| 72 | + /// Input: rPoint - data point of same dimensionality |
| 73 | + template <typename _val> |
| 74 | + value_type Distance(const TDataPoint<K, _val> &rPoint) const |
| 75 | + { |
| 76 | + _val_type fDist2 = 0; |
| 77 | + for (unsigned int i = 0; i < kDimension; ++i) |
| 78 | + fDist2 += pow(GetCoordinate(i) - rPoint.GetCoordinate(i), 2); |
| 79 | + |
| 80 | + return sqrt(fDist2); |
| 81 | + } |
44 | 82 | #endif |
45 | | - value_type GetCoordinate(unsigned int iAxis) const; |
46 | | - value_type GetWeight() const {return m_fWeight;} |
47 | | - Bool_t Less(TDataPoint& rPoint,unsigned int iAxis) const; |
48 | | - void SetCoordinate(unsigned int iAxis,_val_type fValue); |
49 | | - void SetWeight(float fWeight) {m_fWeight = fWeight;} |
| 83 | + /// returns the coordinate at the given axis |
| 84 | + /// |
| 85 | + /// Input: iAxis - axis in the range of [0...kDimension-1] |
| 86 | + value_type GetCoordinate(unsigned int iAxis) const |
| 87 | + { |
| 88 | + assert(iAxis < kDimension); |
| 89 | + return m_vCoordinates[iAxis]; |
| 90 | + } |
| 91 | + value_type GetWeight() const { return m_fWeight; } |
| 92 | + /// compares two points at a given axis |
| 93 | + /// |
| 94 | + /// returns: this_point.at(iAxis) < rPoint.at(iAxis) |
| 95 | + /// |
| 96 | + /// Input: rPoint - second point to compare to (of same dimensionality) |
| 97 | + /// iAxis - axis in the range of [0...kDimension-1] |
| 98 | + Bool_t Less(TDataPoint &rPoint, unsigned int iAxis) const |
| 99 | + { |
| 100 | + assert(iAxis < kDimension); |
| 101 | + return (m_vCoordinates[iAxis] < rPoint.GetCoordinate(iAxis)); |
| 102 | + } |
| 103 | + /// sets the coordinate along one axis |
| 104 | + /// |
| 105 | + /// Input: iAxis - axis in the range of [0...kDimension-1] |
| 106 | + /// fValue - new coordinate |
| 107 | + void SetCoordinate(unsigned int iAxis, _val_type fValue) |
| 108 | + { |
| 109 | + assert(iAxis < kDimension); |
| 110 | + m_vCoordinates[iAxis] = fValue; |
| 111 | + } |
| 112 | + void SetWeight(float fWeight) { m_fWeight = fWeight; } |
50 | 113 |
|
51 | 114 | private: |
52 | | - value_type m_vCoordinates[K]; ///< coordinates |
53 | | - value_type m_fWeight; ///< weight at this point |
| 115 | + value_type m_vCoordinates[K]; ///< coordinates |
| 116 | + value_type m_fWeight = 1; ///< weight at this point |
54 | 117 | }; |
55 | 118 |
|
56 | 119 | // some typedef definitions |
57 | | -typedef TDataPoint<1,Float_t> TDataPoint1F; |
58 | | -typedef TDataPoint<2,Float_t> TDataPoint2F; |
59 | | -typedef TDataPoint<3,Float_t> TDataPoint3F; |
60 | | -typedef TDataPoint<1,Double_t> TDataPoint1D; |
61 | | -typedef TDataPoint<2,Double_t> TDataPoint2D; |
62 | | -typedef TDataPoint<3,Double_t> TDataPoint3D; |
63 | | - |
64 | | -}//namespace Math |
65 | | -}//namespace ROOT |
66 | | - |
67 | | -#include "Math/TDataPoint.icc" |
| 120 | +typedef TDataPoint<1, Float_t> TDataPoint1F; |
| 121 | +typedef TDataPoint<2, Float_t> TDataPoint2F; |
| 122 | +typedef TDataPoint<3, Float_t> TDataPoint3F; |
| 123 | +typedef TDataPoint<1, Double_t> TDataPoint1D; |
| 124 | +typedef TDataPoint<2, Double_t> TDataPoint2D; |
| 125 | +typedef TDataPoint<3, Double_t> TDataPoint3D; |
68 | 126 |
|
| 127 | +} // namespace Math |
| 128 | +} // namespace ROOT |
69 | 129 |
|
70 | 130 | #endif // ROOT_Math_TDataPoint |
0 commit comments