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

struct  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 174 of file KDPoint.h.

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

◆ KDTree() [2/2]

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

Definition at line 180 of file KDPoint.h.

180 : 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 186 of file KDPoint.h.

186 { 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 220 of file KDPoint.h.

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

◆ 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 205 of file KDPoint.h.

206 {
207  if (m_locked) return;
209 }

◆ lock()

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

Definition at line 183 of file KDPoint.h.

183 { 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 251 of file KDPoint.h.

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

◆ unlock()

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

Definition at line 184 of file KDPoint.h.

184 { 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 190 of file KDPoint.h.

◆ m_idLength

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

Definition at line 192 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 191 of file KDPoint.h.

◆ m_locked

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

Definition at line 193 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 189 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:191
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
TrigVSI::KDTree::m_rootNode
std::unique_ptr< Node > m_rootNode
The root node of the tree.
Definition: KDPoint.h:186
query
Definition: query.py:1
TrigVSI::KDTree::m_locked
bool m_locked
Definition: KDPoint.h:193
TrigVSI::KDTree::buildTree
std::unique_ptr< Node > buildTree(int, int, int)
recursive function to create tree structure.
Definition: KDPoint.h:220
lumiFormat.i
int i
Definition: lumiFormat.py:85
TrigVSI::KDTree::Node::leftPtr
std::unique_ptr< Node > leftPtr
Definition: KDPoint.h:167
TrigVSI::KDTree::m_datas
std::vector< KDPoint< T, D > > m_datas
Container of the points.
Definition: KDPoint.h:190
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:251
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
TrigVSI::KDTree::Node::rightPtr
std::unique_ptr< Node > rightPtr
Definition: KDPoint.h:168
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
TrigVSI::KDTree::m_idLength
size_t m_idLength
Definition: KDPoint.h:192
node
Definition: memory_hooks-stdcmalloc.h:74