ATLAS Offline Software
Loading...
Searching...
No Matches
PrimaryVertexReFitter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Local
8
9// Athena
11#include "xAODMuon/Muon.h"
12#include "xAODEgamma/Electron.h"
16
17namespace Prompt {
18//======================================================================================================
19PrimaryVertexReFitter::PrimaryVertexReFitter(const std::string& name, ISvcLocator *pSvcLocator):
20 AthAlgorithm (name, pSvcLocator)
21{}
22
23//=============================================================================
25{
26 ANA_MSG_DEBUG("ReFitPriVtxName = " << m_reFitPrimaryVertexKey);
27 ANA_MSG_DEBUG("LeptonContainerName = " << m_leptonContainerKey);
28 ANA_MSG_DEBUG("PriVertexContainerName = " << m_primaryVertexContainerKey);
29
30 ANA_MSG_DEBUG("PrintTime = " << m_printTime);
31
32 ANA_MSG_DEBUG("DistToRefittedPriVtxName = " << m_distToRefittedPriVtxName);
33 ANA_MSG_DEBUG("NormDistToRefittedPriVtxName = " << m_normDistToRefittedPriVtxName);
34 ANA_MSG_DEBUG("RefittedVtxWithoutLeptonLinkName = " << m_lepRefittedVtxWithoutLeptonLinkName.key());
35
36 ATH_CHECK(m_inDetTracksKey.initialize());
37
38 ATH_CHECK(m_leptonContainerKey.initialize());
40
42
43 //
44 // Must have non-empty container name for refitted primary vertex with/without lepton
45 //
46 if(m_reFitPrimaryVertexKey.empty()) {
47 ATH_MSG_FATAL("initialize - SecVtx container invalid name: \"" << m_reFitPrimaryVertexKey << "\"");
48 return StatusCode::FAILURE;
49 }
50
54
55 ATH_CHECK(m_vertexFitterTool.retrieve());
56
57 if(m_printTime) {
58 //
59 // Reset timers
60 //
61 m_timerAll .Reset();
62 m_timerExec.Reset();
63
64 //
65 // Start full timer
66 //
67 m_timerAll.Start();
68 }
69
70 return StatusCode::SUCCESS;
71}
72
73//=============================================================================
75{
76 if(m_printTime) {
77 //
78 // Print full time stopwatch
79 //
80 m_timerAll.Stop();
81
82 ATH_MSG_INFO("Real time: " << m_timerAll.RealTime() << "\t CPU time: " << m_timerAll.CpuTime());
83 ATH_MSG_INFO("Execute time: " << PrintResetStopWatch(m_timerExec));
84 }
85
86 return StatusCode::SUCCESS;
87}
88
89//=============================================================================
90StatusCode PrimaryVertexReFitter::execute(const EventContext& ctx)
91{
92
93 //
94 // Start execute timer
95 //
97
98 //
99 // Find Inner Detector tracks
100 //
102 if (!h_inDetTracks.isValid()){
103 ATH_MSG_FATAL("execute - failed to find the InDetTrackParticles");
104 return StatusCode::FAILURE;
105 }
106
107 const xAOD::TrackParticleContainer inDetTracks = *h_inDetTracks;
108
109 //
110 // Create output vertex collections and record them immediately
111 // in StoreGate for memory management
112 //
113 std::unique_ptr<xAOD::VertexContainer> refitVtxContainer = std::make_unique< xAOD::VertexContainer>();
114 std::unique_ptr<xAOD::VertexAuxContainer> refitVtxContainerAux = std::make_unique< xAOD::VertexAuxContainer>();
115
116 refitVtxContainer->setStore(refitVtxContainerAux.get());
117
119 ATH_CHECK(h_refitVtxContainer.record(
120 std::move(refitVtxContainer), std::move(refitVtxContainerAux)
121 ));
122 xAOD::VertexContainer &refitVtxContainerRef = *h_refitVtxContainer;
123
124 //
125 // Retrieve containers from evtStore
126 //
128 if (!h_leptonContainer.isValid()){
129 ATH_MSG_FATAL("execute - failed to find the lepton container");
130 return StatusCode::FAILURE;
131 }
132
134 if (!h_vertices.isValid()){
135 ATH_MSG_FATAL("execute - failed to find the vertices");
136 return StatusCode::FAILURE;
137 }
138
139 const xAOD::IParticleContainer leptonContainer = *h_leptonContainer;
140 const xAOD::VertexContainer vertices = *h_vertices;
141
142 Prompt::FittingInput fittingInput(&inDetTracks, 0, 0);
143
144 for(const xAOD::Vertex *vertex: vertices) {
145 if(vertex->vertexType() == 1) {
146 fittingInput.priVtx = dynamic_cast<const xAOD::Vertex*>(vertex);
147 break;
148 }
149 }
150
151 if(!fittingInput.priVtx) {
152 ATH_MSG_INFO("Failed to find primary vertices - save empty containers");
153 return StatusCode::SUCCESS;
154 }
155
156 //
157 // Collect tracks used for primary vertex fit
158 //
159 std::vector<const xAOD::TrackParticle *> priVtx_tracks;
160 priVtx_tracks.reserve(fittingInput.priVtx->nTrackParticles());
161
162 for(unsigned k = 0; k < fittingInput.priVtx->nTrackParticles(); ++k) {
163 const xAOD::TrackParticle *track = fittingInput.priVtx->trackParticle(k);
164
165 if(track) {
166 priVtx_tracks.push_back(track);
167 }
168 }
169
170 // Refit primary vertex
171 std::unique_ptr<xAOD::Vertex> refittedPriVtx = m_vertexFitterTool->fitVertexWithSeed(
172 ctx, fittingInput, priVtx_tracks,
173 fittingInput.priVtx->position(),
175 );
176
177 if(!refittedPriVtx) {
178 ATH_MSG_WARNING("Failed to refit primary vertex - save empty containers");
179 return StatusCode::SUCCESS;
180 }
181
182 //
183 // Save refitted primary vertex for fitting service
184 //
185 fittingInput.refittedPriVtx = refittedPriVtx.get();
186
187 ATH_MSG_DEBUG("execute -- primary vertex NTrack = " << fittingInput.priVtx ->nTrackParticles());
188 ATH_MSG_DEBUG("execute -- refitted primary vertex NTrack = " << fittingInput.refittedPriVtx->nTrackParticles());
189
190 //
191 // Dynamic cast IParticle container to electron or muon container
192 //
193 ATH_MSG_DEBUG("======================================="
194 << "\n\t\t\t Size of lepton container: " << leptonContainer.size()
195 << "\n-----------------------------------------------------------------");
196
198 lepRefittedRMVtxLinkDec (m_lepRefittedVtxWithoutLeptonLinkName, ctx);
199
200 DataLink<xAOD::VertexContainer> refitVtxContainerLink (refitVtxContainerRef, ctx);
201 for(const xAOD::IParticle *lepton: leptonContainer) {
202 const xAOD::TrackParticle *tracklep = 0;
203 const xAOD::Electron *elec = dynamic_cast<const xAOD::Electron*>(lepton);
204 const xAOD::Muon *muon = dynamic_cast<const xAOD::Muon*>(lepton);
205
206 if(elec) {
207 //
208 // get GSF track
209 //
210 const xAOD::TrackParticle *bestmatchedGSFElTrack=elec->trackParticle(0);
211
212 //
213 // get origin ID track for later study
214 //
215 tracklep = xAOD::EgammaHelpers::getOriginalTrackParticleFromGSF(bestmatchedGSFElTrack);
216 }
217 else if(muon) {
218 tracklep = muon->trackParticle(xAOD::Muon::TrackParticleType::InnerDetectorTrackParticle);
219 }
220
221 if(!tracklep) {
222 ATH_MSG_WARNING("PrimaryVertexReFitter::execute - cannot find muon->trackParticle(xAOD::Muon::TrackParticleType::InnerDetectorTrackParticle) nor electron->trackParticle()");
223 continue;
224 }
225
226 if (decorateLepWithReFitPrimaryVertex(ctx, fittingInput, tracklep, priVtx_tracks, refitVtxContainerRef))
227 {
228 lepRefittedRMVtxLinkDec(*lepton) = ElementLink<xAOD::VertexContainer>(refitVtxContainerLink, refitVtxContainerRef.size()-1);
229 }
230 }
231
232 h_refitVtxContainer->push_back(std::move(refittedPriVtx));
233
234 ATH_MSG_DEBUG("SV Vertex container " << m_reFitPrimaryVertexKey << " recorded in store");
235
236 ATH_MSG_DEBUG("execute - all done");
237 ATH_MSG_DEBUG("=======================================");
238
239 return StatusCode::SUCCESS;
240}
241
242//=============================================================================
244 const EventContext& ctx,
245 const FittingInput &input,
246 const xAOD::TrackParticle* tracklep,
247 const std::vector<const xAOD::TrackParticle*> &tracks,
248 xAOD::VertexContainer &refitVtxContainer)
249{
250 //
251 // Check if the lepton track has been used for primary vertex reconstruction.
252 // if true, then remove the lepton track from the input track list, re-fit primary vertex again.
253 // Save the ElementLink of the re-fit primary vertex to the lepton
254 //
255 if(!input.priVtx) {
256 ATH_MSG_WARNING("decorateLepWithReFitPrimaryVertex - invalid input primary vertex pointer");
257 return false;
258 }
259
260 //--------------------------------------------------------
261 // Remove the lepton track from the track list
262 // get re-fitted non-prompt primary vertex
263 //
264 std::vector<const xAOD::TrackParticle*> priVtx_tracks_pass;
265 bool isRefit = false;
266
267 for(const xAOD::TrackParticle *track: tracks) {
268 if(track == tracklep) {
269 isRefit = true;
270 ATH_MSG_DEBUG("decorateLepWithReFitPrimaryVertex -- lepton has been used, lepton pT =" << tracklep->pt() << ", track pT =" << track->pt());
271 continue;
272 }
273
274 priVtx_tracks_pass.push_back(track);
275 }
276
277 if(!isRefit) {
278 ATH_MSG_DEBUG("decorateLepWithReFitPrimaryVertex -- Skip the primary vertex without lepton track");
279
280 return false;
281 }
282
283 if(priVtx_tracks_pass.size() < 2) {
284 ATH_MSG_DEBUG("decorateLepWithReFitPrimaryVertex -- Skip the primary vertex refitting: N tracks =" << priVtx_tracks_pass.size());
285
286 return false;
287 }
288
289 std::unique_ptr<xAOD::Vertex> refittedVtxRMLep = m_vertexFitterTool->fitVertexWithSeed(
290 ctx, input, priVtx_tracks_pass, input.priVtx->position(),
292
293 if(refittedVtxRMLep) {
294 ATH_MSG_DEBUG("decorateLepWithReFitPrimaryVertex -- save refitted non-prompt primary vertex with NTrack = " << refittedVtxRMLep->nTrackParticles());
295
296 if(input.refittedPriVtx) {
297 (*m_distToRefittedPriVtx) (*refittedVtxRMLep) = Prompt::getDistance(input.refittedPriVtx->position(), refittedVtxRMLep->position());
298 (*m_normdistToRefittedPriVtx)(*refittedVtxRMLep) = Prompt::getNormDist(
299 input.refittedPriVtx->position(),
300 refittedVtxRMLep->position(),
301 refittedVtxRMLep->covariance(),
302 msg(MSG::WARNING)
303 );
304 }
305 //
306 // Record vertex with output container
307 //
308 refitVtxContainer.push_back(std::move(refittedVtxRMLep));
309
310 return true;
311 }
312 return false;
313}
314}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Handle class for adding a decoration to an object.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
value_type push_back(value_type pElem)
Add an element to the end of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
SG::WriteDecorHandleKey< xAOD::IParticleContainer > m_lepRefittedVtxWithoutLeptonLinkName
SG::ReadHandleKey< xAOD::VertexContainer > m_primaryVertexContainerKey
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
SG::ReadHandleKey< xAOD::IParticleContainer > m_leptonContainerKey
std::optional< accessorFloat_t > m_normdistToRefittedPriVtx
SG::WriteHandleKey< xAOD::VertexContainer > m_reFitPrimaryVertexKey
std::optional< accessorFloat_t > m_distToRefittedPriVtx
virtual StatusCode finalize() override
PrimaryVertexReFitter(const std::string &name, ISvcLocator *pSvcLocator)
Gaudi::Property< std::string > m_distToRefittedPriVtxName
Gaudi::Property< bool > m_printTime
virtual StatusCode initialize() override
Gaudi::Property< std::string > m_normDistToRefittedPriVtxName
bool decorateLepWithReFitPrimaryVertex(const EventContext &ctx, const FittingInput &input, const xAOD::TrackParticle *tracklep, const std::vector< const xAOD::TrackParticle * > &tracks, xAOD::VertexContainer &refitVtxContainer)
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_inDetTracksKey
ToolHandle< Prompt::VertexFittingTool > m_vertexFitterTool
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Handle class for adding a decoration to an object.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
const xAOD::TrackParticle * trackParticle(size_t index=0) const
Pointer to the xAOD::TrackParticle/s that match the electron candidate.
Class providing the definition of the 4-vector interface.
virtual double pt() const override final
The transverse momentum ( ) of the particle.
size_t nTrackParticles() const
Get the number of tracks associated with this vertex.
const TrackParticle * trackParticle(size_t i) const
Get the pointer to a given track that was used in vertex reco.
const Amg::Vector3D & position() const
Returns the 3-pos.
std::string PrintResetStopWatch(TStopwatch &watch)
@ kRefittedPriVtxWithoutLep
double getNormDist(const Amg::Vector3D &PrimVtx, const Amg::Vector3D &SecVtx, const std::vector< float > &ErrorMatrix, MsgStream &msg)
double getDistance(const xAOD::Vertex *vtx1, const xAOD::Vertex *vtx2)
const xAOD::TrackParticle * getOriginalTrackParticleFromGSF(const xAOD::TrackParticle *trkPar)
Helper function for getting the "Original" Track Particle (i.e before GSF) via the GSF Track Particle...
TrackParticle_v1 TrackParticle
Reference the current persistent version:
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
Muon_v1 Muon
Reference the current persistent version:
Electron_v1 Electron
Definition of the current "egamma version".
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.
const xAOD::Vertex * refittedPriVtx
const xAOD::Vertex * priVtx