ATLAS Offline Software
PRD_AssociationTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 #include "TrkTrack/Track.h"
10 
11 #include <cassert>
12 #include <vector>
13 #include <stdexcept>
14 #include "ext/functional"
15 
17  const std::string& n,
18  const IInterface* p )
19  :
20  base_class(t,n,p)
21 {
22 }
23 
25 {
27  if (sc.isFailure()) return sc;
28  ATH_CHECK( m_prdToTrackMap.initialize( !m_prdToTrackMap.key().empty() ) );
29  ATH_CHECK( m_idHelperSvc.retrieve());
30  return StatusCode::SUCCESS;
31 }
32 
34 {
35  if (!m_prdToTrackMap.key().empty()) throw std::runtime_error("Cannot modify(addPRDs) the PRDtoTrackMap that was read from storegate!");
36  return addPRDs (m_maps, track);
37 }
38 
40 {
41  // test caching
42  TrackPrepRawDataMap::const_iterator itvec = maps.m_trackPrepRawDataMap.find(&track);
43  if (itvec!=maps.m_trackPrepRawDataMap.end())
44  {
45  ATH_MSG_ERROR ("track already found in cache, should not happen");
46  return StatusCode::FAILURE;
47  }
48  // get all prds on 'track'
49  std::vector< const Trk::PrepRawData* > prds = getPrdsOnTrack( maps, track );
50  for (const Trk::PrepRawData* prd : prds) {
51  maps.m_prepRawDataTrackMap.emplace(prd, &track);
52  }
53 
54  // cache this using m_trackPrepRawDataMap
55  maps.m_trackPrepRawDataMap.emplace(&track, prds);
56 
57  ATH_MSG_DEBUG ("Added PRDs from Track at ("<<&track<<") - map now has size: \t"<<
58  maps.m_prepRawDataTrackMap.size());
59  return StatusCode::SUCCESS;
60 }
61 
63 {
64  if (!m_prdToTrackMap.key().empty()) throw std::runtime_error("Cannot modify(removePRDs) the PRDtoTrackMap that was read from storegate!");
65  return removePRDs (m_maps, track);
66 }
67 
69  const Trk::Track& track ) const
70 {
71  // This is NOT pretty code!
72  // At the moment I'm brute-forcing, but maybe I need a second map, containing <Track*, iterator>
73  // The problem is that I think filling such a map is also time-consuming.
74  // Since removes happen much less frequently than add, then the slow bit should be here.
75  // EJWM
76 
77  // save for debugging purposes
78  int oldSize = maps.m_prepRawDataTrackMap.size();//used in debug output at end.
79 
80  // test caching
81  TrackPrepRawDataMap::iterator itvec = maps.m_trackPrepRawDataMap.find(&track);
82  if (itvec==maps.m_trackPrepRawDataMap.end())
83  {
84  ATH_MSG_ERROR ("Track not found in cache, this should not happen");
85  return StatusCode::FAILURE;
86  }
87 
88  // get all prds on 'track'
89  std::vector< const Trk::PrepRawData* > prds = itvec->second;
90  for (const Trk::PrepRawData* prd : prds)
91  {
92  // now get all map elements (i.e. Tracks) that contain this PRD
94  range = maps.m_prepRawDataTrackMap.equal_range(prd);
95 
96  // get iterators for range
97  ConstPRD_MapIt mapIt = range.first;
98  ConstPRD_MapIt mapItEnd = range.second;
99 
100  // simple for loop instead of fancier remove_if above
101  for ( ;mapIt!=mapItEnd; ++mapIt)
102  {
103  if ( mapIt->second==&track )
104  {
105  maps.m_prepRawDataTrackMap.erase( mapIt );
106  break;//should only ever be one Track
107  }
108  }
109  }
110 
111  // remove cached PRD vector
112  maps.m_trackPrepRawDataMap.erase( itvec );
113 
114  ATH_MSG_DEBUG ("Removed PRDs from track (" <<&track<<") \t- map has changed "<<
115  "size from \t"<<oldSize <<" \tto "<<maps.m_prepRawDataTrackMap.size());
116  return StatusCode::SUCCESS;
117 }
118 
121 {
122  if (!m_prdToTrackMap.key().empty()) throw std::runtime_error("findConnectedTracks not supported when using a PRDtoTrackMap from storegate.");
123  return findConnectedTracks (m_maps, track);
124 }
125 
128  const Trk::Track& track ) const
129 {
130  TrackSet connectedTracks;
131 
132  std::vector< const Trk::PrepRawData* > prds = getPrdsOnTrack(maps, track);
133  for (const Trk::PrepRawData* prd : prds)
134  {
136 
137  // TODO use remove_copy_if instead.
138  for ( ; range.first!=range.second; ++(range.first) )
139  {
140  const Track* conTrack = (range.first)->second;
141  // don't copy this track!
142  if (conTrack!=&track) {
143  // this does actually not allow for double entries
144  connectedTracks.insert(conTrack);
145  ATH_MSG_VERBOSE ("Track "<<&track<<" \tshares PRD "<<prd<<" \twith track:"<<conTrack);
146  }
147  }
148  ATH_MSG_VERBOSE ("Added in connected tracks for PRD:"<<prd<<
149  "\tsize of list now:"<<connectedTracks.size());
150  }
151 
152  return connectedTracks;
153 }
154 
155 
156 std::vector< const Trk::PrepRawData* >
158 {
159  if (!m_prdToTrackMap.key().empty()) throw std::runtime_error("getPrdsOnTrack not supported when using a PRDtoTrackMap from storegate.");
160  return getPrdsOnTrack (m_maps, track);
161 }
162 
163 
164 std::vector< const Trk::PrepRawData* >
166  const Trk::Track& track) const
167 {
168  using PRDs_t = std::vector<const PrepRawData *>;
169 
170  // test caching
171  TrackPrepRawDataMap::const_iterator itvec = maps.m_trackPrepRawDataMap.find(&track);
172  if (itvec!=maps.m_trackPrepRawDataMap.end())
173  {
174  ATH_MSG_VERBOSE ("found track in cache, return cached PRD vector for track");
175  return itvec->second;
176  }
177 
178  if (track.measurementsOnTrack()==nullptr) {
179  ATH_MSG_WARNING ("Track has no RoTs");
180  return {};
181  }
182 
183  PRDs_t vec;
184  vec.reserve(track.measurementsOnTrack()->size());
185  for (const MeasurementBase* meas : *track.measurementsOnTrack())
186  {
187  const RIO_OnTrack* rot = dynamic_cast<const RIO_OnTrack*>(meas);
188  if (rot){
189  if(m_idHelperSvc->isMuon(rot->identify())){
190  //only use precision hits for muon track overlap
191  if(!m_idHelperSvc->isMdt(rot->identify()) && (!m_idHelperSvc->isCsc(rot->identify()) || m_idHelperSvc->measuresPhi(rot->identify()))) continue;
192  }
193  vec.push_back(rot->prepRawData());
194  }
195  else{
196  const Trk::CompetingRIOsOnTrack* competingROT = dynamic_cast <const Trk::CompetingRIOsOnTrack*> (meas);
197  if(competingROT){
198  const unsigned int numROTs = competingROT->numberOfContainedROTs();
199  for( unsigned int i=0;i<numROTs;++i ){
200  const Trk::RIO_OnTrack* rot = &competingROT->rioOnTrack(i);
201  if( !rot || !rot->prepRawData() || !m_idHelperSvc->isMuon(rot->identify()) ) continue;
202  //only use precision hits for muon track overlap
203  if(!m_idHelperSvc->isMdt(rot->identify()) && (!m_idHelperSvc->isCsc(rot->identify()) || m_idHelperSvc->measuresPhi(rot->identify()))) continue;
204  vec.push_back(rot->prepRawData());
205  }
206  }
207  }
208  }
209  ATH_MSG_DEBUG (" Getting "<<vec.size()<<" PRDs from track at:"<<&track);
210 
211  return vec;
212 }
213 
216 {
217  if (!m_prdToTrackMap.key().empty()) {return SG::ReadHandle<Trk::PRDtoTrackMap>(m_prdToTrackMap)->onTracks(prd);}
218  return onTracks (m_maps, prd);
219 }
220 
222 Trk::PRD_AssociationTool::onTracks(const Maps& maps, const PrepRawData& prd) const
223 {
224 // std::pair<IPRD_AssociationTool::PRD_MapIt, IPRD_AssociationTool::PRD_MapIt> range =
225  return maps.m_prepRawDataTrackMap.equal_range(&prd);
226 }
227 
229 {
230  if (!m_prdToTrackMap.key().empty()) throw std::runtime_error("cannot reset PRDtoTrack map when it is taken from storegate.");
231  m_maps.m_prepRawDataTrackMap.clear();
232  m_maps.m_trackPrepRawDataMap.clear();
233 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
Trk::PRD_AssociationTool::onTracks
virtual IPRD_AssociationTool::PrepRawDataTrackMapRange onTracks(const PrepRawData &prd) const override
get the Tracks associated with this Trk::PrepRawData.
Definition: PRD_AssociationTool.cxx:215
TrkDetElementBase.h
CompetingRIOsOnTrack.h
Trk::Track
The ATLAS Track class.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/Track.h:73
Trk::PRDtoTrackMap::onTracks
PrepRawDataTrackMapRange onTracks(const PrepRawData &prd)
get the Tracks associated with this PrepRawData.
SG::ReadHandle< Trk::PRDtoTrackMap >
initialize
void initialize()
Definition: run_EoverP.cxx:894
Trk::PRD_AssociationTool::getPrdsOnTrack
virtual std::vector< const PrepRawData * > getPrdsOnTrack(const Track &track) const override
returns a vector of PRDs belonging to the passed track.
Definition: PRD_AssociationTool.cxx:157
CP::TrackSet
std::set< TrackPtr > TrackSet
Definition: PhysicsAnalysis/AnalysisCommon/IsolationSelection/IsolationSelection/Defs.h:72
Trk::RIO_OnTrack
Definition: RIO_OnTrack.h:70
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
PRD_AssociationTool.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
Track.h
CxxUtils::vec
typename vecDetail::vec_typedef< T, N >::type vec
Define a nice alias for the vectorized type.
Definition: vec.h:207
Trk::CompetingRIOsOnTrack::rioOnTrack
virtual const RIO_OnTrack & rioOnTrack(unsigned int) const =0
returns the RIO_OnTrack (also known as ROT) objects depending on the integer.
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Trk::PRD_AssociationTool::initialize
virtual StatusCode initialize() override
Definition: PRD_AssociationTool.cxx:24
Trk::CompetingRIOsOnTrack::numberOfContainedROTs
virtual unsigned int numberOfContainedROTs() const =0
Number of RIO_OnTracks to be contained by this CompetingRIOsOnTrack.
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
Trk::IPRD_AssociationTool::PrepRawDataTrackMapRange
std::pair< ConstPRD_MapIt, ConstPRD_MapIt > PrepRawDataTrackMapRange
the first element is the beginning iterator of the range, the second is the end
Definition: IPRD_AssociationTool.h:48
Trk::CompetingRIOsOnTrack
Base class for all CompetingRIOsOnTack implementations, extends the common MeasurementBase.
Definition: CompetingRIOsOnTrack.h:64
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
Trk::PRD_AssociationTool::addPRDs
virtual StatusCode addPRDs(const Track &track) override
add the PRDs from this track to the store
Definition: PRD_AssociationTool.cxx:33
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::PRD_AssociationTool::findConnectedTracks
virtual IPRD_AssociationTool::TrackSet findConnectedTracks(const Track &track) const override
returns set of tracks which share PRD with this one
Definition: PRD_AssociationTool.cxx:120
Trk::PRD_AssociationTool::removePRDs
virtual StatusCode removePRDs(Maps &maps, const Track &track) const override
remove the PRDs from this track from maps
Definition: PRD_AssociationTool.cxx:68
Trk::PRD_AssociationTool::reset
virtual void reset() override
resets the tool - should be called before using tool (and maybe afterwards to free up memory)
Definition: PRD_AssociationTool.cxx:228
Trk::PrepRawData
Definition: PrepRawData.h:62
Trk::MeasurementBase
Definition: MeasurementBase.h:58
RIO_OnTrack.h
Trk::RIO_OnTrack::prepRawData
virtual const Trk::PrepRawData * prepRawData() const =0
returns the PrepRawData (also known as RIO) object to which this RIO_OnTrack is associated.
Trk::IPRD_AssociationTool::TrackSet
std::set< const Track * > TrackSet
Definition: IPRD_AssociationTool.h:52
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trk::RIO_OnTrack::identify
Identifier identify() const
return the identifier -extends MeasurementBase
Definition: RIO_OnTrack.h:152
Track
Definition: TriggerChamberClusterOnTrackCreator.h:21
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
Trk::PRD_AssociationTool::PRD_AssociationTool
PRD_AssociationTool(const std::string &, const std::string &, const IInterface *)
Definition: PRD_AssociationTool.cxx:16