Belle II Software development
Utils.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <Math/Vector3D.h>
11
12#include <cstddef>
13#include <vector>
14
15namespace Belle2::GNNFinder::Utils {
16
23 typedef struct KDTHit {
25 double x;
27 double y;
35 double squaredDistanceTo(const KDTHit& other) const
36 {
37 double dx = x - other.x;
38 double dy = y - other.y;
39 return dx * dx + dy * dy;
40 }
41 } KDTHit;
42
50 typedef struct KDTNode {
58 bool used;
60 int dim;
61 } KDTNode;
62
71 private:
73 std::vector<KDTNode*> m_pool;
75 size_t m_index;
76
77 public:
85 explicit KDTNodePool(size_t capacity);
86
91
93 KDTNodePool(const KDTNodePool&) = delete;
94
97
104 KDTNode* allocate();
105
111 void reset();
112
113 };
114
124
125 private:
139 static KDTNode* buildKDTree(std::vector<KDTHit>::iterator begin, std::vector<KDTHit>::iterator end, const int depth,
140 KDTNodePool& pool, const size_t insertionSortThreshold = 10);
141
151 template<typename Iterator, typename Compare>
152 static inline void insertionSort(Iterator begin, Iterator end, Compare cmp);
153
159 static void freeKDTree(KDTNode* node);
160
172 static void nearestNeighbor(const KDTNode* node, const KDTHit& query, KDTHit& best, double& bestDist);
173
183 static bool markUsed(KDTNode* node, const KDTHit& hit);
184
185
186 public:
198 static std::vector<int> orderHits(const double startingX, const double startingY,
199 std::vector<KDTHit> kdtHits);
200
204 HitOrderer() = default;
205
209 ~HitOrderer() = default;
210 };
211
225 std::pair<double, double> intersectCylinderXY(const ROOT::Math::XYZVector& pos,
226 const ROOT::Math::XYZVector& mom,
227 const double targetR);
228
229}
static void nearestNeighbor(const KDTNode *node, const KDTHit &query, KDTHit &best, double &bestDist)
Finds the closest unused neighbor to a query hit in the KD-tree.
Definition Utils.cc:93
HitOrderer()=default
Constructor.
static bool markUsed(KDTNode *node, const KDTHit &hit)
Marks a KD-tree node as used if its hit index matches the given hit.
Definition Utils.cc:126
static void freeKDTree(KDTNode *node)
Recursively frees all nodes in a KD-tree.
Definition Utils.cc:150
static KDTNode * buildKDTree(std::vector< KDTHit >::iterator begin, std::vector< KDTHit >::iterator end, const int depth, KDTNodePool &pool, const size_t insertionSortThreshold=10)
Recursively builds a balanced KD-tree from a range of KDTHits.
Definition Utils.cc:57
static void insertionSort(Iterator begin, Iterator end, Compare cmp)
Sorts a small range using insertion sort for performance.
Definition Utils.cc:47
static std::vector< int > orderHits(const double startingX, const double startingY, std::vector< KDTHit > kdtHits)
Sort hits spatially based on proximity to a starting position.
Definition Utils.cc:159
Memory pool for pre-allocating and reusing KDTNode objects.
Definition Utils.h:70
size_t m_index
Current index for the next available node in the pool.
Definition Utils.h:75
KDTNodePool(size_t capacity)
Constructs a pool with the specified capacity.
Definition Utils.cc:18
KDTNodePool & operator=(const KDTNodePool &)=delete
Assignment operator.
std::vector< KDTNode * > m_pool
Internal storage for preallocated KD-tree nodes.
Definition Utils.h:73
KDTNodePool(const KDTNodePool &)=delete
Copy constructor.
void reset()
Resets the pool index, making all nodes available for reuse.
Definition Utils.cc:39
KDTNode * allocate()
Allocates and returns the next available KDTNode from the pool.
Definition Utils.cc:31
map< unsigned, size_t >::const_iterator Iterator
Iterator for m_map.
Lightweight 2D representation of a CDC hit for KD-tree sorting.
Definition Utils.h:23
double squaredDistanceTo(const KDTHit &other) const
Compute squared distance to another KDTHit.
Definition Utils.h:35
double y
Y coordinate of the hit.
Definition Utils.h:27
int hitIndex
Index of the hit in the original input collection.
Definition Utils.h:29
double x
X coordinate of the hit.
Definition Utils.h:25
Node structure for a 2D KD-tree used in spatial hit sorting.
Definition Utils.h:50
KDTHit hit
The hit associated with this node (contains x, y, and index).
Definition Utils.h:52
int dim
Splitting dimension: 0 for x-axis, 1 for y-axis.
Definition Utils.h:60
KDTNode * left
Pointer to the left subtree (for values less than split).
Definition Utils.h:54
KDTNode * right
Pointer to the right subtree (for values greater than split).
Definition Utils.h:56
bool used
Flag indicating whether the node has already been used in traversal.
Definition Utils.h:58