ATLAS Offline Software
Loading...
Searching...
No Matches
VertexDecoratorAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
13#include <TLorentzVector.h>
14#include <cmath>
15
16
18{
19 VertexDecoratorAlg ::VertexDecoratorAlg(const std::string &name,
20 ISvcLocator *pSvcLocator)
21 : AthHistogramAlgorithm (name, pSvcLocator)
22 {
23 }
24
25 StatusCode VertexDecoratorAlg ::initialize()
26 {
27 ATH_CHECK(m_vertexInKey.initialize());
28 ATH_CHECK(m_eventInKey.initialize());
29 ATH_CHECK(m_photonsInKey.initialize());
30 ATH_CHECK(m_electronsInKey.initialize());
31 ATH_CHECK(m_muonsInKey.initialize());
32 ATH_CHECK(m_jetsInKey.initialize());
33 ATH_CHECK(m_multiPhotonsOutKey.initialize());
34
35 const std::string baseName = m_vertexInKey.key();
36
37 // WriteHandleKeys
38 m_photonLinksKey = baseName + ".photonLinks";
39 m_jetLinksKey = baseName + ".jetLinks";
40 m_electronLinksKey = baseName + ".electronLinks";
41 m_muonLinksKey = baseName + ".muonLinks";
42 m_mDecor_ntrk = baseName + "." + m_mDecor_ntrk.key();
43 m_mDecor_sumPt = baseName + "." + m_mDecor_sumPt.key();
44 m_mDecor_chi2Over_ndf = baseName + "." + m_mDecor_chi2Over_ndf.key();
45 m_mDecor_z_asym = baseName + "." + m_mDecor_z_asym.key();
48 m_mDecor_z_skew = baseName + "." + m_mDecor_z_skew.key();
49 m_mDecor_photon_deltaz = baseName + "." + m_mDecor_photon_deltaz.key();
52 m_multiPhotonLinksKey = baseName + ".multiPhotonLinks";
53 m_mDecor_gnnScore = baseName + "." + m_mDecor_gnnScore.key();
54 m_mDecor_nElectrons = baseName + "." + m_mDecor_nElectrons.key();
55 m_mDecor_nMuons = baseName + "." + m_mDecor_nMuons.key();
56 m_mDecor_nPhotons = baseName + "." + m_mDecor_nPhotons.key();
57 m_mDecor_nJets = baseName + "." + m_mDecor_nJets.key();
58
59 ATH_CHECK(m_multiPhotonLinksKey.initialize());
60 ATH_CHECK(m_photonLinksKey.initialize());
61 ATH_CHECK(m_jetLinksKey.initialize());
62 ATH_CHECK(m_electronLinksKey.initialize());
63 ATH_CHECK(m_muonLinksKey.initialize());
64 ATH_CHECK(m_mDecor_ntrk.initialize());
65 ATH_CHECK(m_mDecor_sumPt.initialize());
66 ATH_CHECK(m_mDecor_chi2Over_ndf.initialize());
67 ATH_CHECK(m_mDecor_z_asym.initialize());
70 ATH_CHECK(m_mDecor_z_skew.initialize());
74 ATH_CHECK(m_mDecor_gnnScore.initialize());
75 ATH_CHECK(m_mDecor_nElectrons.initialize());
76 ATH_CHECK(m_mDecor_nMuons.initialize());
77 ATH_CHECK(m_mDecor_nJets.initialize());
78 ATH_CHECK(m_mDecor_nPhotons.initialize());
79
80 // ReadHandleKeys
81 m_deltaZKey = baseName + ".deltaZ";
82 m_deltaPhiKey = baseName + ".deltaPhi";
83 ATH_CHECK(m_deltaZKey.initialize());
84 ATH_CHECK(m_deltaPhiKey.initialize());
85
86 // additional ReadHandleKeys to declare dependencies to the scheduler
87 std::string photonBaseName = m_photonsInKey.key();
88
89 m_caloPointingZKey = photonBaseName + ".caloPointingZ";
90 m_zCommonKey = photonBaseName + ".zCommon";
91 m_zCommonErrorKey = photonBaseName + ".zCommonError";
92
93 ATH_CHECK(m_caloPointingZKey.initialize());
94 ATH_CHECK(m_zCommonKey.initialize());
95 ATH_CHECK(m_zCommonErrorKey.initialize());
96
97 // Tools
98 ATH_CHECK(m_gnnTool.retrieve());
100
101 return StatusCode::SUCCESS;
102 }
103
104 StatusCode VertexDecoratorAlg ::execute(const EventContext& ctx)
105 {
107 ATH_CHECK(vertices.isValid());
108
110 ATH_CHECK(photonsIn.isValid());
111
113 ATH_CHECK(electronsIn.isValid());
115 ATH_CHECK(muonsIn.isValid());
117 ATH_CHECK(jetsIn.isValid());
119 ATH_CHECK(eventInfo.isValid());
120
126
132
134
135 // Decorations needed by the GNNTool
151
152 auto mpCont = std::make_unique<xAOD::CompositeParticleContainer>();
153 auto mpAux = std::make_unique<xAOD::CompositeParticleAuxContainer>();
154 mpCont->setStore(mpAux.get());
155
156 const xAOD::Vertex* bestVtxForMP = nullptr;
157 ElementLink<xAOD::CompositeParticleContainer> mpLink; // will be valid only if created
158 bool haveMP = false;
159
160 // For MT safety, decoration keys must be initialized and available upfront.
161 const bool haveZCommonDecor = acc_zCommon.isAvailable() && acc_zCommonError.isAvailable();
162 const bool haveCaloPointingDecor = acc_caloPointingZ.isAvailable();
163
164 if (photonsIn->size() > 1 && (!haveZCommonDecor || !haveCaloPointingDecor))
165 {
166 ATH_MSG_ERROR("photonsIn has size > 1 but required decorations are missing: "
167 << m_zCommonKey.key() << " and/or " << m_zCommonErrorKey.key()
168 << " and/or " << m_caloPointingZKey.key());
169 return StatusCode::FAILURE;
170 }
171
172 if (photonsIn->size() > 1)
173 {
174 // Combined pointing Z (weighted)
175 double sumW = 0.0;
176 double sumWZ = 0.0;
177
178 TLorentzVector p4sum;
179 int nUsed = 0;
180 for (const xAOD::Photon* ph : *photonsIn)
181 {
182 const float zCommon = acc_zCommon(*ph);
183 const float zCaloPointing = acc_caloPointingZ(*ph);
184 // Value-based selection only: prefer zCommon, then fallback to caloPointing value.
185 const bool useZCommonValue = std::isfinite(zCommon);
186 const float z = useZCommonValue ? zCommon : zCaloPointing;
187 if (!std::isfinite(z)) {
188 ATH_MSG_ERROR("Non-finite photon pointing values found in both "
189 << m_zCommonKey.key() << " and " << m_caloPointingZKey.key());
190 return StatusCode::FAILURE;
191 }
192
193 // Keep weighting consistent with the z source:
194 // use zCommonError only when z comes from zCommon.
195 double w = 1.0;
196 if (useZCommonValue) {
197 const float s = acc_zCommonError(*ph);
198 if (std::isfinite(s) && s > 0.0f) w = 1.0 / (double(s) * double(s));
199 }
200
201 sumW += w;
202 sumWZ += w * z;
203 p4sum += ph->p4();
204 ++nUsed;
205 }
206
207 if (nUsed > 1 && sumW > 0.0)
208 {
209 const float zPoint = sumWZ / sumW;
210
211 // Choose vertex closest in z to combined pointing
212 float bestAbsDZ = 1e30;
213 for (const xAOD::Vertex* vtx : *vertices)
214 {
215 if (vtx->vertexType() == xAOD::VxType::NoVtx) continue;
216
217 const float dz = zPoint - vtx->z();
218 const float adz = std::abs(dz);
219 if (adz < bestAbsDZ)
220 {
221 bestAbsDZ = adz;
222 bestVtxForMP = vtx;
223 }
224 }
225
226 if (bestVtxForMP)
227 {
228 // Create exactly one CompositeParticle node
229 auto* mp = new xAOD::CompositeParticle();
230 mpCont->push_back(mp);
231
232 mp->setP4(p4sum);
233
234 SG::AuxElement::Decorator<float> dec_mp_deltaZ("deltaZ");
235 SG::AuxElement::Decorator<float> dec_mp_deltaPhi("deltaPhi");
236 SG::AuxElement::Decorator<int> dec_mp_nPhotons("nPhotons");
237 SG::AuxElement::Decorator<float> dec_mp_zPointing("zPointing");
238
239 const float deltaZ = zPoint - bestVtxForMP->z();
240 dec_mp_deltaZ(*mp) = deltaZ;
241 dec_mp_zPointing(*mp) = zPoint;
242 dec_mp_nPhotons(*mp) = static_cast<int>(photonsIn->size());
243
244 float dphi = 0.0f;
245 if (acc_deltaPhi.isAvailable())
246 {
247 dphi = acc_deltaPhi(*bestVtxForMP);
248 if (!std::isfinite(dphi)) dphi = 0.0f;
249 }
250 dec_mp_deltaPhi(*mp) = dphi;
251 mpLink.setElement(mp);
252 mpLink.setStorableObject(*mpCont, true);
253
254 haveMP = true;
255 }
256 }
257 }
258
259 // Record the MultiPhotons container (even if empty)
260 ATH_CHECK(mpHandle.record(std::move(mpCont), std::move(mpAux)));
261
262 std::map< const xAOD::Vertex*, std::vector<ElementLink<xAOD::JetContainer>> > jetsInVertex;
263 std::map< const xAOD::Jet*, std::map< const xAOD::Vertex*, int> > jetVertexPt;
264
265 // initialize jet-vertex map
266 for(const xAOD::Vertex *vertex : *vertices){
267 jetsInVertex[vertex] = {};
268 for(const xAOD::Jet *jet : *jetsIn){
269 jetVertexPt[jet][vertex] = 0;
270 }
271 }
272
273 // pre-fill the jet-vertex map
274 for(const xAOD::Jet* jet : *jetsIn){
275
276 // for each jet, calculate the track pT associated to each vertex
277 std::vector<const xAOD::TrackParticle*> ghostTracks = jet->getAssociatedObjects<xAOD::TrackParticle >(xAOD::JetAttribute::GhostTrack);
278 for(const xAOD::TrackParticle* jtrk : ghostTracks){
279 if( !jtrk ) continue;
280 auto jetTrackVertex = m_trkVtxAssociationTool->getUniqueMatchVertexLink(*jtrk, *vertices);
281 if(jetTrackVertex) jetVertexPt[jet][*jetTrackVertex] += jtrk->pt();
282 }
283
284 // find vertex with the largest fraction of jet track pt
285 float maxPtFrac = -1;
286 const xAOD::Vertex* uniqueVertexAddress = nullptr;
287 for (const xAOD::Vertex *vertex : *vertices) {
288 if (vertex->vertexType() == xAOD::VxType::NoVtx) continue;
289 if(jetVertexPt[jet][vertex] > maxPtFrac){
290 maxPtFrac = jetVertexPt[jet][vertex];
291 uniqueVertexAddress = vertex;
292 }
293 }
294
295 // add jet to that vertex's vector of links
297 jetLink.setElement(jet);
298 jetLink.setStorableObject(*jetsIn.ptr(), true);
299 jetsInVertex[uniqueVertexAddress].push_back(jetLink);
300 }
301
302 for (const xAOD::Vertex *vertex : *vertices)
303 {
304 if (vertex->vertexType() == xAOD::VxType::NoVtx)
305 continue;
306
307 if (vertex->nTrackParticles() < 2)
308 continue;
309
310 dec_actualInterPerXing(*vertex) = eventInfo->actualInteractionsPerCrossing();
311
312 // variables for calculation of delta Z asymmetry and delta d asymmetry
313 float z_asym = 0;
314 float sumDZ = 0;
315 float deltaZ = 0;
316 float modsumDZ = 0;
317 float weighted_sumDZ = 0;
318 float weighted_deltaZ = 0;
319 float weighted_modsumDZ = 0;
320 float weighted_z_asym = 0;
321
322 // make vector
323 std::vector<float> track_deltaZ;
324
325 for (size_t i = 0; i < vertex->nTrackParticles(); i++) {
326
327 const xAOD::TrackParticle *trackTmp = vertex->trackParticle(i);
328
329 if(!trackTmp) continue;
330
331 deltaZ = trackTmp->z0() + trackTmp->vz() - vertex->z();
332 track_deltaZ.push_back(deltaZ);
333 // get the track weight for each track to get the deltaZ/trk_weight
334 float trk_weight = vertex->trackWeight(i);
335 weighted_deltaZ = deltaZ * trk_weight;
336 // sum of delta z
337 sumDZ += deltaZ;
338 modsumDZ += std::abs(deltaZ);
339 weighted_sumDZ += weighted_deltaZ;
340 weighted_modsumDZ += std::abs(weighted_deltaZ);
341 } // end loop over tracks
342
343 if (modsumDZ > 0) {
344 z_asym = sumDZ / modsumDZ;
345 }
346 if (weighted_modsumDZ > 0) {
347 weighted_z_asym = weighted_sumDZ / weighted_modsumDZ;
348 }
349
350 const float number_tracks = track_deltaZ.size(); // get number of tracks
351 const float mean_Dz =
352 number_tracks > 0 ? sumDZ / number_tracks : 0.F; // calculate average
353
354 float z_skew = 0; // skewness of DeltaZ asymmetry
355 float z_kurt = 0; // Kurtosis of DeltaZ asymmetry
356 float z_var = 0; // variance of DeltaZ
357
358 for (auto i : track_deltaZ)
359 {
360 float z_zbar = (i - mean_Dz);
361 z_var += std::pow(z_zbar, 2);
362 z_skew += std::pow(z_zbar, 3);
363 z_kurt += std::pow(z_zbar, 4);
364 }
365 if (number_tracks > 1 && z_var > 0) {
366 z_var /= (number_tracks - 1);
367 float z_sd = std::sqrt(z_var);
368 const float skew_denom = (number_tracks - 1) * std::pow(z_sd, 3);
369 const float kurt_denom = (number_tracks - 1) * std::pow(z_sd, 4);
370 if (std::isfinite(skew_denom) && skew_denom != 0.F) {
371 z_skew /= skew_denom;
372 } else {
373 z_skew = 0.F;
374 }
375 if (std::isfinite(kurt_denom) && kurt_denom != 0.F) {
376 z_kurt /= kurt_denom;
377 } else {
378 z_kurt = 0.F;
379 }
380 }
381 else
382 {
383 ATH_MSG_WARNING("z momenta are NaN: setting to zero");
384 z_skew = 0.;
385 z_kurt = 0.;
386 }
387
388 dec_ntrk(*vertex) = number_tracks;
389
390 if(!dec_sumPt.isAvailable()){
391 dec_sumPt(*vertex) = xAOD::PVHelpers::getVertexSumPt(vertex, 1, false);
392 }
393 const float numberDoF = vertex->numberDoF();
394 dec_chi2Over_ndf(*vertex) =
395 numberDoF > 0.F ? vertex->chiSquared() / numberDoF : 0.F;
396 dec_z_asym(*vertex) = z_asym;
397 dec_weighted_z_asym(*vertex) = weighted_z_asym;
398 dec_z_kurt(*vertex) = z_kurt;
399 dec_z_skew(*vertex) = z_skew;
400
401 if (acc_deltaZ.isAvailable() && photonsIn->size() > 0 ) {
402 //protect against rare NaNs before assigning decorator: setting to 0 (-999 cause NaNs)
403 if (!std::isfinite(acc_deltaZ(*vertex))) {
404 ATH_MSG_WARNING("photon deltaPhi is NaN: setting to -1!");
405 dec_photon_deltaz(*vertex) = -1;
406 }
407 else{
408 dec_photon_deltaz(*vertex) = acc_deltaZ(*vertex);
409 }
410 }
411 else{
412 dec_photon_deltaz(*vertex) = -1;
413 }
414 if (acc_deltaPhi.isAvailable() && photonsIn->size() > 0) {
415
416 if (!std::isfinite(acc_deltaPhi(*vertex))) {
417 ATH_MSG_WARNING("photon deltaPhi is NaN: setting to 0!");
418 dec_photon_deltaPhi(*vertex) = -1;
419 }
420 else{
421 dec_photon_deltaPhi(*vertex) = acc_deltaPhi(*vertex);
422 }
423 }
424 else{
425 dec_photon_deltaPhi(*vertex) = -1;
426 }
427
428 // associate objects to vertices
429 auto countValid = [](const auto& links) -> int {
430 int n = 0;
431 for (const auto& l : links) if (l.isValid()) ++n;
432 return n;
433 };
434
435 std::vector<ElementLink<xAOD::ElectronContainer>> electronLinks;
436 for(const xAOD::Electron* electron : *electronsIn){
437 const auto *id_trk = xAOD::EgammaHelpers::getOriginalTrackParticle(electron);
438 if(!id_trk) continue;
439 auto eleVertex = m_trkVtxAssociationTool->getUniqueMatchVertexLink(*id_trk, *vertices);
440 if(!eleVertex) continue;
442 if(*eleVertex == vertex){
443 elLink.setElement(electron);
444 elLink.setStorableObject(*electronsIn.ptr(), true);
445 electronLinks.push_back(elLink);
446 }
447 }
448 dec_electronLinks(*vertex) = electronLinks;
449 dec_nElectrons(*vertex) = countValid(electronLinks);
450
451 std::vector<ElementLink<xAOD::PhotonContainer>> photonLinks;
452 for(const xAOD::Photon* photon : *photonsIn){
454 phLink.setElement(photon);
455 phLink.setStorableObject(*photonsIn.ptr(), true);
456 photonLinks.push_back(phLink);
457 }
458 dec_photonLinks(*vertex) = photonLinks;
459 dec_nPhotons(*vertex) = countValid(photonLinks);
460
461 // multi-photon link
462 std::vector<ElementLink<xAOD::CompositeParticleContainer>> mpLinks;
463 if (haveMP && vertex == bestVtxForMP)
464 {
465 mpLinks.push_back(mpLink);
466 }
467 dec_multiPhotonLinks(*vertex) = mpLinks;
468
469 // for jets, use prefilled map
470 dec_jetLinks(*vertex) = jetsInVertex[vertex];
471 dec_nJets(*vertex) = countValid(jetsInVertex[vertex]);
472
473 std::vector<ElementLink<xAOD::MuonContainer>> muonLinks;
474 for(const xAOD::Muon* muon : *muonsIn){
475 const auto *tp = muon->trackParticle(xAOD::Muon::InnerDetectorTrackParticle);
476 if(!tp) continue;
477 try{
478 auto muonVertex = m_trkVtxAssociationTool->getUniqueMatchVertexLink(*tp, *vertices);
479 if(!muonVertex) continue;
481 if(*muonVertex == vertex){
482 muonLink.setElement(muon);
483 muonLink.setStorableObject(*muonsIn.ptr(), true);
484 muonLinks.push_back(muonLink);
485 }
486 }catch(...) {
487 ATH_MSG_DEBUG("Skipping muon as the track is not associated to any PV ");
488 ATH_MSG_DEBUG("Muon pT, eta = " << muon->pt() << " " << muon->eta());
489 }
490
491 }
492 dec_muonLinks(*vertex) = muonLinks;
493 dec_nMuons(*vertex) = countValid(muonLinks);
494
495 // Finally, decorate the vertices with the GNN score
496 float score_phsvertex = m_gnnTool->decorate(*vertex);
497
498 dec_gnnScore(*vertex) = score_phsvertex;
499 }
500
501 return StatusCode::SUCCESS;
502 }
503
504} // namespace InDetGNNHardScatterSelection
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle class for reading a decoration on an object.
Handle class for adding a decoration to an object.
Base class for elements of a container that can have aux data.
Property holding a SG store/key/clid from which a WriteHandle is made.
#define z
AthHistogramAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_gnnScore
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_photon_deltaPhi
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_z_skew
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_muonLinksKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_sumPt
SG::WriteHandleKey< xAOD::CompositeParticleContainer > m_multiPhotonsOutKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_nJets
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_chi2Over_ndf
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_deltaZKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_photonLinksKey
SG::ReadHandleKey< xAOD::MuonContainer > m_muonsInKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_weighted_z_asym
SG::ReadDecorHandleKey< xAOD::PhotonContainer > m_zCommonKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_electronLinksKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_ntrk
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_nElectrons
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_actualInterPerXing
ToolHandle< CP::TrackVertexAssociationTool > m_trkVtxAssociationTool
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_deltaPhiKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_weighted_z_kurt
SG::ReadDecorHandleKey< xAOD::PhotonContainer > m_zCommonErrorKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_photon_deltaz
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_nPhotons
SG::ReadDecorHandleKey< xAOD::PhotonContainer > m_caloPointingZKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_multiPhotonLinksKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_z_asym
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_mDecor_nMuons
SG::ReadHandleKey< xAOD::VertexContainer > m_vertexInKey
SG::ReadHandleKey< xAOD::PhotonContainer > m_photonsInKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_jetLinksKey
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronsInKey
SG::ReadHandleKey< xAOD::JetContainer > m_jetsInKey
Handle class for reading a decoration on an object.
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
const_pointer_type ptr()
Dereference the pointer.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Handle class for adding a decoration to an object.
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
float z0() const
Returns the parameter.
float vz() const
The z origin for the parameters.
float z() const
Returns the z position.
const xAOD::TrackParticle * getOriginalTrackParticle(const xAOD::Electron *el)
Helper function for getting the "Original" Track Particle (i.e before GSF) via the electron.
float getVertexSumPt(const xAOD::Vertex *vertex, int power=1, bool useAux=true)
Loop over track particles associated with vertex and return scalar sum of pT^power in GeV (from auxda...
@ NoVtx
Dummy vertex. TrackParticle was not used in vertex fit.
Jet_v1 Jet
Definition of the current "jet version".
TrackParticle_v1 TrackParticle
Reference the current persistent version:
Vertex_v1 Vertex
Define the latest version of the vertex class.
CompositeParticle_v1 CompositeParticle
Define the latest version of the composite particle class.
Muon_v1 Muon
Reference the current persistent version:
Photon_v1 Photon
Definition of the current "egamma version".
Electron_v1 Electron
Definition of the current "egamma version".