CMS 3D CMS Logo

StraightLineCylinderCrossing.cc
Go to the documentation of this file.
2 
5 
6 #include <cmath>
7 #include <iostream>
8 using namespace std;
9 
11  const LocalVector& startingDir,
12  const PropagationDirection propDir,
13  double tolerance)
14  : theX0(startingPos), theP0(startingDir.unit()), thePropDir(propDir), theTolerance(tolerance) {}
15 
16 std::pair<bool, double> StraightLineCylinderCrossing::pathLength(const Cylinder& cylinder) const {
17  //
18  // radius of cylinder and transversal position relative to axis
19  //
20  double R(cylinder.radius());
21  PositionType2D xt2d(theX0.x(), theX0.y());
22  //
23  // transverse direction
24  //
25  DirectionType2D pt2d(theP0.x(), theP0.y());
26  //
27  // solution of quadratic equation for s - assume |theP0|=1
28  //
29  RealQuadEquation eq(pt2d.mag2(), 2. * xt2d.dot(pt2d), xt2d.mag2() - R * R);
30  if (!eq.hasSolution) {
31  /*
32  double A= pt2d.mag2();
33  double B= 2.*xt2d.dot(pt2d);
34  double C= xt2d.mag2()-R*R;
35  cout << "A= " << pt2d.mag2()
36  << " B= " << 2.*xt2d.dot(pt2d)
37  << " C= " << xt2d.mag2()-R*R
38  << " D= " << B*B - 4*A*C
39  << endl;
40  */
41  return std::pair<bool, double>(false, 0.);
42  }
43  //
44  // choice of solution and verification of direction
45  //
46  return chooseSolution(eq.first, eq.second);
47 }
48 
49 std::pair<bool, double> StraightLineCylinderCrossing::chooseSolution(const double s1, const double s2) const {
50  //
51  // follows the logic implemented in HelixBarrelCylinderCrossing
52  //
53  if (thePropDir == anyDirection) {
54  return std::pair<bool, double>(true, (fabs(s1) < fabs(s2) ? s1 : s2));
55  } else {
56  int propSign = thePropDir == alongMomentum ? 1 : -1;
57  if (s1 * s2 < 0) {
58  // if different signs return the positive one
59  return std::pair<bool, double>(true, ((s1 * propSign > 0) ? s1 : s2));
60  } else if (s1 * propSign > 0) {
61  // if both positive, return the shortest
62  return std::pair<bool, double>(true, (fabs(s1) < fabs(s2) ? s1 : s2));
63  } else {
64  // if both negative, check if the smaller (abs value) is smaller then tolerance
65  double shorter = std::min(fabs(s1), fabs(s2));
66  if (shorter < theTolerance)
67  return std::pair<bool, double>(true, 0);
68  else
69  return std::pair<bool, double>(false, 0.);
70  }
71  }
72 }
const PropagationDirection thePropDir
const double tolerance
std::pair< bool, double > chooseSolution(const double s1, const double s2) const
Chooses the right solution w.r.t. the propagation direction.
PropagationDirection
std::pair< bool, double > pathLength(const Cylinder &cyl) const
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
Basic3DVector unit() const
StraightLineCylinderCrossing(const LocalPoint &startingPos, const LocalVector &startingDir, const PropagationDirection propDir=alongMomentum, double tolerance=0)
Scalar radius() const
Radius of the cylinder.
Definition: Cylinder.h:64