CbmRoot
Loading...
Searching...
No Matches
KfModuleIndexMap.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
11
12#include "KfDefs.h"
13
14#include <boost/serialization/access.hpp>
15#include <boost/serialization/utility.hpp>
16
17#include <set>
18#include <sstream>
19#include <string>
20#include <vector>
21
22#include <fmt/format.h>
23
24namespace cbm::algo::kf
25{
26
49 class alignas(VcMemAlign) ModuleIndexMap {
51 friend class boost::serialization::access;
52
53 public:
55 int GetNofLayers() const { return fvGlbToLoc.size(); }
56
61 template<class EDetID = int>
62 std::pair<EDetID, int> GlobalToLocal(int globId) const;
63
70 template<class EDetID>
71 int LocalToGlobal(EDetID detId, int locId) const
72 {
73 return fvLocToGlb[fvDetLocOffset[fvDetExtToInt[static_cast<int>(detId)]] + locId];
74 }
75
80 template<class EDetID>
81 void Disable(EDetID detId, int locId);
82
84 void Reset();
85
87 std::string ToString(int indentLevel = 0) const;
88
89 private:
90 template<class Archive>
91 void serialize(Archive& ar, const unsigned int /*version*/)
92 {
93 ar& fvLocToGlb;
94 ar& fvGlbToLoc;
95 ar& fvDetLocOffset;
96 ar& fvDetIntToExt;
97 ar& fvDetExtToInt;
98 }
99
100 std::vector<int> fvLocToGlb{};
101 std::vector<std::pair<int, int>> fvGlbToLoc{};
102 std::vector<int> fvDetLocOffset{};
103 std::vector<int> fvDetIntToExt{};
104 std::vector<int> fvDetExtToInt{};
105 };
106
112 struct Component {
113 Component(int detId, int locId, double z) : fZ(z), fLocId(locId), fDetId(detId) {}
114 double fZ;
115 int fLocId;
116 int fDetId;
117 bool operator<(const Component& rhs) const { return fZ < rhs.fZ; }
118 };
119
120 public:
125 template<class EDetID>
126 void AddComponent(EDetID detId, int locId, double z);
127
129 ModuleIndexMap MakeIndexMap() const;
130
132 void Reset();
133
134 private:
135 std::set<Component> fvComponentLayers;
136 };
137
138 // -------------------------------------------------------------------------------------------------------------------
139 //
140 template<class EDetID>
141 void ModuleIndexMap::Disable(EDetID detIdDisable, int locIdDisable)
142 {
143 // Check, if the detector id is there
144 auto iDetIntDsbl{fvDetExtToInt[static_cast<int>(detIdDisable)]};
145 if (iDetIntDsbl >= static_cast<int>(fvDetIntToExt.size())) {
146 return; // Nothing to disable, detector is already not in the map
147 }
148
149 auto& iGlbDsbl = fvLocToGlb[fvDetLocOffset[iDetIntDsbl] + locIdDisable];
150 if (iGlbDsbl < 0) {
151 return; // Nothing to disable, component is already inactive
152 }
153
154 fvGlbToLoc.erase(fvGlbToLoc.begin() + iGlbDsbl); // Removing component from glob->(det, loc) map
155 for (auto& val : fvLocToGlb) {
156 if (val > iGlbDsbl) {
157 --val;
158 }
159 }
160 iGlbDsbl = -1;
161 }
162
163 // -------------------------------------------------------------------------------------------------------------------
164 //
165 template<class EDetID>
166 inline std::pair<EDetID, int> ModuleIndexMap::GlobalToLocal(int globId) const
167 {
168 const auto& [iDetExt, iLoc] = fvGlbToLoc[globId];
169 return std::pair(static_cast<EDetID>(iDetExt), iLoc);
170 }
171
172 // -------------------------------------------------------------------------------------------------------------------
173 //
174 template<class EDetID>
175 void ModuleIndexMapFactory::AddComponent(EDetID detId, int locId, double z)
176 {
177 if (!fvComponentLayers.emplace(static_cast<int>(detId), locId, z).second) {
178 std::stringstream msg;
179 msg << "ModuleIndexMapFactory: attempt of adding a duplicating component with z = " << z
180 << ", detID = " << static_cast<int>(detId) << " and locId = " << locId
181 << ".\n The next components were already added:";
182 for (const auto& c : fvComponentLayers) {
183 msg << "\n\t- {z, detId, locId} = {" << c.fZ << ", " << c.fDetId << ", " << c.fLocId << "}";
184 }
185 throw std::logic_error(msg.str());
186 }
187 }
188
189
190} // namespace cbm::algo::kf
std::string ToString(ECbmModuleId modId)
Definition CbmDefs.cxx:70
Common constant definitions for the Kalman Filter library.
Creates a valid module mapper.
Maps local detector and station indices to the material maps and field slices.
void serialize(Archive &ar, const unsigned int)
int GetNofLayers() const
Gets total number of components.
int LocalToGlobal(EDetID detId, int locId) const
Converts external pair (detID, locID) to internal layer index.
Structure to keep information on layers.
int fDetId
External index of detector subsystem.
double fZ
Reference z-coordinate of the component.