HepMC3 event record library
Units.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2021 The HepMC collaboration (see AUTHORS for details)
5//
6#ifndef HEPMC3_UNITS_H
7#define HEPMC3_UNITS_H
8/**
9 * @file Units.h
10 * @brief Definition of \b class Units
11 *
12 * @class HepMC3::Units
13 * @brief Stores units-related enums and conversion functions
14 *
15 * Manages units used by HepMC::GenEvent
16 *
17 */
18#include <string>
19#include "HepMC3/Errors.h"
20#include "HepMC3/Setup.h"
21#include "HepMC3/FourVector.h"
22
23namespace HepMC3 {
24
25
26class Units {
27public:
28 /** @brief Momentum units */
29 enum MomentumUnit { MEV, GEV };
30
31 /** @brief Position units */
32 enum LengthUnit { MM, CM };
33
34public:
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 HEPMC3_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 HEPMC3_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:
59 return "MEV";
60 case GEV:
61 return "GEV";
62 }
63
64 return "<UNDEFINED>";
65 }
66
67 /** @brief Get name of length unit */
68 static std::string name( LengthUnit u ) {
69 switch(u) {
70 case MM:
71 return "MM";
72 case CM:
73 return "CM";
74 }
75
76 return "<UNDEFINED>";
77 }
78
79 /** @brief Convert FourVector to different momentum unit */
80 template <typename T>
81 static void convert( T &m, MomentumUnit from, MomentumUnit to ) {
82 if ( from == to ) return;
83
84 if ( from == GEV ) {
85 // GEV -> MEV
86 m *= 1000.;
87 }
88 else if ( from == MEV ) {
89 // MEV -> GEV
90 m *= 0.001;
91 }
92 }
93
94 /** @brief Convert FourVector to different length unit */
95 template <typename T>
96 static void convert( T &m, LengthUnit from, LengthUnit to ) {
97 if ( from == to ) return;
98
99 if ( from == CM ) {
100 // CM -> MM
101 m *= 10.0;
102 }
103 else if ( from == MM ) {
104 // MM -> CM
105 m *= 0.1;
106 }
107 }
108
109};
110
111} // namespace HepMC3
112
113#endif
Implementation of error and HEPMC3_HEPMC3_WARNING macros.
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:24
Definition of class FourVector.
Definition of class Setup.
Stores units-related enums and conversion functions.
Definition: Units.h:26
static void convert(T &m, MomentumUnit from, MomentumUnit to)
Convert FourVector to different momentum unit.
Definition: Units.h:81
static LengthUnit length_unit(const std::string &name)
Get length unit based on its name.
Definition: Units.h:46
LengthUnit
Position units.
Definition: Units.h:32
static std::string name(MomentumUnit u)
Get name of momentum unit.
Definition: Units.h:56
MomentumUnit
Momentum units.
Definition: Units.h:29
static MomentumUnit momentum_unit(const std::string &name)
Get momentum unit based on its name.
Definition: Units.h:36
static std::string name(LengthUnit u)
Get name of length unit.
Definition: Units.h:68
static void convert(T &m, LengthUnit from, LengthUnit to)
Convert FourVector to different length unit.
Definition: Units.h:96
HepMC3 main namespace.