ATLAS Offline Software
Loading...
Searching...
No Matches
ModuleKeyHelper.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3 */
4#ifndef INDET_MODULEKEYHELPER_H
5#define INDET_MODULEKEYHELPER_H
6
7#include <array>
8#include <type_traits>
9
10namespace InDet {
11 namespace MaskUtils {
16 template<unsigned int bit, unsigned int end, typename T=unsigned int>
17 static consteval T createMask() {
18 if constexpr(bit>31u) {
19 return static_cast<T>(0u);
20 }
21 else if constexpr(bit>=end) {
22 return static_cast<T>(0u);
23 }
24 else {
25 return static_cast<T>(1u<<bit) | createMask<bit+1u,end, T>();
26 }
27 }
28 }
29
40 template <typename T, unsigned int T_ROW_BITS, unsigned int T_COL_BITS, unsigned int T_CHIP_BITS, unsigned int T_TYPE_BITS=0u>
42 static constexpr unsigned int ROW_BITS = T_ROW_BITS;
43 static constexpr unsigned int COL_BITS = T_COL_BITS;
44 static constexpr unsigned int CHIP_BITS = T_CHIP_BITS;
45 static constexpr unsigned int RANGE_FLAG_BITS = 1u;
46 static constexpr unsigned int TYPE_BITS = T_TYPE_BITS;
47 static constexpr T ROW_SHIFT = 0u;
48 static constexpr T COL_SHIFT = ROW_BITS;
49 static constexpr T CHIP_SHIFT = ROW_BITS + COL_BITS;
50 static constexpr T RANGE_FLAG_SHIFT = ROW_BITS + COL_BITS + CHIP_BITS;
57 using KEY_TYPE = T;
58
59 protected:
65 template <unsigned int SHIFT, T MASK>
66 static constexpr T makeKeyPart([[maybe_unused]] T val) {
67 if constexpr(MASK==0) {
68 return T{};
69 }
70 else {
71 assert (((val << SHIFT) & MASK) == (val << SHIFT));
72 return (val << SHIFT);
73 }
74 }
75
76 public:
85 static constexpr T makeKey(bool is_range, unsigned int chip, unsigned int col, unsigned int row=0u) {
86 return static_cast<T>(is_range) << RANGE_FLAG_SHIFT
90 }
91
94 //coverity[CONSTANT_EXPRESSION_RESULT]
95 static constexpr T getColumn(T key) { return (key & COL_MASK) >> COL_SHIFT; }
96
99 static constexpr T getRow(T key) { return (key & ROW_MASK) >> ROW_SHIFT; }
100
103 static constexpr T getLimitRowMax() { return ROW_MASK; }
104
107 static constexpr T getLimitColumnMax() { return COL_MASK; }
108
111 //coverity[CONSTANT_EXPRESSION_RESULT]
112 static constexpr T getChip(T key) { return (key & CHIP_MASK) >> CHIP_SHIFT; }
113
116 static constexpr T getDefectType(T key) {
117 if constexpr(TYPE_BITS>0) {
118 return (key & TYPE_MASK) >> TYPE_SHIFT;
119 }
120 else {
121 return T{};
122 }
123 }
124
127 static constexpr T getDefectTypeComponent(T key) {
128 if constexpr(TYPE_BITS>0) {
129 return key & TYPE_MASK;
130 }
131 else {
132 return T{};
133 }
134 }
135
138 static constexpr T makeDefectTypeKey(unsigned int defect_type)
139 {
140 if constexpr(TYPE_BITS>0) {
141 assert( (((defect_type << TYPE_SHIFT ) & TYPE_MASK) >> TYPE_SHIFT) == defect_type);
142 return (defect_type << TYPE_SHIFT ) & TYPE_MASK;
143 }
144 else {
145 return T{};
146 }
147 }
148
152 static constexpr bool isRangeKey(T key) {
153 if constexpr(TYPE_MASK) {
154 return ((key & RANGE_FLAG_MASK)>>RANGE_FLAG_SHIFT);
155 }
156 else {
157 return ((key>>RANGE_FLAG_SHIFT) );
158 }
159 }
160
164 static constexpr T makeRangeKey(T key) { return key | RANGE_FLAG_MASK; }
165
170 static constexpr T makeBaseKey(T key) { return key & (~(RANGE_FLAG_MASK|TYPE_MASK)); }
171
177 static constexpr std::pair<T, T> makeRangeForMask( T key, T mask) {
178 return std::make_pair( key & mask, (key | ((~mask) & (CHIP_MASK|COL_MASK|ROW_MASK))) );
179 }
180
186 static constexpr bool isMatchingDefect(T defect_key, T key) {
187 return (key == makeBaseKey(defect_key) || isRangeKey(defect_key));
188 }
189
190 };
191
192}
193#endif
static consteval T createMask()
Convenience method to create a mask for which exactly one contiguous sequence of bits is set to 1.
Primary Vertex Finder.
Helper class to create keys for defects described by chip, column and row indices,...
static constexpr T COL_SHIFT
static constexpr T makeBaseKey(T key)
Return the key with the range flag removed.
static constexpr unsigned int ROW_BITS
static constexpr T makeKey(bool is_range, unsigned int chip, unsigned int col, unsigned int row=0u)
Create a key from mask, chip, column and row indices.
static constexpr unsigned int TYPE_BITS
static constexpr bool isMatchingDefect(T defect_key, T key)
Convenience method to check whether the key matches the defect.
static constexpr T getRow(T key)
Get the row index from a full key.
static constexpr std::pair< T, T > makeRangeForMask(T key, T mask)
Return a key pair marking the beginning and the end of the range for the given mask and key.
static constexpr bool isRangeKey(T key)
Test whether a key is a range key.
static constexpr unsigned int CHIP_BITS
static constexpr T CHIP_MASK
static constexpr T ROW_MASK
static constexpr T getChip(T key)
Get the column index from a full key.
static constexpr T getColumn(T key)
Get the column index from a full key.
static constexpr T CHIP_SHIFT
static constexpr T RANGE_FLAG_SHIFT
static constexpr unsigned int COL_BITS
static constexpr T makeRangeKey(T key)
Turn a key into a range key.
static constexpr T makeDefectTypeKey(unsigned int defect_type)
Make the key component representing the an associated defect type.
static constexpr unsigned int RANGE_FLAG_BITS
static constexpr T TYPE_MASK
static constexpr T RANGE_FLAG_MASK
static constexpr T getLimitColumnMax()
Get the maximum row value.
static constexpr T ROW_SHIFT
static constexpr T COL_MASK
static constexpr T getDefectType(T key)
Get an associated defect type.
static constexpr T makeKeyPart(T val)
Convenience method to create part of a key.
static constexpr T getLimitRowMax()
Get the maximum row value.
static constexpr T getDefectTypeComponent(T key)
Get key component of an associated defect type.
static constexpr T TYPE_SHIFT