Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrkrClusterContainerv4.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrkrClusterContainerv4.cc
1 
8 #include "TrkrCluster.h"
9 #include "TrkrDefs.h"
10 
11 #include <algorithm>
12 
13 namespace
14 {
15  TrkrClusterContainer::Map dummy_map;
16 }
17 
18 //_________________________________________________________________
20 {
21  // delete all clusters
22  for (auto&& [key, clus_vector] : m_clusmap)
23  {
24  for (auto&& cluster : clus_vector)
25  {
26  delete cluster;
27  }
28  }
29 
30  // clear the maps
31  /* using swap ensures that the memory is properly de-allocated */
32  {
33  std::map<TrkrDefs::hitsetkey, Vector> empty;
34  m_clusmap.swap(empty);
35  }
36 
37  // also clear temporary map
38  {
39  Map empty;
40  m_tmpmap.swap(empty);
41  }
42 }
43 
44 //_________________________________________________________________
45 void TrkrClusterContainerv4::identify(std::ostream& os) const
46 {
47  os << "-----TrkrClusterContainerv4-----" << std::endl;
48  os << "Number of clusters: " << size() << std::endl;
49 
50  for (const auto& [hitsetkey, clus_vector] : m_clusmap)
51  {
52  const unsigned int layer = TrkrDefs::getLayer(hitsetkey);
53  os << "layer: " << layer << " hitsetkey: " << hitsetkey << std::endl;
54 
55  for (const auto& cluster : clus_vector)
56  {
57  if (cluster)
58  {
59  cluster->identify(os);
60  }
61  }
62  }
63 
64  os << "------------------------------" << std::endl;
65 }
66 
67 //_________________________________________________________________
69 {
70  // get hitset key from cluster
72 
73  // find relevant cluster map if any and remove corresponding cluster
74  auto iter = m_clusmap.find(hitsetkey);
75  if (iter != m_clusmap.end())
76  {
77  // local reference to the vector
78  auto& clus_vector = iter->second;
79 
80  // cluster index in vector
81  const auto index = TrkrDefs::getClusIndex(key);
82 
83  // compare to vector size
84  if (index < clus_vector.size())
85  {
86  // delete corresponding element and set to null
87  delete clus_vector[index];
88  clus_vector[index] = nullptr;
89  }
90  }
91 }
92 
93 //_________________________________________________________________
95 {
96  // get hitsetkey from cluster
98 
99  // find relevant vector or create one if not found
100  auto& clus_vector = m_clusmap[hitsetkey];
101 
102  // get cluster index in vector
103  const auto index = TrkrDefs::getClusIndex(key);
104 
105  // compare index to vector size
106  if (index < clus_vector.size())
107  {
108  /*
109  * if index is already contained in vector, check corresponding element
110  * and assign newclus if null
111  * print error message and exit otherwise
112  */
113  if (!clus_vector[index])
114  {
115  clus_vector[index] = newclus;
116  }
117  else
118  {
119  std::cout << "TrkrClusterContainerv4::AddClusterSpecifyKey: duplicate key: " << key << " exiting now" << std::endl;
120  exit(1);
121  }
122  }
123  else if (index == clus_vector.size())
124  {
125  // if index matches the vector size, just push back the new cluster
126  clus_vector.push_back(newclus);
127  }
128  else
129  {
130  // if index exceeds the vector size, resize cluster to the right size with nullptr, and assign
131  clus_vector.resize(index + 1, nullptr);
132  clus_vector[index] = newclus;
133  }
134 }
135 
138 {
139  std::cout << "deprecated function in TrkrClusterContainerv4, user getClusters(TrkrDefs:hitsetkey)"
140  << std::endl;
141  return std::make_pair(dummy_map.begin(), dummy_map.begin());
142 }
143 
144 //_________________________________________________________________
147 {
148  // clear temporary map
149  {
150  Map empty;
151  m_tmpmap.swap(empty);
152  }
153 
154  // find relevant vector
155  const auto iter = m_clusmap.find(hitsetkey);
156  if (iter != m_clusmap.end())
157  {
158  // copy content in temporary map
159  const auto& clusters = iter->second;
160  for (size_t index = 0; index < clusters.size(); ++index)
161  {
162  const auto& cluster = clusters[index];
163  if (cluster)
164  {
165  // generate cluster key from hitset and index
166  const auto ckey = TrkrDefs::genClusKey(hitsetkey, index);
167 
168  // insert in map
169  m_tmpmap.insert(m_tmpmap.end(), std::make_pair(ckey, cluster));
170  }
171  }
172  }
173 
174  // return temporary map range
175  return std::make_pair(m_tmpmap.cbegin(), m_tmpmap.cend());
176 }
177 
178 //_________________________________________________________________
180 {
181  // get hitsetkey from cluster
183 
184  const auto map_iter = m_clusmap.find(hitsetkey);
185  if (map_iter != m_clusmap.end())
186  {
187  // local reference to vector
188  const auto& clus_vector = map_iter->second;
189 
190  // get cluster position in vector
191  const auto index = TrkrDefs::getClusIndex(key);
192 
193  // compare to vector size
194  if (index < clus_vector.size())
195  {
196  return clus_vector[index];
197  }
198  else
199  {
200  return nullptr;
201  }
202  }
203  else
204  {
205  return nullptr;
206  }
207 }
208 
209 //_________________________________________________________________
211 {
213  out.reserve(m_clusmap.size());
215  m_clusmap.begin(), m_clusmap.end(), std::back_inserter(out),
216  [](const std::pair<TrkrDefs::hitsetkey, Vector>& pair)
217  { return pair.first; });
218  return out;
219 }
220 
221 //_________________________________________________________________
223 {
224  /* copy the logic from TrkrHitSetContainerv1::getHitSets */
225  const TrkrDefs::hitsetkey keylo = TrkrDefs::getHitSetKeyLo(trackerid);
226  const TrkrDefs::hitsetkey keyhi = TrkrDefs::getHitSetKeyHi(trackerid);
227 
228  // get relevant range in map
229  const auto begin = m_clusmap.lower_bound(keylo);
230  const auto end = m_clusmap.upper_bound(keyhi);
231 
232  // transform to a vector
234  out.reserve(m_clusmap.size());
236  begin, end, std::back_inserter(out),
237  [](const std::pair<TrkrDefs::hitsetkey, Vector>& pair)
238  { return pair.first; });
239  return out;
240 }
241 
242 //_________________________________________________________________
244 {
245  /* copy the logic from TrkrHitSetContainerv1::getHitSets */
246  TrkrDefs::hitsetkey keylo = TrkrDefs::getHitSetKeyLo(trackerid, layer);
247  TrkrDefs::hitsetkey keyhi = TrkrDefs::getHitSetKeyHi(trackerid, layer);
248 
249  // get relevant range in map
250  const auto begin = m_clusmap.lower_bound(keylo);
251  const auto end = m_clusmap.upper_bound(keyhi);
252 
253  // transform to a vector
255  out.reserve(m_clusmap.size());
257  begin, end, std::back_inserter(out),
258  [](const std::pair<TrkrDefs::hitsetkey, Vector>& pair)
259  { return pair.first; });
260  return out;
261 }
262 
263 //_________________________________________________________________
264 unsigned int TrkrClusterContainerv4::size() const
265 {
266  unsigned int size = 0;
267  for (const auto& [hitsetkey, clus_vector] : m_clusmap)
268  {
269  size += std::count_if(clus_vector.begin(), clus_vector.end(), [](TrkrCluster* cluster)
270  { return cluster; });
271  }
272  return size;
273 }