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-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
8#include <vector>
9
10namespace Trk{
11
12 //constructor
13 VertexMergingTool::VertexMergingTool ( const std::string& t, const std::string& n, const IInterface* p )
14 : AthAlgTool ( t,n,p ),
15 m_iVertexFitter("Trk::AdaptiveVertexFitter"),
17 {
18 declareInterface<IVertexMergingTool> ( this );
19 declareProperty("VertexFitterTool", m_iVertexFitter);
20 declareProperty("useBeamConstraint",m_useBeamConstraint);
21 }
22
23 //destructor
25
26//initialize
28 {
29
30 if ( m_iVertexFitter.retrieve().isFailure() ) {
31 msg(MSG::ERROR) << "Failed to retrieve tool " << m_iVertexFitter << endmsg;
32 return StatusCode::FAILURE;
33 }
34
35 ATH_CHECK(m_beamSpotKey.initialize());
36
37 ATH_MSG_DEBUG("Re-merging tool initialization successful");
38 return StatusCode::SUCCESS;
39 }
40
41
42 std::pair<xAOD::VertexContainer*,xAOD::VertexAuxContainer*> VertexMergingTool::mergeVertexContainer(const xAOD::VertexContainer& MyVxCont) const
43 {
44
45 ATH_MSG_DEBUG("Run vertex remerging");
46
47 //if beamspot constraint was requested, get it now
48 xAOD::Vertex theconstraint;
51 if(not beamSpotHandle.isValid()) ATH_MSG_ERROR("Cannot Retrieve " << m_beamSpotKey.key() );
52 theconstraint = xAOD::Vertex(); // Default constructor creates a private store
53 theconstraint.setPosition( beamSpotHandle->beamVtx().position() );
54 theconstraint.setCovariancePosition( beamSpotHandle->beamVtx().covariancePosition() );
55 theconstraint.setFitQuality( beamSpotHandle->beamVtx().fitQuality().chiSquared(), beamSpotHandle->beamVtx().fitQuality().doubleNumberDoF() );
56 }
57
58 //new output containers to be filled
59 xAOD::VertexContainer *NewContainer = new xAOD::VertexContainer();
61 NewContainer->setStore( auxNewContainer );
62
63 //add remerged flags to all
64 std::vector<bool> remerged( MyVxCont.size(), false );
65
66 xAOD::VertexContainer::const_iterator beginIter = MyVxCont.begin();
67 xAOD::VertexContainer::const_iterator endIter = MyVxCont.end();
68 unsigned int Ni=0;
69 for(xAOD::VertexContainer::const_iterator i = beginIter; i!=endIter; ++i) {
70 xAOD::Vertex * vx = new xAOD::Vertex( **i );
71
72 if( vx->vertexType() == xAOD::VxType::NoVtx ) { //dummy vertex -- just add a copy
73 NewContainer->push_back( vx );
74 } else if( !remerged[Ni] ) { //skip vertices already merged into another
75
76 unsigned int Nj = Ni+1;
77 for(xAOD::VertexContainer::const_iterator j=i+1; j!=endIter; ++j ) {
78 const xAOD::Vertex * mergeCand = (*j);
79 if( mergeCand->vertexType() != xAOD::VxType::NoVtx && !remerged[Nj] ) {
80 //not dummy and not already merged into earlier vertex, so consider it as merging candidate
81 if( checkCompatibility( vx, mergeCand ) ) {
82
83 //get all the track particles to fit
84 std::vector<const xAOD::TrackParticle*> combinedTracks;
85 for(size_t t=0; t<vx->nTrackParticles(); ++t) {
86 combinedTracks.push_back( vx->trackParticle(t) );
87 }
88 for(size_t t=0; t<mergeCand->nTrackParticles(); ++t) {
89 combinedTracks.push_back( mergeCand->trackParticle(t) );
90 }
91
92 //call the fitter -> using xAOD::TrackParticle it should set the track links for us
93 xAOD::Vertex * mergedVtx = nullptr;
95 mergedVtx = m_iVertexFitter->fit( combinedTracks, theconstraint );
96 } else {
97 //no interface for no constraint and no starting point, so use starting point of original vertex
98 const Amg::Vector3D& start( vx->position() );
99 mergedVtx = m_iVertexFitter->fit( combinedTracks, start );
100 }
101
102 //CHECK MERGING IS GOOD?
103 // flag as remerged
104 ATH_MSG_DEBUG("Merge vertices " << Ni << " and " << Nj);
105 remerged[Nj] = true;
106 remerged[Ni] = true;
107 //delete copy of first vertex and then overwrite with merged vertex
108 delete vx;
109 vx = 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( 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 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.
const Amg::Vector3D & position() const
Returns the 3-pos.
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.