CMS 3D CMS Logo

DDRotation.cc
Go to the documentation of this file.
1 #include <cstdio>
2 #include <atomic>
3 #include <cmath>
4 #include <sstream>
5 #include <string>
6 
15 #include "Math/GenVector/AxisAngle.h"
16 #include "Math/GenVector/Cartesian3D.h"
17 #include "Math/GenVector/DisplacementVector3D.h"
18 
19 using namespace geant_units::operators;
20 
21 std::ostream& operator<<(std::ostream& os, const DDRotation& r) {
22  DDBase<DDName, DDRotationMatrix*>::def_type defined(r.isDefined());
23  if (defined.first) {
24  os << *(defined.first) << " ";
25  if (defined.second) {
26  const DDRotationMatrix& rm = r.rotation();
27  DDAxisAngle ra(rm);
28  os << "t=" << convertRadToDeg(ra.Axis().Theta()) << "deg "
29  << "p=" << convertRadToDeg(ra.Axis().Phi()) << "deg "
30  << "a=" << convertRadToDeg(ra.Angle()) << "deg";
31  } else {
32  os << "* rotation not defined * ";
33  }
34  } else {
35  os << "* rotation not declared * ";
36  }
37  return os;
38 }
39 
41  constexpr char const* baseName = "DdBlNa";
42  // In this particular case, we do not really care about multiple threads
43  // using the same counter, we simply need to have a unique id for the
44  // blank matrix being created, so just making this static an atomic should do
45  // the trick. In order to ensure repeatibility one should also include some
46  // some run specific Id, I guess. Not sure it really matters.
47  static std::atomic<int> countBlank;
48  char buf[64];
49  snprintf(buf, 64, "%s%i", baseName, countBlank++);
50  create(DDName(buf, baseName), std::make_unique<DDRotationMatrix>());
51 }
52 
54 
55 DDRotation::DDRotation(const DDName& name, std::unique_ptr<DDRotationMatrix> rot)
58 }
59 
60 DDRotation::DDRotation(std::unique_ptr<DDRotationMatrix> rot) : DDBase<DDName, std::unique_ptr<DDRotationMatrix>>() {
61  static std::atomic<int> countNN;
62  char buf[64];
63  snprintf(buf, 64, "DdNoNa%i", countNN++);
64  create(DDName(buf, "DdNoNa"), std::move(rot));
65 }
66 
67 DDRotation DDrot(const DDName& ddname, std::unique_ptr<DDRotationMatrix> rot) {
68  // memory of rot goes sto DDRotationImpl!!
69  return DDRotation(ddname, std::move(rot));
70 }
71 
72 std::unique_ptr<DDRotation> DDrotPtr(const DDName& ddname, std::unique_ptr<DDRotationMatrix> rot) {
73  // memory of rot goes sto DDRotationImpl!!
74  return std::make_unique<DDRotation>(ddname, std::move(rot));
75 }
76 
77 // makes sure that the DDRotationMatrix constructed is right-handed and orthogonal.
79  const DDName& ddname, double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ) {
80  // define 3 unit std::vectors
81  DD3Vector x(cos(phiX) * sin(thetaX), sin(phiX) * sin(thetaX), cos(thetaX));
82  DD3Vector y(cos(phiY) * sin(thetaY), sin(phiY) * sin(thetaY), cos(thetaY));
83  DD3Vector z(cos(phiZ) * sin(thetaZ), sin(phiZ) * sin(thetaZ), cos(thetaZ));
84 
85  double tol = 1.0e-3; // Geant4 compatible
86  double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system this must be -1
87  if (fabs(1. - check) > tol) {
88  edm::LogError("DDRotation") << ddname << " is not a RIGHT-handed orthonormal matrix!" << std::endl;
89  throw cms::Exception("DDException") << ddname.name() << " is not RIGHT-handed!";
90  }
91 
92  return DDRotation(ddname,
93  std::make_unique<DDRotationMatrix>(x.x(), y.x(), z.x(), x.y(), y.y(), z.y(), x.z(), y.z(), z.z()));
94 }
95 
96 DDRotation DDrotReflect(const DDName& ddname, std::unique_ptr<DDRotationMatrix> rot) {
97  return DDRotation(ddname, std::move(rot));
98 }
99 
100 // makes sure that the DDRotationMatrix built is LEFT-handed coordinate system (i.e. reflected)
102  const DDName& ddname, double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ) {
103  // define 3 unit std::vectors forming the new left-handed axes
104  DD3Vector x(cos(phiX) * sin(thetaX), sin(phiX) * sin(thetaX), cos(thetaX));
105  DD3Vector y(cos(phiY) * sin(thetaY), sin(phiY) * sin(thetaY), cos(thetaY));
106  DD3Vector z(cos(phiZ) * sin(thetaZ), sin(phiZ) * sin(thetaZ), cos(thetaZ));
107 
108  double tol = 1.0e-3; // Geant4 compatible
109  double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system this must be -1
110  if (fabs(1. + check) > tol) {
111  edm::LogError("DDRotation") << ddname << " is not a LEFT-handed orthonormal matrix!" << std::endl;
112  throw cms::Exception("DDException") << ddname.name() << " is not LEFT-handed!";
113  }
114 
115  return DDRotation(ddname,
116  std::make_unique<DDRotationMatrix>(x.x(), y.x(), z.x(), x.y(), y.y(), z.y(), x.z(), y.z(), z.z()));
117 }
118 
119 // does NOT check LEFT or Right handed coordinate system takes either.
120 std::unique_ptr<DDRotationMatrix> DDcreateRotationMatrix(
121  double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ) {
122  // define 3 unit std::vectors forming the new left-handed axes
123  DD3Vector x(cos(phiX) * sin(thetaX), sin(phiX) * sin(thetaX), cos(thetaX));
124  DD3Vector y(cos(phiY) * sin(thetaY), sin(phiY) * sin(thetaY), cos(thetaY));
125  DD3Vector z(cos(phiZ) * sin(thetaZ), sin(phiZ) * sin(thetaZ), cos(thetaZ));
126 
127  double tol = 1.0e-3; // Geant4 compatible
128  double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system this must be -1, RIGHT-handed: +1
129  if ((1. - fabs(check)) > tol) {
130  std::ostringstream o;
131  o << "matrix is not an (left or right handed) orthonormal matrix! (in deg)" << std::endl
132  << " thetaX=" << convertRadToDeg(thetaX) << " phiX=" << convertRadToDeg(phiX) << std::endl
133  << " thetaY=" << convertRadToDeg(thetaY) << " phiY=" << convertRadToDeg(phiY) << std::endl
134  << " thetaZ=" << convertRadToDeg(thetaZ) << " phiZ=" << convertRadToDeg(phiZ) << std::endl;
135  edm::LogError("DDRotation") << o.str() << std::endl;
136 
137  throw cms::Exception("DDException") << o.str();
138  }
139 
140  return std::make_unique<DDRotationMatrix>(x.x(), y.x(), z.x(), x.y(), y.y(), z.y(), x.z(), y.z(), z.z());
141 }
142 
143 DDRotation DDanonymousRot(std::unique_ptr<DDRotationMatrix> rot) { return DDRotation(std::move(rot)); }
Definition: DDBase.h:10
def rm(path, rec=False)
Definition: eostools.py:363
DDRotation DDrotReflect(const DDName &ddname, std::unique_ptr< DDRotationMatrix > rot)
Definition: DDRotation.cc:96
ROOT::Math::AxisAngle DDAxisAngle
constexpr NumType convertRadToDeg(NumType radians)
Definition: angle_units.h:21
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:17
Log< level::Error, false > LogError
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
float float float z
Represents a uniquely identifyable rotation matrix.
Definition: DDTransform.h:57
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > DD3Vector
std::ostream & operator<<(std::ostream &os, const DDRotation &r)
Definition: DDRotation.cc:21
const std::string & name() const
Returns the name.
Definition: DDName.cc:41
std::unique_ptr< DDRotation > DDrotPtr(const DDName &ddname, std::unique_ptr< DDRotationMatrix > rot)
Definition: DDRotation.cc:72
ROOT::Math::Rotation3D DDRotationMatrix
A DDRotationMatrix is currently implemented with a ROOT Rotation3D.
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
std::unique_ptr< DDRotationMatrix > DDcreateRotationMatrix(double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ)
create a new DDRotationMatrix in the GEANT3 style.
Definition: DDRotation.cc:120
DDRotation DDrot(const DDName &ddname, std::unique_ptr< DDRotationMatrix > rot)
Definition of a uniquely identifiable rotation matrix named by DDName name.
Definition: DDRotation.cc:67
DDRotation DDanonymousRot(std::unique_ptr< DDRotationMatrix > rot)
Defines a anonymous rotation or rotation-reflection matrix.
Definition: DDRotation.cc:143
std::pair< const N *, bool > def_type
Definition: DDBase.h:51
float x
DDRotation()
refers to the unit-rotation (no rotation at all)
Definition: DDRotation.cc:40
def move(src, dest)
Definition: eostools.py:511
void create(const DDName &name, std::unique_ptr< DDRotationMatrix > vals)
Definition: DDBase.h:96