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