ATLAS Offline Software
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
TrigVSI::KDTree< T, D > Class Template Reference

KDTree. More...

#include <KDPoint.h>

Collaboration diagram for TrigVSI::KDTree< T, D >:

Classes

class  Node
 Node class for KDTree. More...
 

Public Member Functions

 KDTree (std::vector< KDPoint< T, D >> &v_data)
 
 KDTree ()
 
void genTree ()
 Command to generate tree. More...
 
void lock ()
 
void unlock ()
 
KDPoint< T, D > at (size_t n)
 

Private Member Functions

std::unique_ptr< NodebuildTree (int, int, int)
 recursive function to create tree structure. More...
 
void nearestNeighborRec (const KDPoint< T, D > &, const Node *, double &, int &, std::function< double(const KDPoint< T, D > &, const KDPoint< T, D > &)> &)
 recursive function for nearest neighbor searching. More...
 

Private Attributes

std::unique_ptr< Nodem_rootNode
 The root node of the tree. More...
 
std::vector< KDPoint< T, D > > m_datas
 Container of the points. More...
 
std::vector< size_t > m_indices
 A list of indices of points in m_datas. More...
 
size_t m_idLength
 
bool m_locked
 

Detailed Description

template<typename T, size_t D>
class TrigVSI::KDTree< T, D >

KDTree.

Can be used for fast point searching, which is needed in clustering.

Definition at line 159 of file KDPoint.h.

Constructor & Destructor Documentation

◆ KDTree() [1/2]

template<typename T , size_t D>
TrigVSI::KDTree< T, D >::KDTree ( std::vector< KDPoint< T, D >> &  v_data)
inline

Definition at line 176 of file KDPoint.h.

176  : m_datas( v_data ), m_idLength( m_datas.size() ), m_locked( false )
177  {
178  m_indices.clear();
179  for (size_t i = 0; i < m_idLength; i++) { m_indices.emplace_back(i); }
180  };

◆ KDTree() [2/2]

template<typename T , size_t D>
TrigVSI::KDTree< T, D >::KDTree ( )
inline

Definition at line 182 of file KDPoint.h.

182 : m_idLength(0), m_locked(false){};

Member Function Documentation

◆ at()

template<typename T , size_t D>
KDPoint<T,D> TrigVSI::KDTree< T, D >::at ( size_t  n)
inline

Definition at line 188 of file KDPoint.h.

188 { return m_datas.at(n); };

◆ buildTree()

template<typename T , size_t D>
std::unique_ptr< typename KDTree< T, D >::Node > TrigVSI::KDTree< T, D >::buildTree ( int  l,
int  r,
int  depth 
)
private

recursive function to create tree structure.

Returns
unique ptr to current node
Parameters
[in]lIndex of the left edge of current region.
[in]rIndex of the right edge of current region.
[in]depthDepth of current node. Axe to be separated is selected according to this param.

Definition at line 222 of file KDPoint.h.

223 {
224  if ( l >= r ) return std::make_unique<Node>(nullptr);
225 
226  const int axis_ = depth % D;
227  const int mid = ( l + r ) >> 1;
228 
229  std::nth_element( m_indices.begin()+l, m_indices.begin()+l+mid, m_indices.begin()+r,
230  [this, axis_](size_t lcnt, size_t rcnt) {
231  return m_datas[lcnt].getId(axis_) < m_datas[rcnt].getId(axis_);
232  } );
233 
234  std::unique_ptr<Node> node_ptr = std::make_unique<Node> ( m_datas.at(m_indices[mid]), m_indices[mid] );
235  node_ptr->axis = axis_;
236 
237  node_ptr->leftPtr = buildTree( l, mid, depth+1 );
238  node_ptr->rightPtr = buildTree( mid+1, r, depth+1 );
239 
240  return node_ptr;
241 }

◆ genTree()

template<typename T , size_t D>
void TrigVSI::KDTree< T, D >::genTree

Command to generate tree.

Needed to be locked before execute this command.

Definition at line 207 of file KDPoint.h.

208 {
209  if (m_locked) return;
211 }

◆ lock()

template<typename T , size_t D>
void TrigVSI::KDTree< T, D >::lock ( )
inline

Definition at line 185 of file KDPoint.h.

185 { m_locked = true; };

◆ nearestNeighborRec()

template<typename T , size_t D>
void TrigVSI::KDTree< T, D >::nearestNeighborRec ( const KDPoint< T, D > &  ,
const Node ,
double &  ,
int &  ,
std::function< double(const KDPoint< T, D > &, const KDPoint< T, D > &)> &   
)
private

recursive function for nearest neighbor searching.

Parameters
[in]queryquery point for searching.
[in]nodecurrent node to check.
[out]rminimum distance to query point.
[out]idxindex of candidate point.
[in]dist_funcfunction to calculate the distance between current point and the query point.

Definition at line 253 of file KDPoint.h.

255 {
256  // end processing when reach leaf
257  if ( node == nullptr ) {
258  return;
259  }
260 
261  const KDPoint<T,D>& point = node->dataRef;
262 
263  // update minimum distance and candidate point
264  const double dist = dist_func(query, point);
265  if (dist < r) {
266  idx = node->dataIdx;
267  r = dist;
268  }
269 
270  const int axis_ = node->axis;
271  const Node* next_node = (query.at(axis_) < point.at(axis_))? node->leftPtr.get() : node->rightPtr.get();
272 
273  // recursively call self until reach leaf node
274  nearestNeighborRec( query, next_node, r, idx, dist_func );
275 
276  // Check if hyper-sphere with radius r overlaps neighbor hyper-plane.
277  KDPoint<T,D> proj = query;
278  proj[axis_] = point.at(axis_); // proj is the projected point of query on hyperplane x[axis_]=point[axis_]
279  double diff = dist_func(proj, query);
280  if ( diff < r ) {
281  // if hyper-sphere overlaps, check the other region separated by the hyper-plane.
282  const Node* next_node_opps = (query.at(axis_) < point.at(axis_))? node->rightPtr.get() : node->leftPtr.get();
283  nearestNeighborRec( query, next_node_opps, r, idx, dist_func );
284  }
285  return;
286 }

◆ unlock()

template<typename T , size_t D>
void TrigVSI::KDTree< T, D >::unlock ( )
inline

Definition at line 186 of file KDPoint.h.

186 { m_locked = false; };

Member Data Documentation

◆ m_datas

template<typename T , size_t D>
std::vector<KDPoint<T,D> > TrigVSI::KDTree< T, D >::m_datas
private

Container of the points.

Definition at line 192 of file KDPoint.h.

◆ m_idLength

template<typename T , size_t D>
size_t TrigVSI::KDTree< T, D >::m_idLength
private

Definition at line 194 of file KDPoint.h.

◆ m_indices

template<typename T , size_t D>
std::vector<size_t> TrigVSI::KDTree< T, D >::m_indices
private

A list of indices of points in m_datas.

Definition at line 193 of file KDPoint.h.

◆ m_locked

template<typename T , size_t D>
bool TrigVSI::KDTree< T, D >::m_locked
private

Definition at line 195 of file KDPoint.h.

◆ m_rootNode

template<typename T , size_t D>
std::unique_ptr<Node> TrigVSI::KDTree< T, D >::m_rootNode
private

The root node of the tree.

Definition at line 191 of file KDPoint.h.


The documentation for this class was generated from the following file:
beamspotman.r
def r
Definition: beamspotman.py:676
egammaParameters::depth
@ depth
pointing depth of the shower as calculated in egammaqgcld
Definition: egammaParamDefs.h:276
TrigVSI::KDTree::m_indices
std::vector< size_t > m_indices
A list of indices of points in m_datas.
Definition: KDPoint.h:193
TrigVSI::KDTree::Node::axis
int axis
Definition: KDPoint.h:168
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
TrigVSI::KDTree::Node::leftPtr
std::unique_ptr< Node > leftPtr
Definition: KDPoint.h:169
TrigVSI::KDTree::m_rootNode
std::unique_ptr< Node > m_rootNode
The root node of the tree.
Definition: KDPoint.h:188
query
Definition: query.py:1
TrigVSI::KDTree::m_locked
bool m_locked
Definition: KDPoint.h:195
TrigVSI::KDTree::buildTree
std::unique_ptr< Node > buildTree(int, int, int)
recursive function to create tree structure.
Definition: KDPoint.h:222
lumiFormat.i
int i
Definition: lumiFormat.py:85
TrigVSI::KDTree::m_datas
std::vector< KDPoint< T, D > > m_datas
Container of the points.
Definition: KDPoint.h:192
TrigVSI::KDTree::nearestNeighborRec
void nearestNeighborRec(const KDPoint< T, D > &, const Node *, double &, int &, std::function< double(const KDPoint< T, D > &, const KDPoint< T, D > &)> &)
recursive function for nearest neighbor searching.
Definition: KDPoint.h:253
beamspotman.n
n
Definition: beamspotman.py:731
make_coralServer_rep.proj
proj
Definition: make_coralServer_rep.py:48
query_example.query
query
Definition: query_example.py:15
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
TrigVSI::KDTree::m_idLength
size_t m_idLength
Definition: KDPoint.h:194
node
Definition: memory_hooks-stdcmalloc.h:74
TrigVSI::KDTree::Node::rightPtr
std::unique_ptr< Node > rightPtr
Definition: KDPoint.h:170