Belle II Software  release-06-02-00
HelixSwimmer.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 <top/reconstruction_cpp/HelixSwimmer.h>
10 #include <framework/gearbox/Const.h>
11 #include <framework/gearbox/Unit.h>
12 #include <framework/logging/Logger.h>
13 #include <limits>
14 
15 using namespace std;
16 
17 namespace Belle2 {
22  namespace TOP {
23 
24  void HelixSwimmer::set(const TVector3& position, const TVector3& momentum, double charge, double Bz)
25  {
26  double p = momentum.Mag();
27  double pT = momentum.Perp();
28  double b = -Bz * charge * Const::speedOfLight;
29  if (abs(b / Unit::T) > pT) { // helix for R < 100 m
30  m_R = pT / abs(b);
31  m_omega = b / p;
32  m_kz = momentum.Z() / p;
33  m_phi0 = momentum.Phi() - copysign(M_PI / 2, m_omega);
34  m_xc = position.X() - m_R * cos(m_phi0);
35  m_yc = position.Y() - m_R * sin(m_phi0);
36  m_z0 = position.Z();
37  m_T0 = 2 * M_PI / abs(m_omega);
38  } else { // straight line
39  m_omega = 0; // distinguisher between straight line and helix
40  x0 = position.X();
41  y0 = position.Y();
42  z0 = position.Z();
43  kx = momentum.X() / p;
44  ky = momentum.Y() / p;
45  kz = momentum.Z() / p;
46  }
47  }
48 
49  void HelixSwimmer::setTransformation(const TRotation& rotation, const TVector3& translation)
50  {
51  m_rotationInv = rotation.Inverse();
52  m_translation = translation;
53  }
54 
55  void HelixSwimmer::moveReferencePosition(double length)
56  {
57  if (m_omega != 0) {
58  m_phi0 += m_omega * length;
59  m_z0 += m_kz * length;
60  } else {
61  x0 += kx * length;
62  y0 += ky * length;
63  z0 += kz * length;
64  }
65  }
66 
67  TVector3 HelixSwimmer::getPosition(double length) const
68  {
69  if (m_omega != 0) {
70  double phi = m_phi0 + m_omega * length;
71  TVector3 vec(m_xc + m_R * cos(phi), m_yc + m_R * sin(phi), m_z0 + m_kz * length);
72  return m_rotationInv * (vec - m_translation);
73  } else {
74  TVector3 vec(x0 + kx * length, y0 + ky * length, z0 + kz * length);
75  return m_rotationInv * (vec - m_translation);
76  }
77  }
78 
79  TVector3 HelixSwimmer::getDirection(double length) const
80  {
81  if (m_omega != 0) {
82  double phi = m_phi0 + m_omega * length;
83  double k_T = m_omega * m_R;
84  TVector3 vec(-k_T * sin(phi), k_T * cos(phi), m_kz);
85  return m_rotationInv * vec;
86  } else {
87  TVector3 vec(kx, ky, kz);
88  return m_rotationInv * vec;
89  }
90  }
91 
92  double HelixSwimmer::getDistanceToPlane(const TVector3& point, const TVector3& normal) const
93  {
94  if (m_omega != 0) {
95  auto r = point - TVector3(m_xc, m_yc, m_z0);
96  double phi = normal.Phi();
97  double s = r * normal;
98  if (abs(s) > m_R) return std::numeric_limits<double>::quiet_NaN(); // no solution
99 
100  double t = shortestDistance(s / m_R, phi);
101  double v = m_kz * normal.Z();
102  if (v == 0) return t;
103 
104  double t_prev = t;
105  double dt_prev = 0;
106  for (int i = 0; i < 100; i++) {
107  double cosAlpha = (s - v * t) / m_R;
108  if (abs(cosAlpha) > 1) {
109  return std::numeric_limits<double>::quiet_NaN(); // no solution
110  }
111  t = shortestDistance(cosAlpha, phi);
112  double dt = t - t_prev;
113  if (dt == 0 or (dt + dt_prev) == 0) return t;
114  t_prev = t;
115  dt_prev = dt;
116  }
117  if (abs(dt_prev) < 1e-6) {
118  B2DEBUG(20, "TOP::HelixSwimmer::getDistanceToPlane: not converged"
119  << LogVar("v", v) << LogVar("dt", dt_prev));
120  return t;
121  } else {
122  B2DEBUG(20, "TOP::HelixSwimmer::getDistanceToPlane: not converged"
123  << LogVar("v", v) << LogVar("dt", dt_prev));
124  return std::numeric_limits<double>::quiet_NaN();
125  }
126  } else {
127  auto r = point - TVector3(x0, y0, z0);
128  auto v = TVector3(kx, ky, kz);
129  return (r * normal) / (v * normal);
130  }
131  }
132 
133  } //TOP
135 } //Belle2
136 
Class to store variables with their name which were sent to the logging service.
ExpRunEvt getPosition(const std::vector< Evt > &events, double tEdge)
Get the exp-run-evt number from the event time [hours].
Definition: Splitter.h:341
Abstract base class for different kinds of events.