CbmRoot
Loading...
Searching...
No Matches
KfFieldRegion.cxx
Go to the documentation of this file.
1/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
10
11#include "KfFieldRegion.h"
12
13#include "KfUtils.h"
14
15#include <mutex>
16#include <sstream>
17
18#include <fmt/format.h>
19
20namespace cbm::algo::kf
21{
26
27
28 //* Implementation for EFieldMode::Interpolated
29
30 // -------------------------------------------------------------------------------------------------------------------
31 //
32 template<typename T>
34 const FieldValue<T>& b2)
35 {
36 if (b0.IsZero() && b1.IsZero() && b2.IsZero()) {
37 fFieldType = EFieldType::Null;
38 }
39 else {
40 fFieldType = EFieldType::Normal;
41 }
42
43 fZfirst = b0.GetZ();
44
45 auto dz1 = b1.GetZ() - b0.GetZ();
46 auto dz2 = b2.GetZ() - b0.GetZ();
47 auto det = utils::simd::One<T>() / (dz1 * dz2 * (b2.GetZ() - b1.GetZ()));
48 auto w21 = -dz2 * det;
49 auto w22 = dz1 * det;
50 auto w11 = -dz2 * w21;
51 auto w12 = -dz1 * w22;
52
53 for (int iD = 0; iD < 3; ++iD) {
54 auto db1 = b1.GetComponent(iD) - b0.GetComponent(iD);
55 auto db2 = b2.GetComponent(iD) - b0.GetComponent(iD);
56 auto& coeff = fCoeff[iD];
57 coeff[0] = b0.GetComponent(iD);
58 coeff[1] = db1 * w11 + db2 * w12;
59 coeff[2] = db1 * w21 + db2 * w22;
60 }
61 }
62
63 // -------------------------------------------------------------------------------------------------------------------
64 //
65 template<typename T>
67 {
70 }
71 auto dz = z - this->fZfirst;
72 return FieldValue<T>{this->fCoeff[0][0] + dz * (this->fCoeff[0][1] + dz * this->fCoeff[0][2]),
73 this->fCoeff[1][0] + dz * (this->fCoeff[1][1] + dz * this->fCoeff[1][2]),
74 this->fCoeff[2][0] + dz * (this->fCoeff[2][1] + dz * this->fCoeff[2][2]), z};
75 }
76
77 // -------------------------------------------------------------------------------------------------------------------
78 //
79 template<typename T>
80 std::tuple<T, T, T> ConcreteFieldRegion<T, EFieldMode::Interpolated>::GetDoubleIntegrals(T, T, T z1, T, T, T z2) const
81 {
82 // double integral of the field along z
83
85 // TODO: implement the double integral for the original field
86 }
87 auto fld = *this;
88 fld.Shift(z1);
89 T dz = z2 - z1;
90 T dz2 = dz * dz;
91 T c0 = dz2 * T(1. / 2.);
92 T c1 = dz2 * dz * T(1. / 6.);
93 T c2 = dz2 * dz2 * T(1. / 12.);
94 return {c0 * fld.fCoeff[0][0] + c1 * fld.fCoeff[0][1] + c2 * fld.fCoeff[0][2],
95 c0 * fld.fCoeff[1][0] + c1 * fld.fCoeff[1][1] + c2 * fld.fCoeff[1][2],
96 c0 * fld.fCoeff[2][0] + c1 * fld.fCoeff[2][1] + c2 * fld.fCoeff[2][2]};
97 }
98
99 // -------------------------------------------------------------------------------------------------------------------
100 //
101 template<typename T>
103 {
104 auto dz = z - fZfirst;
105 for (int iD = 0; iD < 3; ++iD) {
106 auto& coeff = fCoeff[iD];
107 auto c2dz = coeff[2] * dz;
108 coeff[0] += (coeff[1] + c2dz) * dz;
109 coeff[1] += (2 * c2dz);
110 }
111 fZfirst = z;
112 }
113
114 // -------------------------------------------------------------------------------------------------------------------
115 //
116 template<typename T>
118 {
119 // Check SIMD data vectors for consistent initialization
120 for (int iD = 0; iD < 3; ++iD) {
121 for (int iC = 0; iC < 3; ++iC) {
122 utils::CheckSimdVectorEquality(fCoeff[iD][iC], fmt::format("FieldRegion::fCoeff[{}][{}]", iD, iC).c_str());
123 }
124 }
125 utils::CheckSimdVectorEquality(fZfirst, "FieldRegion::fZfirst");
126 }
127
128 // -------------------------------------------------------------------------------------------------------------------
129 //
130 template<typename T>
131 std::string ConcreteFieldRegion<T, EFieldMode::Interpolated>::ToString(int indentLevel, int) const
132 {
133 constexpr char IndentChar = '\t';
134 std::stringstream msg;
135 std::string indent(indentLevel, IndentChar);
136 using fmt::format;
137 auto Cnv = [&](const auto& val) { return utils::simd::Cast<T, Literal_t<T>>(val); }; // alias for the conversion fn
138 const auto& coeff = this->fCoeff;
139 msg << indent << format("Field Region (EFieldMode::Interpolated): dz = z - ({})", Cnv(this->fZfirst));
140 msg << '\n'
141 << indent << IndentChar
142 << format("Bx(dz) = {:>12} + {:>12} dz + {:>12} dz2", Cnv(coeff[0][0]), Cnv(coeff[0][1]), Cnv(coeff[0][2]));
143 msg << '\n'
144 << indent << IndentChar
145 << format("Bx(dz) = {:>12} + {:>12} dz + {:>12} dz2", Cnv(coeff[1][0]), Cnv(coeff[1][1]), Cnv(coeff[1][2]));
146 msg << '\n'
147 << indent << IndentChar
148 << format("Bx(dz) = {:>12} + {:>12} dz + {:>12} dz2", Cnv(coeff[2][0]), Cnv(coeff[2][1]), Cnv(coeff[2][2]));
150 msg << indent
151 << "\nWARNING: the GlobalField::ForceUseOfOriginalField() is enabled, so the magnetic field interpolation "
152 << indent
153 << "\nis replaced with the global magnetic function for debugging purposes. If you see this message and are "
154 << indent
155 << "\nnot sure, what is going on, please call GlobalField::ForceUseOfOriginalField(false) and re-run the "
156 "routine";
157 }
158 return msg.str();
159 }
160
161
162 //* Implementation for EFieldMode::Original
163
164 // -------------------------------------------------------------------------------------------------------------------
165 //
166 template<typename T>
168 {
169 // Check SIMD data vectors for consistent initialization
170 }
171
172 // -------------------------------------------------------------------------------------------------------------------
173 //
174 template<typename T>
175 std::string ConcreteFieldRegion<T, EFieldMode::Original>::ToString(int indentLevel, int) const
176 {
177 constexpr char IndentChar = '\t';
178 std::stringstream msg;
179 std::string indent(indentLevel, IndentChar);
180 msg << indent << "Field region (EFieldMode::Original): type=" << static_cast<int>(fFieldType);
181 return msg.str();
182 }
183
184
185 namespace detail
186 {
193 } // namespace detail
194
195 // -------------------------------------------------------------------------------------------------------------------
196 //
197 std::tuple<double, double, double> GlobalField::UndefField(double, double, double)
198 {
200 && "cbm::algo::kf::GlobalField: The original globa magnetic field is required by "
201 "kf::defs::dbg::ForceOriginalField = true. Please provide it with the "
202 "kf::GlobalField::SetGlobalField() function.");
204 }
205
206 template class FieldRegion<float>;
207 template class FieldRegion<double>;
208 template class FieldRegion<fvec>;
209} // namespace cbm::algo::kf
Magnetic flux density interpolation along the track vs. z-coordinate (header)
T GetComponent(int iD) const
Gets component by index.
T GetZ() const
Gets z-coordinate of the spatial point [cm].
bool IsZero() const
Checks, if the field value is zero (negligible)
Magnetic field region, corresponding to a hit triplet.
Magnetic flux density vector.
static EFieldType fgOriginalFieldType
Global field type.
static std::tuple< double, double, double > UndefField(double, double, double)
Undefined magnetic field function.
static FieldValue< T > GetFieldValue(const FieldFn_t &fieldFn, T x, T y, T z)
Creates a field value object in a spactial point of a generic floating-point type.
static FieldFn_t fgOriginalField
Global variable to store the field function (x,y,z)->(Bx,By,Bz)
static bool IsUsingOriginalFieldForced()
Checks if the original field function is used.
static bool fgForceUseOfOriginalField
Flag to force the use of the original field in all field classes.
Concrete representation of the FieldRegion class (primary template)
constexpr T2 Undef
Undefined values.
Definition KfDefs.h:218
DataOut Cast(const DataT &val)
Converts a value of type DataT to type DataOut.
Definition KfUtils.h:212
DataT One()
Returns a value of the data type set to one.
Definition KfUtils.h:316
void CheckSimdVectorEquality(fvec v, const char *name)
Checks, if a SIMD vector horizontally equal TODO: Find this method in the VC!
Definition KfUtils.cxx:29
EFieldType
Magnetic field type in different setup regions.
Definition KfDefs.h:126
@ Normal
Field near the tracker subsystem.
Definition KfDefs.h:127
@ Null
No magnetic field.
Definition KfDefs.h:128
std::function< std::tuple< double, double, double >(double, double, double)> FieldFn_t
Magnetic field function type Signature: tuple<Bx, By, Bz>(x, y, z);.
Definition KfDefs.h:169