hepmc - Rev 337

Subversion Repositories:
Rev:
  1. #ifndef HEPMC_MOMENTUM_UNITS_H
  2. #define HEPMC_MOMENTUM_UNITS_H
  3.  
  4. //--------------------------------------------------------------------------
  5. // MomentumUnits.h
  6. // Author:  Lynn Garren
  7. //
  8. // momentum units used by a GenEvent
  9. //--------------------------------------------------------------------------
  10.  
  11. #include <string>
  12.  
  13. namespace HepMC {
  14.  
  15. //! The MomentumUnits class contains the momentum units used by a GenEvent
  16.  
  17. ///
  18. /// \class MomentumUnits
  19. /// HepMC::MomentumUnits contains information about momentum units
  20. /// and associated methods.
  21. ///
  22. class MomentumUnits {
  23.  
  24. public:
  25.  
  26. /// this enum scheme can accomodate additional definitions
  27. enum HepMCmomentumUnits { UNKNOWN = 0, MEV, GEV };
  28.  
  29.     MomentumUnits( HepMCmomentumUnits mom = UNKNOWN ); //!< default constructor
  30.     ~MomentumUnits() {}
  31.  
  32.     MomentumUnits( const MomentumUnits& inevent );            //!< copy
  33.     MomentumUnits& operator=( const MomentumUnits& inevent ); //!< copy
  34.     MomentumUnits& operator=( const HepMCmomentumUnits ); //!< copy
  35.     void swap( MomentumUnits & other );                       //!< swap
  36.  
  37.     // accessors
  38.     HepMCmomentumUnits units()      const;   //!< return momentum unit enum
  39.     std::string        name()       const;   //!< return unit name
  40.     double             unitFactor() const;   //!< scaling factor relative to eV
  41.     double             conversion_factor( HepMCmomentumUnits ) const; //!< return a conversion multiplier
  42.  
  43.     // mutators
  44.     bool set_units  ( HepMCmomentumUnits ); //!< set the momentum units
  45.     bool set_units  ( std::string& );       //!< set the momentum units
  46.  
  47. private: // data members
  48.     HepMCmomentumUnits m_units;    // UNKNOWN by default
  49. };
  50.  
  51. //--------------------------------------------------------------------------
  52. // INLINE Methods
  53.  
  54. inline MomentumUnits::MomentumUnits( HepMCmomentumUnits mom )
  55. : m_units( mom ) {}
  56.  
  57. inline MomentumUnits::MomentumUnits( const MomentumUnits& inevent )
  58. : m_units( inevent.units() ) {}
  59.  
  60. inline MomentumUnits& MomentumUnits::operator=( const MomentumUnits& inevent )
  61. {
  62.     /// best practices implementation
  63.     MomentumUnits tmp( inevent );
  64.     swap( tmp );
  65.     return *this;
  66. }
  67.  
  68. inline MomentumUnits& MomentumUnits::operator=( const HepMCmomentumUnits u )
  69. {
  70.     /// best practices implementation
  71.     MomentumUnits tmp( u );
  72.     swap( tmp );
  73.     return *this;
  74. }
  75.  
  76. inline void MomentumUnits::swap( MomentumUnits & other )
  77. { std::swap( m_units, other.m_units ); }
  78.  
  79. inline MomentumUnits::HepMCmomentumUnits MomentumUnits::units() const
  80. { return m_units; }
  81.  
  82. inline bool MomentumUnits::set_units( HepMCmomentumUnits mom )
  83. {
  84.    /// set_units will FAIL if the units are already defined
  85.    if( m_units != UNKNOWN ) return false;
  86.    m_units = mom;
  87.    return true;
  88. }
  89.  
  90. inline bool MomentumUnits::set_units( std::string& mom )
  91. {
  92.    /// set_units will FAIL if the units are already defined
  93.    if( m_units != UNKNOWN ) return false;
  94.    if( mom == "MEV"     ) m_units = MEV;
  95.    else if( mom == "GEV"     ) m_units = GEV;
  96.    else if( mom == "UNKNOWN" ) m_units = UNKNOWN;
  97.    else return false;
  98.    return true;
  99. }
  100.  
  101. inline double MomentumUnits::conversion_factor( HepMCmomentumUnits mom ) const
  102. {
  103.    /// This routine returns the desired scale factor
  104.    /// If momentum units are UNKNOWN, the scale factor is 0.
  105.    if( m_units == UNKNOWN ) return 0.;
  106.    if( m_units == mom ) return 1.;
  107.    double f = MomentumUnits(mom).unitFactor();
  108.    return  f < 0. ? 0. : unitFactor()/f;
  109. }
  110.  
  111. inline double MomentumUnits::unitFactor() const
  112. {
  113.     switch( m_units ) {
  114.         case MEV     : return 1.;
  115.         case GEV     : return 1000.;
  116.         default      : return -1.;
  117.     }
  118. }
  119.  
  120. inline std::string MomentumUnits::name() const
  121. {
  122.     switch( m_units ) {
  123.         case MEV     : return "MEV";
  124.         case GEV     : return "GEV";
  125.         case UNKNOWN : return "UNKNOWN";
  126.         default      : return "badValue";
  127.     }
  128. }
  129.  
  130. } // HepMC
  131.  
  132. #endif  // HEPMC_MOMENTUM_UNITS_H
  133.