Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:28:13

0001 // @(#)root/mathcore:$Id$
0002 // Authors: W. Brown, M. Fischler, L. Moneta    2005
0003 
0004  /**********************************************************************
0005   *                                                                    *
0006   * Copyright (c) 2005 , LCG ROOT FNAL MathLib Team                    *
0007   *                                                                    *
0008   *                                                                    *
0009   **********************************************************************/
0010 
0011 // Header file for class RotationZ representing a rotation about the Z axis
0012 //
0013 // Created by: Mark Fischler Mon July 18  2005
0014 //
0015 // Last update: $Id$
0016 //
0017 #ifndef ROOT_Math_GenVector_RotationX
0018 #define ROOT_Math_GenVector_RotationX  1
0019 
0020 
0021 #include "Math/GenVector/Cartesian3D.h"
0022 #include "Math/GenVector/DisplacementVector3D.h"
0023 #include "Math/GenVector/PositionVector3D.h"
0024 #include "Math/GenVector/LorentzVector.h"
0025 #include "Math/GenVector/3DDistances.h"
0026 
0027 #include "Math/GenVector/RotationXfwd.h"
0028 
0029 #include <TMath.h>
0030 #include <cmath>
0031 
0032 namespace ROOT {
0033 namespace Math {
0034 
0035 
0036 //__________________________________________________________________________________________
0037    /**
0038       Rotation class representing a 3D rotation about the X axis by the angle of rotation.
0039       For efficiency reason, in addition to the angle, the sine and cosine of the angle are held
0040 
0041       @ingroup GenVector
0042 
0043       @see GenVector
0044    */
0045 
0046 class RotationX {
0047 
0048 public:
0049 
0050    typedef double Scalar;
0051 
0052 
0053    // ========== Constructors and Assignment =====================
0054 
0055    /**
0056       Default constructor (identity rotation)
0057    */
0058    RotationX() : fAngle(0), fSin(0), fCos(1) { }
0059 
0060    /**
0061       Construct from an angle
0062    */
0063    explicit RotationX( Scalar angle ) :   fAngle(angle),
0064                                           fSin(std::sin(angle)),
0065                                           fCos(std::cos(angle))
0066    {
0067       Rectify();
0068    }
0069 
0070    // The compiler-generated copy ctor, copy assignment, and destructor are OK.
0071 
0072    /**
0073       Rectify makes sure the angle is in (-pi,pi]
0074    */
0075    void Rectify()  {
0076       if (std::fabs(fAngle) >= TMath::Pi()) {
0077          double x = fAngle / TMath::TwoPi();
0078          fAngle = TMath::TwoPi() * (x + std::floor(.5 - x));
0079          fSin = std::sin(fAngle);
0080          fCos = std::cos(fAngle);
0081       }
0082    }
0083 
0084    // ======== Components ==============
0085 
0086    /**
0087       Set given the angle.
0088    */
0089    void SetAngle (Scalar angle) {
0090       fSin=std::sin(angle);
0091       fCos=std::cos(angle);
0092       fAngle= angle;
0093       Rectify();
0094    }
0095    void SetComponents (Scalar angle) { SetAngle(angle); }
0096 
0097    /**
0098       Get the angle
0099    */
0100    void GetAngle(Scalar &angle) const { using std::atan2; angle = atan2(fSin, fCos); }
0101    void GetComponents ( Scalar & angle ) const { GetAngle(angle); }
0102 
0103    /**
0104       Angle of rotation
0105    */
0106    Scalar Angle() const { using std::atan2; return atan2(fSin, fCos); }
0107 
0108    /**
0109       Sine or Cosine of the rotation angle
0110    */
0111    Scalar SinAngle () const { return fSin; }
0112    Scalar CosAngle () const { return fCos; }
0113 
0114    // =========== operations ==============
0115 
0116    /**
0117       Rotation operation on a cartesian vector
0118    */
0119 //   typedef  DisplacementVector3D< Cartesian3D<double> > XYZVector;
0120 //   XYZVector operator() (const XYZVector & v) const {
0121 //     return XYZVector ( v.x(), fCos*v.y()-fSin*v.z(), fCos*v.z()+fSin*v.y() );
0122 //   }
0123 
0124 
0125    /**
0126       Rotation operation on a displacement vector in any coordinate system
0127    */
0128    template <class CoordSystem, class U>
0129    DisplacementVector3D<CoordSystem,U>
0130    operator() (const DisplacementVector3D<CoordSystem,U> & v) const {
0131       DisplacementVector3D< Cartesian3D<double>,U > xyz;
0132       xyz.SetXYZ( v.X(), fCos*v.Y()-fSin*v.Z(), fCos*v.Z()+fSin*v.Y() );
0133       return DisplacementVector3D<CoordSystem,U>(xyz);
0134    }
0135 
0136    /**
0137       Rotation operation on a position vector in any coordinate system
0138    */
0139    template <class CoordSystem, class U>
0140    PositionVector3D<CoordSystem, U>
0141    operator() (const PositionVector3D<CoordSystem,U> & v) const {
0142       DisplacementVector3D< Cartesian3D<double>,U > xyz(v);
0143       DisplacementVector3D< Cartesian3D<double>,U > rxyz = operator()(xyz);
0144       return PositionVector3D<CoordSystem,U> ( rxyz );
0145    }
0146 
0147    /**
0148       Rotation operation on a Lorentz vector in any 4D coordinate system
0149    */
0150    template <class CoordSystem>
0151    LorentzVector<CoordSystem>
0152    operator() (const LorentzVector<CoordSystem> & v) const {
0153       DisplacementVector3D< Cartesian3D<double> > xyz(v.Vect());
0154       xyz = operator()(xyz);
0155       LorentzVector< PxPyPzE4D<double> > xyzt (xyz.X(), xyz.Y(), xyz.Z(), v.E());
0156       return LorentzVector<CoordSystem> ( xyzt );
0157    }
0158 
0159    /**
0160       Rotation operation on an arbitrary vector v.
0161       Preconditions:  v must implement methods x(), y(), and z()
0162       and the arbitrary vector type must have a constructor taking (x,y,z)
0163    */
0164    template <class ForeignVector>
0165    ForeignVector
0166    operator() (const  ForeignVector & v) const {
0167       DisplacementVector3D< Cartesian3D<double> > xyz(v);
0168       DisplacementVector3D< Cartesian3D<double> > rxyz = operator()(xyz);
0169       return ForeignVector ( rxyz.X(), rxyz.Y(), rxyz.Z() );
0170    }
0171 
0172    /**
0173       Overload operator * for rotation on a vector
0174    */
0175    template <class AVector>
0176    inline
0177    AVector operator* (const AVector & v) const
0178    {
0179       return operator()(v);
0180    }
0181 
0182    /**
0183       Invert a rotation in place
0184    */
0185    void Invert() { fAngle = -fAngle; fSin = -fSin; }
0186 
0187    /**
0188       Return inverse of  a rotation
0189    */
0190    RotationX Inverse() const { RotationX t(*this); t.Invert(); return t; }
0191 
0192    // ========= Multi-Rotation Operations ===============
0193 
0194    /**
0195       Multiply (combine) two rotations
0196    */
0197    RotationX operator * (const RotationX & r) const {
0198       RotationX ans;
0199       double x = (fAngle + r.fAngle) / TMath::TwoPi();
0200       ans.fAngle = TMath::TwoPi() * (x + std::floor(.5 - x));
0201       ans.fSin   = fSin*r.fCos + fCos*r.fSin;
0202       ans.fCos   = fCos*r.fCos - fSin*r.fSin;
0203       return ans;
0204    }
0205 
0206    /**
0207       Post-Multiply (on right) by another rotation :  T = T*R
0208    */
0209    RotationX & operator *= (const RotationX & r) { return *this = (*this)*r; }
0210 
0211    /**
0212       Equality/inequality operators
0213    */
0214    bool operator == (const RotationX & rhs) const {
0215       if( fAngle != rhs.fAngle )  return false;
0216       return true;
0217    }
0218    bool operator != (const RotationX & rhs) const {
0219       return ! operator==(rhs);
0220    }
0221 
0222 private:
0223 
0224    Scalar fAngle;   // rotation angle
0225    Scalar fSin;     // sine of the rotation angle
0226    Scalar fCos;     // cosine of the rotation angle
0227 
0228 };  // RotationX
0229 
0230 // ============ Class RotationX ends here ============
0231 
0232 /**
0233    Distance between two rotations
0234  */
0235 template <class R>
0236 inline
0237 typename RotationX::Scalar
0238 Distance ( const RotationX& r1, const R & r2) {return gv_detail::dist(r1,r2);}
0239 
0240 /**
0241    Stream Output and Input
0242  */
0243   // TODO - I/O should be put in the manipulator form
0244 
0245 inline
0246 std::ostream & operator<< (std::ostream & os, const RotationX & r) {
0247   os << " RotationX(" << r.Angle() << ") ";
0248   return os;
0249 }
0250 
0251 
0252 }  // namespace Math
0253 }  // namespace ROOT
0254 
0255 #endif // ROOT_Math_GenVector_RotationX