ATLAS Offline Software
Loading...
Searching...
No Matches
Tracking/TrkVertexFitter/TrkVertexTools/src/VertexMergingTool.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 <memory>
9#include <vector>
10
11namespace Trk{
12
13 //constructor
14 VertexMergingTool::VertexMergingTool ( const std::string& t, const std::string& n, const IInterface* p )
15 : AthAlgTool ( t,n,p ),
16 m_iVertexFitter("Trk::AdaptiveVertexFitter"),
18 {
19 declareInterface<IVertexMergingTool> ( this );
20 declareProperty("VertexFitterTool", m_iVertexFitter);
21 declareProperty("useBeamConstraint",m_useBeamConstraint);
22 }
23
24 //destructor
26
27//initialize
29 {
30
31 if ( m_iVertexFitter.retrieve().isFailure() ) {
32 msg(MSG::ERROR) << "Failed to retrieve tool " << m_iVertexFitter << endmsg;
33 return StatusCode::FAILURE;
34 }
35
36 ATH_CHECK(m_beamSpotKey.initialize());
37
38 ATH_MSG_DEBUG("Re-merging tool initialization successful");
39 return StatusCode::SUCCESS;
40 }
41
42
43 std::pair<xAOD::VertexContainer*,xAOD::VertexAuxContainer*> VertexMergingTool::mergeVertexContainer(const EventContext& ctx, const xAOD::VertexContainer& MyVxCont) const
44 {
45
46 ATH_MSG_DEBUG("Run vertex remerging");
47
48 //if beamspot constraint was requested, get it now
49 xAOD::Vertex theconstraint;
52 if(not beamSpotHandle.isValid()) ATH_MSG_ERROR("Cannot Retrieve " << m_beamSpotKey.key() );
53 theconstraint = xAOD::Vertex(); // Default constructor creates a private store
54 theconstraint.setPosition( beamSpotHandle->beamVtx().position() );
55 theconstraint.setCovariancePosition( beamSpotHandle->beamVtx().covariancePosition() );
56 theconstraint.setFitQuality( beamSpotHandle->beamVtx().fitQuality().chiSquared(), beamSpotHandle->beamVtx().fitQuality().doubleNumberDoF() );
57 }
58
59 //new output containers to be filled
60 xAOD::VertexContainer *NewContainer = new xAOD::VertexContainer();
62 NewContainer->setStore( auxNewContainer );
63
64 //add remerged flags to all
65 std::vector<bool> remerged( MyVxCont.size(), false );
66
67 xAOD::VertexContainer::const_iterator beginIter = MyVxCont.begin();
68 xAOD::VertexContainer::const_iterator endIter = MyVxCont.end();
69 unsigned int Ni=0;
70 for(xAOD::VertexContainer::const_iterator i = beginIter; i!=endIter; ++i) {
71 auto vx = std::make_unique<xAOD::Vertex>( **i );
72
73 if( vx->vertexType() == xAOD::VxType::NoVtx ) { //dummy vertex -- just add a copy
74 NewContainer->push_back( std::move(vx) );
75 } else if( !remerged[Ni] ) { //skip vertices already merged into another
76
77 unsigned int Nj = Ni+1;
78 for(xAOD::VertexContainer::const_iterator j=i+1; j!=endIter; ++j ) {
79 const xAOD::Vertex * mergeCand = (*j);
80 if( mergeCand->vertexType() != xAOD::VxType::NoVtx && !remerged[Nj] ) {
81 //not dummy and not already merged into earlier vertex, so consider it as merging candidate
82 if( checkCompatibility( vx.get(), mergeCand ) ) {
83
84 //get all the track particles to fit
85 std::vector<const xAOD::TrackParticle*> combinedTracks;
86 for(size_t t=0; t<vx->nTrackParticles(); ++t) {
87 combinedTracks.push_back( vx->trackParticle(t) );
88 }
89 for(size_t t=0; t<mergeCand->nTrackParticles(); ++t) {
90 combinedTracks.push_back( mergeCand->trackParticle(t) );
91 }
92
93 //call the fitter -> using xAOD::TrackParticle it should set the track links for us
94 std::unique_ptr<xAOD::Vertex> mergedVtx;
96 mergedVtx = m_iVertexFitter->fit( ctx, combinedTracks, theconstraint );
97 } else {
98 //no interface for no constraint and no starting point, so use starting point of original vertex
99 const Amg::Vector3D& start( vx->position() );
100 mergedVtx = m_iVertexFitter->fit( ctx, combinedTracks, start );
101 }
102
103 //CHECK MERGING IS GOOD?
104 // flag as remerged
105 ATH_MSG_DEBUG("Merge vertices " << Ni << " and " << Nj);
106 remerged[Nj] = true;
107 remerged[Ni] = true;
108 //overwrite with merged vertex
109 vx = std::move(mergedVtx);
110 }
111 }
112
113 Nj++;
114 } //loop over j
115
116 //whether we merged or not, can add vx to the container
117 NewContainer->push_back( std::move(vx) );
118
119 } //end if vtx[Ni] is not remerged
120 Ni++;
121 }
122
123 return std::make_pair( NewContainer, auxNewContainer );
124
125 }
126
127 bool VertexMergingTool::checkCompatibility( const xAOD::Vertex * vx1, const xAOD::Vertex * vx2 ) const {
128
129 double z1 = vx1->z();
130 double z2 = vx2->z();
131
132 double err2_z1 = vx1->covariancePosition()(Trk::z, Trk::z);
133 double err2_z2 = vx2->covariancePosition()(Trk::z, Trk::z);
134
135 double sigmaZ = (fabs(z1-z2))/(sqrt(err2_z1+err2_z2));
136
137 ATH_MSG_DEBUG("z1 = " << z1 << ", z2 = " << z2 << ", error = " << sqrt( err2_z1+err2_z2 ) );
138
139 return sigmaZ<3;
140 }
141
142}
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_DEBUG(x)
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)
MsgStream & msg() const
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
value_type push_back(value_type pElem)
Add an element to the end of the collection.
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 std::pair< xAOD::VertexContainer *, xAOD::VertexAuxContainer * > mergeVertexContainer(const EventContext &ctx, const xAOD::VertexContainer &MyVxCont) const override
Merging.
bool checkCompatibility(const xAOD::Vertex *vx1, const xAOD::Vertex *vx2) const
virtual ~VertexMergingTool()
destructor
VertexMergingTool(const std::string &t, const std::string &n, const IInterface *p)
constructor
float z() const
Returns the z position.
size_t nTrackParticles() const
Get the number of tracks associated with this vertex.
void setCovariancePosition(const AmgSymMatrix(3)&covariancePosition)
Sets the vertex covariance matrix.
const TrackParticle * trackParticle(size_t i) const
Get the pointer to a given track that was used in vertex reco.
void setPosition(const Amg::Vector3D &position)
Sets the 3-position.
VxType::VertexType vertexType() const
The type of the vertex.
void setFitQuality(float chiSquared, float numberDoF)
Set the 'Fit Quality' information.
Eigen::Matrix< double, 3, 1 > Vector3D
Ensure that the ATLAS eigen extensions are properly loaded.
@ z
global position (cartesian)
Definition ParamDefs.h:57
@ NoVtx
Dummy vertex. TrackParticle was not used in vertex fit.
VertexAuxContainer_v1 VertexAuxContainer
Definition of the current jet auxiliary container.
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.