ATLAS Offline Software
Loading...
Searching...
No Matches
JetAccessors.h
Go to the documentation of this file.
1// Dear emacs, this is -*- c++ -*-
2
3/*
4 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5*/
6
7#ifndef XAODJET_JETACCESSORS_H
8#define XAODJET_JETACCESSORS_H
27
28#include <string>
29#include <type_traits>
31#include "AthLinks/ElementLink.h"
32#include "xAODBase/IParticle.h"
33#include "xAODJet/JetTypes.h"
34
35namespace xAOD {
36
37
38 namespace JetAttributeAccessor {
39
40
41 struct Named {
42 Named(const std::string & n) : m_name(n){}
43 const std::string& name() const {return m_name;}
44 protected:
45 std::string m_name;
46 };
47
48 template<class TYPE>
49 class AccessorWrapper : public Named {
50 public:
52 AccessorWrapper(const std::string & n) : Named(n), m_a(n) {}
53
54 void setAttribute(SG::AuxElement& p, const TYPE& v) const {
55 m_a(p) = v;
56 }
57
58 void getAttribute(const SG::AuxElement& p, TYPE& v) const {
59 v = m_a(p);
60 }
61
62 const TYPE & getAttribute(const SG::AuxElement& p) const {
63 return m_a(p);
64 }
65
66 bool isAvailable(const SG::AuxElement& p) const { return m_a.isAvailable(p);}
67
68 // forward calls to internal accessor. useful for performance critical code.
69 const TYPE& operator()(const SG::AuxElement& p) const { return m_a(p);}
70 TYPE& operator()(SG::AuxElement& p) const { return m_a(p);}
71
72 protected:
74 };
75
77 template<>
78 class AccessorWrapper<double> : public Named {
79 public:
81 AccessorWrapper(const std::string & n) : Named(n) , m_a(n) {}
82
83 void setAttribute(SG::AuxElement& p, const double& v) const {
84 m_a(p) = v;
85 }
86
87 void getAttribute(const SG::AuxElement& p, double& v) const {
88 v = m_a(p);
89 }
90
91 // can't return a reference, since the internal is a float.
92 double getAttribute(const SG::AuxElement& p) const {
93 return m_a(p);
94 }
95
96
97 bool isAvailable(const SG::AuxElement& p) const { return m_a.isAvailable(p);}
98
99 protected:
101 };
102
103
105 template<>
106 class AccessorWrapper< std::vector<double> > : public Named{
107 public:
109 AccessorWrapper(const std::string & n) : Named(n), m_a(n) {}
110
111 void setAttribute(SG::AuxElement& p, const std::vector<double>& v) const {
112 m_a(p).assign( v.begin() , v.end() );
113 }
114
115 void getAttribute(const SG::AuxElement& p, std::vector<double>& v) const {
116 const std::vector<float> & vecF = m_a(p);
117 v.assign( vecF.begin() , vecF.end() );
118 }
119
120 // can't return a reference, since the internal is a float.
121 std::vector<double> getAttribute(const SG::AuxElement& p) const {
122 std::vector<double> v; getAttribute(p,v);
123 return v;
124 }
125
126
127 bool isAvailable(const SG::AuxElement& p) const { return m_a.isAvailable(p);}
128
129 protected:
131 };
132
133
134
135
138 class FourMomAccessor : public Named {
139 public:
140 FourMomAccessor(const std::string& name, const std::string& n0, const std::string& n1,
141 const std::string& n2, const std::string& n3) :Named(name) , m_p0(n0), m_p1(n1), m_p2(n2), m_p3(n3) {}
142
143 bool isAvailable(const SG::AuxElement& e) const {return m_p0.isAvailable(e);}
144
145 protected:
150 };
151
152
155 template<>
157 public:
158 AccessorWrapper() : FourMomAccessor("_unnamed_","pt", "eta","phi", "m") {}
159 AccessorWrapper(const std::string &name) : FourMomAccessor(name, name+"_pt", name+"_eta",name+"_phi", name+"_m") {}
160
161 const float & pt(const SG::AuxElement& p) const { return m_p0(p);}
162 const float & eta(const SG::AuxElement& p) const { return m_p1(p);}
163 const float & phi(const SG::AuxElement& p) const { return m_p2(p);}
164 const float & m(const SG::AuxElement& p) const { return m_p3(p);}
165
166 void setAttribute(SG::AuxElement& p, const JetFourMom_t& v) const {
167 m_p0(p) = v.Pt();
168 m_p1(p) = v.Eta();
169 m_p2(p) = v.Phi();
170 m_p3(p) = v.M();
171 }
172
173 void getAttribute(const SG::AuxElement& p, JetFourMom_t& v) const {
174 v.SetPt( m_p0(p) );
175 v.SetEta( m_p1(p) );
176 v.SetPhi( m_p2(p) );
177 v.SetM( m_p3(p) );
178 }
179
182 return v;
183 }
184
185 // other convenience function
186 void setPtEtaPhiM(SG::AuxElement& p, float pt, float eta, float phi, float m ){
187 m_p0(p) = pt ;
188 m_p1(p) = eta ;
189 m_p2(p) = phi ;
190 m_p3(p) = m ;
191 }
192
193
194 };
195
196
197
200 template<>
201 class AccessorWrapper<IParticle::FourMom_t> : public FourMomAccessor {
202 public:
203 AccessorWrapper(const std::string &name) : FourMomAccessor(name, name+"_px", name+"_py",name+"_pz", name+"_e") {}
205 m_p0(p) = v.Px();
206 m_p1(p) = v.Py();
207 m_p2(p) = v.Pz();
208 m_p3(p) = v.E();
209 }
210
212 v.SetPx( m_p0(p) );
213 v.SetPy( m_p1(p) );
214 v.SetPz( m_p2(p) );
215 v.SetE( m_p3(p) );
216 }
217
220 return v;
221 }
222
223 };
224
225
226
227
229 namespace {
231
233 template<typename Obj, bool IsIP>
234 struct InternalTypes {
235 typedef DataVector<Obj> ContainerType;
236 typedef ElementLink< ContainerType > LinkType;
237 typedef SG::AuxElement::Accessor< LinkType > AccessorType;
238 static const Obj* fromEL(const LinkType&el){if(el.isValid())return *el; return NULL;}
239 };
240
241 template<typename Obj>
242 struct InternalTypes<Obj,true> {
243 typedef IParticleContainer ContainerType;
244 typedef ElementLink< ContainerType > LinkType;
245 typedef SG::AuxElement::Accessor< LinkType > AccessorType;
246 static const Obj* fromEL(const LinkType&el){if(el.isValid()) return dynamic_cast<const Obj*>(*el); return NULL;}
247 };
248
249 template<typename Obj, bool IsIP>
250 struct InternalVectorTypes : public InternalTypes<Obj,IsIP> {
251 typedef typename InternalTypes<Obj,IsIP>::LinkType LinkType;
253 };
254 }
255
256
257 template<class TYPE>
259 public:
260 typedef InternalTypes<TYPE, std::is_base_of<IParticle, TYPE>::value> InternalType;
261 typedef typename InternalType::ContainerType ContainerType;
262 typedef typename InternalType::LinkType LinkType;
263 typedef typename InternalType::AccessorType AccessorType;
264
265 ObjectAccessorWrapper(const std::string & n) : Named(n), m_a(n) {}
266
267 void setAttribute(SG::AuxElement& p, const TYPE* o) const {
268 LinkType &el = m_a(p);
269 el.toIndexedElement( *( dynamic_cast< const ContainerType* >( o->container() ) ), o->index() );
270 el.toPersistent();
271 }
272
273
274 const TYPE * getAttribute(const SG::AuxElement& p) const {
275 return InternalType::fromEL( m_a(p) );
276 }
277
278 void getAttribute(const SG::AuxElement& p, const TYPE *& att) const {
279 att= InternalType::fromEL( m_a(p) );
280 }
281
282 bool isAvailable(const SG::AuxElement& p) const { return m_a.isAvailable(p);}
283
284 // // forward calls to internal accessor. useful for performance critical code.
285 const TYPE* operator() (const SG::AuxElement& p) const { return getAttribute(p); }
286 // TYPE& operator()(SG::AuxElement& p) { LinkType &el=m_a(p); return *el;}
287
288 protected:
290 };
291
301 template<class TYPE>
302 class ObjectAccessorWrapper<std::vector<const TYPE*> > : public Named {
303 public:
304 typedef InternalVectorTypes<TYPE, std::is_base_of<IParticle, TYPE>::value> InternalType;
305 typedef typename InternalType::ContainerType ContainerType;
306 typedef typename InternalType::LinkType LinkType;
307 typedef typename InternalType::AccessorType AccessorType;
308
309 ObjectAccessorWrapper(const std::string & n) : Named(n), m_a(n) {}
310
311
312 void vector2vectorEL(const std::vector<const TYPE*> & vec, std::vector< LinkType > & elv) const {
313
314 for(size_t i=0; i< vec.size() ; i++) {
315 LinkType el;
316 el.toIndexedElement( *( dynamic_cast< const ContainerType* >( vec[i]->container() ) ), vec[i]->index() );
317 elv.push_back(el);
318 elv.back().toPersistent(); // The EL will not change anymore since it is purely internal. We can call toPersistent() to be sure it will be saved as expected.
319 }
320 }
321
322 void setAttribute(SG::AuxElement& p, const std::vector<const TYPE*> &vec) const {
323 std::vector<LinkType> &elv = m_a(p); elv.clear();elv.reserve(vec.size());
324 this->vector2vectorEL(vec, elv);
325 }
326
327 void getAttribute(const SG::AuxElement& p, std::vector<const TYPE*>& v) const {
328 const std::vector<LinkType> &elv = m_a(p);
329 v.resize(elv.size());
330 for(size_t i=0;i<elv.size(); i++) {v[i] = InternalType::fromEL(elv[i]) ; }
331 }
332
333 std::vector<const TYPE *> getAttribute(const SG::AuxElement& p) const {
334 const std::vector<LinkType> &elv = m_a(p);
335 std::vector<const TYPE*> ipvec(elv.size() );
336 for(size_t i=0;i<elv.size(); i++) ipvec[i] = InternalType::fromEL(elv[i]) ;
337 return ipvec;
338 }
339
340 bool isAvailable(const SG::AuxElement& p) const { return m_a.isAvailable(p);}
341
342 // // forward calls to internal accessor. useful for performance critical code.
343 // const TYPE* operator()(const AuxElement& p) { const LinkType &el = m_a(p) ;return *el;}
344 // TYPE& operator()(AuxElement& p) { LinkType &el=m_a(p); return *el;}
345
346 protected:
348 };
349
350
351
352
353 }// namespace JetAttributeAccessor
354}
355#endif
Base class for elements of a container that can have aux data.
std::vector< size_t > vec
#define TYPE(CODE, TYP, IOTYP)
Derived DataVector<T>.
Definition DataVector.h:795
Base class for elements of a container that can have aux data.
Definition AuxElement.h:484
SG::Accessor< T, ALLOC > Accessor
Definition AuxElement.h:573
Class providing the definition of the 4-vector interface.
TLorentzVector FourMom_t
Definition of the 4-momentum type.
void setAttribute(SG::AuxElement &p, const IParticle::FourMom_t &v) const
IParticle::FourMom_t getAttribute(const SG::AuxElement &p) const
void getAttribute(const SG::AuxElement &p, IParticle::FourMom_t &v) const
const float & eta(const SG::AuxElement &p) const
void setAttribute(SG::AuxElement &p, const JetFourMom_t &v) const
JetFourMom_t getAttribute(const SG::AuxElement &p) const
const float & m(const SG::AuxElement &p) const
void setPtEtaPhiM(SG::AuxElement &p, float pt, float eta, float phi, float m)
const float & pt(const SG::AuxElement &p) const
void getAttribute(const SG::AuxElement &p, JetFourMom_t &v) const
const float & phi(const SG::AuxElement &p) const
SG::AuxElement::Accessor< float > AccessorType
bool isAvailable(const SG::AuxElement &p) const
void getAttribute(const SG::AuxElement &p, double &v) const
void setAttribute(SG::AuxElement &p, const double &v) const
double getAttribute(const SG::AuxElement &p) const
std::vector< double > getAttribute(const SG::AuxElement &p) const
void setAttribute(SG::AuxElement &p, const std::vector< double > &v) const
void getAttribute(const SG::AuxElement &p, std::vector< double > &v) const
SG::AuxElement::Accessor< std::vector< float > > AccessorType
bool isAvailable(const SG::AuxElement &p) const
SG::AuxElement::Accessor< TYPE > AccessorType
void getAttribute(const SG::AuxElement &p, TYPE &v) const
void setAttribute(SG::AuxElement &p, const TYPE &v) const
TYPE & operator()(SG::AuxElement &p) const
const TYPE & getAttribute(const SG::AuxElement &p) const
const TYPE & operator()(const SG::AuxElement &p) const
FourMomAccessor(const std::string &name, const std::string &n0, const std::string &n1, const std::string &n2, const std::string &n3)
SG::AuxElement::Accessor< float > m_p3
SG::AuxElement::Accessor< float > m_p1
bool isAvailable(const SG::AuxElement &e) const
SG::AuxElement::Accessor< float > m_p2
SG::AuxElement::Accessor< float > m_p0
void getAttribute(const SG::AuxElement &p, std::vector< const TYPE * > &v) const
void setAttribute(SG::AuxElement &p, const std::vector< const TYPE * > &vec) const
void vector2vectorEL(const std::vector< const TYPE * > &vec, std::vector< LinkType > &elv) const
std::vector< const TYPE * > getAttribute(const SG::AuxElement &p) const
InternalVectorTypes< TYPE, std::is_base_of< IParticle, TYPE >::value > InternalType
const TYPE * operator()(const SG::AuxElement &p) const
void setAttribute(SG::AuxElement &p, const TYPE *o) const
const TYPE * getAttribute(const SG::AuxElement &p) const
void getAttribute(const SG::AuxElement &p, const TYPE *&att) const
bool isAvailable(const SG::AuxElement &p) const
InternalTypes< TYPE, std::is_base_of< IParticle, TYPE >::value > InternalType
Definition index.py:1
STL namespace.
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< double > > JetFourMom_t
Base 4 Momentum type for Jet.
Definition JetTypes.h:17
Named(const std::string &n)
const std::string & name() const