CMS 3D CMS Logo

KDTreeLinkerTools.h
Go to the documentation of this file.
1 #ifndef KDTreeLinkerToolsTemplated_h
2 #define KDTreeLinkerToolsTemplated_h
3 
4 #include <algorithm>
5 #include <vector>
6 
7 // Box structure used to define 2D field.
8 // It's used in KDTree building step to divide the detector
9 // space (ECAL, HCAL...) and in searching step to create a bounding
10 // box around the demanded point (Track collision point, PS projection...).
11 struct KDTreeBox {
12  float dim1min, dim1max;
13  float dim2min, dim2max;
14 
15 public:
16  KDTreeBox(float d1min, float d1max, float d2min, float d2max)
17  : dim1min(d1min), dim1max(d1max), dim2min(d2min), dim2max(d2max) {}
18 
19  KDTreeBox() : dim1min(0), dim1max(0), dim2min(0), dim2max(0) {}
20 };
21 
22 // Data stored in each KDTree node.
23 // The dim1/dim2 fields are usually the duplication of some PFRecHit values
24 // (eta/phi or x/y). But in some situations, phi field is shifted by +-2.Pi
25 template <typename DATA>
28  float dim[2];
29  enum { kDim1 = 0, kDim2 = 1 };
30 
31 public:
33 
34  KDTreeNodeInfo(const DATA& d, float dim_1, float dim_2) : data(d), dim{dim_1, dim_2} {}
35 };
36 
37 template <typename DATA>
38 struct KDTreeNodes {
39  std::vector<float> median; // or dimCurrent;
40  std::vector<int> right;
41  std::vector<float> dimOther;
42  std::vector<DATA> data;
43 
44  int poolSize;
45  int poolPos;
46 
47  constexpr KDTreeNodes() : poolSize(-1), poolPos(-1) {}
48 
49  bool empty() const { return poolPos == -1; }
50  int size() const { return poolPos + 1; }
51 
52  void clear() {
53  median.clear();
54  right.clear();
55  dimOther.clear();
56  data.clear();
57  poolSize = -1;
58  poolPos = -1;
59  }
60 
61  int getNextNode() {
62  ++poolPos;
63  return poolPos;
64  }
65 
66  void build(int sizeData) {
67  poolSize = sizeData * 2 - 1;
68  median.resize(poolSize);
69  right.resize(poolSize);
70  dimOther.resize(poolSize);
71  data.resize(poolSize);
72  };
73 
74  constexpr bool isLeaf(int right) const {
75  // Valid values of right are always >= 2
76  // index 0 is the root, and 1 is the first left node
77  // Exploit index values 0 and 1 to mark which of dim1/dim2 is the
78  // current one in recSearch() at the depth of the leaf.
79  return right < 2;
80  }
81 
82  bool isLeafIndex(int index) const { return isLeaf(right[index]); }
83 };
84 
85 #endif
constexpr KDTreeNodes()
constexpr bool isLeaf(int right) const
std::vector< int > right
std::vector< float > dimOther
std::vector< float > median
KDTreeBox(float d1min, float d1max, float d2min, float d2max)
bool empty() const
bool isLeafIndex(int index) const
int size() const
void build(int sizeData)
std::vector< DATA > data
#define constexpr
KDTreeNodeInfo(const DATA &d, float dim_1, float dim_2)