APRILContent
Algorithm of Particle Reconstruction for ILC - implementation with PandoraSDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations
KDTreeLinkerToolsT.h
Go to the documentation of this file.
1 
8 #ifndef LC_KD_TREE_LINKER_TOOLS_TEMPLATED_H
9 #define LC_KD_TREE_LINKER_TOOLS_TEMPLATED_H 1
10 
11 #include "Api/PandoraContentApi.h"
12 
13 #include "Objects/CaloHit.h"
14 #include "Objects/Track.h"
15 #include "Objects/CartesianVector.h"
16 
17 #include "Pandora/PandoraInternal.h"
18 
19 #include <array>
20 
21 using namespace std;
22 
23 namespace pandora { class Algorithm; }
24 
25 //------------------------------------------------------------------------------------------------------------------------------------------
26 
27 namespace april_content
28 {
29 
34 template<unsigned DIM>
36 {
37 public:
41  KDTreeBoxT();
42 
48  template<typename... Ts>
49  KDTreeBoxT(Ts... dimargs);
50 
51  std::array<float, DIM> dimmin;
52  std::array<float, DIM> dimmax;
53 };
54 
55 typedef KDTreeBoxT<2> KDTreeBox;
58 
59 //------------------------------------------------------------------------------------------------------------------------------------------
60 
65 template<typename DATA, unsigned DIM>
66 class KDTreeNodeInfoT
67 {
68 public:
73 
80  template<typename... Ts>
81  KDTreeNodeInfoT(const DATA &d, Ts... dimargs);
82 
83  DATA data;
84  std::array<float, DIM> dims;
85 };
86 
87 //------------------------------------------------------------------------------------------------------------------------------------------
88 
92 template <typename DATA, unsigned DIM>
94 {
95 public:
99  KDTreeNodeT();
100 
107  void setAttributs(const KDTreeBoxT<DIM> &regionBox, const KDTreeNodeInfoT<DATA, DIM> &infoToStore);
108 
114  void setAttributs(const KDTreeBoxT<DIM> &regionBox);
115 
120 };
121 
122 //------------------------------------------------------------------------------------------------------------------------------------------
123 
127 template<typename T>
129 {
130 public:
138  static const pandora::CartesianVector &position(const T *const t);
139 };
140 
141 //------------------------------------------------------------------------------------------------------------------------------------------
142 
151 std::pair<float,float> minmax(const float a, const float b);
152 
161 template<typename T>
162 KDTreeCube fill_and_bound_3d_kd_tree(const MANAGED_CONTAINER<const T*> &points, std::vector<KDTreeNodeInfoT<const T*, 3> > &nodes);
163 
172 template<typename T>
173 KDTreeCube fill_and_bound_3d_kd_tree_by_index(const std::vector<const T*> &points, std::vector<KDTreeNodeInfoT<unsigned, 3> > &nodes);
174 
185 template<typename T>
186 KDTreeCube fill_and_bound_3d_kd_tree(const pandora::Algorithm *const caller, const MANAGED_CONTAINER<const T*> &points,
187  std::vector<KDTreeNodeInfoT<const T*, 3> > &nodes, bool passthru = false);
188 
199 KDTreeTesseract fill_and_bound_4d_kd_tree(const pandora::Algorithm *const caller, const MANAGED_CONTAINER<const pandora::CaloHit*> &points,
200  std::vector<KDTreeNodeInfoT<const pandora::CaloHit*, 4> > &nodes, bool passthru = false);
201 
212 KDTreeCube build_3d_kd_search_region(const pandora::CaloHit *const point, const float x_span, const float y_span, const float z_span);
213 
225 KDTreeTesseract build_4d_kd_search_region(const pandora::CaloHit *const point, const float x_span, const float y_span, const float z_span, const float search_layer);
226 
238 KDTreeTesseract build_4d_kd_search_region(const pandora::CartesianVector &pos, const float x_span, const float y_span, const float z_span, const float search_layer);
239 
240 //------------------------------------------------------------------------------------------------------------------------------------------
241 //------------------------------------------------------------------------------------------------------------------------------------------
242 
243 template<unsigned DIM>
245 {
246 }
247 
248 //------------------------------------------------------------------------------------------------------------------------------------------
249 
250 template<unsigned DIM>
251 template<typename... Ts>
252 inline KDTreeBoxT<DIM>::KDTreeBoxT(Ts... dimargs)
253 {
254  static_assert(sizeof...(dimargs) == 2 * DIM, "Constructor requires 2*DIM args");
255  std::vector<float> dims = {dimargs...};
256 
257  for (unsigned i = 0; i < DIM; ++i)
258  {
259  dimmin[i] = dims[2 * i];
260  dimmax[i] = dims[2 * i + 1];
261  }
262 }
263 
264 //------------------------------------------------------------------------------------------------------------------------------------------
265 //------------------------------------------------------------------------------------------------------------------------------------------
266 
267 template<typename DATA, unsigned DIM>
269  data()
270 {
271 }
272 
273 //------------------------------------------------------------------------------------------------------------------------------------------
274 
275 template<typename DATA, unsigned DIM>
276 template<typename... Ts>
277 inline KDTreeNodeInfoT<DATA, DIM>::KDTreeNodeInfoT(const DATA &d, Ts... dimargs) :
278  data(d),
279  dims{ {dimargs...} }
280 {
281 }
282 
283 //------------------------------------------------------------------------------------------------------------------------------------------
284 //------------------------------------------------------------------------------------------------------------------------------------------
285 
286 template <typename DATA, unsigned DIM>
288  left(nullptr),
289  right(nullptr)
290 {
291 }
292 
293 //------------------------------------------------------------------------------------------------------------------------------------------
294 
295 template <typename DATA, unsigned DIM>
296 inline void KDTreeNodeT<DATA, DIM>::setAttributs(const KDTreeBoxT<DIM> &regionBox, const KDTreeNodeInfoT<DATA, DIM> &infoToStore)
297 {
298  info = infoToStore;
299  region = regionBox;
300 }
301 
302 //------------------------------------------------------------------------------------------------------------------------------------------
303 
304 template <typename DATA, unsigned DIM>
306 {
307  region = regionBox;
308 }
309 
310 //------------------------------------------------------------------------------------------------------------------------------------------
311 
312 template<>
313 inline const pandora::CartesianVector &kdtree_type_adaptor<const pandora::Track>::position(const pandora::Track *const t)
314 {
315  return t->GetTrackStateAtCalorimeter().GetPosition();
316 }
317 
318 template<>
319 inline const pandora::CartesianVector &kdtree_type_adaptor<const pandora::CaloHit>::position(const pandora::CaloHit *const t)
320 {
321  return t->GetPositionVector();
322 }
323 
324 template<>
325 inline const pandora::CartesianVector &kdtree_type_adaptor<const pandora::CartesianVector>::position(const pandora::CartesianVector *const t)
326 {
327  return *t;
328 }
329 
330 //------------------------------------------------------------------------------------------------------------------------------------------
331 
332 template<typename T>
333 KDTreeCube fill_and_bound_3d_kd_tree(const MANAGED_CONTAINER<const T*> &points, std::vector<KDTreeNodeInfoT<const T*, 3> > &nodes)
334 {
335  std::array<float, 3> minpos{ {0.f, 0.f, 0.f} }, maxpos{ {0.f, 0.f, 0.f} };
336 
337  unsigned i = 0;
338 
339  for (const T *const point : points)
340  {
341  const pandora::CartesianVector &pos = kdtree_type_adaptor<const T>::position(point);
342  nodes.emplace_back(point, pos.GetX(), pos.GetY(), pos.GetZ());
343 
344  if (0 == i)
345  {
346  minpos[0] = pos.GetX(); minpos[1] = pos.GetY(); minpos[2] = pos.GetZ();
347  maxpos[0] = pos.GetX(); maxpos[1] = pos.GetY(); maxpos[2] = pos.GetZ();
348  }
349  else
350  {
351  minpos[0] = std::min(pos.GetX(), minpos[0]);
352  minpos[1] = std::min(pos.GetY(), minpos[1]);
353  minpos[2] = std::min(pos.GetZ(), minpos[2]);
354  maxpos[0] = std::max(pos.GetX(), maxpos[0]);
355  maxpos[1] = std::max(pos.GetY(), maxpos[1]);
356  maxpos[2] = std::max(pos.GetZ(), maxpos[2]);
357  }
358 
359  ++i;
360  }
361 
362  return KDTreeCube(minpos[0], maxpos[0], minpos[1], maxpos[1], minpos[2], maxpos[2]);
363 }
364 
365 //------------------------------------------------------------------------------------------------------------------------------------------
366 
367 template<typename T>
368 KDTreeCube fill_and_bound_3d_kd_tree_by_index(const std::vector<const T*> &points, std::vector<KDTreeNodeInfoT<unsigned, 3> > &nodes)
369 {
370  std::array<float, 3> minpos{ {0.f, 0.f, 0.f} }, maxpos{ {0.f, 0.f, 0.f} };
371 
372  unsigned i = 0;
373 
374  for (const T *const point : points)
375  {
376  const pandora::CartesianVector &pos = kdtree_type_adaptor<const T>::position(point);
377  nodes.emplace_back(i, pos.GetX(), pos.GetY(), pos.GetZ());
378 
379  if (0 == i)
380  {
381  minpos[0] = pos.GetX(); minpos[1] = pos.GetY(); minpos[2] = pos.GetZ();
382  maxpos[0] = pos.GetX(); maxpos[1] = pos.GetY(); maxpos[2] = pos.GetZ();
383  }
384  else
385  {
386  minpos[0] = std::min(pos.GetX(), minpos[0]);
387  minpos[1] = std::min(pos.GetY(), minpos[1]);
388  minpos[2] = std::min(pos.GetZ(), minpos[2]);
389  maxpos[0] = std::max(pos.GetX(), maxpos[0]);
390  maxpos[1] = std::max(pos.GetY(), maxpos[1]);
391  maxpos[2] = std::max(pos.GetZ(), maxpos[2]);
392  }
393 
394  ++i;
395  }
396 
397  return KDTreeCube(minpos[0], maxpos[0], minpos[1], maxpos[1], minpos[2], maxpos[2]);
398 }
399 
400 //------------------------------------------------------------------------------------------------------------------------------------------
401 
402 template<typename T>
403 KDTreeCube fill_and_bound_3d_kd_tree(const pandora::Algorithm *const caller, const MANAGED_CONTAINER<const T*> &points,
404  std::vector<KDTreeNodeInfoT<const T*, 3> > &nodes, bool passthru)
405 {
406  std::array<float, 3> minpos{ {0.f, 0.f, 0.f} }, maxpos{ {0.f, 0.f, 0.f} };
407 
408  unsigned i = 0;
409 
410  for (const T *const point : points)
411  {
412  if (!passthru && !PandoraContentApi::IsAvailable(*caller, point))
413  continue;
414 
415  const pandora::CartesianVector &pos = kdtree_type_adaptor<const T>::position(point);
416  nodes.emplace_back(point, pos.GetX(), pos.GetY(), pos.GetZ());
417 
418  if (0 == i)
419  {
420  minpos[0] = pos.GetX(); minpos[1] = pos.GetY(); minpos[2] = pos.GetZ();
421  maxpos[0] = pos.GetX(); maxpos[1] = pos.GetY(); maxpos[2] = pos.GetZ();
422  }
423  else
424  {
425  minpos[0] = std::min(pos.GetX(), minpos[0]);
426  minpos[1] = std::min(pos.GetY(), minpos[1]);
427  minpos[2] = std::min(pos.GetZ(), minpos[2]);
428  maxpos[0] = std::max(pos.GetX(), maxpos[0]);
429  maxpos[1] = std::max(pos.GetY(), maxpos[1]);
430  maxpos[2] = std::max(pos.GetZ(), maxpos[2]);
431  }
432 
433  ++i;
434  }
435 
436  return KDTreeCube(minpos[0], maxpos[0], minpos[1], maxpos[1], minpos[2], maxpos[2]);
437 }
438 
439 } // namespace lc_content
440 
441 #endif // LC_KD_TREE_LINKER_TOOLS_TEMPLATED_H
KDTreeCube fill_and_bound_3d_kd_tree_by_index(const std::vector< const T * > &points, std::vector< KDTreeNodeInfoT< unsigned, 3 > > &nodes)
fill_and_bound_3d_kd_tree_by_index
Definition: KDTreeLinkerToolsT.h:368
KDTreeNodeT()
Default constructor.
KDTreeNodeInfoT< DATA, DIM > info
Data.
Definition: KDTreeLinkerToolsT.h:116
KDTreeTesseract fill_and_bound_4d_kd_tree(const pandora::Algorithm *const caller, const MANAGED_CONTAINER< const pandora::CaloHit * > &points, std::vector< KDTreeNodeInfoT< const pandora::CaloHit *, 4 > > &nodes, bool passthru=false)
fill_and_bound_4d_kd_tree
Definition: KDTreeLinkerToolsT.cc:62
KDTree node.
Definition: KDTreeLinkerToolsT.h:93
KDTreeCube fill_and_bound_3d_kd_tree(const MANAGED_CONTAINER< const T * > &points, std::vector< KDTreeNodeInfoT< const T *, 3 > > &nodes)
fill_and_bound_3d_kd_tree
Definition: KDTreeLinkerToolsT.h:333
Data stored in each KDTree node. The dim1/dim2 fields are usually the duplication of some PFRecHit va...
Definition: TrackClusterAssociationAlgorithm.h:19
std::pair< float, float > minmax(const float a, const float b)
minmax
Definition: KDTreeLinkerToolsT.cc:16
KDTreeNodeT< DATA, DIM > * left
Left son.
Definition: KDTreeLinkerToolsT.h:117
static const pandora::CartesianVector & position(const T *const t)
position
KDTreeNodeT< DATA, DIM > * right
Right son.
Definition: KDTreeLinkerToolsT.h:118
KDTreeBoxT< DIM > region
Region bounding box.
Definition: KDTreeLinkerToolsT.h:119
KDTreeNodeInfoT()
Default constructor.
Definition: KDTreeLinkerToolsT.h:268
kdtree_type_adaptor
Definition: KDTreeLinkerToolsT.h:128
Box structure used to define 2D field. It&#39;s used in KDTree building step to divide the detector space...
Definition: KDTreeLinkerToolsT.h:35
KDTreeCube build_3d_kd_search_region(const pandora::CaloHit *const point, const float x_span, const float y_span, const float z_span)
build_3d_kd_search_region
Definition: KDTreeLinkerToolsT.cc:103
void setAttributs(const KDTreeBoxT< DIM > &regionBox, const KDTreeNodeInfoT< DATA, DIM > &infoToStore)
setAttributs
Definition: KDTreeLinkerToolsT.h:296
KDTreeTesseract build_4d_kd_search_region(const pandora::CaloHit *const point, const float x_span, const float y_span, const float z_span, const float search_layer)
build_4d_kd_search_region
Definition: KDTreeLinkerToolsT.cc:116