Skip to content

Commit

Permalink
Inline and access optimizations for TLorentzVector and TVector3 (root…
Browse files Browse the repository at this point in the history
…-project#4)

* move important functions (constructors,destructors,accessors) to header
   to avoid overhead in creating and accessing these small objects

 * optimize access to TLorentzVector by avoiding a double switch statement
   (switch on direction in TLorentzVector followed by same switch in TVector3)

 * TLorentzVector made friend of TVector3

This commit is a performance patch for ALICE's ROOT production version;
It is cherry-picked from the ROOT master branch
  • Loading branch information
sawenzel authored and dberzano committed Nov 14, 2016
1 parent 2c364e5 commit c2208a7
Show file tree
Hide file tree
Showing 4 changed files with 574 additions and 532 deletions.
65 changes: 60 additions & 5 deletions math/physics/inc/TLorentzVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class TLorentzVector : public TObject {
TLorentzVector(const TLorentzVector & lorentzvector);
// Copy constructor.

virtual ~TLorentzVector();
virtual ~TLorentzVector(){};
// Destructor

// inline operator TVector3 () const;
Expand Down Expand Up @@ -339,9 +339,9 @@ inline void TLorentzVector::SetPxPyPzE(Double_t px, Double_t py, Double_t pz, Do
}

inline void TLorentzVector::SetXYZM(Double_t x, Double_t y, Double_t z, Double_t m) {
if ( m >= 0 )
if ( m >= 0 )
SetXYZT( x, y, z, TMath::Sqrt(x*x+y*y+z*z+m*m) );
else
else
SetXYZT( x, y, z, TMath::Sqrt( TMath::Max((x*x+y*y+z*z-m*m), 0. ) ) );
}

Expand All @@ -365,8 +365,8 @@ inline void TLorentzVector::GetXYZT(Float_t *carray) const{
carray[3] = fE;
}

Double_t & TLorentzVector::operator [] (int i) { return (*this)(i); }
Double_t TLorentzVector::operator [] (int i) const { return (*this)(i); }
inline Double_t & TLorentzVector::operator [] (int i) { return (*this)(i); }
inline Double_t TLorentzVector::operator [] (int i) const { return (*this)(i); }

inline TLorentzVector &TLorentzVector::operator = (const TLorentzVector & q) {
fP = q.Vect();
Expand Down Expand Up @@ -596,5 +596,60 @@ inline TLorentzVector operator * (Double_t a, const TLorentzVector & p) {
return TLorentzVector(a*p.X(), a*p.Y(), a*p.Z(), a*p.T());
}

inline TLorentzVector::TLorentzVector()
: fP(), fE(0.0) {}

inline TLorentzVector::TLorentzVector(Double_t x, Double_t y, Double_t z, Double_t t)
: fP(x,y,z), fE(t) {}

inline TLorentzVector::TLorentzVector(const Double_t * x0)
: fP(x0), fE(x0[3]) {}

inline TLorentzVector::TLorentzVector(const Float_t * x0)
: fP(x0), fE(x0[3]) {}

inline TLorentzVector::TLorentzVector(const TVector3 & p, Double_t e)
: fP(p), fE(e) {}

inline TLorentzVector::TLorentzVector(const TLorentzVector & p) : TObject(p)
, fP(p.Vect()), fE(p.T()) {}



inline Double_t TLorentzVector::operator () (int i) const
{
//dereferencing operator const
switch(i) {
case kX:
return fP.X();
case kY:
return fP.Y();
case kZ:
return fP.Z();
case kT:
return fE;
default:
Error("operator()()", "bad index (%d) returning 0",i);
}
return 0.;
}

inline Double_t & TLorentzVector::operator () (int i)
{
//dereferencing operator
switch(i) {
case kX:
return fP.fX;
case kY:
return fP.fY;
case kZ:
return fP.fZ;
case kT:
return fE;
default:
Error("operator()()", "bad index (%d) returning &fE",i);
}
return fE;
}

#endif
60 changes: 55 additions & 5 deletions math/physics/inc/TVector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#ifndef ROOT_TMatrix
#include "TMatrix.h"
#endif
#ifndef ROOT_TMath
#include "TMath.h"
#endif

class TRotation;

Expand All @@ -42,7 +45,7 @@ class TVector3 : public TObject {
TVector3(const TVector3 &);
// The copy constructor.

virtual ~TVector3();
virtual ~TVector3() {};
// Destructor

Double_t operator () (int) const;
Expand Down Expand Up @@ -88,7 +91,7 @@ class TVector3 : public TObject {
inline Double_t Mag2() const;
// The magnitude squared (rho^2 in spherical coordinate system).

Double_t Mag() const;
Double_t Mag() const { return TMath::Sqrt(Mag2()); }
// The magnitude (rho in spherical coordinate system).

void SetPhi(Double_t);
Expand Down Expand Up @@ -192,9 +195,10 @@ class TVector3 : public TObject {

ClassDef(TVector3,3) // A 3D physics vector

// make TLorentzVector a friend class
friend class TLorentzVector;
};


TVector3 operator + (const TVector3 &, const TVector3 &);
// Addition of 3-vectors.

Expand All @@ -211,8 +215,8 @@ TVector3 operator * (Double_t a, const TVector3 &);
TVector3 operator * (const TMatrix &, const TVector3 &);


Double_t & TVector3::operator[] (int i) { return operator()(i); }
Double_t TVector3::operator[] (int i) const { return operator()(i); }
inline Double_t & TVector3::operator[] (int i) { return operator()(i); }
inline Double_t TVector3::operator[] (int i) const { return operator()(i); }

inline Double_t TVector3::x() const { return fX; }
inline Double_t TVector3::y() const { return fY; }
Expand Down Expand Up @@ -246,6 +250,51 @@ inline void TVector3::GetXYZ(Float_t *carray) const {
carray[2] = fZ;
}

////////////////////////////////////////////////////////////////////////////////
/// Constructors
inline TVector3::TVector3()
: fX(0.0), fY(0.0), fZ(0.0) {}

inline TVector3::TVector3(const TVector3 & p) : TObject(p),
fX(p.fX), fY(p.fY), fZ(p.fZ) {}

inline TVector3::TVector3(Double_t xx, Double_t yy, Double_t zz)
: fX(xx), fY(yy), fZ(zz) {}

inline TVector3::TVector3(const Double_t * x0)
: fX(x0[0]), fY(x0[1]), fZ(x0[2]) {}

inline TVector3::TVector3(const Float_t * x0)
: fX(x0[0]), fY(x0[1]), fZ(x0[2]) {}


inline Double_t TVector3::operator () (int i) const {
switch(i) {
case 0:
return fX;
case 1:
return fY;
case 2:
return fZ;
default:
Error("operator()(i)", "bad index (%d) returning 0",i);
}
return 0.;
}

inline Double_t & TVector3::operator () (int i) {
switch(i) {
case 0:
return fX;
case 1:
return fY;
case 2:
return fZ;
default:
Error("operator()(i)", "bad index (%d) returning &fX",i);
}
return fX;
}

inline TVector3 & TVector3::operator = (const TVector3 & p) {
fX = p.fX;
Expand Down Expand Up @@ -373,4 +422,5 @@ inline TVector2 TVector3::XYvector() const {
return TVector2(fX,fY);
}


#endif
Loading

0 comments on commit c2208a7

Please sign in to comment.