Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define and use cms units #27637

Merged
merged 3 commits into from Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions DataFormats/GeometryVector/interface/Phi.h
Expand Up @@ -3,6 +3,7 @@

#include "DataFormats/GeometryVector/interface/Pi.h"
#include "DataFormats/Math/interface/deltaPhi.h"
#include "DataFormats/Math/interface/angle_units.h"
#include <cmath>

namespace Geom {
Expand Down
36 changes: 36 additions & 0 deletions DataFormats/Math/interface/CMSUnits.h
@@ -0,0 +1,36 @@
#ifndef DataFormats_Math_CMS_Units_h
#define DataFormats_Math_CMS_Units_h

// This file provides units represented with user-defined literals to more easily attach units to numerical values.
// The CMS convention is that centimeter = 1 and GeV = 1

#include "DataFormats/Math/interface/angle_units.h"

namespace cms_units {

using angle_units::piRadians; // Needed by files the include this file

namespace operators {

// The following are needed by files that include this header
// Since "using namespace" is prohibited in header files, each
// name is individually imported with a "using" statement.
using angle_units::operators::operator""_deg;
using angle_units::operators::operator""_pi;
using angle_units::operators::operator""_rad;
using angle_units::operators::convertDegToRad;
using angle_units::operators::convertRadToDeg;

// Length
constexpr double operator"" _mm(long double length) { return length * 0.1; }
constexpr double operator"" _cm(long double length) { return length * 1.; }
constexpr double operator"" _m(long double length) { return length * 100.; }
constexpr double operator"" _cm3(long double length) { return length * 1._cm * 1._cm * 1._cm; }
constexpr double operator"" _m3(long double length) { return length * 1._m * 1._m * 1._m; }
constexpr double operator"" _mm(unsigned long long int length) { return length * 0.1; }
constexpr double operator"" _cm(unsigned long long int length) { return length * 1; }
ianna marked this conversation as resolved.
Show resolved Hide resolved

} // namespace operators
} // namespace cms_units

#endif
2 changes: 1 addition & 1 deletion DataFormats/Math/interface/GeantUnits.h
Expand Up @@ -6,7 +6,7 @@
// The CMS convention is that centimeter = 1 and GeV = 1, so care must be taken with code that converts between
// the two conventions.

#include "DataFormats/Math/interface/deltaPhi.h"
#include "DataFormats/Math/interface/angle_units.h"

namespace geant_units {

Expand Down
65 changes: 65 additions & 0 deletions DataFormats/Math/interface/angle_units.h
@@ -0,0 +1,65 @@
#ifndef DataFormats_Math_Angle_Units_h
#define DataFormats_Math_Angle_Units_h

#include <cmath>

namespace angle_units {

constexpr long double piRadians(M_PIl); // M_PIl is long double version of pi
constexpr long double degPerRad = 180. / piRadians; // Degrees per radian

namespace operators {

// Angle
constexpr long double operator"" _pi(long double x) { return x * piRadians; }
constexpr long double operator"" _pi(unsigned long long int x) { return x * piRadians; }
constexpr long double operator"" _deg(long double deg) { return deg / degPerRad; }
constexpr long double operator"" _deg(unsigned long long int deg) { return deg / degPerRad; }
constexpr long double operator"" _rad(long double rad) { return rad * 1.; }

template <class NumType>
inline constexpr NumType convertRadToDeg(NumType radians) // Radians -> degrees
{
return (radians * degPerRad);
}

template <class NumType>
inline constexpr long double convertDegToRad(NumType degrees) // Degrees -> radians
{
return (degrees / degPerRad);
}
} // namespace operators
} // namespace angle_units

namespace angle0to2pi {

using angle_units::operators::operator""_pi;

// make0To2pi constrains an angle to be >= 0 and < 2pi.
// This function is a faster version of reco::reduceRange.
// In timing tests, it is almost always faster than reco::reduceRange.
// It also protects against floating-point value drift over repeated calculations.
// This implementation uses multiplication instead of division and avoids
// calling fmod to improve performance.

template <class valType>
inline constexpr valType make0To2pi(valType angle) {
ianna marked this conversation as resolved.
Show resolved Hide resolved
constexpr valType twoPi = 2._pi;
constexpr valType oneOverTwoPi = 1. / twoPi;
constexpr valType epsilon = 1.e-13;

if ((std::abs(angle) <= epsilon) || (std::abs(twoPi - std::abs(angle)) <= epsilon))
return (0.);
if (std::abs(angle) > twoPi) {
valType nFac = trunc(angle * oneOverTwoPi);
angle -= (nFac * twoPi);
if (std::abs(angle) <= epsilon)
return (0.);
}
if (angle < 0.)
angle += twoPi;
return (angle);
}
} // namespace angle0to2pi

#endif
59 changes: 0 additions & 59 deletions DataFormats/Math/interface/deltaPhi.h
Expand Up @@ -51,63 +51,4 @@ struct DeltaPhi {
}
};

namespace angle_units {

constexpr long double piRadians(M_PIl); // M_PIl is long double version of pi
constexpr long double degPerRad = 180. / piRadians; // Degrees per radian

namespace operators {

// Angle
constexpr long double operator"" _pi(long double x) { return x * piRadians; }
constexpr long double operator"" _pi(unsigned long long int x) { return x * piRadians; }
constexpr long double operator"" _deg(long double deg) { return deg / degPerRad; }
constexpr long double operator"" _deg(unsigned long long int deg) { return deg / degPerRad; }
constexpr long double operator"" _rad(long double rad) { return rad * 1.; }

template <class NumType>
inline constexpr NumType convertRadToDeg(NumType radians) // Radians -> degrees
{
return (radians * degPerRad);
}

template <class NumType>
inline constexpr long double convertDegToRad(NumType degrees) // Degrees -> radians
{
return (degrees / degPerRad);
}
} // namespace operators
} // namespace angle_units

namespace angle0to2pi {

using angle_units::operators::operator""_pi;

// make0To2pi constrains an angle to be >= 0 and < 2pi.
// This function is a faster version of reco::reduceRange.
// In timing tests, it is almost always faster than reco::reduceRange.
// It also protects against floating-point value drift over repeated calculations.
// This implementation uses multiplication instead of division and avoids
// calling fmod to improve performance.

template <class valType>
inline constexpr valType make0To2pi(valType angle) {
constexpr valType twoPi = 2._pi;
constexpr valType oneOverTwoPi = 1. / twoPi;
constexpr valType epsilon = 1.e-13;

if ((std::abs(angle) <= epsilon) || (std::abs(twoPi - std::abs(angle)) <= epsilon))
return (0.);
if (std::abs(angle) > twoPi) {
valType nFac = trunc(angle * oneOverTwoPi);
angle -= (nFac * twoPi);
if (std::abs(angle) <= epsilon)
return (0.);
}
if (angle < 0.)
angle += twoPi;
return (angle);
}
} // namespace angle0to2pi

#endif
29 changes: 2 additions & 27 deletions Geometry/EcalCommonData/plugins/dd4hep/DDEcalBarrelNewAlgo.cc
@@ -1,7 +1,6 @@
#include "DD4hep/DetFactoryHelper.h"
#include "DD4hep/Printout.h"
// FIXME: use local definition until dd4hep fixes the uniits
//#include "DataFormats/Math/interface/GeantUnits.h"
#include "DataFormats/Math/interface/CMSUnits.h"
#include "DetectorDescription/DDCMS/interface/DDPlugins.h"
#include "DetectorDescription/DDCMS/interface/BenchmarkGrd.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
Expand All @@ -14,31 +13,7 @@
using namespace std;
using namespace cms;
using namespace dd4hep;

// FIXME: use local definition until dd4hep fixes the uniits
// using namespace geant_units::operators;

constexpr long double piRadians(M_PIl); // M_PIl is long double version of pi
constexpr long double degPerRad = 180. / piRadians; // Degrees per radian
constexpr double operator"" _deg(long double deg) { return deg / degPerRad; }
constexpr double operator"" _deg(unsigned long long int deg) { return deg / degPerRad; }

// Length
constexpr double operator"" _mm(long double length) { return length * 0.1; }
constexpr double operator"" _mm(unsigned long long int length) { return length * 0.1; }
constexpr double operator"" _cm(unsigned long long int length) { return length * 1; }

template <class NumType>
inline constexpr NumType convertRadToDeg(NumType radians) // Radians -> degrees
{
return (radians * degPerRad);
}

template <class NumType>
inline constexpr long double convertDegToRad(NumType degrees) // Degrees -> radians
{
return (degrees / degPerRad);
}
using namespace cms_units::operators;

using VecDouble = vector<double>;
using VecStr = vector<string>;
Expand Down
14 changes: 2 additions & 12 deletions Geometry/EcalCommonData/plugins/dd4hep/DDEcalEndcapAlgo.cc
@@ -1,6 +1,7 @@
#include "DD4hep/DetFactoryHelper.h"
#include "DetectorDescription/DDCMS/interface/DDPlugins.h"
#include "DetectorDescription/DDCMS/interface/BenchmarkGrd.h"
#include "DataFormats/Math/interface/CMSUnits.h"
// Header files for endcap supercrystal geometry
#include "Geometry/EcalCommonData/interface/DDEcalEndcapTrap.h"
#include <CLHEP/Geometry/Transform3D.h>
Expand All @@ -11,18 +12,7 @@
using namespace std;
using namespace cms;
using namespace dd4hep;
// FIXME: use local definition until dd4hep fixes the uniits
// using namespace geant_units::operators;

constexpr long double piRadians(M_PIl); // M_PIl is long double version of pi
constexpr long double degPerRad = 180. / piRadians; // Degrees per radian
constexpr double operator"" _deg(long double deg) { return deg / degPerRad; }
constexpr double operator"" _deg(unsigned long long int deg) { return deg / degPerRad; }

// Length
constexpr double operator"" _mm(long double length) { return length * 0.1; }
constexpr double operator"" _mm(unsigned long long int length) { return length * 0.1; }
constexpr double operator"" _cm(unsigned long long int length) { return length * 1; }
using namespace cms_units::operators;

using DDTranslation = ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double> >;
using DDRotation = ROOT::Math::Rotation3D;
Expand Down
16 changes: 2 additions & 14 deletions Geometry/EcalCommonData/plugins/dd4hep/DDEcalPreshowerAlgo.cc
@@ -1,6 +1,7 @@
#include "DD4hep/DetFactoryHelper.h"
#include "DetectorDescription/DDCMS/interface/DDPlugins.h"
#include "DetectorDescription/DDCMS/interface/BenchmarkGrd.h"
#include "DataFormats/Math/interface/CMSUnits.h"
#include "DD4hep/Shapes.h"

#include <string>
Expand All @@ -9,20 +10,7 @@
using namespace std;
using namespace cms;
using namespace dd4hep;

// FIXME: use local definition until dd4hep fixes the uniits
// using namespace geant_units::operators;

constexpr long double piRadians(M_PIl); // M_PIl is long double version of pi
constexpr long double degPerRad = 180. / piRadians; // Degrees per radian
constexpr double operator"" _deg(long double deg) { return deg / degPerRad; }
constexpr double operator"" _deg(unsigned long long int deg) { return deg / degPerRad; }

// Length
constexpr double operator"" _mm(long double length) { return length * 0.1; }
constexpr double operator"" _mm(unsigned long long int length) { return length * 0.1; }
constexpr double operator"" _cm(long double length) { return length * 1.; }
constexpr double operator"" _cm(unsigned long long int length) { return length * 1; }
using namespace cms_units::operators;

namespace {
struct EcalPreshower {
Expand Down