ATLAS Offline Software
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 
10 namespace 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"),
16  m_useBeamConstraint(false)
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 
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;
49  if (m_useBeamConstraint) {
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();
60  xAOD::VertexAuxContainer* auxNewContainer = new xAOD::VertexAuxContainer();
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 }
xAOD::Vertex_v1::setPosition
void setPosition(const Amg::Vector3D &position)
Sets the 3-position.
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
xAOD::Vertex_v1::setFitQuality
void setFitQuality(float chiSquared, float numberDoF)
Set the 'Fit Quality' information.
Definition: Vertex_v1.cxx:150
xAOD::Vertex_v1::nTrackParticles
size_t nTrackParticles() const
Get the number of tracks associated with this vertex.
Definition: Vertex_v1.cxx:270
xAOD::VertexAuxContainer_v1
Temporary container used until we have I/O for AuxStoreInternal.
Definition: VertexAuxContainer_v1.h:32
Trk::z
@ z
global position (cartesian)
Definition: ParamDefs.h:57
beamspotman.sigmaZ
sigmaZ
Definition: beamspotman.py:1625
Trk::VertexMergingTool::mergeVertexContainer
virtual std::pair< xAOD::VertexContainer *, xAOD::VertexAuxContainer * > mergeVertexContainer(const xAOD::VertexContainer &MyVxCont) const override
Merging
Definition: Tracking/TrkVertexFitter/TrkVertexTools/src/VertexMergingTool.cxx:42
xAOD::Vertex
Vertex_v1 Vertex
Define the latest version of the vertex class.
Definition: Event/xAOD/xAODTracking/xAODTracking/Vertex.h:16
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
VertexMergingTool.h
xAOD::Vertex_v1::position
const Amg::Vector3D & position() const
Returns the 3-pos.
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
Trk::VertexMergingTool::VertexMergingTool
VertexMergingTool(const std::string &t, const std::string &n, const IInterface *p)
constructor
Definition: Tracking/TrkVertexFitter/TrkVertexTools/src/VertexMergingTool.cxx:13
xAOD::VertexContainer
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Definition: VertexContainer.h:14
IVertexWeightCalculator.h
xAOD::Vertex_v1::vertexType
VxType::VertexType vertexType() const
The type of the vertex.
xAOD::VxType::NoVtx
@ NoVtx
Dummy vertex. TrackParticle was not used in vertex fit.
Definition: TrackingPrimitives.h:570
Trk::VertexMergingTool::m_beamSpotKey
SG::ReadCondHandleKey< InDet::BeamSpotData > m_beamSpotKey
Definition: Tracking/TrkVertexFitter/TrkVertexTools/TrkVertexTools/VertexMergingTool.h:64
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
xAOD::VertexAuxContainer
VertexAuxContainer_v1 VertexAuxContainer
Definition of the current jet auxiliary container.
Definition: VertexAuxContainer.h:19
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
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
VxTrackAtVertex.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAOD::Vertex_v1::trackParticle
const TrackParticle * trackParticle(size_t i) const
Get the pointer to a given track that was used in vertex reco.
Definition: Vertex_v1.cxx:249
xAOD::Vertex_v1::z
float z() const
Returns the z position.
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
Trk::VertexMergingTool::checkCompatibility
bool checkCompatibility(const xAOD::Vertex *vx1, const xAOD::Vertex *vx2) const
Definition: Tracking/TrkVertexFitter/TrkVertexTools/src/VertexMergingTool.cxx:127
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
Trk::VertexMergingTool::initialize
StatusCode initialize() override
Definition: Tracking/TrkVertexFitter/TrkVertexTools/src/VertexMergingTool.cxx:27
Trk::VertexMergingTool::m_useBeamConstraint
bool m_useBeamConstraint
Definition: Tracking/TrkVertexFitter/TrkVertexTools/TrkVertexTools/VertexMergingTool.h:66
AthCommonMsg< AlgTool >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
Trk::VertexMergingTool::~VertexMergingTool
virtual ~VertexMergingTool()
destructor
AthAlgTool
Definition: AthAlgTool.h:26
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
xAOD::Vertex_v1::setCovariancePosition
void setCovariancePosition(const AmgSymMatrix(3)&covariancePosition)
Sets the vertex covariance matrix.
Trk::VertexMergingTool::m_iVertexFitter
ToolHandle< Trk::IVertexFitter > m_iVertexFitter
Definition: Tracking/TrkVertexFitter/TrkVertexTools/TrkVertexTools/VertexMergingTool.h:65
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.