ATLAS Offline Software
Reconstruction/Jet/JetRecTools/Root/TrackVertexAssociationTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
8 #include <cmath>
9 
11  : AsgTool(t)
12  , m_tvaTool("",this) // default as empty
13  , m_transDistMax(10e6)
14  , m_longDistMax(10e6)
15  , m_maxZ0SinTheta(10e6)
16 {
17  declareProperty("TrackVertexAssoTool", m_tvaTool); // Tool handle
18 
19  declareProperty("MaxTransverseDistance",m_transDistMax);
20  declareProperty("MaxLongitudinalDistance",m_longDistMax);
21  declareProperty("MaxZ0SinTheta", m_maxZ0SinTheta);
22 
23  declareProperty("TrackParticleContainer",m_trackContainer_key);
24  declareProperty("VertexContainer",m_vertexContainer_key);
25  declareProperty("TrackVertexAssociation",m_tva_key);
26 
27 }
28 
30  ATH_MSG_INFO("Initializing tool " << name() << "...");
31  ATH_MSG_DEBUG("initializing version with data handles");
32 
34  ATH_CHECK(m_vertexContainer_key.initialize());
36 
37  if(! m_tvaTool.empty() ) {
38  ATH_MSG_INFO("Intialized using ITrackVertexAssociationTool");
39  return m_tvaTool.retrieve();
40  }
41 
42  ATH_MSG_INFO("Intialized using custom track-vertex association");
43  return StatusCode::SUCCESS;
44 }
45 
47  // Get input track collection
48 
49  auto handle_tracks = SG::makeHandle(m_trackContainer_key);
50 
51  if(!handle_tracks.isValid()){
52  ATH_MSG_ERROR("Error retrieving TrackParticleContainer from evtStore: "
54  return 1;
55  }
56 
57  const auto *trackContainer = handle_tracks.cptr();
58 
59 
60  // If this is a view container, then we assume that it contains elements from only one owning container
61  if ( trackContainer->ownPolicy() != SG::OWN_ELEMENTS ) {
62  bool oneOwningContainer(true);
63  for(const xAOD::TrackParticle* track : *trackContainer) {
64  if(track->container() != trackContainer->front()->container()) {
65  oneOwningContainer=false;
66  break;
67  }
68  }
69  if(!oneOwningContainer) {
70  ATH_MSG_ERROR("Track view container holds track from multiple owning containers.");
71  ATH_MSG_ERROR("Problem is this container: " << m_trackContainer_key.key());
72  return 11;
73  }
74  }
75 
76  // Get input vertex collection
77  auto handle_vert = SG::makeHandle(m_vertexContainer_key);
78 
79  if(!handle_vert.isValid()){
80  ATH_MSG_ERROR("Error retrieving VertexContainer from evtStore: "
81  << m_vertexContainer_key.key());
82  return 2;
83  }
84 
85  const auto *vertexContainer = handle_vert.cptr();
86 
87  auto useCustom = m_tvaTool.empty();
88  auto tva = makeTrackVertexAssociation(trackContainer,
89  vertexContainer,
90  useCustom);
91  // Check the result.
92  // if ( tva == nullptr ) {
93  if (!tva){
94  ATH_MSG_ERROR("Could not build the TrackVertexAssociation");
95  return 3;
96  }
97 
98  // Store it
99 
100  auto handle_tva = SG::makeHandle(m_tva_key);
101  if(!handle_tva.record(std::move(tva))){
102  ATH_MSG_ERROR("Unable to write new TrackVertexAssociation to evtStore: "
103  << m_tva_key.key());
104  return 4;
105  }
106 
107  // Success
108  ATH_MSG_DEBUG("Wrote new TrackVertexAssociation to evtStore: "
109  << m_tva_key.key());
110  return 0;
111 }
112 
113 
114 
115 
117 // jet::TrackVertexAssociation*
118 std::unique_ptr<jet::TrackVertexAssociation>
120 {
121 
122  ATH_MSG_DEBUG("Building track-vertex association USING InDet tool. trk size="<< trackContainer->size()
123  << " vtx size="<< vertexContainer->size());
124 
125  auto tva = std::make_unique<jet::TrackVertexAssociation>(trackContainer);
126 
127  std::vector<const xAOD::Vertex*> vecVert;
128  vecVert.assign(vertexContainer->begin(), vertexContainer->end());
129 
130  for( const xAOD::TrackParticle* track : *trackContainer) {
131 
132  const xAOD::Vertex * v = m_tvaTool->getUniqueMatchVertex(*track, vecVert) ;
133  tva->associate( track, v );
134  }
135  return tva;
136 }
137 
139 // jet::TrackVertexAssociation*
140 std::unique_ptr<jet::TrackVertexAssociation>
142 {
143  ATH_MSG_DEBUG("Building track-vertex association trk size="<< trackContainer->size()
144  << " vtx size="<< vertexContainer->size());
145 
146  auto tva = std::make_unique<jet::TrackVertexAssociation>(trackContainer);
147 
148  for (size_t iTrack = 0; iTrack < trackContainer->size(); ++iTrack)
149  {
150  const xAOD::TrackParticle* track = trackContainer->at(iTrack);
151 
152  // Apply track transverse distance cut
153  const float transverseDistance = track->d0();//perigeeParameters().parameters()[Trk::d0];
154  if (transverseDistance > m_transDistMax) continue;
155 
156 
157  // Get track longitudinal distance offset
158  const float longitudinalDistance = track->z0()+track->vz();
159 
160  double sinTheta = std::sin(track->theta());
161 
162  // For each track, find the vertex with highest sum pt^2 within z0 cut
163  size_t matchedIndex = 0;
164  bool foundMatch = false;
165  for (size_t iVertex = 0; iVertex < vertexContainer->size(); ++iVertex)
166  {
167  const xAOD::Vertex* vertex = vertexContainer->at(iVertex);
168 
169  double deltaz = longitudinalDistance - vertex->z();
170 
171  // Check longitudinal distance between track and vertex
172  if ( fabs(deltaz) > m_longDistMax)
173  continue;
174 
175  // Check z0*sinThetha between track and vertex
176  if (fabs(deltaz*sinTheta) > m_maxZ0SinTheta)
177  continue;
178 
179  // If it passed the cuts, then this is the vertex we want
180  // This does make the assumption that the container is sorted in sum pT^2 order
181  foundMatch = true;
182  matchedIndex = iVertex;
183  break;
184  }
185 
186  // If we matched a vertex, then associate that vertex to the track
187  if (foundMatch)
188  tva->associate(trackContainer->at(iTrack),vertexContainer->at(matchedIndex));
189  }
190 
191 
192  // Return the TVA object
193  return tva;
194 }
195 
197 using PVC = const xAOD::VertexContainer*;
198 std::unique_ptr<jet::TrackVertexAssociation>
200  PVC vertexContainer,
201  bool useCustom) const {
202  if (useCustom) {
203  return buildTrackVertexAssociation_custom(trackContainer,
204  vertexContainer);
205  } else {
206  return buildTrackVertexAssociation_withTool(trackContainer,
207  vertexContainer);
208  }
209 }
WriteHandle.h
Handle class for recording to StoreGate.
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TrackVertexAssociationTool::m_longDistMax
float m_longDistMax
Definition: Reconstruction/Jet/JetRecTools/JetRecTools/TrackVertexAssociationTool.h:80
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
TrackVertexAssociationTool::buildTrackVertexAssociation_custom
std::unique_ptr< jet::TrackVertexAssociation > buildTrackVertexAssociation_custom(const xAOD::TrackParticleContainer *, const xAOD::VertexContainer *) const
Definition: Reconstruction/Jet/JetRecTools/Root/TrackVertexAssociationTool.cxx:141
TrackVertexAssociationTool::makeTrackVertexAssociation
std::unique_ptr< jet::TrackVertexAssociation > makeTrackVertexAssociation(const xAOD::TrackParticleContainer *, const xAOD::VertexContainer *, bool useCustom) const
Definition: Reconstruction/Jet/JetRecTools/Root/TrackVertexAssociationTool.cxx:199
TrackVertexAssociationTool::m_tvaTool
ToolHandle< CP::ITrackVertexAssociationTool > m_tvaTool
Definition: Reconstruction/Jet/JetRecTools/JetRecTools/TrackVertexAssociationTool.h:75
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:269
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TrackVertexAssociationTool::m_tva_key
SG::WriteHandleKey< jet::TrackVertexAssociation > m_tva_key
Definition: Reconstruction/Jet/JetRecTools/JetRecTools/TrackVertexAssociationTool.h:85
SG::OWN_ELEMENTS
@ OWN_ELEMENTS
this data object owns its elements
Definition: OwnershipPolicy.h:17
TrackVertexAssociationTool::m_vertexContainer_key
SG::ReadHandleKey< xAOD::VertexContainer > m_vertexContainer_key
Definition: Reconstruction/Jet/JetRecTools/JetRecTools/TrackVertexAssociationTool.h:84
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TrackVertexAssociationTool::m_maxZ0SinTheta
float m_maxZ0SinTheta
Definition: Reconstruction/Jet/JetRecTools/JetRecTools/TrackVertexAssociationTool.h:81
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TrackVertexAssociationTool::buildTrackVertexAssociation_withTool
std::unique_ptr< jet::TrackVertexAssociation > buildTrackVertexAssociation_withTool(const xAOD::TrackParticleContainer *, const xAOD::VertexContainer *) const
Definition: Reconstruction/Jet/JetRecTools/Root/TrackVertexAssociationTool.cxx:119
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TrackVertexAssociationTool::m_trackContainer_key
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trackContainer_key
Definition: Reconstruction/Jet/JetRecTools/JetRecTools/TrackVertexAssociationTool.h:83
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DataVector< xAOD::TrackParticle_v1 >
TrackVertexAssociationTool::execute
int execute() const
Method to be called for each event.
Definition: Reconstruction/Jet/JetRecTools/Root/TrackVertexAssociationTool.cxx:46
TrackVertexAssociationTool.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
ReadHandle.h
Handle class for reading from StoreGate.
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
python.PyAthena.v
v
Definition: PyAthena.py:157
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
TrackVertexAssociationTool::m_transDistMax
float m_transDistMax
Definition: Reconstruction/Jet/JetRecTools/JetRecTools/TrackVertexAssociationTool.h:79
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
TrackVertexAssociationTool::initialize
StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: Reconstruction/Jet/JetRecTools/Root/TrackVertexAssociationTool.cxx:29
TrackVertexAssociationTool::TrackVertexAssociationTool
TrackVertexAssociationTool(const std::string &t)
Definition: Reconstruction/Jet/JetRecTools/Root/TrackVertexAssociationTool.cxx:10