Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:19:05

0001 #include "BaseTruthEval.h"
0002 
0003 #include <g4main/PHG4Hit.h>
0004 #include <g4main/PHG4Particle.h>
0005 #include <g4main/PHG4Shower.h>
0006 #include <g4main/PHG4TruthInfoContainer.h>
0007 #include <g4main/PHG4VtxPoint.h>
0008 
0009 #include <phool/getClass.h>
0010 #include <phool/phool.h>
0011 
0012 #include <cassert>
0013 #include <cstdlib>
0014 #include <iostream>
0015 #include <utility>
0016 
0017 BaseTruthEval::BaseTruthEval(PHCompositeNode* topNode)
0018 {
0019   get_node_pointers(topNode);
0020 }
0021 
0022 BaseTruthEval::~BaseTruthEval()
0023 {
0024   if (m_Verbosity > 0)
0025   {
0026     if ((m_Errors > 0) || (m_Verbosity > 1))
0027     {
0028       std::cout << "BaseTruthEval::~BaseTruthEval() - Error Count: " << m_Errors << std::endl;
0029     }
0030   }
0031 }
0032 
0033 void BaseTruthEval::next_event(PHCompositeNode* topNode)
0034 {
0035   get_node_pointers(topNode);
0036 }
0037 
0038 int BaseTruthEval::get_embed(PHG4Particle* particle)
0039 {
0040   if (!has_reduced_node_pointers())
0041   {
0042     ++m_Errors;
0043     return 0;
0044   }
0045 
0046   if (m_Strict)
0047   {
0048     assert(particle);
0049   }
0050   else if (!particle)
0051   {
0052     ++m_Errors;
0053     return 0;
0054   }
0055 
0056   //  if (!is_primary(particle)) return 0;
0057   // allow the secondary particles to be tagged with its embedding ID of tis primary particle
0058 
0059   PHG4Particle* primary = get_primary_particle(particle);
0060   if (m_Strict)
0061   {
0062     assert(primary);
0063   }
0064   else if (!primary)
0065   {
0066     ++m_Errors;
0067     return 0;
0068   }
0069 
0070   return m_TruthInfo->isEmbeded(primary->get_track_id());
0071 }
0072 
0073 PHG4VtxPoint* BaseTruthEval::get_vertex(PHG4Particle* particle)
0074 {
0075   if (!has_reduced_node_pointers())
0076   {
0077     ++m_Errors;
0078     return nullptr;
0079   }
0080 
0081   if (m_Strict)
0082   {
0083     assert(particle);
0084   }
0085   else if (!particle)
0086   {
0087     ++m_Errors;
0088     return nullptr;
0089   }
0090 
0091   PHG4VtxPoint* vtx = m_TruthInfo->GetVtx(particle->get_vtx_id());
0092   if (m_Strict)
0093   {
0094     assert(vtx);
0095   }
0096   else if (!vtx)
0097   {
0098     m_Errors++;
0099   }
0100 
0101   return vtx;
0102 }
0103 
0104 bool BaseTruthEval::is_primary(PHG4Shower* shower)
0105 {
0106   if (!has_reduced_node_pointers())
0107   {
0108     ++m_Errors;
0109     return false;
0110   }
0111 
0112   if (m_Strict)
0113   {
0114     assert(shower);
0115   }
0116   else if (!shower)
0117   {
0118     ++m_Errors;
0119     return false;
0120   }
0121 
0122   bool is_primary = false;
0123   if (shower->get_parent_shower_id() == 0)
0124   {
0125     is_primary = true;
0126   }
0127 
0128   return is_primary;
0129 }
0130 
0131 bool BaseTruthEval::is_primary(PHG4Particle* particle)
0132 {
0133   if (!has_reduced_node_pointers())
0134   {
0135     ++m_Errors;
0136     return false;
0137   }
0138 
0139   if (m_Strict)
0140   {
0141     assert(particle);
0142   }
0143   else if (!particle)
0144   {
0145     ++m_Errors;
0146     return false;
0147   }
0148 
0149   bool is_primary = false;
0150   if (particle->get_parent_id() == 0)
0151   {
0152     is_primary = true;
0153   }
0154 
0155   return is_primary;
0156 }
0157 
0158 PHG4Shower* BaseTruthEval::get_primary_shower(PHG4Shower* shower)
0159 {
0160   if (!has_reduced_node_pointers())
0161   {
0162     ++m_Errors;
0163     return nullptr;
0164   }
0165 
0166   if (m_Strict)
0167   {
0168     assert(shower);
0169   }
0170   else if (!shower)
0171   {
0172     ++m_Errors;
0173     return nullptr;
0174   }
0175 
0176   if (is_primary(shower))
0177   {
0178     return shower;
0179   }
0180 
0181   while (!is_primary(shower))
0182   {
0183     shower = m_TruthInfo->GetShower(shower->get_parent_shower_id());
0184 
0185     if (m_Strict)
0186     {
0187       assert(shower);
0188     }
0189     else if (!shower)
0190     {
0191       ++m_Errors;
0192       break;
0193     }
0194   }
0195 
0196   return shower;
0197 }
0198 
0199 PHG4Shower* BaseTruthEval::get_primary_shower(PHG4Particle* particle)
0200 {
0201   if (!has_reduced_node_pointers())
0202   {
0203     ++m_Errors;
0204     return nullptr;
0205   }
0206 
0207   if (m_Strict)
0208   {
0209     assert(particle);
0210   }
0211   else if (!particle)
0212   {
0213     ++m_Errors;
0214     return nullptr;
0215   }
0216 
0217   if (!is_primary(particle))
0218   {
0219     particle = get_primary_particle(particle);
0220   }
0221 
0222   PHG4Shower* returnval = nullptr;
0223 
0224   PHG4TruthInfoContainer::ShowerRange range = m_TruthInfo->GetPrimaryShowerRange();
0225   for (PHG4TruthInfoContainer::ShowerIterator iter = range.first;
0226        iter != range.second;
0227        ++iter)
0228   {
0229     PHG4Shower* shower = iter->second;
0230     if (shower->get_parent_particle_id() == particle->get_track_id())
0231     {
0232       returnval = shower;
0233       break;
0234     }
0235   }
0236 
0237   return returnval;
0238 }
0239 
0240 PHG4Particle* BaseTruthEval::get_primary_particle(PHG4Particle* particle)
0241 {
0242   if (!has_reduced_node_pointers())
0243   {
0244     ++m_Errors;
0245     return nullptr;
0246   }
0247 
0248   if (m_Strict)
0249   {
0250     assert(particle);
0251   }
0252   else if (!particle)
0253   {
0254     ++m_Errors;
0255     return nullptr;
0256   }
0257 
0258   if (is_primary(particle))
0259   {
0260     return particle;
0261   }
0262 
0263   PHG4Particle* returnval = m_TruthInfo->GetPrimaryParticle(particle->get_primary_id());
0264 
0265   if (m_Strict)
0266   {
0267     assert(returnval);
0268   }
0269   else if (!returnval)
0270   {
0271     ++m_Errors;
0272   }
0273 
0274   return returnval;
0275 }
0276 
0277 PHG4Particle* BaseTruthEval::get_primary_particle(PHG4Shower* shower)
0278 {
0279   if (!has_reduced_node_pointers())
0280   {
0281     ++m_Errors;
0282     return nullptr;
0283   }
0284 
0285   if (m_Strict)
0286   {
0287     assert(shower);
0288   }
0289   else if (!shower)
0290   {
0291     ++m_Errors;
0292     return nullptr;
0293   }
0294 
0295   PHG4Particle* parent_particle = m_TruthInfo->GetParticle(shower->get_parent_particle_id());
0296 
0297   if (m_Strict)
0298   {
0299     assert(parent_particle);
0300   }
0301   else if (!parent_particle)
0302   {
0303     ++m_Errors;
0304     return nullptr;
0305   }
0306 
0307   PHG4Particle* primary_particle = get_primary_particle(parent_particle);
0308 
0309   if (m_Strict)
0310   {
0311     assert(primary_particle);
0312   }
0313   else if (!primary_particle)
0314   {
0315     ++m_Errors;
0316     return nullptr;
0317   }
0318 
0319   return primary_particle;
0320 }
0321 
0322 std::set<PHG4Shower*> BaseTruthEval::all_secondary_showers(PHG4Shower* shower)
0323 {
0324   if (!has_reduced_node_pointers())
0325   {
0326     ++m_Errors;
0327     return std::set<PHG4Shower*>();
0328   }
0329 
0330   if (m_Strict)
0331   {
0332     assert(shower);
0333   }
0334   else if (!shower)
0335   {
0336     ++m_Errors;
0337     return std::set<PHG4Shower*>();
0338   }
0339 
0340   std::set<PHG4Shower*> subshowers;
0341 
0342   PHG4TruthInfoContainer::ShowerRange range = m_TruthInfo->GetSecondaryShowerRange();
0343   for (PHG4TruthInfoContainer::ShowerIterator iter = range.first;
0344        iter != range.second;
0345        ++iter)
0346   {
0347     PHG4Shower* local_shower = iter->second;
0348 
0349     if (m_Strict)
0350     {
0351       assert(local_shower);
0352     }
0353     else if (!local_shower)
0354     {
0355       ++m_Errors;
0356     }
0357 
0358     if (local_shower)
0359     {
0360       if (local_shower->get_parent_shower_id() == local_shower->get_id())
0361       {
0362         subshowers.insert(local_shower);
0363       }
0364     }
0365   }
0366 
0367   return subshowers;
0368 }
0369 
0370 bool BaseTruthEval::are_same_shower(PHG4Shower* s1, PHG4Shower* s2)
0371 {
0372   if (!has_reduced_node_pointers())
0373   {
0374     ++m_Errors;
0375     return false;
0376   }
0377 
0378   if (m_Strict)
0379   {
0380     assert(s1);
0381     assert(s2);
0382   }
0383   else if (!s1 || !s2)
0384   {
0385     ++m_Errors;
0386     return false;
0387   }
0388 
0389   if (s1->get_id() == s2->get_id())
0390   {
0391     return true;
0392   }
0393   return false;
0394 }
0395 
0396 bool BaseTruthEval::are_same_particle(PHG4Particle* p1, PHG4Particle* p2)
0397 {
0398   if (!has_reduced_node_pointers())
0399   {
0400     ++m_Errors;
0401     return false;
0402   }
0403 
0404   if (m_Strict)
0405   {
0406     assert(p1);
0407     assert(p2);
0408   }
0409   else if (!p1 || !p2)
0410   {
0411     ++m_Errors;
0412     return false;
0413   }
0414 
0415   if (p1->get_track_id() == p2->get_track_id())
0416   {
0417     return true;
0418   }
0419   return false;
0420 }
0421 
0422 bool BaseTruthEval::are_same_vertex(PHG4VtxPoint* vtx1, PHG4VtxPoint* vtx2)
0423 {
0424   if (!has_reduced_node_pointers())
0425   {
0426     ++m_Errors;
0427     return false;
0428   }
0429 
0430   if (m_Strict)
0431   {
0432     assert(vtx1);
0433     assert(vtx2);
0434   }
0435   else if (!vtx1 || !vtx2)
0436   {
0437     ++m_Errors;
0438     return false;
0439   }
0440 
0441   if (vtx1->get_id() == vtx2->get_id())
0442   {
0443     return true;
0444   }
0445   return false;
0446 }
0447 
0448 PHG4Particle* BaseTruthEval::get_particle(PHG4Hit* g4hit)
0449 {
0450   if (!has_reduced_node_pointers())
0451   {
0452     ++m_Errors;
0453     return nullptr;
0454   }
0455 
0456   if (m_Strict)
0457   {
0458     assert(g4hit);
0459   }
0460   else if (!g4hit)
0461   {
0462     ++m_Errors;
0463     return nullptr;
0464   }
0465 
0466   PHG4Particle* particle = m_TruthInfo->GetParticle(g4hit->get_trkid());
0467   if (m_Strict)
0468   {
0469     assert(particle);
0470   }
0471   else if (!particle)
0472   {
0473     ++m_Errors;
0474   }
0475 
0476   return particle;
0477 }
0478 
0479 PHG4Shower* BaseTruthEval::get_primary_shower(PHG4Hit* g4hit)
0480 {
0481   if (!has_reduced_node_pointers())
0482   {
0483     ++m_Errors;
0484     return nullptr;
0485   }
0486 
0487   if (m_Strict)
0488   {
0489     assert(g4hit);
0490   }
0491   else if (!g4hit)
0492   {
0493     ++m_Errors;
0494     return nullptr;
0495   }
0496 
0497   PHG4Shower* shower = m_TruthInfo->GetShower(g4hit->get_shower_id());
0498   if (m_Strict)
0499   {
0500     assert(shower);
0501   }
0502   else if (!shower)
0503   {
0504     ++m_Errors;
0505   }
0506 
0507   return shower;
0508 }
0509 
0510 PHG4Particle* BaseTruthEval::get_primary_particle(PHG4Hit* g4hit)
0511 {
0512   if (!has_reduced_node_pointers())
0513   {
0514     ++m_Errors;
0515     return nullptr;
0516   }
0517 
0518   if (m_Strict)
0519   {
0520     assert(g4hit);
0521   }
0522   else if (!g4hit)
0523   {
0524     ++m_Errors;
0525     return nullptr;
0526   }
0527 
0528   PHG4Particle* particle = get_particle(g4hit);
0529   PHG4Particle* primary = get_primary_particle(particle);
0530 
0531   if (m_Strict)
0532   {
0533     assert(primary);
0534   }
0535   else if (!primary)
0536   {
0537     ++m_Errors;
0538   }
0539 
0540   return primary;
0541 }
0542 
0543 PHG4Particle* BaseTruthEval::get_particle(const int trackid)
0544 {
0545   return m_TruthInfo->GetParticle(trackid);
0546 }
0547 
0548 bool BaseTruthEval::is_g4hit_from_primary_shower(PHG4Hit* g4hit, PHG4Shower* shower)
0549 {
0550   if (!has_reduced_node_pointers())
0551   {
0552     ++m_Errors;
0553     return false;
0554   }
0555 
0556   if (m_Strict)
0557   {
0558     assert(g4hit);
0559     assert(shower);
0560   }
0561   else if (!g4hit || !shower)
0562   {
0563     ++m_Errors;
0564     return false;
0565   }
0566 
0567   if (g4hit->get_shower_id() == shower->get_id())
0568   {
0569     return true;
0570   }
0571 
0572   return false;
0573 }
0574 
0575 bool BaseTruthEval::is_g4hit_from_particle(PHG4Hit* g4hit, PHG4Particle* particle)
0576 {
0577   if (!has_reduced_node_pointers())
0578   {
0579     ++m_Errors;
0580     return false;
0581   }
0582 
0583   if (m_Strict)
0584   {
0585     assert(g4hit);
0586     assert(particle);
0587   }
0588   else if (!g4hit || !particle)
0589   {
0590     ++m_Errors;
0591     return false;
0592   }
0593 
0594   if (g4hit->get_trkid() == particle->get_track_id())
0595   {
0596     return true;
0597   }
0598 
0599   return false;
0600 }
0601 
0602 void BaseTruthEval::get_node_pointers(PHCompositeNode* topNode)
0603 {
0604   m_TruthInfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
0605   if (!m_TruthInfo)
0606   {
0607     std::cout << PHWHERE << " ERROR: Can't find G4TruthInfo" << std::endl;
0608     exit(-1);
0609   }
0610 
0611   return;
0612 }
0613 
0614 bool BaseTruthEval::has_reduced_node_pointers()
0615 {
0616   if (m_Strict)
0617   {
0618     assert(m_TruthInfo);
0619   }
0620   else if (!m_TruthInfo)
0621   {
0622     return false;
0623   }
0624 
0625   return true;
0626 }