LArOverlapMatrix.h
Go to the documentation of this file.
1 /**
2  * @file larpandoracontent/LArObjects/LArOverlapMatrix.h
3  *
4  * @brief Header file for the lar overlap matrix class.
5  *
6  * $Log: $
7  */
8 #ifndef LAR_OVERLAP_MATRIX_H
9 #define LAR_OVERLAP_MATRIX_H 1
10 
11 #include "Pandora/PandoraInternal.h"
12 
13 #include <unordered_map>
14 #include <vector>
15 
16 namespace lar_content
17 {
18 
19 /**
20  * @brief OverlapMatrix class
21  */
22 template <typename T>
24 {
25 public:
26  typedef T OverlapResult;
27 
28  /**
29  * @brief Element class
30  */
31  class Element
32  {
33  public:
34  /**
35  * @brief Constructor
36  *
37  * @param pCluster1 the address of cluster 1
38  * @param pCluster2 the address of cluster 2
39  * @param overlapResult the overlap result
40  */
41  Element(const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2, const OverlapResult &overlapResult);
42 
43  /**
44  * @brief Get the address of cluster 1
45  *
46  * @return address of cluster 1
47  */
48  const pandora::Cluster *GetCluster1() const;
49 
50  /**
51  * @brief Get the address of cluster 2
52  *
53  * @return address of cluster 2
54  */
55  const pandora::Cluster *GetCluster2() const;
56 
57  /**
58  * @brief Get the overlap result
59  *
60  * @return the overlap result
61  */
62  const OverlapResult &GetOverlapResult() const;
63 
64  /**
65  * @brief Element less than operator
66  *
67  * @param rhs the element for comparison
68  */
69  bool operator<(const Element &rhs) const;
70 
71  private:
72  const pandora::Cluster *m_pCluster1; ///< The address of cluster 1
73  const pandora::Cluster *m_pCluster2; ///< The address of cluster 2
74  OverlapResult m_overlapResult; ///< The overlap result
75  };
76 
77  typedef std::vector<Element> ElementList;
78 
79  /**
80  * @brief Get unambiguous elements
81  *
82  * @param ignoreUnavailable whether to ignore unavailable clusters
83  * @param elementList to receive the unambiguous element list
84  */
85  void GetUnambiguousElements(const bool ignoreUnavailable, ElementList &elementList) const;
86 
87  /**
88  * @brief Default ambiguity function, checking that only one cluster from view 1 and view 2 is found
89  *
90  * @param clusterList1 cluster list 1
91  * @param clusterList2 cluster list 2
92  * @param pCluster1 to receive the address of the unambiguous cluster 1
93  * @param pCluster2 to receive the address of the unambiguous cluster 2
94  *
95  * @return boolean
96  */
97  bool DefaultAmbiguityFunction(const pandora::ClusterList &clusterList1, const pandora::ClusterList &clusterList2,
98  const pandora::Cluster *&pCluster1, const pandora::Cluster *&pCluster2) const;
99 
100  /**
101  * @brief Get the number of connections for a specified cluster
102  *
103  * @param pCluster address of a cluster
104  * @param ignoreUnavailable whether to ignore unavailable clusters
105  * @param n1 to receive the number of view 1 connections
106  * @param n2 to receive the number of view 2 connections
107  */
108  void GetNConnections(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, unsigned int &n1, unsigned int &n2) const;
109 
110  /**
111  * @brief Get a list of elements connected to a specified cluster
112  *
113  * @param pCluster address of a cluster
114  * @param ignoreUnavailable whether to ignore unavailable clusters
115  * @param elementList to receive the connected element list
116  */
117  void GetConnectedElements(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, ElementList &elementList) const;
118 
119  /**
120  * @brief Get a list of elements connected to a specified cluster
121  *
122  * @param pCluster address of a cluster
123  * @param ignoreUnavailable whether to ignore unavailable clusters
124  * @param elementList to receive the connected element list
125  * @param n1 to receive the number of view 1 connections
126  * @param n2 to receive the number of view 2 connections
127  */
128  void GetConnectedElements(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, ElementList &elementList,
129  unsigned int &n1, unsigned int &n2) const;
130 
131  typedef std::unordered_map<const pandora::Cluster *, pandora::ClusterList> ClusterNavigationMap;
132  typedef std::unordered_map<const pandora::Cluster *, OverlapResult> OverlapList;
133  typedef std::unordered_map<const pandora::Cluster *, OverlapList> TheMatrix;
134 
136 
137  /**
138  * @brief Returns an iterator referring to the first element in the overlap matrix
139  */
140  const_iterator begin() const;
141 
142  /**
143  * @brief Returns an iterator referring to the past-the-end element in the overlap matrix
144  */
145  const_iterator end() const;
146 
147  /**
148  * @brief Get a sorted vector of key clusters (view 1 clusters with current implementation)
149  *
150  * @param sortedKeyClusters to receive the sorted vector of key clusters
151  */
152  void GetSortedKeyClusters(pandora::ClusterVector &sortedKeyClusters) const;
153 
154  /**
155  * @brief Get the overlap result for a specified pair of clusters
156  *
157  * @param pCluster1 address of cluster 1
158  * @param pCluster2 address of cluster 2
159  *
160  * @return the address of the overlap result
161  */
162  const OverlapResult &GetOverlapResult(const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2) const;
163 
164  /**
165  * @brief Get the overlap list for a specified cluster
166  *
167  * @param pCluster1 address of cluster 1
168  *
169  * @return the cluster overlap list
170  */
171  const OverlapList &GetOverlapList(const pandora::Cluster *const pCluster1) const;
172 
173  /**
174  * @brief Get the cluster navigation map 1->2
175  *
176  * @return the cluster navigation map 1->2
177  */
178  const ClusterNavigationMap &GetClusterNavigationMap12() const;
179 
180  /**
181  * @brief Get the cluster navigation map 2->1
182  *
183  * @return the cluster navigation map 2->1
184  */
185  const ClusterNavigationMap &GetClusterNavigationMap21() const;
186 
187  /**
188  * @brief Set overlap result
189  *
190  * @param pCluster1 address of cluster 1
191  * @param pCluster2 address of cluster 2
192  * @param overlapResult the overlap result
193  */
194  void SetOverlapResult(const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2, const OverlapResult &overlapResult);
195 
196  /**
197  * @brief SetReplace an existing overlap result
198  *
199  * @param pCluster1 address of cluster 1
200  * @param pCluster2 address of cluster 2
201  * @param overlapResult the overlap result
202  */
203  void ReplaceOverlapResult(const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2, const OverlapResult &overlapResult);
204 
205  /**
206  * @brief Remove entries from matrix corresponding to specified cluster
207  *
208  * @param pCluster address of the cluster
209  */
210  void RemoveCluster(const pandora::Cluster *const pCluster);
211 
212  /**
213  * @brief Clear overlap matrix
214  */
215  void Clear();
216 
217 private:
218  /**
219  * @brief Get elements connected to a specified cluster
220  *
221  * @param pCluster address of the cluster
222  * @param elementList the element list
223  * @param clusterList1 connected view 1 clusters
224  * @param clusterList2 connected view 2 clusters
225  */
226  void GetConnectedElements(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, ElementList &elementList,
227  pandora::ClusterList &clusterList1, pandora::ClusterList &clusterList2) const;
228 
229  /**
230  * @brief Explore connections associated with a given cluster
231  *
232  * @param pCluster address of the cluster
233  * @param clusterList1 connected view 1 clusters
234  * @param clusterList2 connected view 2 clusters
235  */
236  void ExploreConnections(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, pandora::ClusterList &clusterList1,
237  pandora::ClusterList &clusterList2) const;
238 
239  TheMatrix m_overlapMatrix; ///< The overlap matrix
240  ClusterNavigationMap m_clusterNavigationMap12; ///< The cluster navigation map 1->2
241  ClusterNavigationMap m_clusterNavigationMap21; ///< The cluster navigation map 2->1
242 };
243 
244 //------------------------------------------------------------------------------------------------------------------------------------------
245 
246 template <typename T>
247 inline void OverlapMatrix<T>::GetNConnections(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, unsigned int &n1, unsigned int &n2) const
248 {
249  ElementList elementList;
250  this->GetConnectedElements(pCluster, ignoreUnavailable, elementList, n1, n2);
251 }
252 
253 //------------------------------------------------------------------------------------------------------------------------------------------
254 
255 template <typename T>
256 inline void OverlapMatrix<T>::GetConnectedElements(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, ElementList &elementList) const
257 {
258  unsigned int n1(0), n2(0);
259  this->GetConnectedElements(pCluster, ignoreUnavailable, elementList, n1, n2);
260 }
261 
262 //------------------------------------------------------------------------------------------------------------------------------------------
263 
264 template <typename T>
266 {
267  return m_overlapMatrix.begin();
268 }
269 
270 //------------------------------------------------------------------------------------------------------------------------------------------
271 
272 template <typename T>
274 {
275  return m_overlapMatrix.end();
276 }
277 
278 //------------------------------------------------------------------------------------------------------------------------------------------
279 
280 template <typename T>
282  const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2) const
283 {
284  const OverlapList &overlapList(this->GetOverlapList(pCluster1));
285  typename OverlapList::const_iterator iter = overlapList.find(pCluster2);
286 
287  if (overlapList.end() == iter)
288  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
289 
290  return iter->second;
291 }
292 
293 //------------------------------------------------------------------------------------------------------------------------------------------
294 
295 template <typename T>
296 inline const typename OverlapMatrix<T>::OverlapList &OverlapMatrix<T>::GetOverlapList(const pandora::Cluster *const pCluster1) const
297 {
298  typename TheMatrix::const_iterator iter = m_overlapMatrix.find(pCluster1);
299 
300  if (m_overlapMatrix.end() == iter)
301  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
302 
303  return iter->second;
304 }
305 
306 //------------------------------------------------------------------------------------------------------------------------------------------
307 
308 template <typename T>
310 {
312 }
313 
314 //------------------------------------------------------------------------------------------------------------------------------------------
315 
316 template <typename T>
318 {
320 }
321 
322 //------------------------------------------------------------------------------------------------------------------------------------------
323 
324 template <typename T>
326 {
327  m_overlapMatrix.clear();
328  m_clusterNavigationMap12.clear();
329  m_clusterNavigationMap21.clear();
330 }
331 
332 //------------------------------------------------------------------------------------------------------------------------------------------
333 //------------------------------------------------------------------------------------------------------------------------------------------
334 
335 template <typename T>
337  const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2, const OverlapResult &overlapResult) :
338  m_pCluster1(pCluster1),
339  m_pCluster2(pCluster2),
340  m_overlapResult(overlapResult)
341 {
342 }
343 
344 //------------------------------------------------------------------------------------------------------------------------------------------
345 
346 template <typename T>
347 inline const pandora::Cluster *OverlapMatrix<T>::Element::GetCluster1() const
348 {
349  return m_pCluster1;
350 }
351 
352 //------------------------------------------------------------------------------------------------------------------------------------------
353 
354 template <typename T>
355 inline const pandora::Cluster *OverlapMatrix<T>::Element::GetCluster2() const
356 {
357  return m_pCluster2;
358 }
359 
360 //------------------------------------------------------------------------------------------------------------------------------------------
361 
362 template <typename T>
364 {
365  return m_overlapResult;
366 }
367 
368 //------------------------------------------------------------------------------------------------------------------------------------------
369 
370 template <typename T>
372 {
373  if (this == &rhs)
374  return false;
375 
376  return (this->GetOverlapResult() < rhs.GetOverlapResult());
377 }
378 
379 } // namespace lar_content
380 
381 #endif // #ifndef LAR_OVERLAP_MATRIX_H
OverlapResult m_overlapResult
The overlap result.
const pandora::Cluster * m_pCluster1
The address of cluster 1.
void Clear()
Clear overlap matrix.
void RemoveCluster(const pandora::Cluster *const pCluster)
Remove entries from matrix corresponding to specified cluster.
Element(const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2, const OverlapResult &overlapResult)
Constructor.
bool DefaultAmbiguityFunction(const pandora::ClusterList &clusterList1, const pandora::ClusterList &clusterList2, const pandora::Cluster *&pCluster1, const pandora::Cluster *&pCluster2) const
Default ambiguity function, checking that only one cluster from view 1 and view 2 is found...
const OverlapResult & GetOverlapResult() const
Get the overlap result.
const ClusterNavigationMap & GetClusterNavigationMap12() const
Get the cluster navigation map 1->2.
intermediate_table::const_iterator const_iterator
std::vector< Element > ElementList
void GetSortedKeyClusters(pandora::ClusterVector &sortedKeyClusters) const
Get a sorted vector of key clusters (view 1 clusters with current implementation) ...
const OverlapResult & GetOverlapResult(const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2) const
Get the overlap result for a specified pair of clusters.
ClusterNavigationMap m_clusterNavigationMap12
The cluster navigation map 1->2.
const OverlapList & GetOverlapList(const pandora::Cluster *const pCluster1) const
Get the overlap list for a specified cluster.
bool operator<(const Element &rhs) const
Element less than operator.
const ClusterNavigationMap & GetClusterNavigationMap21() const
Get the cluster navigation map 2->1.
std::unordered_map< const pandora::Cluster *, pandora::ClusterList > ClusterNavigationMap
const_iterator begin() const
Returns an iterator referring to the first element in the overlap matrix.
TheMatrix::const_iterator const_iterator
std::unordered_map< const pandora::Cluster *, OverlapResult > OverlapList
void GetUnambiguousElements(const bool ignoreUnavailable, ElementList &elementList) const
Get unambiguous elements.
void SetOverlapResult(const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2, const OverlapResult &overlapResult)
Set overlap result.
TheMatrix m_overlapMatrix
The overlap matrix.
void ExploreConnections(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, pandora::ClusterList &clusterList1, pandora::ClusterList &clusterList2) const
Explore connections associated with a given cluster.
const pandora::Cluster * GetCluster2() const
Get the address of cluster 2.
const_iterator end() const
Returns an iterator referring to the past-the-end element in the overlap matrix.
std::unordered_map< const pandora::Cluster *, OverlapList > TheMatrix
const pandora::Cluster * m_pCluster2
The address of cluster 2.
void GetConnectedElements(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, ElementList &elementList) const
Get a list of elements connected to a specified cluster.
std::vector< art::Ptr< recob::Cluster > > ClusterVector
void ReplaceOverlapResult(const pandora::Cluster *const pCluster1, const pandora::Cluster *const pCluster2, const OverlapResult &overlapResult)
SetReplace an existing overlap result.
OverlapMatrix class.
ClusterNavigationMap m_clusterNavigationMap21
The cluster navigation map 2->1.
const pandora::Cluster * GetCluster1() const
Get the address of cluster 1.
void GetNConnections(const pandora::Cluster *const pCluster, const bool ignoreUnavailable, unsigned int &n1, unsigned int &n2) const
Get the number of connections for a specified cluster.