Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:09:10

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2016-2019 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 namespace Acts {
0012 
0013 /// @verbatim embed:rst:leading-slashes
0014 /// All physical quantities have both a numerical value and a unit. For the
0015 /// computations we always choose a particular unit for a given physical
0016 /// quantity so we only need to consider the numerical values as such. The
0017 /// chosen base unit for a particular physical dimension, e.g. length, time, or
0018 /// energy, within this code base is called the native unit. The base units
0019 /// should be chosen such that they are internally consistent, require the least
0020 /// amount of explicit conversion factors (ideally none at all), and have
0021 /// typical numerical values close to unity to reduce numerical issues.
0022 ///
0023 /// Here, the following native units are used:
0024 ///
0025 /// - Length is expressed in mm.
0026 /// - Time is expressed in [speed-of-light * time] == mm. A consequence
0027 ///   of this choice is that the speed-of-light expressed in native units
0028 ///   is 1.
0029 /// - Angles are expressed in radian.
0030 /// - Energy, mass, and momentum are all expressed in GeV (consistent with
0031 ///   speed-of-light == 1).
0032 /// - Electric charge is expressed in e, i.e. units of the elementary charge.
0033 /// - The magnetic field is expressed in GeV/(e*mm). The magnetic field
0034 ///   connects momentum to length, e.g. in SI units the radius of a charged
0035 ///   particle trajectory in a constant magnetic field is given by
0036 ///
0037 ///   .. math::
0038 ///
0039 ///      radius = - (momentum / charge) / field
0040 ///
0041 ///   With the chosen magnetic field unit the expression above stays the
0042 ///   same and no additional conversion factors are necessary.
0043 /// - Amount of substance is expressed in mol.
0044 ///
0045 /// Depending on the context a physical quantity might not be given in the
0046 /// native units. In this case we need to convert to the native unit first
0047 /// before the value can be used. The necessary conversion factors are defined
0048 /// in  ``Acts::UnitConstants``. Multiplying a value with the unit constant
0049 /// produces the equivalent value in the native unit, e.g.
0050 ///
0051 /// .. code-block:: cpp
0052 ///
0053 ///    double length = 1 * Acts::UnitConstants::m;       // length == 1000.0
0054 ///    double momentum = 100 * Acts::UnitConstants::MeV; // momentum == 0.1
0055 ///
0056 /// The conversion of a value in native units into the equivalent value in a
0057 /// specific other unit is computed by dividing with the relevant unit, e.g.
0058 ///
0059 /// .. code-block:: cpp
0060 ///
0061 ///    double length = 10.0;                               // native units mm
0062 ///    double lengthInM = length / Acts::UnitConstants::m; // == 0.01;
0063 ///
0064 /// To further simplify the usage, physical quantities can also be expressed via
0065 /// `C++ user literals
0066 /// <https://en.cppreference.com/w/cpp/language/user_literal>`_ defined in
0067 /// ``Acts::UnitLiterals``. This allows us to express quantities in a concise
0068 /// way:
0069 ///
0070 /// .. code-block:: cpp
0071 ///
0072 ///    using namespace Acts::UnitLiterals;
0073 ///
0074 ///    double length = 1_m;                     // == 1000.0
0075 ///    double momentum = 1.25_TeV;              // == 1250.0
0076 ///    double lengthInUm = length / 1_um;       // == 1000000.0
0077 ///    double momentumInMeV = momentum / 1_MeV; // == 1250000.0
0078 ///
0079 /// .. warning::
0080 ///    Since using user-defined literals requires a namespace import of
0081 ///    ``Acts::UnitLiterals`` it should not be used in headers since it would
0082 ///    (accidentally) modify the namespace wherever the header is included.
0083 ///
0084 /// To ensure consistent computations and results the following guidelines
0085 /// **must** be followed when handling physical quantities with units:
0086 ///
0087 /// - All unqualified numerical values, i.e. without a unit, are assumed to
0088 ///   be expressed in the relevant native unit, e.g. mm for lengths or GeV
0089 ///   for energy/momentum.
0090 /// - If a variable stores a physical quantity in a specific unit that is
0091 ///   not the native unit, clearly mark this in the variable, i.e.
0092 ///
0093 ///   .. code-block:: cpp
0094 ///
0095 ///      double momentum = 100.0; // momentum is stored as native unit GeV
0096 ///      double momentumInMeV = 10.0; // would be 0.01 in native units
0097 ///
0098 /// - All input values must be given as ``numerical_value * unit_constant`` or
0099 ///   equivalently using the unit literals as ``value_unit``. The resulting
0100 ///   unqualified numerical value will be automatically converted to the
0101 ///   native unit.
0102 /// - To output an unqualified numerical value in the native units as a
0103 ///   numerical value in a specific unit divide by the unit constants as
0104 ///   ``numerical_value / unit_constant`` or using the unit literals as
0105 ///   ``value / 1_unit``.
0106 ///
0107 /// Examples:
0108 ///
0109 /// .. code-block:: cpp
0110 ///
0111 ///    #include <Acts/include/Definitions/Units.hpp>
0112 ///    using namespace Acts::UnitLiterals;
0113 ///
0114 ///    // define input values w/ units (via unit constants)
0115 ///    double width    = 12 * Acts::UnitConstants::mm;
0116 ///    double mmuon    = 105.7 * Acts::UnitConstants::MeV;
0117 ///    // define input values w/ units (via unit user literals)
0118 ///    double length   = 23_cm;
0119 ///    double time     = 1214.2_ns;
0120 ///    double angle    = 123_degree;
0121 ///    double momentum = 2.5_TeV;
0122 ///    double mass     = 511_keV;
0123 ///    double velocity = 345_m / 1_s;
0124 ///    double bfield   = 3.9_T;
0125 ///
0126 ///    // convert output values (via unit constants)
0127 ///    double t_in_ns    = trackPars.time() / Acts::UnitConstants::ns;
0128 ///    // convert output values (via unit user literals)
0129 ///    double x_in_mm   = trackPars.position()[ePos0] / 1_mm;
0130 ///    double p_in_TeV = trackPars.absoluteMomentum() / 1_TeV;
0131 ///
0132 /// @endverbatim
0133 
0134 /// @note A helper script is available in
0135 ///   `Core/scripts/print_units_physical_constants.py` to validate some of the
0136 ///   numerical values.
0137 
0138 namespace UnitConstants {
0139 // Length, native unit mm
0140 constexpr double mm = 1.0;
0141 constexpr double fm = 1e-12 * mm;
0142 constexpr double pm = 1e-9 * mm;
0143 constexpr double nm = 1e-6 * mm;
0144 constexpr double um = 1e-3 * mm;
0145 constexpr double cm = 1e1 * mm;
0146 constexpr double m = 1e3 * mm;
0147 constexpr double km = 1e6 * mm;
0148 // Shortcuts for commonly used area and volume units. This intentionally
0149 // contains not all possible combinations to avoid cluttering the namespace.
0150 // Missing area or volume units can always be defined on the fly using the
0151 // existing length units e.g. 1fm³ -> 1fm * 1fm * 1fm
0152 // Area, native unit mm²
0153 constexpr double mm2 = mm * mm;
0154 constexpr double cm2 = cm * cm;
0155 constexpr double m2 = m * m;
0156 // Volume, native unit mm³
0157 constexpr double mm3 = mm * mm * mm;
0158 constexpr double cm3 = cm * cm * cm;
0159 constexpr double m3 = m * m * m;
0160 // Time, native unit mm = [speed-of-light * time] = mm/s * s
0161 /// @note Depends on speed of light in SI units
0162 constexpr double s = 299792458000.0;  // = 299792458.0 * (m / 1.0) * 1.0;
0163 constexpr double fs = 1e-15 * s;
0164 constexpr double ps = 1e-12 * s;
0165 constexpr double ns = 1e-9 * s;
0166 constexpr double us = 1e-6 * s;
0167 constexpr double ms = 1e-3 * s;
0168 constexpr double min = 60.0 * s;
0169 constexpr double h = 3600.0 * s;
0170 // Angles, native unit radian
0171 constexpr double mrad = 1e-3;
0172 constexpr double rad = 1.0;
0173 constexpr double degree = 0.017453292519943295;  // = M_PI / 180.0 * rad;
0174 // Energy/mass/momentum, native unit GeV
0175 constexpr double GeV = 1.0;
0176 constexpr double eV = 1e-9 * GeV;
0177 constexpr double keV = 1e-6 * GeV;
0178 constexpr double MeV = 1e-3 * GeV;
0179 constexpr double TeV = 1e3 * GeV;
0180 constexpr double J = 6241509074.460763 * GeV;
0181 /// atomic mass unit u
0182 constexpr double u = 0.93149410242;
0183 //     1eV/c² == 1.782662e-36kg
0184 //    1GeV/c² == 1.782662e-27kg
0185 // ->     1kg == (1/1.782662e-27)GeV/c²
0186 // ->      1g == (1/(1e3*1.782662e-27))GeV/c²
0187 constexpr double g = 1.0 / 1.782662e-24;
0188 constexpr double kg = 1.0 / 1.782662e-27;
0189 /// Charge, native unit e (elementary charge)
0190 constexpr double e = 1.0;
0191 /// Magnetic field, native unit (eV*s)/(e*m²)
0192 /// @note Depends on speed of light in SI units
0193 constexpr double T = 0.000299792458;  // = eV * s / (e * m2);
0194 constexpr double Gauss = 1e-4 * T;
0195 constexpr double kGauss = 1e-1 * T;
0196 /// Amount of substance, native unit mol
0197 constexpr double mol = 1.0;
0198 }  // namespace UnitConstants
0199 
0200 namespace UnitLiterals {
0201 // define user literal functions for the given unit constant
0202 #define ACTS_DEFINE_UNIT_LITERAL(name)                        \
0203   constexpr double operator""_##name(long double x) {        \
0204     return ::Acts::UnitConstants::name * x;                   \
0205   }                                                           \
0206   constexpr double operator""_##name(unsigned long long x) { \
0207     return ::Acts::UnitConstants::name * x;                   \
0208   }
0209 ACTS_DEFINE_UNIT_LITERAL(fm)
0210 ACTS_DEFINE_UNIT_LITERAL(pm)
0211 ACTS_DEFINE_UNIT_LITERAL(nm)
0212 ACTS_DEFINE_UNIT_LITERAL(um)
0213 ACTS_DEFINE_UNIT_LITERAL(mm)
0214 ACTS_DEFINE_UNIT_LITERAL(cm)
0215 ACTS_DEFINE_UNIT_LITERAL(m)
0216 ACTS_DEFINE_UNIT_LITERAL(km)
0217 ACTS_DEFINE_UNIT_LITERAL(mm2)
0218 ACTS_DEFINE_UNIT_LITERAL(cm2)
0219 ACTS_DEFINE_UNIT_LITERAL(m2)
0220 ACTS_DEFINE_UNIT_LITERAL(mm3)
0221 ACTS_DEFINE_UNIT_LITERAL(cm3)
0222 ACTS_DEFINE_UNIT_LITERAL(m3)
0223 ACTS_DEFINE_UNIT_LITERAL(fs)
0224 ACTS_DEFINE_UNIT_LITERAL(ps)
0225 ACTS_DEFINE_UNIT_LITERAL(ns)
0226 ACTS_DEFINE_UNIT_LITERAL(us)
0227 ACTS_DEFINE_UNIT_LITERAL(ms)
0228 ACTS_DEFINE_UNIT_LITERAL(s)
0229 ACTS_DEFINE_UNIT_LITERAL(min)
0230 ACTS_DEFINE_UNIT_LITERAL(h)
0231 ACTS_DEFINE_UNIT_LITERAL(mrad)
0232 ACTS_DEFINE_UNIT_LITERAL(rad)
0233 ACTS_DEFINE_UNIT_LITERAL(degree)
0234 ACTS_DEFINE_UNIT_LITERAL(eV)
0235 ACTS_DEFINE_UNIT_LITERAL(keV)
0236 ACTS_DEFINE_UNIT_LITERAL(MeV)
0237 ACTS_DEFINE_UNIT_LITERAL(GeV)
0238 ACTS_DEFINE_UNIT_LITERAL(TeV)
0239 ACTS_DEFINE_UNIT_LITERAL(J)
0240 ACTS_DEFINE_UNIT_LITERAL(u)
0241 ACTS_DEFINE_UNIT_LITERAL(g)
0242 ACTS_DEFINE_UNIT_LITERAL(kg)
0243 ACTS_DEFINE_UNIT_LITERAL(e)
0244 ACTS_DEFINE_UNIT_LITERAL(T)
0245 ACTS_DEFINE_UNIT_LITERAL(Gauss)
0246 ACTS_DEFINE_UNIT_LITERAL(kGauss)
0247 ACTS_DEFINE_UNIT_LITERAL(mol)
0248 // not needed anymore. undef to prevent littering the namespace
0249 #undef ACTS_DEFINE_UNIT_LITERAL
0250 }  // namespace UnitLiterals
0251 
0252 /// Physical constants in native units.
0253 ///
0254 /// Unit constants are intentionally not listed.
0255 namespace PhysicalConstants {
0256 // Speed of light
0257 constexpr double c = 1.0;
0258 /// Reduced Planck constant h/2*pi.
0259 ///
0260 /// Computed from CODATA 2018 constants to double precision.
0261 constexpr double hbar =
0262     6.582119569509066e-25 * UnitConstants::GeV * UnitConstants::s;
0263 }  // namespace PhysicalConstants
0264 
0265 }  // namespace Acts