CMS 3D CMS Logo

TwoTrackMinimumDistanceHelixLine.cc
Go to the documentation of this file.
5 #include <iostream>
6 #include <iomanip>
7 
8 using namespace std;
9 
11  bool isFirstALine = firstGTP->charge() == 0. || firstGTP->magneticField().inTesla(firstGTP->position()).z() == 0.;
12  bool isSecondALine = secondGTP->charge() == 0. || secondGTP->magneticField().inTesla(secondGTP->position()).z() == 0.;
13  if (isFirstALine && !isSecondALine) {
14  theL = firstGTP;
15  theH = secondGTP;
16  } else if (!isFirstALine && isSecondALine) {
17  theH = firstGTP;
18  theL = secondGTP;
19  } else {
20  edm::LogWarning("TwoTrackMinimumDistanceHelixLine")
21  << "Error in track charge: "
22  << "One of the tracks has to be charged, and the other not." << endl
23  << "Track Charges: " << firstGTP->charge() << " and " << secondGTP->charge();
24  return true;
25  }
26 
27  Hn = theH->momentum().mag();
28  Ln = theL->momentum().mag();
29 
30  if (Hn == 0. || Ln == 0.) {
31  edm::LogWarning("TwoTrackMinimumDistanceHelixLine") << "Momentum of input trajectory is zero.";
32  return true;
33  };
34 
35  GlobalPoint lOrig = theL->position();
36  GlobalPoint hOrig = theH->position();
37  posDiff = GlobalVector((lOrig - hOrig).basicVector());
38  X = posDiff.x();
39  Y = posDiff.y();
40  Z = posDiff.z();
41  theLp = theL->momentum();
42  px = theLp.x();
43  px2 = px * px;
44  py = theLp.y();
45  py2 = py * py;
46  pz = theLp.z();
47  pz2 = pz * pz;
48 
49  const double Bc2kH = theH->magneticField().inTesla(hOrig).z() * 2.99792458e-3;
50  // MagneticField::inInverseGeV ( hOrig ).z();
51 
52  if (Bc2kH == 0.) {
53  edm::LogWarning("TwoTrackMinimumDistanceHelixLine") << "Magnetic field at point " << hOrig << " is zero.";
54  return true;
55  };
56 
57  theh = -Hn / (theH->charge() * Bc2kH) * sqrt(1 - (((theH->momentum().z() * theH->momentum().z()) / (Hn * Hn))));
58 
59  thetanlambdaH = -theH->momentum().z() / (theH->charge() * Bc2kH * theh);
60 
61  thePhiH0 = theH->momentum().phi();
62  thesinPhiH0 = sin(thePhiH0);
63  thecosPhiH0 = cos(thePhiH0);
64 
65  aa = (X + theh * thesinPhiH0) * (py2 + pz2) - px * (py * Y + pz * Z);
66  bb = (Y - theh * thecosPhiH0) * (px2 + pz2) - py * (px * X + pz * Z);
67  cc = pz * theh * thetanlambdaH;
68  dd = theh * px * py;
69  ee = theh * (px2 - py2);
70  ff = (px2 + py2) * theh * thetanlambdaH * thetanlambdaH;
71 
72  baseFct = thetanlambdaH * (Z * (px2 + py2) - pz * (px * X + py * Y));
73  baseDer = -ff;
74  return false;
75 }
76 
77 bool TwoTrackMinimumDistanceHelixLine::oneIteration(double& thePhiH, double& fct, double& derivative) const {
78  double thesinPhiH = sin(thePhiH);
79  double thecosPhiH = cos(thePhiH);
80 
81  // Fonction of which the root is to be found:
82 
83  fct = baseFct;
84  fct -= ff * (thePhiH - thePhiH0);
85  fct += thecosPhiH * aa;
86  fct += thesinPhiH * bb;
87  fct += cc * (thePhiH - thePhiH0) * (px * thecosPhiH + py * thesinPhiH);
88  fct += cc * (px * (thesinPhiH - thesinPhiH0) - py * (thecosPhiH - thecosPhiH0));
89  fct += dd * (thesinPhiH * (thesinPhiH - thesinPhiH0) - thecosPhiH * (thecosPhiH - thecosPhiH0));
90  fct += ee * thecosPhiH * thesinPhiH;
91 
92  // Its derivative:
93 
94  derivative = baseDer;
95  derivative += -thesinPhiH * aa;
96  derivative += thecosPhiH * bb;
97  derivative += cc * (thePhiH - thePhiH0) * (py * thecosPhiH - px * thesinPhiH);
98  derivative += 2 * cc * (px * thecosPhiH + py * thesinPhiH);
99  derivative += dd * (4 * thecosPhiH * thesinPhiH - thecosPhiH * thesinPhiH0 - thesinPhiH * thecosPhiH0);
100  derivative += ee * (thecosPhiH * thecosPhiH - thesinPhiH * thesinPhiH);
101 
102  return false;
103 }
104 
106  const GlobalTrajectoryParameters& theSecondGTP,
107  const float qual) {
108  pointsUpdated = false;
109  firstGTP = &theFirstGTP;
110  secondGTP = &theSecondGTP;
111 
112  if (updateCoeffs()) {
113  finalPoints();
114  return true;
115  };
116 
117  double fctVal, derVal, dPhiH;
118  thePhiH = thePhiH0;
119 
120  double x1 = thePhiH0 - M_PI, x2 = thePhiH0 + M_PI;
121  for (int j = 1; j <= themaxiter; ++j) {
122  oneIteration(thePhiH, fctVal, derVal);
123  dPhiH = fctVal / derVal;
124  thePhiH -= dPhiH;
125  if ((x1 - thePhiH) * (thePhiH - x2) < 0.0) {
126  LogDebug("TwoTrackMinimumDistanceHelixLine") << "Jumped out of brackets in root finding. Will be moved closer.";
127  thePhiH += (dPhiH * 0.8);
128  }
129  if (fabs(dPhiH) < qual) {
130  finalPoints();
131  return false;
132  }
133  }
134  LogDebug("TwoTrackMinimumDistanceHelixLine") << "Number of steps exceeded. Has not converged.";
135  finalPoints();
136  return true;
137 }
138 
140  if (firstGTP == theL)
141  return theL->momentum().phi();
142  else
143  return thePhiH;
144 }
145 
147  if (secondGTP == theL)
148  return theL->momentum().phi();
149  else
150  return thePhiH;
151 }
152 
153 pair<GlobalPoint, GlobalPoint> TwoTrackMinimumDistanceHelixLine::points() const {
154  if (firstGTP == theL)
155  return pair<GlobalPoint, GlobalPoint>(linePoint, helixPoint);
156  else
157  return pair<GlobalPoint, GlobalPoint>(helixPoint, linePoint);
158 }
159 
160 pair<double, double> TwoTrackMinimumDistanceHelixLine::pathLength() const {
161  if (firstGTP == theL)
162  return pair<double, double>(linePath, helixPath);
163  else
164  return pair<double, double>(helixPath, linePath);
165 }
166 
168  if (pointsUpdated)
169  return;
170  helixPoint = GlobalPoint(theH->position().x() + theh * (sin(thePhiH) - thesinPhiH0),
171  theH->position().y() + theh * (-cos(thePhiH) + thecosPhiH0),
172  theH->position().z() + theh * (thetanlambdaH * (thePhiH - thePhiH0)));
173  helixPath = (thePhiH - thePhiH0) * (theh * sqrt(1 + thetanlambdaH * thetanlambdaH));
174 
175  GlobalVector diff((theL->position() - helixPoint).basicVector());
176  tL = (-diff.dot(theLp)) / (Ln * Ln);
177  linePoint =
178  GlobalPoint(theL->position().x() + tL * px, theL->position().y() + tL * py, theL->position().z() + tL * pz);
179  linePath = tL * theLp.mag();
180  pointsUpdated = true;
181 }
Derivative< X, A >::type derivative(const A &_)
Definition: Derivative.h:18
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
#define X(str)
Definition: MuonsGrabber.cc:38
string dd
Definition: createTree.py:154
bool oneIteration(double &thePhiH, double &fct, double &derivative) const
T sqrt(T t)
Definition: SSEVec.h:19
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
T mag() const
Definition: PV3DBase.h:64
std::pair< double, double > pathLength() const
#define M_PI
Log< level::Warning, false > LogWarning
std::pair< GlobalPoint, GlobalPoint > points() const
bool calculate(const GlobalTrajectoryParameters &, const GlobalTrajectoryParameters &, const float qual=.0001)
Global3DVector GlobalVector
Definition: GlobalVector.h:10
#define LogDebug(id)