Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
KDTree.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Authors: C. Gumpert 09/2011
3/**********************************************************************
4 * *
5 * Copyright (c) 2011 , LCG ROOT MathLib Team *
6 * *
7 * *
8 **********************************************************************/
9//
10// Header file for KDTree class
11//
12
13
14#ifndef ROOT_Math_KDTree
15#define ROOT_Math_KDTree
16
17//STL header
18#include <cassert>
19#include <vector>
20#include <cmath>
21#include <utility>
22
23// ROOT include(s)
24#include "RtypesCore.h"
25
26namespace ROOT
27{
28 namespace Math
29 {
30
31 //______________________________________________________________________________
32 //Begin_Html
33 //End_Html
34 template<class _DataPoint>
35 class KDTree
36 {
37 public:
38
40 typedef typename _DataPoint::value_type value_type;
41 static UInt_t Dimension() {return _DataPoint::Dimension();}
43 kEffective = 0, //split according to effective entries
44 kBinContent //split according to bin content
45 };
46
47 private:
48
50 {
51 public:
53
54 UInt_t GetAxis() const {return fAxis;}
56
57 private:
58 UInt_t fAxis; //axis at which the points are compared
59 };
60
61 class Cut
62 {
63 public:
64 Cut():fAxis(0),fCutValue(0) {}
66 ~Cut() {}
67
68 UInt_t GetAxis() const {return fAxis;}
72
73 Bool_t operator<(const point_type& rPoint) const;
74 Bool_t operator>(const point_type& rPoint) const;
75
76 private:
77 UInt_t fAxis; //axis at which the splitting is done
78 Double_t fCutValue; //split value
79 };
80
81 //forward declarations
82 class BaseNode;
83 class HeadNode;
84 class SplitNode;
85 class BinNode;
86 class TerminalNode;
87
89 {
90 public:
91 //constructor and destructor
93 virtual ~BaseNode();
94
95 //providing usual functionality of a tree
96 virtual BaseNode* Clone() = 0;
97 virtual const BinNode* FindNode(const point_type& rPoint) const = 0;
98 virtual void GetClosestPoints(const point_type& rRef,UInt_t nPoints,std::vector<std::pair<const _DataPoint*,Double_t> >& vFoundPoints) const = 0;
99 virtual void GetPointsWithinDist(const point_type& rRef,value_type fDist,std::vector<const point_type*>& vFoundPoints) const = 0;
100 virtual Bool_t Insert(const point_type& rPoint) = 0;
101 virtual void Print(int iRow = 0) const = 0;
102
103 //navigating the tree
105 const BaseNode* LeftChild() const {return fLeftChild;}
106 BaseNode*& Parent() {return fParent;}
107 const BaseNode* Parent() const {return fParent;}
109 const BaseNode* RightChild() const {return fRightChild;}
110
111 //information about relative position of current node
113 virtual Bool_t IsHeadNode() const {return false;}
114 Bool_t IsLeftChild() const;
115
116 private:
117 // node should never be copied or assigned
118 BaseNode(const BaseNode& ) {}
119 BaseNode& operator=(const BaseNode& ) {return *this;}
120
121 //links to adjacent nodes
122 BaseNode* fParent; ///<!pointer to parent node
123 BaseNode* fLeftChild; ///<!pointer to left child
124 BaseNode* fRightChild; ///<!pointer to right child
125 };
126
127 class HeadNode : public BaseNode
128 {
129 public:
130 //constructor and destructor
132 virtual ~HeadNode() {delete Parent();}
133
134 //delegate everything to the actual root node of the tree
135 const BinNode* FindNode(const point_type& rPoint) const override {return Parent()->FindNode(rPoint);}
136 void GetClosestPoints(const point_type& rRef,UInt_t nPoints,std::vector<std::pair<const _DataPoint*,Double_t> >& vFoundPoints) const override;
137 void GetPointsWithinDist(const point_type& rRef,value_type fDist,std::vector<const _DataPoint*>& vFoundPoints) const override;
138 Bool_t Insert(const point_type& rPoint) override {return Parent()->Insert(rPoint);}
139 void Print(Int_t) const override {Parent()->Print();}
140
141 private:
142 // node should never be copied
143 HeadNode(const HeadNode& ) {}
144 HeadNode& operator=(const HeadNode& ) {return *this;}
145
146 HeadNode* Clone() override;
147 bool IsHeadNode() const override {return true;}
148
149 // only delegate everything else is private and should not be used
150 using BaseNode::Parent;
153
156 };
157
158 class SplitNode : public BaseNode
159 {
160 public:
161 // constructors and destructors
163 virtual ~SplitNode();
164
165 //accessing information about this split node
166 const Cut* GetCut() const {return fCut;}
167 void Print(Int_t iRow = 0) const override;
168
169 private:
170 // node should never be copied
172 SplitNode& operator=(const SplitNode& ) {return *this;}
173
174 SplitNode* Clone() override;
175 const BinNode* FindNode(const point_type& rPoint) const override;
176 void GetClosestPoints(const point_type& rRef,UInt_t nPoints,std::vector<std::pair<const _DataPoint*,Double_t> >& vFoundPoints) const override;
177 void GetPointsWithinDist(const point_type& rRef,value_type fDist,std::vector<const _DataPoint*>& vFoundPoints) const override;
178 Bool_t Insert(const point_type& rPoint) override;
179
180 const Cut* fCut; //pointer to cut object owned by this node
181 };
182
183 class BinNode : public BaseNode
184 {
185 protected:
186 //save some typing
187 typedef std::pair<value_type,value_type> tBoundary;
188 public:
189 // constructors and destructors
191 BinNode(const BinNode& copy);
192 virtual ~BinNode() {}
193
194 // usual bin operations
195 virtual void EmptyBin();
196 const BinNode* FindNode(const point_type& rPoint) const override;
197 point_type GetBinCenter() const;
198 Double_t GetBinContent() const {return GetSumw();}
199 virtual const std::vector<tBoundary>& GetBoundaries() const {return fBoundaries;}
201 Double_t GetEffectiveEntries() const {return (GetSumw2()) ? std::pow(GetSumw(),2)/GetSumw2() : 0;}
202 UInt_t GetEntries() const {return fEntries;}
203 Double_t GetVolume() const;
204 Double_t GetSumw() const {return fSumw;}
205 Double_t GetSumw2() const {return fSumw2;}
206 Bool_t Insert(const point_type& rPoint) override;
207 Bool_t IsInBin(const point_type& rPoint) const;
208 void Print(int iRow = 0) const override;
209
210 protected:
211 BinNode* Clone() override;
212
213 // intrinsic bin properties
214 std::vector<tBoundary> fBoundaries; ///< bin boundaries
215 Double_t fSumw; ///< sum of weights
216 Double_t fSumw2; ///< sum of weights^2
217 UInt_t fEntries; ///< number of entries
218
219 private:
220 BinNode& operator=(const BinNode& rhs);
221
222 // bin does not contain any point like information
223 void GetClosestPoints(const point_type&,UInt_t,std::vector<std::pair<const _DataPoint*,Double_t> >&) const override {}
224 void GetPointsWithinDist(const point_type&,value_type,std::vector<const point_type*>&) const override {}
225
226 // a bin does not have children
229 };
230
231 class TerminalNode : public BinNode
232 {
233 friend class KDTree<_DataPoint>;
234 //save some typing
235 typedef std::pair<value_type,value_type> tBoundary;
236
237 public:
238 //constructor and destructor
240 virtual ~TerminalNode();
241
242 void EmptyBin() override;
243 const std::vector<tBoundary>& GetBoundaries() const override;
244 void GetClosestPoints(const point_type& rRef,UInt_t nPoints,std::vector<std::pair<const _DataPoint*,Double_t> >& vFoundPoints) const override;
245 const std::vector<const point_type*>& GetPoints() const {return fDataPoints;}
246 void GetPointsWithinDist(const point_type& rRef,value_type fDist,std::vector<const _DataPoint*>& vFoundPoints) const override;
247 void Print(int iRow = 0) const override;
248
249 private:
250 // node should never be copied
252 TerminalNode& operator=(const TerminalNode& ) {return *this;}
253
254 // save some typing
255 typedef typename std::vector<const point_type* >::iterator data_it;
256 typedef typename std::vector<const point_type* >::const_iterator const_data_it;
257
258 // creating new Terminal Node when splitting, copying elements in the given range
260
261 //tree operations
262 BinNode* Clone() override {return ConvertToBinNode();}
264 const BinNode* FindNode(const point_type&) const override {return this;}
265 Bool_t Insert(const point_type& rPoint) override;
266 void Split();
271 void UpdateBoundaries();
272
273 Bool_t fOwnData; ///< terminal node owns the data objects (default = false)
274 eSplitOption fSplitOption; ///< according to which figure of merit the node is split
275 Double_t fBucketSize; ///< target number of entries per bucket
276 UInt_t fSplitAxis; ///< axis at which the next split will occur
277 std::vector<const _DataPoint*> fDataPoints; ///< data points in this bucket
278 };
279
280 public:
281 //////////////////////////////////////////////////////////////////////
282 //
283 // template<class _DataPoint> class KDTree<_DataPoint>::iterator
284 //
285 //////////////////////////////////////////////////////////////////////
286 typedef BinNode Bin;
288 {
289 friend class KDTree<_DataPoint>;
290 public:
291 iterator(): fBin(0) {}
292 iterator(const iterator& copy): fBin(copy.fBin) {}
294
296 const iterator& operator++() const;
297 iterator operator++(int);
298 const iterator operator++(int) const;
300 const iterator& operator--() const;
301 iterator operator--(int);
302 const iterator operator--(int) const;
303 bool operator==(const iterator& rIterator) const {return (fBin == rIterator.fBin);}
304 bool operator!=(const iterator& rIterator) const {return !(*this == rIterator);}
306 Bin& operator*() {return *fBin;}
307 const Bin& operator*() const {return *fBin;}
308 Bin* operator->() {return fBin;}
309 const Bin* operator->() const {return fBin;}
310
311 TerminalNode* TN() {assert(dynamic_cast<TerminalNode*>(fBin)); return (TerminalNode*)fBin;}
312
313 private:
315
316 Bin* Next() const;
317 Bin* Previous() const;
318
319 mutable Bin* fBin;
320 };
321
322 //constructor and destructor
324 ~KDTree();
325
326 //public member functions
327 void EmptyBins();
328 iterator End();
329 const iterator End() const;
330 const Bin* FindBin(const point_type& rPoint) const {return fHead->FindNode(rPoint);}
331 iterator First();
332 const iterator First() const;
333 void Freeze();
335 void GetClosestPoints(const point_type& rRef,UInt_t nPoints,std::vector<std::pair<const _DataPoint*,Double_t> >& vFoundPoints) const;
338 UInt_t GetNBins() const;
339 UInt_t GetEntries() const;
340 void GetPointsWithinDist(const point_type& rRef,value_type fDist,std::vector<const point_type*>& vFoundPoints) const;
341 Double_t GetTotalSumw() const;
342 Double_t GetTotalSumw2() const;
344 Bool_t IsFrozen() const {return fIsFrozen;}
345 iterator Last();
346 const iterator Last() const;
347 void Print() {fHead->Parent()->Print();}
348 void Reset();
349 void SetOwner(Bool_t bIsOwner = true);
350 void SetSplitOption(eSplitOption opt);
351
352 private:
353 KDTree();
356
360 };
361
362
363 }//namespace Math
364}//namespace ROOT
365
366#include "Math/KDTree.icc"
367
368#endif // ROOT_Math_KDTree
Basic types used by ROOT and required by TInterpreter.
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:78
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
BaseNode *& GetParentPointer()
Definition KDTree.icc:520
BaseNode(const BaseNode &)
Definition KDTree.h:118
BaseNode * fParent
!pointer to parent node
Definition KDTree.h:122
const BaseNode * LeftChild() const
Definition KDTree.h:105
virtual Bool_t Insert(const point_type &rPoint)=0
const BaseNode * Parent() const
Definition KDTree.h:107
virtual const BinNode * FindNode(const point_type &rPoint) const =0
const BaseNode * RightChild() const
Definition KDTree.h:109
virtual void GetPointsWithinDist(const point_type &rRef, value_type fDist, std::vector< const point_type * > &vFoundPoints) const =0
virtual void GetClosestPoints(const point_type &rRef, UInt_t nPoints, std::vector< std::pair< const _DataPoint *, Double_t > > &vFoundPoints) const =0
Bool_t IsLeftChild() const
Definition KDTree.icc:542
BaseNode * fRightChild
!pointer to right child
Definition KDTree.h:124
virtual Bool_t IsHeadNode() const
Definition KDTree.h:113
BaseNode & operator=(const BaseNode &)
Definition KDTree.h:119
BaseNode(BaseNode *pParent=0)
Definition KDTree.icc:496
BaseNode *& LeftChild()
Definition KDTree.h:104
virtual void Print(int iRow=0) const =0
virtual BaseNode * Clone()=0
BaseNode *& RightChild()
Definition KDTree.h:108
BaseNode * fLeftChild
!pointer to left child
Definition KDTree.h:123
virtual const std::vector< tBoundary > & GetBoundaries() const
Definition KDTree.h:199
virtual void EmptyBin()
Definition KDTree.icc:788
std::vector< tBoundary > fBoundaries
bin boundaries
Definition KDTree.h:214
BinNode * Clone() override
Definition KDTree.icc:779
BinNode(BaseNode *pParent=0)
Definition KDTree.icc:748
point_type GetBinCenter() const
Definition KDTree.icc:826
Double_t GetSumw2() const
Definition KDTree.h:205
Double_t GetSumw() const
Definition KDTree.h:204
void GetClosestPoints(const point_type &, UInt_t, std::vector< std::pair< const _DataPoint *, Double_t > > &) const override
Definition KDTree.h:223
std::pair< value_type, value_type > tBoundary
Definition KDTree.h:187
Bool_t IsInBin(const point_type &rPoint) const
Definition KDTree.icc:878
BinNode & operator=(const BinNode &rhs)
Definition KDTree.icc:798
Double_t fSumw2
sum of weights^2
Definition KDTree.h:216
const BinNode * FindNode(const point_type &rPoint) const override
Definition KDTree.icc:814
UInt_t GetEntries() const
Definition KDTree.h:202
UInt_t fEntries
number of entries
Definition KDTree.h:217
Double_t GetDensity() const
Definition KDTree.h:200
Bool_t Insert(const point_type &rPoint) override
Definition KDTree.icc:860
Double_t GetEffectiveEntries() const
Definition KDTree.h:201
Double_t fSumw
sum of weights
Definition KDTree.h:215
Double_t GetBinContent() const
Definition KDTree.h:198
void GetPointsWithinDist(const point_type &, value_type, std::vector< const point_type * > &) const override
Definition KDTree.h:224
Double_t GetVolume() const
Definition KDTree.icc:843
Bool_t operator()(const point_type *pFirst, const point_type *pSecond) const
Definition KDTree.icc:433
void SetAxis(UInt_t iAxis)
Definition KDTree.h:55
value_type GetCutValue() const
Definition KDTree.h:69
Cut(UInt_t iAxis, Double_t fNewCutValue)
Definition KDTree.h:65
UInt_t GetAxis() const
Definition KDTree.h:68
Bool_t operator>(const point_type &rPoint) const
Definition KDTree.icc:476
void SetAxis(UInt_t iAxis)
Definition KDTree.h:70
void SetCutValue(Double_t fNewCutValue)
Definition KDTree.h:71
Bool_t operator<(const point_type &rPoint) const
Definition KDTree.icc:456
void GetPointsWithinDist(const point_type &rRef, value_type fDist, std::vector< const _DataPoint * > &vFoundPoints) const override
Definition KDTree.icc:580
void GetClosestPoints(const point_type &rRef, UInt_t nPoints, std::vector< std::pair< const _DataPoint *, Double_t > > &vFoundPoints) const override
Definition KDTree.icc:572
HeadNode & operator=(const HeadNode &)
Definition KDTree.h:144
bool IsHeadNode() const override
Definition KDTree.h:147
const BinNode * FindNode(const point_type &rPoint) const override
Definition KDTree.h:135
HeadNode(BaseNode &rNode)
Definition KDTree.h:131
void Print(Int_t) const override
Definition KDTree.h:139
HeadNode(const HeadNode &)
Definition KDTree.h:143
Bool_t Insert(const point_type &rPoint) override
Definition KDTree.h:138
HeadNode * Clone() override
Definition KDTree.icc:556
Bool_t Insert(const point_type &rPoint) override
Definition KDTree.icc:721
SplitNode(UInt_t iAxis, Double_t fCutValue, BaseNode &rLeft, BaseNode &rRight, BaseNode *pParent=0)
Definition KDTree.icc:588
SplitNode * Clone() override
Definition KDTree.icc:619
const BinNode * FindNode(const point_type &rPoint) const override
Definition KDTree.icc:638
const Cut * GetCut() const
Definition KDTree.h:166
SplitNode & operator=(const SplitNode &)
Definition KDTree.h:172
void GetClosestPoints(const point_type &rRef, UInt_t nPoints, std::vector< std::pair< const _DataPoint *, Double_t > > &vFoundPoints) const override
Definition KDTree.icc:652
void GetPointsWithinDist(const point_type &rRef, value_type fDist, std::vector< const _DataPoint * > &vFoundPoints) const override
Definition KDTree.icc:689
SplitNode(const SplitNode &)
Definition KDTree.h:171
Bool_t Insert(const point_type &rPoint) override
Definition KDTree.icc:1103
const BinNode * FindNode(const point_type &) const override
Definition KDTree.h:264
std::vector< constpoint_type * >::iterator data_it
Definition KDTree.h:255
Double_t fBucketSize
target number of entries per bucket
Definition KDTree.h:275
void SetOwner(Bool_t bIsOwner=true)
Definition KDTree.h:267
void SetSplitOption(eSplitOption opt)
Definition KDTree.h:268
Bool_t fOwnData
terminal node owns the data objects (default = false)
Definition KDTree.h:273
std::pair< value_type, value_type > tBoundary
Definition KDTree.h:235
BinNode * Clone() override
Definition KDTree.h:262
TerminalNode(Double_t iBucketSize, BaseNode *pParent=0)
Definition KDTree.icc:915
std::vector< constpoint_type * >::const_iterator const_data_it
Definition KDTree.h:256
const std::vector< tBoundary > & GetBoundaries() const override
Definition KDTree.icc:1015
UInt_t fSplitAxis
axis at which the next split will occur
Definition KDTree.h:276
TerminalNode & operator=(const TerminalNode &)
Definition KDTree.h:252
void GetPointsWithinDist(const point_type &rRef, value_type fDist, std::vector< const _DataPoint * > &vFoundPoints) const override
Definition KDTree.icc:1082
TerminalNode(const TerminalNode &)
Definition KDTree.h:251
void GetClosestPoints(const point_type &rRef, UInt_t nPoints, std::vector< std::pair< const _DataPoint *, Double_t > > &vFoundPoints) const override
Definition KDTree.icc:1037
std::vector< const _DataPoint * > fDataPoints
data points in this bucket
Definition KDTree.h:277
const std::vector< const point_type * > & GetPoints() const
Definition KDTree.h:245
eSplitOption fSplitOption
according to which figure of merit the node is split
Definition KDTree.h:274
bool operator==(const iterator &rIterator) const
Definition KDTree.h:303
TerminalNode * TN()
Definition KDTree.h:311
const Bin & operator*() const
Definition KDTree.h:307
bool operator!=(const iterator &rIterator) const
Definition KDTree.h:304
iterator(BinNode *pNode)
Definition KDTree.h:314
iterator(const iterator &copy)
Definition KDTree.h:292
iterator & operator=(const iterator &rhs)
Definition KDTree.icc:1476
const Bin * operator->() const
Definition KDTree.h:309
Bool_t IsFrozen() const
Definition KDTree.h:344
Double_t GetTotalSumw2() const
Definition KDTree.icc:321
Bool_t Insert(const point_type &rData)
Definition KDTree.h:343
void SetOwner(Bool_t bIsOwner=true)
Definition KDTree.icc:391
KDTree< _DataPoint > * GetFrozenCopy()
Definition KDTree.icc:256
Double_t GetTotalSumw() const
Definition KDTree.icc:308
_DataPoint point_type
Definition KDTree.h:39
_DataPoint::value_type value_type
Definition KDTree.h:40
const Bin * FindBin(const point_type &rPoint) const
Definition KDTree.h:330
UInt_t GetEntries() const
Definition KDTree.icc:243
void GetPointsWithinDist(const point_type &rRef, value_type fDist, std::vector< const point_type * > &vFoundPoints) const
Definition KDTree.icc:286
iterator First()
Definition KDTree.icc:131
UInt_t GetNBins() const
Definition KDTree.icc:273
iterator Last()
Definition KDTree.icc:334
Double_t fBucketSize
Definition KDTree.h:358
Double_t GetBucketSize() const
Definition KDTree.h:334
Double_t GetEffectiveEntries() const
Definition KDTree.icc:224
KDTree< point_type > & operator=(const KDTree< point_type > &)
Definition KDTree.h:355
BaseNode * fHead
Definition KDTree.h:357
void SetSplitOption(eSplitOption opt)
Definition KDTree.icc:410
void GetClosestPoints(const point_type &rRef, UInt_t nPoints, std::vector< std::pair< const _DataPoint *, Double_t > > &vFoundPoints) const
Definition KDTree.icc:200
KDTree(const KDTree< point_type > &)
Definition KDTree.h:354
iterator End()
Definition KDTree.icc:103
static UInt_t Dimension()
Definition KDTree.h:41
Namespace for new Math classes and functions.