CMS 3D CMS Logo

BaseVolumeHandle.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * \author N. Amapane - INFN Torino (original developer)
5  */
6 
7 #include "BaseVolumeHandle.h"
8 
13 
15 
16 #include <cassert>
17 #include <string>
18 #include <iterator>
19 #include <iomanip>
20 #include <iostream>
21 
22 using namespace SurfaceOrientation;
23 using namespace std;
24 using namespace magneticfield;
25 
26 BaseVolumeHandle::BaseVolumeHandle(bool expand2Pi, bool debugVal)
27  : magVolume(nullptr),
28  masterSector(1),
29  theRN(0.),
30  theRMin(0.),
31  theRMax(0.),
32  refPlane(nullptr),
33  expand(expand2Pi),
34  isIronFlag(false),
35  debug(debugVal) {}
36 
38  if (refPlane != nullptr) {
39  delete refPlane;
40  refPlane = nullptr;
41  }
42 }
43 
45 
46 void BaseVolumeHandle::buildPhiZSurf(double startPhi, double deltaPhi, double zhalf, double rCentr) {
47  // This is 100% equal for cons and tubs!!!
48 
49  GlobalVector planeXAxis = refPlane->toGlobal(LocalVector(1, 0, 0));
50  GlobalVector planeYAxis = refPlane->toGlobal(LocalVector(0, 1, 0));
51  GlobalVector planeZAxis = refPlane->toGlobal(LocalVector(0, 0, 1));
52 
53  // Local Y axis of the faces at +-phi.
54  GlobalVector y_phiplus = refPlane->toGlobal(LocalVector(cos(startPhi + deltaPhi), sin(startPhi + deltaPhi), 0.));
55  GlobalVector y_phiminus = refPlane->toGlobal(LocalVector(cos(startPhi), sin(startPhi), 0.));
56 
57  Surface::RotationType rot_Z(planeXAxis, planeYAxis);
58  Surface::RotationType rot_phiplus(planeZAxis, y_phiplus);
59  Surface::RotationType rot_phiminus(planeZAxis, y_phiminus);
60 
61  GlobalPoint pos_zplus(center_.x(), center_.y(), center_.z() + zhalf);
62  GlobalPoint pos_zminus(center_.x(), center_.y(), center_.z() - zhalf);
63  // BEWARE: in this case, the origin for phiplus,phiminus surfaces is
64  // at radius R and not on a plane passing by center_ orthogonal to the radius.
65  GlobalPoint pos_phiplus(
66  refPlane->toGlobal(LocalPoint(rCentr * cos(startPhi + deltaPhi), rCentr * sin(startPhi + deltaPhi), 0.)));
67  GlobalPoint pos_phiminus(refPlane->toGlobal(LocalPoint(rCentr * cos(startPhi), rCentr * sin(startPhi), 0.)));
68  surfaces[zplus] = new Plane(pos_zplus, rot_Z);
69  surfaces[zminus] = new Plane(pos_zminus, rot_Z);
70  surfaces[phiplus] = new Plane(pos_phiplus, rot_phiplus);
71  surfaces[phiminus] = new Plane(pos_phiminus, rot_phiminus);
72 
73  if (debug) {
74  cout << "Actual Center at: " << center_ << " R " << center_.perp() << " phi " << center_.phi() << endl;
75  cout << "RN " << theRN << endl;
76 
77  cout << "pos_zplus " << pos_zplus << " " << pos_zplus.perp() << " " << pos_zplus.phi() << endl
78  << "pos_zminus " << pos_zminus << " " << pos_zminus.perp() << " " << pos_zminus.phi() << endl
79  << "pos_phiplus " << pos_phiplus << " " << pos_phiplus.perp() << " " << pos_phiplus.phi() << endl
80  << "pos_phiminus " << pos_phiminus << " " << pos_phiminus.perp() << " " << pos_phiminus.phi() << endl;
81 
82  cout << "y_phiplus " << y_phiplus << endl;
83  cout << "y_phiminus " << y_phiminus << endl;
84 
85  cout << "rot_Z " << surfaces[zplus]->toGlobal(LocalVector(0., 0., 1.)) << endl
86  << "rot_phi+ " << surfaces[phiplus]->toGlobal(LocalVector(0., 0., 1.)) << " phi "
87  << surfaces[phiplus]->toGlobal(LocalVector(0., 0., 1.)).phi() << endl
88  << "rot_phi- " << surfaces[phiminus]->toGlobal(LocalVector(0., 0., 1.)) << " phi "
89  << surfaces[phiminus]->toGlobal(LocalVector(0., 0., 1.)).phi() << endl;
90  }
91 
92  // // Check ordering.
93  if (debug) {
94  if (pos_zplus.z() < pos_zminus.z()) {
95  cout << "*** WARNING: pos_zplus < pos_zminus " << endl;
96  }
97  if (Geom::Phi<float>(pos_phiplus.phi() - pos_phiminus.phi()) < 0.) {
98  cout << "*** WARNING: pos_phiplus < pos_phiminus " << endl;
99  }
100  }
101 }
102 
103 bool BaseVolumeHandle::sameSurface(const Surface &s1, Sides which_side, float tolerance) {
104  //Check for null comparison
105  if (&s1 == (surfaces[which_side]).get()) {
106  if (debug)
107  cout << " sameSurface: OK (same ptr)" << endl;
108  return true;
109  }
110 
111  const float maxtilt = 0.999;
112 
113  const Surface &s2 = *(surfaces[which_side]);
114  // Try with a plane.
115  const Plane *p1 = dynamic_cast<const Plane *>(&s1);
116  if (p1 != nullptr) {
117  const Plane *p2 = dynamic_cast<const Plane *>(&s2);
118  if (p2 == nullptr) {
119  if (debug)
120  cout << " sameSurface: different types" << endl;
121  return false;
122  }
123 
124  if ((fabs(p1->normalVector().dot(p2->normalVector())) > maxtilt) &&
125  (fabs((p1->toLocal(p2->position())).z()) < tolerance)) {
126  if (debug)
127  cout << " sameSurface: OK " << fabs(p1->normalVector().dot(p2->normalVector())) << " "
128  << fabs((p1->toLocal(p2->position())).z()) << endl;
129  return true;
130  } else {
131  if (debug)
132  cout << " sameSurface: not the same: " << p1->normalVector() << p1->position() << endl
133  << " " << p2->normalVector() << p2->position() << endl
134  << fabs(p1->normalVector().dot(p2->normalVector())) << " " << (p1->toLocal(p2->position())).z() << endl;
135  return false;
136  }
137  }
138 
139  // Try with a cylinder.
140  const Cylinder *cy1 = dynamic_cast<const Cylinder *>(&s1);
141  if (cy1 != nullptr) {
142  const Cylinder *cy2 = dynamic_cast<const Cylinder *>(&s2);
143  if (cy2 == nullptr) {
144  if (debug)
145  cout << " sameSurface: different types" << endl;
146  return false;
147  }
148  // Assume axis is the same!
149  if (fabs(cy1->radius() - cy2->radius()) < tolerance) {
150  return true;
151  } else {
152  return false;
153  }
154  }
155 
156  // Try with a cone.
157  const Cone *co1 = dynamic_cast<const Cone *>(&s1);
158  if (co1 != nullptr) {
159  const Cone *co2 = dynamic_cast<const Cone *>(&s2);
160  if (co2 == nullptr) {
161  if (debug)
162  cout << " sameSurface: different types" << endl;
163  return false;
164  }
165  // FIXME
166  if (fabs(co1->openingAngle() - co2->openingAngle()) < maxtilt &&
167  (co1->vertex() - co2->vertex()).mag() < tolerance) {
168  return true;
169  } else {
170  return false;
171  }
172  }
173 
174  if (debug)
175  cout << " sameSurface: unknown surfaces..." << endl;
176  return false;
177 }
178 
179 bool BaseVolumeHandle::setSurface(const Surface &s1, Sides which_side) {
180  //Check for null assignment
181  if (&s1 == (surfaces[which_side]).get()) {
182  isAssigned[which_side] = true;
183  return true;
184  }
185 
186  if (!sameSurface(s1, which_side)) {
187  cout << "***ERROR: setSurface: trying to assign a surface that does not match destination surface. Skipping."
188  << endl;
189  const Surface &s2 = *(surfaces[which_side]);
190  //FIXME: Just planes for the time being!!!
191  const Plane *p1 = dynamic_cast<const Plane *>(&s1);
192  const Plane *p2 = dynamic_cast<const Plane *>(&s2);
193  if (p1 != nullptr && p2 != nullptr)
194  cout << p1->normalVector() << p1->position() << endl << p2->normalVector() << p2->position() << endl;
195  return false;
196  }
197 
198  if (isAssigned[which_side]) {
199  if (&s1 != (surfaces[which_side]).get()) {
200  cout << "*** WARNING BaseVolumeHandle::setSurface: trying to reassign a surface to a different surface instance"
201  << endl;
202  return false;
203  }
204  } else {
205  surfaces[which_side] = &s1;
206  isAssigned[which_side] = true;
207  if (debug)
208  cout << " Volume " << name << " # " << copyno << " Assigned: " << (int)which_side << endl;
209  return true;
210  }
211 
212  return false; // let the compiler be happy
213 }
214 
215 const Surface &BaseVolumeHandle::surface(Sides which_side) const { return *(surfaces[which_side]); }
216 
217 const Surface &BaseVolumeHandle::surface(int which_side) const {
218  assert(which_side >= 0 && which_side < 6);
219  return *(surfaces[which_side]);
220 }
Definition: Cone.h:17
T perp() const
Definition: PV3DBase.h:69
Surface::LocalVector LocalVector
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
const double tolerance
GlobalVector normalVector() const
Definition: Plane.h:41
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
#define nullptr
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
T y() const
Definition: PV3DBase.h:60
PreciseFloatType< T, U >::Type dot(const Vector3DBase< U, FrameTag > &v) const
Definition: Vector3DBase.h:99
Definition: Plane.h:16
unsigned short copyno
copy number
GlobalPoint vertex() const
Global position of the cone vertex.
Definition: Cone.h:47
GloballyPositioned< float > * refPlane
const Surface & surface(int which_side) const
Get the current surface on specified side.
Scalar radius() const
Radius of the cylinder.
Definition: Cylinder.h:64
LocalPoint toLocal(const GlobalPoint &gp) const
T z() const
Definition: PV3DBase.h:61
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
double p2[4]
Definition: TauolaWrapper.h:90
const GlobalPoint & center() const
Return the center of the volume.
void buildPhiZSurf(double startPhi, double deltaPhi, double zhalf, double rCentr)
GlobalPoint toGlobal(const LocalPoint &lp) const
#define debug
Definition: HDRShower.cc:19
std::string name
Name of the volume.
double p1[4]
Definition: TauolaWrapper.h:89
bool setSurface(const Surface &s1, Sides which_side)
Assign a shared surface perorming sanity checks.
T x() const
Definition: PV3DBase.h:59
const PositionType & position() const
bool sameSurface(const Surface &s1, Sides which_side, float tolerance=0.01)
Find out if two surfaces are the same physical surface.
Geom::Theta< float > openingAngle() const
Angle of the cone.
Definition: Cone.h:50