HepMC event record
include/HepMC/Units.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2015 The HepMC collaboration (see AUTHORS for details)
5 //
6 #ifndef HEPMC_UNITS_H
7 #define HEPMC_UNITS_H
8 /**
9  * @file Units.h
10  * @brief Definition of \b class Units
11  *
12  * @class HepMC::Units
13  * @brief Stores units-related enums and conversion functions
14  *
15  * Manages units used by HepMC::GenEvent
16  *
17  */
18 
19 #include "HepMC/Errors.h"
20 #include "HepMC/Setup.h"
21 #include "HepMC/FourVector.h"
22 
23 namespace HepMC {
24 
25 
26 class Units {
27 public:
28  /** @brief Momentum units */
29  enum MomentumUnit { MEV, GEV };
30 
31  /** @brief Position units */
32  enum LengthUnit { MM, CM };
33 
34 public:
35  /** @brief Get momentum unit based on its name*/
36  static MomentumUnit momentum_unit( const std::string& name ) {
37  if( name.compare(0,3,"GEV") == 0 ) return GEV;
38  if( name.compare(0,3,"MEV") == 0 ) return MEV;
39 
40  ERROR("Units::momentum_unit: unrecognised unit name: '" << name <<"', setting to GEV" )
41 
42  return GEV;
43  }
44 
45  /** @brief Get length unit based on its name*/
46  static LengthUnit length_unit( const std::string& name ) {
47  if( name.compare(0,2,"CM") == 0 ) return CM;
48  if( name.compare(0,2,"MM") == 0 ) return MM;
49 
50  ERROR("Units::length_unit: unrecognised unit name: '" << name <<"', setting to CM" )
51 
52  return CM;
53  }
54 
55  /** @brief Get name of momentum unit */
56  static std::string name( MomentumUnit u ) {
57  switch(u) {
58  case MEV: return "MEV";
59  case GEV: return "GEV";
60  }
61 
62  return "<UNDEFINED>";
63  }
64 
65  /** @brief Get name of length unit */
66  static std::string name( LengthUnit u ) {
67  switch(u) {
68  case MM: return "MM";
69  case CM: return "CM";
70  }
71 
72  return "<UNDEFINED>";
73  }
74 
75  /** @brief Convert FourVector to different momentum unit */
76  static void convert( FourVector &m, MomentumUnit from, MomentumUnit to ) {
77  if( from == to ) return;
78 
79  if( from == GEV ) {
80  // GEV -> MEV
81  m *= 1000.;
82  }
83  else if( from == MEV ) {
84  // MEV -> GEV
85  m *= 0.001;
86  }
87  }
88 
89  /** @brief Convert FourVector to different length unit */
90  static void convert( FourVector &m, LengthUnit from, LengthUnit to ) {
91  if( from == to ) return;
92 
93  if( from == CM ) {
94  // CM -> MM
95  m *= 100.;
96  }
97  else if( from == MM ) {
98  // MM -> CM
99  m *= 0.01;
100  }
101  }
102 
103 };
104 
105 } // namespace HepMC
106 
107 #endif
static void convert(FourVector &m, LengthUnit from, LengthUnit to)
Convert FourVector to different length unit.
static std::string name(LengthUnit u)
Get name of length unit.
static std::string name(MomentumUnit u)
Get name of momentum unit.
static void convert(FourVector &m, MomentumUnit from, MomentumUnit to)
Convert FourVector to different momentum unit.
static MomentumUnit momentum_unit(const std::string &name)
Get momentum unit based on its name.
Definition of template class SmartPointer.
static LengthUnit length_unit(const std::string &name)
Get length unit based on its name.