CMS 3D CMS Logo

GeantUnits.h
Go to the documentation of this file.
1 #ifndef DataFormats_Math_GeantUnits_h
2 #define DataFormats_Math_GeantUnits_h
3 
4 // This file provides units represented with user-defined literals to more easily attach units to numerical values.
5 // Units here are based upon Geant conventions: millimeter = 1, MeV = 1.
6 // The CMS convention is that centimeter = 1 and GeV = 1, so care must be taken with code that converts between
7 // the two conventions.
8 
10 
11 namespace geant_units {
12 
13  using angle_units::piRadians; // Needed by files the include this file
14  constexpr long double joule(6.24150e+12);
15  constexpr long double seconds(1.e+9);
16  constexpr long double nanoseconds(1.);
17 
18  namespace operators {
19 
20  // The following are needed by files that include this header
21  // Since "using namespace" is prohibited in header files, each
22  // name is individually imported with a "using" statement.
28 
29  // Length
30  constexpr long double operator"" _mm(long double length) { return length * 1.; }
31  constexpr long double operator"" _cm(long double length) { return length * 10.; }
32  constexpr long double operator"" _m(long double length) { return length * 1000.; }
33  constexpr long double operator"" _cm3(long double length) { return length * 1._cm * 1._cm * 1._cm; }
34  constexpr long double operator"" _m3(long double length) { return length * 1._m * 1._m * 1._m; }
35  constexpr long double operator"" _mm(unsigned long long int length) { return length * 1; }
36  constexpr long double operator"" _cm(unsigned long long int length) { return length * 10; }
37 
38  // Time
39  constexpr long double operator"" _s(long double x) { return x * seconds; }
40  constexpr long double operator"" _ns(long double x) { return x * nanoseconds; }
41 
42  // Energy
43  constexpr long double operator"" _MeV(long double energy) { return energy * 1.; }
44  constexpr long double operator"" _eV(long double energy) { return energy * 1.e-6_MeV; }
45  constexpr long double operator"" _TeV(long double energy) { return energy * 1.e6_MeV; }
46  constexpr long double operator"" _GeV(long double energy) { return energy * 1000._MeV; }
47 
48  // Mass
49  constexpr long double operator"" _kg(long double mass) {
50  return mass * (1._eV / 1.602176487e-19) * 1._s * 1._s / (1._m * 1._m);
51  }
52  constexpr long double operator"" _g(long double mass) { return mass * 1.e-3_kg; }
53  constexpr long double operator"" _mg(long double mass) { return mass * 1.e-3_g; }
54  constexpr long double operator"" _mole(long double mass) { return mass * 1.; }
55 
56  // Material properties
57  constexpr long double operator"" _mg_per_cm3(long double density) { return density * 1._mg / 1._cm3; }
58  constexpr long double operator"" _g_per_cm3(long double density) { return density * 1._g / 1._cm3; }
59  constexpr long double operator"" _g_per_mole(long double mass) { return mass * 1._g / 1._mole; }
60 
61  template <class NumType>
62  inline constexpr NumType convertMmToCm(NumType millimeters) // Millimeters -> centimeters
63  {
64  return (millimeters / 10.);
65  }
66 
67  template <class NumType>
68  inline constexpr NumType convertCmToMm(NumType centimeters) // Centimeters -> Milliimeters
69  {
70  return (centimeters * 10.);
71  }
72 
73  template <class NumType>
74  inline constexpr NumType convertCm2ToMm2(NumType centimeters) // Centimeters^2 -> Milliimeters^2
75  {
76  return (centimeters * 100.);
77  }
78 
79  template <class NumType>
80  inline constexpr NumType convertMm3ToM3(NumType mm3) // Cubic millimeters -> cubic meters
81  {
82  return (mm3 / 1.e9);
83  }
84 
85  // Convert Geant units to desired units
86  template <class NumType>
87  inline constexpr NumType convertUnitsTo(long double desiredUnits, NumType val) {
88  return (val / desiredUnits);
89  }
90  } // namespace operators
91 } // namespace geant_units
92 
93 #endif
constexpr NumType convertCm2ToMm2(NumType centimeters)
Definition: GeantUnits.h:74
constexpr NumType convertUnitsTo(long double desiredUnits, NumType val)
Definition: GeantUnits.h:87
constexpr long double convertDegToRad(NumType degrees)
Definition: angle_units.h:27
constexpr long double seconds(1.e+9)
constexpr NumType convertRadToDeg(NumType radians)
Definition: angle_units.h:21
constexpr long double piRadians(M_PIl)
constexpr long double nanoseconds(1.)
constexpr NumType convertCmToMm(NumType centimeters)
Definition: GeantUnits.h:68
constexpr NumType convertMm3ToM3(NumType mm3)
Definition: GeantUnits.h:80
constexpr NumType convertMmToCm(NumType millimeters)
Definition: GeantUnits.h:62
constexpr long double joule(6.24150e+12)
#define constexpr