ROOT  6.06/09
Reference Guide
TKDTree.h
Go to the documentation of this file.
1 #ifndef ROOT_TKDTree
2 #define ROOT_TKDTree
3 
4 #ifndef ROOT_TObject
5 #include "TObject.h"
6 #endif
7 
8 #include "TMath.h"
9 #include <vector>
10 
11 template <typename Index, typename Value> class TKDTree : public TObject
12 {
13 public:
14 
15  TKDTree();
16  TKDTree(Index npoints, Index ndim, UInt_t bsize);
17  TKDTree(Index npoints, Index ndim, UInt_t bsize, Value **data);
18  ~TKDTree();
19 
20  void Build(); // build the tree
21 
22  Double_t Distance(const Value *point, Index ind, Int_t type=2) const;
23  void DistanceToNode(const Value *point, Index inode, Value &min, Value &max, Int_t type=2);
24 
25  // Get indexes of left and right daughter nodes
26  Int_t GetLeft(Int_t inode) const {return inode*2+1;}
27  Int_t GetRight(Int_t inode) const {return (inode+1)*2;}
28  Int_t GetParent(Int_t inode) const {return (inode-1)/2;}
29  //
30  // Other getters
31  Index* GetPointsIndexes(Int_t node) const;
32  void GetNodePointsIndexes(Int_t node, Int_t &first1, Int_t &last1, Int_t &first2, Int_t &last2) const;
33  UChar_t GetNodeAxis(Int_t id) const {return (id < 0 || id >= fNNodes) ? 0 : fAxis[id];}
34  Value GetNodeValue(Int_t id) const {return (id < 0 || id >= fNNodes) ? 0 : fValue[id];}
35  Int_t GetNNodes() const {return fNNodes;}
36  Int_t GetTotalNodes() const {return fTotalNodes;}
39  Value* GetBoundary(const Int_t node);
40  Value* GetBoundaryExact(const Int_t node);
41  Index GetNPoints() { return fNPoints; }
42  Index GetNDim() { return fNDim; }
43  Index GetNPointsNode(Int_t node) const;
44 
45  //Getters for internal variables.
46  Int_t GetRowT0() {return fRowT0;} //! smallest terminal row
47  Int_t GetCrossNode() {return fCrossNode;} //! cross node
48  Int_t GetOffset() {return fOffset;} //! offset in fIndPoints
51 
52  void FindNearestNeighbors(const Value *point, Int_t k, Index *ind, Value *dist);
53  Index FindNode(const Value * point) const;
54  void FindPoint(Value * point, Index &index, Int_t &iter);
55  void FindInRange(Value *point, Value range, std::vector<Index> &res);
56  void FindBNodeA(Value * point, Value * delta, Int_t &inode);
57 
58  Bool_t IsTerminal(Index inode) const {return (inode>=fNNodes);}
59  Int_t IsOwner() { return fDataOwner; }
60  Value KOrdStat(Index ntotal, Value *a, Index k, Index *index) const;
61 
62 
63  void MakeBoundaries(Value *range = 0x0);
64  void MakeBoundariesExact();
65  void SetData(Index npoints, Index ndim, UInt_t bsize, Value **data);
66  Int_t SetData(Index idim, Value *data);
67  void SetOwner(Int_t owner) { fDataOwner = owner; }
68  void Spread(Index ntotal, Value *a, Index *index, Value &min, Value &max) const;
69 
70  private:
71  TKDTree(const TKDTree &); // not implemented
72  TKDTree<Index, Value>& operator=(const TKDTree<Index, Value>&); // not implemented
73  void CookBoundaries(const Int_t node, Bool_t left);
74 
75  void UpdateNearestNeighbors(Index inode, const Value *point, Int_t kNN, Index *ind, Value *dist);
76  void UpdateRange(Index inode, Value *point, Value range, std::vector<Index> &res);
77 
78  protected:
79  Int_t fDataOwner; //! 0 - not owner, 2 - owner of the pointer array, 1 - owner of the whole 2-d array
80  Int_t fNNodes; // size of node array
81  Int_t fTotalNodes; // total number of nodes (fNNodes + terminal nodes)
82  Index fNDim; // number of dimensions
83  Index fNDimm; // dummy 2*fNDim
84  Index fNPoints; // number of multidimensional points
85  Index fBucketSize; // size of the terminal nodes
86  UChar_t *fAxis; //[fNNodes] nodes cutting axis
87  Value *fValue; //[fNNodes] nodes cutting value
88  //
89  Value *fRange; //[fNDimm] range of data for each dimension
90  Value **fData; //! data points
91  Value *fBoundaries;//! nodes boundaries
92 
93 
94  Index *fIndPoints; //! array of points indexes
95  Int_t fRowT0; //! smallest terminal row - first row that contains terminal nodes
96  Int_t fCrossNode; //! cross node - node that begins the last row (with terminal nodes only)
97  Int_t fOffset; //! offset in fIndPoints - if there are 2 rows, that contain terminal nodes
98  // fOffset returns the index in the fIndPoints array of the first point
99  // that belongs to the first node on the second row.
100 
101 
102  ClassDef(TKDTree, 1) // KD tree
103 };
104 
105 
108 
109 // Test functions:
111 
112 #endif
113 
void Spread(Index ntotal, Value *a, Index *index, Value &min, Value &max) const
Calculate spread of the array a.
Definition: TKDTree.cxx:970
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
Int_t fOffset
cross node - node that begins the last row (with terminal nodes only)
Definition: TKDTree.h:97
Int_t GetNNodes() const
Definition: TKDTree.h:35
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
void SetData(Index npoints, Index ndim, UInt_t bsize, Value **data)
Set the data array. See the constructor function comments for details.
Definition: TKDTree.cxx:925
Int_t fDataOwner
Definition: TKDTree.h:79
Int_t GetRowT0()
Definition: TKDTree.h:46
UChar_t GetNodeAxis(Int_t id) const
Definition: TKDTree.h:33
TKDTree< Int_t, Float_t > TKDTreeIF
Definition: TKDTree.h:107
Index GetNPoints()
Definition: TKDTree.h:41
Int_t fTotalNodes
Definition: TKDTree.h:81
void FindBNodeA(Value *point, Value *delta, Int_t &inode)
find the smallest node covering the full range - start
Definition: TKDTree.cxx:1180
Value GetNodeValue(Int_t id) const
Definition: TKDTree.h:34
Index GetNDim()
Definition: TKDTree.h:42
Index fNPoints
Definition: TKDTree.h:84
Int_t GetOffset()
cross node
Definition: TKDTree.h:48
void FindInRange(Value *point, Value range, std::vector< Index > &res)
Find all points in the sphere of a given radius "range" around the given point 1st argument - the poi...
Definition: TKDTree.cxx:756
int Int_t
Definition: RtypesCore.h:41
Index fBucketSize
Definition: TKDTree.h:85
bool Bool_t
Definition: RtypesCore.h:59
void MakeBoundariesExact()
Build boundaries for each node.
Definition: TKDTree.cxx:1121
TArc * a
Definition: textangle.C:12
void SetOwner(Int_t owner)
Definition: TKDTree.h:67
Value * fRange
Definition: TKDTree.h:89
Int_t GetCrossNode()
smallest terminal row
Definition: TKDTree.h:47
void Build()
Build the kd-tree.
Definition: TKDTree.cxx:413
Int_t GetLeft(Int_t inode) const
Definition: TKDTree.h:26
TKDTree()
Definition: TKDTree.cxx:272
Index fNDim
Definition: TKDTree.h:82
#define ClassDef(name, id)
Definition: Rtypes.h:254
Int_t GetRight(Int_t inode) const
Definition: TKDTree.h:27
void FindPoint(Value *point, Index &index, Int_t &iter)
find the index of point works only if we keep fData pointers
Definition: TKDTree.cxx:710
Int_t bsize[]
Definition: SparseFit4.cxx:31
Index GetBucketSize()
Definition: TKDTree.h:50
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
Int_t GetTotalNodes() const
Definition: TKDTree.h:36
Index * fIndPoints
nodes boundaries
Definition: TKDTree.h:94
XFontStruct * id
Definition: TGX11.cxx:108
void GetNodePointsIndexes(Int_t node, Int_t &first1, Int_t &last1, Int_t &first2, Int_t &last2) const
Return the indices of points in that node Indices are returned as the first and last value of the par...
Definition: TKDTree.cxx:848
a kd-tree
Definition: TKDTree.h:11
Value KOrdStat(Index ntotal, Value *a, Index k, Index *index) const
copy of the TMath::KOrdStat because I need an Index work array
Definition: TKDTree.cxx:987
UChar_t * fAxis
Definition: TKDTree.h:86
Bool_t IsTerminal(Index inode) const
Definition: TKDTree.h:58
Int_t IsOwner()
Definition: TKDTree.h:59
void UpdateNearestNeighbors(Index inode, const Value *point, Int_t kNN, Index *ind, Value *dist)
Update the nearest neighbors values by examining the node inode.
Definition: TKDTree.cxx:570
Int_t fRowT0
array of points indexes
Definition: TKDTree.h:95
~TKDTree()
Destructor By default, the original data is not owned by kd-tree and is not deleted with it...
Definition: TKDTree.cxx:375
TKDTree< Index, Value > & operator=(const TKDTree< Index, Value > &)
Index GetNPointsNode(Int_t node) const
Get number of points in this node for all the terminal nodes except last, the size is fBucketSize for...
Definition: TKDTree.cxx:902
TKDTreeIF * TKDTreeTestBuild()
Index * GetIndPoints()
offset in fIndPoints
Definition: TKDTree.h:49
unsigned int UInt_t
Definition: RtypesCore.h:42
void FindNearestNeighbors(const Value *point, Int_t k, Index *ind, Value *dist)
Find kNN nearest neighbors to the point in the first argument Returns 1 on success, 0 on failure Arrays ind and dist are provided by the user and are assumed to be at least kNN elements long.
Definition: TKDTree.cxx:550
void CookBoundaries(const Int_t node, Bool_t left)
define index of this terminal node
Definition: TKDTree.cxx:1076
void DistanceToNode(const Value *point, Index inode, Value &min, Value &max, Int_t type=2)
Find the minimal and maximal distance from a given point to a given node.
Definition: TKDTree.cxx:642
TKDTree< Int_t, Double_t > TKDTreeID
Definition: TKDTree.h:106
Value * fValue
Definition: TKDTree.h:87
void MakeBoundaries(Value *range=0x0)
Build boundaries for each node.
Definition: TKDTree.cxx:1041
RooCmdArg Index(RooCategory &icat)
Value * GetBoundariesExact()
Get the boundaries.
Definition: TKDTree.cxx:1203
double Double_t
Definition: RtypesCore.h:55
Int_t fCrossNode
smallest terminal row - first row that contains terminal nodes
Definition: TKDTree.h:96
int type
Definition: TGX11.cxx:120
Index fNDimm
Definition: TKDTree.h:83
Value ** fData
Definition: TKDTree.h:90
Double_t Distance(const Value *point, Index ind, Int_t type=2) const
Find the distance between point of the first argument and the point at index value ind Type argument ...
Definition: TKDTree.cxx:617
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
Mother of all ROOT objects.
Definition: TObject.h:58
void UpdateRange(Index inode, Value *point, Value range, std::vector< Index > &res)
Internal recursive function with the implementation of range searches.
Definition: TKDTree.cxx:766
Int_t fNNodes
0 - not owner, 2 - owner of the pointer array, 1 - owner of the whole 2-d array
Definition: TKDTree.h:80
Int_t GetParent(Int_t inode) const
Definition: TKDTree.h:28
Value * GetBoundaries()
Get the boundaries.
Definition: TKDTree.cxx:1192
unsigned char UChar_t
Definition: RtypesCore.h:34
Index * GetPointsIndexes(Int_t node) const
return the indices of the points in that terminal node for all the nodes except last, the size is fBucketSize for the last node it's fOffsetfBucketSize
Definition: TKDTree.cxx:819
Value * GetBoundaryExact(const Int_t node)
Get a boundary.
Definition: TKDTree.cxx:1223
Value * fBoundaries
data points
Definition: TKDTree.h:91
Value * GetBoundary(const Int_t node)
Get a boundary.
Definition: TKDTree.cxx:1213
const char * Value
Definition: TXMLSetup.cxx:73
Index FindNode(const Value *point) const
returns the index of the terminal node to which point belongs (index in the fAxis, fValue, etc arrays) returns -1 in case of failure
Definition: TKDTree.cxx:679