Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:22:14

0001 #include "EmbRecoMatchContainerv1.h"
0002 #include "EmbRecoMatch.h"
0003 #include "EmbRecoMatchv1.h"
0004 
0005 #include <algorithm>
0006 #include <iomanip>
0007 
0008 // WARNING to user:
0009 // methods depend on the internal data being sorted appropriately
0010 // -> BE SURE TO CALL *SORT*
0011 
0012 void EmbRecoMatchContainerv1::sort()
0013 {
0014   std::sort(m_data.begin(), m_data.end(), EmbRecoMatch::Comp());
0015   std::sort(m_RecoToTruth.begin(), m_RecoToTruth.end());
0016   std::sort(m_idsTruthUnmatched.begin(), m_idsTruthUnmatched.end());
0017 }
0018 
0019 void EmbRecoMatchContainerv1::addMatch(EmbRecoMatch* match)
0020 {
0021   m_data.push_back(match);
0022   auto id_true = match->idTruthTrack();
0023   auto id_reco = match->idRecoTrack();
0024 
0025   m_RecoToTruth.emplace_back(id_reco, id_true);  // vector of which to go to
0026 
0027   if (!m_nTruthPerReco.contains(id_reco))
0028   {
0029     m_nTruthPerReco[id_reco] = 1;
0030   }
0031   else
0032   {
0033     m_nTruthPerReco[id_reco] += 1;
0034   }
0035   if (!m_nRecoPerTruth.contains(id_true))
0036   {
0037     m_nRecoPerTruth[id_true] = 1;
0038   }
0039   else
0040   {
0041     m_nRecoPerTruth[id_true] += 1;
0042   }
0043 }
0044 
0045 void EmbRecoMatchContainerv1::checkfill_idsTruthUnmatched(unsigned short id_true)
0046 {
0047   if (!m_nRecoPerTruth.contains(id_true))
0048   {
0049     m_idsTruthUnmatched.push_back(id_true);
0050   }
0051 }
0052 
0053 void EmbRecoMatchContainerv1::Reset()
0054 {
0055   for (auto& m : m_data)
0056   {
0057     delete m;
0058   }
0059   m_data.clear();
0060   m_RecoToTruth.clear();
0061   m_idsTruthUnmatched.clear();
0062   m_nTruthPerReco.clear();
0063   m_nRecoPerTruth.clear();
0064 }
0065 
0066 std::vector<unsigned short> EmbRecoMatchContainerv1::ids_TruthMatched() const
0067 {
0068   std::vector<unsigned short> vec;
0069   for (const auto& id : m_data)
0070   {
0071     vec.push_back(id->idTruthTrack());
0072   }
0073   return vec;
0074 }
0075 
0076 std::vector<unsigned short> EmbRecoMatchContainerv1::ids_RecoMatched() const
0077 {
0078   std::vector<unsigned short> vec;
0079   vec.reserve(m_RecoToTruth.size());
0080 
0081   for (const auto& [id, _] : m_RecoToTruth)
0082   {
0083     vec.push_back(id);
0084   }
0085 
0086   return vec;
0087 }
0088 
0089 EmbRecoMatch* EmbRecoMatchContainerv1::getMatchTruth(unsigned short idtruth, unsigned short offset)
0090 {
0091   auto iter = std::lower_bound(m_data.begin(), m_data.end(), idtruth, EmbRecoMatch::Comp());
0092   iter += offset;
0093 
0094   if (iter >= m_data.end() || (*iter)->idTruthTrack() != idtruth)
0095   {
0096     std::cout << "Error: asking for match (offset by " << offset << ") for truth track id " << idtruth
0097               << " which is not present. Returning null pointer." << nullptr;
0098     return nullptr;
0099   }
0100   return *iter;
0101 }
0102 
0103 EmbRecoMatch* EmbRecoMatchContainerv1::getMatchReco(unsigned short idreco, unsigned short offset)
0104 {
0105   auto iter = std::lower_bound(m_RecoToTruth.begin(), m_RecoToTruth.end(), idreco, CompShortToPair());
0106   iter += offset;
0107   if (iter >= m_RecoToTruth.end() || iter->first != idreco)
0108   {
0109     std::cout << "Error: asking for match (offset by " << offset << ") for reco track id " << idreco
0110               << " which is not present. Returning null pointer." << nullptr;
0111     return nullptr;
0112   }
0113   return getMatchTruth(iter->second, offset);
0114 }
0115 
0116 bool EmbRecoMatchContainerv1::hasTruthMatch(unsigned short idtruth)
0117 {
0118   return std::binary_search(m_data.begin(), m_data.end(), idtruth, EmbRecoMatch::Comp());
0119 }
0120 
0121 bool EmbRecoMatchContainerv1::hasRecoMatch(unsigned short idreco)
0122 {
0123   return std::binary_search(m_RecoToTruth.begin(), m_RecoToTruth.end(), idreco, CompShortToPair());
0124 }
0125 
0126 void EmbRecoMatchContainerv1::identify(std::ostream& os) const
0127 {
0128   os << " EmbRecoMatchContainerv1 data. N(matched emb. tracks) = "
0129      << nMatches() << ",  N(not matched) " << nTruthUnmatched() << std::endl;
0130   os << " Matched track data: " << std::endl;
0131   //    id-Emb  id-Reco  id-Seed  id-SvtxSeed  nClus-Emb nClus-Reco meanZClus meanPhiClus
0132   os << std::setw(6) << "id-Emb "
0133      << std::setw(7) << "id-Reco "
0134      << std::setw(7) << "id-Seed "
0135      << std::setw(11) << "id-SvtxSeed "
0136      << std::setw(9) << "nClus-Emb "
0137      << std::setw(10) << "nClus-Reco "
0138      << std::setw(12) << "nClus_Matched " << std::endl;
0139   for (const auto& _m : m_data)
0140   {
0141     auto *m = static_cast<EmbRecoMatchv1*>(_m);
0142     os << std::setw(6) << m->idTruthTrack() << " "              // id-Emb"
0143        << std::setw(7) << m->idRecoTrack() << " "               //"id-Reco"
0144        << std::setw(7) << m->idTpcTrackSeed() << " "            //"id-Seed"
0145        << std::setw(11) << m->idSvtxTrackSeed() << " "          // id-SvtxSeed"
0146        << std::setw(9) << m->nClustersTruth() << " "            // nClus-Emb"
0147        << std::setw(10) << m->nClustersReco() << " "            //"nClus-Reco"
0148        << std::setw(12) << m->nClustersMatched() << std::endl;  // //"nClus-Reco"
0149                                                                 /* << std::setw(9)  << m->meanClusterZDiff() << " " //"meanZclus" */
0150                                                                 /* << std::setw(11) << m->meanClusterPhiDiff() << std::endl; //"meanPhiclus" << std::endl; */
0151 
0152     /* os << Form(" %7i %7i %8s %11s %11s %10s %10s %10s", */
0153     /* m->idTruthTrack(), m->idRecoTrack(), m->idTrackSeed(), m->idSvtxTrackSeed(), */
0154     /* m->nClustersTruth(), m->nClustersReco(), m->meanClusterZDiff(), m->meanClusterPhiDiff()) << std::endl; */
0155   }
0156   os << " IDs of embedded tracks that were not reconstructed: " << std::endl;
0157   for (const auto n : m_idsTruthUnmatched)
0158   {
0159     os << n << " ";
0160   }
0161   os << std::endl;
0162 }