CMS 3D CMS Logo

FitUtils.h
Go to the documentation of this file.
1 #ifndef RecoTracker_PixelTrackFitting_FitUtils_h
2 #define RecoTracker_PixelTrackFitting_FitUtils_h
3 
7 
8 namespace riemannFit {
9 
10  constexpr double epsilon = 1.e-4;
11 
14  template <int N>
15  using MatrixNd = Eigen::Matrix<double, N, N>;
16  template <int N>
17  using MatrixNplusONEd = Eigen::Matrix<double, N + 1, N + 1>;
18  template <int N>
19  using ArrayNd = Eigen::Array<double, N, N>;
20  template <int N>
21  using Matrix2Nd = Eigen::Matrix<double, 2 * N, 2 * N>;
22  template <int N>
23  using Matrix3Nd = Eigen::Matrix<double, 3 * N, 3 * N>;
24  template <int N>
25  using Matrix2xNd = Eigen::Matrix<double, 2, N>;
26  template <int N>
27  using Array2xNd = Eigen::Array<double, 2, N>;
28  template <int N>
29  using MatrixNx3d = Eigen::Matrix<double, N, 3>;
30  template <int N>
31  using MatrixNx5d = Eigen::Matrix<double, N, 5>;
32  template <int N>
33  using VectorNd = Eigen::Matrix<double, N, 1>;
34  template <int N>
35  using VectorNplusONEd = Eigen::Matrix<double, N + 1, 1>;
36  template <int N>
37  using Vector2Nd = Eigen::Matrix<double, 2 * N, 1>;
38  template <int N>
39  using Vector3Nd = Eigen::Matrix<double, 3 * N, 1>;
40  template <int N>
41  using RowVectorNd = Eigen::Matrix<double, 1, 1, N>;
42  template <int N>
43  using RowVector2Nd = Eigen::Matrix<double, 1, 2 * N>;
44 
45  using Matrix2x3d = Eigen::Matrix<double, 2, 3>;
46 
50  using Vector6f = Eigen::Matrix<double, 6, 1>;
51 
52  template <class C>
53  __host__ __device__ void printIt(C* m, const char* prefix = "") {
54 #ifdef RFIT_DEBUG
55  for (uint r = 0; r < m->rows(); ++r) {
56  for (uint c = 0; c < m->cols(); ++c) {
57  printf("%s Matrix(%d,%d) = %g\n", prefix, r, c, (*m)(r, c));
58  }
59  }
60 #endif
61  }
62 
66  template <typename T>
67  constexpr T sqr(const T a) {
68  return a * a;
69  }
70 
79  __host__ __device__ inline double cross2D(const Vector2d& a, const Vector2d& b) {
80  return a.x() * b.y() - a.y() * b.x();
81  }
82 
87  template <typename M6xNf, typename M2Nd>
88  __host__ __device__ void loadCovariance2D(M6xNf const& ge, M2Nd& hits_cov) {
89  // Index numerology:
90  // i: index of the hits/point (0,..,3)
91  // j: index of space component (x,y,z)
92  // l: index of space components (x,y,z)
93  // ge is always in sync with the index i and is formatted as:
94  // ge[] ==> [xx, xy, yy, xz, yz, zz]
95  // in (j,l) notation, we have:
96  // ge[] ==> [(0,0), (0,1), (1,1), (0,2), (1,2), (2,2)]
97  // so the index ge_idx corresponds to the matrix elements:
98  // | 0 1 3 |
99  // | 1 2 4 |
100  // | 3 4 5 |
101  constexpr uint32_t hits_in_fit = M6xNf::ColsAtCompileTime;
102  for (uint32_t i = 0; i < hits_in_fit; ++i) {
103  {
104  constexpr uint32_t ge_idx = 0, j = 0, l = 0;
105  hits_cov(i + j * hits_in_fit, i + l * hits_in_fit) = ge.col(i)[ge_idx];
106  }
107  {
108  constexpr uint32_t ge_idx = 2, j = 1, l = 1;
109  hits_cov(i + j * hits_in_fit, i + l * hits_in_fit) = ge.col(i)[ge_idx];
110  }
111  {
112  constexpr uint32_t ge_idx = 1, j = 1, l = 0;
113  hits_cov(i + l * hits_in_fit, i + j * hits_in_fit) = hits_cov(i + j * hits_in_fit, i + l * hits_in_fit) =
114  ge.col(i)[ge_idx];
115  }
116  }
117  }
118 
119  template <typename M6xNf, typename M3xNd>
120  __host__ __device__ void loadCovariance(M6xNf const& ge, M3xNd& hits_cov) {
121  // Index numerology:
122  // i: index of the hits/point (0,..,3)
123  // j: index of space component (x,y,z)
124  // l: index of space components (x,y,z)
125  // ge is always in sync with the index i and is formatted as:
126  // ge[] ==> [xx, xy, yy, xz, yz, zz]
127  // in (j,l) notation, we have:
128  // ge[] ==> [(0,0), (0,1), (1,1), (0,2), (1,2), (2,2)]
129  // so the index ge_idx corresponds to the matrix elements:
130  // | 0 1 3 |
131  // | 1 2 4 |
132  // | 3 4 5 |
133  constexpr uint32_t hits_in_fit = M6xNf::ColsAtCompileTime;
134  for (uint32_t i = 0; i < hits_in_fit; ++i) {
135  {
136  constexpr uint32_t ge_idx = 0, j = 0, l = 0;
137  hits_cov(i + j * hits_in_fit, i + l * hits_in_fit) = ge.col(i)[ge_idx];
138  }
139  {
140  constexpr uint32_t ge_idx = 2, j = 1, l = 1;
141  hits_cov(i + j * hits_in_fit, i + l * hits_in_fit) = ge.col(i)[ge_idx];
142  }
143  {
144  constexpr uint32_t ge_idx = 5, j = 2, l = 2;
145  hits_cov(i + j * hits_in_fit, i + l * hits_in_fit) = ge.col(i)[ge_idx];
146  }
147  {
148  constexpr uint32_t ge_idx = 1, j = 1, l = 0;
149  hits_cov(i + l * hits_in_fit, i + j * hits_in_fit) = hits_cov(i + j * hits_in_fit, i + l * hits_in_fit) =
150  ge.col(i)[ge_idx];
151  }
152  {
153  constexpr uint32_t ge_idx = 3, j = 2, l = 0;
154  hits_cov(i + l * hits_in_fit, i + j * hits_in_fit) = hits_cov(i + j * hits_in_fit, i + l * hits_in_fit) =
155  ge.col(i)[ge_idx];
156  }
157  {
158  constexpr uint32_t ge_idx = 4, j = 2, l = 1;
159  hits_cov(i + l * hits_in_fit, i + j * hits_in_fit) = hits_cov(i + j * hits_in_fit, i + l * hits_in_fit) =
160  ge.col(i)[ge_idx];
161  }
162  }
163  }
164 
173  __host__ __device__ inline void par_uvrtopak(CircleFit& circle, const double B, const bool error) {
174  Vector3d par_pak;
175  const double temp0 = circle.par.head(2).squaredNorm();
176  const double temp1 = sqrt(temp0);
177  par_pak << atan2(circle.qCharge * circle.par(0), -circle.qCharge * circle.par(1)),
178  circle.qCharge * (temp1 - circle.par(2)), circle.par(2) * B;
179  if (error) {
180  const double temp2 = sqr(circle.par(0)) * 1. / temp0;
181  const double temp3 = 1. / temp1 * circle.qCharge;
182  Matrix3d j4Mat;
183  j4Mat << -circle.par(1) * temp2 * 1. / sqr(circle.par(0)), temp2 * 1. / circle.par(0), 0., circle.par(0) * temp3,
184  circle.par(1) * temp3, -circle.qCharge, 0., 0., B;
185  circle.cov = j4Mat * circle.cov * j4Mat.transpose();
186  }
187  circle.par = par_pak;
188  }
189 
197  Vector3d par_pak;
198  const double temp0 = circle.par.head(2).squaredNorm();
199  const double temp1 = sqrt(temp0);
200  par_pak << atan2(circle.qCharge * circle.par(0), -circle.qCharge * circle.par(1)),
201  circle.qCharge * (temp1 - circle.par(2)), circle.qCharge / circle.par(2);
202 
203  const double temp2 = sqr(circle.par(0)) * 1. / temp0;
204  const double temp3 = 1. / temp1 * circle.qCharge;
205  Matrix3d j4Mat;
206  j4Mat << -circle.par(1) * temp2 * 1. / sqr(circle.par(0)), temp2 * 1. / circle.par(0), 0., circle.par(0) * temp3,
207  circle.par(1) * temp3, -circle.qCharge, 0., 0., -circle.qCharge / (circle.par(2) * circle.par(2));
208  circle.cov = j4Mat * circle.cov * j4Mat.transpose();
209 
210  circle.par = par_pak;
211  }
212 
213  // transformation between the "perigee" to cmssw localcoord frame
214  // the plane of the latter is the perigee plane...
215  // from //!<(phi,Tip,q/pt,cotan(theta)),Zip)
216  // to q/p,dx/dz,dy/dz,x,z
217  template <typename VI5, typename MI5, typename VO5, typename MO5>
218  __host__ __device__ inline void transformToPerigeePlane(VI5 const& ip, MI5 const& icov, VO5& op, MO5& ocov) {
219  auto sinTheta2 = 1. / (1. + ip(3) * ip(3));
220  auto sinTheta = std::sqrt(sinTheta2);
221  auto cosTheta = ip(3) * sinTheta;
222 
223  op(0) = sinTheta * ip(2);
224  op(1) = 0.;
225  op(2) = -ip(3);
226  op(3) = ip(1);
227  op(4) = -ip(4);
228 
229  Matrix5d jMat = Matrix5d::Zero();
230 
231  jMat(0, 2) = sinTheta;
232  jMat(0, 3) = -sinTheta2 * cosTheta * ip(2);
233  jMat(1, 0) = 1.;
234  jMat(2, 3) = -1.;
235  jMat(3, 1) = 1.;
236  jMat(4, 4) = -1;
237 
238  ocov = jMat * icov * jMat.transpose();
239  }
240 
241 } // namespace riemannFit
242 
243 #endif // RecoTracker_PixelTrackFitting_FitUtils_h
Eigen::Matrix< double, 2, N > Matrix2xNd
Definition: FitUtils.h:25
Eigen::Matrix< double, 3 *N, 1 > Vector3Nd
Definition: FitUtils.h:39
int32_t qCharge
particle charge
Definition: FitResult.h:34
Eigen::Matrix< double, 1, 2 *N > RowVector2Nd
Definition: FitUtils.h:43
Definition: APVGainStruct.h:7
Eigen::Vector3f Vector3f
Definition: FitUtils.h:48
Eigen::Matrix3f Matrix3f
Definition: FitUtils.h:47
Eigen::Matrix< double, 2 *N, 1 > Vector2Nd
Definition: FitUtils.h:37
#define __host__
Eigen::Matrix< double, 3 *N, 3 *N > Matrix3Nd
Definition: FitUtils.h:23
Eigen::Array< double, N, N > ArrayNd
Definition: FitUtils.h:19
Eigen::Matrix< double, N+1, 1 > VectorNplusONEd
Definition: FitUtils.h:35
Vector3d par
parameter: (X0,Y0,R)
Definition: FitResult.h:27
__host__ __device__ double cross2D(const Vector2d &a, const Vector2d &b)
Compute cross product of two 2D vector (assuming z component 0), returning z component of the result...
Definition: FitUtils.h:79
Eigen::Vector2d Vector2d
Definition: FitResult.h:13
__host__ __device__ void par_uvrtopak(CircleFit &circle, const double B, const bool error)
Transform circle parameter from (X0,Y0,R) to (phi,Tip,p_t) and consequently covariance matrix...
Definition: FitUtils.h:173
Eigen::Matrix< double, 2, 3 > Matrix2x3d
Definition: FitUtils.h:45
Eigen::Matrix< double, N, 1 > VectorNd
Definition: FitUtils.h:33
__host__ __device__ void transformToPerigeePlane(VI5 const &ip, MI5 const &icov, VO5 &op, MO5 &ocov)
Definition: FitUtils.h:218
T sqrt(T t)
Definition: SSEVec.h:19
Eigen::VectorXd VectorXd
Definition: FitUtils.h:12
Eigen::Matrix< double, N, N > MatrixNd
Definition: FitUtils.h:15
constexpr T sqr(const T a)
raise to square.
Definition: FitUtils.h:67
constexpr double epsilon
used in numerical derivative (J2 in Circle_fit())
Definition: FitUtils.h:10
Eigen::Vector4f Vector4f
Definition: FitUtils.h:49
__host__ __device__ void loadCovariance(M6xNf const &ge, M3xNd &hits_cov)
Definition: FitUtils.h:120
Eigen::Matrix< double, 6, 1 > Vector6f
Definition: FitUtils.h:50
Eigen::Matrix< double, N, 5 > MatrixNx5d
Definition: FitUtils.h:31
Eigen::Matrix< double, 2 *N, 2 *N > Matrix2Nd
Definition: FitUtils.h:21
Eigen::Array< double, 2, N > Array2xNd
Definition: FitUtils.h:27
Eigen::MatrixXd MatrixXd
Definition: FitUtils.h:13
double b
Definition: hdecay.h:120
Eigen::Matrix< double, N, 3 > MatrixNx3d
Definition: FitUtils.h:29
Eigen::Matrix< double, N+1, N+1 > MatrixNplusONEd
Definition: FitUtils.h:17
Eigen::Matrix< double, 5, 5 > Matrix5d
Definition: FitResult.h:20
__host__ __device__ void fromCircleToPerigee(CircleFit &circle)
Transform circle parameter from (X0,Y0,R) to (phi,Tip,q/R) and consequently covariance matrix...
Definition: FitUtils.h:196
double a
Definition: hdecay.h:121
__host__ __device__ void loadCovariance2D(M6xNf const &ge, M2Nd &hits_cov)
Definition: FitUtils.h:88
Eigen::Matrix< double, 1, 1, N > RowVectorNd
Definition: FitUtils.h:41
Eigen::Matrix3d Matrix3d
Definition: FitResult.h:18
long double T
#define __device__
Eigen::Vector3d Vector3d
Definition: FitResult.h:14
__host__ __device__ void printIt(C *m, const char *prefix="")
Definition: FitUtils.h:53