Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:22:02

0001 #include "PHG4TruthInfoContainer.h"
0002 
0003 #include "PHG4Particle.h"
0004 #include "PHG4Shower.h"
0005 #include "PHG4VtxPoint.h"
0006 
0007 #include <algorithm>
0008 #include <boost/tuple/tuple.hpp>
0009 
0010 #include <limits>
0011 #include <string>
0012 
0013 PHG4TruthInfoContainer::~PHG4TruthInfoContainer() { Reset(); }
0014 
0015 void PHG4TruthInfoContainer::Reset()
0016 {
0017   for (auto& iter : particlemap)
0018   {
0019     delete iter.second;
0020   }
0021   particlemap.clear();
0022 
0023   for (auto& iter : sPHENIXprimaryparticlemap)
0024   {
0025     delete iter.second;
0026   }
0027   sPHENIXprimaryparticlemap.clear();
0028 
0029   for (auto& iter : vtxmap)
0030   {
0031     delete iter.second;
0032   }
0033   vtxmap.clear();
0034 
0035   for (auto& iter : showermap)
0036   {
0037     delete iter.second;
0038   }
0039   showermap.clear();
0040 
0041   particle_embed_flags.clear();
0042   vertex_embed_flags.clear();
0043 
0044   return;
0045 }
0046 
0047 void PHG4TruthInfoContainer::identify(std::ostream& os) const
0048 {
0049   os << "---particlemap--------------------------" << std::endl;
0050   for (auto iter : particlemap)
0051   {
0052     os << "particle id " << iter.first << std::endl;
0053     (iter.second)->identify();
0054   }
0055 
0056   os << "---vtxmap-------------------------------" << std::endl;
0057   for (auto vter : vtxmap)
0058   {
0059     os << "vtx id: " << vter.first << std::endl;
0060     (vter.second)->identify();
0061   }
0062 
0063   os << "---showermap-------------------------------" << std::endl;
0064   for (auto ster : showermap)
0065   {
0066     os << "shower id: " << ster.first << std::endl;
0067     (ster.second)->identify();
0068   }
0069 
0070   os << "---list of embeded track flags-------------------" << std::endl;
0071   for (auto particle_embed_flag : particle_embed_flags)
0072   {
0073     os << "embeded track id: " << particle_embed_flag.first
0074        << " flag: " << particle_embed_flag.second << std::endl;
0075   }
0076 
0077   os << "---list of embeded vtx flags-------------------" << std::endl;
0078   for (auto vertex_embed_flag : vertex_embed_flags)
0079   {
0080     os << "embeded vertex id: " << vertex_embed_flag.first
0081        << " flag: " << vertex_embed_flag.second << std::endl;
0082   }
0083 
0084   os << "---primary vertex-------------------" << std::endl;
0085   os << "Vertex " << GetPrimaryVertexIndex() << " is identified as the primary vertex" << std::endl;
0086 
0087   return;
0088 }
0089 
0090 PHG4TruthInfoContainer::ConstIterator
0091 PHG4TruthInfoContainer::AddParticle(const int trackid, PHG4Particle* newparticle)
0092 {
0093   int key = trackid;
0094   ConstIterator it;
0095   bool added = false;
0096   boost::tie(it, added) = particlemap.insert(std::make_pair(key, newparticle));
0097   if (added)
0098   {
0099     return it;
0100   }
0101 
0102   std::cout << "PHG4TruthInfoContainer::AddParticle"
0103             << " - Attempt to add particle with existing trackid "
0104             << trackid << ": " << newparticle->get_name() << " id "
0105             << newparticle->get_track_id()
0106             << ", p = [" << newparticle->get_px()
0107             << ", " << newparticle->get_py()
0108             << ", " << newparticle->get_pz() << "], "
0109             << " parent ID " << newparticle->get_parent_id()
0110             << std::endl;
0111   return particlemap.end();
0112 }
0113 
0114 PHG4TruthInfoContainer::ConstIterator
0115 PHG4TruthInfoContainer::AddsPHENIXPrimaryParticle(const int trackid, PHG4Particle* newparticle)
0116 {
0117   int key = trackid;
0118   ConstIterator it;
0119   bool added = false;
0120 
0121   boost::tie(it, added) = sPHENIXprimaryparticlemap.insert(std::make_pair(key, newparticle));
0122   if (added)
0123   {
0124     return it;
0125   }
0126 
0127   std::cout << "PHG4TruthInfoContainer::AddsPHENIXPrimaryParticle"
0128             << " - Attempt to add sPHENIX primary particle with existing trackid "
0129             << trackid << ": " << newparticle->get_name() << " id "
0130             << newparticle->get_track_id()
0131             << ", p = [" << newparticle->get_px()
0132             << ", " << newparticle->get_py()
0133             << ", " << newparticle->get_pz() << "], "
0134             << " parent ID " << newparticle->get_parent_id()
0135             << std::endl;
0136   return sPHENIXprimaryparticlemap.end();
0137 }
0138 
0139 PHG4Particle* PHG4TruthInfoContainer::GetParticle(const int trackid)
0140 {
0141   int key = trackid;
0142   Iterator it = particlemap.find(key);
0143   if (it != particlemap.end())
0144   {
0145     return it->second;
0146   }
0147   return nullptr;
0148 }
0149 
0150 PHG4Particle* PHG4TruthInfoContainer::GetParticle(const int trackid) const
0151 {
0152   int key = trackid;
0153   ConstIterator it = particlemap.find(key);
0154   if (it != particlemap.end())
0155   {
0156     return it->second;
0157   }
0158   return nullptr;
0159 }
0160 
0161 PHG4Particle* PHG4TruthInfoContainer::GetPrimaryParticle(const int trackid)
0162 {
0163   if (trackid <= 0)
0164   {
0165     return nullptr;
0166   }
0167   Iterator it = particlemap.find(trackid);
0168   if (it != particlemap.end())
0169   {
0170     return it->second;
0171   }
0172   return nullptr;
0173 }
0174 
0175 PHG4Particle* PHG4TruthInfoContainer::GetsPHENIXPrimaryParticle(const int trackid)
0176 {
0177   Iterator it = sPHENIXprimaryparticlemap.find(trackid);
0178   if (it != sPHENIXprimaryparticlemap.end())
0179   {
0180     return it->second;
0181   }
0182   return nullptr;
0183 }
0184 
0185 PHG4VtxPoint* PHG4TruthInfoContainer::GetVtx(const int vtxid)
0186 {
0187   int key = vtxid;
0188   VtxIterator it = vtxmap.find(key);
0189   if (it != vtxmap.end())
0190   {
0191     return it->second;
0192   }
0193   return nullptr;
0194 }
0195 
0196 PHG4VtxPoint* PHG4TruthInfoContainer::GetPrimaryVtx(const int vtxid)
0197 {
0198   if (vtxid <= 0)
0199   {
0200     return nullptr;
0201   }
0202   VtxIterator it = vtxmap.find(vtxid);
0203   if (it != vtxmap.end())
0204   {
0205     return it->second;
0206   }
0207   return nullptr;
0208 }
0209 
0210 PHG4Shower* PHG4TruthInfoContainer::GetShower(const int showerid)
0211 {
0212   int key = showerid;
0213   ShowerIterator it = showermap.find(key);
0214   if (it != showermap.end())
0215   {
0216     return it->second;
0217   }
0218   return nullptr;
0219 }
0220 
0221 PHG4Shower* PHG4TruthInfoContainer::GetPrimaryShower(const int showerid)
0222 {
0223   if (showerid <= 0)
0224   {
0225     return nullptr;
0226   }
0227   ShowerIterator it = showermap.find(showerid);
0228   if (it != showermap.end())
0229   {
0230     return it->second;
0231   }
0232   return nullptr;
0233 }
0234 
0235 PHG4TruthInfoContainer::ConstVtxIterator
0236 PHG4TruthInfoContainer::AddVertex(const int id, PHG4VtxPoint* newvtx)
0237 {
0238   int key = id;
0239   ConstVtxIterator it;
0240   bool added = false;
0241 
0242   if (vtxmap.contains(id))
0243   {
0244     std::cout << "trying to add existing vtx " << id
0245               << " vtx pos: " << std::endl;
0246     (vtxmap.find(id)->second)->identify();
0247     identify();
0248   }
0249 
0250   boost::tie(it, added) = vtxmap.insert(std::make_pair(key, newvtx));
0251   if (added)
0252   {
0253     newvtx->set_id(key);
0254     return it;
0255   }
0256 
0257   std::cout << "PHG4TruthInfoContainer::AddVertex"
0258             << " - Attempt to add vertex with existing id " << id << std::endl;
0259   return vtxmap.end();
0260 }
0261 
0262 PHG4TruthInfoContainer::ConstShowerIterator
0263 PHG4TruthInfoContainer::AddShower(const int id, PHG4Shower* newshower)
0264 {
0265   int key = id;
0266   ConstShowerIterator it;
0267   bool added = false;
0268 
0269   if (showermap.contains(id))
0270   {
0271     std::cout << "trying to add existing shower " << id
0272               << " shower pos: " << std::endl;
0273     (showermap.find(id)->second)->identify();
0274     identify();
0275   }
0276 
0277   boost::tie(it, added) = showermap.insert(std::make_pair(key, newshower));
0278   if (added)
0279   {
0280     newshower->set_id(key);
0281     return it;
0282   }
0283 
0284   std::cout << "PHG4TruthInfoContainer::AddShower"
0285             << " - Attempt to add shower with existing id " << id << std::endl;
0286   return showermap.end();
0287 }
0288 
0289 int PHG4TruthInfoContainer::maxtrkindex() const
0290 {
0291   int key = 0;
0292   if (!particlemap.empty())
0293   {
0294     key = particlemap.rbegin()->first;
0295   }
0296   key = std::max(key, 0);
0297   return key;
0298 }
0299 
0300 int PHG4TruthInfoContainer::mintrkindex() const
0301 {
0302   int key = 0;
0303   if (!particlemap.empty())
0304   {
0305     key = particlemap.begin()->first;
0306   }
0307   key = std::min(key, 0);
0308   return key;
0309 }
0310 
0311 int PHG4TruthInfoContainer::maxvtxindex() const
0312 {
0313   int key = 0;
0314   if (!vtxmap.empty())
0315   {
0316     key = vtxmap.rbegin()->first;
0317   }
0318   key = std::max(key, 0);
0319   return key;
0320 }
0321 
0322 int PHG4TruthInfoContainer::minvtxindex() const
0323 {
0324   int key = 0;
0325   if (!vtxmap.empty())
0326   {
0327     key = vtxmap.begin()->first;
0328   }
0329   key = std::min(key, 0);
0330   return key;
0331 }
0332 
0333 int PHG4TruthInfoContainer::maxshowerindex() const
0334 {
0335   int key = 0;
0336   if (!showermap.empty())
0337   {
0338     key = showermap.rbegin()->first;
0339   }
0340   key = std::max(key, 0);
0341   return key;
0342 }
0343 
0344 int PHG4TruthInfoContainer::minshowerindex() const
0345 {
0346   int key = 0;
0347   if (!showermap.empty())
0348   {
0349     key = showermap.begin()->first;
0350   }
0351   key = std::min(key, 0);
0352   return key;
0353 }
0354 
0355 void PHG4TruthInfoContainer::delete_particle(Iterator piter)
0356 {
0357   delete piter->second;
0358   particlemap.erase(piter);
0359   return;
0360 }
0361 
0362 void PHG4TruthInfoContainer::delete_particle(int trackid)
0363 {
0364   Iterator it = particlemap.find(trackid);
0365   if (it != particlemap.end())
0366   {
0367     delete_particle(it);
0368   }
0369 }
0370 
0371 void PHG4TruthInfoContainer::delete_vtx(VtxIterator viter)
0372 {
0373   delete viter->second;
0374   vtxmap.erase(viter);
0375   return;
0376 }
0377 
0378 void PHG4TruthInfoContainer::delete_vtx(int vtxid)
0379 {
0380   VtxIterator it = vtxmap.find(vtxid);
0381   if (it != vtxmap.end())
0382   {
0383     delete_vtx(it);
0384   }
0385 }
0386 
0387 void PHG4TruthInfoContainer::delete_shower(ShowerIterator siter)
0388 {
0389   delete siter->second;
0390   showermap.erase(siter);
0391   return;
0392 }
0393 
0394 int PHG4TruthInfoContainer::isEmbeded(const int trackid) const
0395 {
0396   // I think here for G4 secondarys we just check their primary's embedding ID
0397   int trackid_embed = trackid;
0398   if (trackid_embed <= 0)
0399   {
0400     const PHG4Particle* p = GetParticle(trackid_embed);
0401     if (p)
0402     {
0403       trackid_embed = p->get_primary_id();
0404     }
0405   }
0406   std::map<int, int>::const_iterator iter = particle_embed_flags.find(trackid_embed);
0407   if (iter == particle_embed_flags.end())
0408   {
0409     return 0;
0410   }
0411 
0412   return iter->second;
0413 }
0414 
0415 int PHG4TruthInfoContainer::isEmbededVtx(const int vtxid) const
0416 {
0417   std::map<int, int>::const_iterator iter = vertex_embed_flags.find(vtxid);
0418   if (iter == vertex_embed_flags.end())
0419   {
0420     return 0;
0421   }
0422 
0423   return iter->second;
0424 }
0425 
0426 bool PHG4TruthInfoContainer::is_primary_vtx(const PHG4VtxPoint* v) const
0427 {
0428   return (v->get_id() > 0);
0429 }
0430 
0431 bool PHG4TruthInfoContainer::is_primary(const PHG4Particle* p) const
0432 {
0433   return (p->get_track_id() > 0);
0434 }
0435 
0436 // this is O(log N)...
0437 bool PHG4TruthInfoContainer::is_sPHENIX_primary(const PHG4Particle* p) const
0438 {
0439   // sPHENIX primary particles are in the sPHENIXprimaryparticlemap
0440   return (sPHENIXprimaryparticlemap.contains(p->get_track_id()));
0441 }
0442 
0443 int PHG4TruthInfoContainer::GetPrimaryVertexIndex() const
0444 {
0445   ConstVtxRange vrange = GetPrimaryVtxRange();
0446 
0447   int highest_embedding_ID = std::numeric_limits<int>::min();
0448   int vtx_id_for_highest_embedding_ID = 0;
0449   for (auto iter = vrange.first; iter != vrange.second; ++iter)
0450   {
0451     //        std::cout <<"PHG4TruthInfoContainer::GetPrimaryVertexIndex - vertex ID "<<iter->first<<" embedding ID "<< isEmbededVtx(iter->first) <<": ";
0452     // iter->second->identify();
0453     const int embedding_ID = isEmbededVtx(iter->first);
0454 
0455     if (embedding_ID >= highest_embedding_ID)
0456     {
0457       highest_embedding_ID = embedding_ID;
0458       vtx_id_for_highest_embedding_ID = iter->first;
0459     }
0460   }
0461 
0462   if (highest_embedding_ID == std::numeric_limits<int>::min())
0463   {
0464     std::cout << "PHG4TruthInfoContainer::GetPrimaryVertexIndex - "
0465               << "WARNING: no valid primary vertex. Return an invalid ID of 0"
0466               << std::endl;
0467     return 0;
0468   }
0469 
0470   return vtx_id_for_highest_embedding_ID;
0471 }
0472 
0473 bool operator==(const PHG4TruthInfoContainer::Map::value_type& lhs, const PHG4TruthInfoContainer::Map::value_type& rhs)
0474 {
0475   return *lhs.second == *rhs.second;
0476 }
0477 
0478 bool operator==(const PHG4TruthInfoContainer::VtxMap::value_type& lhs, const PHG4TruthInfoContainer::VtxMap::value_type& rhs)
0479 {
0480   return *lhs.second == *rhs.second;
0481 }
0482 
0483 bool operator==(const PHG4TruthInfoContainer::ShowerMap::value_type& lhs, const PHG4TruthInfoContainer::ShowerMap::value_type& rhs)
0484 {
0485   return *lhs.second == *rhs.second;
0486 }