CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
KDTreeLinkerAlgo Class Reference

#include <KDTreeLinkerAlgo.h>

Public Member Functions

void build (std::vector< KDTreeNodeInfo > &eltList, const KDTreeBox &region)
 
void clear ()
 
 KDTreeLinkerAlgo ()
 
void search (const KDTreeBox &searchBox, std::vector< KDTreeNodeInfo > &resRecHitList)
 
 ~KDTreeLinkerAlgo ()
 

Private Member Functions

void addSubtree (const KDTreeNode *current, std::vector< KDTreeNodeInfo > &recHits)
 
void clearTree ()
 
KDTreeNodegetNextNode ()
 
int medianSearch (std::vector< KDTreeNodeInfo > &eltList, int low, int high, int treeDepth)
 
KDTreeNoderecBuild (std::vector< KDTreeNodeInfo > &eltList, int low, int hight, int depth, const KDTreeBox &region)
 
void recSearch (const KDTreeNode *current, const KDTreeBox &trackBox, std::vector< KDTreeNodeInfo > &recHits)
 
void swap (KDTreeNodeInfo &e1, KDTreeNodeInfo &e2)
 

Private Attributes

KDTreeNodenodePool_
 
int nodePoolPos_
 
int nodePoolSize_
 
KDTreeNoderoot_
 

Detailed Description

Definition at line 10 of file KDTreeLinkerAlgo.h.

Constructor & Destructor Documentation

KDTreeLinkerAlgo::KDTreeLinkerAlgo ( )

Definition at line 3 of file KDTreeLinkerAlgo.cc.

4  : root_ (0),
5  nodePool_(0),
6  nodePoolSize_(-1),
7  nodePoolPos_(-1)
8 {
9 }
KDTreeNode * nodePool_
KDTreeNode * root_
KDTreeLinkerAlgo::~KDTreeLinkerAlgo ( )

Definition at line 11 of file KDTreeLinkerAlgo.cc.

References clear().

12 {
13  clear();
14 }

Member Function Documentation

void KDTreeLinkerAlgo::addSubtree ( const KDTreeNode current,
std::vector< KDTreeNodeInfo > &  recHits 
)
private

Definition at line 209 of file KDTreeLinkerAlgo.cc.

References KDTreeNode::left, KDTreeNode::rh, and KDTreeNode::right.

Referenced by recSearch().

211 {
212  // By construction, current can't be null
213  assert(current != 0);
214 
215  if ((current->left == 0) && (current->right == 0)) // leaf
216  recHits.push_back(current->rh);
217  else { // node
218  addSubtree(current->left, recHits);
219  addSubtree(current->right, recHits);
220  }
221 }
void addSubtree(const KDTreeNode *current, std::vector< KDTreeNodeInfo > &recHits)
KDTreeNode * right
KDTreeNode * left
KDTreeNodeInfo rh
void KDTreeLinkerAlgo::build ( std::vector< KDTreeNodeInfo > &  eltList,
const KDTreeBox region 
)

Definition at line 17 of file KDTreeLinkerAlgo.cc.

References nodePool_, nodePoolSize_, recBuild(), and root_.

Referenced by KDTreeLinkerTrackEcal::buildTree(), KDTreeLinkerTrackHcal::buildTree(), and KDTreeLinkerPSEcal::buildTree().

19 {
20  if (eltList.size()) {
21  nodePoolSize_ = eltList.size() * 2 - 1;
23 
24  // Here we build the KDTree
25  root_ = recBuild(eltList, 0, eltList.size(), 0, region);
26  }
27 }
KDTreeNode * nodePool_
KDTreeNode * recBuild(std::vector< KDTreeNodeInfo > &eltList, int low, int hight, int depth, const KDTreeBox &region)
KDTreeNode * root_
void KDTreeLinkerAlgo::clear ( void  )

Definition at line 235 of file KDTreeLinkerAlgo.cc.

References clearTree(), and root_.

Referenced by KDTreeLinkerPSEcal::clear(), KDTreeLinkerTrackHcal::clear(), KDTreeLinkerTrackEcal::clear(), and ~KDTreeLinkerAlgo().

236 {
237  if (root_)
238  clearTree();
239 }
KDTreeNode * root_
void KDTreeLinkerAlgo::clearTree ( )
private

Definition at line 225 of file KDTreeLinkerAlgo.cc.

References nodePool_, nodePoolPos_, nodePoolSize_, and root_.

Referenced by clear().

226 {
227  delete[] nodePool_;
228  nodePool_ = 0;
229  root_ = 0;
230  nodePoolSize_ = -1;
231  nodePoolPos_ = -1;
232 }
KDTreeNode * nodePool_
KDTreeNode * root_
KDTreeNode * KDTreeLinkerAlgo::getNextNode ( )
private

Definition at line 243 of file KDTreeLinkerAlgo.cc.

References nodePool_, nodePoolPos_, and nodePoolSize_.

Referenced by recBuild().

244 {
245  ++nodePoolPos_;
246 
247  // The tree size is exactly 2 * nbrElts - 1 and this is the total allocated memory.
248  // If we have used more than that....there is a big problem.
249  assert(nodePoolPos_ < nodePoolSize_);
250 
251  return &(nodePool_[nodePoolPos_]);
252 }
KDTreeNode * nodePool_
int KDTreeLinkerAlgo::medianSearch ( std::vector< KDTreeNodeInfo > &  eltList,
int  low,
int  high,
int  treeDepth 
)
private

Definition at line 90 of file KDTreeLinkerAlgo.cc.

References KDTreeNodeInfo::dim1, KDTreeNodeInfo::dim2, i, j, prof2calltree::l, m, and swap().

Referenced by recBuild().

94 {
95  //We should have at least 1 element to calculate the median...
96  assert(low < high);
97 
98  int nbrElts = high - low;
99  int median = (nbrElts & 1) ? nbrElts / 2
100  : nbrElts / 2 - 1;
101  median += low;
102 
103  int l = low;
104  int m = high - 1;
105 
106  while (l < m) {
107  KDTreeNodeInfo elt = eltList[median];
108  int i = l;
109  int j = m;
110 
111  do {
112 
113  // The even depth is associated to dim1 dimension
114  // The odd one to dim2 dimension
115  if (treeDepth & 1) {
116  while (eltList[i].dim2 < elt.dim2) i++;
117  while (eltList[j].dim2 > elt.dim2) j--;
118  } else {
119  while (eltList[i].dim1 < elt.dim1) i++;
120  while (eltList[j].dim1 > elt.dim1) j--;
121  }
122 
123  if (i <= j){
124  swap(eltList[i], eltList[j]);
125  i++;
126  j--;
127  }
128  } while (i <= j);
129  if (j < median) l = i;
130  if (i > median) m = j;
131  }
132 
133  return median;
134 }
int i
Definition: DBlmapReader.cc:9
int j
Definition: DBlmapReader.cc:9
void swap(KDTreeNodeInfo &e1, KDTreeNodeInfo &e2)
KDTreeNode * KDTreeLinkerAlgo::recBuild ( std::vector< KDTreeNodeInfo > &  eltList,
int  low,
int  hight,
int  depth,
const KDTreeBox region 
)
private

Definition at line 31 of file KDTreeLinkerAlgo.cc.

References KDTreeBox::dim1max, KDTreeBox::dim1min, KDTreeBox::dim2max, KDTreeBox::dim2min, getNextNode(), python.Node::leaf, KDTreeNode::left, medianSearch(), python.Node::node, KDTreeNode::right, and KDTreeNode::setAttributs().

Referenced by build().

36 {
37  int portionSize = high - low;
38 
39  // By construction, portionSize > 0 can't happend.
40  assert(portionSize > 0);
41 
42  if (portionSize == 1) { // Leaf case
43 
45  leaf->setAttributs(region, eltList[low]);
46  return leaf;
47 
48  } else { // Node case
49 
50  // The even depth is associated to dim1 dimension
51  // The odd one to dim2 dimension
52  int medianId = medianSearch(eltList, low, high, depth);
53 
54  // We create the node
56  node->setAttributs(region);
57 
58 
59  // Here we split into 2 halfplanes the current plane
60  KDTreeBox leftRegion = region;
61  KDTreeBox rightRegion = region;
62  if (depth & 1) {
63 
64  double medianVal = eltList[medianId].dim2;
65  leftRegion.dim2max = medianVal;
66  rightRegion.dim2min = medianVal;
67 
68  } else {
69 
70  double medianVal = eltList[medianId].dim1;
71  leftRegion.dim1max = medianVal;
72  rightRegion.dim1min = medianVal;
73 
74  }
75 
76  ++depth;
77  ++medianId;
78 
79  // We recursively build the son nodes
80  node->left = recBuild(eltList, low, medianId, depth, leftRegion);
81  node->right = recBuild(eltList, medianId, high, depth, rightRegion);
82 
83  return node;
84  }
85 }
void setAttributs(const KDTreeBox &regionBox, const KDTreeNodeInfo &rhinfo)
tuple node
Definition: Node.py:50
tuple leaf
Definition: Node.py:62
KDTreeNode * getNextNode()
KDTreeNode * right
KDTreeNode * left
KDTreeNode * recBuild(std::vector< KDTreeNodeInfo > &eltList, int low, int hight, int depth, const KDTreeBox &region)
int medianSearch(std::vector< KDTreeNodeInfo > &eltList, int low, int high, int treeDepth)
void KDTreeLinkerAlgo::recSearch ( const KDTreeNode current,
const KDTreeBox trackBox,
std::vector< KDTreeNodeInfo > &  recHits 
)
private

Definition at line 154 of file KDTreeLinkerAlgo.cc.

References addSubtree(), KDTreeNodeInfo::dim1, KDTreeBox::dim1max, KDTreeBox::dim1min, KDTreeNodeInfo::dim2, KDTreeBox::dim2max, KDTreeBox::dim2min, KDTreeNode::left, KDTreeNode::region, KDTreeNode::rh, and KDTreeNode::right.

Referenced by search().

157 {
158  // By construction, current can't be null
159  assert(current != 0);
160 
161  // By Construction, a node can't have just 1 son.
162  assert (!(((current->left == 0) && (current->right != 0)) ||
163  ((current->left != 0) && (current->right == 0))));
164 
165  if ((current->left == 0) && (current->right == 0)) {//leaf case
166 
167  // If point inside the rectangle/area
168  if ((current->rh.dim1 >= trackBox.dim1min) && (current->rh.dim1 <= trackBox.dim1max) &&
169  (current->rh.dim2 >= trackBox.dim2min) && (current->rh.dim2 <= trackBox.dim2max))
170  recHits.push_back(current->rh);
171 
172  } else {
173 
174  //if region( v->left ) is fully contained in the rectangle
175  if ((current->left->region.dim1min >= trackBox.dim1min) &&
176  (current->left->region.dim1max <= trackBox.dim1max) &&
177  (current->left->region.dim2min >= trackBox.dim2min) &&
178  (current->left->region.dim2max <= trackBox.dim2max))
179  addSubtree(current->left, recHits);
180 
181  else { //if region( v->left ) intersects the rectangle
182 
183  if (!((current->left->region.dim1min >= trackBox.dim1max) ||
184  (current->left->region.dim1max <= trackBox.dim1min) ||
185  (current->left->region.dim2min >= trackBox.dim2max) ||
186  (current->left->region.dim2max <= trackBox.dim2min)))
187  recSearch(current->left, trackBox, recHits);
188  }
189 
190  //if region( v->right ) is fully contained in the rectangle
191  if ((current->right->region.dim1min >= trackBox.dim1min) &&
192  (current->right->region.dim1max <= trackBox.dim1max) &&
193  (current->right->region.dim2min >= trackBox.dim2min) &&
194  (current->right->region.dim2max <= trackBox.dim2max))
195  addSubtree(current->right, recHits);
196 
197  else { //if region( v->right ) intersects the rectangle
198 
199  if (!((current->right->region.dim1min >= trackBox.dim1max) ||
200  (current->right->region.dim1max <= trackBox.dim1min) ||
201  (current->right->region.dim2min >= trackBox.dim2max) ||
202  (current->right->region.dim2max <= trackBox.dim2min)))
203  recSearch(current->right, trackBox, recHits);
204  }
205  }
206 }
void addSubtree(const KDTreeNode *current, std::vector< KDTreeNodeInfo > &recHits)
KDTreeBox region
KDTreeNode * right
KDTreeNode * left
void recSearch(const KDTreeNode *current, const KDTreeBox &trackBox, std::vector< KDTreeNodeInfo > &recHits)
KDTreeNodeInfo rh
void KDTreeLinkerAlgo::search ( const KDTreeBox searchBox,
std::vector< KDTreeNodeInfo > &  resRecHitList 
)

Definition at line 146 of file KDTreeLinkerAlgo.cc.

References recSearch(), and root_.

Referenced by KDTreeLinkerPSEcal::searchLinks(), KDTreeLinkerTrackHcal::searchLinks(), and KDTreeLinkerTrackEcal::searchLinks().

148 {
149  if (root_)
150  recSearch(root_, trackBox, recHits);
151 }
void recSearch(const KDTreeNode *current, const KDTreeBox &trackBox, std::vector< KDTreeNodeInfo > &recHits)
KDTreeNode * root_
void KDTreeLinkerAlgo::swap ( KDTreeNodeInfo e1,
KDTreeNodeInfo e2 
)
private

Definition at line 137 of file KDTreeLinkerAlgo.cc.

References tmp.

Referenced by medianSearch().

139 {
140  KDTreeNodeInfo tmp = e1;
141  e1 = e2;
142  e2 = tmp;
143 }
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100

Member Data Documentation

KDTreeNode* KDTreeLinkerAlgo::nodePool_
private

Definition at line 35 of file KDTreeLinkerAlgo.h.

Referenced by build(), clearTree(), and getNextNode().

int KDTreeLinkerAlgo::nodePoolPos_
private

Definition at line 37 of file KDTreeLinkerAlgo.h.

Referenced by clearTree(), and getNextNode().

int KDTreeLinkerAlgo::nodePoolSize_
private

Definition at line 36 of file KDTreeLinkerAlgo.h.

Referenced by build(), clearTree(), and getNextNode().

KDTreeNode* KDTreeLinkerAlgo::root_
private

Definition at line 32 of file KDTreeLinkerAlgo.h.

Referenced by build(), clear(), clearTree(), and search().