CbmRoot
Loading...
Searching...
No Matches
KfSetupBuilder.h
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
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
25namespace cbm::algo::kf
26{
29 template<class EDetID>
30 struct GeoLayer {
31 GeoLayer(EDetID detID, int iLoc, double zRef, double zMin, double zMax, double xMax, double yMax)
32 : fZref(zRef)
33 , fZmin(zMin)
34 , fZmax(zMax)
35 , fXmax(xMax)
36 , fYmax(yMax)
37 , fDetID(detID)
38 , fLocID(iLoc)
39 {
40 }
41
42 double fZref;
43 double fZmin;
44 double fZmax;
45 double fXmax;
46 double fYmax;
47 EDetID fDetID;
48 int fLocID;
49 bool operator<(const GeoLayer& rhs) const { return fZref < rhs.fZref; }
50 };
51
65 public:
67 SetupBuilder() = default;
68
70 SetupBuilder(const SetupBuilder&) = delete;
71
74
76 virtual ~SetupBuilder() = default;
77
80
83
87 template<class EDetID>
88 void AddLayer(const GeoLayer<EDetID>& geoLayer);
89
92 template<typename T>
94
96 void Reset();
97
101 void SetFieldFunction(const FieldFn_t& fieldFn, EFieldType fieldType)
102 {
103 fbReady = false;
104 fFieldFactory.SetFieldFunction(fieldFn, fieldType);
107 }
108
112 template<typename T>
113 void SetFromSetup(const Setup<T>& inSetup);
114
117 void SetMaterialMapFactory(const std::shared_ptr<IMaterialMapFactory>& pMaterialFactory)
118 {
119 fbReady = false;
120 fpMaterialMapFactory = pMaterialFactory;
121 }
122
131 void SetMaterialCacheFile(const std::string& filename, size_t refHash)
132 {
133 fsMaterialCacheFile = filename;
134 fGeoHash = refHash;
135 }
136
143 void SetTargetProperty(double x, double y, double z, double dz, double r);
144
145 // ********************
146 // ** Static methods **
147 // ********************
148
156 static void Store(const Setup<double>& setup, const std::string& fileName);
157
162 template<typename T>
163 static Setup<T> Load(const std::string& fileName);
164
165 private:
169 void Init();
170
172 std::string InitStatusMsg() const;
173
177 bool LoadMaterial();
178
180 void StoreMaterial() const;
181
182 // TODO: Define target material more precisely
183 static constexpr double kTargetCenterOffset{0.05}; // Offset from target center [cm]
184 static constexpr double kTargetMaterialOffset{2.}; // Offset of the target material [in dz]
185 static constexpr double kTargetMaterialTransverseSizeMargin{1.3}; // Margin of target transverse size
186 static constexpr double kTargetFieldInitStep{2.5}; // Step between nodal points to determine field near target [cm]
187 static constexpr int kTargetMaterialMapNofBins{20};
188
189 std::set<GeoLayer<int>> fGeoLayers{};
190 std::string fsMaterialCacheFile{""};
191 std::vector<MaterialMap> fvMaterial{};
192 std::shared_ptr<IMaterialMapFactory> fpMaterialMapFactory{nullptr};
196 size_t fGeoHash{0};
197 int fMatMapNofBins{100};
198
199 bool fbIfTargetSet{false};
200 bool fbIfGeoLayersInit{false};
202 bool fbIfSetFromSetup{false};
203 bool fbReady{false};
204 };
205
206
207 // ********************************
208 // ** Template method definition **
209 // ********************************
210
211
212 // ---------------------------------------------------------------------------------------------------------------------
213 //
214 template<class EDetID>
216 {
217 fbReady = false;
218 auto layer = fGeoLayers.emplace(static_cast<int>(geoLayer.fDetID), geoLayer.fLocID, geoLayer.fZref, geoLayer.fZmin,
219 geoLayer.fZmax, geoLayer.fXmax, geoLayer.fYmax);
220 if (!layer.second) {
221 std::stringstream msg;
222 msg << "SetupBuilder::AddLayer: attempt of adding a duplicating geometry layer: ";
223 msg << "DetID = " << static_cast<int>(geoLayer.fDetID) << ", localID = " << geoLayer.fLocID
224 << "fZref = " << geoLayer.fZref << ", fZmin = " << geoLayer.fZmin << ", fZmax = " << geoLayer.fZmax
225 << ", fXmax = " << geoLayer.fXmax << ", fYmax = " << geoLayer.fYmax
226 << ".\nThe next layers were already added:";
227 for (const auto& el : fGeoLayers) {
228 msg << "\n\t- DetID = " << static_cast<int>(el.fDetID) << ", localID = " << el.fLocID << "fZref = " << el.fZref
229 << ", fZmin = " << el.fZmin << ", fZmax = " << el.fZmax << ", fXmax = " << el.fXmax
230 << ", fYmax = " << el.fYmax;
231 }
232 throw std::logic_error(msg.str());
233 }
234 }
235
236 // ---------------------------------------------------------------------------------------------------------------------
237 //
238 template<typename T>
240 {
241 LOG(info) << inSetup.ToString(2);
242 fbReady = false;
243 // Reset geo layer information
244 fGeoLayers.clear();
245 fvMaterial.clear();
248
249 // Init target properties
250 fTarget = Target<double>(inSetup.fTarget);
251
252 // Init geometry layers
253 for (int iLayer{0}; iLayer < inSetup.GetNofMaterialLayers(); ++iLayer) {
254 const auto& material{inSetup.GetMaterial(iLayer)};
255 fvMaterial.push_back(material);
256 fFieldFactory.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
261 fbIfGeoLayersInit = true;
262 fbIfTargetSet = true;
263 fbIfSetFromSetup = true;
264 }
265
266 // ---------------------------------------------------------------------------------------------------------------------
267 //
268 template<typename T>
270 {
271 Setup<T> setup(fldMode);
272 if (!fbReady) {
273 this->Init();
274 }
275 setup.fTarget = this->fTarget;
276 for (const auto& material : this->fvMaterial) {
277 setup.fvMaterialLayers.push_back(material);
278 }
279 setup.fField = fFieldFactory.MakeField<T>();
281 return setup;
282 }
283
284 // -------------------------------------------------------------------------------------------------------------------
285 //
286 template<typename T>
287 Setup<T> SetupBuilder::Load(const std::string& fileName)
288 {
290 std::ifstream ifs(fileName, std::ios::binary);
291 if (!ifs) {
292 std::stringstream msg;
293 msg << "kf::SetupBuilder::Load: intput setup file \"" << fileName << "\" was not found";
294 throw std::runtime_error(msg.str());
295 }
296 try {
297 boost::archive::binary_iarchive ia(ifs);
298 ia >> setup;
299 }
300 catch (const std::exception& err) {
301 std::stringstream msg;
302 msg << "kf::SetupBuilder::Load: input setup file \"" << fileName
303 << "\" has inconsistent format or was "
304 "corrupted. The exception message: "
305 << err.what();
306 throw std::runtime_error(msg.str());
307 }
308 return Setup<T>(setup);
309 }
310} // namespace cbm::algo::kf
Interface to the material map creator.
Setup representation for the Kalman-filter framework (header)
A helper class to instantiate a Field object.
Definition KfField.h:328
void SetStep(double step=2.5)
Sets a step for the primary vertex field region estimation.
Definition KfField.h:395
Field< T > MakeField() const
Create field.
Definition KfField.h:417
void ResetSliceReferences()
Resets slicer references.
Definition KfField.h:380
void SetFieldFunction(const FieldFn_t &fieldFn, EFieldType fldType)
Sets magnetic field function.
Definition KfField.h:384
void AddSliceReference(double halfSizeX, double halfSizeY, double refZ)
Adds a slice reference.
Definition KfField.cxx:96
Creates a valid module mapper.
ModuleIndexMap MakeIndexMap() const
Creates a module index map.
void AddComponent(EDetID detId, int locId, double z)
Adds component info.
Creates a valid initialized Setup instance.
void AddLayer(const GeoLayer< EDetID > &geoLayer)
Adds material layer.
void Reset()
Resets the instance.
static constexpr double kTargetFieldInitStep
SetupBuilder()=default
Default constructor.
int fMatMapNofBins
Number of bins in material maps.
std::string fsMaterialCacheFile
A cache file for the material.
static constexpr double kTargetMaterialOffset
std::string InitStatusMsg() const
Prints initialization status message.
bool fbIfFieldFunctionSet
Field function initialized.
static constexpr double kTargetMaterialTransverseSizeMargin
Setup< T > MakeSetup(EFieldMode fldMode)
Creates a setup instance.
static Setup< T > Load(const std::string &fileName)
Loads a serialized setup from a file.
std::set< GeoLayer< int > > fGeoLayers
Set of geo layers.
FieldFactory fFieldFactory
Instance of field factory.
ModuleIndexMapFactory fModuleIndexFactory
Module index factory.
Target< double > fTarget
Target properties.
virtual ~SetupBuilder()=default
Destructor.
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.
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.
void SetTargetProperty(double x, double y, double z, double dz, double r)
Sets target initialization properties.
static constexpr int kTargetMaterialMapNofBins
static constexpr double kTargetCenterOffset
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.
void SetFromSetup(const Setup< T > &inSetup)
Initializes the setup builder from existing setup.
KF-framework representation of the detector setup.
Definition KfSetup.h:33
const ModuleIndexMap & GetIndexMap() const
Gets module index map.
Definition KfSetup.h:98
Field< T > fField
Interpolated field (NOTE: maybe make optional)
Definition KfSetup.h:123
ModuleIndexMap fModuleIndexMap
Index map (internal->external)
Definition KfSetup.h:121
std::string ToString(int verbosity=1, int indentLevel=0) const
String representation of the class contents.
Definition KfSetup.cxx:20
const MaterialMap & GetMaterial(int iLayer) const
Gets material layer.
Definition KfSetup.h:85
std::vector< MaterialMap > fvMaterialLayers
Container of the material maps.
Definition KfSetup.h:122
Target< T > fTarget
Target layer.
Definition KfSetup.h:124
A geometry layer in the target region.
Definition KfTarget.h:25
EFieldMode
Enumiration for the magnetic field representation variants in the track fitting algorithm.
Definition KfDefs.h:27
@ Intrpl
Interpolated magnetic field.
EFieldType
Magnetic field type in different setup regions.
Definition KfDefs.h:37
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:66
A helper structure to store geometrical information of the layers.
double fZmax
Upper z-coordinate boundary [cm].
GeoLayer(EDetID detID, int iLoc, double zRef, double zMin, double zMax, double xMax, double yMax)
double fXmax
Half size of the layer in the x-direction [cm].
double fYmax
Half size of the layer in the y-direction [cm].
int fLocID
Local index of the detector module.
EDetID fDetID
Index of detector subsystem.
bool operator<(const GeoLayer &rhs) const
double fZmin
Lower z-coordinate boundary [cm].
double fZref
Reference z-coordinate [cm].