CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HelixBarrelPlaneCrossingByCircle.cc
Go to the documentation of this file.
6 
7 #include <algorithm>
8 #include <cfloat>
9 
12  const DirectionType& dir,
13  double rho, PropagationDirection propDir) :
14  theStartingPos( pos), theStartingDir(dir),
15  theRho(rho), thePropDir(propDir) { init();}
16 
19  const GlobalVector& dir,
20  double rho, PropagationDirection propDir) :
21  theStartingPos( pos.basicVector()), theStartingDir(dir.basicVector()),
22  theRho(rho), thePropDir(propDir) { init();}
23 
25 {
26  double pabsI = 1./theStartingDir.mag();
27  double pt = theStartingDir.perp();
28  theCosTheta = theStartingDir.z()*pabsI;
29  theSinTheta = pt*pabsI;
30 
31  // protect for zero curvature case
32  const double sraightLineCutoff = 1.e-7;
33  if (fabs(theRho) < sraightLineCutoff &&
34  fabs(theRho)*theStartingPos.perp() < sraightLineCutoff) {
35  useStraightLine = true;
36  }else{
37  // circle parameters
38  // position of center of curvature is on a line perpendicular
39  // to startingDir and at a distance 1/curvature.
40  double o = 1./(pt*theRho);
43  useStraightLine = false;
44  }
45 }
46 
47 std::pair<bool,double>
49 {
50  typedef std::pair<bool,double> ResultType;
51 
52  if(useStraightLine){
53  // switch to straight line case
56  return slc.pathLength( plane);
57  }
58 
59 
60  // plane parameters
61  GlobalVector n = plane.normalVector();
62  double distToPlane = -plane.localZ( GlobalPoint( theStartingPos));
63  // double distToPlane = (plane.position().x()-theStartingPos.x()) * n.x() +
64  // (plane.position().y()-theStartingPos.y()) * n.y();
65  double nx = n.x(); // convert to double
66  double ny = n.y(); // convert to double
67  double distCx = theStartingPos.x() - theXCenter;
68  double distCy = theStartingPos.y() - theYCenter;
69 
70  double nfac, dfac;
71  double A, B, C;
72  bool solveForX;
73  if (fabs(nx) > fabs(ny)) {
74  solveForX = false;
75  nfac = ny/nx;
76  dfac = distToPlane/nx;
77  B = distCy - nfac*distCx; // only part of B, may have large cancelation
78  C = (2.*distCx + dfac)*dfac;
79  }
80  else {
81  solveForX = true;
82  nfac = nx/ny;
83  dfac = distToPlane/ny;
84  B = distCx - nfac*distCy; // only part of B, may have large cancelation
85  C = (2.*distCy + dfac)*dfac;
86  }
87  B -= nfac*dfac; B *= 2; // the rest of B (normally small)
88  A = 1.+ nfac*nfac;
89 
90  double dx1, dx2, dy1, dy2;
91  RealQuadEquation equation(A,B,C);
92  if (!equation.hasSolution) return ResultType( false, 0.);
93  else {
94  if (solveForX) {
95  dx1 = equation.first;
96  dx2 = equation.second;
97  dy1 = dfac - nfac*dx1;
98  dy2 = dfac - nfac*dx2;
99  }
100  else {
101  dy1 = equation.first;
102  dy2 = equation.second;
103  dx1 = dfac - nfac*dy1;
104  dx2 = dfac - nfac*dy2;
105  }
106  }
107  bool solved = chooseSolution( Vector2D(dx1, dy1), Vector2D(dx2, dy2));
108  if (solved) {
109  theDmag = theD.mag();
110  // protect asin
111  double sinAlpha = 0.5*theDmag*theRho;
112  if ( std::abs(sinAlpha)>1.) sinAlpha = std::copysign(1.,sinAlpha);
113  theS = theActualDir*2./(theRho*theSinTheta) * asin( sinAlpha);
114  return ResultType( true, theS);
115  }
116  else return ResultType( false, 0.);
117 }
118 
119 bool
121  const Vector2D& d2)
122 {
123  bool solved;
124  double momProj1 = theStartingDir.x()*d1.x() + theStartingDir.y()*d1.y();
125  double momProj2 = theStartingDir.x()*d2.x() + theStartingDir.y()*d2.y();
126 
127  if ( thePropDir == anyDirection ) {
128  solved = true;
129  if (d1.mag2()<d2.mag2()) {
130  theD = d1;
131  theActualDir = (momProj1 > 0) ? 1. : -1.;
132  }
133  else {
134  theD = d2;
135  theActualDir = (momProj2 > 0) ? 1. : -1.;
136  }
137 
138  }
139  else {
140  double propSign = thePropDir==alongMomentum ? 1 : -1;
141  if (!mathSSE::samesign(momProj1,momProj2)) {
142  // if different signs return the positive one
143  solved = true;
144  theD = mathSSE::samesign(momProj1,propSign) ? d1 : d2;
145  theActualDir = propSign;
146  }
147  else if (mathSSE::samesign(momProj1,propSign)) {
148  // if both positive, return the shortest
149  solved = true;
150  theD = (d1.mag2()<d2.mag2()) ? d1 : d2;
151  theActualDir = propSign;
152  }
153  else solved = false;
154  }
155  return solved;
156 }
157 
160 {
161  if(useStraightLine){
163  }else{
164  if ( s==theS) {
165  return PositionType( theStartingPos.x() + theD.x(),
166  theStartingPos.y() + theD.y(),
168  }
169  else {
170  double phi = s*theSinTheta*theRho;
171  double x1Shift = theStartingPos.x() - theXCenter;
172  double y1Shift = theStartingPos.y() - theYCenter;
173 
174  return PositionType(x1Shift*cos(phi)-y1Shift*sin(phi) + theXCenter,
175  x1Shift*sin(phi)+y1Shift*cos(phi) + theYCenter,
177  }
178  }
179 }
180 
183 {
184  if(useStraightLine){return theStartingDir;}
185  else{
186  double sinPhi, cosPhi;
187  if ( s==theS) {
188  double tmp = 0.5*theDmag*theRho;
189  if (s < 0) tmp = -tmp;
190  // protect sqrt
191  sinPhi = 1.-(tmp*tmp);
192  if ( sinPhi<0 ) sinPhi = 0.;
193  sinPhi = 2.*tmp*sqrt(sinPhi);
194  cosPhi = 1.-2.*(tmp*tmp);
195  }
196  else {
197  double phi = s*theSinTheta*theRho;
198  sinPhi = sin(phi);
199  cosPhi = cos(phi);
200  }
201  return DirectionType(theStartingDir.x()*cosPhi-theStartingDir.y()*sinPhi,
202  theStartingDir.x()*sinPhi+theStartingDir.y()*cosPhi,
203  theStartingDir.z());
204  }
205 }
virtual PositionType position(double s) const
T y() const
Cartesian y coordinate.
T x() const
Cartesian x coordinate.
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
double_binary B
Definition: DDStreamer.cc:234
GlobalVector normalVector() const
Definition: Plane.h:41
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Basic3DVector unit() const
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:63
float localZ(const GlobalPoint &gp) const
Definition: Plane.h:45
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
PropagationDirection
Definition: Plane.h:17
bool chooseSolution(const Vector2D &d1, const Vector2D &d2)
T z() const
Cartesian z coordinate.
T sqrt(T t)
Definition: SSEVec.h:18
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
T perp() const
Magnitude of transverse component.
Basic3DVector< float > PositionType
the helix is passed to the constructor and does not appear in the interface
Basic3DVector< float > DirectionType
T y() const
Cartesian y coordinate.
std::pair< bool, double > pathLength(const Plane &plane) const
virtual std::pair< bool, double > pathLength(const Plane &)
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
bool samesign(T rh, T lh)
dbl *** dir
Definition: mlp_gen.cc:35
HelixBarrelPlaneCrossingByCircle(const PositionType &pos, const DirectionType &dir, double rho, PropagationDirection propDir=alongMomentum)
T x() const
Definition: PV3DBase.h:62
virtual DirectionType direction(double s) const
T x() const
Cartesian x coordinate.