2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
6#include "TrackParametersHelper.h"
7#include "InDetTrackPerfMon/ITrackAnalysisDefinitionSvc.h"
10#include "GaudiKernel/ISvcLocator.h"
11#include "GaudiKernel/Service.h"
19template< typename T, typename R >
20IDTPM::TrackMatchingLookupBase<T, R>::TrackMatchingLookupBase(
21 const std::string& anaTag_s ) :
22 AthMessaging( "TrackMatchingLookup" + anaTag_s ),
23 m_anaTag( anaTag_s ), m_mapTestToRef(), m_mapRefToTest() { }
27template< typename T, typename R >
28const R* IDTPM::TrackMatchingLookupBase<T, R>::getMatchedRef( const T& t ) const
30 typename mapTtoR_t::const_iterator titr = m_mapTestToRef.find( &t );
31 if( titr != m_mapTestToRef.end() ) return titr->second;
37template< typename T, typename R >
38const std::vector<const T*>&
39IDTPM::TrackMatchingLookupBase<T, R>::getMatchedTest( const R& r ) const
41 typename mapRtoT_t::const_iterator titr = m_mapRefToTest.find( &r );
42 if( titr != m_mapRefToTest.end() ) return titr->second;
48template< typename T, typename R >
49float IDTPM::TrackMatchingLookupBase<T, R>::getDist( const T& t ) const
51 typename mapTtoDist_t::const_iterator titr = m_mapTestToDist.find( &t );
52 if( titr != m_mapTestToDist.end() ) return titr->second;
53 return 999.; /// very large distance
58template< typename T, typename R >
59bool IDTPM::TrackMatchingLookupBase<T, R>::isTestInMaps( const T& t ) const
61 return ( getMatchedRef(t) != nullptr );
66template< typename T, typename R >
67bool IDTPM::TrackMatchingLookupBase<T, R>::isRefInMaps( const R& r ) const
69 return ( not getMatchedTest(r).empty() );
74template< typename T, typename R >
75StatusCode IDTPM::TrackMatchingLookupBase<T, R>::updateMaps(
76 const T& t, const R& r, float dist )
78 ATH_MSG_DEBUG( "Adding new match = test : pT = " << pT(t) <<
79 " -> reference : pT = " << pT(r) );
81 /// Test->Reference caching (1 to 1)
82 std::pair< typename mapTtoR_t::iterator, bool > retTtoR =
83 m_mapTestToRef.insert( typename mapTtoR_t::value_type( &t, &r ) );
85 if( not retTtoR.second ) {
86 ATH_MSG_DEBUG( "Test is already matched to reference with pT = " <<
87 pT( *(retTtoR.first->second) ) <<
88 " .\n\t-> New matched reference is not cached!" );
91 /// Test->Distance caching (1 to 1)
92 m_mapTestToDist.insert( typename mapTtoDist_t::value_type( &t, dist ) );
94 /// (Reverse) Reference -> Test(s) caching (1 to 1+)
95 /// The matched test vector is sorted by increasing values of the distance parameter
96 /// i.e. the first element will always be the best-matched test
97 if( isRefInMaps(r) ) {
98 typename mapRtoT_t::iterator mitr = m_mapRefToTest.find( &r );
99 if (mitr != m_mapRefToTest.end()){
100 ATH_MSG_DEBUG( "Reference already matched to other " <<
101 mitr->second.size() << " test(s). Adding a new one..." );
102 /// copy of the matched test vector
103 std::vector< const T* > tvec = mitr->second;
104 /// Adding the new test track
105 tvec.push_back( &t );
106 /// sorting the vector by increasing distance
107 std::sort( tvec.begin(), tvec.end(),
108 [&]( const T* t1, const T* t2 ) -> bool{
109 return ( getDist(*t1) < getDist(*t2) ); } );
110 /// update the vector in the map
111 mitr->second.clear();
112 mitr->second.insert( mitr->second.begin(), tvec.begin(), tvec.end() );
116 std::vector< const T* > tvec; // creating new vector of matched tests
117 tvec.push_back( &t );
118 m_mapRefToTest.insert( typename mapRtoT_t::value_type( &r, tvec ) );
121 return StatusCode::SUCCESS;
125/// clear lookup tables
126template< typename T, typename R >
127void IDTPM::TrackMatchingLookupBase<T, R>::clearMaps()
129 m_mapTestToRef.clear();
130 m_mapRefToTest.clear();
131 m_mapTestToDist.clear();
135/// print info about matching and reverse matchings
136template< typename T, typename R >
137std::string IDTPM::TrackMatchingLookupBase<T, R>::printMaps(
138 const std::vector< const T* >& testVec,
139 const std::vector< const R* >& refVec,
140 std::string_view chainRoiName_s ) const
142 std::string testT(""), refT("");
143 ISvcLocator* svcLoc = Gaudi::svcLocator();
144 SmartIF<ITrackAnalysisDefinitionSvc> trkAnaDefSvc(svcLoc->service( "TrkAnaDefSvc"+m_anaTag ));
146 testT = "( " + trkAnaDefSvc->testType() + " ) ";
147 refT = "( " + trkAnaDefSvc->referenceType() + " ) ";
149 ATH_MSG_DEBUG( "Could not retrieve TrkAnaDefSvc" << m_anaTag );
152 std::stringstream ss;
153 ss << "TrackMatchingLookup" << m_anaTag << " : " << chainRoiName_s
154 <<" --> Found " << getMapsSize() << " matches\n";
155 if( getMapsSize() == 0 ) return ss.str();
157 ss << "\t\tTest " << testT
158 << "-> Reference " << refT << "matches:\n";
160 for( const T* t : testVec ) {
161 ss << "\t\t\t\tTest with pT = " << pT(*t)
162 << " matches with --> ";
163 if( isTestInMaps(*t) ) {
164 ss << "Reference with pT = "
165 << pT( *(getMatchedRef(*t)) )
166 << " (dist = " << getDist(*t) << ")\n";
170 if( it > 20 ) { ss << "et al...\n"; break; }
174 ss << "\t\tReference -> Test matches:\n";
176 for( const R* r : refVec ) {
177 std::vector<const T*> testVecMatch = getMatchedTest(*r);
178 ss << "\t\t\t\tReference with pT = "
179 << pT(*r) << " matches with --> ";
180 if( testVecMatch.empty() ) ss << "N/A\n";
182 ss << "tests with pTs = [";
183 for( size_t it=0 ; it<testVecMatch.size() ; it++ ) {
184 ss << " " << pT( *(testVecMatch.at(it)) );
185 if( it+1 != testVecMatch.size() ) ss << " ,";
189 if( it > 20 ) { ss << "et al...\n"; break; }