CbmRoot
Loading...
Searching...
No Matches
KfSetupBuilder.h
Go to the documentation of this file.
1/* Copyright (C) 2024-2025 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#pragma once // include this header only once per compilation unit
11
13#include "KfSetup.h"
14
15#include <boost/archive/binary_iarchive.hpp>
16#include <boost/archive/binary_oarchive.hpp>
17
18#include <fstream>
19#include <memory>
20#include <optional>
21#include <sstream>
22#include <string>
23#include <vector>
24
25#include <fmt/format.h>
26
27namespace cbm::algo::kf
28{
29
30
44 public:
48 int detId;
50 int locId;
51 double zRef;
52 double zMin;
53 double zMax;
54 double xMinFull;
55 double xMaxFull;
56 double yMinFull;
57 double yMaxFull;
58 double xMinAct;
59 double xMaxAct;
60 double yMinAct;
61 double yMaxAct;
63 bool timeInfo;
64
66 bool operator<(const LayerProperty& rhs) const { return zRef < rhs.zRef; }
67
70 template<typename F>
75
77 std::string ToString() const;
78 };
79
81 SetupBuilder() = default;
82
84 SetupBuilder(const SetupBuilder&) = delete;
85
88
90 virtual ~SetupBuilder() = default;
91
94
97
100 void AddLayer(LayerProperty geoLayer);
101
103 const FieldFn_t& GetFieldFunction() const { return fFieldBuilder.GetFieldFunction(); }
104
108 void Init();
109
114 template<typename F>
115 Setup<F> MakeSetup(EFieldMode fldMode) const;
116
118 void Reset();
119
125 void SetFieldFunction(const FieldFn_t& fieldFn, EFieldType fieldType)
126 {
127 fbReady = false;
128 fFieldBuilder.SetFieldFunction(fieldFn, fieldType);
131 }
132
136 template<typename F>
137 void SetFromSetup(const Setup<F>& inSetup);
138
141 void SetMaterialMapFactory(const std::shared_ptr<IMaterialMapFactory>& pMaterialFactory)
142 {
143 fbReady = false;
144 fpMaterialMapFactory = pMaterialFactory;
145 }
146
155 void SetMaterialCacheFile(const std::string& filename, size_t refHash)
156 {
157 fsMaterialCacheFile = filename;
158 fGeoHash = refHash;
159 }
160
167 void SetTargetProperty(double x, double y, double z, double dz, double r);
168
169
170 // ********************
171 // ** Static methods **
172 // ********************
173
181 static void Store(const Setup<double>& setup, const std::string& fileName);
182
187 template<typename F>
188 static Setup<F> Load(const std::string& fileName);
189
190 private:
191
193 std::string InitStatusMsg() const;
194
198 bool LoadMaterial();
199
201 void StoreMaterial() const;
202
203 // TODO: Define target material more precisely
204 static constexpr double kTargetCenterOffset{0.05};
205 static constexpr double kTargetMaterialOffset{2.};
206 static constexpr double kTargetMaterialTransverseSizeMargin{1.3};
207 static constexpr double kTargetFieldInitStep{
208 2.5};
209 static constexpr int kTargetMaterialMapNofBins{20};
210
211 std::set<LayerProperty> fGeoLayers{};
212 std::string fsMaterialCacheFile{""};
213 std::vector<MaterialMap> fvMaterial{};
214 std::shared_ptr<IMaterialMapFactory> fpMaterialMapFactory{nullptr};
218 size_t fGeoHash{0};
219 int fMatMapNofBins{100};
220
221 bool fbIfTargetSet{false};
222 bool fbIfGeoLayersInit{false};
224 bool fbIfSetFromSetup{false};
225 bool fbReady{false};
226 };
227
228
229 // ********************************
230 // ** Template method definition **
231 // ********************************
232
233
234 // ---------------------------------------------------------------------------------------------------------------------
235 //
236 template<typename F>
238 {
239 LOG(debug) << "kf::SetupBuilder: setting properties from input setup: " << inSetup.ToString(2);
240 fbReady = false;
241 // Reset geo layer information
242 fGeoLayers.clear();
243 fvMaterial.clear();
244 fvMaterial.reserve(inSetup.GetNofLayers());
245 fModuleIndexFactory.Reset();
246 fFieldBuilder.ResetSliceReferences();
247
248 // Init target properties
249 fTarget = Target<double>(inSetup.fTarget);
250
251 // Init geometry layers
252 for (int iLayer{0}; iLayer < inSetup.GetNofLayers(); ++iLayer) {
253 const auto& material{inSetup.GetMaterial(iLayer)};
254 fvMaterial.push_back(material);
255 const auto& actLayer{inSetup.GetActiveLayer(iLayer)};
256 fFieldBuilder.AddSliceReference(material.GetXYmax(), material.GetXYmax(), material.GetZref());
257 const auto& [iDetExt, iLoc] = inSetup.GetIndexMap().template GlobalToLocal<int>(iLayer);
258 fModuleIndexFactory.AddComponent(iDetExt, iLoc, material.GetZref());
259
260 AddLayer(std::move(
261 {.detId = iDetExt, //
262 .locId = iLoc, //
263 .zRef = utils::simd::Cast<F, double>(actLayer.GetZref()), //
264 .zMin = utils::simd::Cast<F, double>(actLayer.GetZmin()), //
265 .zMax = utils::simd::Cast<F, double>(actLayer.GetZmax()), //
266 .xMinFull = -material.GetXYMax(), // NOTE: the original info. on full vol. X min is lost in kf::Setup
267 .xMaxFull = +material.GetXYMax(), // NOTE: the original info. on full vol. X max is lost in kf::Setup
268 .yMinFull = -material.GetXYMax(), // NOTE: the original info. on full vol. Y min is lost in kf::Setup
269 .yMaxFull = +material.GetXYMax(), // NOTE: the original info. on full vol. Y max is lost in kf::Setup
270 .xMinAct = utils::simd::Cast<F, double>(actLayer.GetXmin()), //
271 .xMaxAct = utils::simd::Cast<F, double>(actLayer.GetXmax()), //
272 .yMinAct = utils::simd::Cast<F, double>(actLayer.GetYmin()), //
273 .yMaxAct = utils::simd::Cast<F, double>(actLayer.GetYmax()), //
274 .fieldType = actLayer.GetFieldType(),
275 .timeInfo = actLayer.IsTimeMeasured()}));
276 }
277
278 fbIfGeoLayersInit = true;
279 fbIfTargetSet = true;
280 fbIfSetFromSetup = true;
281 fbReady = true;
282 }
283
284 // ---------------------------------------------------------------------------------------------------------------------
285 //
286 template<typename F>
288 {
289 if (!fbReady) {
290 throw std::runtime_error(
291 "kf::SetupBuilder is not ready to build a setup. Please, provide all setters and call the "
292 "Init method");
293 }
294
295 // Form a vector of active layers
296 std::vector<ActiveLayer<F>> vActiveLayers;
297 vActiveLayers.reserve(fGeoLayers.size());
298 std::transform(fGeoLayers.begin(), fGeoLayers.end(), std::back_inserter(vActiveLayers),
299 [](const auto& in) -> ActiveLayer<F> { return in.template CreateActiveLayer<F>(); });
300
301 return Setup<F>(fModuleIndexFactory.MakeIndexMap(), fvMaterial, vActiveLayers, fFieldBuilder.MakeField<F>(fldMode),
302 fTarget);
303 }
304
305 // -------------------------------------------------------------------------------------------------------------------
306 //
307 template<typename F>
308 Setup<F> SetupBuilder::Load(const std::string& fileName)
309 {
310 Setup<double> setup;
311 std::ifstream ifs(fileName, std::ios::binary);
312 if (!ifs) {
313 throw std::runtime_error(fmt::format("kf::SetupBuilder::Load: intput setup file \"{}\" was not found", fileName));
314 }
315 try {
316 boost::archive::binary_iarchive ia(ifs);
317 ia >> setup;
318 }
319 catch (const std::exception& err) {
320 throw std::runtime_error(fmt::format("kf::SetupBuilder::Load: input setup file \"{}\" has inconsistent format or "
321 "was corrupted. The exception message: ",
322 fileName, err.what()));
323 }
324 if constexpr (std::is_same_v<F, double>) {
325 return setup;
326 }
327 else {
328 return Setup<F>(setup);
329 }
330 }
331} // namespace cbm::algo::kf
Interface to the material map creator.
Setup representation for the Kalman-filter framework (header)
Properties of an active surface of the layer.
A builder class for kf::Field.
Definition KfField.h:494
Creates a valid module mapper.
Setup< F > MakeSetup(EFieldMode fldMode) const
Creates a setup instance.
void Reset()
Resets the instance.
static constexpr double kTargetFieldInitStep
Step between nodal points to determine field near target [cm].
SetupBuilder()=default
Default constructor.
int fMatMapNofBins
Number of bins in material maps.
std::string fsMaterialCacheFile
A cache file for the material.
std::set< LayerProperty > fGeoLayers
Set of geometrical layers.
void SetFromSetup(const Setup< F > &inSetup)
Initializes the setup builder from existing setup.
static constexpr double kTargetMaterialOffset
Offset of the target material [in dz].
std::string InitStatusMsg() const
Prints initialization status message.
bool fbIfFieldFunctionSet
Field function initialized.
static constexpr double kTargetMaterialTransverseSizeMargin
Margin of target transverse size.
ModuleIndexMapFactory fModuleIndexFactory
Module index factory.
Target< double > fTarget
Target properties.
virtual ~SetupBuilder()=default
Destructor.
FieldBuilder fFieldBuilder
Instance of field factory.
bool fbIfGeoLayersInit
Geo layers initialized.
bool LoadMaterial()
Reads material from file.
void StoreMaterial() const
Stores material to file.
SetupBuilder & operator=(SetupBuilder &&)=delete
Move assignment operator.
static Setup< F > Load(const std::string &fileName)
Loads a serialized setup from a file.
void SetMaterialMapFactory(const std::shared_ptr< IMaterialMapFactory > &pMaterialFactory)
Sets material map creator.
void SetFieldFunction(const FieldFn_t &fieldFn, EFieldType fieldType)
Sets magnetic field function.
void Init()
Initializes, validates and caches the parameters.
SetupBuilder(const SetupBuilder &)=delete
Copy constructor.
const FieldFn_t & GetFieldFunction() const
Access to field funciton.
void AddLayer(LayerProperty geoLayer)
Adds material layer.
void SetTargetProperty(double x, double y, double z, double dz, double r)
Sets target initialization properties.
static constexpr int kTargetMaterialMapNofBins
Number of bins along x- and y-axis in the target region;.
static constexpr double kTargetCenterOffset
Offset from target center [cm].
void SetMaterialCacheFile(const std::string &filename, size_t refHash)
Sets the material budget cache file name.
SetupBuilder(SetupBuilder &&)=delete
Move constructor.
std::vector< MaterialMap > fvMaterial
Material map container.
SetupBuilder & operator=(const SetupBuilder &)=delete
Copy assignment operator.
bool fbIfTargetSet
Target initialized.
bool fbReady
Instance is ready for setup generation.
static void Store(const Setup< double > &setup, const std::string &fileName)
Stores a serialized setup to a file.
std::shared_ptr< IMaterialMapFactory > fpMaterialMapFactory
Material map creator.
size_t fGeoHash
A hash of the geometry.
KF-framework representation of the detector setup.
Definition KfSetup.h:37
Target< F > fTarget
Target layer.
Definition KfSetup.h:175
std::string ToString(int verbosity=1, int indentLevel=0) const
String representation of the class contents.
Definition KfSetup.cxx:20
int GetNofLayers() const
Gets number of geometry layers.
Definition KfSetup.h:152
const ModuleIndexMap & GetIndexMap() const
Gets module index map.
Definition KfSetup.h:149
const MaterialMap & GetMaterial(int iLayer) const
Gets material layer.
Definition KfSetup.h:136
const ActiveLayer< F > & GetActiveLayer(int iLayer) const
Gets active layer instance.
Definition KfSetup.h:115
A geometry layer in the target region.
Definition KfTarget.h:25
DataOut Cast(const DataT &val)
Converts a value of type DataT to type DataOut.
Definition KfUtils.h:212
EFieldMode
Enumiration for the magnetic field representation variants in the track fitting algorithm.
Definition KfDefs.h:108
EFieldType
Magnetic field type in different setup regions.
Definition KfDefs.h:126
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
A helper structure to store geometrical information of the layers.
double xMaxAct
Upper x-coordinate boundary of active volume [cm].
bool timeInfo
If a layer stores time information.
double yMinAct
Lower x-coordinate boundary of active volume [cm].
std::string ToString() const
String representation of the class.
double zMin
Lower z-coordinate boundary [cm].
int locId
Local index of the detector module.
double xMaxFull
Upper x-coordinate boundary of full volume [cm].
double yMinFull
Lower x-coordinate boundary of full volume [cm].
bool operator<(const LayerProperty &rhs) const
Comparison method (for sorting layers)
double zMax
Upper z-coordinate boundary [cm].
double zRef
Reference z-coordinate [cm].
EFieldType fieldType
Type of the magnetic field.
double xMinAct
Lower x-coordinate boundary of active volume [cm].
ActiveLayer< F > CreateActiveLayer() const
Constructs an active layer.
double xMinFull
Lower x-coordinate boundary of full volume [cm].
double yMaxFull
Upper x-coordinate boundary of full volume [cm].
double yMaxAct
Upper x-coordinate boundary of active volume [cm].