From 93f4ec96541d6b1a4775346e694f5085ba9923f0 Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Tue, 22 Oct 2013 10:53:40 +0200 Subject: [PATCH 01/10] Thread safe changes --- DataFormats/Candidate/interface/Particle.h | 100 ++++++++++----------- DataFormats/Candidate/src/Particle.cc | 94 +++++++++++++++++++ 2 files changed, 140 insertions(+), 54 deletions(-) diff --git a/DataFormats/Candidate/interface/Particle.h b/DataFormats/Candidate/interface/Particle.h index 8dee7f8bff2a0..ac76925a649c6 100755 --- a/DataFormats/Candidate/interface/Particle.h +++ b/DataFormats/Candidate/interface/Particle.h @@ -10,6 +10,9 @@ * \version $Id: Particle.h,v 1.29 2011/10/27 16:29:58 wmtan Exp $ * */ +#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) +#include +#endif #include "DataFormats/Math/interface/Point3D.h" #include "DataFormats/Math/interface/Vector3D.h" #include "DataFormats/Math/interface/LorentzVector.h" @@ -30,28 +33,20 @@ namespace reco { /// point in the space typedef math::XYZVector Vector; /// default constructor - Particle() : - qx3_(0), pt_(0), eta_(0), phi_(0), mass_(0), - vertex_(0, 0, 0), pdgId_(0), status_(0), - cachePolarFixed_( false ) { } + Particle(); /// constructor from values Particle( Charge q, const LorentzVector & p4, const Point & vertex = Point( 0, 0, 0 ), - int pdgId = 0, int status = 0, bool integerCharge = true ) : - qx3_( q ), pt_( p4.pt() ), eta_( p4.eta() ), phi_( p4.phi() ), mass_( p4.mass() ), - vertex_( vertex ), pdgId_( pdgId ), status_( status ), - cachePolarFixed_( false ), cacheCartesianFixed_( false ) { - if ( integerCharge ) qx3_ *= 3; - } + int pdgId = 0, int status = 0, bool integerCharge = true ); /// constructor from values Particle( Charge q, const PolarLorentzVector & p4, const Point & vertex = Point( 0, 0, 0 ), - int pdgId = 0, int status = 0, bool integerCharge = true ) : - qx3_( q ), pt_( p4.pt() ), eta_( p4.eta() ), phi_( p4.phi() ), mass_( p4.mass() ), - vertex_( vertex ), pdgId_( pdgId ), status_( status ), - cachePolarFixed_( false ), cacheCartesianFixed_( false ) { - if ( integerCharge ) qx3_ *= 3; - } + int pdgId = 0, int status = 0, bool integerCharge = true ); + void swap(Particle& other); /// destructor virtual ~Particle() { } + // copy ctor + Particle(const Particle& srv); + // assignment operator + Particle& operator=(const Particle& rhs); /// electric charge int charge() const { return qx3_ / 3; } /// set electric charge @@ -102,40 +97,13 @@ namespace reco { /// repidity double y() const { return rapidity(); } /// set 4-momentum - void setP4( const LorentzVector & p4 ) { - p4Cartesian_ = p4; - p4Polar_ = p4; - pt_ = p4Polar_.pt(); - eta_ = p4Polar_.eta(); - phi_ = p4Polar_.phi(); - mass_ = p4Polar_.mass(); - cachePolarFixed_ = true; - cacheCartesianFixed_ = true; - } + void setP4( const LorentzVector & p4 ); /// set 4-momentum - void setP4( const PolarLorentzVector & p4 ) { - p4Polar_ = p4; - pt_ = p4Polar_.pt(); - eta_ = p4Polar_.eta(); - phi_ = p4Polar_.phi(); - mass_ = p4Polar_.mass(); - cachePolarFixed_ = true; - cacheCartesianFixed_ = false; - } + void setP4( const PolarLorentzVector & p4 ); /// set particle mass - void setMass( double m ) { - mass_ = m; - clearCache(); - } - void setPz( double pz ) { - cacheCartesian(); - p4Cartesian_.SetPz(pz); - p4Polar_ = p4Cartesian_; - pt_ = p4Polar_.pt(); - eta_ = p4Polar_.eta(); - phi_ = p4Polar_.phi(); - mass_ = p4Polar_.mass(); - } + void setMass( double m ); + /// set Pz + void setPz( double pz ); /// vertex position const Point & vertex() const { return vertex_; } /// x coordinate of vertex position @@ -173,28 +141,52 @@ namespace reco { /// status word int status_; /// internal cache for p4 - mutable PolarLorentzVector p4Polar_; + mutable PolarLorentzVector p4Polar_; // CMS-THREADING protected by cachePolarFixed_ /// internal cache for p4 - mutable LorentzVector p4Cartesian_; + mutable LorentzVector p4Cartesian_; // CMS-THREADING protected by cacheCartesianFixed_ /// has cache been set? +#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) + mutable std::atomic cachePolarFixed_, cacheCartesianFixed_; +#else mutable bool cachePolarFixed_, cacheCartesianFixed_; +#endif /// set internal cache inline void cachePolar() const { +#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) + if(!cachePolarFixed_.load(std::memory_order_acquire)) { + p4Polar_ = PolarLorentzVector( pt_, eta_, phi_, mass_ ); + cachePolarFixed_.store(true, std::memory_order_release); + } +#else if ( cachePolarFixed_ ) return; p4Polar_ = PolarLorentzVector( pt_, eta_, phi_, mass_ ); - cachePolarFixed_ = true; + cachePolarFixed_; +#endif } /// set internal cache inline void cacheCartesian() const { +#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) + if(!cacheCartesianFixed_.load(std::memory_order_acquire)) { + cachePolar(); + p4Cartesian_ = p4Polar_; + cacheCartesianFixed_.store(true, std::memory_order_release); + } +#else if ( cacheCartesianFixed_ ) return; cachePolar(); p4Cartesian_ = p4Polar_; - cacheCartesianFixed_ = true; + cacheCartesianFixed_; +#endif } /// clear internal cache inline void clearCache() const { - cachePolarFixed_ = false; - cacheCartesianFixed_ = false; +#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) + cachePolarFixed_.store(false, std::memory_order_release); + cacheCartesianFixed_.store(false, std::memory_order_release); +#else + cachePolarFixed_; + cacheCartesianFixed_; +#endif } }; diff --git a/DataFormats/Candidate/src/Particle.cc b/DataFormats/Candidate/src/Particle.cc index 71a37155f4643..7368639b31d46 100755 --- a/DataFormats/Candidate/src/Particle.cc +++ b/DataFormats/Candidate/src/Particle.cc @@ -1,3 +1,97 @@ #include "DataFormats/Candidate/interface/Particle.h" const unsigned int reco::Particle::longLivedTag = 65536; + +namespace reco { + +/// default constructor +Particle::Particle() + : qx3_(0), pt_(0), eta_(0), phi_(0), mass_(0), + vertex_(0, 0, 0), pdgId_(0), status_(0) +{ + cachePolarFixed_.store(false, std::memory_order_release); + cacheCartesianFixed_.store(false, std::memory_order_release); +} +/// constructor from values +Particle::Particle( Charge q, const LorentzVector & p4, const Point & vertex, + int pdgId, int status, bool integerCharge) + : qx3_( q ), pt_( p4.pt() ), eta_( p4.eta() ), phi_( p4.phi() ), mass_( p4.mass() ), + vertex_( vertex ), pdgId_( pdgId ), status_( status ) +{ + cachePolarFixed_.store(false, std::memory_order_release); + cacheCartesianFixed_.store(false, std::memory_order_release); + if ( integerCharge ) qx3_ *= 3; +} +/// constructor from values +Particle::Particle( Charge q, const PolarLorentzVector & p4, const Point & vertex, + int pdgId, int status, bool integerCharge) + : qx3_( q ), pt_( p4.pt() ), eta_( p4.eta() ), phi_( p4.phi() ), mass_( p4.mass() ), + vertex_( vertex ), pdgId_( pdgId ), status_( status ) { + cachePolarFixed_.store(false, std::memory_order_release); + cacheCartesianFixed_.store(false, std::memory_order_release); + if ( integerCharge ) qx3_ *= 3; +} +// copy-ctor +Particle::Particle(const Particle& src) + : qx3_(src.qx3_), pt_(src.pt_), eta_(src.eta_), phi_(src.phi_), mass_(src.mass_), + vertex_(src.vertex_), pdgId_(src.pdgId_), status_(src.status_) { + cachePolarFixed_.store(false, std::memory_order_release); + cacheCartesianFixed_.store(false, std::memory_order_release); +} +// copy assignment operator +Particle& +Particle::operator=(const Particle& rhs) { + Particle temp(rhs); + temp.swap(*this); + return *this; +} +// public swap function +void Particle::swap(Particle& other) { + std::swap(qx3_, other.qx3_); + std::swap(pt_, other.pt_); + std::swap(eta_, other.eta_); + std::swap(phi_, other.phi_); + std::swap(mass_, other.mass_); + std::swap(vertex_, other.vertex_); + std::swap(pdgId_, other.pdgId_); + std::swap(status_, other.status_); + other.cachePolarFixed_.exchange(cachePolarFixed_.exchange(other.cachePolarFixed_)); + other.cacheCartesianFixed_.exchange(cachePolarFixed_.exchange(other.cachePolarFixed_)); +} + +/// set 4-momentum +void Particle::setP4( const LorentzVector & p4 ) { + p4Cartesian_ = p4; + p4Polar_ = p4; + pt_ = p4Polar_.pt(); + eta_ = p4Polar_.eta(); + phi_ = p4Polar_.phi(); + mass_ = p4Polar_.mass(); + cachePolarFixed_.store(true, std::memory_order_release); + cacheCartesianFixed_.store(true, std::memory_order_release); +} +/// set 4-momentum +void Particle::setP4( const PolarLorentzVector & p4 ) { + p4Polar_ = p4; + pt_ = p4Polar_.pt(); + eta_ = p4Polar_.eta(); + phi_ = p4Polar_.phi(); + mass_ = p4Polar_.mass(); + cachePolarFixed_.store(true, std::memory_order_release); + cacheCartesianFixed_.store(false, std::memory_order_release); +} +/// set particle mass +void Particle::setMass( double m ) { + mass_ = m; + clearCache(); +} +void Particle::setPz( double pz ) { + cacheCartesian(); + p4Cartesian_.SetPz(pz); + p4Polar_ = p4Cartesian_; + pt_ = p4Polar_.pt(); + eta_ = p4Polar_.eta(); + phi_ = p4Polar_.phi(); + mass_ = p4Polar_.mass(); +} +} // end of reco namespace From 5a2b521130a9140099788d2e3ebb39bb664ab0e4 Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Thu, 7 Nov 2013 10:45:25 -0500 Subject: [PATCH 02/10] Convert p4Polar_ and p4Cartesian_ to atomic pointers; re-factor code accordingly --- DataFormats/Candidate/interface/Particle.h | 123 ++++------ DataFormats/Candidate/src/Particle.cc | 259 ++++++++++++++++++--- DataFormats/Candidate/src/classes_def.xml | 8 - 3 files changed, 270 insertions(+), 120 deletions(-) diff --git a/DataFormats/Candidate/interface/Particle.h b/DataFormats/Candidate/interface/Particle.h index ac76925a649c6..bec66a1f3dc4e 100755 --- a/DataFormats/Candidate/interface/Particle.h +++ b/DataFormats/Candidate/interface/Particle.h @@ -42,60 +42,60 @@ namespace reco { int pdgId = 0, int status = 0, bool integerCharge = true ); void swap(Particle& other); /// destructor - virtual ~Particle() { } + virtual ~Particle(); // copy ctor Particle(const Particle& srv); // assignment operator Particle& operator=(const Particle& rhs); /// electric charge - int charge() const { return qx3_ / 3; } + int charge() const; /// set electric charge - void setCharge( Charge q ) { qx3_ = q * 3; } + void setCharge( Charge q ); /// electric charge - int threeCharge() const { return qx3_; } + int threeCharge() const; /// set electric charge - void setThreeCharge( Charge qx3 ) { qx3_ = qx3; } + void setThreeCharge( Charge qx3 ); /// four-momentum Lorentz vector - const LorentzVector & p4() const { cacheCartesian(); return p4Cartesian_; } + const LorentzVector & p4() const; /// four-momentum Lorentz vector - const PolarLorentzVector & polarP4() const { cachePolar(); return p4Polar_; } + const PolarLorentzVector & polarP4() const; /// spatial momentum vector - Vector momentum() const { cacheCartesian(); return p4Cartesian_.Vect(); } + Vector momentum() const; /// boost vector to boost a Lorentz vector /// to the particle center of mass system - Vector boostToCM() const { cacheCartesian(); return p4Cartesian_.BoostToCM(); } + Vector boostToCM() const; /// magnitude of momentum vector - double p() const { cacheCartesian(); return p4Cartesian_.P(); } + double p() const; /// energy - double energy() const { cacheCartesian(); return p4Cartesian_.E(); } + double energy() const; /// transverse energy - double et() const { cachePolar(); return p4Polar_.Et(); } + double et() const; /// mass - double mass() const { return mass_; } + double mass() const; /// mass squared - double massSqr() const { return mass_ * mass_; } + double massSqr() const; /// transverse mass - double mt() const { cachePolar(); return p4Polar_.Mt(); } + double mt() const; /// transverse mass squared - double mtSqr() const { cachePolar(); return p4Polar_.Mt2(); } + double mtSqr() const; /// x coordinate of momentum vector - double px() const { cacheCartesian(); return p4Cartesian_.Px(); } + double px() const; /// y coordinate of momentum vector - double py() const { cacheCartesian(); return p4Cartesian_.Py(); } + double py() const; /// z coordinate of momentum vector - double pz() const { cacheCartesian(); return p4Cartesian_.Pz(); } + double pz() const; /// transverse momentum - double pt() const { return pt_; } + double pt() const; /// momentum azimuthal angle - double phi() const { return phi_; } + double phi() const; /// momentum polar angle - double theta() const { cacheCartesian(); return p4Cartesian_.Theta(); } + double theta() const; /// momentum pseudorapidity - double eta() const { return eta_; } + double eta() const; /// repidity - double rapidity() const { cachePolar(); return p4Polar_.Rapidity(); } + double rapidity() const; /// repidity - double y() const { return rapidity(); } + double y() const; /// set 4-momentum void setP4( const LorentzVector & p4 ); /// set 4-momentum @@ -105,29 +105,29 @@ namespace reco { /// set Pz void setPz( double pz ); /// vertex position - const Point & vertex() const { return vertex_; } + const Point & vertex() const; /// x coordinate of vertex position - double vx() const { return vertex_.X(); } + double vx() const; /// y coordinate of vertex position - double vy() const { return vertex_.Y(); } + double vy() const; /// z coordinate of vertex position - double vz() const { return vertex_.Z(); } + double vz() const; /// set vertex - void setVertex( const Point & vertex ) { vertex_ = vertex; } + void setVertex( const Point & vertex ); /// PDG identifier - int pdgId() const { return pdgId_; } + int pdgId() const; // set PDG identifier - void setPdgId( int pdgId ) { pdgId_ = pdgId; } + void setPdgId( int pdgId ); /// status word - int status() const { return status_; } + int status() const; /// set status word - void setStatus( int status ) { status_ = status; } + void setStatus( int status ); /// long lived flag static const unsigned int longLivedTag; /// set long lived flag - void setLongLived() { status_ |= longLivedTag; } + void setLongLived(); /// is long lived? - bool longLived() const { return status_ & longLivedTag; } + bool longLived() const; protected: /// electric charge @@ -140,54 +140,23 @@ namespace reco { int pdgId_; /// status word int status_; +#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) /// internal cache for p4 - mutable PolarLorentzVector p4Polar_; // CMS-THREADING protected by cachePolarFixed_ + mutable std::atomic p4Polar_; /// internal cache for p4 - mutable LorentzVector p4Cartesian_; // CMS-THREADING protected by cacheCartesianFixed_ - /// has cache been set? -#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) - mutable std::atomic cachePolarFixed_, cacheCartesianFixed_; + mutable std::atomic p4Cartesian_; #else - mutable bool cachePolarFixed_, cacheCartesianFixed_; + /// internal cache for p4 + mutable PolarLorentzVector* p4Polar_; + /// internal cache for p4 + mutable LorentzVector* p4Cartesian_; #endif /// set internal cache - inline void cachePolar() const { -#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) - if(!cachePolarFixed_.load(std::memory_order_acquire)) { - p4Polar_ = PolarLorentzVector( pt_, eta_, phi_, mass_ ); - cachePolarFixed_.store(true, std::memory_order_release); - } -#else - if ( cachePolarFixed_ ) return; - p4Polar_ = PolarLorentzVector( pt_, eta_, phi_, mass_ ); - cachePolarFixed_; -#endif - } + void cachePolar() const; /// set internal cache - inline void cacheCartesian() const { -#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) - if(!cacheCartesianFixed_.load(std::memory_order_acquire)) { - cachePolar(); - p4Cartesian_ = p4Polar_; - cacheCartesianFixed_.store(true, std::memory_order_release); - } -#else - if ( cacheCartesianFixed_ ) return; - cachePolar(); - p4Cartesian_ = p4Polar_; - cacheCartesianFixed_; -#endif - } + void cacheCartesian() const; /// clear internal cache - inline void clearCache() const { -#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__) - cachePolarFixed_.store(false, std::memory_order_release); - cacheCartesianFixed_.store(false, std::memory_order_release); -#else - cachePolarFixed_; - cacheCartesianFixed_; -#endif - } + void clearCache() const; }; } diff --git a/DataFormats/Candidate/src/Particle.cc b/DataFormats/Candidate/src/Particle.cc index 7368639b31d46..1e8ab79234c7c 100755 --- a/DataFormats/Candidate/src/Particle.cc +++ b/DataFormats/Candidate/src/Particle.cc @@ -1,4 +1,5 @@ #include "DataFormats/Candidate/interface/Particle.h" +#include const unsigned int reco::Particle::longLivedTag = 65536; @@ -7,36 +8,34 @@ namespace reco { /// default constructor Particle::Particle() : qx3_(0), pt_(0), eta_(0), phi_(0), mass_(0), - vertex_(0, 0, 0), pdgId_(0), status_(0) + vertex_(0, 0, 0), pdgId_(0), status_(0), + p4Polar_(nullptr), p4Cartesian_(nullptr) { - cachePolarFixed_.store(false, std::memory_order_release); - cacheCartesianFixed_.store(false, std::memory_order_release); } /// constructor from values Particle::Particle( Charge q, const LorentzVector & p4, const Point & vertex, int pdgId, int status, bool integerCharge) : qx3_( q ), pt_( p4.pt() ), eta_( p4.eta() ), phi_( p4.phi() ), mass_( p4.mass() ), - vertex_( vertex ), pdgId_( pdgId ), status_( status ) + vertex_( vertex ), pdgId_( pdgId ), status_( status ), + p4Polar_(nullptr), p4Cartesian_(nullptr) { - cachePolarFixed_.store(false, std::memory_order_release); - cacheCartesianFixed_.store(false, std::memory_order_release); if ( integerCharge ) qx3_ *= 3; } /// constructor from values Particle::Particle( Charge q, const PolarLorentzVector & p4, const Point & vertex, int pdgId, int status, bool integerCharge) : qx3_( q ), pt_( p4.pt() ), eta_( p4.eta() ), phi_( p4.phi() ), mass_( p4.mass() ), - vertex_( vertex ), pdgId_( pdgId ), status_( status ) { - cachePolarFixed_.store(false, std::memory_order_release); - cacheCartesianFixed_.store(false, std::memory_order_release); + vertex_( vertex ), pdgId_( pdgId ), status_( status ), + p4Polar_(nullptr), p4Cartesian_(nullptr) +{ if ( integerCharge ) qx3_ *= 3; } // copy-ctor Particle::Particle(const Particle& src) : qx3_(src.qx3_), pt_(src.pt_), eta_(src.eta_), phi_(src.phi_), mass_(src.mass_), - vertex_(src.vertex_), pdgId_(src.pdgId_), status_(src.status_) { - cachePolarFixed_.store(false, std::memory_order_release); - cacheCartesianFixed_.store(false, std::memory_order_release); + vertex_(src.vertex_), pdgId_(src.pdgId_), status_(src.status_), + p4Polar_(nullptr), p4Cartesian_(nullptr) +{ } // copy assignment operator Particle& @@ -55,30 +54,151 @@ void Particle::swap(Particle& other) { std::swap(vertex_, other.vertex_); std::swap(pdgId_, other.pdgId_); std::swap(status_, other.status_); - other.cachePolarFixed_.exchange(cachePolarFixed_.exchange(other.cachePolarFixed_)); - other.cacheCartesianFixed_.exchange(cachePolarFixed_.exchange(other.cachePolarFixed_)); + other.p4Polar_.exchange( + p4Polar_.exchange(other.p4Polar_, std::memory_order_acq_rel), + std::memory_order_acq_rel); + other.p4Cartesian_.exchange( + p4Cartesian_.exchange(other.p4Cartesian_, std::memory_order_acq_rel), + std::memory_order_acq_rel); +} +/// dtor +Particle::~Particle() { + delete p4Polar_; + p4Polar_ = nullptr; + delete p4Cartesian_; + p4Cartesian_ = nullptr; +} + +/// electric charge +int Particle::charge() const { + return qx3_ / 3; +} +/// set electric charge +void Particle::setCharge( Charge q ) { + qx3_ = q * 3; +} +/// electric charge +int Particle::threeCharge() const { + return qx3_; +} +/// set electric charge +void Particle::setThreeCharge( Charge qx3 ) { + qx3_ = qx3; +} +/// four-momentum Lorentz vector +const Particle::LorentzVector & Particle::p4() const { + cacheCartesian(); + return (*p4Cartesian_); +} +/// four-momentum Lorentz vector +const Particle::PolarLorentzVector & Particle::polarP4() const { + cachePolar(); + return (*p4Polar_); +} +/// spatial momentum vector +Particle::Vector Particle::momentum() const { + cacheCartesian(); + return (*p4Cartesian_).Vect(); +} +/// boost vector to boost a Lorentz vector +/// to the particle center of mass system +Particle::Vector Particle::boostToCM() const { + cacheCartesian(); + return (*p4Cartesian_).BoostToCM(); +} +/// magnitude of momentum vector +double Particle::p() const { + cacheCartesian(); + return (*p4Cartesian_).P(); +} +/// energy +double Particle::energy() const { + cacheCartesian(); + return (*p4Cartesian_).E(); +} +/// transverse energy +double Particle::et() const { + cachePolar(); + return (*p4Polar_).Et(); +} +/// mass +double Particle::mass() const { + return mass_; +} +/// mass squared +double Particle::massSqr() const { + return mass_ * mass_; +} +/// transverse mass +double Particle::mt() const { + cachePolar(); + return (*p4Polar_).Mt(); +} +/// transverse mass squared +double Particle::mtSqr() const { + cachePolar(); + return (*p4Polar_).Mt2(); +} +/// x coordinate of momentum vector +double Particle::px() const { + cacheCartesian(); + return (*p4Cartesian_).Px(); +} +/// y coordinate of momentum vector +double Particle::py() const { + cacheCartesian(); + return (*p4Cartesian_).Py(); +} +/// z coordinate of momentum vector +double Particle::pz() const { + cacheCartesian(); + return (*p4Cartesian_).Pz(); +} +/// transverse momentum +double Particle::pt() const { + return pt_; +} +/// momentum azimuthal angle +double Particle::phi() const { + return phi_; +} +/// momentum polar angle +double Particle::theta() const { + cacheCartesian(); + return (*p4Cartesian_).Theta(); +} +/// momentum pseudorapidity +double Particle::eta() const { + return eta_; +} +/// repidity +double Particle::rapidity() const { + cachePolar(); + return (*p4Polar_).Rapidity(); +} +/// repidity +double Particle::y() const { + return rapidity(); } /// set 4-momentum void Particle::setP4( const LorentzVector & p4 ) { - p4Cartesian_ = p4; - p4Polar_ = p4; - pt_ = p4Polar_.pt(); - eta_ = p4Polar_.eta(); - phi_ = p4Polar_.phi(); - mass_ = p4Polar_.mass(); - cachePolarFixed_.store(true, std::memory_order_release); - cacheCartesianFixed_.store(true, std::memory_order_release); + *p4Cartesian_ = p4; + *p4Polar_ = p4; + pt_ = (*p4Polar_).pt(); + eta_ = (*p4Polar_).eta(); + phi_ = (*p4Polar_).phi(); + mass_ = (*p4Polar_).mass(); } /// set 4-momentum void Particle::setP4( const PolarLorentzVector & p4 ) { - p4Polar_ = p4; - pt_ = p4Polar_.pt(); - eta_ = p4Polar_.eta(); - phi_ = p4Polar_.phi(); - mass_ = p4Polar_.mass(); - cachePolarFixed_.store(true, std::memory_order_release); - cacheCartesianFixed_.store(false, std::memory_order_release); + *p4Polar_ = p4; + pt_ = (*p4Polar_).pt(); + eta_ = (*p4Polar_).eta(); + phi_ = (*p4Polar_).phi(); + mass_ = (*p4Polar_).mass(); + delete p4Cartesian_; + p4Cartesian_=nullptr; } /// set particle mass void Particle::setMass( double m ) { @@ -87,11 +207,80 @@ void Particle::setMass( double m ) { } void Particle::setPz( double pz ) { cacheCartesian(); - p4Cartesian_.SetPz(pz); - p4Polar_ = p4Cartesian_; - pt_ = p4Polar_.pt(); - eta_ = p4Polar_.eta(); - phi_ = p4Polar_.phi(); - mass_ = p4Polar_.mass(); + (*p4Cartesian_).SetPz(pz); + (*p4Polar_) = (*p4Cartesian_); + pt_ = (*p4Polar_).pt(); + eta_ = (*p4Polar_).eta(); + phi_ = (*p4Polar_).phi(); + mass_ = (*p4Polar_).mass(); +} + +const Particle::Point & Particle::vertex() const { + return vertex_; +} +/// x coordinate of vertex position +double Particle::vx() const { + return vertex_.X(); +} +/// y coordinate of vertex position +double Particle::vy() const { + return vertex_.Y(); +} +/// z coordinate of vertex position +double Particle::vz() const { + return vertex_.Z(); +} +/// set vertex +void Particle::setVertex( const Point & vertex ) { + vertex_ = vertex; +} +/// PDG identifier +int Particle::pdgId() const { + return pdgId_; +} +// set PDG identifier +void Particle::setPdgId( int pdgId ) { + pdgId_ = pdgId; +} +/// status word +int Particle::status() const { + return status_; +} +/// set status word +void Particle::setStatus( int status ) { + status_ = status; +} +/// set long lived flag +void Particle::setLongLived() { + status_ |= longLivedTag; +} +/// is long lived? +bool Particle::longLived() const { + return status_ & longLivedTag; +} + +/// set internal cache +void Particle::cachePolar() const { + if(!p4Polar_.load(std::memory_order_acquire)) { + std::unique_ptr ptr{new PolarLorentzVector(pt_,eta_,phi_,mass_)}; + PolarLorentzVector* expect = nullptr; + if(p4Polar_.compare_exchange_strong(expect, ptr.get(), std::memory_order_acq_rel)) { + ptr.release(); + } + } +} +/// set internal cache +void Particle::cacheCartesian() const { + if(!p4Cartesian_.load(std::memory_order_acquire)) { + cachePolar(); + (*p4Cartesian_) = (*p4Polar_); + } +} +/// clear internal cache +void Particle::clearCache() const { + delete p4Polar_; + p4Polar_ = nullptr; + delete p4Cartesian_; + p4Cartesian_ = nullptr; } } // end of reco namespace diff --git a/DataFormats/Candidate/src/classes_def.xml b/DataFormats/Candidate/src/classes_def.xml index 0448038db4f90..9e16641e9f55f 100755 --- a/DataFormats/Candidate/src/classes_def.xml +++ b/DataFormats/Candidate/src/classes_def.xml @@ -4,17 +4,9 @@ - - - - - - - - From 3ec52a80f7c94ef0d9fd628236207ad9ea516c74 Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Thu, 7 Nov 2013 13:39:28 -0500 Subject: [PATCH 03/10] Convert all (*) to use load/store --- DataFormats/Candidate/src/Particle.cc | 67 +++++++++++++-------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/DataFormats/Candidate/src/Particle.cc b/DataFormats/Candidate/src/Particle.cc index 1e8ab79234c7c..d2318ce343773 100755 --- a/DataFormats/Candidate/src/Particle.cc +++ b/DataFormats/Candidate/src/Particle.cc @@ -3,7 +3,7 @@ const unsigned int reco::Particle::longLivedTag = 65536; -namespace reco { +using namespace reco; /// default constructor Particle::Particle() @@ -88,38 +88,38 @@ void Particle::setThreeCharge( Charge qx3 ) { /// four-momentum Lorentz vector const Particle::LorentzVector & Particle::p4() const { cacheCartesian(); - return (*p4Cartesian_); + return (*p4Cartesian_.load(std::memory_order_acquire)); } /// four-momentum Lorentz vector const Particle::PolarLorentzVector & Particle::polarP4() const { cachePolar(); - return (*p4Polar_); + return (*p4Polar_.load(std::memory_order_acquire)); } /// spatial momentum vector Particle::Vector Particle::momentum() const { cacheCartesian(); - return (*p4Cartesian_).Vect(); + return (*p4Cartesian_.load(std::memory_order_acquire)).Vect(); } /// boost vector to boost a Lorentz vector /// to the particle center of mass system Particle::Vector Particle::boostToCM() const { cacheCartesian(); - return (*p4Cartesian_).BoostToCM(); + return (*p4Cartesian_.load(std::memory_order_acquire)).BoostToCM(); } /// magnitude of momentum vector double Particle::p() const { cacheCartesian(); - return (*p4Cartesian_).P(); + return (*p4Cartesian_.load(std::memory_order_acquire)).P(); } /// energy double Particle::energy() const { cacheCartesian(); - return (*p4Cartesian_).E(); + return (*p4Cartesian_.load(std::memory_order_acquire)).E(); } /// transverse energy double Particle::et() const { cachePolar(); - return (*p4Polar_).Et(); + return (*p4Polar_.load(std::memory_order_acquire)).Et(); } /// mass double Particle::mass() const { @@ -132,27 +132,27 @@ double Particle::massSqr() const { /// transverse mass double Particle::mt() const { cachePolar(); - return (*p4Polar_).Mt(); + return (*p4Polar_.load(std::memory_order_acquire)).Mt(); } /// transverse mass squared double Particle::mtSqr() const { cachePolar(); - return (*p4Polar_).Mt2(); + return (*p4Polar_.load(std::memory_order_acquire)).Mt2(); } /// x coordinate of momentum vector double Particle::px() const { cacheCartesian(); - return (*p4Cartesian_).Px(); + return (*p4Cartesian_.load(std::memory_order_acquire)).Px(); } /// y coordinate of momentum vector double Particle::py() const { cacheCartesian(); - return (*p4Cartesian_).Py(); + return (*p4Cartesian_.load(std::memory_order_acquire)).Py(); } /// z coordinate of momentum vector double Particle::pz() const { cacheCartesian(); - return (*p4Cartesian_).Pz(); + return (*p4Cartesian_.load(std::memory_order_acquire)).Pz(); } /// transverse momentum double Particle::pt() const { @@ -165,7 +165,7 @@ double Particle::phi() const { /// momentum polar angle double Particle::theta() const { cacheCartesian(); - return (*p4Cartesian_).Theta(); + return (*p4Cartesian_.load(std::memory_order_acquire)).Theta(); } /// momentum pseudorapidity double Particle::eta() const { @@ -174,7 +174,7 @@ double Particle::eta() const { /// repidity double Particle::rapidity() const { cachePolar(); - return (*p4Polar_).Rapidity(); + return (*p4Polar_.load(std::memory_order_acquire)).Rapidity(); } /// repidity double Particle::y() const { @@ -183,20 +183,20 @@ double Particle::y() const { /// set 4-momentum void Particle::setP4( const LorentzVector & p4 ) { - *p4Cartesian_ = p4; - *p4Polar_ = p4; - pt_ = (*p4Polar_).pt(); - eta_ = (*p4Polar_).eta(); - phi_ = (*p4Polar_).phi(); - mass_ = (*p4Polar_).mass(); + *p4Cartesian_.load(std::memory_order_acquire) = p4; + *p4Polar_.load(std::memory_order_acquire) = p4; + pt_ = (*p4Polar_.load(std::memory_order_acquire)).pt(); + eta_ = (*p4Polar_.load(std::memory_order_acquire)).eta(); + phi_ = (*p4Polar_.load(std::memory_order_acquire)).phi(); + mass_ = (*p4Polar_.load(std::memory_order_acquire)).mass(); } /// set 4-momentum void Particle::setP4( const PolarLorentzVector & p4 ) { - *p4Polar_ = p4; - pt_ = (*p4Polar_).pt(); - eta_ = (*p4Polar_).eta(); - phi_ = (*p4Polar_).phi(); - mass_ = (*p4Polar_).mass(); + *p4Polar_.load(std::memory_order_acquire) = p4; + pt_ = (*p4Polar_.load(std::memory_order_acquire)).pt(); + eta_ = (*p4Polar_.load(std::memory_order_acquire)).eta(); + phi_ = (*p4Polar_.load(std::memory_order_acquire)).phi(); + mass_ = (*p4Polar_.load(std::memory_order_acquire)).mass(); delete p4Cartesian_; p4Cartesian_=nullptr; } @@ -207,12 +207,12 @@ void Particle::setMass( double m ) { } void Particle::setPz( double pz ) { cacheCartesian(); - (*p4Cartesian_).SetPz(pz); - (*p4Polar_) = (*p4Cartesian_); - pt_ = (*p4Polar_).pt(); - eta_ = (*p4Polar_).eta(); - phi_ = (*p4Polar_).phi(); - mass_ = (*p4Polar_).mass(); + (*p4Cartesian_.load(std::memory_order_acquire)).SetPz(pz); + (*p4Polar_.load(std::memory_order_acquire)) = (*p4Cartesian_.load(std::memory_order_acquire)); + pt_ = (*p4Polar_.load(std::memory_order_acquire)).pt(); + eta_ = (*p4Polar_.load(std::memory_order_acquire)).eta(); + phi_ = (*p4Polar_.load(std::memory_order_acquire)).phi(); + mass_ = (*p4Polar_.load(std::memory_order_acquire)).mass(); } const Particle::Point & Particle::vertex() const { @@ -273,7 +273,7 @@ void Particle::cachePolar() const { void Particle::cacheCartesian() const { if(!p4Cartesian_.load(std::memory_order_acquire)) { cachePolar(); - (*p4Cartesian_) = (*p4Polar_); + (*p4Cartesian_.load(std::memory_order_acquire)) = (*p4Polar_.load(std::memory_order_acquire)); } } /// clear internal cache @@ -283,4 +283,3 @@ void Particle::clearCache() const { delete p4Cartesian_; p4Cartesian_ = nullptr; } -} // end of reco namespace From a51049f6cb91b68c5a4e7e668c8cdb996807208b Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Thu, 7 Nov 2013 15:16:00 -0500 Subject: [PATCH 04/10] Adjust HLTReco build file to link against Candidate library --- DataFormats/HLTReco/BuildFile.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/DataFormats/HLTReco/BuildFile.xml b/DataFormats/HLTReco/BuildFile.xml index ffe7152e21193..5e5d9f9af7cef 100644 --- a/DataFormats/HLTReco/BuildFile.xml +++ b/DataFormats/HLTReco/BuildFile.xml @@ -1,6 +1,7 @@ + From a6856e49b5ed98d6cc22dcc9c788a9914afa091d Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Fri, 8 Nov 2013 13:54:18 -0500 Subject: [PATCH 05/10] Re-factor code to be more efficient with memory order calls --- DataFormats/Candidate/src/Particle.cc | 66 +++++++++++++++------------ 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/DataFormats/Candidate/src/Particle.cc b/DataFormats/Candidate/src/Particle.cc index d2318ce343773..829dc944369e6 100755 --- a/DataFormats/Candidate/src/Particle.cc +++ b/DataFormats/Candidate/src/Particle.cc @@ -63,10 +63,7 @@ void Particle::swap(Particle& other) { } /// dtor Particle::~Particle() { - delete p4Polar_; - p4Polar_ = nullptr; - delete p4Cartesian_; - p4Cartesian_ = nullptr; + clearCache(); } /// electric charge @@ -183,36 +180,47 @@ double Particle::y() const { /// set 4-momentum void Particle::setP4( const LorentzVector & p4 ) { - *p4Cartesian_.load(std::memory_order_acquire) = p4; - *p4Polar_.load(std::memory_order_acquire) = p4; - pt_ = (*p4Polar_.load(std::memory_order_acquire)).pt(); - eta_ = (*p4Polar_.load(std::memory_order_acquire)).eta(); - phi_ = (*p4Polar_.load(std::memory_order_acquire)).phi(); - mass_ = (*p4Polar_.load(std::memory_order_acquire)).mass(); + // ensure that we have non-null pointers + cacheCartesian(); + *p4Cartesian_.load(std::memory_order_acquire) = p4; + // ensure that we have non-null pointers + cachePolar(); + *p4Polar_.load(std::memory_order_acquire) = p4; + auto const* p4Polar = p4Polar_.load(std::memory_order_acquire); + pt_ = p4Polar->pt(); + eta_ = p4Polar->eta(); + phi_ = p4Polar->phi(); + mass_ = p4Polar->mass(); } /// set 4-momentum void Particle::setP4( const PolarLorentzVector & p4 ) { - *p4Polar_.load(std::memory_order_acquire) = p4; - pt_ = (*p4Polar_.load(std::memory_order_acquire)).pt(); - eta_ = (*p4Polar_.load(std::memory_order_acquire)).eta(); - phi_ = (*p4Polar_.load(std::memory_order_acquire)).phi(); - mass_ = (*p4Polar_.load(std::memory_order_acquire)).mass(); - delete p4Cartesian_; - p4Cartesian_=nullptr; + // ensure that we have non-null pointers + cachePolar(); + *p4Polar_.load(std::memory_order_acquire) = p4; + auto const* p4Polar = p4Polar_.load(std::memory_order_acquire); + pt_ = p4Polar->pt(); + eta_ = p4Polar->eta(); + phi_ = p4Polar->phi(); + mass_ = p4Polar->mass(); + delete p4Cartesian_.exchange(nullptr, std::memory_order_acq_rel); } /// set particle mass void Particle::setMass( double m ) { - mass_ = m; - clearCache(); + mass_ = m; + clearCache(); } void Particle::setPz( double pz ) { - cacheCartesian(); - (*p4Cartesian_.load(std::memory_order_acquire)).SetPz(pz); - (*p4Polar_.load(std::memory_order_acquire)) = (*p4Cartesian_.load(std::memory_order_acquire)); - pt_ = (*p4Polar_.load(std::memory_order_acquire)).pt(); - eta_ = (*p4Polar_.load(std::memory_order_acquire)).eta(); - phi_ = (*p4Polar_.load(std::memory_order_acquire)).phi(); - mass_ = (*p4Polar_.load(std::memory_order_acquire)).mass(); + // ensure that we have non-null pointers + cacheCartesian(); + (*p4Cartesian_.load(std::memory_order_acquire)).SetPz(pz); + // ensure that we have non-null pointers + cachePolar(); + (*p4Polar_.load(std::memory_order_acquire)) = (*p4Cartesian_.load(std::memory_order_acquire)); + auto const* p4Polar = p4Polar_.load(std::memory_order_acquire); + pt_ = p4Polar->pt(); + eta_ = p4Polar->eta(); + phi_ = p4Polar->phi(); + mass_ = p4Polar->mass(); } const Particle::Point & Particle::vertex() const { @@ -278,8 +286,6 @@ void Particle::cacheCartesian() const { } /// clear internal cache void Particle::clearCache() const { - delete p4Polar_; - p4Polar_ = nullptr; - delete p4Cartesian_; - p4Cartesian_ = nullptr; + delete p4Polar_.exchange(nullptr, std::memory_order_acq_rel); + delete p4Cartesian_.exchange(nullptr, std::memory_order_acq_rel); } From 9963d17429ca49d09bbb6d6810fc0f25b3ef3ad5 Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Mon, 11 Nov 2013 13:16:14 -0500 Subject: [PATCH 06/10] Invoke cachePolar earlier to prevent uninitialized p4Polar_ --- DataFormats/Candidate/src/Particle.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataFormats/Candidate/src/Particle.cc b/DataFormats/Candidate/src/Particle.cc index 829dc944369e6..f49aaa82d3889 100755 --- a/DataFormats/Candidate/src/Particle.cc +++ b/DataFormats/Candidate/src/Particle.cc @@ -279,8 +279,8 @@ void Particle::cachePolar() const { } /// set internal cache void Particle::cacheCartesian() const { + cachePolar(); if(!p4Cartesian_.load(std::memory_order_acquire)) { - cachePolar(); (*p4Cartesian_.load(std::memory_order_acquire)) = (*p4Polar_.load(std::memory_order_acquire)); } } From fd7dc23d05f83e7df00ab580929220f51c5146c7 Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Mon, 11 Nov 2013 13:16:27 -0500 Subject: [PATCH 07/10] Add ioread --- DataFormats/Candidate/src/classes_def.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DataFormats/Candidate/src/classes_def.xml b/DataFormats/Candidate/src/classes_def.xml index 9e16641e9f55f..5f3ffa79c01e7 100755 --- a/DataFormats/Candidate/src/classes_def.xml +++ b/DataFormats/Candidate/src/classes_def.xml @@ -7,6 +7,12 @@ + + + + + + From e17e11f85b376702e034f11daef7dcdb95a755aa Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Fri, 22 Nov 2013 09:01:04 -0500 Subject: [PATCH 08/10] Fix issue with p4Cartesian_ assignment --- DataFormats/Candidate/src/Particle.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DataFormats/Candidate/src/Particle.cc b/DataFormats/Candidate/src/Particle.cc index f49aaa82d3889..579b3b4b34541 100755 --- a/DataFormats/Candidate/src/Particle.cc +++ b/DataFormats/Candidate/src/Particle.cc @@ -279,9 +279,9 @@ void Particle::cachePolar() const { } /// set internal cache void Particle::cacheCartesian() const { - cachePolar(); if(!p4Cartesian_.load(std::memory_order_acquire)) { - (*p4Cartesian_.load(std::memory_order_acquire)) = (*p4Polar_.load(std::memory_order_acquire)); + cachePolar(); + *p4Cartesian = (*p4Polar_.load(std::memory_order_acquire)); } } /// clear internal cache From 6e5762c5ea5691b72248640a241820e6e1f2b1da Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Fri, 22 Nov 2013 09:12:40 -0500 Subject: [PATCH 09/10] Fix typo --- DataFormats/Candidate/src/Particle.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataFormats/Candidate/src/Particle.cc b/DataFormats/Candidate/src/Particle.cc index 579b3b4b34541..0cd22397f4a8f 100755 --- a/DataFormats/Candidate/src/Particle.cc +++ b/DataFormats/Candidate/src/Particle.cc @@ -281,7 +281,7 @@ void Particle::cachePolar() const { void Particle::cacheCartesian() const { if(!p4Cartesian_.load(std::memory_order_acquire)) { cachePolar(); - *p4Cartesian = (*p4Polar_.load(std::memory_order_acquire)); + *p4Cartesian_ = (*p4Polar_.load(std::memory_order_acquire)); } } /// clear internal cache From c57cdaefd03309376bee2d6e749e8cbf6d7e5c5a Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Sun, 24 Nov 2013 22:02:33 -0500 Subject: [PATCH 10/10] properly initialize p4Cartesian_ pointer --- DataFormats/Candidate/src/Particle.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/DataFormats/Candidate/src/Particle.cc b/DataFormats/Candidate/src/Particle.cc index 0cd22397f4a8f..dda3527823d3f 100755 --- a/DataFormats/Candidate/src/Particle.cc +++ b/DataFormats/Candidate/src/Particle.cc @@ -281,7 +281,11 @@ void Particle::cachePolar() const { void Particle::cacheCartesian() const { if(!p4Cartesian_.load(std::memory_order_acquire)) { cachePolar(); - *p4Cartesian_ = (*p4Polar_.load(std::memory_order_acquire)); + std::unique_ptr ptr{new LorentzVector(*p4Polar_.load(std::memory_order_acquire))}; + LorentzVector* expected = nullptr; + if( p4Cartesian_.compare_exchange_strong(expected, ptr.get(), std::memory_order_acq_rel) ) { + ptr.release(); + } } } /// clear internal cache