ATLAS Offline Software
Loading...
Searching...
No Matches
ModuleKeyHelper.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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 static constexpr T getColumn(T key) { return (key & COL_MASK) >> COL_SHIFT; }
95
98 static constexpr T getRow(T key) { return (key & ROW_MASK) >> ROW_SHIFT; }
99
102 static constexpr T getLimitRowMax() { return ROW_MASK; }
103
106 static constexpr T getLimitColumnMax() { return COL_MASK; }
107
110 static constexpr T getChip(T key) { return (key & CHIP_MASK) >> CHIP_SHIFT; }
111
114 static constexpr T getDefectType(T key) {
115 if constexpr(TYPE_BITS>0) {
116 return (key & TYPE_MASK) >> TYPE_SHIFT;
117 }
118 else {
119 return T{};
120 }
121 }
122
125 static constexpr T getDefectTypeComponent(T key) {
126 if constexpr(TYPE_BITS>0) {
127 return key & TYPE_MASK;
128 }
129 else {
130 return T{};
131 }
132 }
133
136 static constexpr T makeDefectTypeKey(unsigned int defect_type)
137 {
138 if constexpr(TYPE_BITS>0) {
139 assert( (((defect_type << TYPE_SHIFT ) & TYPE_MASK) >> TYPE_SHIFT) == defect_type);
140 return (defect_type << TYPE_SHIFT ) & TYPE_MASK;
141 }
142 else {
143 return T{};
144 }
145 }
146
150 static constexpr bool isRangeKey(T key) {
151 if constexpr(TYPE_MASK) {
152 return ((key & RANGE_FLAG_MASK)>>RANGE_FLAG_SHIFT);
153 }
154 else {
155 return ((key>>RANGE_FLAG_SHIFT) );
156 }
157 }
158
162 static constexpr T makeRangeKey(T key) { return key | RANGE_FLAG_MASK; }
163
168 static constexpr T makeBaseKey(T key) { return key & (~(RANGE_FLAG_MASK|TYPE_MASK)); }
169
175 static constexpr std::pair<T, T> makeRangeForMask( T key, T mask) {
176 return std::make_pair( key & mask, (key | ((~mask) & (CHIP_MASK|COL_MASK|ROW_MASK))) );
177 }
178
184 static constexpr bool isMatchingDefect(T defect_key, T key) {
185 return (key == makeBaseKey(defect_key) || isRangeKey(defect_key));
186 }
187
188 };
189
190}
191#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