ATLAS Offline Software
TrackMatchingLookup.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /// local include(s)
6 #include "TrackMatchingLookup.h"
7 #include "TrackParametersHelper.h"
8 #include "InDetTrackPerfMon/ITrackAnalysisDefinitionSvc.h"
9 
10 /// gaudi includes
11 #include "GaudiKernel/ISvcLocator.h"
12 #include "GaudiKernel/Service.h"
13 
14 /// STD includes
15 #include <sstream>
16 #include <algorithm>
17 
18 
19 /// Constructor
20 template< typename T, typename R >
21 IDTPM::TrackMatchingLookupBase<T, R>::TrackMatchingLookupBase(
22  const std::string& anaTag_s ) :
23  AthMessaging( "TrackMatchingLookup" + anaTag_s ),
24  m_anaTag( anaTag_s ), m_mapTestToRef(), m_mapRefToTest() { }
25 
26 
27 /// getMatchedRef
28 template< typename T, typename R >
29 const R* IDTPM::TrackMatchingLookupBase<T, R>::getMatchedRef( const T& t ) const
30 {
31  typename mapTtoR_t::const_iterator titr = m_mapTestToRef.find( &t );
32  if( titr != m_mapTestToRef.end() ) return titr->second;
33  return nullptr;
34 }
35 
36 
37 /// getMatchedTest
38 template< typename T, typename R >
39 const std::vector<const T*>&
40 IDTPM::TrackMatchingLookupBase<T, R>::getMatchedTest( const R& r ) const
41 {
42  typename mapRtoT_t::const_iterator titr = m_mapRefToTest.find( &r );
43  if( titr != m_mapRefToTest.end() ) return titr->second;
44  return m_nullTest;
45 }
46 
47 
48 /// getDist
49 template< typename T, typename R >
50 float IDTPM::TrackMatchingLookupBase<T, R>::getDist( const T& t ) const
51 {
52  typename mapTtoDist_t::const_iterator titr = m_mapTestToDist.find( &t );
53  if( titr != m_mapTestToDist.end() ) return titr->second;
54  return 999.; /// very large distance
55 }
56 
57 
58 /// isTestInMaps
59 template< typename T, typename R >
60 bool IDTPM::TrackMatchingLookupBase<T, R>::isTestInMaps( const T& t ) const
61 {
62  return ( getMatchedRef(t) != nullptr );
63 }
64 
65 
66 /// isRefInMaps
67 template< typename T, typename R >
68 bool IDTPM::TrackMatchingLookupBase<T, R>::isRefInMaps( const R& r ) const
69 {
70  return ( not getMatchedTest(r).empty() );
71 }
72 
73 
74 /// updateMaps
75 template< typename T, typename R >
76 StatusCode IDTPM::TrackMatchingLookupBase<T, R>::updateMaps(
77  const T& t, const R& r, float dist )
78 {
79  ATH_MSG_DEBUG( "Adding new match = test : pT = " << pT(t) <<
80  " -> reference : pT = " << pT(r) );
81 
82  /// Test->Reference caching (1 to 1)
83  std::pair< typename mapTtoR_t::iterator, bool > retTtoR =
84  m_mapTestToRef.insert( typename mapTtoR_t::value_type( &t, &r ) );
85 
86  if( not retTtoR.second ) {
87  ATH_MSG_DEBUG( "Test is already matched to reference with pT = " <<
88  pT( *(retTtoR.first->second) ) <<
89  " .\n\t-> New matched reference is not cached!" );
90  }
91 
92  /// Test->Distance caching (1 to 1)
93  m_mapTestToDist.insert( typename mapTtoDist_t::value_type( &t, dist ) );
94 
95  /// (Reverse) Reference -> Test(s) caching (1 to 1+)
96  /// The matched test vector is sorted by increasing values of the distance parameter
97  /// i.e. the first element will always be the best-matched test
98  if( isRefInMaps(r) ) {
99  typename mapRtoT_t::iterator mitr = m_mapRefToTest.find( &r );
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() );
113  }
114  else {
115  std::vector< const T* > tvec; // creating new vector of matched tests
116  tvec.push_back( &t );
117  m_mapRefToTest.insert( typename mapRtoT_t::value_type( &r, tvec ) );
118  }
119 
120  return StatusCode::SUCCESS;
121 }
122 
123 
124 /// clear lookup tables
125 template< typename T, typename R >
126 void IDTPM::TrackMatchingLookupBase<T, R>::clearMaps()
127 {
128  m_mapTestToRef.clear();
129  m_mapRefToTest.clear();
130  m_mapTestToDist.clear();
131 }
132 
133 
134 /// print info about matching and reverse matchings
135 template< typename T, typename R >
136 std::string IDTPM::TrackMatchingLookupBase<T, R>::printMaps(
137  const std::vector< const T* >& testVec,
138  const std::vector< const R* >& refVec,
139  std::string_view chainRoiName_s ) const
140 {
141  std::string testT(""), refT("");
142  ITrackAnalysisDefinitionSvc* trkAnaDefSvc;
143  ISvcLocator* svcLoc = Gaudi::svcLocator();
144  StatusCode sc = svcLoc->service( "TrkAnaDefSvc"+m_anaTag, trkAnaDefSvc );
145  if( sc.isSuccess() ) {
146  testT = "( " + trkAnaDefSvc->testType() + " ) ";
147  refT = "( " + trkAnaDefSvc->referenceType() + " ) ";
148  } else {
149  ATH_MSG_DEBUG( "Could not retrieve TrkAnaDefSvc" << m_anaTag );
150  }
151 
152  std::stringstream ss;
153  ss << "TrackMatchingLookup" << m_anaTag << " : " << chainRoiName_s
154  <<" --> Found " << getMapsSize() << " matches\n";
155  if( getMapsSize() == 0 ) return ss.str();
156 
157  ss << "\t\tTest " << testT
158  << "-> Reference " << refT << "matches:\n";
159  size_t it(0);
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";
167  } else {
168  ss << "N/A\n";
169  }
170  if( it > 20 ) { ss << "et al...\n"; break; }
171  it++;
172  }
173 
174  ss << "\t\tReference -> Test matches:\n";
175  it = 0;
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";
181  else {
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 << " ,";
186  }
187  ss << " ]\n";
188  }
189  if( it > 20 ) { ss << "et al...\n"; break; }
190  it++;
191  }
192 
193  return ss.str();
194 }