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 
9 
10 #include <cmath>
11 
12 
13 namespace geant_units {
14 
15  constexpr long double piRadians(M_PI);
16  constexpr long double degPerRad = 180. / piRadians; // Degrees per radian
17  constexpr long double joule(6.24150e+12);
18  constexpr long double seconds(1.e+9);
19  constexpr long double nanoseconds(1.);
20 
21  namespace operators {
22 
23  // Angle
24  constexpr long double operator "" _pi( long double x )
25  { return x * piRadians; }
26  constexpr long double operator "" _pi( unsigned long long int x )
27  { return x * piRadians; }
28  constexpr long double operator"" _deg( long double deg )
29  {
30  return deg / degPerRad;
31  }
32  constexpr long double operator"" _deg( unsigned long long int deg )
33  {
34  return deg / degPerRad;
35  }
36  constexpr long double operator"" _rad( long double rad )
37  {
38  return rad*1.;
39  }
40 
41  // Length
42  constexpr long double operator"" _mm( long double length )
43  {
44  return length*1.;
45  }
46  constexpr long double operator"" _cm( long double length )
47  {
48  return length*10.;
49  }
50  constexpr long double operator"" _m( long double length )
51  {
52  return length*1000.;
53  }
54  constexpr long double operator"" _cm3( long double length )
55  {
56  return length*1._cm*1._cm*1._cm;
57  }
58  constexpr long double operator"" _m3( long double length )
59  {
60  return length*1._m*1._m*1._m;
61  }
62 
63  // Time
64  constexpr long double operator "" _s( long double x )
65  { return x * seconds; }
66  constexpr long double operator "" _ns( long double x )
67  { return x * nanoseconds; }
68 
69  // Energy
70  constexpr long double operator "" _MeV( long double energy )
71  { return energy * 1.; }
72  constexpr long double operator "" _eV( long double energy )
73  { return energy * 1.e-6_MeV; }
74  constexpr long double operator "" _TeV( long double energy )
75  { return energy * 1.e6_MeV; }
76  constexpr long double operator "" _GeV( long double energy )
77  { return energy * 1000._MeV; }
78 
79  // Mass
80  constexpr long double operator "" _kg( long double mass )
81  { return mass * ( 1._eV / 1.602176487e-19 ) * 1._s * 1._s / ( 1._m * 1._m ); }
82  constexpr long double operator "" _g( long double mass )
83  { return mass * 1.e-3_kg; }
84  constexpr long double operator "" _mg( long double mass )
85  { return mass * 1.e-3_g; }
86  constexpr long double operator "" _mole( long double mass )
87  { return mass * 1.; }
88 
89  // Material properties
90  constexpr long double operator"" _mg_per_cm3( long double density )
91  { return density * 1._mg / 1._cm3; }
92  constexpr long double operator"" _g_per_cm3( long double density )
93  { return density * 1._g / 1._cm3; }
94  constexpr long double operator"" _g_per_mole( long double mass )
95  { return mass * 1._g / 1._mole; }
96 
97  template <class NumType>
98  inline constexpr NumType convertRadToDeg(NumType radians) // Radians -> degrees
99  {
100  return (radians * degPerRad);
101  }
102 
103  template <class NumType>
104  inline constexpr long double convertDegToRad(NumType degrees) // Degrees -> radians
105  {
106  return (degrees / degPerRad);
107  }
108 
109  template <class NumType>
110  inline constexpr NumType convertMmToCm(NumType millimeters) // Millimeters -> centimeters
111  {
112  return (millimeters / 10.);
113  }
114 
115  template <class NumType>
116  inline constexpr NumType convertCmToMm(NumType centimeters) // Centimeters -> Milliimeters
117  {
118  return (centimeters * 10.);
119  }
120 
121  template <class NumType>
122  inline constexpr NumType convertMm3ToM3(NumType mm3) // Cubic millimeters -> cubic meters
123  {
124  return (mm3 / 1.e9);
125  }
126 
127  template <class NumType>
128  inline constexpr NumType convertUnitsTo(long double desiredUnits, NumType val) // Convert Geant units to desired units
129  {
130  return (val / desiredUnits);
131  }
132  }
133 }
134 
135 #endif
double degrees(double radiants)
def degrees
constexpr NumType convertUnitsTo(long double desiredUnits, NumType val)
Definition: GeantUnits.h:128
constexpr long double seconds(1.e+9)
constexpr NumType convertRadToDeg(NumType radians)
Definition: GeantUnits.h:98
constexpr long double piRadians(M_PI)
constexpr long double degPerRad
Definition: GeantUnits.h:16
constexpr long double nanoseconds(1.)
constexpr NumType convertCmToMm(NumType centimeters)
Definition: GeantUnits.h:116
constexpr long double convertDegToRad(NumType degrees)
Definition: GeantUnits.h:104
#define M_PI
constexpr NumType convertMm3ToM3(NumType mm3)
Definition: GeantUnits.h:122
constexpr NumType convertMmToCm(NumType millimeters)
Definition: GeantUnits.h:110
constexpr long double joule(6.24150e+12)
#define constexpr