00001
00002 #ifndef XAODJET_JETACCESSORS_H
00003 #define XAODJET_JETACCESSORS_H
00022
00023 #include <string>
00024 #include "AthContainers/AuxElement.h"
00025 #include "xAODBase/IParticle.h"
00026 #include "xAODJet/JetTypes.h"
00027
00028 using SG::AuxElement;
00029 namespace xAOD {
00030
00031
00032 namespace JetAttributeAccessor {
00033
00034
00035 struct Named {
00036 Named(const std::string & n) : m_name(n){}
00037 std::string name(){return m_name;}
00038 protected:
00039 std::string m_name;
00040 };
00041
00042 template<class TYPE>
00043 class AccessorWrapper : public Named {
00044 public:
00045 typedef typename SG::AuxElement::Accessor< TYPE > AccessorType;
00046 AccessorWrapper(const std::string & n) : Named(n), m_a(n) {}
00047
00048 void setAttribute(SG::AuxElement& p, const TYPE& v){
00049 m_a(p) = v;
00050 }
00051
00052 void getAttribute(const SG::AuxElement& p, TYPE& v){
00053 v = m_a(p);
00054 }
00055
00056 const TYPE & getAttribute(const SG::AuxElement& p){
00057 return m_a(p);
00058 }
00059
00060 bool isAvailable(const SG::AuxElement& p){ return m_a.isAvailable(p);}
00061
00062
00063 const TYPE& operator()(const SG::AuxElement& p) { return m_a(p);}
00064 TYPE& operator()(SG::AuxElement& p) { return m_a(p);}
00065
00066 protected:
00067 AccessorType m_a;
00068 };
00069
00071 template<>
00072 class AccessorWrapper<double> : public Named {
00073 public:
00074 typedef SG::AuxElement::Accessor< float > AccessorType;
00075 AccessorWrapper(const std::string & n) : Named(n) , m_a(n) {}
00076
00077 void setAttribute(SG::AuxElement& p, const double& v){
00078 m_a(p) = v;
00079 }
00080
00081 void getAttribute(const SG::AuxElement& p, double& v){
00082 v = m_a(p);
00083 }
00084
00085
00086 double getAttribute(const SG::AuxElement& p){
00087 return m_a(p);
00088 }
00089
00090
00091 bool isAvailable(const SG::AuxElement& p){ return m_a.isAvailable(p);}
00092
00093 protected:
00094 AccessorType m_a;
00095 };
00096
00097
00099 template<>
00100 class AccessorWrapper< std::vector<double> > : public Named{
00101 public:
00102 typedef SG::AuxElement::Accessor< std::vector<float> > AccessorType;
00103 AccessorWrapper(const std::string & n) : Named(n), m_a(n) {}
00104
00105 void setAttribute(SG::AuxElement& p, const std::vector<double>& v){
00106 m_a(p).assign( v.begin() , v.end() );
00107 }
00108
00109 void getAttribute(const SG::AuxElement& p, std::vector<double>& v){
00110 const std::vector<float> & vecF = m_a(p);
00111 v.assign( vecF.begin() , vecF.end() );
00112 }
00113
00114
00115 std::vector<double> getAttribute(const SG::AuxElement& p){
00116 std::vector<double> v; getAttribute(p,v);
00117 return v;
00118 }
00119
00120
00121 bool isAvailable(const SG::AuxElement& p){ return m_a.isAvailable(p);}
00122
00123 protected:
00124 AccessorType m_a;
00125 };
00126
00127
00128
00129
00132 class FourMomAccessor : public Named {
00133 public:
00134 FourMomAccessor(const std::string& name, const std::string& n0, const std::string& n1, const std::string& n2, const std::string& n3) :Named(name) , m_p0(n0), m_p1(n1), m_p2(n2), m_p3(n3) {}
00135
00136 bool isAvailable(const SG::AuxElement& e) const {return m_p0.isAvailable(e);}
00137
00138 protected:
00139 SG::AuxElement::Accessor< float > m_p0;
00140 SG::AuxElement::Accessor< float > m_p1;
00141 SG::AuxElement::Accessor< float > m_p2;
00142 SG::AuxElement::Accessor< float > m_p3;
00143 };
00144
00145
00148 template<>
00149 class AccessorWrapper<JetFourMom_t> : public FourMomAccessor {
00150 public:
00151 AccessorWrapper() : FourMomAccessor("_unnamed_","pt", "eta","phi", "m") {}
00152 AccessorWrapper(const std::string &name) : FourMomAccessor(name, name+"_pt", name+"_eta",name+"_phi", name+"_m") {}
00153
00154 const float & pt(const SG::AuxElement& p){ return m_p0(p);}
00155 const float & eta(const SG::AuxElement& p){ return m_p1(p);}
00156 const float & phi(const SG::AuxElement& p){ return m_p2(p);}
00157 const float & m(const SG::AuxElement& p){ return m_p3(p);}
00158
00159 void setAttribute(SG::AuxElement& p, const JetFourMom_t& v){
00160 m_p0(p) = v.Pt();
00161 m_p1(p) = v.Eta();
00162 m_p2(p) = v.Phi();
00163 m_p3(p) = v.M();
00164 }
00165
00166 void getAttribute(const SG::AuxElement& p, JetFourMom_t& v){
00167 v.SetPt( m_p0(p) );
00168 v.SetEta( m_p1(p) );
00169 v.SetPhi( m_p2(p) );
00170 v.SetM( m_p3(p) );
00171 }
00172
00173 JetFourMom_t getAttribute(const SG::AuxElement &p){
00174 JetFourMom_t v; getAttribute(p,v);
00175 return v;
00176 }
00177
00178
00179 void setPtEtaPhiM(SG::AuxElement& p, float pt, float eta, float phi, float m ){
00180 m_p0(p) = pt ;
00181 m_p1(p) = eta ;
00182 m_p2(p) = phi ;
00183 m_p3(p) = m ;
00184 }
00185
00186
00187 };
00189
00190
00193 template<>
00194 class AccessorWrapper<IParticle::FourMom_t> : public FourMomAccessor {
00195 public:
00196 AccessorWrapper(const std::string &name) : FourMomAccessor(name, name+"_px", name+"_py",name+"_pz", name+"_e") {}
00197 void setAttribute(SG::AuxElement& p, const IParticle::FourMom_t& v){
00198 m_p0(p) = v.Px();
00199 m_p1(p) = v.Py();
00200 m_p2(p) = v.Pz();
00201 m_p3(p) = v.E();
00202 }
00203
00204 void getAttribute(const SG::AuxElement& p, IParticle::FourMom_t& v){
00205 v.SetPx( m_p0(p) );
00206 v.SetPy( m_p1(p) );
00207 v.SetPz( m_p2(p) );
00208 v.SetE( m_p3(p) );
00209 }
00210
00211 IParticle::FourMom_t getAttribute(const SG::AuxElement &p){
00212 IParticle::FourMom_t v; getAttribute(p,v);
00213 return v;
00214 }
00215
00216 };
00218
00219
00220
00222 namespace {
00225
00229 template<typename Obj>
00230 class InheritsIParticle
00231 {
00232
00233 class No { };
00234 class Yes { No no[3]; };
00235
00236 static Yes testFunc( IParticle* );
00237 static No testFunc( ... );
00238
00239 public:
00240 enum { Test = sizeof(testFunc(static_cast<Obj*>(0))) == sizeof(Yes) };
00241 };
00242
00243
00245 template<typename Obj, bool IsIP>
00246 struct InternalTypes {
00247 typedef DataVector<Obj> ContainerType;
00248 typedef ElementLink< ContainerType > LinkType;
00249 typedef SG::AuxElement::Accessor< LinkType > AccessorType;
00250 static const Obj* fromEL(const LinkType&el){if(el.isValid())return *el; return NULL;}
00251 };
00252
00253 template<typename Obj>
00254 struct InternalTypes<Obj,true> {
00255 typedef IParticleContainer ContainerType;
00256 typedef ElementLink< ContainerType > LinkType;
00257 typedef SG::AuxElement::Accessor< LinkType > AccessorType;
00258 static const Obj* fromEL(const LinkType&el){if(el.isValid()) return dynamic_cast<const Obj*>(*el); return NULL;}
00259 };
00260
00261 template<typename Obj, bool IsIP>
00262 struct InternalVectorTypes : public InternalTypes<Obj,IsIP> {
00263 typedef typename InternalTypes<Obj,IsIP>::LinkType LinkType;
00264 typedef SG::AuxElement::Accessor< std::vector<LinkType> > AccessorType;
00265 };
00266
00267
00268
00269 }
00270
00271
00272 template<class TYPE>
00273 class ObjectAccessorWrapper: public Named {
00274 public:
00275 typedef InternalTypes<TYPE, InheritsIParticle<TYPE>::Test> InternalType;
00276 typedef typename InternalType::ContainerType ContainerType;
00277 typedef typename InternalType::LinkType LinkType;
00278 typedef typename InternalType::AccessorType AccessorType;
00279
00280 ObjectAccessorWrapper(const std::string & n) : Named(n), m_a(n) {}
00281
00282 void setAttribute(SG::AuxElement& p, const TYPE* o){
00283 LinkType &el = m_a(p);
00284 el.toIndexedElement( *( dynamic_cast< const ContainerType* >( o->container() ) ), o->index() );
00285 el.toPersistent();
00286 }
00287
00288
00289 const TYPE * getAttribute(const SG::AuxElement& p){
00290 return InternalType::fromEL( m_a(p) );
00291 }
00292
00293 void getAttribute(const SG::AuxElement& p, const TYPE *& att){
00294 att= InternalType::fromEL( m_a(p) );
00295 }
00296
00297 bool isAvailable(const SG::AuxElement& p){ return m_a.isAvailable(p);}
00298
00299
00300 const TYPE* operator()(const SG::AuxElement& p) { return getAttribute(p); }
00301
00302
00303 protected:
00304 AccessorType m_a;
00305 };
00306
00316 template<class TYPE>
00317 class ObjectAccessorWrapper<std::vector<const TYPE*> > : public Named {
00318 public:
00319 typedef InternalVectorTypes<TYPE, InheritsIParticle<TYPE>::Test> InternalType;
00320 typedef typename InternalType::ContainerType ContainerType;
00321 typedef typename InternalType::LinkType LinkType;
00322 typedef typename InternalType::AccessorType AccessorType;
00323
00324 ObjectAccessorWrapper(const std::string & n) : Named(n), m_a(n) {}
00325
00326
00327 void vector2vectorEL(const std::vector<const TYPE*> & vec, std::vector< LinkType > & elv) {
00328
00329 for(size_t i=0; i< vec.size() ; i++) {
00330 LinkType el;
00331 el.toIndexedElement( *( dynamic_cast< const ContainerType* >( vec[i]->container() ) ), vec[i]->index() );
00332 elv.push_back(el);
00333 elv.back().toPersistent();
00334 }
00335 }
00336
00337 void setAttribute(SG::AuxElement& p, const std::vector<const TYPE*> &vec){
00338 std::vector<LinkType> &elv = m_a(p); elv.clear();elv.reserve(vec.size());
00339 this->vector2vectorEL(vec, elv);
00340 }
00341
00342 void getAttribute(const SG::AuxElement& p, std::vector<const TYPE*>& v){
00343 const std::vector<LinkType> &elv = m_a(p);
00344 v.resize(elv.size());
00345 for(size_t i=0;i<elv.size(); i++) {v[i] = InternalType::fromEL(elv[i]) ; }
00346 }
00347
00348 std::vector<const TYPE *> getAttribute(const SG::AuxElement& p){
00349 const std::vector<LinkType> &elv = m_a(p);
00350 std::vector<const TYPE*> ipvec(elv.size() );
00351 for(size_t i=0;i<elv.size(); i++) ipvec[i] = InternalType::fromEL(elv[i]) ;
00352 return ipvec;
00353 }
00354
00355 bool isAvailable(const SG::AuxElement& p){ return m_a.isAvailable(p);}
00356
00357
00358
00359
00360
00361 protected:
00362 AccessorType m_a;
00363 };
00364
00365
00366
00367
00368 }
00369 }
00370 #endif