ATLAS Offline Software
Loading...
Searching...
No Matches
MetHelpers.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8#ifndef COLUMNAR_MET_MET_HELPERS_H
9#define COLUMNAR_MET_MET_HELPERS_H
10
13#include <ColumnarMet/MetDef.h>
15
16namespace columnar
17{
18 namespace MetHelpers
19 {
37 template<ContainerIdConcept CI = ContainerId::met,typename CM=ColumnarModeDefault>
39
40 template<ContainerIdConcept CI>
42 {
43 public:
45
47
48 [[nodiscard]] OptObjectId<CI,CM> operator () (ObjectRange<CI,CM> metMap, const std::string& metKey) const
49 {
50 return metMap.getXAODObject()[metKey];
51 }
52
53 [[nodiscard]] ObjectId<CI,CM> getRequired (ObjectRange<CI,CM> metMap, const std::string& metKey) const
54 {
55 auto result = metMap.getXAODObject()[metKey];
56 if (!result)
57 throw std::runtime_error ("MET object does not have the requested term: " + metKey);
58 return *result;
59 }
60
61 ObjectId<CI> fillMET(ObjectRange<CI,CM> metMap, const std::string& metKey, const MissingETBase::Types::bitmask_t metSource) const {
62 xAOD::MissingET *metPtr = nullptr;
63 if (met::fillMET(metPtr, &metMap.getXAODObject(), metKey, metSource).isFailure())
64 throw std::runtime_error ("failed to fill MET term \"" + metKey + "\"");
65 return ObjectId<CI> (*metPtr);}
66
67 StatusCode tryCreateIfMissing (ObjectRange<CI,CM> metMap, const std::string& metKey, const MissingETBase::Types::bitmask_t metSource) const
68 {
69 auto iter = metMap.getXAODObject().find (metKey);
70 if (iter != metMap.getXAODObject().end() && *iter != nullptr)
71 return StatusCode::SUCCESS;
72 xAOD::MissingET *metPtr = nullptr;
73 if (met::fillMET(metPtr, &metMap.getXAODObject(), metKey, metSource).isFailure())
74 return StatusCode::FAILURE;
75 return StatusCode::SUCCESS;
76 }
77 };
78
79 template<ContainerIdConcept CI>
81 {
82 public:
84
85 private:
87
88 // For output MET terms we have to pre-place all the MET terms in
89 // the MET container, just so that we don't have to allocate them
90 // dynamically. To indicate whether a pre-placed MET term exists
91 // the source bitmask is used and checked against m_nullSource.
92 // This should be set to whatever an invalid source value is.
93 // Technically we may not need this for input MET terms, but it
94 // seems prudent to use it there as well for consistency. Ideally
95 // for output MET terms this should be an `update` mode, but I
96 // don't have that defined yet.
99
100 public:
101 MapLookupAccessor (ColumnarTool<CM>& columnBase) : m_nameHashAcc (columnBase, "nameHash"), m_sourceAcc (columnBase, "source") {}
102
103 [[nodiscard]] OptObjectId<CI,CM> operator () (ObjectRange<CI,CM> metMap, const std::string& metKey) const
104 {
105 auto hash = std::hash<std::string>()(metKey);
106 for (auto metObj : metMap)
107 {
108 if (m_nameHashAcc(metObj) == hash)
109 {
110 if (m_sourceAcc(metObj) != m_nullSource)
111 return metObj;
112 else
113 return std::nullopt;
114 }
115 }
116 return std::nullopt;
117 }
118
119 [[nodiscard]] ObjectId<CI,CM> getRequired (ObjectRange<CI,CM> metMap, const std::string& metKey) const
120 {
121 auto result = operator() (metMap, metKey);
122 if (!result)
123 throw std::runtime_error ("MET object does not have the requested term: " + metKey);
124 return result.value();
125 }
126
127 ObjectId<CI> fillMET(ObjectRange<CI,CM> metMap, const std::string& metKey, const MissingETBase::Types::bitmask_t metSource) const {
128 auto hash = std::hash<std::string>()(metKey);
129 for (auto metObj : metMap)
130 {
131 if (m_nameHashAcc(metObj) == hash)
132 {
133 if (m_sourceAcc(metObj) != m_nullSource)
134 throw std::runtime_error ("MET object already has the requested term: " + metKey);
135 m_sourceAcc(metObj) = metSource;
136 return metObj;
137 }
138 }
139 throw std::runtime_error ("MET object does not have the requested term pre-placed: " + metKey);
140 }
141
142 StatusCode tryCreateIfMissing (ObjectRange<CI,CM> metMap, const std::string& metKey, const MissingETBase::Types::bitmask_t metSource) const
143 {
144 auto hash = std::hash<std::string>()(metKey);
145 for (auto metObj : metMap)
146 {
147 if (m_nameHashAcc(metObj) == hash)
148 {
149 if (m_sourceAcc(metObj) == m_nullSource)
150 m_sourceAcc(metObj) = metSource;
151 return StatusCode::SUCCESS;
152 }
153 }
154 throw std::runtime_error ("MET object does not have the requested term pre-placed: " + metKey);
155 }
156 };
157
158
159
166 template<ContainerIdConcept CI = ContainerId::met,typename CM=ColumnarModeDefault>
168 {
169 static constexpr bool isMutable = CI::isMutable;
171
172
174 : mpx (columnarBase, "mpx"),
175 mpy (columnarBase, "mpy"),
176 sumet (columnarBase, "sumet")
177 {}
178
182
183 [[nodiscard]] double met (ObjectId<CI,CM> object) const {
184 return std::hypot(mpx(object), mpy(object));}
185 [[nodiscard]] double phi (ObjectId<CI,CM> object) const {
186 return std::atan2(mpy(object), mpx(object));}
187
188 void addParticle (ObjectId<CI,CM> met, float px,float py,float pt) const requires (isMutable) {
189 mpx(met) -= px; mpy(met) -= py; sumet(met) += pt;}
190 void addParticle (ObjectId<CI,CM> met, const xAOD::IParticle& particle) const requires (isMutable) {
191 auto p4 = particle.p4();
192 addParticle (met, p4.Px(), p4.Py(), particle.pt());}
193 template<ContainerIdConcept CI2,typename MomAcc>
194 void addParticle (ObjectId<CI,CM> met, const MomAcc& momAcc, const ObjectId<CI2,CM>& object) const requires (isMutable) && requires(const MomAcc& acc,ObjectId<CI2,CM> object) {float(acc.px(object));float(acc.py(object));float(acc.pt(object));} {
195 this->addParticle(met, momAcc.px(object), momAcc.py(object), momAcc.pt(object));}
196 template<ContainerIdConcept CI2>
197 void addMet (ObjectId<CI,CM> met, const MetMomentumAccessors<CI2,CM>& momAcc, ObjectId<CI2,CM> metSource) const requires (isMutable) {
198 mpx(met) += momAcc.mpx(metSource); mpy(met) += momAcc.mpy(metSource); sumet(met) += momAcc.sumet(metSource);}
199 };
200 }
201}
202
203#endif
the raw column accessor template class
the base class for all columnar components
StatusCode tryCreateIfMissing(ObjectRange< CI, CM > metMap, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource) const
Definition MetHelpers.h:142
AccessorTemplate< CI, MissingETBase::Types::bitmask_t, CI::isMutable?ColumnAccessMode::output:ColumnAccessMode::input, CM > m_sourceAcc
Definition MetHelpers.h:98
ObjectId< CI > fillMET(ObjectRange< CI, CM > metMap, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource) const
Definition MetHelpers.h:127
static constexpr MissingETBase::Types::bitmask_t m_nullSource
Definition MetHelpers.h:97
ObjectId< CI, CM > getRequired(ObjectRange< CI, CM > metMap, const std::string &metKey) const
Definition MetHelpers.h:119
ObjectId< CI > fillMET(ObjectRange< CI, CM > metMap, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource) const
Definition MetHelpers.h:61
ObjectId< CI, CM > getRequired(ObjectRange< CI, CM > metMap, const std::string &metKey) const
Definition MetHelpers.h:53
StatusCode tryCreateIfMissing(ObjectRange< CI, CM > metMap, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource) const
Definition MetHelpers.h:67
a special "accessor" that allows to do the MET term lookup by name
Definition MetHelpers.h:38
a class representing a single object (electron, muons, etc.)
a class representing a continuous sequence of objects (a.k.a. a container)
a class representing a single optional object (electron, muons, etc.)
Class providing the definition of the 4-vector interface.
uint64_t bitmask_t
Type for status word bit mask.
AccessorTemplate< CI, CT, ColumnAccessMode::input, CM > ColumnAccessor
ColumnAccessMode
an enum for the different access modes for a column
Definition ColumnInfo.h:19
@ output
an output column
Definition ColumnInfo.h:24
@ input
an input column
Definition ColumnInfo.h:21
StatusCode fillMET(xAOD::MissingET *&met, xAOD::MissingETContainer *metCont, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource)
MissingET_v1 MissingET
Version control by type defintion.
double met(ObjectId< CI, CM > object) const
Definition MetHelpers.h:183
void addParticle(ObjectId< CI, CM > met, const xAOD::IParticle &particle) const
Definition MetHelpers.h:190
void addMet(ObjectId< CI, CM > met, const MetMomentumAccessors< CI2, CM > &momAcc, ObjectId< CI2, CM > metSource) const
Definition MetHelpers.h:197
void addParticle(ObjectId< CI, CM > met, const MomAcc &momAcc, const ObjectId< CI2, CM > &object) const
Definition MetHelpers.h:194
AccessorTemplate< CI, float, CAM, CM > sumet
Definition MetHelpers.h:181
MetMomentumAccessors(ColumnarTool< CM > &columnarBase)
Definition MetHelpers.h:173
AccessorTemplate< CI, float, CAM, CM > mpy
Definition MetHelpers.h:180
static constexpr ColumnAccessMode CAM
Definition MetHelpers.h:170
double phi(ObjectId< CI, CM > object) const
Definition MetHelpers.h:185
AccessorTemplate< CI, float, CAM, CM > mpx
Definition MetHelpers.h:179
void addParticle(ObjectId< CI, CM > met, float px, float py, float pt) const
Definition MetHelpers.h:188
std::map< std::string, HypoJetVector >::const_iterator CI