Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHCosmicTrackMerger.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHCosmicTrackMerger.cc
1 
2 #include "PHCosmicTrackMerger.h"
3 
6 #include <phool/getClass.h>
7 
11 
13 namespace
14 {
15  template <class T>
16  inline constexpr T square(T &x)
17  {
18  return x * x;
19  }
20  template <class T>
21  inline constexpr T r(T &x, T &y)
22  {
23  return std::sqrt(square(x) + square(y));
24  }
25 
26 } // namespace
27 //____________________________________________________________________________..
29  : SubsysReco(name)
30 {
31 }
32 
33 //____________________________________________________________________________..
35 {
36 }
37 
38 //____________________________________________________________________________..
40 {
42 }
43 
44 //____________________________________________________________________________..
46 {
47  m_seeds = findNode::getClass<TrackSeedContainer>(topNode, "SvtxTrackSeedContainer");
48  if (!m_seeds)
49  {
50  std::cout << PHWHERE << "No track seed container, cannot continue" << std::endl;
52  }
53 
54  m_siliconSeeds = findNode::getClass<TrackSeedContainer>(topNode, "SiliconTrackSeedContainer");
55  m_tpcSeeds = findNode::getClass<TrackSeedContainer>(topNode, "TpcTrackSeedContainer");
56  if (!m_siliconSeeds or !m_tpcSeeds)
57  {
58  std::cout << PHWHERE << "Missing seed container, exiting." << std::endl;
60  }
61 
62  m_geometry = findNode::getClass<ActsGeometry>(topNode, "ActsGeometry");
63  if (!m_geometry)
64  {
65  std::cout << PHWHERE << "no acts geometry, can't continue" << std::endl;
67  }
68 
69  m_clusterMap = findNode::getClass<TrkrClusterContainer>(topNode, "TRKR_CLUSTER");
70  if (!m_clusterMap)
71  {
72  std::cout << PHWHERE << "no cluster map, can't continue" << std::endl;
74  }
75 
77 }
78 
79 //____________________________________________________________________________..
81 {
82  if (Verbosity() > 3)
83  {
84  std::cout << "Seed container size " << m_seeds->size() << std::endl;
85  }
86  for (auto tr1it = m_seeds->begin(); tr1it != m_seeds->end();
87  ++tr1it)
88  {
89  auto track1 = *tr1it;
90  if (!track1)
91  {
92  continue;
93  }
94  unsigned int tpcid1 = track1->get_tpc_seed_index();
95  unsigned int siid1 = track1->get_silicon_seed_index();
96  auto tpcseed1 = m_tpcSeeds->get(tpcid1);
97  auto silseed1 = m_siliconSeeds->get(siid1);
98 
99  for (auto tr2it = tr1it; tr2it != m_seeds->end();
100  ++tr2it)
101  {
103  TrackFitUtils::position_vector_t tr1_rz_pts, tr1_xy_pts;
104  auto globTr1 = getGlobalPositions(tpcseed1);
105  for (auto &pos : globTr1.second)
106  {
107  float clusr = r(pos.x(), pos.y());
108  if (pos.y() < 0) clusr *= -1;
109  tr1_rz_pts.push_back(std::make_pair(pos.z(), clusr));
110  tr1_xy_pts.push_back(std::make_pair(pos.x(), pos.y()));
111  }
112 
113  auto xyTr1Params = TrackFitUtils::line_fit(tr1_xy_pts);
114  auto rzTr1Params = TrackFitUtils::line_fit(tr1_rz_pts);
115  float tr1xyslope = std::get<0>(xyTr1Params);
116  float tr1rzslope = std::get<0>(rzTr1Params);
119  if (fabs(tr1rzslope) < 0.005)
120  {
121  m_seeds->erase(m_seeds->index(tr1it));
122  break;
123  }
124  if (tr1it == tr2it)
125  {
126  continue;
127  }
128 
129  auto track2 = *tr2it;
130  if (!track2)
131  {
132  continue;
133  }
134 
135  unsigned int tpcid2 = track2->get_tpc_seed_index();
136  unsigned int siid2 = track2->get_silicon_seed_index();
137  auto tpcseed2 = m_tpcSeeds->get(tpcid2);
138  auto silseed2 = m_siliconSeeds->get(siid2);
139 
140  TrackFitUtils::position_vector_t tr2_rz_pts, tr2_xy_pts;
141  auto globTr2 = getGlobalPositions(tpcseed2);
142 
143  for (auto &pos : globTr2.second)
144  {
145  float clusr = r(pos.x(), pos.y());
146  if (pos.y() < 0) clusr *= -1;
147  tr2_rz_pts.push_back(std::make_pair(pos.z(), clusr));
148  tr2_xy_pts.push_back(std::make_pair(pos.x(), pos.y()));
149  }
150 
151  auto xyTr2Params = TrackFitUtils::line_fit(tr2_xy_pts);
152  auto rzTr2Params = TrackFitUtils::line_fit(tr2_rz_pts);
153  // float tr2xyint = std::get<1>(xyTr2Params);
154  float tr2xyslope = std::get<0>(xyTr2Params);
155  // float tr2rzint = std::get<1>(rzTr2Params);
156  float tr2rzslope = std::get<0>(rzTr2Params);
157 
158  std::vector<TrkrDefs::cluskey> ckeyUnion;
159  std::set_intersection(globTr1.first.begin(), globTr1.first.end(),
160  globTr2.first.begin(), globTr2.first.end(), std::back_inserter(ckeyUnion));
161 
162  if (
164  (ckeyUnion.size() > 10) or
166  (fabs(tr1xyslope - tr2xyslope) < 0.5 &&
168  fabs(tr1rzslope - tr2rzslope * -1) < 0.5))
169  {
170  if (Verbosity() > 3)
171  {
172  std::cout << "Combining tr" << m_seeds->index(tr1it) << " with sil/tpc " << tpcid1
173  << ", " << siid1 << " with tr "
174  << m_seeds->index(tr2it) << " with sil/tpc " << tpcid2 << ", "
175  << siid2 << " with slopes "
176  << tr1xyslope << ", " << tr2xyslope << ", "
177  << tr1rzslope << ", " << tr2rzslope << " and ckey union "
178  << ckeyUnion.size() << std::endl;
179  }
180 
181  for (auto &key : globTr2.first)
182  {
183  globTr1.first.push_back(key);
184  }
185  for (auto &pos : globTr2.second)
186  {
187  globTr1.second.push_back(pos);
188  }
189 
190  addKeys(tpcseed1, tpcseed2);
191  if (silseed1 && silseed2)
192  {
193  addKeys(silseed1, silseed2);
194  }
196  m_seeds->erase(m_seeds->index(tr2it));
197  }
198  }
199 
202  removeOutliers(tpcseed1);
203  }
204  if (Verbosity() > 3)
205  {
206  std::cout << "Seed container size to finish " << m_seeds->size() << std::endl;
207  for (auto &seed : *m_seeds)
208  {
209  if (seed)
210  {
211  seed->identify();
212  std::cout << std::endl;
213  unsigned int tpcid1 = seed->get_tpc_seed_index();
214  unsigned int siid1 = seed->get_silicon_seed_index();
215  auto tpcseed1 = m_tpcSeeds->get(tpcid1);
216  auto silseed1 = m_siliconSeeds->get(siid1);
217  tpcseed1->identify();
218  if (silseed1)
219  {
220  silseed1->identify();
221  }
222  }
223  else
224  {
225  std::cout << "nullptr seed was removed" << std::endl;
226  }
227  }
228  }
230 }
231 
233 {
234  TrackFitUtils::position_vector_t tr_rz_pts, tr_xy_pts;
235  auto glob = getGlobalPositions(seed);
236  for (const auto &pos : glob.second)
237  {
238  float clusr = r(pos.x(), pos.y());
239  if (pos.y() < 0) clusr *= -1;
240  // skip tpot clusters, as they are always bad in 1D due to 1D resolution
241  if (fabs(clusr) > 80.) continue;
242  tr_rz_pts.push_back(std::make_pair(pos.z(), clusr));
243  tr_xy_pts.push_back(std::make_pair(pos.x(), pos.y()));
244  }
245 
246  auto xyParams = TrackFitUtils::line_fit(tr_xy_pts);
247  auto rzParams = TrackFitUtils::line_fit(tr_rz_pts);
248 
249  for (int i = 0; i < glob.first.size(); i++)
250  {
251  auto &pos = glob.second[i];
252  float clusr = r(pos.x(), pos.y());
253  if (pos.y() < 0) clusr *= -1;
254  // skip tpot clusters, as they are always bad in 1D due to 1D resolution
255  if (fabs(clusr) > 80.) continue;
256  float perpxyslope = -1. / std::get<0>(xyParams);
257  float perpxyint = pos.y() - perpxyslope * pos.x();
258  float perprzslope = -1. / std::get<0>(rzParams);
259  float perprzint = clusr - perprzslope * pos.z();
260 
261  float pcax = (perpxyint - std::get<1>(xyParams)) / (std::get<0>(xyParams) - perpxyslope);
262  float pcay = std::get<0>(xyParams) * pcax + std::get<1>(xyParams);
263 
264  float pcaz = (perprzint - std::get<1>(rzParams)) / (std::get<0>(rzParams) - perprzslope);
265  float pcar = std::get<0>(rzParams) * pcaz + std::get<1>(rzParams);
266  float dcax = pcax - pos.x();
267  float dcay = pcay - pos.y();
268  float dcar = pcar - clusr;
269  float dcaz = pcaz - pos.z();
270  float dcaxy = std::sqrt(square(dcax) + square(dcay));
271  float dcarz = std::sqrt(square(dcar) + square(dcaz));
272  if (dcaxy > 1. || dcarz > 1.)
273  {
274  seed->erase_cluster_key(glob.first[i]);
275  }
276  }
277 }
279 {
280  for (auto it = toAdd->begin_cluster_keys(); it != toAdd->end_cluster_keys();
281  ++it)
282  {
283  if (Verbosity() > 3)
284  {
285  auto clus = m_clusterMap->findCluster(*it);
286  auto glob = m_geometry->getGlobalPosition(*it, clus);
287  std::cout << "adding " << *it << " with pos " << glob.transpose() << std::endl;
288  }
289  toAddTo->insert_cluster_key(*it);
290  }
291 }
294 {
295  KeyPosMap glob;
296  std::vector<TrkrDefs::cluskey> ckeys;
297  std::vector<Acts::Vector3> positions;
298  for (auto it = seed->begin_cluster_keys(); it != seed->end_cluster_keys();
299  ++it)
300  {
301  auto key = *it;
302  auto clus = m_clusterMap->findCluster(key);
303  ckeys.push_back(key);
304  positions.push_back(m_geometry->getGlobalPosition(key, clus));
305  }
306  glob.first = ckeys;
307  glob.second = positions;
308  return glob;
309 }
310 //____________________________________________________________________________..
312 {
314 }