ECCE @ EIC 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 (Iterator iter = particlemap.begin(); iter != particlemap.end(); ++iter)
27  {
28  delete iter->second;
29  }
30  particlemap.clear();
31 
32  for (VtxIterator iter = vtxmap.begin(); iter != vtxmap.end(); ++iter)
33  {
34  delete iter->second;
35  }
36  vtxmap.clear();
37 
38  for (ShowerIterator iter = showermap.begin(); iter != showermap.end(); ++iter)
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 
50 void PHG4TruthInfoContainer::identify(ostream& os) const
51 {
52  os << "---particlemap--------------------------" << endl;
53  for (ConstIterator iter = particlemap.begin(); iter != particlemap.end(); ++iter)
54  {
55  os << "particle id " << iter->first << endl;
56  (iter->second)->identify();
57  }
58 
59  os << "---vtxmap-------------------------------" << endl;
60  for (ConstVtxIterator vter = vtxmap.begin(); vter != vtxmap.end(); ++vter)
61  {
62  os << "vtx id: " << vter->first << endl;
63  (vter->second)->identify();
64  }
65 
66  os << "---showermap-------------------------------" << endl;
67  for (ConstShowerIterator ster = showermap.begin(); ster != showermap.end(); ++ster)
68  {
69  os << "shower id: " << ster->first << endl;
70  (ster->second)->identify();
71  }
72 
73  os << "---list of embeded track flags-------------------" << endl;
74  for (std::map<int, int>::const_iterator eter = particle_embed_flags.begin();
75  eter != particle_embed_flags.end();
76  ++eter)
77  {
78  os << "embeded track id: " << eter->first
79  << " flag: " << eter->second << endl;
80  }
81 
82  os << "---list of embeded vtx flags-------------------" << endl;
83  for (std::map<int, int>::const_iterator eter = vertex_embed_flags.begin();
84  eter != vertex_embed_flags.end();
85  ++eter)
86  {
87  os << "embeded vertex id: " << eter->first
88  << " flag: " << eter->second << endl;
89  }
90 
91  os << "---primary vertex-------------------" << endl;
92  os << "Vertex " << GetPrimaryVertexIndex() << " is identified as the primary vertex" << endl;
93 
94  return;
95 }
96 
98 PHG4TruthInfoContainer::AddParticle(const int trackid, PHG4Particle* newparticle)
99 {
100  int key = trackid;
102  bool added = false;
103  boost::tie(it, added) = particlemap.insert(std::make_pair(key, newparticle));
104  if (added) return it;
105 
106  cerr << "PHG4TruthInfoContainer::AddParticle"
107  << " - Attempt to add particle with existing trackid "
108  << trackid << ": " << newparticle->get_name() << " id "
109  << newparticle->get_track_id()
110  << ", p = [" << newparticle->get_px()
111  << ", " << newparticle->get_py()
112  << ", " << newparticle->get_pz() << "], "
113  << " parent ID " << newparticle->get_parent_id()
114  << std::endl;
115  return particlemap.end();
116 }
117 
119 {
120  int key = trackid;
121  Iterator it = particlemap.find(key);
122  if (it != particlemap.end()) return it->second;
123  return nullptr;
124 }
125 
127 {
128  if (trackid <= 0) return nullptr;
129  Iterator it = particlemap.find(trackid);
130  if (it != particlemap.end()) return it->second;
131  return nullptr;
132 }
133 
135 {
136  int key = vtxid;
137  VtxIterator it = vtxmap.find(key);
138  if (it != vtxmap.end()) return it->second;
139  return nullptr;
140 }
141 
143 {
144  if (vtxid <= 0) return nullptr;
145  VtxIterator it = vtxmap.find(vtxid);
146  if (it != vtxmap.end()) return it->second;
147  return nullptr;
148 }
149 
151 {
152  int key = showerid;
153  ShowerIterator it = showermap.find(key);
154  if (it != showermap.end()) return it->second;
155  return nullptr;
156 }
157 
159 {
160  if (showerid <= 0) return nullptr;
161  ShowerIterator it = showermap.find(showerid);
162  if (it != showermap.end()) return it->second;
163  return nullptr;
164 }
165 
168 {
169  int key = id;
171  bool added = false;
172 
173  if (vtxmap.find(id) != vtxmap.end())
174  {
175  cout << "trying to add existing vtx " << id
176  << " vtx pos: " << endl;
177  (vtxmap.find(id)->second)->identify();
178  identify();
179  }
180 
181  boost::tie(it, added) = vtxmap.insert(std::make_pair(key, newvtx));
182  if (added)
183  {
184  newvtx->set_id(key);
185  return it;
186  }
187 
188  cerr << "PHG4TruthInfoContainer::AddVertex"
189  << " - Attempt to add vertex with existing id " << id << std::endl;
190  return vtxmap.end();
191 }
192 
195 {
196  int key = id;
198  bool added = false;
199 
200  if (showermap.find(id) != showermap.end())
201  {
202  cout << "trying to add existing shower " << id
203  << " shower pos: " << endl;
204  (showermap.find(id)->second)->identify();
205  identify();
206  }
207 
208  boost::tie(it, added) = showermap.insert(std::make_pair(key, newshower));
209  if (added)
210  {
211  newshower->set_id(key);
212  return it;
213  }
214 
215  cerr << "PHG4TruthInfoContainer::AddShower"
216  << " - Attempt to add shower with existing id " << id << std::endl;
217  return showermap.end();
218 }
219 
221 {
222  int key = 0;
223  if (!particlemap.empty()) key = particlemap.rbegin()->first;
224  if (key < 0) key = 0;
225  return key;
226 }
227 
229 {
230  int key = 0;
231  if (!particlemap.empty()) key = particlemap.begin()->first;
232  if (key > 0) key = 0;
233  return key;
234 }
235 
237 {
238  int key = 0;
239  if (!vtxmap.empty()) key = vtxmap.rbegin()->first;
240  if (key < 0) key = 0;
241  return key;
242 }
243 
245 {
246  int key = 0;
247  if (!vtxmap.empty()) key = vtxmap.begin()->first;
248  if (key > 0) key = 0;
249  return key;
250 }
251 
253 {
254  int key = 0;
255  if (!showermap.empty()) key = showermap.rbegin()->first;
256  if (key < 0) key = 0;
257  return key;
258 }
259 
261 {
262  int key = 0;
263  if (!showermap.empty()) key = showermap.begin()->first;
264  if (key > 0) key = 0;
265  return key;
266 }
267 
269 {
270  delete piter->second;
271  particlemap.erase(piter);
272  return;
273 }
274 
276 {
277  Iterator it = particlemap.find(trackid);
278  if (it != particlemap.end())
279  delete_particle(it);
280 }
281 
283 {
284  delete viter->second;
285  vtxmap.erase(viter);
286  return;
287 }
288 
290 {
291  VtxIterator it = vtxmap.find(vtxid);
292  if (it != vtxmap.end())
293  delete_vtx(it);
294 }
295 
297 {
298  delete siter->second;
299  showermap.erase(siter);
300  return;
301 }
302 
303 int PHG4TruthInfoContainer::isEmbeded(const int trackid) const
304 {
305  std::map<int, int>::const_iterator iter = particle_embed_flags.find(trackid);
306  if (iter == particle_embed_flags.end())
307  {
308  return 0;
309  }
310 
311  return iter->second;
312 }
313 
314 int PHG4TruthInfoContainer::isEmbededVtx(const int vtxid) const
315 {
316  std::map<int, int>::const_iterator iter = vertex_embed_flags.find(vtxid);
317  if (iter == vertex_embed_flags.end())
318  {
319  return 0;
320  }
321 
322  return iter->second;
323 }
324 
326 {
327  return (v->get_id() > 0);
328 }
329 
331 {
332  return (p->get_track_id() > 0);
333 }
334 
336 {
338 
339  int highest_embedding_ID = numeric_limits<int>::min();
340  int vtx_id_for_highest_embedding_ID = 0;
341  for (auto iter = vrange.first; iter != vrange.second; ++iter)
342  {
343  // cout <<"PHG4TruthInfoContainer::GetPrimaryVertexIndex - vertex ID "<<iter->first<<" embedding ID "<< isEmbededVtx(iter->first) <<": ";
344  // iter->second->identify();
345  const int embedding_ID = isEmbededVtx(iter->first);
346 
347  if (embedding_ID >= highest_embedding_ID)
348  {
349  highest_embedding_ID = embedding_ID;
350  vtx_id_for_highest_embedding_ID = iter->first;
351  }
352  }
353 
354  if (highest_embedding_ID == numeric_limits<int>::min())
355  {
356  cout << "PHG4TruthInfoContainer::GetPrimaryVertexIndex - "
357  << "WARNING: no valid primary vertex. Return an invalid ID of 0"
358  << endl;
359  return 0;
360  }
361 
362  return vtx_id_for_highest_embedding_ID;
363 }
364 
365 bool operator==(const PHG4TruthInfoContainer::Map::value_type& lhs, const PHG4TruthInfoContainer::Map::value_type& rhs)
366 {
367  return *lhs.second == *rhs.second;
368 }
369 
370 bool operator==(const PHG4TruthInfoContainer::VtxMap::value_type& lhs, const PHG4TruthInfoContainer::VtxMap::value_type& rhs)
371 {
372  return *lhs.second == *rhs.second;
373 }
374 
375 bool operator==(const PHG4TruthInfoContainer::ShowerMap::value_type& lhs, const PHG4TruthInfoContainer::ShowerMap::value_type& rhs)
376 {
377  return *lhs.second == *rhs.second;
378 }