Skip to content

Commit 7da2026

Browse files
committed
[Math] Merge TDataPoint*.h and TDataPoint*.icc files
There is no need to split up this short header file in two pieces. Inlining the function definitions makes the code easier to change and need less boilerplate code related to templates.
1 parent 8ad447e commit 7da2026

File tree

3 files changed

+155
-172
lines changed

3 files changed

+155
-172
lines changed

math/mathcore/inc/Math/TDataPoint.h

Lines changed: 96 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,58 +13,118 @@
1313
#ifndef ROOT_Math_TDataPoint
1414
#define ROOT_Math_TDataPoint
1515

16-
//ROOT include(s)
16+
// ROOT include(s)
1717
#include "RtypesCore.h"
1818

19+
#include <cassert>
20+
#include <math.h>
1921

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.
2432

33+
namespace ROOT {
34+
namespace Math {
2535

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 {
2938
public:
3039
typedef _val_type value_type;
3140
enum {
32-
kDimension = K //the dimensionality of this data point
41+
kDimension = K // the dimensionality of this data point
3342
};
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+
}
4154
#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+
}
4482
#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; }
50113

51114
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
54117
};
55118

56119
// 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;
68126

127+
} // namespace Math
128+
} // namespace ROOT
69129

70130
#endif // ROOT_Math_TDataPoint

math/mathcore/inc/Math/TDataPointN.h

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,90 @@
1010
// Header file for TDataPointN class
1111
//
1212

13-
1413
#ifndef ROOT_Math_TDataPointN
1514
#define ROOT_Math_TDataPointN
1615

17-
//STL include(s)
16+
// STL include(s)
1817
#include <cassert>
1918

20-
//ROOT include(s)
19+
// ROOT include(s)
2120
#include "RtypesCore.h"
2221

23-
namespace ROOT
24-
{
25-
namespace Math
26-
{
27-
22+
namespace ROOT {
23+
namespace Math {
2824

29-
template<typename _val_type = float>
30-
class TDataPointN
31-
{
25+
template <typename _val_type = float>
26+
class TDataPointN {
3227
private:
3328
static UInt_t kDimension;
3429

3530
public:
3631
typedef _val_type value_type;
3732

38-
static UInt_t Dimension() {return kDimension;}
39-
static void SetDimension(UInt_t dim) {assert(dim>0);kDimension=dim;}
40-
41-
TDataPointN();
33+
static UInt_t Dimension() { return kDimension; }
34+
static void SetDimension(UInt_t dim)
35+
{
36+
assert(dim > 0);
37+
kDimension = dim;
38+
}
39+
40+
TDataPointN()
41+
{
42+
m_vCoordinates = new _val_type[kDimension];
43+
for (UInt_t k = 0; k < kDimension; ++k)
44+
m_vCoordinates[k] = 0;
45+
}
4246
#ifndef __MAKECINT__
43-
template<typename _coord_typ>
44-
TDataPointN(const _coord_typ* pData,value_type fWeight = 1);
45-
template<typename _val>
46-
TDataPointN(const TDataPointN<_val>&);
47+
template <typename _coord_typ>
48+
TDataPointN(const _coord_typ *pData, value_type fWeight = 1)
49+
{
50+
// fill coordinates
51+
m_vCoordinates = new _val_type[kDimension];
52+
for (unsigned int i = 0; i < kDimension; ++i)
53+
m_vCoordinates[i] = pData[i];
54+
}
55+
TDataPointN(TDataPointN const &) = delete;
4756
#endif
48-
virtual ~TDataPointN();
57+
virtual ~TDataPointN() { delete[] m_vCoordinates; }
4958

5059
#ifndef __MAKECINT__
51-
template<typename _val>
52-
_val_type Distance(const TDataPointN<_val>& rPoint) const;
60+
template <typename _val>
61+
_val_type Distance(const TDataPointN<_val> &rPoint) const
62+
{
63+
_val_type fDist2 = 0;
64+
for (unsigned int i = 0; i < kDimension; ++i)
65+
fDist2 += pow(GetCoordinate(i) - rPoint.GetCoordinate(i), 2);
66+
67+
return sqrt(fDist2);
68+
}
5369
#endif
54-
_val_type GetCoordinate(unsigned int iAxis) const;
55-
_val_type GetWeight() const {return m_fWeight;}
56-
bool Less(TDataPointN& rPoint,unsigned int iAxis) const;
57-
void SetCoordinate(unsigned int iAxis,value_type fValue);
58-
void SetWeight(float fWeight) {m_fWeight = fWeight;}
70+
_val_type GetCoordinate(unsigned int iAxis) const
71+
{
72+
assert(iAxis < kDimension);
73+
return m_vCoordinates[iAxis];
74+
}
75+
_val_type GetWeight() const { return m_fWeight; }
76+
bool Less(TDataPointN &rPoint, unsigned int iAxis) const
77+
{
78+
assert(iAxis < kDimension);
79+
return (m_vCoordinates[iAxis] < rPoint.GetCoordinate(iAxis));
80+
}
81+
void SetCoordinate(unsigned int iAxis, value_type fValue)
82+
{
83+
assert(iAxis < kDimension);
84+
m_vCoordinates[iAxis] = fValue;
85+
}
86+
void SetWeight(float fWeight) { m_fWeight = fWeight; }
5987

6088
private:
61-
value_type* m_vCoordinates;
62-
value_type m_fWeight;
89+
value_type *m_vCoordinates = nullptr;
90+
value_type m_fWeight = 1;
6391
};
6492

6593
template <typename _val_type>
6694
UInt_t TDataPointN<_val_type>::kDimension = 0;
6795

68-
}//namespace Math
69-
}//namespace ROOT
70-
71-
72-
#include "TDataPointN.icc"
96+
} // namespace Math
97+
} // namespace ROOT
7398

7499
#endif // ROOT_Math_TDataPointN

math/mathcore/inc/Math/TDataPointN.icc

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)