ATLAS Offline Software
VertexDecoratorAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "VertexDecoratorAlg.h"
12 
14 {
16  ISvcLocator *pSvcLocator)
17  : AthHistogramAlgorithm (name, pSvcLocator)
18  {
19  }
20 
22  {
23  ATH_CHECK(m_vertexInKey.initialize());
25  ATH_CHECK(m_photonsInKey.initialize());
27  ATH_CHECK(m_muonsInKey.initialize());
28  ATH_CHECK(m_jetsInKey.initialize());
29 
30  const std::string baseName = m_vertexInKey.key();
31 
32  // WriteHandleKeys
33  m_photonLinksKey = baseName + ".photonLinks";
34  m_jetLinksKey = baseName + ".jetLinks";
35  m_electronLinksKey = baseName + ".electronLinks";
36  m_muonLinksKey = baseName + ".muonLinks";
37 
38  ATH_CHECK(m_photonLinksKey.initialize());
39  ATH_CHECK(m_jetLinksKey.initialize());
40  ATH_CHECK(m_electronLinksKey.initialize());
41  ATH_CHECK(m_muonLinksKey.initialize());
42 
43  // ReadHandleKeys
44  m_deltaZKey = baseName + ".deltaZ";
45  m_deltaPhiKey = baseName + ".deltaPhi";
46  ATH_CHECK(m_deltaZKey.initialize());
47  ATH_CHECK(m_deltaPhiKey.initialize());
48 
49  // additional ReadHandleKeys to declare dependencies to the scheduler
50  std::string photonBaseName = m_photonsInKey.key();
51 
52  m_caloPointingZKey = photonBaseName + ".caloPointingZ";
53  m_zCommonKey = photonBaseName + ".zCommon";
54  m_zCommonErrorKey = photonBaseName + ".zCommonError";
55 
56  ATH_CHECK(m_caloPointingZKey.initialize());
57  ATH_CHECK(m_zCommonKey.initialize());
58  ATH_CHECK(m_zCommonErrorKey.initialize());
59 
60  // Tools
61  ATH_CHECK(m_gnnTool.retrieve());
63 
64  return StatusCode::SUCCESS;
65  }
66 
68  {
69  const EventContext &ctx = Gaudi::Hive::currentContext();
71  ATH_CHECK(vertices.isValid());
72 
74  ATH_CHECK(photonsIn.isValid());
76  ATH_CHECK(electronsIn.isValid());
78  ATH_CHECK(muonsIn.isValid());
80  ATH_CHECK(jetsIn.isValid());
82  ATH_CHECK(eventInfo.isValid());
83 
88 
91 
92  // Decorations needed by the GNNTool
93  SG::AuxElement::Decorator<int> dec_ntrk("ntrk");
94  SG::AuxElement::Decorator<float> dec_sumPt("sumPt");
95  SG::AuxElement::Decorator<float> dec_chi2Over_ndf("chi2Over_ndf");
96  SG::AuxElement::Decorator<float> dec_z_asym("z_asymmetry");
97  SG::AuxElement::Decorator<float> dec_weighted_z_asym("weighted_z_asymmetry");
98  SG::AuxElement::Decorator<float> dec_z_kurt("z_kurtosis");
99  SG::AuxElement::Decorator<float> dec_z_skew("z_skewness");
100  SG::AuxElement::Decorator<float> dec_photon_deltaz("photon_deltaz");
101  SG::AuxElement::Decorator<float> dec_photon_deltaPhi("photon_deltaPhi");
102  SG::AuxElement::Decorator<float> dec_actualInterPerXing("actualIntPerXing");
103 
104  std::map< const xAOD::Vertex*, std::vector<const xAOD::Jet*> > jetsInVertex;
105  std::map< const xAOD::Jet*, std::map< const xAOD::Vertex*, int> > jetVertexPt;
106 
107  for (const xAOD::Vertex *vertex : *vertices)
108  {
109  if (vertex->vertexType() == xAOD::VxType::NoVtx)
110  continue;
111 
112  jetsInVertex[vertex] = {};
113  for(const xAOD::Jet *jet : *jetsIn){
114  jetVertexPt[jet][vertex] = 0;
115  }
116 
117  dec_actualInterPerXing(*vertex) = eventInfo->actualInteractionsPerCrossing();
118 
119  // taken from InDetPerfPlot_VertexTruthMatching.cxx
120  float sumPt = 0;
121 
122  // variables for calculation of delta Z asymmetry and delta d asymmetry
123  float z_asym = 0;
124  float sumDZ = 0;
125  float deltaZ = 0;
126  float modsumDZ = 0;
127  float weighted_sumDZ = 0;
128  float weighted_deltaZ = 0;
129  float weighted_modsumDZ = 0;
130  float weighted_z_asym = 0;
131 
132  // make vector
133  std::vector<float> track_deltaZ;
134 
135  for (size_t i = 0; i < vertex->nTrackParticles(); i++) {
136 
137  const xAOD::TrackParticle *trackTmp = vertex->trackParticle(i);
138 
139  if(!trackTmp) continue;
140 
141  sumPt += trackTmp->pt();
142  deltaZ = trackTmp->z0() + trackTmp->vz() - vertex->z();
143  track_deltaZ.push_back(deltaZ);
144  // get the track weight for each track to get the deltaZ/trk_weight
145  float trk_weight = vertex->trackWeight(i);
146  weighted_deltaZ = deltaZ * trk_weight;
147  // sum of delta z
148  sumDZ += deltaZ;
149  modsumDZ += std::abs(deltaZ);
150  weighted_sumDZ += weighted_deltaZ;
151  weighted_modsumDZ += std::abs(weighted_deltaZ);
152  } // end loop over tracks
153 
154  if (modsumDZ > 0) {
155  z_asym = sumDZ / modsumDZ;
156  }
157  if (weighted_modsumDZ > 0) {
158  weighted_z_asym = weighted_sumDZ / weighted_modsumDZ;
159  }
160 
161  float mean_Dz = sumDZ / track_deltaZ.size(); // calculate average
162  float number_tracks = track_deltaZ.size(); // get number of tracks
163 
164  float z_skew = 0; // skewness of DeltaZ asymmetry
165  float z_kurt = 0; // Kurtosis of DeltaZ asymmetry
166  float z_var = 0; // variance of DeltaZ
167 
168  for (auto i : track_deltaZ)
169  {
170  float z_zbar = (i - mean_Dz);
171  z_var += std::pow(z_zbar, 2);
172  z_skew += std::pow(z_zbar, 3);
173  z_kurt += std::pow(z_zbar, 4);
174  }
175  if (number_tracks > 1 && z_var > 0) {
176  z_var /= (number_tracks - 1);
177  float z_sd = std::sqrt(z_var);
178  z_skew /= (number_tracks - 1) * std::pow(z_sd, 3);
179  z_kurt /= (number_tracks - 1) * std::pow(z_sd, 4);
180  }
181  else
182  {
183  ATH_MSG_WARNING("z momenta are NaN: setting to zero");
184  z_skew = 0.;
185  z_kurt = 0.;
186  }
187 
188  dec_ntrk(*vertex) = number_tracks;
189  dec_sumPt(*vertex) = sumPt;
190 
191  dec_chi2Over_ndf(*vertex) = vertex->chiSquared() / vertex->numberDoF();
192 
193  dec_z_asym(*vertex) = z_asym;
194  dec_weighted_z_asym(*vertex) = weighted_z_asym;
195  dec_z_kurt(*vertex) = z_kurt;
196  dec_z_skew(*vertex) = z_skew;
197 
198  if (acc_deltaZ.isAvailable()) {
199  //protect against rare NaNs before assigning decorator
200  if (std::isnan(acc_deltaZ(*vertex))) {
201  ATH_MSG_WARNING("photon deltaZ is NaN: setting to -999!");
202  dec_photon_deltaz(*vertex) = -999;
203  }
204  else{
205  dec_photon_deltaz(*vertex) = acc_deltaZ(*vertex);
206  }
207  }
208  else{
209  dec_photon_deltaz(*vertex) = -999;
210  }
211  if (acc_deltaPhi.isAvailable()) {
212  if (std::isnan(acc_deltaPhi(*vertex))) {
213  ATH_MSG_WARNING("photon deltaPhi is NaN: setting to -999!");
214  dec_photon_deltaPhi(*vertex) = -999;
215  }
216  else{
217  dec_photon_deltaPhi(*vertex) = acc_deltaPhi(*vertex);
218  }
219  }
220  else{
221  dec_photon_deltaPhi(*vertex) = -999;
222  }
223 
224  // associate objects to vertices
225  std::vector<ElementLink<xAOD::ElectronContainer>> electronLinks;
226  for(const xAOD::Electron* electron : *electronsIn){
227 
229  if(!id_trk) continue;
230 
231  for(size_t i = 0; i < vertex->nTrackParticles(); i++){
232  const xAOD::TrackParticle *trk = vertex->trackParticle(i);
233  if(trk && id_trk == trk) {
235  elLink.setElement(electron);
236  elLink.setStorableObject(*electronsIn.ptr(), true);
237  electronLinks.push_back(elLink);
238  break;
239  }
240  }
241  }
242  dec_electronLinks(*vertex) = electronLinks;
243 
244  std::vector<ElementLink<xAOD::PhotonContainer>> photonLinks;
245  for(const xAOD::Photon* photon : *photonsIn){
247  phLink.setElement(photon);
248  phLink.setStorableObject(*photonsIn.ptr(), true);
249  photonLinks.push_back(phLink);
250  }
251  dec_photonLinks(*vertex) = photonLinks;
252 
253  float maxPtFrac = -1;
254  const xAOD::Jet* uniqueJetAddress = nullptr;
255 
256  std::vector<ElementLink<xAOD::JetContainer>> jetLinks;
257  for(const xAOD::Jet* jet : *jetsIn){
258 
259  std::vector<const xAOD::TrackParticle*> ghostTracks = jet->getAssociatedObjects<xAOD::TrackParticle >(xAOD::JetAttribute::GhostTrack);
260 
261  for(const xAOD::TrackParticle* jtrk : ghostTracks){
262  if( !jtrk ) continue;
263  auto jetTrackVertex = m_trkVtxAssociationTool->getUniqueMatchVertexLink(*jtrk, *vertices);
264  if(jetTrackVertex) jetVertexPt[jet][*jetTrackVertex] += jtrk->pt();
265  }
266  if(jetVertexPt[jet][vertex] > maxPtFrac){
267  maxPtFrac = jetVertexPt[jet][vertex];
268  uniqueJetAddress = jet;
269  }
270  }
271 
272  for(const xAOD::Jet* jet : *jetsIn){
273  if(uniqueJetAddress == jet){
275  jetLink.setElement(jet);
276  jetLink.setStorableObject(*jetsIn.ptr(), true);
277  jetLinks.push_back(jetLink);
278  break;
279  }
280  }
281  dec_jetLinks(*vertex) = jetLinks;
282 
283  std::vector<ElementLink<xAOD::MuonContainer>> muonLinks;
284  for(const xAOD::Muon* muon : *muonsIn){
285 
286  auto tp = muon->trackParticle(xAOD::Muon::InnerDetectorTrackParticle);
287 
288  for (size_t i = 0; i < vertex->nTrackParticles(); i++){
289  const xAOD::TrackParticle *trk = vertex->trackParticle(i);
290  if(trk && tp == trk){
292  muonLink.setElement(muon);
293  muonLink.setStorableObject(*muonsIn.ptr(), true);
294  muonLinks.push_back(muonLink);
295  break;
296  }
297  }
298  }
299  dec_muonLinks(*vertex) = muonLinks;
300 
301  // Finally, decorate the vertices with the GNN score
302  m_gnnTool->decorate(*vertex);
303  }
304 
305  return StatusCode::SUCCESS;
306  }
307 
308 } // namespace InDetGNNHardScatterSelection
xAOD::TrackParticle_v1::pt
virtual double pt() const override final
The transverse momentum ( ) of the particle.
Definition: TrackParticle_v1.cxx:73
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_photonsInKey
SG::ReadHandleKey< xAOD::PhotonContainer > m_photonsInKey
Definition: VertexDecoratorAlg.h:58
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_caloPointingZKey
SG::ReadDecorHandleKey< xAOD::PhotonContainer > m_caloPointingZKey
Definition: VertexDecoratorAlg.h:77
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_zCommonErrorKey
SG::ReadDecorHandleKey< xAOD::PhotonContainer > m_zCommonErrorKey
Definition: VertexDecoratorAlg.h:81
InDetGNNHardScatterSelection::VertexDecoratorAlg::initialize
StatusCode initialize() override
Definition: VertexDecoratorAlg.cxx:21
VertexDecoratorAlg.h
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_eventInKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInKey
Definition: VertexDecoratorAlg.h:50
ConstDataVector.h
DataVector adapter that acts like it holds const pointers.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_photonLinksKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_photonLinksKey
Definition: VertexDecoratorAlg.h:61
xAOD::TrackParticle_v1::vz
float vz() const
The z origin for the parameters.
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
xAOD::TrackParticle_v1::z0
float z0() const
Returns the parameter.
ElectronxAODHelpers.h
ParticleTest.tp
tp
Definition: ParticleTest.py:25
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_vertexInKey
SG::ReadHandleKey< xAOD::VertexContainer > m_vertexInKey
Definition: VertexDecoratorAlg.h:48
xAOD::VxType::NoVtx
@ NoVtx
Dummy vertex. TrackParticle was not used in vertex fit.
Definition: TrackingPrimitives.h:570
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_gnnTool
ToolHandle< GNNTool > m_gnnTool
Definition: VertexDecoratorAlg.h:90
EgammaContainer.h
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_trkVtxAssociationTool
ToolHandle< CP::TrackVertexAssociationTool > m_trkVtxAssociationTool
Definition: VertexDecoratorAlg.h:85
InDetVertexTruthMatchUtils.h
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:59
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_deltaPhiKey
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_deltaPhiKey
Definition: VertexDecoratorAlg.h:72
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
AthHistogramAlgorithm
Definition: AthHistogramAlgorithm.h:32
WriteDecorHandle.h
Handle class for adding a decoration to an object.
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_deltaZKey
SG::ReadDecorHandleKey< xAOD::VertexContainer > m_deltaZKey
Definition: VertexDecoratorAlg.h:70
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_electronLinksKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_electronLinksKey
Definition: VertexDecoratorAlg.h:65
xAOD::Electron_v1
Definition: Electron_v1.h:34
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
InDetGNNHardScatterSelection::VertexDecoratorAlg::VertexDecoratorAlg
VertexDecoratorAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: VertexDecoratorAlg.cxx:15
xAOD::photon
@ photon
Definition: TrackingPrimitives.h:199
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
xAOD::Photon_v1
Definition: Photon_v1.h:37
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
InDetGNNHardScatterSelection
Definition: InnerDetector/InDetRecTools/InDetGNNHardScatterSelection/InDetGNNHardScatterSelection/ConstituentsLoader.h:22
ReadDecorHandle.h
Handle class for reading a decoration on an object.
xAOD::EgammaParameters::electron
@ electron
Definition: EgammaEnums.h:18
xAOD::EgammaHelpers::getOriginalTrackParticle
const xAOD::TrackParticle * getOriginalTrackParticle(const xAOD::Electron *el)
Helper function for getting the "Original" Track Particle (i.e before GSF) via the electron.
Definition: ElectronxAODHelpers.cxx:11
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_jetsInKey
SG::ReadHandleKey< xAOD::JetContainer > m_jetsInKey
Definition: VertexDecoratorAlg.h:56
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_zCommonKey
SG::ReadDecorHandleKey< xAOD::PhotonContainer > m_zCommonKey
Definition: VertexDecoratorAlg.h:79
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_jetLinksKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_jetLinksKey
Definition: VertexDecoratorAlg.h:63
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
InDetGNNHardScatterSelection::VertexDecoratorAlg::execute
StatusCode execute() override
Definition: VertexDecoratorAlg.cxx:67
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_electronsInKey
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronsInKey
Definition: VertexDecoratorAlg.h:52
makeComparison.deltaZ
int deltaZ
Definition: makeComparison.py:46
xAOD::JetAttribute::GhostTrack
@ GhostTrack
Definition: JetAttributes.h:252
SG::ReadDecorHandle::isAvailable
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_muonLinksKey
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_muonLinksKey
Definition: VertexDecoratorAlg.h:67
InDetGNNHardScatterSelection::VertexDecoratorAlg::m_muonsInKey
SG::ReadHandleKey< xAOD::MuonContainer > m_muonsInKey
Definition: VertexDecoratorAlg.h:54
xAOD::EventInfo_v1::actualInteractionsPerCrossing
float actualInteractionsPerCrossing() const
Average interactions per crossing for the current BCID - for in-time pile-up.
Definition: EventInfo_v1.cxx:380