ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
HGTD::ClusterCollection< T > Class Template Reference

#include <Clustering.h>

Collaboration diagram for HGTD::ClusterCollection< T >:

Public Member Functions

void addCluster (Cluster< T > vx)
 
void doClustering (ClusterAlgo algo)
 
Cluster< T > getMaxEntriesCluster ()
 
int getMaxClusterSize_Info ()
 
const std::vector< Cluster< T > > & getClusters () const
 
int getNClusters () const
 
void updateDistanceCut (double cut_value)
 Set the distance cut. More...
 
void setDebugLevel (int debug_level)
 

Private Member Functions

double getDistanceBetweenClusters (const Cluster< T > &a, const Cluster< T > &b)
 
Cluster< T > mergeClusters (const Cluster< T > &a, const Cluster< T > &b)
 Creates a new Cluster object that is a fusion of the two clusters given in the arguments. More...
 
Cluster< T > mergeClustersMean (const Cluster< T > &a, const Cluster< T > &b)
 
std::pair< Cluster< T >, int > largestClusterInfo ()
 

Private Attributes

int m_debug_level = 0
 
double m_distance_cut = 3.0
 
std::vector< Cluster< T > > m_clusters
 

Detailed Description

template<typename T>
class HGTD::ClusterCollection< T >

Definition at line 123 of file Clustering.h.

Member Function Documentation

◆ addCluster()

template<typename T >
void HGTD::ClusterCollection< T >::addCluster ( Cluster< T >  vx)

Definition at line 274 of file Clustering.h.

274  {
275  m_clusters.push_back(vx);
276 }

◆ doClustering()

template<typename T >
void HGTD::ClusterCollection< T >::doClustering ( ClusterAlgo  algo)

Definition at line 368 of file Clustering.h.

368  {
369  if (algo == ClusterAlgo::Eager) {
370  for (const auto &clust : m_clusters) {
371  if (clust.containsUnknowns()) {
372  throw std::invalid_argument(
373  "[ClusterCollection::doClustering] ERROR "
374  "- eager clustering does not allow for unknown values");
375  }
376  }
377  }
378 
379  // TODO case where I have 2 vertices in my collection
380  if (m_debug_level > 0) {
381  std::cout << "ClusterCollection::doTimeClustering" << std::endl;
382  }
383  double distance = 1.e30; // initial distance value, "far away"
384 
385  while (m_clusters.size() > 1) {
386  int i0 = 0;
387  int j0 = 0;
388  if (m_debug_level > 0) {
389  std::cout << "using " << m_clusters.size() << " vertices" << std::endl;
390  }
391  // find the two vertices that are closest to each other
393  for (size_t i = 0; i < m_clusters.size(); i++) {
394  for (size_t j = i + 1; j < m_clusters.size(); j++) {
395 
396  if (algo == ClusterAlgo::Simultaneous ||
398 
399  if (m_clusters.at(i).mergeStatus() or m_clusters.at(j).mergeStatus()) {
400  continue;
401  }
402  }
403 
404  double current_distance =
406  if (current_distance <= distance) {
407  distance = current_distance;
408  i0 = i;
409  j0 = j;
410  }
411  } // loop over j
412  } // loop over i
413  if (m_debug_level > 0) {
414  std::cout << "using vertex " << i0 << " and " << j0 << std::endl;
415  }
416  // now the closest two vertices are found and will be fused if cut passes
417  if (distance < m_distance_cut && i0 != j0) {
418 
419  Cluster<T> new_cluster { };
420 
421  if (algo == ClusterAlgo::SimultaneousMean) {
422  new_cluster = mergeClustersMean(m_clusters.at(i0), m_clusters.at(j0));
423  } else {
424  new_cluster = mergeClusters(m_clusters.at(i0), m_clusters.at(j0));
425  }
426 
427  if (m_debug_level > 0) {
428  std::cout << "starting to erase" << std::endl;
429  }
430  m_clusters.erase(m_clusters.begin() + j0);
431  if (i0 < j0) {
432  m_clusters.erase(m_clusters.begin() + i0);
433  } else {
434  m_clusters.erase(m_clusters.begin() + (i0 - 1));
435  }
436  if (m_debug_level > 0) {
437  std::cout << "erase done" << std::endl;
438  }
439  m_clusters.push_back(new_cluster);
440  if (m_debug_level > 0) {
441  std::cout << "new cluster stored" << std::endl;
442  }
443  } else {
444  if (algo == ClusterAlgo::Eager) {
445  break;
446  }
447  // if there is a cluster that was merged
448  if (std::find_if(m_clusters.begin(), m_clusters.end(),
449  [](const Cluster<T> &c) { return c.mergeStatus(); }) !=
450  m_clusters.end()) {
451  // reset each status to false
452  std::for_each(m_clusters.begin(), m_clusters.end(), [](Cluster<T> &c) {
453  c.setMergeStatus(false);
454  });
455  } else {
456  // if not, this is the end of the clustering
457  break;
458  }
459  }
460  } // while loop
461 }

◆ getClusters()

template<typename T >
const std::vector< Cluster< T > > & HGTD::ClusterCollection< T >::getClusters

Definition at line 506 of file Clustering.h.

506  {
507  return m_clusters;
508 }

◆ getDistanceBetweenClusters()

template<typename T >
double HGTD::ClusterCollection< T >::getDistanceBetweenClusters ( const Cluster< T > &  a,
const Cluster< T > &  b 
)
private

Definition at line 279 of file Clustering.h.

280  {
281  std::vector<double> a_values = a.getValues();
282  std::vector<double> b_values = b.getValues();
283  std::vector<double> a_sigmas = a.getSigmas();
284  std::vector<double> b_sigmas = b.getSigmas();
285 
286  std::vector<double> distances(a_values.size());
287  for (size_t i = 0; i < a_values.size(); i++) {
288  double distance_i = 0;
289  if (a_sigmas.at(i) >= 0.0 && b_sigmas.at(i) >= 0.0) {
290  distance_i = std::abs(a_values.at(i) - b_values.at(i)) /
291  std::hypot(a_sigmas.at(i), b_sigmas.at(i));
292  }
293  distances.at(i) = distance_i;
294  }
295  double distance2 = 0.;
296  for (double d : distances) {
297  distance2 += d * d;
298  }
299  return std::sqrt(distance2);
300 }

◆ getMaxClusterSize_Info()

template<typename T >
int HGTD::ClusterCollection< T >::getMaxClusterSize_Info

Definition at line 497 of file Clustering.h.

497  {
498  // find the vertex with a maximum amount of hits clustered in it
499  // and return thisn umber
500  const auto [max_cluster, nMaxClusters] = largestClusterInfo();
501 
502  return max_cluster.getNEntries();
503 }

◆ getMaxEntriesCluster()

template<typename T >
Cluster< T > HGTD::ClusterCollection< T >::getMaxEntriesCluster

Definition at line 486 of file Clustering.h.

486  {
487 
488  const auto [max_cluster, nMaxClusters] = largestClusterInfo();
489  // If two or more clusters have the same maximum number of entries, I can't
490  // decide, so return default
491  if (nMaxClusters > 1) {
492  return Cluster<T> { };
493  }
494  return max_cluster;
495 }

◆ getNClusters()

template<typename T >
int HGTD::ClusterCollection< T >::getNClusters

Definition at line 510 of file Clustering.h.

510  {
511  return static_cast<int>(m_clusters.size());
512 }

◆ largestClusterInfo()

template<typename T >
std::pair< Cluster< T >, int > HGTD::ClusterCollection< T >::largestClusterInfo
private

Definition at line 464 of file Clustering.h.

464  {
465 
466  int max_n_hits = 0;
467  int count = 0;
468  Cluster<T> max_n_vertex { };
469 
470  for (const auto& vx : m_clusters) {
471  int current_n = vx.getNEntries();
472 
473  if (current_n > max_n_hits) {
474  max_n_hits = current_n;
475  max_n_vertex = vx;
476  count = 1;
477  }
478  else if (current_n == max_n_hits) {
479  count++;
480  }
481  }
482 
483  return std::make_pair(max_n_vertex, count);
484 }

◆ mergeClusters()

template<class T >
Cluster< T > HGTD::ClusterCollection< T >::mergeClusters ( const Cluster< T > &  a,
const Cluster< T > &  b 
)
private

Creates a new Cluster object that is a fusion of the two clusters given in the arguments.

Parameters
[in]aA cluster that should be merged.
[in]bA cluster that should be merged.

Definition at line 303 of file Clustering.h.

304  {
305  Cluster<T> merged_cluster;
306  merged_cluster.addEntryVector(a.getEntries());
307  merged_cluster.addEntryVector(b.getEntries());
308 
309  std::vector<double> a_values = a.getValues();
310  std::vector<double> b_values = b.getValues();
311  std::vector<double> a_sigmas = a.getSigmas();
312  std::vector<double> b_sigmas = b.getSigmas();
313 
314  std::vector<double> new_cluster_values(a_values.size());
315  std::vector<double> new_cluster_sigmas(a_values.size());
316 
317  for (size_t i = 0; i < a_values.size(); i++) {
318  double value1 = a_values.at(i);
319  double value2 = b_values.at(i);
320  double var1 = std::pow(a_sigmas.at(i), 2.0);
321  double var2 = std::pow(b_sigmas.at(i), 2.0);
322  double new_cluster_value =
323  (value1 / var1 + value2 / var2) / (1.0 / var1 + 1.0 / var2);
324  double new_cluster_sigma = std::sqrt(var1 * var2 / (var1 + var2));
325  new_cluster_values.at(i) = new_cluster_value;
326  new_cluster_sigmas.at(i) = new_cluster_sigma;
327  }
328  int new_merge_iteration = a.getMergeIteration() + b.getMergeIteration() + 1;
329  merged_cluster.setClusterValue(new_cluster_values, new_cluster_sigmas);
330  merged_cluster.setMergeStatus(true);
331  merged_cluster.setMergeIteration(new_merge_iteration);
332  return merged_cluster;
333 }

◆ mergeClustersMean()

template<class T >
Cluster< T > HGTD::ClusterCollection< T >::mergeClustersMean ( const Cluster< T > &  a,
const Cluster< T > &  b 
)
private

Definition at line 336 of file Clustering.h.

337  {
338  Cluster<T> merged_cluster;
339  merged_cluster.addEntryVector(a.getEntries());
340  merged_cluster.addEntryVector(b.getEntries());
341 
342  std::vector<double> a_values = a.getValues();
343  std::vector<double> b_values = b.getValues();
344  std::vector<double> a_sigmas = a.getSigmas();
345  std::vector<double> b_sigmas = b.getSigmas();
346 
347  std::vector<double> new_cluster_values(a_values.size());
348  std::vector<double> new_cluster_sigmas(a_values.size());
349 
350  for (size_t i = 0; i < a_values.size(); i++) {
351  double value1 = a_values.at(i);
352  double value2 = b_values.at(i);
353  double sigma1 = a_sigmas.at(i);
354  double sigma2 = b_sigmas.at(i);
355  double new_cluster_value = (value1 + value2) / 2.;
356  double new_cluster_sigma = std::hypot(sigma1, sigma2);
357  new_cluster_values.at(i) = new_cluster_value;
358  new_cluster_sigmas.at(i) = new_cluster_sigma;
359  }
360  int new_merge_iteration = a.getMergeIteration() + b.getMergeIteration() + 1;
361  merged_cluster.setClusterValue(new_cluster_values, new_cluster_sigmas);
362  merged_cluster.setMergeStatus(true);
363  merged_cluster.setMergeIteration(new_merge_iteration);
364  return merged_cluster;
365 }

◆ setDebugLevel()

template<typename T >
void HGTD::ClusterCollection< T >::setDebugLevel ( int  debug_level)
inline

Definition at line 157 of file Clustering.h.

157 { m_debug_level = debug_level; }

◆ updateDistanceCut()

template<class T >
void HGTD::ClusterCollection< T >::updateDistanceCut ( double  cut_value)

Set the distance cut.

This allows an update and rerunning of the vertexing then no vertex that fulfills the selection criteria is found.

Parameters
[in]cut_valueGiven in units of resolution.

Definition at line 270 of file Clustering.h.

270  {
271  m_distance_cut = cut_value;
272 }

Member Data Documentation

◆ m_clusters

template<typename T >
std::vector<Cluster<T> > HGTD::ClusterCollection< T >::m_clusters
private

Definition at line 162 of file Clustering.h.

◆ m_debug_level

template<typename T >
int HGTD::ClusterCollection< T >::m_debug_level = 0
private

Definition at line 160 of file Clustering.h.

◆ m_distance_cut

template<typename T >
double HGTD::ClusterCollection< T >::m_distance_cut = 3.0
private

Definition at line 161 of file Clustering.h.


The documentation for this class was generated from the following file:
HGTD::ClusterAlgo::Eager
@ Eager
HGTD::ClusterCollection::getDistanceBetweenClusters
double getDistanceBetweenClusters(const Cluster< T > &a, const Cluster< T > &b)
Definition: Clustering.h:279
hist_file_dump.d
d
Definition: hist_file_dump.py:137
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
HGTD::ClusterCollection::largestClusterInfo
std::pair< Cluster< T >, int > largestClusterInfo()
Definition: Clustering.h:464
HGTD::ClusterAlgo::SimultaneousMean
@ SimultaneousMean
HGTD::ClusterCollection::m_debug_level
int m_debug_level
Definition: Clustering.h:160
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
HGTD::ClusterCollection::mergeClusters
Cluster< T > mergeClusters(const Cluster< T > &a, const Cluster< T > &b)
Creates a new Cluster object that is a fusion of the two clusters given in the arguments.
Definition: Clustering.h:303
lumiFormat.i
int i
Definition: lumiFormat.py:85
HGTD::ClusterCollection::m_clusters
std::vector< Cluster< T > > m_clusters
Definition: Clustering.h:162
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
HGTD::ClusterCollection::mergeClustersMean
Cluster< T > mergeClustersMean(const Cluster< T > &a, const Cluster< T > &b)
Definition: Clustering.h:336
a
TList * a
Definition: liststreamerinfos.cxx:10
HGTD::ClusterAlgo::Simultaneous
@ Simultaneous
Amg::distance2
float distance2(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the squared distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:48
HGTD::ClusterCollection::m_distance_cut
double m_distance_cut
Definition: Clustering.h:161
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
python.compressB64.c
def c
Definition: compressB64.py:93