Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4TruthInfoContainer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4TruthInfoContainer.cc
2 
3 #include "PHG4Particle.h"
4 #include "PHG4Shower.h"
5 #include "PHG4VtxPoint.h"
6 
7 #include <boost/tuple/tuple.hpp>
8 
9 #include <limits>
10 #include <string>
11 
12 using namespace std;
13 
15  : particlemap()
16  , vtxmap()
17  , particle_embed_flags()
18  , vertex_embed_flags()
19 {
20 }
21 
23 
25 {
26  for (auto& iter : particlemap)
27  {
28  delete iter.second;
29  }
30  particlemap.clear();
31 
32  for (auto& iter : vtxmap)
33  {
34  delete iter.second;
35  }
36  vtxmap.clear();
37 
38  for (auto& iter : showermap)
39  {
40  delete iter.second;
41  }
42  showermap.clear();
43 
44  particle_embed_flags.clear();
45  vertex_embed_flags.clear();
46 
47  return;
48 }
49 
51 {
52  os << "---particlemap--------------------------" << endl;
53  for (auto iter : particlemap)
54  {
55  os << "particle id " << iter.first << endl;
56  (iter.second)->identify();
57  }
58 
59  os << "---vtxmap-------------------------------" << endl;
60  for (auto vter : vtxmap)
61  {
62  os << "vtx id: " << vter.first << endl;
63  (vter.second)->identify();
64  }
65 
66  os << "---showermap-------------------------------" << endl;
67  for (auto ster : showermap)
68  {
69  os << "shower id: " << ster.first << endl;
70  (ster.second)->identify();
71  }
72 
73  os << "---list of embeded track flags-------------------" << endl;
74  for (auto particle_embed_flag : particle_embed_flags)
75  {
76  os << "embeded track id: " << particle_embed_flag.first
77  << " flag: " << particle_embed_flag.second << endl;
78  }
79 
80  os << "---list of embeded vtx flags-------------------" << endl;
81  for (auto vertex_embed_flag : vertex_embed_flags)
82  {
83  os << "embeded vertex id: " << vertex_embed_flag.first
84  << " flag: " << vertex_embed_flag.second << endl;
85  }
86 
87  os << "---primary vertex-------------------" << endl;
88  os << "Vertex " << GetPrimaryVertexIndex() << " is identified as the primary vertex" << endl;
89 
90  return;
91 }
92 
94 PHG4TruthInfoContainer::AddParticle(const int trackid, PHG4Particle* newparticle)
95 {
96  int key = trackid;
98  bool added = false;
99  boost::tie(it, added) = particlemap.insert(std::make_pair(key, newparticle));
100  if (added)
101  {
102  return it;
103  }
104 
105  cerr << "PHG4TruthInfoContainer::AddParticle"
106  << " - Attempt to add particle with existing trackid "
107  << trackid << ": " << newparticle->get_name() << " id "
108  << newparticle->get_track_id()
109  << ", p = [" << newparticle->get_px()
110  << ", " << newparticle->get_py()
111  << ", " << newparticle->get_pz() << "], "
112  << " parent ID " << newparticle->get_parent_id()
113  << std::endl;
114  return particlemap.end();
115 }
116 
118 {
119  int key = trackid;
120  Iterator it = particlemap.find(key);
121  if (it != particlemap.end())
122  {
123  return it->second;
124  }
125  return nullptr;
126 }
127 
129 {
130  if (trackid <= 0)
131  {
132  return nullptr;
133  }
134  Iterator it = particlemap.find(trackid);
135  if (it != particlemap.end())
136  {
137  return it->second;
138  }
139  return nullptr;
140 }
141 
143 {
144  int key = vtxid;
145  VtxIterator it = vtxmap.find(key);
146  if (it != vtxmap.end())
147  {
148  return it->second;
149  }
150  return nullptr;
151 }
152 
154 {
155  if (vtxid <= 0)
156  {
157  return nullptr;
158  }
159  VtxIterator it = vtxmap.find(vtxid);
160  if (it != vtxmap.end())
161  {
162  return it->second;
163  }
164  return nullptr;
165 }
166 
168 {
169  int key = showerid;
170  ShowerIterator it = showermap.find(key);
171  if (it != showermap.end())
172  {
173  return it->second;
174  }
175  return nullptr;
176 }
177 
179 {
180  if (showerid <= 0)
181  {
182  return nullptr;
183  }
184  ShowerIterator it = showermap.find(showerid);
185  if (it != showermap.end())
186  {
187  return it->second;
188  }
189  return nullptr;
190 }
191 
194 {
195  int key = id;
197  bool added = false;
198 
199  if (vtxmap.find(id) != vtxmap.end())
200  {
201  cout << "trying to add existing vtx " << id
202  << " vtx pos: " << endl;
203  (vtxmap.find(id)->second)->identify();
204  identify();
205  }
206 
207  boost::tie(it, added) = vtxmap.insert(std::make_pair(key, newvtx));
208  if (added)
209  {
210  newvtx->set_id(key);
211  return it;
212  }
213 
214  cerr << "PHG4TruthInfoContainer::AddVertex"
215  << " - Attempt to add vertex with existing id " << id << std::endl;
216  return vtxmap.end();
217 }
218 
221 {
222  int key = id;
224  bool added = false;
225 
226  if (showermap.find(id) != showermap.end())
227  {
228  cout << "trying to add existing shower " << id
229  << " shower pos: " << endl;
230  (showermap.find(id)->second)->identify();
231  identify();
232  }
233 
234  boost::tie(it, added) = showermap.insert(std::make_pair(key, newshower));
235  if (added)
236  {
237  newshower->set_id(key);
238  return it;
239  }
240 
241  cerr << "PHG4TruthInfoContainer::AddShower"
242  << " - Attempt to add shower with existing id " << id << std::endl;
243  return showermap.end();
244 }
245 
247 {
248  int key = 0;
249  if (!particlemap.empty())
250  {
251  key = particlemap.rbegin()->first;
252  }
253  if (key < 0)
254  {
255  key = 0;
256  }
257  return key;
258 }
259 
261 {
262  int key = 0;
263  if (!particlemap.empty())
264  {
265  key = particlemap.begin()->first;
266  }
267  if (key > 0)
268  {
269  key = 0;
270  }
271  return key;
272 }
273 
275 {
276  int key = 0;
277  if (!vtxmap.empty())
278  {
279  key = vtxmap.rbegin()->first;
280  }
281  if (key < 0)
282  {
283  key = 0;
284  }
285  return key;
286 }
287 
289 {
290  int key = 0;
291  if (!vtxmap.empty())
292  {
293  key = vtxmap.begin()->first;
294  }
295  if (key > 0)
296  {
297  key = 0;
298  }
299  return key;
300 }
301 
303 {
304  int key = 0;
305  if (!showermap.empty())
306  {
307  key = showermap.rbegin()->first;
308  }
309  if (key < 0)
310  {
311  key = 0;
312  }
313  return key;
314 }
315 
317 {
318  int key = 0;
319  if (!showermap.empty())
320  {
321  key = showermap.begin()->first;
322  }
323  if (key > 0)
324  {
325  key = 0;
326  }
327  return key;
328 }
329 
331 {
332  delete piter->second;
333  particlemap.erase(piter);
334  return;
335 }
336 
338 {
339  Iterator it = particlemap.find(trackid);
340  if (it != particlemap.end())
341  {
342  delete_particle(it);
343  }
344 }
345 
347 {
348  delete viter->second;
349  vtxmap.erase(viter);
350  return;
351 }
352 
354 {
355  VtxIterator it = vtxmap.find(vtxid);
356  if (it != vtxmap.end())
357  {
358  delete_vtx(it);
359  }
360 }
361 
363 {
364  delete siter->second;
365  showermap.erase(siter);
366  return;
367 }
368 
369 int PHG4TruthInfoContainer::isEmbeded(const int trackid) const
370 {
371  std::map<int, int>::const_iterator iter = particle_embed_flags.find(trackid);
372  if (iter == particle_embed_flags.end())
373  {
374  return 0;
375  }
376 
377  return iter->second;
378 }
379 
380 int PHG4TruthInfoContainer::isEmbededVtx(const int vtxid) const
381 {
382  std::map<int, int>::const_iterator iter = vertex_embed_flags.find(vtxid);
383  if (iter == vertex_embed_flags.end())
384  {
385  return 0;
386  }
387 
388  return iter->second;
389 }
390 
392 {
393  return (v->get_id() > 0);
394 }
395 
397 {
398  return (p->get_track_id() > 0);
399 }
400 
402 {
404 
405  int highest_embedding_ID = numeric_limits<int>::min();
406  int vtx_id_for_highest_embedding_ID = 0;
407  for (auto iter = vrange.first; iter != vrange.second; ++iter)
408  {
409  // cout <<"PHG4TruthInfoContainer::GetPrimaryVertexIndex - vertex ID "<<iter->first<<" embedding ID "<< isEmbededVtx(iter->first) <<": ";
410  // iter->second->identify();
411  const int embedding_ID = isEmbededVtx(iter->first);
412 
413  if (embedding_ID >= highest_embedding_ID)
414  {
415  highest_embedding_ID = embedding_ID;
416  vtx_id_for_highest_embedding_ID = iter->first;
417  }
418  }
419 
420  if (highest_embedding_ID == numeric_limits<int>::min())
421  {
422  cout << "PHG4TruthInfoContainer::GetPrimaryVertexIndex - "
423  << "WARNING: no valid primary vertex. Return an invalid ID of 0"
424  << endl;
425  return 0;
426  }
427 
428  return vtx_id_for_highest_embedding_ID;
429 }
430 
432 {
433  return *lhs.second == *rhs.second;
434 }
435 
437 {
438  return *lhs.second == *rhs.second;
439 }
440 
442 {
443  return *lhs.second == *rhs.second;
444 }