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 
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 
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 
41  ATH_CHECK(m_clusterSplitProbContainerIn.initialize(!m_clusterSplitProbContainerIn.key().empty()));
42  ATH_CHECK(m_clusterSplitProbContainerOut.initialize(!m_clusterSplitProbContainerOut.key().empty()));
43 
44  if (m_disableSorting) ATH_MSG_DEBUG( "Internal sorting disabled, using external ordering!" );
45  return sc;
46 }
47 //==================================================================================================
48 
52  return sc;
53 }
54 
55 //==================================================================================================
56 
60 const TrackCollection*
62  Trk::PRDtoTrackMap *pPrdToTrackMap) const{
63  //TODO: make sure the ownership; delete origin tracks from map?
64  std::vector<const Track*> tracks;
65  tracks.reserve(tracksCol->size());
66  for(const auto *e: *tracksCol){
67  tracks.push_back(e);
68  }
69  ATH_MSG_DEBUG ("Processing "<<tracks.size()<<" tracks");
70  std::unique_ptr<Trk::PRDtoTrackMap> tmpPrdToTrackMap;
71  if (!pPrdToTrackMap) {
72  tmpPrdToTrackMap = m_assoTool->createPRDtoTrackMap();
73  if (!m_assoMapName.key().empty()) {
74  SG::ReadHandle<Trk::PRDtoTrackMap> inputPrdMap(m_assoMapName);
75  if (!inputPrdMap.isValid()) {
76  ATH_MSG_ERROR("Failed to retrieve prd to track map " << m_assoMapName.key() );
77  } else {
78  *tmpPrdToTrackMap = *inputPrdMap;
79  }
80  }
81  pPrdToTrackMap = tmpPrdToTrackMap.get();
82  }
83  TrackScoreMap trackScoreTrackMap;
84  //put tracks into maps etc
85  addNewTracks(trackScoreTrackMap,*pPrdToTrackMap, tracks);
86  // going to do simple algorithm for now:
87  // - take track with highest score
88  // - remove shared hits from all other tracks
89  // - take next highest scoring tracks, and repeat
90  std::unique_ptr<ConstDataVector<TrackCollection> > result(std::make_unique<ConstDataVector<TrackCollection> >(SG::VIEW_ELEMENTS)); //TODO, old or new
91  solveTracks(trackScoreTrackMap, *pPrdToTrackMap, *result);
92  if (msgLvl(MSG::DEBUG)) dumpTracks(*result->asDataVector());
93  return result.release()->asDataVector();
94 }
95 
96 
97 //==================================================================================================
98 void
100  Trk::PRDtoTrackMap &prdToTrackMap,
101  const std::vector<const Track*> &tracks) const{
102  ATH_MSG_DEBUG ("Number of tracks at Input: "<<tracks.size());
103  PrdSignatureSet prdSigSet;
104  TrackScore itrack=0;
105  for (const Track*a_track : tracks ) {
106  if(m_disableSorting) {
107  // add track to map using ordering provided by the collection
108  trackScoreTrackMap.insert( std::make_pair(itrack, TrackPtr(a_track)) );
109  itrack++;
110  continue;
111  }
112  bool reject = false;
113  TrackScore score = m_scoringTool->score( *a_track );
114  // veto tracks with score 0
115  if (score==0) {
116  ATH_MSG_DEBUG ("Track score is zero, reject it");
117  reject = true;
118  } else {
119  if (m_dropDouble) {
120  const std::vector<const Trk::PrepRawData*> & prds = m_assoTool->getPrdsOnTrack(prdToTrackMap, *a_track);
121  // unfortunately PrepRawDataSet is not a set !
122  PrdSignature prdSig;
123  prdSig.insert( prds.begin(),prds.end() );
124  // we try to insert it into the set, if we fail (pair.second), it then exits already
125  if ( !(prdSigSet.insert(prdSig)).second ) {
126  ATH_MSG_DEBUG ("Double track, reject it !");
127  reject = true;
128  } else {
129  ATH_MSG_DEBUG ("Insert new track in PrdSignatureSet");
130  }
131  }
132  }
133  if (!reject) {
134  // add track to map, map is sorted small to big ! set if fitted
135  ATH_MSG_VERBOSE ("Track ("<< a_track <<") has score "<<score);
136  trackScoreTrackMap.insert( std::make_pair(-score, TrackPtr(a_track) ) );
137  }
138  }
139  ATH_MSG_DEBUG ("Number of tracks in map:"<<trackScoreTrackMap.size());
140 }
141 
142 void
144  Trk::PRDtoTrackMap &prdToTrackMap,
146 {
147  using namespace std;
148 
149  const EventContext& ctx = Gaudi::Hive::currentContext();
151  if (!m_clusterSplitProbContainerIn.key().empty()) {
152  splitProbContainerIn = SG::ReadHandle( m_clusterSplitProbContainerIn, ctx);
153  if (!splitProbContainerIn.isValid()) {
154  ATH_MSG_ERROR( "Failed to get input cluster split probability container " << m_clusterSplitProbContainerIn.key());
155  }
156  }
157  std::unique_ptr<Trk::ClusterSplitProbabilityContainer> splitProbContainerCleanup(!m_clusterSplitProbContainerIn.key().empty()
158  ? std::make_unique<ClusterSplitProbabilityContainer>(*splitProbContainerIn)
159  : std::make_unique<ClusterSplitProbabilityContainer>());
161  Trk::ClusterSplitProbabilityContainer *splitProbContainer;
162  if (!m_clusterSplitProbContainerOut.key().empty()) {
163  splitProbContainerHandle = SG::WriteHandle<Trk::ClusterSplitProbabilityContainer>( m_clusterSplitProbContainerOut, ctx);
164  if (splitProbContainerHandle.record(std::move(splitProbContainerCleanup)).isFailure()) {
165  ATH_MSG_FATAL( "Failed to record output cluster split probability container " << m_clusterSplitProbContainerOut.key());
166  }
167  splitProbContainer=splitProbContainerHandle.ptr();
168  }
169  else {
170  splitProbContainer=splitProbContainerCleanup.get();
171  }
172 
173  ATH_MSG_VERBOSE ("Starting to solve tracks");
174  // now loop as long as map is not empty
175  while ( !trackScoreTrackMap.empty() ) {
176  TrackScoreMap::iterator itnext = trackScoreTrackMap.begin();
177  TrackPtr atrack( std::move(itnext->second) );
178  TrackScore ascore( itnext->first);
179  trackScoreTrackMap.erase(itnext);
180  ATH_MSG_VERBOSE ("--- Trying next track "<<atrack.track()<<"\t with score "<<-ascore);
181  std::unique_ptr<Trk::Track> cleanedTrack;
182  const auto &[cleanedTrack_tmp, keepOriginal] = m_selectionTool->getCleanedOutTrack( atrack.track() , -(ascore), *splitProbContainer, prdToTrackMap, -1, -1);
183  cleanedTrack.reset(cleanedTrack_tmp);
184  if (keepOriginal ){
185  // track can be kept as identical to the input track
186  ATH_MSG_DEBUG ("Accepted track "<<atrack.track()<<"\t has score "<<-(ascore));
187  // add track to PRD_AssociationTool
188  StatusCode sc = m_assoTool->addPRDs(prdToTrackMap,*atrack);
189  if (sc.isFailure()) ATH_MSG_ERROR( "addPRDs() failed" );
190  // add to output list
191  result.push_back( atrack.track() );
192 
193  } else if ( !cleanedTrack ) {
194  // track should be discarded
195  ATH_MSG_DEBUG ("Track "<< atrack.track() << " doesn't meet the cuts of the AmbiTrack Selection tool");
196  } else {
197  // delete cleaned track
198  cleanedTrack.reset();
199  // stripped down version cannot be handled discarding
200  ATH_MSG_DEBUG("Selection tool returned a new track, cannot handle memory management of new track, deleting it. Check you configuration ");
201  }
202  // don't forget to drop track from map
203  }
204  ATH_MSG_DEBUG ("Finished, number of track on output: "<<result.size());
205 }
206 
207 //==================================================================================================
208 
209 void
211  ATH_MSG_VERBOSE ("Dumping tracks in collection");
212  int num=0;
213  TrackScore totalScore = 0;
214  TrackCollection::const_iterator it = tracks.begin();
215  TrackCollection::const_iterator itEnd = tracks.end();
216  for (; it != itEnd ; ++it){
217  // score track:
218  const TrackScore score = m_scoringTool->score( **it );
219  ATH_MSG_VERBOSE (num++<<"\tTrack :"<<*it<<"\tScore: "<<score);
220  totalScore+=score;
221  }
222  ATH_MSG_DEBUG ("Total event score : "<<totalScore);
223 }
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:113
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
python.tests.PyTestsLib.finalize
def finalize(self)
_info( "content of StoreGate..." ) self.sg.dump()
Definition: PyTestsLib.py:50
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
get_generator_info.result
result
Definition: get_generator_info.py:21
Trk::TrackSelectionProcessorTool::TrackScoreMap
std::multimap< TrackScore, TrackPtr > TrackScoreMap
Definition: TrackSelectionProcessorTool.h:38
Trk::TrackSelectionProcessorTool::m_disableSorting
bool m_disableSorting
option to disable sorting based on track score and use the ordering provided externally
Definition: TrackSelectionProcessorTool.h:132
xAOD::addNewTracks
@ addNewTracks
Definition: TrackingPrimitives.h:461
Trk::TrackSelectionProcessorTool::PrdSignature
std::set< const PrepRawData * > PrdSignature
Definition: TrackSelectionProcessorTool.h:40
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:102
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:41
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:396
Trk::TrackSelectionProcessorTool::initialize
virtual StatusCode initialize() override
Definition: TrackSelectionProcessorTool.cxx:30
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()
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
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:210
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:61
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
DataVector< Trk::Track >
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
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
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:12
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:50
Track
Definition: TriggerChamberClusterOnTrackCreator.h:21
Trk::TrackSelectionProcessorTool::solveTracks
void solveTracks(TrackScoreMap &trackScoreTrackMap, Trk::PRDtoTrackMap &prdToTrackMap, ConstDataVector< TrackCollection > &final_tracks) const
Definition: TrackSelectionProcessorTool.cxx:143
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:99
CP::TrackPtr
SortedObjPtr< xAOD::TrackParticle > TrackPtr
Definition: PhysicsAnalysis/AnalysisCommon/IsolationSelection/IsolationSelection/Defs.h:70
xAOD::solveTracks
@ solveTracks
Definition: TrackingPrimitives.h:459