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 <cmath>
5 #include "CLHEP/Units/GlobalSystemOfUnits.h"
6 #include <Math/AxisAngle.h>
7 
8 #include <sstream>
9 #include <cstdlib>
10 #include <atomic>
11 
12 // Message logger.
14 
15 //static DDRotationMatrix GLOBAL_UNIT;
16 
17 //DDBase<DDName,DDRotationMatrix*>::StoreT::pointer_type
18 // DDBase<DDName,DDRotationMatrix*>::StoreT::instance_ = 0;
19 
20 std::ostream & operator<<(std::ostream & os, const DDRotation & r)
21 {
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=" << ra.Axis().Theta()/deg << "deg "
29  << "p=" << ra.Axis().Phi()/deg << "deg "
30  << "a=" << ra.Angle()/deg << "deg";
31  DCOUT_V('R', rm);
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  //DCOUT('c', "DDrot: new rotation " << ddname);
94  //if (rot) rot->invert();
95  return DDRotation(ddname, rot);
96 }
97 
98 // makes sure that the DDRotationMatrix constructed is right-handed and orthogonal.
99 DDRotation DDrot(const DDName & ddname,
100  double thetaX, double phiX,
101  double thetaY, double phiY,
102  double thetaZ, double phiZ)
103 {
104  // define 3 unit std::vectors
105  DD3Vector x(cos(phiX)*sin(thetaX), sin(phiX)*sin(thetaX), cos(thetaX));
106  DD3Vector y(cos(phiY)*sin(thetaY), sin(phiY)*sin(thetaY), cos(thetaY));
107  DD3Vector z(cos(phiZ)*sin(thetaZ), sin(phiZ)*sin(thetaZ), cos(thetaZ));
108 
109  double tol = 1.0e-3; // Geant4 compatible
110  double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system this must be -1
111  if (fabs(1.-check)>tol) {
112  edm::LogError("DDRotation") << ddname << " is not a RIGHT-handed orthonormal matrix!" << std::endl;
113  throw cms::Exception("DDException") << ddname.name() << " is not RIGHT-handed!";
114  }
115 
116  DDRotationMatrix* rot = new DDRotationMatrix(x.x(),y.x(),z.x(),
117  x.y(),y.y(),z.y(),
118  x.z(),y.z(),z.z());
119 
120  return DDRotation(ddname, rot);
121 
122 }
123 
124 
126 {
127  // memory of rot goes sto DDRotationImpl!!
128  //DCOUT('c', "DDrot: new rotation " << ddname);
129 // if (rot) rot->invert();
130  return DDRotation(ddname, rot);
131 }
132 
133 
134 // makes sure that the DDRotationMatrix built is LEFT-handed coordinate system (i.e. reflected)
136  double thetaX, double phiX,
137  double thetaY, double phiY,
138  double thetaZ, double phiZ)
139 {
140 
141  // define 3 unit std::vectors forming the new left-handed axes
142  DD3Vector x(cos(phiX)*sin(thetaX), sin(phiX)*sin(thetaX), cos(thetaX));
143  DD3Vector y(cos(phiY)*sin(thetaY), sin(phiY)*sin(thetaY), cos(thetaY));
144  DD3Vector z(cos(phiZ)*sin(thetaZ), sin(phiZ)*sin(thetaZ), cos(thetaZ));
145 
146  double tol = 1.0e-3; // Geant4 compatible
147  double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system this must be -1
148  if (fabs(1.+check)>tol) {
149  edm::LogError("DDRotation") << ddname << " is not a LEFT-handed orthonormal matrix!" << std::endl;
150  throw cms::Exception("DDException") << ddname.name() << " is not LEFT-handed!";
151  }
152 
153  DDRotationMatrix* rot = new DDRotationMatrix(x.x(),y.x(),z.x(),
154  x.y(),y.y(),z.y(),
155  x.z(),y.z(),z.z());
156 
157  //DCOUT('c', "DDrotReflect: new reflection " << ddname);
158  //rot->invert();
159  return DDRotation(ddname, rot);
160 
161 }
162 
163 
164 // does NOT check LEFT or Right handed coordinate system takes either.
165 DDRotationMatrix * DDcreateRotationMatrix(double thetaX, double phiX,
166  double thetaY, double phiY,
167  double thetaZ, double phiZ)
168 {
169  // define 3 unit std::vectors forming the new left-handed axes
170  DD3Vector x(cos(phiX)*sin(thetaX), sin(phiX)*sin(thetaX), cos(thetaX));
171  DD3Vector y(cos(phiY)*sin(thetaY), sin(phiY)*sin(thetaY), cos(thetaY));
172  DD3Vector z(cos(phiZ)*sin(thetaZ), sin(phiZ)*sin(thetaZ), cos(thetaZ));
173 
174  double tol = 1.0e-3; // Geant4 compatible
175  double check = (x.Cross(y)).Dot(z);// in case of a LEFT-handed orthogonal system this must be -1, RIGHT-handed: +1
176  if ((1.-fabs(check))>tol) {
177  std::ostringstream o;
178  o << "matrix is not an (left or right handed) orthonormal matrix! (in deg)" << std::endl
179  << " thetaX=" << thetaX/deg << " phiX=" << phiX/deg << std::endl
180  << " thetaY=" << thetaY/deg << " phiY=" << phiY/deg << std::endl
181  << " thetaZ=" << thetaZ/deg << " phiZ=" << phiZ/deg << std::endl;
182  edm::LogError("DDRotation") << o.str() << std::endl;
183 
184 
185  throw cms::Exception("DDException") << o.str();
186  }
187 
188  return new DDRotationMatrix(x.x(),y.x(),z.x(),
189  x.y(),y.y(),z.y(),
190  x.z(),y.z(),z.z());
191 }
192 
193 
195 {
196  return DDRotation(rot);
197 }
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:14
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:194
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:135
DDRotation DDrot(const DDName &name, DDRotationMatrix *rot)
Definition of a uniquely identifiable rotation matrix named by DDName name.
Definition: DDRotation.cc:90
#define DCOUT_V(M_v_Y, M_v_S)
Definition: DDdebug.h:54
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:87
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:165