LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 { class Algorithm; }
22 
23 //------------------------------------------------------------------------------------------------------------------------------------------
24 
25 namespace lar_content
26 {
27 
32 template<unsigned DIM>
34 {
35 public:
39  KDTreeBoxT();
40 
46  template<typename... Ts>
47  KDTreeBoxT(Ts... dimargs);
48 
49  std::array<float, DIM> dimmin;
50  std::array<float, DIM> dimmax;
51 };
52 
55 
56 //------------------------------------------------------------------------------------------------------------------------------------------
57 
62 template<typename DATA, unsigned DIM>
63 class KDTreeNodeInfoT
64 {
65 public:
70 
77  template<typename... Ts>
78  KDTreeNodeInfoT(const DATA &d, Ts... dimargs);
79 
80  DATA data;
81  std::array<float, DIM> dims;
82 };
83 
84 //------------------------------------------------------------------------------------------------------------------------------------------
85 
89 template <typename DATA, unsigned DIM>
91 {
92 public:
96  KDTreeNodeT();
97 
104  void setAttributs(const KDTreeBoxT<DIM> &regionBox, const KDTreeNodeInfoT<DATA, DIM> &infoToStore);
105 
111  void setAttributs(const KDTreeBoxT<DIM> &regionBox);
112 
117 };
118 
119 //------------------------------------------------------------------------------------------------------------------------------------------
120 
124 template<typename T>
126 {
127 public:
135  static const pandora::CartesianVector &position(const T *const t);
136 };
137 
138 //------------------------------------------------------------------------------------------------------------------------------------------
139 
148 std::pair<float,float> minmax(const float a, const float b);
149 
158 template<typename T>
159 KDTreeBox fill_and_bound_2d_kd_tree(const MANAGED_CONTAINER<const T*> &points, std::vector<KDTreeNodeInfoT<const T*, 2> > &nodes);
160 
169 template<typename T>
170 KDTreeCube fill_and_bound_3d_kd_tree(const MANAGED_CONTAINER<const T*> &points, std::vector<KDTreeNodeInfoT<const T*, 3> > &nodes);
171 
181 KDTreeBox build_2d_kd_search_region(const pandora::CaloHit *const point, const float x_span, const float z_span);
182 
192 KDTreeBox build_2d_kd_search_region(const pandora::CartesianVector &pos, const float x_span, const float z_span);
193 
204 KDTreeCube build_3d_kd_search_region(const pandora::CaloHit *const point, const float x_span, const float y_span, const float z_span);
205 
216 KDTreeCube build_3d_kd_search_region(const pandora::CartesianVector &pos, const float x_span, const float y_span, const float z_span);
217 
218 //------------------------------------------------------------------------------------------------------------------------------------------
219 //------------------------------------------------------------------------------------------------------------------------------------------
220 
221 template<unsigned DIM>
223 {
224 }
225 
226 //------------------------------------------------------------------------------------------------------------------------------------------
227 
228 template<unsigned DIM>
229 template<typename... Ts>
230 inline KDTreeBoxT<DIM>::KDTreeBoxT(Ts... dimargs)
231 {
232  static_assert(sizeof...(dimargs) == 2 * DIM, "Constructor requires 2*DIM args");
233  std::vector<float> dims = {dimargs...};
234 
235  for (unsigned i = 0; i < DIM; ++i)
236  {
237  dimmin[i] = dims[2 * i];
238  dimmax[i] = dims[2 * i + 1];
239  }
240 }
241 
242 //------------------------------------------------------------------------------------------------------------------------------------------
243 //------------------------------------------------------------------------------------------------------------------------------------------
244 
245 template<typename DATA, unsigned DIM>
247  data()
248 {
249 }
250 
251 //------------------------------------------------------------------------------------------------------------------------------------------
252 
253 template<typename DATA, unsigned DIM>
254 template<typename... Ts>
255 inline KDTreeNodeInfoT<DATA, DIM>::KDTreeNodeInfoT(const DATA &d, Ts... dimargs) :
256  data(d),
257  dims{ {dimargs...} }
258 {
259 }
260 
261 //------------------------------------------------------------------------------------------------------------------------------------------
262 //------------------------------------------------------------------------------------------------------------------------------------------
263 
264 template <typename DATA, unsigned DIM>
266  left(nullptr),
267  right(nullptr)
268 {
269 }
270 
271 //------------------------------------------------------------------------------------------------------------------------------------------
272 
273 template <typename DATA, unsigned DIM>
274 inline void KDTreeNodeT<DATA, DIM>::setAttributs(const KDTreeBoxT<DIM> &regionBox, const KDTreeNodeInfoT<DATA, DIM> &infoToStore)
275 {
276  info = infoToStore;
277  region = regionBox;
278 }
279 
280 //------------------------------------------------------------------------------------------------------------------------------------------
281 
282 template <typename DATA, unsigned DIM>
284 {
285  region = regionBox;
286 }
287 
288 //------------------------------------------------------------------------------------------------------------------------------------------
289 
290 template<typename T>
291 inline const pandora::CartesianVector &kdtree_type_adaptor<T>::position(const T *const t)
292 {
293  return t->GetPosition();
294 }
295 
296 template<>
297 inline const pandora::CartesianVector &kdtree_type_adaptor<const pandora::CaloHit>::position(const pandora::CaloHit *const t)
298 {
299  return t->GetPositionVector();
300 }
301 
302 template<>
303 inline const pandora::CartesianVector &kdtree_type_adaptor<const pandora::CartesianVector>::position(const pandora::CartesianVector *const t)
304 {
305  return *t;
306 }
307 
308 //------------------------------------------------------------------------------------------------------------------------------------------
309 
310 template<typename T>
311 KDTreeBox fill_and_bound_2d_kd_tree(const MANAGED_CONTAINER<const T*> &points, std::vector<KDTreeNodeInfoT<const T*, 2> > &nodes)
312 {
313  std::array<float, 2> minpos{ {0.f, 0.f} }, maxpos{ {0.f, 0.f} };
314 
315  unsigned i = 0;
316 
317  for (const T *const point : points)
318  {
319  const pandora::CartesianVector &pos = kdtree_type_adaptor<const T>::position(point);
320  nodes.emplace_back(point, pos.GetX(), pos.GetZ());
321 
322  if (0 == i)
323  {
324  minpos[0] = pos.GetX(); minpos[1] = pos.GetZ();
325  maxpos[0] = pos.GetX(); maxpos[1] = pos.GetZ();
326  }
327  else
328  {
329  minpos[0] = std::min(pos.GetX(), minpos[0]);
330  minpos[1] = std::min(pos.GetZ(), minpos[1]);
331  maxpos[0] = std::max(pos.GetX(), maxpos[0]);
332  maxpos[1] = std::max(pos.GetZ(), maxpos[1]);
333  }
334 
335  ++i;
336  }
337 
338  return KDTreeBox(minpos[0], maxpos[0], minpos[1], maxpos[1]);
339 }
340 
341 //------------------------------------------------------------------------------------------------------------------------------------------
342 
343 template<typename T>
344 KDTreeCube fill_and_bound_3d_kd_tree(const MANAGED_CONTAINER<const T*> &points, std::vector<KDTreeNodeInfoT<const T*, 3> > &nodes)
345 {
346  std::array<float, 3> minpos{ {0.f, 0.f, 0.f} }, maxpos{ {0.f, 0.f, 0.f} };
347 
348  unsigned i = 0;
349 
350  for (const T *const point : points)
351  {
352  const pandora::CartesianVector &pos = kdtree_type_adaptor<const T>::position(point);
353  nodes.emplace_back(point, pos.GetX(), pos.GetY(), pos.GetZ());
354 
355  if (0 == i)
356  {
357  minpos[0] = pos.GetX(); minpos[1] = pos.GetY(); minpos[2] = pos.GetZ();
358  maxpos[0] = pos.GetX(); maxpos[1] = pos.GetY(); maxpos[2] = pos.GetZ();
359  }
360  else
361  {
362  minpos[0] = std::min(pos.GetX(), minpos[0]);
363  minpos[1] = std::min(pos.GetY(), minpos[1]);
364  minpos[2] = std::min(pos.GetZ(), minpos[2]);
365  maxpos[0] = std::max(pos.GetX(), maxpos[0]);
366  maxpos[1] = std::max(pos.GetY(), maxpos[1]);
367  maxpos[2] = std::max(pos.GetZ(), maxpos[2]);
368  }
369 
370  ++i;
371  }
372 
373  return KDTreeCube(minpos[0], maxpos[0], minpos[1], maxpos[1], minpos[2], maxpos[2]);
374 }
375 
376 } // namespace lar_content
377 
378 #endif // LAR_KD_TREE_LINKER_TOOLS_TEMPLATED_H
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
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:112
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
#define nodes
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:265
KDTreeNodeT< DATA, DIM > * left
Left son.
Int_t max
Definition: plot.C:27
Float_t d
Definition: plot.C:237
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:104
std::array< float, DIM > dimmin
Int_t min
Definition: plot.C:26
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
KDTreeBoxT< 2 > KDTreeBox