Project
Loading...
Searching...
No Matches
ElectronicMapperImplHelper.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef O2_MCH_RAW_ELECMAP_ELECTRONIC_MAPPER_IMPL_HELPER_H
13#define O2_MCH_RAW_ELECMAP_ELECTRONIC_MAPPER_IMPL_HELPER_H
14
20#include "dslist.h"
21#include <cstdint>
22#include <fmt/format.h>
23#include <functional>
24#include <iostream>
25#include <map>
26#include <optional>
27#include <set>
28
29namespace o2::mch::raw::impl
30{
31template <typename T>
32std::function<std::optional<o2::mch::raw::DsDetId>(o2::mch::raw::DsElecId)>
33 mapperElec2Det(const std::map<uint32_t, uint32_t>& elec2det)
34{
35 return [elec2det](o2::mch::raw::DsElecId id) -> std::optional<o2::mch::raw::DsDetId> {
36 auto it = elec2det.find(encode(id));
37 if (it == elec2det.end()) {
38 return std::nullopt;
39 }
40 return o2::mch::raw::decodeDsDetId(it->second);
41 };
42}
43
44template <typename T>
45std::function<std::optional<o2::mch::raw::DsElecId>(o2::mch::raw::DsDetId)>
46 mapperDet2Elec(const std::map<uint32_t, uint32_t>& det2elec)
47{
48 return [det2elec](o2::mch::raw::DsDetId id) -> std::optional<o2::mch::raw::DsElecId> {
49 auto it = det2elec.find(encode(id));
50 if (it == det2elec.end()) {
51 return std::nullopt;
52 }
53 return o2::mch::raw::decodeDsElecId(it->second);
54 };
55}
56
57template <typename T>
58std::function<std::optional<FeeLinkId>(uint16_t)>
59 mapperSolar2FeeLink(const std::map<uint16_t, uint32_t>& solar2cruLink)
60{
61 return [solar2cruLink](uint16_t solarId) -> std::optional<FeeLinkId> {
62 auto it = solar2cruLink.find(solarId);
63 if (it == solar2cruLink.end()) {
64 return std::nullopt;
65 }
66 return decodeFeeLinkId(it->second);
67 };
68}
69
70template <typename T>
71std::function<std::optional<uint16_t>(FeeLinkId)>
72 mapperFeeLink2Solar(const std::map<uint32_t, uint16_t>& cruLink2solar)
73{
74 return [cruLink2solar](o2::mch::raw::FeeLinkId id) -> std::optional<uint16_t> {
75 auto it = cruLink2solar.find(encode(id));
76 if (it == cruLink2solar.end()) {
77 return std::nullopt;
78 }
79 return it->second;
80 };
81}
82
83template <typename KEY, typename VALUE>
84std::map<VALUE, KEY> inverseMap(const std::map<KEY, VALUE>& src)
85{
86 std::map<VALUE, KEY> dest;
87 for (auto p : src) {
88 dest.emplace(p.second, p.first);
89 }
90 return dest;
91}
92
93template <typename T>
94std::set<uint16_t> getSolarUIDs(int deid)
95{
96 static auto d2e = o2::mch::raw::createDet2ElecMapper<T>();
97 std::set<uint16_t> solarsForDE;
98 static auto dslist = createDualSampaMapper();
99 for (auto dsid : dslist(deid)) {
100 DsDetId id{static_cast<uint16_t>(deid), static_cast<uint16_t>(dsid)};
101 auto dsel = d2e(id);
102 if (dsel.has_value()) {
103 solarsForDE.insert(dsel->solarId());
104 }
105 }
106 return solarsForDE;
107}
108
109template <typename T>
110std::set<uint16_t> getSolarUIDs()
111{
112 std::set<uint16_t> solarUIDs;
113
114 for (auto deid : o2::mch::constants::deIdsForAllMCH) {
115 std::set<uint16_t> solarsForDE = getSolarUIDs<T>(deid);
116 for (auto s : solarsForDE) {
117 solarUIDs.insert(s);
118 }
119 }
120 return solarUIDs;
121}
122
123template <typename T>
124std::set<uint16_t> getSolarUIDsPerFeeId(uint16_t feeid)
125{
126 std::set<uint16_t> solars;
127 static auto feeLink2Solar = createFeeLink2SolarMapper<T>();
128 for (uint8_t link = 0; link < 12; link++) {
129 auto solar = feeLink2Solar(FeeLinkId{feeid, link});
130 if (solar.has_value()) {
131 solars.insert(solar.value());
132 }
133 }
134 return solars;
135}
136
137template <typename T>
138std::set<DsDetId> getDualSampasPerFeeId(uint16_t feeId)
139{
140 auto solarIds = getSolarUIDsPerFeeId<T>(feeId);
141 int n{0};
142 std::set<DsDetId> allDualSampas;
143 for (auto solarId : solarIds) {
144 auto solarDualSampas = getDualSampas<T>(solarId);
145 allDualSampas.insert(solarDualSampas.begin(), solarDualSampas.end());
146 }
147 return allDualSampas;
148}
149
150template <typename T>
151std::vector<std::string> solar2FeeLinkConsistencyCheck()
152{
153 std::vector<std::string> errors;
154
155 // All solars must have a FeeLinkId
156 std::set<uint16_t> solarIds = getSolarUIDs<T>();
157 static auto solar2feeLink = createSolar2FeeLinkMapper<T>();
158 std::vector<o2::mch::raw::FeeLinkId> feeLinkIds;
159 for (auto s : solarIds) {
160 auto p = solar2feeLink(s);
161 if (!p.has_value()) {
162 errors.push_back(fmt::format("Got no feelinkId for solarId {}", s));
163 } else {
164 feeLinkIds.push_back(p.value());
165 }
166 }
167
168 // All FeeLinkId must have a SolarId
169 auto feeLinkId2SolarId = createFeeLink2SolarMapper<T>();
170 for (auto f : feeLinkIds) {
171 auto p = feeLinkId2SolarId(f);
172 if (!p.has_value()) {
173 errors.push_back(fmt::format("Got no solarId for FeeLinkId {}", asString(f)));
174 }
175 }
176 return errors;
177}
178
179template <typename T>
180std::set<DsElecId> getAllDs()
181{
182 std::set<DsElecId> dsElecIds;
183
184 static auto dslist = createDualSampaMapper();
185 static auto det2ElecMapper = createDet2ElecMapper<T>();
186
187 for (auto deId : o2::mch::constants::deIdsForAllMCH) {
188 for (auto dsId : dslist(deId)) {
189 auto dsElecId = det2ElecMapper(DsDetId{deId, dsId});
190 if (dsElecId.has_value()) {
191 dsElecIds.insert(dsElecId.value());
192 }
193 }
194 }
195
196 return dsElecIds;
197}
198
199template <typename T>
200std::set<DsDetId> getDualSampas(uint16_t solarId)
201{
202 std::set<DsDetId> dualSampas;
203
204 static auto elec2det = o2::mch::raw::createElec2DetMapper<T>();
205 for (uint8_t group = 0; group < 8; group++) {
206 for (uint8_t index = 0; index < 5; index++) {
207 DsElecId dsElecId{solarId, group, index};
208 auto dsDetId = elec2det(dsElecId);
209 if (dsDetId.has_value()) {
210 dualSampas.insert(dsDetId.value());
211 }
212 }
213 }
214 return dualSampas;
215}
216
217template <typename T>
218std::map<uint16_t, uint16_t> generateSolarIndex2IdMap()
219{
220 std::set<uint16_t> solarIds = raw::getSolarUIDs<T>();
221 std::map<uint16_t, uint16_t> m;
222 uint16_t i{0};
223 for (const auto& solarId : solarIds) {
224 m[i] = solarId;
225 ++i;
226 }
227 return m;
228}
229
230template <typename T>
231std::optional<uint16_t> solarIndex2Id(uint16_t solarIndex)
232{
233 static std::map<uint16_t, uint16_t> ix2id = generateSolarIndex2IdMap<T>();
234 auto it = ix2id.find(solarIndex);
235 if (it == ix2id.end()) {
236 return std::nullopt;
237 }
238 return it->second;
239}
240
241template <typename T>
242std::optional<uint16_t> solarId2Index(uint16_t solarId)
243{
244 static std::map<uint16_t, uint16_t> id2ix =
245 inverseMap(generateSolarIndex2IdMap<T>());
246 auto it = id2ix.find(solarId);
247 if (it == id2ix.end()) {
248 return std::nullopt;
249 }
250 return it->second;
251}
252
253} // namespace o2::mch::raw::impl
254
255#endif
int32_t i
A DsDetId is just a pair (detection element id, dual sampa id)
Definition DsDetId.h:22
std::function< std::set< int >(int deId)> createDualSampaMapper()
Definition dslist.cxx:15
GLdouble n
Definition glcorearb.h:1982
const GLfloat * m
Definition glcorearb.h:4066
GLenum src
Definition glcorearb.h:1767
GLuint index
Definition glcorearb.h:781
GLdouble f
Definition glcorearb.h:310
GLboolean GLuint group
Definition glcorearb.h:3991
GLuint id
Definition glcorearb.h:650
std::array< int, 156 > deIdsForAllMCH
std::set< DsDetId > getDualSampasPerFeeId(uint16_t feeId)
std::function< std::optional< o2::mch::raw::DsDetId >(o2::mch::raw::DsElecId)> mapperElec2Det(const std::map< uint32_t, uint32_t > &elec2det)
std::function< std::optional< FeeLinkId >(uint16_t)> mapperSolar2FeeLink(const std::map< uint16_t, uint32_t > &solar2cruLink)
std::function< std::optional< o2::mch::raw::DsElecId >(o2::mch::raw::DsDetId)> mapperDet2Elec(const std::map< uint32_t, uint32_t > &det2elec)
std::optional< uint16_t > solarIndex2Id(uint16_t solarIndex)
std::set< DsElecId > getAllDs()
std::set< uint16_t > getSolarUIDsPerFeeId(uint16_t feeid)
std::vector< std::string > solar2FeeLinkConsistencyCheck()
std::set< uint16_t > getSolarUIDs()
std::set< DsDetId > getDualSampas(uint16_t solarId)
std::optional< uint16_t > solarId2Index(uint16_t solarId)
std::map< uint16_t, uint16_t > generateSolarIndex2IdMap()
std::map< VALUE, KEY > inverseMap(const std::map< KEY, VALUE > &src)
std::function< std::optional< uint16_t >(FeeLinkId)> mapperFeeLink2Solar(const std::map< uint32_t, uint16_t > &cruLink2solar)
FeeLinkId decodeFeeLinkId(uint32_t code)
Definition FeeLinkId.cxx:30
std::string asString(const SampaCluster &sc)
std::optional< DsElecId > decodeDsElecId(uint32_t code)
Definition DsElecId.cxx:37
DsDetId decodeDsDetId(uint32_t code)
Create a DsDetId object from a integer code.
Definition DsDetId.cxx:33
uint32_t encode(const DsDetId &id)
Create an integer code for the given id.
Definition DsDetId.cxx:28
auto dslist