hepmc - Rev 337

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