Belle II Software  release-06-01-15
ExtCylSurfaceTarget.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <simulation/kernel/ExtCylSurfaceTarget.h>
10 #include <G4GeometryTolerance.hh>
11 #include <geomdefs.hh>
12 #include <G4Plane3D.hh>
13 #include <framework/logging/Logger.h>
14 
15 using namespace std;
16 using namespace Belle2;
17 using namespace Belle2::Simulation;
18 
19 ExtCylSurfaceTarget::ExtCylSurfaceTarget(const G4double& radius,
20  const G4double& zmin,
21  const G4double& zmax) :
22  m_radius(radius),
23  m_zmin(zmin),
24  m_zmax(zmax),
25  m_tolerance(1000.0 * G4GeometryTolerance::GetInstance()->GetSurfaceTolerance())
26 {
27  theType = G4ErrorTarget_CylindricalSurface;
28  B2DEBUG(1, "Simulation::ExtCylSurfaceTarget created with radius "
29  << m_radius << " zmin " << zmin << " zmax " << zmax);
30 }
31 
33 {
34 }
35 
36 G4double ExtCylSurfaceTarget::GetDistanceFromPoint(const G4ThreeVector& point,
37  const G4ThreeVector& dir) const
38 {
39  if (fabs(dir.mag() - 1.0) > 1.0E-10) {
40  B2FATAL("Simulation::ExtCylSurfaceTarget::GetDistanceFromPoint() direction is not a unit vector: " << dir);
41  }
42 
43  // Get distance to intersection point with the cylinder's curved surface
44  // should be negative if outside! G4double dist = (point - IntersectLocal(point, dir)).mag();
45  G4double dist = (IntersectLocal(point, dir) - point) * dir;
46 
47  // Get intersection point with the plane at either zmin or zmax
48  G4double dirz = dir.z();
49  if (dirz < -1.0E-10) {
50  dist = fmin(dist, (m_zmin - point.z()) / dirz);
51  } else if (dirz > 1.0E-10) {
52  dist = fmin(dist, (m_zmax - point.z()) / dirz);
53  }
54 
55  B2DEBUG(300, "Simulation::ExtCylSurfaceTarget::GetDistanceFromPoint(): Global point "
56  << point << " dir " << dir << " distance " << dist);
57 
58  return dist;
59 }
60 
61 G4double ExtCylSurfaceTarget::GetDistanceFromPoint(const G4ThreeVector& point) const
62 {
63 
64  G4double dist = m_radius - point.perp();
65  dist = fmin(dist, point.z() - m_zmin);
66  dist = fmin(dist, m_zmax - point.z());
67 
68  B2DEBUG(300, "Simulation::ExtCylSurfaceTarget::GetDistanceFromPoint(): Global point "
69  << point << " minimum distance " << dist);
70 
71  return dist;
72 }
73 
74 G4ThreeVector ExtCylSurfaceTarget::IntersectLocal(const G4ThreeVector& localPoint,
75  const G4ThreeVector& localDir) const
76 {
77  // localDir has already been verified to be a unit vector
78  G4double eqa = localDir.x() * localDir.x() + localDir.y() * localDir.y();
79  G4double eqb = 2.0 * (localPoint.x() * localDir.x() + localPoint.y() * localDir.y());
80  G4double eqc = localPoint.x() * localPoint.x() + localPoint.y() * localPoint.y()
81  - m_radius * m_radius;
82  G4double eqaInside = (localPoint.perp() > m_radius) ? -eqa : eqa;
83 
84  G4ThreeVector intersection = localPoint;
85  if (eqaInside > 0.0) {
86  intersection += localDir * ((-eqb + sqrt(eqb * eqb - 4.0 * eqa * eqc)) / (2.0 * eqa));
87  } else if (eqaInside < 0.0) {
88  intersection += localDir * ((-eqb - sqrt(eqb * eqb - 4.0 * eqa * eqc)) / (2.0 * eqa));
89  } else {
90  if (eqb != 0.0) {
91  intersection -= localDir * (eqc / eqb);
92  } else {
93  B2WARNING("Simulation::ExtCylSurfaceTarget::IntersectLocal(): localPoint "
94  << localPoint << " localDir " << localDir << " does not intersect with cylinder");
95  intersection = localDir * kInfinity;
96  }
97  }
98 
99  B2DEBUG(300, "Simulation::ExtCylSurfaceTarget::IntersectLocal(): localPoint "
100  << localPoint << " localDir " << localDir << " radial intersection " << intersection.perp());
101 
102  return intersection;
103 }
104 
105 G4Plane3D ExtCylSurfaceTarget::GetTangentPlane(const G4ThreeVector& point) const
106 {
107  // check that point is at the cylinder's curved surface
108  if (fabs(point.perp() - m_radius) > m_tolerance) {
109  B2ERROR("Simulation::ExtCylSurfaceTarget::GetTangentPlane(): point "
110  << point << " is not at surface; radial distance is " << point.perp() - m_radius);
111  }
112 
113  G4Normal3D normal(point);
114 
115  return G4Plane3D(normal, point);
116 }
117 
119 void ExtCylSurfaceTarget::Dump(const G4String&) const
120 {
121 }
122 
G4double m_tolerance
Tolerance for distance between a point and cylinder's curved surface.
virtual G4Plane3D GetTangentPlane(const G4ThreeVector &point) const
Get the plane tangent to the cylinder at a given point.
virtual void Dump(const G4String &msg) const
Dump the cylinder parameters.
virtual G4ThreeVector IntersectLocal(const G4ThreeVector &point, const G4ThreeVector &direc) const
Return the intersection of the cylinder with the line defined in local (cylinder) coordinates by poin...
G4double m_zmax
Cylinder maximum-z coordinate.
virtual G4double GetDistanceFromPoint(const G4ThreeVector &point, const G4ThreeVector &direc) const
Get the distance from a point to the cylinder along direc.
G4double m_zmin
Cylinder minimum-z coordinate.
Abstract base class for different kinds of events.