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 
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 std::unique_ptr<DDRotation> DDrotPtr(const DDName & ddname, DDRotationMatrix * rot)
97 {
98  // memory of rot goes sto DDRotationImpl!!
99  return std::make_unique<DDRotation>(ddname, rot);
100 }
101 
102 // makes sure that the DDRotationMatrix constructed is right-handed and orthogonal.
104  double thetaX, double phiX,
105  double thetaY, double phiY,
106  double thetaZ, double phiZ)
107 {
108  // define 3 unit std::vectors
109  DD3Vector x(cos(phiX)*sin(thetaX), sin(phiX)*sin(thetaX), cos(thetaX));
110  DD3Vector y(cos(phiY)*sin(thetaY), sin(phiY)*sin(thetaY), cos(thetaY));
111  DD3Vector z(cos(phiZ)*sin(thetaZ), sin(phiZ)*sin(thetaZ), cos(thetaZ));
112 
113  double tol = 1.0e-3; // Geant4 compatible
114  double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system this must be -1
115  if (fabs(1.-check)>tol) {
116  edm::LogError("DDRotation") << ddname << " is not a RIGHT-handed orthonormal matrix!" << std::endl;
117  throw cms::Exception("DDException") << ddname.name() << " is not RIGHT-handed!";
118  }
119 
120  DDRotationMatrix* rot = new DDRotationMatrix(x.x(),y.x(),z.x(),
121  x.y(),y.y(),z.y(),
122  x.z(),y.z(),z.z());
123 
124  return DDRotation(ddname, rot);
125 
126 }
127 
128 
130 {
131  // memory of rot goes sto DDRotationImpl!!
132  return DDRotation(ddname, rot);
133 }
134 
135 
136 // makes sure that the DDRotationMatrix built is LEFT-handed coordinate system (i.e. reflected)
138  double thetaX, double phiX,
139  double thetaY, double phiY,
140  double thetaZ, double phiZ)
141 {
142 
143  // define 3 unit std::vectors forming the new left-handed axes
144  DD3Vector x(cos(phiX)*sin(thetaX), sin(phiX)*sin(thetaX), cos(thetaX));
145  DD3Vector y(cos(phiY)*sin(thetaY), sin(phiY)*sin(thetaY), cos(thetaY));
146  DD3Vector z(cos(phiZ)*sin(thetaZ), sin(phiZ)*sin(thetaZ), cos(thetaZ));
147 
148  double tol = 1.0e-3; // Geant4 compatible
149  double check = (x.Cross(y)).Dot(z); // in case of a LEFT-handed orthogonal system this must be -1
150  if (fabs(1.+check)>tol) {
151  edm::LogError("DDRotation") << ddname << " is not a LEFT-handed orthonormal matrix!" << std::endl;
152  throw cms::Exception("DDException") << ddname.name() << " is not LEFT-handed!";
153  }
154 
155  DDRotationMatrix* rot = new DDRotationMatrix(x.x(),y.x(),z.x(),
156  x.y(),y.y(),z.y(),
157  x.z(),y.z(),z.z());
158 
159  return DDRotation(ddname, rot);
160 }
161 
162 
163 // does NOT check LEFT or Right handed coordinate system takes either.
165  double thetaY, double phiY,
166  double thetaZ, double phiZ)
167 {
168  // define 3 unit std::vectors forming the new left-handed axes
169  DD3Vector x(cos(phiX)*sin(thetaX), sin(phiX)*sin(thetaX), cos(thetaX));
170  DD3Vector y(cos(phiY)*sin(thetaY), sin(phiY)*sin(thetaY), cos(thetaY));
171  DD3Vector z(cos(phiZ)*sin(thetaZ), sin(phiZ)*sin(thetaZ), cos(thetaZ));
172 
173  double tol = 1.0e-3; // Geant4 compatible
174  double check = (x.Cross(y)).Dot(z);// in case of a LEFT-handed orthogonal system this must be -1, RIGHT-handed: +1
175  if ((1.-fabs(check))>tol) {
176  std::ostringstream o;
177  o << "matrix is not an (left or right handed) orthonormal matrix! (in deg)" << std::endl
178  << " thetaX=" << thetaX/deg << " phiX=" << phiX/deg << std::endl
179  << " thetaY=" << thetaY/deg << " phiY=" << phiY/deg << std::endl
180  << " thetaZ=" << thetaZ/deg << " phiZ=" << phiZ/deg << std::endl;
181  edm::LogError("DDRotation") << o.str() << std::endl;
182 
183 
184  throw cms::Exception("DDException") << o.str();
185  }
186 
187  return new DDRotationMatrix(x.x(),y.x(),z.x(),
188  x.y(),y.y(),z.y(),
189  x.z(),y.z(),z.z());
190 }
191 
192 
194 {
195  return DDRotation(rot);
196 }
Definition: DDBase.h:10
const DDRotationMatrix * rotation() const
Returns the read-only rotation-matrix.
Definition: DDTransform.h:91
def_type isDefined() const
Definition: DDBase.h:110
const DDName & name() const
Definition: DDBase.h:78
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:15
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:164
#define constexpr
Represents a uniquely identifyable rotation matrix.
Definition: DDTransform.h:67
static value_type & instance()
std::ostream & operator<<(std::ostream &os, const DDRotation &r)
Definition: DDRotation.cc:21
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > DD3Vector
A DD Translation is currently implemented with Root Vector3D.
Definition: DDTranslation.h:6
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
friend DDRotation DDrotReflect(const DDName &, double, double, double, double, double, double)
Defines a rotation-reflection in the Geant3 way.
Definition: DDRotation.cc:137
friend DDRotation DDrot(const DDName &, DDRotationMatrix *)
Definition of a uniquely identifiable rotation matrix named by DDName name.
Definition: DDRotation.cc:90
friend std::unique_ptr< DDRotation > DDrotPtr(const DDName &, DDRotationMatrix *)
Definition: DDRotation.cc:96
friend DDRotation DDanonymousRot(DDRotationMatrix *)
Defines a anonymous rotation or rotation-reflection matrix.
Definition: DDRotation.cc:193
DDRotation()
refers to the unit-rotation (no rotation at all)
Definition: DDRotation.cc:44
def check(config)
Definition: trackerTree.py:14
rm
Definition: submit.py:76
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
const DDName & ddname() const
Definition: DDBase.h:80