Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-23 10:02:35

0001 // @(#)root/mathcore:$Id$
0002 // Authors: C. Gumpert    09/2011
0003 /**********************************************************************
0004  *                                                                    *
0005  * Copyright (c) 2011 , LCG ROOT MathLib Team                         *
0006  *                                                                    *
0007  *                                                                    *
0008  **********************************************************************/
0009 //
0010 // Header file for TDataPointclass
0011 //
0012 
0013 #ifndef ROOT_Math_TDataPoint
0014 #define ROOT_Math_TDataPoint
0015 
0016 // ROOT include(s)
0017 #include "RtypesCore.h"
0018 
0019 #include <cassert>
0020 #include <cmath>
0021 
0022 namespace ROOT {
0023 namespace Math {
0024 
0025 /// \brief class representing a data point
0026 ///
0027 /// This class can be used for describing data points in a high-dimensional space.
0028 /// The (positive) dimension is specified by the first template parameter. The second
0029 /// template parameter can be used to tweak the precision of the stored coordinates. By
0030 /// default all coordinates are stored with 4 byte float precision. In addition to the
0031 /// coordinates a weight can be assigned to each data point allowing the representation
0032 /// of fields in high dimensions.
0033 /// Basic functionality for accessing/modifying the coordinates/weight are provided
0034 /// as well as a comparison method and the basic euclidean metric.
0035 template <unsigned int K, typename _val_type = float>
0036 class TDataPoint {
0037 public:
0038    typedef _val_type value_type;
0039    enum {
0040       kDimension = K // the dimensionality of this data point
0041    };
0042    static UInt_t Dimension() { return kDimension; }
0043    /// standard constructor
0044    /// sets the weight to 1 and initialises all coordinates with 0
0045    TDataPoint()
0046    {
0047       // at least one dimension
0048       assert(kDimension > 0);
0049 
0050       for (UInt_t k = 0; k < K; ++k)
0051          m_vCoordinates[k] = 0;
0052    }
0053 #ifndef __MAKECINT__
0054    /// constructor initialising the data point from an array
0055    ///
0056    /// Input: pData   - array with kDimension coordinates
0057    ///        fWeight - weight (default = 1)
0058    template <typename _coord_typ>
0059    TDataPoint(const _coord_typ *pData, _val_type fWeight = 1)
0060    {
0061       // at least one dimension
0062       assert(kDimension > 0);
0063       // fill coordinates
0064       for (unsigned int i = 0; i < kDimension; ++i)
0065          m_vCoordinates[i] = pData[i];
0066       m_fWeight = fWeight;
0067    }
0068    /// euclidean distance
0069    ///
0070    /// returns the euclidean distance to the given data point
0071    ///
0072    /// Input: rPoint - data point of same dimensionality
0073    template <typename _val>
0074    value_type Distance(const TDataPoint<K, _val> &rPoint) const
0075    {
0076       _val_type fDist2 = 0;
0077       for (unsigned int i = 0; i < kDimension; ++i)
0078          fDist2 += pow(GetCoordinate(i) - rPoint.GetCoordinate(i), 2);
0079 
0080       return sqrt(fDist2);
0081    }
0082 #endif
0083    /// returns the coordinate at the given axis
0084    ///
0085    /// Input: iAxis - axis in the range of [0...kDimension-1]
0086    value_type GetCoordinate(unsigned int iAxis) const
0087    {
0088       assert(iAxis < kDimension);
0089       return m_vCoordinates[iAxis];
0090    }
0091    value_type GetWeight() const { return m_fWeight; }
0092    /// compares two points at a given axis
0093    ///
0094    ///  returns: this_point.at(iAxis) < rPoint.at(iAxis)
0095    ///
0096    /// Input: rPoint - second point to compare to (of same dimensionality)
0097    ///        iAxis  - axis in the range of [0...kDimension-1]
0098    Bool_t Less(TDataPoint &rPoint, unsigned int iAxis) const
0099    {
0100       assert(iAxis < kDimension);
0101       return (m_vCoordinates[iAxis] < rPoint.GetCoordinate(iAxis));
0102    }
0103    /// sets the coordinate along one axis
0104    ///
0105    /// Input: iAxis  - axis in the range of [0...kDimension-1]
0106    ///        fValue - new coordinate
0107    void SetCoordinate(unsigned int iAxis, _val_type fValue)
0108    {
0109       assert(iAxis < kDimension);
0110       m_vCoordinates[iAxis] = fValue;
0111    }
0112    void SetWeight(float fWeight) { m_fWeight = fWeight; }
0113 
0114 private:
0115    value_type m_vCoordinates[K]; ///< coordinates
0116    value_type m_fWeight = 1;     ///< weight at this point
0117 };
0118 
0119 // some typedef definitions
0120 typedef TDataPoint<1, Float_t> TDataPoint1F;
0121 typedef TDataPoint<2, Float_t> TDataPoint2F;
0122 typedef TDataPoint<3, Float_t> TDataPoint3F;
0123 typedef TDataPoint<1, Double_t> TDataPoint1D;
0124 typedef TDataPoint<2, Double_t> TDataPoint2D;
0125 typedef TDataPoint<3, Double_t> TDataPoint3D;
0126 
0127 } // namespace Math
0128 } // namespace ROOT
0129 
0130 #endif // ROOT_Math_TDataPoint