ATLAS Offline Software
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 
10 namespace 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>
41  struct ModuleKeyHelper {
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;
52  static constexpr T ROW_MASK = MaskUtils::createMask<0, ROW_BITS>();
53  static constexpr T COL_MASK = MaskUtils::createMask<ROW_BITS, ROW_BITS+COL_BITS>();
54  static constexpr T CHIP_MASK = MaskUtils::createMask<ROW_BITS+COL_BITS, ROW_BITS+COL_BITS+CHIP_BITS>();
55  static constexpr T RANGE_FLAG_MASK = MaskUtils::createMask<ROW_BITS+COL_BITS+CHIP_BITS,ROW_BITS+COL_BITS+CHIP_BITS+RANGE_FLAG_BITS>();
56  static constexpr T TYPE_MASK = MaskUtils::createMask<TYPE_SHIFT,TYPE_SHIFT+TYPE_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
87  | makeKeyPart<CHIP_SHIFT,CHIP_MASK>(chip)
88  | makeKeyPart<COL_SHIFT,COL_MASK>(col)
89  | makeKeyPart<ROW_SHIFT,ROW_MASK>(row);
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
InDet::ModuleKeyHelper::getColumn
static constexpr T getColumn(T key)
Get the column index from a full key.
Definition: ModuleKeyHelper.h:94
InDet::ModuleKeyHelper::getRow
static constexpr T getRow(T key)
Get the row index from a full key.
Definition: ModuleKeyHelper.h:98
InDet::ModuleKeyHelper::makeRangeForMask
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.
Definition: ModuleKeyHelper.h:175
InDet::ModuleKeyHelper::ROW_BITS
static constexpr unsigned int ROW_BITS
Definition: ModuleKeyHelper.h:42
InDet::ModuleKeyHelper::makeRangeKey
static constexpr T makeRangeKey(T key)
Turn a key into a range key.
Definition: ModuleKeyHelper.h:162
InDet::ModuleKeyHelper::makeDefectTypeKey
static constexpr T makeDefectTypeKey(unsigned int defect_type)
Make the key component representing the an associated defect type.
Definition: ModuleKeyHelper.h:136
InDet::ModuleKeyHelper::CHIP_MASK
static constexpr T CHIP_MASK
Definition: ModuleKeyHelper.h:54
InDet::ModuleKeyHelper::getDefectType
static constexpr T getDefectType(T key)
Get an associated defect type.
Definition: ModuleKeyHelper.h:114
InDet
Primary Vertex Finder.
Definition: VP1ErrorUtils.h:36
InDet::ModuleKeyHelper::RANGE_FLAG_BITS
static constexpr unsigned int RANGE_FLAG_BITS
Definition: ModuleKeyHelper.h:45
InDet::ModuleKeyHelper::COL_BITS
static constexpr unsigned int COL_BITS
Definition: ModuleKeyHelper.h:43
InDet::ModuleKeyHelper::RANGE_FLAG_MASK
static constexpr T RANGE_FLAG_MASK
Definition: ModuleKeyHelper.h:55
InDet::ModuleKeyHelper::TYPE_BITS
static constexpr unsigned int TYPE_BITS
Definition: ModuleKeyHelper.h:46
keylayer_zslicemap.row
row
Definition: keylayer_zslicemap.py:155
InDet::ModuleKeyHelper::getLimitColumnMax
static constexpr T getLimitColumnMax()
Get the maximum row value.
Definition: ModuleKeyHelper.h:106
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:459
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:92
InDet::ModuleKeyHelper::CHIP_SHIFT
static constexpr T CHIP_SHIFT
Definition: ModuleKeyHelper.h:49
InDet::ModuleKeyHelper< unsigned short, 12, 0, 0 >::KEY_TYPE
unsigned short KEY_TYPE
Definition: ModuleKeyHelper.h:57
InDet::ModuleKeyHelper::getDefectTypeComponent
static constexpr T getDefectTypeComponent(T key)
Get key component of an associated defect type.
Definition: ModuleKeyHelper.h:125
InDet::ModuleKeyHelper::makeBaseKey
static constexpr T makeBaseKey(T key)
Return the key with the range flag removed.
Definition: ModuleKeyHelper.h:168
InDet::ModuleKeyHelper::COL_SHIFT
static constexpr T COL_SHIFT
Definition: ModuleKeyHelper.h:48
InDet::ModuleKeyHelper::ROW_MASK
static constexpr T ROW_MASK
Definition: ModuleKeyHelper.h:52
InDet::ModuleKeyHelper::getChip
static constexpr T getChip(T key)
Get the column index from a full key.
Definition: ModuleKeyHelper.h:110
InDet::ModuleKeyHelper::makeKeyPart
static constexpr T makeKeyPart([[maybe_unused]] T val)
Convenience method to create part of a key.
Definition: ModuleKeyHelper.h:66
InDet::ModuleKeyHelper::TYPE_MASK
static constexpr T TYPE_MASK
Definition: ModuleKeyHelper.h:56
InDet::ModuleKeyHelper::COL_MASK
static constexpr T COL_MASK
Definition: ModuleKeyHelper.h:53
InDet::ModuleKeyHelper::CHIP_BITS
static constexpr unsigned int CHIP_BITS
Definition: ModuleKeyHelper.h:44
InDet::ModuleKeyHelper::ROW_SHIFT
static constexpr T ROW_SHIFT
Definition: ModuleKeyHelper.h:47
InDet::ModuleKeyHelper::makeKey
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.
Definition: ModuleKeyHelper.h:85
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
InDet::ModuleKeyHelper::TYPE_SHIFT
static constexpr T TYPE_SHIFT
Definition: ModuleKeyHelper.h:51
InDet::ModuleKeyHelper::isMatchingDefect
static constexpr bool isMatchingDefect(T defect_key, T key)
Convenience method to check whether the key matches the defect.
Definition: ModuleKeyHelper.h:184
InDet::ModuleKeyHelper
Helper class to create keys for defects described by chip, column and row indices,...
Definition: ModuleKeyHelper.h:41
InDet::ModuleKeyHelper::RANGE_FLAG_SHIFT
static constexpr T RANGE_FLAG_SHIFT
Definition: ModuleKeyHelper.h:50
InDet::ModuleKeyHelper::getLimitRowMax
static constexpr T getLimitRowMax()
Get the maximum row value.
Definition: ModuleKeyHelper.h:102
InDet::ModuleKeyHelper::isRangeKey
static constexpr bool isRangeKey(T key)
Test whether a key is a range key.
Definition: ModuleKeyHelper.h:150
RoiUtil::MASK
MASK
Definition: RoiSerialise.cxx:35
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37