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 double joule(6.24150e+12);
15  constexpr double seconds(1.e+9);
16  constexpr 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.
29 
30  // Length
31  constexpr double operator"" _mm(long double length) { return length * 1.; }
32  constexpr double operator"" _cm(long double length) { return length * 10.; }
33  constexpr double operator"" _m(long double length) { return length * 1000.; }
34  constexpr double operator"" _cm3(long double length) { return length * 1._cm * 1._cm * 1._cm; }
35  constexpr double operator"" _m3(long double length) { return length * 1._m * 1._m * 1._m; }
36  constexpr double operator"" _mm(unsigned long long int length) { return length * 1; }
37  constexpr double operator"" _cm(unsigned long long int length) { return length * 10; }
38 
39  // Time
40  constexpr double operator"" _s(long double x) { return x * seconds; }
41  constexpr double operator"" _ns(long double x) { return x * nanoseconds; }
42 
43  // Energy
44  constexpr double operator"" _MeV(long double energy) { return energy * 1.; }
45  constexpr double operator"" _eV(long double energy) { return energy * 1.e-6_MeV; }
46  constexpr double operator"" _TeV(long double energy) { return energy * 1.e6_MeV; }
47  constexpr double operator"" _GeV(long double energy) { return energy * 1000._MeV; }
48 
49  // Mass
50  constexpr double operator"" _kg(long double mass) {
51  return mass * (1._eV / 1.602176487e-19) * 1._s * 1._s / (1._m * 1._m);
52  }
53  constexpr double operator"" _g(long double mass) { return mass * 1.e-3_kg; }
54  constexpr double operator"" _mg(long double mass) { return mass * 1.e-3_g; }
55  constexpr double operator"" _mole(long double mass) { return mass * 1.; }
56 
57  // Material properties
58  constexpr double operator"" _mg_per_cm3(long double density) { return density * 1._mg / 1._cm3; }
59  constexpr double operator"" _g_per_cm3(long double density) { return density * 1._g / 1._cm3; }
60  constexpr double operator"" _g_per_mole(long double mass) { return mass * 1._g / 1._mole; }
61 
62  // Add these conversion functions to this namespace for convenience
70 
71  // Convert Geant units to desired units
72  template <class NumType>
73  inline constexpr NumType convertUnitsTo(double desiredUnits, NumType val) {
74  return (val / desiredUnits);
75  }
76  } // namespace operators
77 } // namespace geant_units
78 
79 #endif
constexpr NumType convertCm2ToMm2(NumType centimeters)
Definition: angle_units.h:56
constexpr double convertDegToRad(NumType degrees)
Definition: angle_units.h:27
constexpr NumType convertCmToMm(NumType centimeters)
Definition: angle_units.h:50
constexpr double joule(6.24150e+12)
constexpr NumType convertRadToDeg(NumType radians)
Definition: angle_units.h:21
std::enable_if<!std::numeric_limits< NumType >::is_integer, bool >::type almostEqual(NumType x, NumType y, int ulp)
Definition: angle_units.h:33
constexpr NumType convertMeVToGeV(NumType mev)
Definition: angle_units.h:68
constexpr NumType convertUnitsTo(double desiredUnits, NumType val)
Definition: GeantUnits.h:73
constexpr NumType convertGeVToMeV(NumType gev)
Definition: angle_units.h:74
constexpr double seconds(1.e+9)
constexpr NumType convertMm3ToM3(NumType mm3)
Definition: angle_units.h:62
constexpr NumType convertGeVToKeV(NumType gev)
Definition: angle_units.h:80
constexpr NumType convertMmToCm(NumType millimeters)
Definition: angle_units.h:44
constexpr double piRadians(M_PI)
float x
constexpr double nanoseconds(1.)