Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:14

0001 // -*- C++ -*-
0002 //
0003 // This file is part of HepMC
0004 // Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
0005 //
0006 #ifndef HEPMC3_UNITS_H
0007 #define HEPMC3_UNITS_H
0008 /**
0009  *  @file Units.h
0010  *  @brief Definition of \b class Units
0011  *
0012  *  @class HepMC3::Units
0013  *  @brief Stores units-related enums and conversion functions
0014  *
0015  *  Manages units used by HepMC::GenEvent
0016  *
0017  */
0018 #include <string>
0019 #include "HepMC3/Errors.h"
0020 #include "HepMC3/Setup.h"
0021 #include "HepMC3/FourVector.h"
0022 
0023 namespace HepMC3 {
0024 
0025 
0026 class Units {
0027 public:
0028     /** @brief Momentum units */
0029     enum MomentumUnit { MEV, GEV };
0030 
0031     /** @brief Position units */
0032     enum LengthUnit   { MM,  CM  };
0033 
0034 public:
0035     /** @brief Get momentum unit based on its name*/
0036     static MomentumUnit momentum_unit( const std::string& name ) {
0037         if ( name.compare(0,3,"GEV") == 0 ) return GEV;
0038         if ( name.compare(0,3,"MEV") == 0 ) return MEV;
0039 
0040         HEPMC3_ERROR_LEVEL(300,"Units::momentum_unit: unrecognised unit name: '" << name <<"', setting to GEV" )
0041 
0042         return GEV;
0043     }
0044 
0045     /** @brief Get length unit based on its name*/
0046     static LengthUnit length_unit( const std::string& name ) {
0047         if ( name.compare(0,2,"CM") == 0 ) return CM;
0048         if ( name.compare(0,2,"MM") == 0 ) return MM;
0049 
0050         HEPMC3_ERROR_LEVEL(300,"Units::length_unit: unrecognised unit name: '" << name <<"', setting to CM" )
0051 
0052         return CM;
0053     }
0054 
0055     /** @brief Get name of momentum unit */
0056     static std::string name( MomentumUnit u ) {
0057         switch(u) {
0058         case MEV:
0059             return "MEV";
0060         case GEV:
0061             return "GEV";
0062         }
0063 
0064         return "<UNDEFINED>";
0065     }
0066 
0067     /** @brief Get name of length unit */
0068     static std::string name( LengthUnit u ) {
0069         switch(u) {
0070         case MM:
0071             return "MM";
0072         case CM:
0073             return "CM";
0074         }
0075 
0076         return "<UNDEFINED>";
0077     }
0078 
0079     /** @brief Convert FourVector to different momentum unit */
0080     template <typename T>
0081     static void convert( T &m, MomentumUnit from, MomentumUnit to ) {
0082         if ( from == to ) return;
0083 
0084         if ( from == GEV ) {
0085             // GEV -> MEV
0086             m *= 1000.;
0087         }
0088         else if ( from == MEV ) {
0089             // MEV -> GEV
0090             m *= 0.001;
0091         }
0092     }
0093 
0094     /** @brief Convert FourVector to different length unit */
0095     template <typename T>
0096     static void convert( T &m, LengthUnit from, LengthUnit to ) {
0097         if ( from == to ) return;
0098 
0099         if ( from == CM ) {
0100             // CM -> MM
0101             m *= 10.0;
0102         }
0103         else if ( from == MM ) {
0104             // MM -> CM
0105             m *= 0.1;
0106         }
0107     }
0108 
0109 };
0110 
0111 } // namespace HepMC3
0112 
0113 #endif