CbmRoot
Loading...
Searching...
No Matches
KfSetup.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
12#include "KfActiveLayer.h"
13#include "KfDefs.h"
14#include "KfField.h"
15#include "KfMaterialMap.h"
16#include "KfModuleIndexMap.h"
17#include "KfTarget.h"
18#include "KfVector.h"
19
20#include <boost/serialization/access.hpp>
21
22#include <fstream>
23#include <numeric>
24#include <optional>
25#include <sstream>
26#include <string>
27#include <unordered_map>
28#include <unordered_set>
29#include <vector>
30
31namespace cbm::algo::kf
32{
36 template<typename F>
37 class alignas(VcMemAlign) Setup {
38 template<typename>
39 friend class Setup;
40
41 public:
43 // FIXME: hide to private
44 Setup() = default;
45
52 Setup(const ModuleIndexMap& moduleIndexMap, //
53 const std::vector<MaterialMap>& materialLayers, //
54 std::vector<ActiveLayer<F>> activeLayers, //
55 const Field<F>& field, //
56 const Target<F>& target)
57 : fModuleIndexMap(moduleIndexMap)
58 , fvMaterialLayers(materialLayers)
59 , fvActiveLayers(std::move(activeLayers))
60 , fField(field)
61 , fTarget(target)
62 {
63 if (!Validate()) {
64 throw std::logic_error("Attempt to create a kf::Setup from inconsistent set of parameters. Please, use the "
65 "kf::SetupBuilder to create a valid setup.");
66 }
67 }
68
71 template<typename FSrc>
72 Setup(const Setup<FSrc>& other)
75 , fField(std::move(Field<F>::CopyConvert(other.fField)))
76 , fTarget(other.fTarget)
77 {
78 fvActiveLayers.reserve(other.fvActiveLayers.size());
79 for (const auto& layer : other.fvActiveLayers) {
80 fvActiveLayers.emplace_back(layer);
81 }
82 }
83
85 Setup(const Setup&) = default;
86
88 Setup(Setup&&) = default;
89
91 ~Setup() = default;
92
94 Setup& operator=(const Setup& other) = default;
95
97 Setup& operator=(Setup&&) = default;
98
103 template<class EDetID>
104 Setup CreateSetupWithDisabledLayers(const std::vector<std::pair<EDetID, int>>& toDisable) const;
105
110 template<class EDetID>
111 void DisableLayer(EDetID iDet, int iLoc);
112
115 const ActiveLayer<F>& GetActiveLayer(int iLayer) const { return fvActiveLayers[iLayer]; }
116
118 const auto& GetActiveLayers() const { return fvActiveLayers; }
119
124 template<class EDetID>
125 const ActiveLayer<F>& GetActiveLayer(EDetID iDet, int iLoc) const
126 {
127 return fvActiveLayers[fModuleIndexMap.LocalToGlobal(iDet, iLoc)];
128 }
129
132 const Field<F>& GetField() const { return fField; }
133
136 const MaterialMap& GetMaterial(int iLayer) const { return fvMaterialLayers[iLayer]; }
137
142 template<class EDetID>
143 const MaterialMap& GetMaterial(EDetID iDet, int iLoc) const
144 {
145 return fvMaterialLayers[fModuleIndexMap.LocalToGlobal(iDet, iLoc)];
146 }
147
149 const ModuleIndexMap& GetIndexMap() const { return fModuleIndexMap; }
150
152 int GetNofLayers() const { return fModuleIndexMap.GetNofLayers(); }
153
156 template<class EDetID>
157 int GetNofLayers(EDetID iDet) const
158 {
159 return fModuleIndexMap.GetNofLayers(iDet);
160 }
161
163 const Target<F>& GetTarget() const { return fTarget; }
164
168 std::string ToString(int verbosity = 1, int indentLevel = 0) const;
169
170 private:
172 std::vector<MaterialMap> fvMaterialLayers{};
173 std::vector<ActiveLayer<F>> fvActiveLayers{};
176
177
183 bool Validate() const;
184
186 friend class boost::serialization::access;
187 template<class Archive>
188 void serialize(Archive& ar, const unsigned int /*version*/)
189 {
190 ar& fModuleIndexMap;
192 ar& fvActiveLayers;
193 ar& fTarget;
194 ar& fField;
195 }
196 };
197
198
199 // -------------------------------------------------------------------------------------------------------------------
200 //
201 template<typename F>
202 template<class EDetID>
203 Setup<F> Setup<F>::CreateSetupWithDisabledLayers(const std::vector<std::pair<EDetID, int>>& toDisableInp) const
204 {
205 Setup<F> res(*this);
206 // We also take into account a situation, when some of the elements in the input vector make no sense in terms of
207 // the geometry setup, so we create a new container for keeping the inactive layers of only existing stations and
208 // sort it in the direction of geoId decrease:
209
210 using Inp_t = typename std::remove_cv_t<std::remove_reference_t<decltype(toDisableInp)>>;
211
212 // Build a temporary array, which contains only valid {iDet, iLoc} to be disabled
213 Inp_t sorted;
214 {
215 // Collect the {iDet: locIds} map: actual iLoc for each iDet to be disabled
216 // This approach addresses the following:
217 // 1) filters {iDet, iLoc(> -1)} pairs, which are passed with toDisable vector, which do not exist in the setup
218 // 2) translates iLoc == -1 into a sequence of iLoc [0, NofLayers(detId)), which are to be disabled
219 std::unordered_map<EDetID, std::unordered_set<int>> filtered;
220 // Lookup the input toDisable, fill the toDisableTmp map:
221 for (auto [iDet, iLoc] : toDisableInp) {
222 // Disable a single layer (with locId > -1), if it exists in this setup
223 if (iLoc > -1 && fModuleIndexMap.LocalToGlobal(iDet, iLoc) > -1) {
224 filtered[iDet].insert(iLoc);
225 }
226
227 // Disable all layers of the detector subsystem, it at has at least one layer
228 if (iLoc == -1 && fModuleIndexMap.GetNofLayers(iDet) > 0) {
229 auto& locIds = filtered[iDet];
230 for (int iL = 0; iL < fModuleIndexMap.GetNofLayers(iDet); ++iL) {
231 locIds.insert(iL);
232 }
233 }
234 }
235
236 // Fill the temporary array
237 sorted.reserve(std::accumulate(filtered.begin(), filtered.end(), 0,
238 [](int sum, const auto& item) { return sum + item.second.size(); }));
239 for (const auto& [iDet, locIds] : filtered) {
240 // NOTE: [iDet = iDet] is a solution to pass structure bindings in lambdas for some compilers
241 std::transform(locIds.begin(), locIds.end(), std::back_inserter(sorted), [iDet = iDet](int iLoc) {
242 return std::pair{iDet, iLoc};
243 });
244 }
245 }
246
247 // Sort the prepared array in the decreasing order of global Ids
248 std::sort(sorted.begin(), sorted.end(), [&](auto l, auto r) {
249 return fModuleIndexMap.LocalToGlobal(l.first, l.second) > fModuleIndexMap.LocalToGlobal(r.first, r.second);
250 });
251
252 // Disable the selected layers
253 for (auto [iDet, iLoc] : sorted)
254 res.DisableLayer(iDet, iLoc);
255 return res;
256 }
257
258 // -------------------------------------------------------------------------------------------------------------------
259 //
260 template<typename F>
261 template<class EDetID>
262 void Setup<F>::DisableLayer(EDetID iDet, int iLoc)
263 {
264 int iLayer{fModuleIndexMap.LocalToGlobal(iDet, iLoc)};
265 if (iLayer < 0) {
266 return;
267 }
268
269 // Remove material layer and add it to the next one
270 if (iLayer < static_cast<int>(fvMaterialLayers.size() - 1)) {
272 }
273 fvMaterialLayers.erase(fvMaterialLayers.begin() + iLayer);
274
275 // Remove active layer
276 fvActiveLayers.erase(fvActiveLayers.begin() + iLayer);
277
278 // Remove field slice
279 fField.RemoveSlice(iLayer);
280
281 // Disable layer in the index map
282 fModuleIndexMap.Disable(iDet, iLoc);
283 }
284
285
286} // namespace cbm::algo::kf
std::string ToString(CbmCutId id)
Convert CbmCutId to a string representation.
Definition CbmCutId.cxx:7
Properties of an active surface of the layer.
Common constant definitions for the Kalman Filter library.
Magnetic field representation in KF (header)
A helper class to map external indices with the ones of KF-setup.
A target layer in the KF-setup (header)
std::vector with an additional utility set
Properties of an active surface of the layer.
Magnetic field manager class.
Definition KfField.h:272
A map of station thickness in units of radiation length (X0) to the specific point in XY plane.
Maps local detector and station indices to the material maps and field slices.
const Field< F > & GetField() const
Makes an instance of the field depending on the template parameter.
Definition KfSetup.h:132
Setup()=default
Default constructor (for boost serialization)
Setup & operator=(Setup &&)=default
Move assignment operator.
Target< F > fTarget
Target layer.
Definition KfSetup.h:175
int GetNofLayers(EDetID iDet) const
Gets number of layers for detector.
Definition KfSetup.h:157
const auto & GetActiveLayers() const
Gets active layers container.
Definition KfSetup.h:118
friend class Setup
Definition KfSetup.h:39
Setup(Setup &&)=default
Move constructor.
const Target< F > & GetTarget() const
Gets target.
Definition KfSetup.h:163
Setup & operator=(const Setup &other)=default
Copy assignment operator.
void DisableLayer(EDetID iDet, int iLoc)
Disables geo layer.
Definition KfSetup.h:262
bool Validate() const
Validates setup as a system of magnetic field, target and field.
Definition KfSetup.cxx:45
const ActiveLayer< F > & GetActiveLayer(EDetID iDet, int iLoc) const
Gets active layer instance.
Definition KfSetup.h:125
Setup CreateSetupWithDisabledLayers(const std::vector< std::pair< EDetID, int > > &toDisable) const
Creates another setup from this with some of the layers disabled.
const MaterialMap & GetMaterial(EDetID iDet, int iLoc) const
Gets material layer from external indices.
Definition KfSetup.h:143
int GetNofLayers() const
Gets number of geometry layers.
Definition KfSetup.h:152
Field< F > fField
Magnetic field.
Definition KfSetup.h:174
const ModuleIndexMap & GetIndexMap() const
Gets module index map.
Definition KfSetup.h:149
std::vector< ActiveLayer< F > > fvActiveLayers
Container for active layers.
Definition KfSetup.h:173
~Setup()=default
Destructor.
ModuleIndexMap fModuleIndexMap
Index map (internal->external)
Definition KfSetup.h:171
const MaterialMap & GetMaterial(int iLayer) const
Gets material layer.
Definition KfSetup.h:136
Setup(const ModuleIndexMap &moduleIndexMap, const std::vector< MaterialMap > &materialLayers, std::vector< ActiveLayer< F > > activeLayers, const Field< F > &field, const Target< F > &target)
Constructor.
Definition KfSetup.h:52
const ActiveLayer< F > & GetActiveLayer(int iLayer) const
Gets active layer instance.
Definition KfSetup.h:115
void serialize(Archive &ar, const unsigned int)
Definition KfSetup.h:188
Setup(const Setup &)=default
Default copy constructor.
std::vector< MaterialMap > fvMaterialLayers
Container for material maps.
Definition KfSetup.h:172
Setup(const Setup< FSrc > &other)
Copy-convert constructor.
Definition KfSetup.h:72
A geometry layer in the target region.
Definition KfTarget.h:25
void DisableLayer(EDetID iDet, int iLoc)
Disables geo layer.
Definition KfSetup.h:262
DataOut Cast(const DataT &val)
Converts a value of type DataT to type DataOut.
Definition KfUtils.h:212
Hash for CbmL1LinkKey.