ATLAS Offline Software
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 
16 namespace columnar
17 {
18  namespace MetHelpers
19  {
37  template<ContainerIdConcept CI = ContainerId::met,typename CM=ColumnarModeDefault>
39 
40  template<ContainerIdConcept CI>
42  {
43  public:
45 
46  MapLookupAccessor (ColumnarTool<CM>& /*columnBase*/) {}
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.
97  static constexpr MissingETBase::Types::bitmask_t m_nullSource = 0;
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;}
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>
198  mpx(met) += momAcc.mpx(metSource); mpy(met) += momAcc.mpy(metSource); sumet(met) += momAcc.sumet(metSource);}
199  };
200  }
201 }
202 
203 #endif
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeArray >::m_sourceAcc
AccessorTemplate< CI, MissingETBase::Types::bitmask_t, CI::isMutable?ColumnAccessMode::output:ColumnAccessMode::input, CM > m_sourceAcc
Definition: MetHelpers.h:98
OptObjectId.h
createLinkingScheme.iter
iter
Definition: createLinkingScheme.py:62
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:79
columnar::ColumnAccessMode::input
@ input
an input column
get_generator_info.result
result
Definition: get_generator_info.py:21
test_pyathena.px
px
Definition: test_pyathena.py:18
columnar::MetHelpers::MetMomentumAccessors
a special accessor for the MET momentum
Definition: MetHelpers.h:168
columnar::MetHelpers::MetMomentumAccessors::CAM
static constexpr ColumnAccessMode CAM
Definition: MetHelpers.h:170
test_pyathena.pt
pt
Definition: test_pyathena.py:11
columnar::MetHelpers::MetMomentumAccessors::float
float(acc.py(object))
columnar::OptObjectId
a class representing a single optional object (electron, muons, etc.)
Definition: ContainerId.h:177
columnar::MetHelpers::MapLookupAccessor
a special "accessor" that allows to do the MET term lookup by name
Definition: MetHelpers.h:38
MissingETBase::Types::bitmask_t
uint64_t bitmask_t
Type for status word bit mask.
Definition: MissingETBase.h:39
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeXAOD >::MapLookupAccessor
MapLookupAccessor(ColumnarTool< CM > &)
Definition: MetHelpers.h:46
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
columnar::ObjectRange
a class representing a continuous sequence of objects (a.k.a. a container)
Definition: ContainerId.h:175
columnar::MetHelpers::MetMomentumAccessors::met
double met(ObjectId< CI, CM > object) const
Definition: MetHelpers.h:183
CI
std::map< std::string, HypoJetVector >::const_iterator CI
Definition: xAODJetCollector.h:18
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeArray >::m_nameHashAcc
ColumnAccessor< CI, std::size_t, CM > m_nameHashAcc
Definition: MetHelpers.h:86
columnar::ColumnAccessMode::output
@ output
an output column
METHelpers.h
met
Definition: IMETSignificance.h:24
columnar::MetHelpers::MetMomentumAccessors::sumet
AccessorTemplate< CI, float, CAM, CM > sumet
Definition: MetHelpers.h:181
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeArray >::getRequired
ObjectId< CI, CM > getRequired(ObjectRange< CI, CM > metMap, const std::string &metKey) const
Definition: MetHelpers.h:119
columnar::MetHelpers::MetMomentumAccessors::mpy
AccessorTemplate< CI, float, CAM, CM > mpy
Definition: MetHelpers.h:180
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ColumnAccessor.h
xAOD::MissingET_v1
Principal data object for Missing ET.
Definition: MissingET_v1.h:25
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeXAOD >::tryCreateIfMissing
StatusCode tryCreateIfMissing(ObjectRange< CI, CM > metMap, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource) const
Definition: MetHelpers.h:67
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
columnar::MetHelpers::MetMomentumAccessors::mpx
AccessorTemplate< CI, float, CAM, CM > mpx
Definition: MetHelpers.h:179
columnar::ColumnAccessMode
ColumnAccessMode
an enum for the different access modes for a column
Definition: ColumnInfo.h:19
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeArray >::MapLookupAccessor
MapLookupAccessor(ColumnarTool< CM > &columnBase)
Definition: MetHelpers.h:101
columnar::final
CM final
Definition: ColumnAccessor.h:106
columnar::ObjectId
a class representing a single object (electron, muons, etc.)
Definition: ContainerId.h:176
Amg::py
@ py
Definition: GeoPrimitives.h:39
columnar::MetHelpers::MetMomentumAccessors::phi
double phi(ObjectId< CI, CM > object) const
Definition: MetHelpers.h:185
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeXAOD >::fillMET
ObjectId< CI > fillMET(ObjectRange< CI, CM > metMap, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource) const
Definition: MetHelpers.h:61
columnar::ColumnarModeArray
Definition: ColumnarDef.h:30
columnar::MetHelpers::MetMomentumAccessors::addMet
void addMet(ObjectId< CI, CM > met, const MetMomentumAccessors< CI2, CM > &momAcc, ObjectId< CI2, CM > metSource) const requires(isMutable)
Definition: MetHelpers.h:197
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:108
columnar
Definition: ClusterDef.h:16
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeArray >::tryCreateIfMissing
StatusCode tryCreateIfMissing(ObjectRange< CI, CM > metMap, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource) const
Definition: MetHelpers.h:142
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeXAOD >::getRequired
ObjectId< CI, CM > getRequired(ObjectRange< CI, CM > metMap, const std::string &metKey) const
Definition: MetHelpers.h:53
columnar::ColumnarTool
the base class for all columnar components
Definition: ColumnAccessorDataArray.h:17
columnar::ColumnarModeXAOD
Definition: ColumnarDef.h:15
columnar::requires
requires requires
Definition: VariantAccessor.h:23
columnar::MetHelpers::MetMomentumAccessors::MetMomentumAccessors
MetMomentumAccessors(ColumnarTool< CM > &columnarBase)
Definition: MetHelpers.h:173
met::fillMET
StatusCode fillMET(xAOD::MissingET *&met, xAOD::MissingETContainer *metCont, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource)
Definition: METHelpers.cxx:123
columnar::MetHelpers::MetMomentumAccessors::isMutable
static constexpr bool isMutable
Definition: MetHelpers.h:169
columnar::AccessorTemplate
the raw column accessor template class
Definition: ColumnAccessor.h:81
columnar::operator()
decltype(auto) operator()(ObjectId< CI, CM > id) const noexcept
Definition: ColumnAccessor.h:173
columnar::MetHelpers::MapLookupAccessor< CI, ColumnarModeArray >::fillMET
ObjectId< CI > fillMET(ObjectRange< CI, CM > metMap, const std::string &metKey, const MissingETBase::Types::bitmask_t metSource) const
Definition: MetHelpers.h:127
columnar::MetHelpers::MetMomentumAccessors::addParticle
void addParticle(ObjectId< CI, CM > met, float px, float py, float pt) const requires(isMutable)
Definition: MetHelpers.h:188
columnar::MetHelpers::MetMomentumAccessors::addParticle
void addParticle(ObjectId< CI, CM > met, const xAOD::IParticle &particle) const requires(isMutable)
Definition: MetHelpers.h:190
MetDef.h