ATLAS Offline Software
TrackAnalysisInfoWriteTool.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 /**
6  * @file TrackAnalysisInfoWriteTool.icc
7  * @author Marco Aparo <marco.aparo@cern.ch>
8  **/
9 
10 /// Local includes
11 #include "TrackAnalysisInfoWriteTool.h"
12 #include "TrackMatchingLookup.h"
13 #include "TrackParametersHelper.h"
14 
15 /// STL includes
16 #include <algorithm>
17 #include <sstream>
18 
19 
20 ///-------------------------
21 ///----- getMatchInfo ------
22 ///-------------------------
23 template< typename Tcoll_t, typename Rcoll_t >
24 IDTPM::MatchInfo_t< Tcoll_t, Rcoll_t >
25 IDTPM::TrackAnalysisInfoWriteTool::getMatchInfo(
26  const Vec_t< Tcoll_t >& Tvec,
27  const Vec_t< Rcoll_t >& Rvec,
28  const Tcoll_t* Tcoll,
29  const Rcoll_t* Rcoll,
30  const ITrackMatchingLookup* matches ) const
31 {
32  VecEL_t< Tcoll_t > testVec;
33  VecEL_t< Rcoll_t > refVec;
34  VecF_t distVec;
35  Vec_t< Rcoll_t > matchedRefVec;
36 
37  auto matches_c = dynamic_cast< const TrackMatchingLookupBase<
38  typename Tcoll_t::base_value_type,
39  typename Rcoll_t::base_value_type >* >( matches );
40 
41  /// Loop over test vector
42  for( typename Tcoll_t::const_value_type thisTest : Tvec ) {
43  EL_t< Tcoll_t > testLink;
44  EL_t< Rcoll_t > refLink;
45  float dist( 999. );
46 
47  testLink.toContainedElement( *Tcoll, thisTest );
48 
49  if( matches_c->isTestInMaps( *thisTest ) ) {
50  typename Rcoll_t::const_value_type thisRef =
51  matches_c->getMatchedRef( *thisTest );
52  refLink.toContainedElement( *Rcoll, thisRef );
53  dist = matches_c->getDist( *thisTest );
54  matchedRefVec.push_back( thisRef );
55  }
56 
57  /// Fill both matched and unmatched tests
58  testVec.push_back( testLink );
59  refVec.push_back( refLink );
60  distVec.push_back( dist );
61  } // Close loop over test vector
62 
63  /// Loop over reference vector
64  /// for remaining/unmatched reference only
65  for( typename Rcoll_t::const_value_type thisRef : Rvec ) {
66  EL_t< Tcoll_t > testLink;
67  EL_t< Rcoll_t > refLink;
68  float dist( 999. );
69 
70  /// Skip already-recorded matched references
71  if( std::find( matchedRefVec.begin(), matchedRefVec.end(), thisRef ) !=
72  matchedRefVec.end() ) continue;
73 
74  if( matches_c->isRefInMaps( *thisRef ) ) {
75  ATH_MSG_WARNING( "Remaining reference is matched and it should not be." );
76  }
77 
78  refLink.toContainedElement( *Rcoll, thisRef );
79 
80  /// Fill remaining unmatched reference
81  testVec.push_back( testLink );
82  refVec.push_back( refLink );
83  distVec.push_back( dist );
84  } // Close loop over reference vector
85 
86  return std::make_tuple( testVec, refVec, distVec );
87 }
88 
89 
90 ///---------------------------
91 ///----- printMatchInfo ------
92 ///---------------------------
93 template< typename Tcoll_t, typename Rcoll_t >
94 std::string IDTPM::TrackAnalysisInfoWriteTool::printMatchInfo(
95  const VecEL_t< Tcoll_t >& testVec,
96  const VecEL_t< Rcoll_t >& refVec,
97  const VecF_t& distVec ) const
98 {
99  std::stringstream ss;
100 
101  if( testVec.size() != refVec.size() or testVec.size() != distVec.size() ) {
102  ATH_MSG_ERROR( "Vector size mismatch" );
103  return "";
104  }
105 
106  for( size_t iv=0; iv<distVec.size(); iv++ ) {
107  /// return the (track or truth) particle pT
108  /// if their ElementLink is valid, i.e. if the particle exists
109  std::string testPt = testVec.at(iv).isValid() ? std::to_string( pT( **testVec.at(iv) ) ) : "N/A";
110  std::string refPt = refVec.at(iv).isValid() ? std::to_string( pT( **refVec.at(iv) ) ) : "N/A";
111  std::string dist = std::to_string( distVec.at(iv) );
112  ss << "\t\t\t\t" << testPt << " --> " << refPt << " ( " << dist << " )\n";
113  if( iv > 20 ) { ss << "\t\t\t\tet al....\n"; break; }
114  }
115  return ss.str();
116 }