test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DDRotation.cc
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <atomic>
3 #include <cmath>
4 #include <sstream>
5 #include <string>
6 
7 #include "CLHEP/Units/GlobalSystemOfUnits.h"
8 #include "CLHEP/Units/SystemOfUnits.h"
17 #include "Math/GenVector/AxisAngle.h"
18 #include "Math/GenVector/Cartesian3D.h"
19 #include "Math/GenVector/DisplacementVector3D.h"
20 
21 std::ostream & operator<<(std::ostream & os, const DDRotation & r)
22 {
24  if (defined.first) {
25  os << *(defined.first) << " ";
26  if (defined.second) {
27  const DDRotationMatrix & rm = *(r.rotation());
28  DDAxisAngle ra(rm);
29  os << "t=" << ra.Axis().Theta()/deg << "deg "
30  << "p=" << ra.Axis().Phi()/deg << "deg "
31  << "a=" << ra.Angle()/deg << "deg";
32  }
33  else {
34  os << "* rotation not defined * ";
35  }
36  }
37  else {
38  os << "* rotation not declared * ";
39  }
40  return os;
41 }
42 
43 
45 {
46  constexpr char const* baseName = "DdBlNa";
47  // In this particular case, we do not really care about multiple threads
48  // using the same counter, we simply need to have a unique id for the
49  // blank matrix being created, so just making this static an atomic should do
50  // the trick. In order to ensure repeatibility one should also include some
51  // some run specific Id, I guess. Not sure it really matters.
52  static std::atomic<int> countBlank;
53  char buf[64];
54  snprintf(buf, 64, "%s%i", baseName, countBlank++);
55  prep_ = StoreT::instance().create(DDName(buf,baseName), new DDRotationMatrix );
56  // std::cout << "making a BLANK " << buf << " named rotation, " << prep_->second << std::endl;
57 }
58 
59 
61 {
62  prep_ = StoreT::instance().create(name);
63 
64 }
65 
66 
69 {
70  prep_ = StoreT::instance().create(name,rot);
71 
72 }
73 
74 
77 {
78  static std::atomic<int> countNN;
79  char buf[64];
80  snprintf(buf, 64, "DdNoNa%i", countNN++);
81  prep_ = StoreT::instance().create(DDName(buf, "DdNoNa"), rot);
82  // std::cout << "making a NO-NAME " << buf << " named rotation, " << prep_->second << std::endl;
83 }
84 
85 // void DDRotation::clear()
86 // {
87 // StoreT::instance().clear();
88 // }
89 
91 {
92  // memory of rot goes sto DDRotationImpl!!
93  return DDRotation(ddname, rot);
94 }
95 
96 // makes sure that the DDRotationMatrix constructed is right-handed and orthogonal.
97 DDRotation DDrot(const DDName & ddname,
98  double thetaX, double phiX,
99  double thetaY, double phiY,
100  double thetaZ, double phiZ)
101 {
102  // define 3 unit std::vectors
103  DD3Vector x(cos(phiX)*sin(thetaX), sin(phiX)*sin(thetaX), cos(thetaX));
104  DD3Vector y(cos(phiY)*sin(thetaY), sin(phiY)*sin(thetaY), cos(thetaY));
105  DD3Vector z(cos(phiZ)*sin(thetaZ), sin(phiZ)*sin(thetaZ), cos(thetaZ));
106 
107  double tol = 1.0e-3; // Geant4 compatible
108  double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system this must be -1
109  if (fabs(1.-check)>tol) {
110  edm::LogError("DDRotation") << ddname << " is not a RIGHT-handed orthonormal matrix!" << std::endl;
111  throw cms::Exception("DDException") << ddname.name() << " is not RIGHT-handed!";
112  }
113 
114  DDRotationMatrix* rot = new DDRotationMatrix(x.x(),y.x(),z.x(),
115  x.y(),y.y(),z.y(),
116  x.z(),y.z(),z.z());
117 
118  return DDRotation(ddname, rot);
119 
120 }
121 
122 
124 {
125  // memory of rot goes sto DDRotationImpl!!
126  return DDRotation(ddname, rot);
127 }
128 
129 
130 // makes sure that the DDRotationMatrix built is LEFT-handed coordinate system (i.e. reflected)
132  double thetaX, double phiX,
133  double thetaY, double phiY,
134  double thetaZ, double phiZ)
135 {
136 
137  // define 3 unit std::vectors forming the new left-handed axes
138  DD3Vector x(cos(phiX)*sin(thetaX), sin(phiX)*sin(thetaX), cos(thetaX));
139  DD3Vector y(cos(phiY)*sin(thetaY), sin(phiY)*sin(thetaY), cos(thetaY));
140  DD3Vector z(cos(phiZ)*sin(thetaZ), sin(phiZ)*sin(thetaZ), cos(thetaZ));
141 
142  double tol = 1.0e-3; // Geant4 compatible
143  double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system this must be -1
144  if (fabs(1.+check)>tol) {
145  edm::LogError("DDRotation") << ddname << " is not a LEFT-handed orthonormal matrix!" << std::endl;
146  throw cms::Exception("DDException") << ddname.name() << " is not LEFT-handed!";
147  }
148 
149  DDRotationMatrix* rot = new DDRotationMatrix(x.x(),y.x(),z.x(),
150  x.y(),y.y(),z.y(),
151  x.z(),y.z(),z.z());
152 
153  return DDRotation(ddname, rot);
154 }
155 
156 
157 // does NOT check LEFT or Right handed coordinate system takes either.
158 DDRotationMatrix * DDcreateRotationMatrix(double thetaX, double phiX,
159  double thetaY, double phiY,
160  double thetaZ, double phiZ)
161 {
162  // define 3 unit std::vectors forming the new left-handed axes
163  DD3Vector x(cos(phiX)*sin(thetaX), sin(phiX)*sin(thetaX), cos(thetaX));
164  DD3Vector y(cos(phiY)*sin(thetaY), sin(phiY)*sin(thetaY), cos(thetaY));
165  DD3Vector z(cos(phiZ)*sin(thetaZ), sin(phiZ)*sin(thetaZ), cos(thetaZ));
166 
167  double tol = 1.0e-3; // Geant4 compatible
168  double check = (x.Cross(y)).Dot(z);// in case of a LEFT-handed orthogonal system this must be -1, RIGHT-handed: +1
169  if ((1.-fabs(check))>tol) {
170  std::ostringstream o;
171  o << "matrix is not an (left or right handed) orthonormal matrix! (in deg)" << std::endl
172  << " thetaX=" << thetaX/deg << " phiX=" << phiX/deg << std::endl
173  << " thetaY=" << thetaY/deg << " phiY=" << phiY/deg << std::endl
174  << " thetaZ=" << thetaZ/deg << " phiZ=" << phiZ/deg << std::endl;
175  edm::LogError("DDRotation") << o.str() << std::endl;
176 
177 
178  throw cms::Exception("DDException") << o.str();
179  }
180 
181  return new DDRotationMatrix(x.x(),y.x(),z.x(),
182  x.y(),y.y(),z.y(),
183  x.z(),y.z(),z.z());
184 }
185 
186 
188 {
189  return DDRotation(rot);
190 }
Definition: DDBase.h:10
const DDRotationMatrix * rotation() const
Returns the read-only rotation-matrix.
Definition: DDTransform.h:90
def_type isDefined() const
Definition: DDBase.h:110
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:16
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:188
#define constexpr
float float float z
DDRotation DDanonymousRot(DDRotationMatrix *rot)
Defines a anonymous rotation or rotation-reflection matrix.
Definition: DDRotation.cc:187
Represents a uniquely identifyable rotation matrix.
Definition: DDTransform.h:66
T x() const
Cartesian x coordinate.
static value_type & instance()
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > DD3Vector
A DD Translation is currently implemented with Root Vector3D.
Definition: DDTranslation.h:6
string rm
Definition: submit.py:76
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
DDRotation DDrotReflect(const DDName &name, double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ)
Defines a rotation-reflection in the Geant3 way.
Definition: DDRotation.cc:131
DDRotation DDrot(const DDName &name, DDRotationMatrix *rot)
Definition of a uniquely identifiable rotation matrix named by DDName name.
Definition: DDRotation.cc:90
DDRotation()
refers to the unit-rotation (no rotation at all)
Definition: DDRotation.cc:44
ROOT::Math::AxisAngle DDAxisAngle
ROOT::Math::Rotation3D DDRotationMatrix
A DDRotationMatrix is currently implemented with a ROOT Rotation3D.
const std::string & name() const
Returns the name.
Definition: DDName.cc:90
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:158