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  ISvcLocator* svcLoc = Gaudi::svcLocator();
143  SmartIF<ITrackAnalysisDefinitionSvc> trkAnaDefSvc(svcLoc->service( "TrkAnaDefSvc"+m_anaTag ));
144  if( trkAnaDefSvc ) {
145  testT = "( " + trkAnaDefSvc->testType() + " ) ";
146  refT = "( " + trkAnaDefSvc->referenceType() + " ) ";
147  } else {
148  ATH_MSG_DEBUG( "Could not retrieve TrkAnaDefSvc" << m_anaTag );
149  }
150 
151  std::stringstream ss;
152  ss << "TrackMatchingLookup" << m_anaTag << " : " << chainRoiName_s
153  <<" --> Found " << getMapsSize() << " matches\n";
154  if( getMapsSize() == 0 ) return ss.str();
155 
156  ss << "\t\tTest " << testT
157  << "-> Reference " << refT << "matches:\n";
158  size_t it(0);
159  for( const T* t : testVec ) {
160  ss << "\t\t\t\tTest with pT = " << pT(*t)
161  << " matches with --> ";
162  if( isTestInMaps(*t) ) {
163  ss << "Reference with pT = "
164  << pT( *(getMatchedRef(*t)) )
165  << " (dist = " << getDist(*t) << ")\n";
166  } else {
167  ss << "N/A\n";
168  }
169  if( it > 20 ) { ss << "et al...\n"; break; }
170  it++;
171  }
172 
173  ss << "\t\tReference -> Test matches:\n";
174  it = 0;
175  for( const R* r : refVec ) {
176  std::vector<const T*> testVecMatch = getMatchedTest(*r);
177  ss << "\t\t\t\tReference with pT = "
178  << pT(*r) << " matches with --> ";
179  if( testVecMatch.empty() ) ss << "N/A\n";
180  else {
181  ss << "tests with pTs = [";
182  for( size_t it=0 ; it<testVecMatch.size() ; it++ ) {
183  ss << " " << pT( *(testVecMatch.at(it)) );
184  if( it+1 != testVecMatch.size() ) ss << " ,";
185  }
186  ss << " ]\n";
187  }
188  if( it > 20 ) { ss << "et al...\n"; break; }
189  it++;
190  }
191 
192  return ss.str();
193 }