LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
KDTreeLinkerToolsT.h
Go to the documentation of this file.
1 
8 #ifndef LAR_KD_TREE_LINKER_TOOLS_TEMPLATED_H
9 #define LAR_KD_TREE_LINKER_TOOLS_TEMPLATED_H 1
10 
11 #include "Api/PandoraContentApi.h"
12 
13 #include "Objects/CaloHit.h"
14 #include "Objects/CartesianVector.h"
15 
16 #include "Pandora/PandoraInternal.h"
17 
18 #include <array>
19 #include <vector>
20 
21 namespace pandora
22 {
23 class Algorithm;
24 }
25 
26 //------------------------------------------------------------------------------------------------------------------------------------------
27 
28 namespace lar_content
29 {
30 
35 template <unsigned DIM>
37 {
38 public:
42  KDTreeBoxT();
43 
49  template <typename... Ts>
50  KDTreeBoxT(Ts... dimargs);
51 
52  std::array<float, DIM> dimmin;
53  std::array<float, DIM> dimmax;
54 };
55 
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 KDTreeBox fill_and_bound_2d_kd_tree(const MANAGED_CONTAINER<const T *> &points, std::vector<KDTreeNodeInfoT<const T *, 2>> &nodes);
163 
172 template <typename T>
173 KDTreeCube fill_and_bound_3d_kd_tree(const MANAGED_CONTAINER<const T *> &points, std::vector<KDTreeNodeInfoT<const T *, 3>> &nodes);
174 
184 KDTreeBox build_2d_kd_search_region(const pandora::CaloHit *const point, const float x_span, const float z_span);
185 
195 KDTreeBox build_2d_kd_search_region(const pandora::CartesianVector &pos, const float x_span, const float z_span);
196 
207 KDTreeCube build_3d_kd_search_region(const pandora::CaloHit *const point, const float x_span, const float y_span, const float z_span);
208 
219 KDTreeCube build_3d_kd_search_region(const pandora::CartesianVector &pos, const float x_span, const float y_span, const float z_span);
220 
221 //------------------------------------------------------------------------------------------------------------------------------------------
222 //------------------------------------------------------------------------------------------------------------------------------------------
223 
224 template <unsigned DIM>
226 {
227 }
228 
229 //------------------------------------------------------------------------------------------------------------------------------------------
230 
231 template <unsigned DIM>
232 template <typename... Ts>
233 inline KDTreeBoxT<DIM>::KDTreeBoxT(Ts... dimargs)
234 {
235  static_assert(sizeof...(dimargs) == 2 * DIM, "Constructor requires 2*DIM args");
236  std::vector<float> dims = {dimargs...};
237 
238  for (unsigned i = 0; i < DIM; ++i)
239  {
240  dimmin[i] = dims[2 * i];
241  dimmax[i] = dims[2 * i + 1];
242  }
243 }
244 
245 //------------------------------------------------------------------------------------------------------------------------------------------
246 //------------------------------------------------------------------------------------------------------------------------------------------
247 
248 template <typename DATA, unsigned DIM>
250  data()
251 {
252 }
253 
254 //------------------------------------------------------------------------------------------------------------------------------------------
255 
256 template <typename DATA, unsigned DIM>
257 template <typename... Ts>
258 inline KDTreeNodeInfoT<DATA, DIM>::KDTreeNodeInfoT(const DATA &d, Ts... dimargs) :
259  data(d),
260  dims{{dimargs...}}
261 {
262 }
263 
264 //------------------------------------------------------------------------------------------------------------------------------------------
265 //------------------------------------------------------------------------------------------------------------------------------------------
266 
267 template <typename DATA, unsigned DIM>
269  left(nullptr),
270  right(nullptr)
271 {
272 }
273 
274 //------------------------------------------------------------------------------------------------------------------------------------------
275 
276 template <typename DATA, unsigned DIM>
277 inline void KDTreeNodeT<DATA, DIM>::setAttributs(const KDTreeBoxT<DIM> &regionBox, const KDTreeNodeInfoT<DATA, DIM> &infoToStore)
278 {
279  info = infoToStore;
280  region = regionBox;
281 }
282 
283 //------------------------------------------------------------------------------------------------------------------------------------------
284 
285 template <typename DATA, unsigned DIM>
287 {
288  region = regionBox;
289 }
290 
291 //------------------------------------------------------------------------------------------------------------------------------------------
292 
293 template <typename T>
294 inline const pandora::CartesianVector &kdtree_type_adaptor<T>::position(const T *const t)
295 {
296  return t->GetPosition();
297 }
298 
299 template <>
300 inline const pandora::CartesianVector &kdtree_type_adaptor<const pandora::CaloHit>::position(const pandora::CaloHit *const t)
301 {
302  return t->GetPositionVector();
303 }
304 
305 template <>
306 inline const pandora::CartesianVector &kdtree_type_adaptor<const pandora::CartesianVector>::position(const pandora::CartesianVector *const t)
307 {
308  return *t;
309 }
310 
311 //------------------------------------------------------------------------------------------------------------------------------------------
312 
313 template <typename T>
314 KDTreeBox fill_and_bound_2d_kd_tree(const MANAGED_CONTAINER<const T *> &points, std::vector<KDTreeNodeInfoT<const T *, 2>> &nodes)
315 {
316  std::array<float, 2> minpos{{0.f, 0.f}}, maxpos{{0.f, 0.f}};
317 
318  unsigned i = 0;
319 
320  for (const T *const point : points)
321  {
322  const pandora::CartesianVector &pos = kdtree_type_adaptor<const T>::position(point);
323  nodes.emplace_back(point, pos.GetX(), pos.GetZ());
324 
325  if (0 == i)
326  {
327  minpos[0] = pos.GetX();
328  minpos[1] = pos.GetZ();
329  maxpos[0] = pos.GetX();
330  maxpos[1] = pos.GetZ();
331  }
332  else
333  {
334  minpos[0] = std::min(pos.GetX(), minpos[0]);
335  minpos[1] = std::min(pos.GetZ(), minpos[1]);
336  maxpos[0] = std::max(pos.GetX(), maxpos[0]);
337  maxpos[1] = std::max(pos.GetZ(), maxpos[1]);
338  }
339 
340  ++i;
341  }
342 
343  return KDTreeBox(minpos[0], maxpos[0], minpos[1], maxpos[1]);
344 }
345 
346 //------------------------------------------------------------------------------------------------------------------------------------------
347 
348 template <typename T>
349 KDTreeCube fill_and_bound_3d_kd_tree(const MANAGED_CONTAINER<const T *> &points, std::vector<KDTreeNodeInfoT<const T *, 3>> &nodes)
350 {
351  std::array<float, 3> minpos{{0.f, 0.f, 0.f}}, maxpos{{0.f, 0.f, 0.f}};
352 
353  unsigned i = 0;
354 
355  for (const T *const point : points)
356  {
357  const pandora::CartesianVector &pos = kdtree_type_adaptor<const T>::position(point);
358  nodes.emplace_back(point, pos.GetX(), pos.GetY(), pos.GetZ());
359 
360  if (0 == i)
361  {
362  minpos[0] = pos.GetX();
363  minpos[1] = pos.GetY();
364  minpos[2] = pos.GetZ();
365  maxpos[0] = pos.GetX();
366  maxpos[1] = pos.GetY();
367  maxpos[2] = pos.GetZ();
368  }
369  else
370  {
371  minpos[0] = std::min(pos.GetX(), minpos[0]);
372  minpos[1] = std::min(pos.GetY(), minpos[1]);
373  minpos[2] = std::min(pos.GetZ(), minpos[2]);
374  maxpos[0] = std::max(pos.GetX(), maxpos[0]);
375  maxpos[1] = std::max(pos.GetY(), maxpos[1]);
376  maxpos[2] = std::max(pos.GetZ(), maxpos[2]);
377  }
378 
379  ++i;
380  }
381 
382  return KDTreeCube(minpos[0], maxpos[0], minpos[1], maxpos[1], minpos[2], maxpos[2]);
383 }
384 
385 } // namespace lar_content
386 
387 #endif // LAR_KD_TREE_LINKER_TOOLS_TEMPLATED_H
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
std::pair< float, float > minmax(const float a, const float b)
minmax
static const pandora::CartesianVector & position(const T *const t)
position
void setAttributs(const KDTreeBoxT< DIM > &regionBox, const KDTreeNodeInfoT< DATA, DIM > &infoToStore)
setAttributs
Box structure used to define 2D field. It&#39;s used in KDTree building step to divide the detector space...
KDTreeBoxT< DIM > region
Region bounding box.
KDTreeNodeInfoT< DATA, DIM > info
Data.
std::array< float, DIM > dims
KDTreeNodeT< DATA, DIM > * right
Right son.
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
Data stored in each KDTree node. The dim1/dim2 fields are usually the duplication of some PFRecHit va...
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
KDTreeNodeT< DATA, DIM > * left
Left son.
Float_t d
Definition: plot.C:235
KDTreeBoxT< 3 > KDTreeCube
KDTreeNodeInfoT()
Default constructor.
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
std::array< float, DIM > dimmin
KDTreeNodeT()
Default constructor.
KDTreeBox fill_and_bound_2d_kd_tree(const MANAGED_CONTAINER< const T * > &points, std::vector< KDTreeNodeInfoT< const T *, 2 >> &nodes)
fill_and_bound_2d_kd_tree
std::array< float, DIM > dimmax
KDTreeBox build_2d_kd_search_region(const pandora::CaloHit *const point, const float x_span, const float z_span)
build_2d_kd_search_region
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
KDTreeBoxT< 2 > KDTreeBox