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 // TODO: Account for undefined detId and locId
27
50 class alignas(VcMemAlign) ModuleIndexMap {
52 friend class boost::serialization::access;
53
54 public:
56 int GetNofLayers() const { return fvGlbToLoc.size(); }
57
61 template<class EDetId>
62 int GetNofLayers(EDetId detId) const
63 {
64 int iDet = GetIntDetIndex(detId);
65 return iDet > -1 ? fvNofLayersIntDet[iDet] : 0;
66 }
67
72 template<class EDetId = int>
73 std::pair<EDetId, int> GlobalToLocal(int globId) const;
74
81 template<class EDetId>
82 int LocalToGlobal(EDetId detId, int locId) const
83 {
84 return fvLocToGlb[fvDetLocOffset[fvDetExtToInt[static_cast<int>(detId)]] + locId];
85 }
86
91 template<class EDetId>
92 void Disable(EDetId detId, int locId);
93
95 void Reset();
96
98 std::string ToString(int indentLevel = 0) const;
99
100 private:
103 template<class EDetId>
104 int GetIntDetIndex(EDetId detId) const
105 {
106 return static_cast<size_t>(detId) < fvDetExtToInt.size() ? fvDetExtToInt[static_cast<int>(detId)] : -1;
107 }
108
109 template<class Archive>
110 void serialize(Archive& ar, const unsigned int /*version*/)
111 {
112 ar& fvLocToGlb;
113 ar& fvGlbToLoc;
114 ar& fvDetLocOffset;
115 ar& fvDetIntToExt;
116 ar& fvDetExtToInt;
118 }
119
120 std::vector<int> fvLocToGlb{};
121 std::vector<std::pair<int, int>> fvGlbToLoc{};
122 std::vector<int> fvDetLocOffset{};
123 std::vector<int> fvDetIntToExt{};
124 std::vector<int> fvDetExtToInt{};
125 std::vector<int> fvNofLayersIntDet{};
126 };
127
133 struct Component {
134 Component(int detId, int locId, double z) : fZ(z), fLocId(locId), fDetId(detId) {}
135 double fZ;
136 int fLocId;
137 int fDetId;
138 bool operator<(const Component& rhs) const { return fZ < rhs.fZ; }
139 };
140
141 public:
146 template<class EDetId>
147 void AddComponent(EDetId detId, int locId, double z);
148
150 ModuleIndexMap MakeIndexMap() const;
151
153 void Reset();
154
155 private:
156 std::set<Component> fvComponentLayers;
157 };
158
159 // -------------------------------------------------------------------------------------------------------------------
160 //
161 template<class EDetId>
162 void ModuleIndexMap::Disable(EDetId detIdDisable, int locIdDisable)
163 {
164 // Check, if the detector id is there
165 auto iDetIntDsbl{fvDetExtToInt[static_cast<int>(detIdDisable)]};
166 if (iDetIntDsbl >= static_cast<int>(fvDetIntToExt.size())) {
167 return; // Nothing to disable, detector is already not in the map
168 }
169
170 auto& iGlbDsbl = fvLocToGlb[fvDetLocOffset[iDetIntDsbl] + locIdDisable];
171 if (iGlbDsbl < 0) {
172 return; // Nothing to disable, component is already inactive
173 }
174
175 fvGlbToLoc.erase(fvGlbToLoc.begin() + iGlbDsbl); // Removing component from glob->(det, loc) map
176 for (auto& val : fvLocToGlb) {
177 if (val > iGlbDsbl) {
178 --val;
179 }
180 }
181
182 iGlbDsbl = -1;
183 --fvNofLayersIntDet[iDetIntDsbl];
184 }
185
186 // -------------------------------------------------------------------------------------------------------------------
187 //
188 template<class EDetId>
189 inline std::pair<EDetId, int> ModuleIndexMap::GlobalToLocal(int globId) const
190 {
191 const auto& [iDetExt, iLoc] = fvGlbToLoc[globId];
192 return std::pair(static_cast<EDetId>(iDetExt), iLoc);
193 }
194
195 // -------------------------------------------------------------------------------------------------------------------
196 //
197 template<class EDetId>
198 void ModuleIndexMapFactory::AddComponent(EDetId detId, int locId, double z)
199 {
200 if (!fvComponentLayers.emplace(static_cast<int>(detId), locId, z).second) {
201 std::stringstream msg;
202 msg << "ModuleIndexMapFactory: attempt of adding a duplicating component with z = " << z
203 << ", detID = " << static_cast<int>(detId) << " and locId = " << locId
204 << ".\n The next components were already added:";
205 for (const auto& c : fvComponentLayers) {
206 msg << "\n\t- {z, detId, locId} = {" << c.fZ << ", " << c.fDetId << ", " << c.fLocId << "}";
207 }
208 throw std::logic_error(msg.str());
209 }
210 }
211
212
213} // namespace cbm::algo::kf
std::string ToString(CbmCutId id)
Convert CbmCutId to a string representation.
Definition CbmCutId.cxx:7
Common constant definitions for the Kalman Filter library.
Creates a valid module mapper.
void AddComponent(EDetId detId, int locId, double z)
Adds component info.
Maps local detector and station indices to the material maps and field slices.
int GetNofLayers(EDetId detId) const
Gets number of layers of a given EDetId.
void Disable(EDetId detId, int locId)
Disables a component.
std::vector< int > fvNofLayersIntDet
Number of layers in a detector.
std::vector< int > fvDetLocOffset
First index of component for det.
int LocalToGlobal(EDetId detId, int locId) const
Converts external pair (detID, locID) to internal layer index.
void serialize(Archive &ar, const unsigned int)
std::vector< int > fvDetIntToExt
Maps external detID to internal.
int GetNofLayers() const
Gets total number of components.
std::vector< std::pair< int, int > > fvGlbToLoc
int GetIntDetIndex(EDetId detId) const
Gets internal index of the detector ID.
std::vector< int > fvDetExtToInt
Maps internal detID to external.
std::pair< EDetId, int > GlobalToLocal(int globId) const
Converts internal layer index to pair (detID, locID)
int fDetId
External index of detector subsystem.
double fZ
Reference z-coordinate of the component.