ATLAS Offline Software
TrackSelectionProcessorTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "TrackScoringTool.h"
10 #include "GaudiKernel/MsgStream.h"
11 #include <map>
12 
13 //==================================================================================================
15  const std::string& n,
16  const IInterface* p )
17  :
18  AthAlgTool(t,n,p),
19  m_scoringTool("Trk::TrackScoringTool/TrackScoringTool"),
20  m_selectionTool("InDet::InDetAmbiTrackSelectionTool/InDetAmbiTrackSelectionTool")
21 {
22  declareInterface<ITrackAmbiguityProcessorTool>(this);
23  declareProperty("DropDouble" , m_dropDouble = true);
24  declareProperty("ScoringTool" , m_scoringTool);
25  declareProperty("SelectionTool" , m_selectionTool);
26  declareProperty("DisableSorting" , m_disableSorting = false);
27 }
28 //==================================================================================================
29 
31 //==================================================================================================
32 
36  if (sc.isFailure()) {
37  ATH_MSG_FATAL( "AlgTool::initialise failed" );
38  return StatusCode::FAILURE;
39  }
40  ATH_CHECK( m_assoMapName.initialize(!m_assoMapName.key().empty()));
41  ATH_CHECK( m_assoTool.retrieve() );
42  ATH_CHECK( m_scoringTool.retrieve());
43  ATH_CHECK(m_selectionTool.retrieve());
44 
45  ATH_CHECK(m_clusterSplitProbContainerIn.initialize(!m_clusterSplitProbContainerIn.key().empty()));
46  ATH_CHECK(m_clusterSplitProbContainerOut.initialize(!m_clusterSplitProbContainerOut.key().empty()));
47 
48  if (m_disableSorting) ATH_MSG_DEBUG( "Internal sorting disabled, using external ordering!" );
49  return sc;
50 }
51 //==================================================================================================
52 
56  return sc;
57 }
58 
59 //==================================================================================================
60 
64 const TrackCollection*
66  Trk::PRDtoTrackMap *pPrdToTrackMap) const{
67  //TODO: make sure the ownership; delete origin tracks from map?
68  std::vector<const Track*> tracks;
69  tracks.reserve(tracksCol->size());
70  for(const auto *e: *tracksCol){
71  tracks.push_back(e);
72  }
73  ATH_MSG_DEBUG ("Processing "<<tracks.size()<<" tracks");
74  std::unique_ptr<Trk::PRDtoTrackMap> tmpPrdToTrackMap;
75  if (!pPrdToTrackMap) {
76  tmpPrdToTrackMap = m_assoTool->createPRDtoTrackMap();
77  if (!m_assoMapName.key().empty()) {
78  SG::ReadHandle<Trk::PRDtoTrackMap> inputPrdMap(m_assoMapName);
79  if (!inputPrdMap.isValid()) {
80  ATH_MSG_ERROR("Failed to retrieve prd to track map " << m_assoMapName.key() );
81  } else {
82  *tmpPrdToTrackMap = *inputPrdMap;
83  }
84  }
85  pPrdToTrackMap = tmpPrdToTrackMap.get();
86  }
87  TrackScoreMap trackScoreTrackMap;
88  //put tracks into maps etc
89  addNewTracks(trackScoreTrackMap,*pPrdToTrackMap, tracks);
90  // going to do simple algorithm for now:
91  // - take track with highest score
92  // - remove shared hits from all other tracks
93  // - take next highest scoring tracks, and repeat
94  std::unique_ptr<ConstDataVector<TrackCollection> > result(std::make_unique<ConstDataVector<TrackCollection> >(SG::VIEW_ELEMENTS)); //TODO, old or new
95  solveTracks(trackScoreTrackMap, *pPrdToTrackMap, *result);
96  if (msgLvl(MSG::DEBUG)) dumpTracks(*result->asDataVector());
97  return result.release()->asDataVector();
98 }
99 
100 
101 //==================================================================================================
102 void
104  Trk::PRDtoTrackMap &prdToTrackMap,
105  const std::vector<const Track*> &tracks) const{
106  ATH_MSG_DEBUG ("Number of tracks at Input: "<<tracks.size());
107  PrdSignatureSet prdSigSet;
108  TrackScore itrack=0;
109  for (const Track*a_track : tracks ) {
110  if(m_disableSorting) {
111  // add track to map using ordering provided by the collection
112  trackScoreTrackMap.insert( std::make_pair(itrack, TrackPtr(a_track)) );
113  itrack++;
114  continue;
115  }
116  bool reject = false;
117  TrackScore score = m_scoringTool->score( *a_track );
118  // veto tracks with score 0
119  if (score==0) {
120  ATH_MSG_DEBUG ("Track score is zero, reject it");
121  reject = true;
122  } else {
123  if (m_dropDouble) {
124  const std::vector<const Trk::PrepRawData*> & prds = m_assoTool->getPrdsOnTrack(prdToTrackMap, *a_track);
125  // unfortunately PrepRawDataSet is not a set !
126  PrdSignature prdSig;
127  prdSig.insert( prds.begin(),prds.end() );
128  // we try to insert it into the set, if we fail (pair.second), it then exits already
129  if ( !(prdSigSet.insert(prdSig)).second ) {
130  ATH_MSG_DEBUG ("Double track, reject it !");
131  reject = true;
132  } else {
133  ATH_MSG_DEBUG ("Insert new track in PrdSignatureSet");
134  }
135  }
136  }
137  if (!reject) {
138  // add track to map, map is sorted small to big ! set if fitted
139  ATH_MSG_VERBOSE ("Track ("<< a_track <<") has score "<<score);
140  trackScoreTrackMap.insert( std::make_pair(-score, TrackPtr(a_track) ) );
141  }
142  }
143  ATH_MSG_DEBUG ("Number of tracks in map:"<<trackScoreTrackMap.size());
144 }
145 
146 void
148  Trk::PRDtoTrackMap &prdToTrackMap,
150 {
151  using namespace std;
152 
153  const EventContext& ctx = Gaudi::Hive::currentContext();
155  if (!m_clusterSplitProbContainerIn.key().empty()) {
156  splitProbContainerIn = SG::ReadHandle( m_clusterSplitProbContainerIn, ctx);
157  if (!splitProbContainerIn.isValid()) {
158  ATH_MSG_ERROR( "Failed to get input cluster split probability container " << m_clusterSplitProbContainerIn.key());
159  }
160  }
161  std::unique_ptr<Trk::ClusterSplitProbabilityContainer> splitProbContainerCleanup(!m_clusterSplitProbContainerIn.key().empty()
162  ? std::make_unique<ClusterSplitProbabilityContainer>(*splitProbContainerIn)
163  : std::make_unique<ClusterSplitProbabilityContainer>());
165  Trk::ClusterSplitProbabilityContainer *splitProbContainer;
166  if (!m_clusterSplitProbContainerOut.key().empty()) {
167  splitProbContainerHandle = SG::WriteHandle<Trk::ClusterSplitProbabilityContainer>( m_clusterSplitProbContainerOut, ctx);
168  if (splitProbContainerHandle.record(std::move(splitProbContainerCleanup)).isFailure()) {
169  ATH_MSG_FATAL( "Failed to record output cluster split probability container " << m_clusterSplitProbContainerOut.key());
170  }
171  splitProbContainer=splitProbContainerHandle.ptr();
172  }
173  else {
174  splitProbContainer=splitProbContainerCleanup.get();
175  }
176 
177  ATH_MSG_VERBOSE ("Starting to solve tracks");
178  // now loop as long as map is not empty
179  while ( !trackScoreTrackMap.empty() ) {
180  TrackScoreMap::iterator itnext = trackScoreTrackMap.begin();
181  TrackPtr atrack( std::move(itnext->second) );
182  TrackScore ascore( itnext->first);
183  trackScoreTrackMap.erase(itnext);
184  ATH_MSG_VERBOSE ("--- Trying next track "<<atrack.track()<<"\t with score "<<-ascore);
185  std::unique_ptr<Trk::Track> cleanedTrack;
186  const auto &[cleanedTrack_tmp, keepOriginal] = m_selectionTool->getCleanedOutTrack( atrack.track() , -(ascore), *splitProbContainer, prdToTrackMap, -1, -1);
187  cleanedTrack.reset(cleanedTrack_tmp);
188  if (keepOriginal ){
189  // track can be kept as identical to the input track
190  ATH_MSG_DEBUG ("Accepted track "<<atrack.track()<<"\t has score "<<-(ascore));
191  // add track to PRD_AssociationTool
192  StatusCode sc = m_assoTool->addPRDs(prdToTrackMap,*atrack);
193  if (sc.isFailure()) ATH_MSG_ERROR( "addPRDs() failed" );
194  // add to output list
195  result.push_back( atrack.track() );
196 
197  } else if ( !cleanedTrack ) {
198  // track should be discarded
199  ATH_MSG_DEBUG ("Track "<< atrack.track() << " doesn't meet the cuts of the AmbiTrack Selection tool");
200  } else {
201  // delete cleaned track
202  cleanedTrack.reset();
203  // stripped down version cannot be handled discarding
204  ATH_MSG_DEBUG("Selection tool returned a new track, cannot handle memory management of new track, deleting it. Check you configuration ");
205  }
206  // don't forget to drop track from map
207  }
208  ATH_MSG_DEBUG ("Finished, number of track on output: "<<result.size());
209 }
210 
211 //==================================================================================================
212 
213 void
215  ATH_MSG_VERBOSE ("Dumping tracks in collection");
216  int num=0;
217  TrackScore totalScore = 0;
218  TrackCollection::const_iterator it = tracks.begin();
219  TrackCollection::const_iterator itEnd = tracks.end();
220  for (; it != itEnd ; ++it){
221  // score track:
222  const TrackScore score = m_scoringTool->score( **it );
223  ATH_MSG_VERBOSE (num++<<"\tTrack :"<<*it<<"\tScore: "<<score);
224  totalScore+=score;
225  }
226  ATH_MSG_DEBUG ("Total event score : "<<totalScore);
227 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
Trk::TrackSelectionProcessorTool::m_selectionTool
ToolHandle< IAmbiTrackSelectionTool > m_selectionTool
selection tool - here the decision which hits remain on a track and which are removed are made
Definition: TrackSelectionProcessorTool.h:114
python.tests.PyTestsLib.finalize
def finalize(self)
_info( "content of StoreGate..." ) self.sg.dump()
Definition: PyTestsLib.py:53
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
TrackScoringTool.h
get_generator_info.result
result
Definition: get_generator_info.py:21
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
Trk::TrackSelectionProcessorTool::TrackScoreMap
std::multimap< TrackScore, TrackPtr > TrackScoreMap
Definition: TrackSelectionProcessorTool.h:40
Trk::TrackSelectionProcessorTool::m_disableSorting
bool m_disableSorting
option to disable sorting based on track score and use the ordering provided externally
Definition: TrackSelectionProcessorTool.h:133
xAOD::addNewTracks
@ addNewTracks
Definition: TrackingPrimitives.h:461
Trk::TrackSelectionProcessorTool::PrdSignature
std::set< const PrepRawData * > PrdSignature
Definition: TrackSelectionProcessorTool.h:42
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
Trk::TrackSelectionProcessorTool::m_dropDouble
bool m_dropDouble
by default drop double tracks before refit
Definition: TrackSelectionProcessorTool.h:104
Trk::PRDtoTrackMap
Definition: PRDtoTrackMap.h:17
ConstDataVector.h
DataVector adapter that acts like it holds const pointers.
SG::ReadHandle< Trk::PRDtoTrackMap >
Trk::TrackSelectionProcessorTool::PrdSignatureSet
std::set< PrdSignature > PrdSignatureSet
Definition: TrackSelectionProcessorTool.h:43
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
initialize
void initialize()
Definition: run_EoverP.cxx:894
skel.it
it
Definition: skel.GENtoEVGEN.py:423
Trk::TrackSelectionProcessorTool::initialize
virtual StatusCode initialize() override
Definition: TrackSelectionProcessorTool.cxx:34
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
Trk::TrackPtr::track
const Trk::Track * track() const
Definition: TrackPtr.h:54
Trk::TrackSelectionProcessorTool::~TrackSelectionProcessorTool
virtual ~TrackSelectionProcessorTool()
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Trk::TrackSelectionProcessorTool::dumpTracks
void dumpTracks(const TrackCollection &tracks) const
print out tracks and their scores for debugging
Definition: TrackSelectionProcessorTool.cxx:214
Trk::TrackSelectionProcessorTool::process
virtual const TrackCollection * process(const TrackCollection *tracksCol, Trk::PRDtoTrackMap *prdToTrackMap) const override
Returns a processed TrackCollection from the passed 'tracks' WITHOUT copying or refitting the input t...
Definition: TrackSelectionProcessorTool.cxx:65
TrackCollection
DataVector< Trk::Track > TrackCollection
This typedef represents a collection of Trk::Track objects.
Definition: TrackCollection.h:19
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TrackCollection.h
Trk::TrackPtr
Definition: TrackPtr.h:10
Trk::TrackScore
float TrackScore
Definition: TrackScore.h:10
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::TrackSelectionProcessorTool::m_scoringTool
ToolHandle< ITrackScoringTool > m_scoringTool
Scoring tool This tool is used to 'score' the tracks, i.e.
Definition: TrackSelectionProcessorTool.h:109
DataVector< Trk::Track >
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
IPRD_AssociationTool.h
trigbs_pickEvents.num
num
Definition: trigbs_pickEvents.py:76
Trk::ClusterSplitProbabilityContainer
Container to associate Cluster with cluster splitting probabilities.
Definition: ClusterSplitProbabilityContainer.h:17
TrackSelectionProcessorTool.h
xAOD::score
@ score
Definition: TrackingPrimitives.h:513
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
Trk::TrackSelectionProcessorTool::TrackSelectionProcessorTool
TrackSelectionProcessorTool(const std::string &, const std::string &, const IInterface *)
Definition: TrackSelectionProcessorTool.cxx:14
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
DEBUG
#define DEBUG
Definition: page_access.h:11
Trk::TrackSelectionProcessorTool::finalize
virtual StatusCode finalize() override
Definition: TrackSelectionProcessorTool.cxx:54
Track
Definition: TriggerChamberClusterOnTrackCreator.h:21
Trk::TrackSelectionProcessorTool::solveTracks
void solveTracks(TrackScoreMap &trackScoreTrackMap, Trk::PRDtoTrackMap &prdToTrackMap, ConstDataVector< TrackCollection > &final_tracks) const
Definition: TrackSelectionProcessorTool.cxx:147
AthAlgTool
Definition: AthAlgTool.h:26
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
Trk::TrackSelectionProcessorTool::addNewTracks
void addNewTracks(TrackScoreMap &trackScoreTrackMap, Trk::PRDtoTrackMap &prdToTrackMap, const std::vector< const Track * > &tracks) const
Add passed TrackCollection, and Trk::PrepRawData from tracks to caches.
Definition: TrackSelectionProcessorTool.cxx:103
CP::TrackPtr
SortedObjPtr< xAOD::TrackParticle > TrackPtr
Definition: PhysicsAnalysis/AnalysisCommon/IsolationSelection/IsolationSelection/Defs.h:70
xAOD::solveTracks
@ solveTracks
Definition: TrackingPrimitives.h:459