ATLAS Offline Software
Loading...
Searching...
No Matches
DecoratorHelpers.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
7
8#ifndef DECORATORHELPERS_H
9#define DECORATORHELPERS_H
10
12#include <Math/Vector4D.h>
13#include <TMath.h>
15
16#include <stdexcept>
17#include <string>
18#include <unordered_map>
19#include <vector>
20
21namespace CP {
22
36template <typename T, typename U, typename V>
37void FillParticleInfo(const SG::Decorator<T>& dec, const U& value,
38 const V* object) {
39 dec(*object) = value;
40}
41
53template <typename T, typename U, typename V>
54void FillVectorParticleInfo(const SG::Decorator<std::vector<T>>& dec,
55 const U& value, const V* object) {
56 dec(*object).push_back(value);
57}
58
72template <typename T, typename U>
74 const SG::Decorator<T>& dec_eta,
75 const SG::Decorator<T>& dec_phi,
76 const SG::Decorator<T>& dec_m,
77 const ROOT::Math::PtEtaPhiMVector& particle, U* object) {
78 FillParticleInfo(dec_pt, particle.Pt(), object);
79 FillParticleInfo(dec_eta, particle.Eta(), object);
80 FillParticleInfo(dec_phi, particle.Phi(), object);
81 FillParticleInfo(dec_m, particle.M(), object);
82}
83
91template <typename T, typename U>
92void FillVectorParticleInfo(const SG::Decorator<std::vector<T>>& dec_pt,
93 const SG::Decorator<std::vector<T>>& dec_eta,
94 const SG::Decorator<std::vector<T>>& dec_phi,
95 const SG::Decorator<std::vector<T>>& dec_m,
96 const ROOT::Math::PtEtaPhiMVector& particle,
97 U* object) {
98 FillVectorParticleInfo(dec_pt, particle.Pt(), object);
99 FillVectorParticleInfo(dec_eta, particle.Eta(), object);
100 FillVectorParticleInfo(dec_phi, particle.Phi(), object);
101 FillVectorParticleInfo(dec_m, particle.M(), object);
102}
103
111template <typename T, typename U>
113 const SG::Decorator<T>& dec_eta,
114 const SG::Decorator<T>& dec_phi,
115 const SG::Decorator<T>& dec_m,
116 const SG::Decorator<int>& dec_pdgId,
117 const ROOT::Math::PtEtaPhiMVector& particle, int pdgId,
118 U* object) {
119 FillParticleInfo(dec_pdgId, pdgId, object);
120 FillParticleInfo(dec_pt, dec_eta, dec_phi, dec_m, particle, object);
121}
122
130template <typename T, typename U>
131void FillVectorParticleInfo(const SG::Decorator<std::vector<T>>& dec_pt,
132 const SG::Decorator<std::vector<T>>& dec_eta,
133 const SG::Decorator<std::vector<T>>& dec_phi,
134 const SG::Decorator<std::vector<T>>& dec_m,
135 const SG::Decorator<std::vector<int>>& dec_pdgId,
136 const ROOT::Math::PtEtaPhiMVector& particle,
137 int pdgId, U* object) {
138 FillVectorParticleInfo(dec_pdgId, pdgId, object);
139 FillVectorParticleInfo(dec_pt, dec_eta, dec_phi, dec_m, particle, object);
140}
141
149template <typename T, typename U>
151 const SG::Decorator<T>& dec_eta,
152 const SG::Decorator<T>& dec_phi,
153 const SG::Decorator<T>& dec_m, U* object) {
154 FillParticleInfo(dec_pt, -1, object);
155 FillParticleInfo(dec_eta, -999, object);
156 FillParticleInfo(dec_phi, -999, object);
157 FillParticleInfo(dec_m, -1, object);
158}
159
167template <typename T, typename U>
168void FillDefaultVectorParticleInfo(const SG::Decorator<std::vector<T>>& dec_pt,
169 const SG::Decorator<std::vector<T>>& dec_eta,
170 const SG::Decorator<std::vector<T>>& dec_phi,
171 const SG::Decorator<std::vector<T>>& dec_m,
172 U* object) {
173 dec_pt(*object) = {};
174 dec_eta(*object) = {};
175 dec_phi(*object) = {};
176 dec_m(*object) = {};
177}
178
186template <typename T, typename U>
188 const SG::Decorator<T>& dec_eta,
189 const SG::Decorator<T>& dec_phi,
190 const SG::Decorator<T>& dec_m,
191 const SG::Decorator<int>& dec_pdgId, U* object) {
192 FillParticleInfo(dec_pdgId, 0, object);
193 FillDefaultParticleInfo(dec_pt, dec_eta, dec_phi, dec_m, object);
194}
195
203template <typename T, typename U>
205 const SG::Decorator<std::vector<T>>& dec_pt,
206 const SG::Decorator<std::vector<T>>& dec_eta,
207 const SG::Decorator<std::vector<T>>& dec_phi,
208 const SG::Decorator<std::vector<T>>& dec_m,
209 const SG::Decorator<std::vector<int>>& dec_pdgId, U* object) {
210 dec_pdgId(*object) = {};
211 FillDefaultVectorParticleInfo(dec_pt, dec_eta, dec_phi, dec_m, object);
212}
213
232 std::unordered_map<std::string, SG::Decorator<float>> floatDecorators;
233 std::unordered_map<std::string, SG::Decorator<std::vector<float>>>
235 std::unordered_map<std::string, SG::Decorator<int>> intDecorators;
236 std::unordered_map<std::string, SG::Decorator<std::vector<int>>>
238
247 void setPrefix(const std::string& prefix) {
248 m_prefix = prefix;
249 clearCaches();
250 }
251
260 void setEventInfo(const xAOD::EventInfo* eventInfo) {
261 m_eventInfo = eventInfo;
262 }
263
264 // ── Initialize methods ────────────────────────────────────────────────────
265
267 void initializeFloatDecorator(const std::string& name) {
268 const std::string key = fullName(name);
269 floatDecorators.try_emplace(key, key);
270 clearCaches();
271 }
272
274 void initializeVectorFloatDecorator(const std::string& name) {
275 const std::string key = fullName(name);
276 vectorfloatDecorators.try_emplace(key, key);
277 clearCaches();
278 }
279
281 void initializeFloatDecorator(const std::vector<std::string>& names) {
282 for (const auto& name : names)
284 }
285
291 void initializePtEtaPhiMDecorator(const std::string& prefix) {
292 initializeFloatDecorator(prefix + "_pt");
293 initializeFloatDecorator(prefix + "_eta");
294 initializeFloatDecorator(prefix + "_phi");
295 initializeFloatDecorator(prefix + "_m");
296 }
297
302 void initializeParticleDecorators(const std::string& prefix) {
304 initializeIntDecorator(prefix + "_pdgId");
305 }
306
311 void initializeVectorPtEtaPhiMDecorator(const std::string& prefix) {
312 initializeVectorFloatDecorator(prefix + "_pt");
313 initializeVectorFloatDecorator(prefix + "_eta");
314 initializeVectorFloatDecorator(prefix + "_phi");
315 initializeVectorFloatDecorator(prefix + "_m");
316 }
317
322 void initializeVectorParticleDecorators(const std::string& prefix) {
324 initializeVectorIntDecorator(prefix + "_pdgId");
325 }
326
328 void initializeIntDecorator(const std::string& name) {
329 const std::string key = fullName(name);
330 intDecorators.try_emplace(key, key);
331 clearCaches();
332 }
333
335 void initializeVectorIntDecorator(const std::string& name) {
336 const std::string key = fullName(name);
337 vectorintDecorators.try_emplace(key, key);
338 clearCaches();
339 }
340
342 void initializeIntDecorator(const std::vector<std::string>& names) {
343 for (const auto& name : names)
345 }
346
347 // ── Getter methods ────────────────────────────────────────────────────────
348
353 const SG::Decorator<int>* getIntDecorator(const std::string& name) const {
355 }
356
362 const std::string& name) const {
364 }
365
370 const SG::Decorator<float>* getFloatDecorator(const std::string& name) const {
372 }
373
379 const std::string& name) const {
381 }
382
383 // ── Decorate methods ──────────────────────────────────────────────────────
384 // All write to m_eventInfo. Call setEventInfo() once per event first.
385
391 void decorateDefault(const std::string& prefix) {
392 const ParticleDecorators& decs = particleDecorators(prefix);
393 FillDefaultParticleInfo(*decs.pt, *decs.eta, *decs.phi, *decs.m,
394 pdgIdDecorator(decs, prefix), m_eventInfo);
395 }
396
398 void decorateVectorDefault(const std::string& prefix) {
400 FillDefaultVectorParticleInfo(*decs.pt, *decs.eta, *decs.phi, *decs.m,
401 *decs.pdgId, m_eventInfo);
402 }
403
407 void decorateDefaultNoPdgId(const std::string& prefix) {
408 const ParticleDecorators& decs = particleDecorators(prefix);
409 FillDefaultParticleInfo(*decs.pt, *decs.eta, *decs.phi, *decs.m,
411 }
412
414 void decorateCustom(const std::string& name, float value) {
416 }
417
419 void decorateCustom(const std::string& name, int value) {
421 }
422
429 void decorateParticle(const std::string& prefix,
430 const ROOT::Math::PtEtaPhiMVector& p) {
431 const ParticleDecorators& decs = particleDecorators(prefix);
432 FillParticleInfo(*decs.pt, *decs.eta, *decs.phi, *decs.m, p, m_eventInfo);
433 }
434
442 void decorateParticle(const std::string& prefix,
443 const ROOT::Math::PtEtaPhiMVector& p, int pdgId) {
444 const ParticleDecorators& decs = particleDecorators(prefix);
445 FillParticleInfo(*decs.pt, *decs.eta, *decs.phi, *decs.m,
446 pdgIdDecorator(decs, prefix), p, pdgId, m_eventInfo);
447 }
448
458 const std::string& prefix,
459 const std::vector<ROOT::Math::PtEtaPhiMVector>& vec_p,
460 const std::vector<int>& vec_pdgId) {
462 for (size_t i = 0; i < vec_p.size(); i++) {
463 FillVectorParticleInfo(*decs.pt, *decs.eta, *decs.phi, *decs.m,
464 *decs.pdgId, vec_p.at(i), vec_pdgId.at(i),
466 }
467 }
468
469 private:
473 const SG::Decorator<float>* pt{nullptr};
474 const SG::Decorator<float>* eta{nullptr};
475 const SG::Decorator<float>* phi{nullptr};
476 const SG::Decorator<float>* m{nullptr};
477 const SG::Decorator<int>* pdgId{nullptr};
478 };
479
488
489 std::string m_prefix;
491 nullptr};
492
495 std::unordered_map<std::string, ParticleDecorators> m_particleCache;
496 std::unordered_map<std::string, VectorParticleDecorators>
498
502 std::string fullName(const std::string& name) const {
503 return m_prefix.empty() ? name : m_prefix + "_" + name;
504 }
505
507 template <typename T>
508 const T* findDecorator(const std::unordered_map<std::string, T>& decorators,
509 const std::string& name) const {
510 const std::string key = fullName(name);
511 auto it = decorators.find(key);
512 if (it != decorators.end())
513 return &it->second;
514 throw std::runtime_error("Decorator with name " + key + " not found.");
515 }
516
518 void clearCaches() {
519 m_particleCache.clear();
520 m_vectorParticleCache.clear();
521 }
522
525 const ParticleDecorators& particleDecorators(const std::string& prefix) {
526 auto it = m_particleCache.find(prefix);
527 if (it != m_particleCache.end())
528 return it->second;
530 decs.pt = getFloatDecorator(prefix + "_pt");
531 decs.eta = getFloatDecorator(prefix + "_eta");
532 decs.phi = getFloatDecorator(prefix + "_phi");
533 decs.m = getFloatDecorator(prefix + "_m");
534 auto pdgIdIt = intDecorators.find(fullName(prefix + "_pdgId"));
535 if (pdgIdIt != intDecorators.end())
536 decs.pdgId = &pdgIdIt->second;
537 return m_particleCache.emplace(prefix, decs).first->second;
538 }
539
543 const std::string& prefix) {
544 auto it = m_vectorParticleCache.find(prefix);
545 if (it != m_vectorParticleCache.end())
546 return it->second;
548 decs.pt = getVectorFloatDecorator(prefix + "_pt");
549 decs.eta = getVectorFloatDecorator(prefix + "_eta");
550 decs.phi = getVectorFloatDecorator(prefix + "_phi");
551 decs.m = getVectorFloatDecorator(prefix + "_m");
552 decs.pdgId = getVectorIntDecorator(prefix + "_pdgId");
553 return m_vectorParticleCache.emplace(prefix, decs).first->second;
554 }
555
558 const std::string& prefix) const {
559 if (!decs.pdgId)
560 throw std::runtime_error("Decorator with name " +
561 fullName(prefix + "_pdgId") + " not found.");
562 return *decs.pdgId;
563 }
564};
565
566} // namespace CP
567
568#endif // DECORATORHELPERS_H
Helper class to provide type-safe access to aux data.
Select isolated Photons, Electrons and Muons.
void FillVectorParticleInfo(const SG::Decorator< std::vector< T > > &dec, const U &value, const V *object)
Template function to fill vector particle information into a given object.
void FillDefaultVectorParticleInfo(const SG::Decorator< std::vector< T > > &dec_pt, const SG::Decorator< std::vector< T > > &dec_eta, const SG::Decorator< std::vector< T > > &dec_phi, const SG::Decorator< std::vector< T > > &dec_m, U *object)
Template function to fill default (placeholder) vector particle kinematics into an object.
void FillParticleInfo(const SG::Decorator< T > &dec, const U &value, const V *object)
Template function to fill particle information into a given object.
void FillDefaultParticleInfo(const SG::Decorator< T > &dec_pt, const SG::Decorator< T > &dec_eta, const SG::Decorator< T > &dec_phi, const SG::Decorator< T > &dec_m, U *object)
Template function to fill default (placeholder) particle kinematics into an object.
SG::Decorator< T, ALLOC > Decorator
Helper class to provide type-safe access to aux data, specialized for JaggedVecElt.
Definition AuxElement.h:576
EventInfo_v1 EventInfo
Definition of the latest event info version.
static const EventInfo_v1::Accessor< std::vector< std::string > > names("streamTagNames")
Decorators of one scalar particle prefix; pdgId is nullptr if the "<prefix>_pdgId" decorator was not ...
const SG::Decorator< float > * eta
const SG::Decorator< float > * phi
const SG::Decorator< float > * m
const SG::Decorator< float > * pt
Decorators of one vector particle prefix.
const SG::Decorator< std::vector< float > > * phi
const SG::Decorator< std::vector< float > > * pt
const SG::Decorator< std::vector< float > > * m
const SG::Decorator< std::vector< float > > * eta
const SG::Decorator< std::vector< int > > * pdgId
Struct to manage and apply decorators to the EventInfo object.
void initializeParticleDecorators(const std::string &prefix)
Initialize pt, eta, phi, m float and pdgId int decorators for a bare prefix.
const ParticleDecorators & particleDecorators(const std::string &prefix)
Resolve (once) the scalar decorators belonging to a bare prefix.
void initializeVectorFloatDecorator(const std::string &name)
Initialize a vector-of-float decorator by bare name.
void decorateParticle(const std::string &prefix, const ROOT::Math::PtEtaPhiMVector &p, int pdgId)
Decorate with particle kinematics and PDG ID (bare prefix).
void clearCaches()
Invalidate the per-prefix bundles, e.g. after new decorators were added.
void decorateDefault(const std::string &prefix)
Decorate with default particle kinematics (bare prefix).
void initializeVectorParticleDecorators(const std::string &prefix)
Initialize vector pt, eta, phi, m float and pdgId int decorators for a bare prefix.
const T * findDecorator(const std::unordered_map< std::string, T > &decorators, const std::string &name) const
Look up a decorator by bare name; throws if it was not initialized.
std::unordered_map< std::string, SG::Decorator< std::vector< float > > > vectorfloatDecorators
void initializeIntDecorator(const std::string &name)
Initialize an integer decorator by bare name.
void initializeFloatDecorator(const std::string &name)
Initialize a float decorator by bare name.
const xAOD::EventInfo * m_eventInfo
Target object; set once per event via setEventInfo().
void decorateCustom(const std::string &name, int value)
Decorate with a custom integer value (bare name).
std::unordered_map< std::string, SG::Decorator< int > > intDecorators
const SG::Decorator< int > * getIntDecorator(const std::string &name) const
Retrieve a pointer to an integer decorator by bare name.
void initializeVectorIntDecorator(const std::string &name)
Initialize a vector-of-int decorator by bare name.
void initializeIntDecorator(const std::vector< std::string > &names)
Initialize multiple integer decorators by a list of bare names.
void decorateDefaultNoPdgId(const std::string &prefix)
Decorate with default particle kinematics, no PDG ID (bare prefix).
const SG::Decorator< float > * getFloatDecorator(const std::string &name) const
Retrieve a pointer to a float decorator by bare name.
void setEventInfo(const xAOD::EventInfo *eventInfo)
Set the EventInfo object to decorate for the current event.
std::string m_prefix
Prefix prepended to all decorator names.
void decorateCustom(const std::string &name, float value)
Decorate with a custom float value (bare name).
void initializeFloatDecorator(const std::vector< std::string > &names)
Initialize multiple float decorators by a list of bare names.
void initializeVectorPtEtaPhiMDecorator(const std::string &prefix)
Initialize vector pt, eta, phi, m float decorators for a bare prefix.
void decorateVectorParticle(const std::string &prefix, const std::vector< ROOT::Math::PtEtaPhiMVector > &vec_p, const std::vector< int > &vec_pdgId)
Decorate with a vector of particle kinematics and PDG IDs (bare prefix).
std::unordered_map< std::string, VectorParticleDecorators > m_vectorParticleCache
void initializePtEtaPhiMDecorator(const std::string &prefix)
Initialize pt, eta, phi, m float decorators for a bare prefix.
const VectorParticleDecorators & vectorParticleDecorators(const std::string &prefix)
Resolve (once) the vector decorators belonging to a bare prefix.
std::unordered_map< std::string, SG::Decorator< std::vector< int > > > vectorintDecorators
std::string fullName(const std::string &name) const
Returns the full (prefixed) decorator key for internal use.
std::unordered_map< std::string, ParticleDecorators > m_particleCache
Per-prefix decorator bundles, keyed by bare prefix.
void setPrefix(const std::string &prefix)
Set the prefix prepended to all decorator names.
const SG::Decorator< std::vector< int > > * getVectorIntDecorator(const std::string &name) const
Retrieve a pointer to a vector-of-int decorator by bare name.
const SG::Decorator< std::vector< float > > * getVectorFloatDecorator(const std::string &name) const
Retrieve a pointer to a vector-of-float decorator by bare name.
const SG::Decorator< int > & pdgIdDecorator(const ParticleDecorators &decs, const std::string &prefix) const
The pdgId decorator of a bundle; throws if it was not initialized.
void decorateParticle(const std::string &prefix, const ROOT::Math::PtEtaPhiMVector &p)
Decorate with particle kinematics, no PDG ID (bare prefix).
std::unordered_map< std::string, SG::Decorator< float > > floatDecorators
void decorateVectorDefault(const std::string &prefix)
Decorate with default vector particle kinematics (bare prefix).