CbmRoot
Loading...
Searching...
No Matches
KfTrackParam.cxx
Go to the documentation of this file.
1/* Copyright (C) 2007-2020 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Igor Kulakov [committer], Maksym Zyzak */
4
5#include "KfTrackParam.h"
6
8
9#include <iomanip>
10#include <iostream>
11
12namespace cbm::algo::kf
13{
14 template<typename DataT>
15 std::string TrackParamBase<DataT>::ToString(int i) const
16 {
17 std::stringstream s;
18 s.setf(std::ios::scientific, std::ios::floatfield);
19
20 // print only one component of the SIMD vector
21 if constexpr (std::is_same_v<DataT, fvec>) {
22 if (i >= 0) {
23 s << "T = ";
24 s << " x " << GetX()[i];
25 s << " y " << GetY()[i];
26 s << " tx " << GetTx()[i];
27 s << " ty " << GetTy()[i];
28 s << " qp " << GetQp()[i];
29 s << " t " << GetTime()[i];
30 s << " vi " << GetVi()[i];
31
32 s << " z " << GetZ()[i] << std::endl;
33 s << "C = ";
34 s << " c00 " << C00()[i];
35 s << " c11 " << C11()[i];
36 s << " c22 " << C22()[i];
37 s << " c33 " << C33()[i];
38 s << " c44 " << C44()[i] << std::endl;
39 s << " c55 " << C55()[i] << std::endl;
40 s << " c66 " << C66()[i] << std::endl;
41 s << ToStringCorrelations(i);
42
43 s << " chi2 " << fChiSq[i];
44 s << " ndf " << fNdf[i] << std::endl;
45 return s.str();
46 }
47 }
48
49 { // print all SIMD values
50 s << "T = " << std::endl;
51 s << " x " << GetX() << std::endl;
52 s << " y " << GetY() << std::endl;
53 s << " tx " << GetTx() << std::endl;
54 s << " ty " << GetTy() << std::endl;
55 s << " qp " << GetQp() << std::endl;
56 s << " t " << GetTime() << std::endl;
57 s << " vi " << GetVi() << std::endl;
58 s << " z " << GetZ() << std::endl;
59 s << "C = " << std::endl;
60 s << " c00 " << C00() << std::endl;
61 s << " c11 " << C11() << std::endl;
62 s << " c22 " << C22() << std::endl;
63 s << " c33 " << C33() << std::endl;
64 s << " c44 " << C44() << std::endl;
65 s << " c55 " << C55() << std::endl;
66 s << " c66 " << C66() << std::endl;
67 s << ToStringCorrelations(i);
68
69 s << " chi2 " << fChiSq << std::endl;
70 s << " ndf " << fNdf << std::endl;
71 }
72 return s.str();
73 }
74
75 template<typename DataT>
77 {
78 std::stringstream s;
79 s << std::setprecision(6);
80
81 // print only one component of the SIMD vector
82 if constexpr (std::is_same_v<DataT, fvec>) {
83 if (i >= 0) {
84 float s0 = sqrt(C00()[i]);
85 float s1 = sqrt(C11()[i]);
86 float s2 = sqrt(C22()[i]);
87 float s3 = sqrt(C33()[i]);
88 float s4 = sqrt(C44()[i]);
89 float s5 = sqrt(C55()[i]);
90 float s6 = sqrt(C66()[i]);
91
92 s << "K = " << std::endl;
93 s << " " << C10()[i] / s1 / s0 << std::endl;
94 s << " " << C20()[i] / s2 / s0 << " " << C21()[i] / s2 / s1 << std::endl;
95 s << " " << C30()[i] / s3 / s0 << " " << C31()[i] / s3 / s1 << " " << C32()[i] / s3 / s2 << std::endl;
96 s << " " << C40()[i] / s4 / s0 << " " << C41()[i] / s4 / s1 << " " << C42()[i] / s4 / s2 << " "
97 << C43()[i] / s4 / s3 << std::endl;
98 s << " " << C50()[i] / s5 / s0 << " " << C51()[i] / s5 / s1 << " " << C52()[i] / s5 / s2 << " "
99 << C53()[i] / s5 / s3 << " " << C54()[i] / s5 / s4 << std::endl;
100 s << " " << C60()[i] / s6 / s0 << " " << C61()[i] / s6 / s1 << " " << C62()[i] / s6 / s2 << " "
101 << C63()[i] / s6 / s3 << " " << C64()[i] / s6 / s4 << " " << C65()[i] / s6 / s5 << std::endl;
102 return s.str();
103 }
104 }
105
106 // print all SIMD values
107 {
108 DataT s0 = sqrt(C00());
109 DataT s1 = sqrt(C11());
110 DataT s2 = sqrt(C22());
111 DataT s3 = sqrt(C33());
112 DataT s4 = sqrt(C44());
113 DataT s5 = sqrt(C55());
114 DataT s6 = sqrt(C66());
115
116 s << "K = " << std::endl;
117 s << " k10 " << C10() / s1 / s0 << std::endl;
118
119 s << "\n k20 " << C20() / s2 / s0 << std::endl;
120 s << " k21 " << C21() / s2 / s1 << std::endl;
121
122 s << "\n k30 " << C30() / s3 / s0 << std::endl;
123 s << " k31 " << C31() / s3 / s1 << std::endl;
124 s << " k32 " << C32() / s3 / s2 << std::endl;
125
126 s << "\n k40 " << C40() / s4 / s0 << std::endl;
127 s << " k41 " << C41() / s4 / s1 << std::endl;
128 s << " k42 " << C42() / s4 / s2 << std::endl;
129 s << " k43 " << C43() / s4 / s3 << std::endl;
130
131 s << "\n k50 " << C50() / s5 / s0 << std::endl;
132 s << " k51 " << C51() / s5 / s1 << std::endl;
133 s << " k52 " << C52() / s5 / s2 << std::endl;
134 s << " k53 " << C53() / s5 / s3 << std::endl;
135 s << " k54 " << C54() / s5 / s4 << std::endl;
136
137 s << "\n k60 " << C60() / s6 / s0 << std::endl;
138 s << " k61 " << C61() / s6 / s1 << std::endl;
139 s << " k62 " << C62() / s6 / s2 << std::endl;
140 s << " k63 " << C63() / s6 / s3 << std::endl;
141 s << " k64 " << C64() / s6 / s4 << std::endl;
142 s << " k65 " << C65() / s6 / s5 << std::endl;
143 }
144
145 return s.str();
146 }
147
148 template<typename DataT>
149 double GetEntry(DataT v, int)
150 {
151 return (double) v;
152 }
153
154 template<>
155 double GetEntry<fvec>(fvec v, int k)
156 {
157 return (double) v[k];
158 }
159
160 template<typename DataT>
161 bool TrackParamBase<DataT>::IsFinite(bool printWhenWrong) const
162 {
163 // verify that all the numbers in the object are valid floats
164 bool ret = true;
165
166 auto check = [&](const std::string& s, DataT val) {
167 if (!utils::IsFinite(val)) {
168 ret = false;
169 if (printWhenWrong) {
170 LOG(warning) << " TrackParam parameter " << s << " is undefined: " << val;
171 }
172 }
173 };
174
175 check("x", fX);
176 check("y", fY);
177 check("tx", fTx);
178 check("ty", fTy);
179 check("qp", fQp);
180 check("t", fT);
181 check("vi", fVi);
182 check("z", fZ);
183 check("chi2", fChiSq);
184 check("ndf", fNdf);
185 check("chi2time", fChiSqTime);
186 check("ndfTime", fNdfTime);
187
188 for (int i = 0; i < kNtrackParam; i++) {
189 for (int j = 0; j <= i; j++) {
190 DataT val = C(i, j);
191 if (!utils::IsFinite(val)) {
192 ret = false;
193 if (printWhenWrong) {
194 LOG(warning) << " TrackParam Cov matrix element (" << i << "," << j << ") is undefined: " << val;
195 }
196 }
197 }
198 }
199
200 return ret;
201 }
202
203 template<typename DataT>
204 bool TrackParamBase<DataT>::IsEntryConsistent(bool printWhenWrong, int k) const
205 {
206 // verify that all the numbers in the object are valid floats
207
208 bool ok = true;
209
210 auto check = [&](const std::string& s, DataT val) {
211 double dval = GetEntry(val, k);
212 if (!std::isfinite(dval)) {
213 ok = false;
214 if (printWhenWrong) {
215 LOG(warning) << " TrackParam parameter " << s << ", vector entry " << k << " is not finite: " << dval;
216 }
217 }
218 };
219
220 check("x", fX);
221 check("y", fY);
222 check("tx", fTx);
223 check("ty", fTy);
224 check("qp", fQp);
225 check("t", fT);
226 check("vi", fVi);
227 check("z", fZ);
228 check("chi2", fChiSq);
229 check("ndf", fNdf);
230 check("chi2time", fChiSqTime);
231 check("ndfTime", fNdfTime);
232
233 // verify diagonal elements.
234 // Cii is a squared dispersion of i-th parameter, it must be positive
235
236 for (int i = 0; i < 7; i++) {
237 double val = GetEntry(C(i, i), k);
238 if (val <= 0.) {
239 ok = false;
240 if (printWhenWrong) {
241 LOG(warning) << " TrackParam: C[" << i << "," << i << "], vec entry " << k << " is not positive: " << val
242 << std::endl;
243 }
244 }
245 }
246
247 // verify non-diagonal elements.
248 // Cij/sqrt(Cii*Cjj) is a correlation between i-th and j-th parameter,
249 // it must belong to [-1,1]
250
251 for (int i = 1; i < 7; i++) {
252 for (int j = 0; j < i; j++) {
253 double tolerance = 1.0;
254 double cij = GetEntry(C(i, j), k);
255 double cii = GetEntry(C(i, i), k);
256 double cjj = GetEntry(C(j, j), k);
257 if (cij * cij > tolerance * (cii * cjj)) {
258 ok = false;
259 if (printWhenWrong) {
260 LOG(warning) << " TrackParam: correlation [" << i << "," << j << "], vec entry " << k
261 << " is too large: " << cij / sqrt(cii * cjj) << std::endl;
262 }
263 }
264 }
265 }
266
267 // verify triplets of correlations
268 // Kxy * Kxy + Kxz * Kxz + Kyz * Kyz <= 1 + 2 * Kxy * Kxz * Kyz
269
270 for (int i = 2; i < 7; i++) {
271 for (int j = 1; j < i; j++) {
272 for (int m = 0; m < j; m++) {
273 double tolerance = 1.0;
274 double Cxx = GetEntry(C(i, i), k);
275 double Cyy = GetEntry(C(j, j), k);
276 double Czz = GetEntry(C(m, m), k);
277 double Cxy = GetEntry(C(i, j), k);
278 double Cxz = GetEntry(C(i, m), k);
279 double Cyz = GetEntry(C(j, m), k);
280 if (Cxx * Cyz * Cyz + Cyy * Cxz * Cxz + Czz * Cxy * Cxy
281 > tolerance * (Cxx * Cyy * Czz + 2. * Cxy * Cyz * Cxz)) {
282 ok = false;
283 if (printWhenWrong) {
284 double Kxy = Cxy / sqrt(Cxx * Cyy);
285 double Kxz = Cxz / sqrt(Cxx * Czz);
286 double Kyz = Cyz / sqrt(Cyy * Czz);
287 LOG(warning) << " TrackParam: correlations between parametetrs " << i << ", " << j << ", " << m
288 << ", vec entry " << k << " are wrong: " << Kxy << " " << Kxz << " " << Kyz << std::endl
289 << " inequation: " << Kxy * Kxy + Kxz * Kxz + Kyz * Kyz << " > " << 1 + 2 * Kxy * Kxz * Kyz
290 << std::endl;
291 }
292 }
293 }
294 }
295 }
296
297 if (!ok && printWhenWrong) {
298 LOG(warning) << "TrackParam parameters are not consistent: " << std::endl;
299 LOG(warning) << ToString(k);
300 }
301 return ok;
302 }
303
304 template<typename DataT>
305 bool TrackParamBase<DataT>::IsConsistent(bool printWhenWrong, int nFilled) const
306 {
307 int size = 1;
308
309 if constexpr (std::is_same_v<DataT, fvec>) {
310 size = fvec::size();
311 }
312
313 assert(nFilled <= size);
314 if (nFilled < 0) {
315 nFilled = size;
316 }
317
318 bool ok = true;
319 for (int i = 0; i < nFilled; ++i) {
320 ok = ok && IsEntryConsistent(printWhenWrong, i);
321 }
322
323 if (!ok && printWhenWrong) {
324 LOG(warning) << "TrackParam parameters are not consistent: " << std::endl;
325 if (nFilled == size) {
326 LOG(warning) << " All vector elements are filled " << std::endl;
327 }
328 else {
329 LOG(warning) << " Only first " << nFilled << " vector elements are filled " << std::endl;
330 }
331 LOG(warning) << ToString(-1);
332 }
333 return ok;
334 }
335
336 template class TrackParamBase<fvec>;
337 template class TrackParamBase<float>;
338 template class TrackParamBase<double>;
339
340} // namespace cbm::algo::kf
friend fvec sqrt(const fvec &a)
static constexpr size_t size()
Definition KfSimdPseudo.h:2
fscal v[fmask::Size]
Definition KfSimdPseudo.h:4
It is a technical base class of kf::TrackParam.
bool IsConsistent(bool printWhenWrong, int nFilled) const
Checks whether first nFilled SIMD entries are consistent.
std::string ToStringCorrelations(int i=-1) const
Prints correlations to a string.
bool IsFinite(bool printWhenWrong) const
Checks whether some parameters are finite.
bool IsEntryConsistent(bool printWhenWrong, int i) const
Checks whether SIMD entry i is consistent.
std::string ToString(int i=-1) const
Prints parameters to a string.
bool IsFinite(const T &val)
Checks whether a variable of a particular type is finite.
Definition KfUtils.h:100
double GetEntry< fvec >(fvec v, int k)
double GetEntry(DataT v, int)
std::string_view ToString(T t)
Definition EnumDict.h:64