Belle II Software  release-05-02-19
Clusterizer.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2012 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/ca/AutomatonCell.h>
13 
14 #include <tracking/trackFindingCDC/utilities/VectorRange.h>
15 #include <tracking/trackFindingCDC/utilities/WeightedRelation.h>
16 
17 #include <tracking/trackFindingCDC/findlets/base/Findlet.h>
18 
19 #include <framework/logging/Logger.h>
20 
21 namespace Belle2 {
26  namespace TrackFindingCDC {
27 
41  template <class ACellHolder, class ACluster = std::vector<ACellHolder*> >
42  class Clusterizer
43  : public Findlet<ACellHolder* const, WeightedRelation<ACellHolder> const, ACluster> {
44 
45  public:
56  void apply(std::vector<ACellHolder*> const& cellHolders,
57  std::vector<WeightedRelation<ACellHolder> > const& cellHolderRelations,
58  std::vector<ACluster>& clusters) override
59  {
60  // Expect the relations to be sorted for lookup
61  assert(std::is_sorted(cellHolderRelations.begin(),
62  cellHolderRelations.end()));
63 
64  // Prepare some output clusters
65  clusters.reserve(30);
66 
67  // Prepare states
68  for (ACellHolder* cellHolder : cellHolders) {
69  setCellState(cellHolder, -1);
70  }
71 
72  // Work horse cluster
73  std::vector<ACellHolder*> cluster;
74 
75  // Go through each cell holder and start a cluster on each that is unassigned yet
76  int iCluster = -1;
77  for (ACellHolder* cellHolder : cellHolders) {
78  if (getCellState(cellHolder) != -1) continue;
79 
80  cluster.clear();
81 
82  ++iCluster;
83  setCellState(cellHolder, iCluster);
84  cluster.push_back(cellHolder);
85 
86  expandCluster(cellHolderRelations, cluster);
87 
88  clusters.emplace_back(std::move(cluster));
89  cluster.clear();
90  }
91  }
92 
93  private:
95  void expandCluster(std::vector<WeightedRelation<ACellHolder>> const& cellHolderRelations,
96  std::vector<ACellHolder*>& cluster) const
97  {
98  ACellHolder* seedCellHolder = cluster.front();
99  int iCluster = getCellState(seedCellHolder);
100 
101  // Grow the cluster iterativelly
102  std::vector<ACellHolder*> checkNow;
103  std::vector<ACellHolder*> checkNext;
104 
105  checkNow.reserve(10);
106  checkNext.reserve(10);
107 
108  checkNext.push_back(seedCellHolder);
109 
110  while (not checkNext.empty()) {
111 
112  checkNow.swap(checkNext);
113  checkNext.clear();
114 
115  for (ACellHolder* cellHolder : checkNow) {
116 
118  std::equal_range(cellHolderRelations.begin(),
119  cellHolderRelations.end(),
120  cellHolder));
121 
122  // Setting the cell weight to the number of neighbors
123  size_t nNeighbors = neighborRelations.size();
124  setCellWeight(cellHolder, nNeighbors);
125 
126  for (const WeightedRelation<ACellHolder>& neighborRelation : neighborRelations) {
127  ACellHolder* neighborCellHolder = neighborRelation.getTo();
128 
129  Weight neighborICluster = getCellState(neighborCellHolder);
130  if (neighborICluster == -1) {
131  // Neighbor not yet in cluster
132  setCellState(neighborCellHolder, iCluster);
133  cluster.push_back(neighborCellHolder);
134 
135  // Register neighbor for further expansion
136  checkNext.push_back(neighborCellHolder);
137  continue;
138  }
139 
140  if (neighborICluster != iCluster) {
141  B2WARNING("Clusterizer: Neighboring item was already assigned to different "
142  "cluster. Check if the neighborhood is symmetric.");
143  continue;
144  }
145  }
146  }
147  }
148  }
149 
151  void setCellState(ACellHolder* cellHolder, Weight cellState) const
152  {
153  AutomatonCell& automatonCell = cellHolder->getAutomatonCell();
154  automatonCell.setCellState(cellState);
155  }
156 
158  Weight getCellState(ACellHolder* cellHolder) const
159  {
160  const AutomatonCell& automatonCell = cellHolder->getAutomatonCell();
161  return automatonCell.getCellState();
162  }
163 
165  void setCellWeight(ACellHolder* cellHolder, Weight cellWeight) const
166  {
167  AutomatonCell& automatonCell = cellHolder->getAutomatonCell();
168  automatonCell.setCellWeight(cellWeight);
169  }
170 
171  };
172  }
174 }
Belle2::TrackFindingCDC::Clusterizer::setCellWeight
void setCellWeight(ACellHolder *cellHolder, Weight cellWeight) const
Setter for the cell weight of a pointed object that holds an AutomatonCell.
Definition: Clusterizer.h:173
Belle2::TrackFindingCDC::Range::size
std::size_t size() const
Returns the total number of objects in this range.
Definition: Range.h:86
Belle2::TrackFindingCDC::Clusterizer::apply
void apply(std::vector< ACellHolder * > const &cellHolders, std::vector< WeightedRelation< ACellHolder > > const &cellHolderRelations, std::vector< ACluster > &clusters) override
Creates the clusters.
Definition: Clusterizer.h:64
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::AutomatonCell
Cell used by the cellular automata.
Definition: AutomatonCell.h:39
Belle2::TrackFindingCDC::Range
A pair of iterators usable with the range base for loop.
Definition: Range.h:35
Belle2::TrackFindingCDC::Clusterizer::setCellState
void setCellState(ACellHolder *cellHolder, Weight cellState) const
Setter for the cell state of a pointed object that holds an AutomatonCell.
Definition: Clusterizer.h:159
Belle2::TrackFindingCDC::WeightedRelation
Type for two related objects with a weight.
Definition: CDCSegment2D.h:36
Belle2::TrackFindingCDC::AutomatonCell::setCellWeight
void setCellWeight(Weight weight)
Setter for the cell weight.
Definition: AutomatonCell.h:132
Belle2::TrackFindingCDC::Clusterizer::getCellState
Weight getCellState(ACellHolder *cellHolder) const
Getter for the cell state of a pointed object that holds an AutomatonCell.
Definition: Clusterizer.h:166
Belle2::TrackFindingCDC::Clusterizer::expandCluster
void expandCluster(std::vector< WeightedRelation< ACellHolder >> const &cellHolderRelations, std::vector< ACellHolder * > &cluster) const
Helper function. Starting a new cluster and iterativelly expands it.
Definition: Clusterizer.h:103
Belle2::TrackFindingCDC::AutomatonCell::getCellState
Weight getCellState() const
Getter for the cell state.
Definition: AutomatonCell.h:106