2   Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
 
    6 #include "TrackMatchingLookup.h"
 
    7 #include "TrackParametersHelper.h"
 
    8 #include "InDetTrackPerfMon/ITrackAnalysisDefinitionSvc.h"
 
   11 #include "GaudiKernel/ISvcLocator.h"
 
   12 #include "GaudiKernel/Service.h"
 
   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() { }
 
   28 template< typename T, typename R >
 
   29 const R* IDTPM::TrackMatchingLookupBase<T, R>::getMatchedRef( const T& t ) const
 
   31   typename mapTtoR_t::const_iterator titr = m_mapTestToRef.find( &t );
 
   32   if( titr != m_mapTestToRef.end() ) return titr->second;
 
   38 template< typename T, typename R >
 
   39 const std::vector<const T*>&
 
   40 IDTPM::TrackMatchingLookupBase<T, R>::getMatchedTest( const R& r ) const
 
   42   typename mapRtoT_t::const_iterator titr = m_mapRefToTest.find( &r );
 
   43   if( titr != m_mapRefToTest.end() ) return titr->second;
 
   49 template< typename T, typename R >
 
   50 float IDTPM::TrackMatchingLookupBase<T, R>::getDist( const T& t ) const
 
   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
 
   59 template< typename T, typename R >
 
   60 bool IDTPM::TrackMatchingLookupBase<T, R>::isTestInMaps( const T& t ) const
 
   62   return ( getMatchedRef(t) != nullptr );
 
   67 template< typename T, typename R >
 
   68 bool IDTPM::TrackMatchingLookupBase<T, R>::isRefInMaps( const R& r ) const
 
   70   return ( not getMatchedTest(r).empty() );
 
   75 template< typename T, typename R >
 
   76 StatusCode IDTPM::TrackMatchingLookupBase<T, R>::updateMaps(
 
   77     const T& t, const R& r, float dist )
 
   79   ATH_MSG_DEBUG( "Adding new match = test : pT = " << pT(t) <<
 
   80                  " -> reference : pT = " << pT(r) );  
 
   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 ) );
 
   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!" );
 
   92   /// Test->Distance caching (1 to 1)
 
   93   m_mapTestToDist.insert( typename mapTtoDist_t::value_type( &t, dist ) );
 
   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() );
 
  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 ) );
 
  120   return StatusCode::SUCCESS;
 
  124 /// clear lookup tables
 
  125 template< typename T, typename R >
 
  126 void IDTPM::TrackMatchingLookupBase<T, R>::clearMaps()
 
  128   m_mapTestToRef.clear();
 
  129   m_mapRefToTest.clear();
 
  130   m_mapTestToDist.clear();
 
  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
 
  141   std::string testT(""), refT("");
 
  142   ISvcLocator* svcLoc = Gaudi::svcLocator();
 
  143   SmartIF<ITrackAnalysisDefinitionSvc> trkAnaDefSvc(svcLoc->service( "TrkAnaDefSvc"+m_anaTag ));
 
  145     testT = "( " + trkAnaDefSvc->testType() + " ) ";
 
  146     refT  = "( " + trkAnaDefSvc->referenceType()  + " ) ";
 
  148     ATH_MSG_DEBUG( "Could not retrieve TrkAnaDefSvc" << m_anaTag );
 
  151   std::stringstream ss;
 
  152   ss << "TrackMatchingLookup" << m_anaTag << " : " << chainRoiName_s
 
  153      <<" --> Found " << getMapsSize() << " matches\n";
 
  154   if( getMapsSize() == 0 ) return ss.str();
 
  156   ss << "\t\tTest " << testT
 
  157      << "-> Reference " << refT << "matches:\n";
 
  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";
 
  169     if( it > 20 ) { ss << "et al...\n"; break; }
 
  173   ss << "\t\tReference -> Test matches:\n";
 
  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";
 
  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 << " ,";
 
  188     if( it > 20 ) { ss << "et al...\n"; break; }