CMS 3D CMS Logo

lst_math.h
Go to the documentation of this file.
1 #ifndef lst_math_h
2 #define lst_math_h
3 #include <tuple>
4 #include <vector>
5 #include <cmath>
6 
7 namespace lst_math {
8  inline float Phi_mpi_pi(float x) {
9  if (std::isnan(x)) {
10  std::cout << "MathUtil::Phi_mpi_pi() function called with NaN" << std::endl;
11  return x;
12  }
13 
14  while (x >= M_PI)
15  x -= 2. * M_PI;
16 
17  while (x < -M_PI)
18  x += 2. * M_PI;
19 
20  return x;
21  };
22 
23  inline float ATan2(float y, float x) {
24  if (x != 0)
25  return atan2(y, x);
26  if (y == 0)
27  return 0;
28  if (y > 0)
29  return M_PI / 2;
30  else
31  return -M_PI / 2;
32  };
33 
34  inline float ptEstimateFromRadius(float radius) { return 2.99792458e-3 * 3.8 * radius; };
35 
36  class Helix {
37  public:
38  std::vector<float> center_;
39  float radius_;
40  float phi_;
41  float lam_;
42  float charge_;
43 
44  Helix(std::vector<float> c, float r, float p, float l, float q) {
45  center_ = c;
46  radius_ = r;
47  phi_ = Phi_mpi_pi(p);
48  lam_ = l;
49  charge_ = q;
50  }
51 
52  Helix(float pt, float eta, float phi, float vx, float vy, float vz, float charge) {
53  // Radius based on pt
54  radius_ = pt / (2.99792458e-3 * 3.8);
55  phi_ = phi;
56  charge_ = charge;
57 
58  // reference point vector which for sim track is the vertex point
59  float ref_vec_x = vx;
60  float ref_vec_y = vy;
61  float ref_vec_z = vz;
62 
63  // The reference to center vector
64  float inward_radial_vec_x = charge_ * radius_ * sin(phi_);
65  float inward_radial_vec_y = charge_ * radius_ * -cos(phi_);
66  float inward_radial_vec_z = 0;
67 
68  // Center point
69  float center_vec_x = ref_vec_x + inward_radial_vec_x;
70  float center_vec_y = ref_vec_y + inward_radial_vec_y;
71  float center_vec_z = ref_vec_z + inward_radial_vec_z;
72  center_.push_back(center_vec_x);
73  center_.push_back(center_vec_y);
74  center_.push_back(center_vec_z);
75 
76  // Lambda
77  lam_ = copysign(M_PI / 2. - 2. * atan(exp(-abs(eta))), eta);
78  }
79 
80  const std::vector<float> center() { return center_; }
81  const float radius() { return radius_; }
82  const float phi() { return phi_; }
83  const float lam() { return lam_; }
84  const float charge() { return charge_; }
85 
86  std::tuple<float, float, float, float> get_helix_point(float t) {
87  float x = center()[0] - charge() * radius() * sin(phi() - (charge()) * t);
88  float y = center()[1] + charge() * radius() * cos(phi() - (charge()) * t);
89  float z = center()[2] + radius() * tan(lam()) * t;
90  float r = sqrt(x * x + y * y);
91  return std::make_tuple(x, y, z, r);
92  }
93 
94  float infer_t(const std::vector<float> point) {
95  // Solve for t based on z position
96  float t = (point[2] - center()[2]) / (radius() * tan(lam()));
97  return t;
98  }
99 
100  float compare_radius(const std::vector<float> point) {
101  float t = infer_t(point);
102  auto [x, y, z, r] = get_helix_point(t);
103  float point_r = sqrt(point[0] * point[0] + point[1] * point[1]);
104  return (point_r - r);
105  }
106 
107  float compare_xy(const std::vector<float> point) {
108  float t = infer_t(point);
109  auto [x, y, z, r] = get_helix_point(t);
110  float xy_dist = sqrt(pow(point[0] - x, 2) + pow(point[1] - y, 2));
111  return xy_dist;
112  }
113  };
114 
115  class Hit {
116  public:
117  float x_, y_, z_;
118  // Derived quantities
119  float r3_, rt_, phi_, eta_;
120  int idx_;
121 
122  // Default constructor
123  Hit() : x_(0), y_(0), z_(0), idx_(-1) { setDerivedQuantities(); }
124 
125  // Parameterized constructor
126  Hit(float x, float y, float z, int idx = -1) : x_(x), y_(y), z_(z), idx_(idx) { setDerivedQuantities(); }
127 
128  // Copy constructor
130 
131  // Getters for derived quantities
132  float phi() const { return phi_; }
133  float eta() const { return eta_; }
134  float rt() const { return rt_; }
135  float x() const { return x_; }
136  float y() const { return y_; }
137 
138  private:
140  // Setting r3
141  r3_ = sqrt(x_ * x_ + y_ * y_ + z_ * z_);
142 
143  // Setting rt
144  rt_ = sqrt(x_ * x_ + y_ * y_);
145 
146  // Setting phi
147  phi_ = Phi_mpi_pi(M_PI + ATan2(-y_, -x_));
148 
149  // Setting eta
150  eta_ = ((z_ > 0) - (z_ < 0)) * std::acosh(r3_ / rt_);
151  }
152  };
153 
154  inline Hit getCenterFromThreePoints(Hit& hitA, Hit& hitB, Hit& hitC) {
155  // C
156  //
157  //
158  //
159  // B d <-- find this point that makes the arc that goes throw a b c
160  //
161  //
162  // A
163 
164  // Steps:
165  // 1. Calculate mid-points of lines AB and BC
166  // 2. Find slopes of line AB and BC
167  // 3. construct a perpendicular line between AB and BC
168  // 4. set the two equations equal to each other and solve to find intersection
169 
170  float xA = hitA.x();
171  float yA = hitA.y();
172  float xB = hitB.x();
173  float yB = hitB.y();
174  float xC = hitC.x();
175  float yC = hitC.y();
176 
177  float x_mid_AB = (xA + xB) / 2.;
178  float y_mid_AB = (yA + yB) / 2.;
179 
180  //float x_mid_BC = (xB + xC) / 2.;
181  //float y_mid_BC = (yB + yC) / 2.;
182 
183  bool slope_AB_inf = (xB - xA) == 0;
184  bool slope_BC_inf = (xC - xB) == 0;
185 
186  bool slope_AB_zero = (yB - yA) == 0;
187  bool slope_BC_zero = (yC - yB) == 0;
188 
189  float slope_AB = slope_AB_inf ? 0 : (yB - yA) / (xB - xA);
190  float slope_BC = slope_BC_inf ? 0 : (yC - yB) / (xC - xB);
191 
192  float slope_perp_AB = slope_AB_inf or slope_AB_zero ? 0. : -1. / (slope_AB);
193  //float slope_perp_BC = slope_BC_inf or slope_BC_zero ? 0. : -1. / (slope_BC);
194 
195  if ((slope_AB - slope_BC) == 0) {
196  std::cout << " slope_AB_zero: " << slope_AB_zero << std::endl;
197  std::cout << " slope_BC_zero: " << slope_BC_zero << std::endl;
198  std::cout << " slope_AB_inf: " << slope_AB_inf << std::endl;
199  std::cout << " slope_BC_inf: " << slope_BC_inf << std::endl;
200  std::cout << " slope_AB: " << slope_AB << std::endl;
201  std::cout << " slope_BC: " << slope_BC << std::endl;
202  std::cout << "MathUtil::getCenterFromThreePoints() function the three points are in straight line!" << std::endl;
203  return Hit();
204  }
205 
206  float x =
207  (slope_AB * slope_BC * (yA - yC) + slope_BC * (xA + xB) - slope_AB * (xB + xC)) / (2. * (slope_BC - slope_AB));
208  float y = slope_perp_AB * (x - x_mid_AB) + y_mid_AB;
209 
210  return Hit(x, y, 0);
211  };
212 } // namespace lst_math
213 #endif
def isnan(num)
float ATan2(float y, float x)
Definition: lst_math.h:23
const float charge()
Definition: lst_math.h:84
float phi_
Definition: lst_math.h:119
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
const std::vector< float > center()
Definition: lst_math.h:80
float radius_
Definition: lst_math.h:39
Helix(float pt, float eta, float phi, float vx, float vy, float vz, float charge)
Definition: lst_math.h:52
float eta_
Definition: lst_math.h:119
Hit(float x, float y, float z, int idx=-1)
Definition: lst_math.h:126
float rt() const
Definition: lst_math.h:134
float compare_xy(const std::vector< float > point)
Definition: lst_math.h:107
std::tuple< float, float, float, float > get_helix_point(float t)
Definition: lst_math.h:86
float Phi_mpi_pi(float x)
Definition: lst_math.h:8
float eta() const
Definition: lst_math.h:133
T sqrt(T t)
Definition: SSEVec.h:23
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float charge_
Definition: lst_math.h:42
float compare_radius(const std::vector< float > point)
Definition: lst_math.h:100
const float lam()
Definition: lst_math.h:83
float infer_t(const std::vector< float > point)
Definition: lst_math.h:94
std::vector< float > center_
Definition: lst_math.h:38
SeedingHitSet::ConstRecHitPointer Hit
#define M_PI
void setDerivedQuantities()
Definition: lst_math.h:139
const float radius()
Definition: lst_math.h:81
float phi() const
Definition: lst_math.h:132
float ptEstimateFromRadius(float radius)
Definition: lst_math.h:34
Helix(std::vector< float > c, float r, float p, float l, float q)
Definition: lst_math.h:44
const float phi()
Definition: lst_math.h:82
float x
Hit getCenterFromThreePoints(Hit &hitA, Hit &hitB, Hit &hitC)
Definition: lst_math.h:154
float y() const
Definition: lst_math.h:136
Hit(const Hit &hit)
Definition: lst_math.h:129
float x() const
Definition: lst_math.h:135
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
MPlex< T, D1, D2, N > atan2(const MPlex< T, D1, D2, N > &y, const MPlex< T, D1, D2, N > &x)
Definition: Matriplex.h:648