ATLAS Offline Software
ITkPixQCoreEncodingLUT.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ITKPIXV2QCOREENCODINGLUT_H
6 #define ITKPIXV2QCOREENCODINGLUT_H
7 #include <iostream>
8 #include <cstdint>
9 #include <array>
10 #include <cstddef>
11 #include <algorithm>
12 #include <ranges>
13 
14 namespace ITkPixEncoding{
15  constexpr std::size_t LookUpTableSize(1<<16);
16 
17 
18  template<std::size_t Length, typename Generator>
19  constexpr auto lut(Generator&& f){
20  using content_type = decltype(f(std::size_t{0}));
21  std::array<content_type, Length> arr;
22  using namespace std::ranges;
23  auto rangy = views::iota(std::size_t{0}, Length) // Generate a sequence
24  | views::transform(std::forward<Generator>(f)); // Transform using our generator
25  copy(rangy, arr.begin());
26  return arr;
27  }
28 
29  constexpr uint32_t
31  return v!=0;
32  }
33 
34  // nb. If we use std::array here instead of a C array,
35  // then compilation fails with gcc13 in the dbg build
36  // because the compile-time execution of encode() then takes
37  // too many steps.
38  constexpr void prepByte(uint32_t word, uint32_t b[8]){
39  for(int i = 0 ;i<8;i++) {
40  b[i] = ((word >> (2*i)) & 0x1) << 1 | ((word >> (2*i+1)) & 0x1);
41  }
42  }
43 
44  std::array<uint32_t, 8> prepByte(uint32_t word)
45  {
46  std::array<uint32_t, 8> b;
47  prepByte(word, b.data());
48  return b;
49  }
50 
51 
52  //QCore encoding function, taken from Matthias Wittgen and Carlos
53  uint32_t
54  constexpr encode(uint32_t decoded, uint32_t &encoded) {
55  uint32_t b[8];
56  prepByte(decoded, b);
57  //
58  uint32_t S1 = (one_bit(b[0] | b[1] | b[2] | b[3]) << 1) | one_bit(b[4] | b[5] | b[6] | b[7]);
59  uint32_t S2t = (one_bit(b[0] | b[1]) << 1) | one_bit(b[2] | b[3]);
60  uint32_t S2b = (one_bit(b[4] | b[5]) << 1) | one_bit(b[6] | b[7]);
61  uint32_t S3tl = (one_bit(b[0]) << 1) | one_bit(b[1]);
62  uint32_t S3tr = (one_bit(b[2]) << 1) | one_bit(b[3]);
63  uint32_t S3bl = (one_bit(b[4]) << 1) | one_bit(b[5]);
64  uint32_t S3br = (one_bit(b[6]) << 1) | one_bit(b[7]);
65 
66  uint32_t pos = 0;
67  encoded = 0;
68 
69  auto writeTwo = [&](uint32_t src) {
70  if (src == 0b01) {
71  encoded |= (0b0) << (28 - pos);
72  pos++;
73  } else {
74  encoded |= (src & 0x3) << (28 - pos);
75  pos += 2;
76  }
77  };
78  for(auto & val:{
79  S1, S2t, S3tl, S3tr, b[0], b[1],b[2], b[3],
80  S2b, S3bl, S3br,b[4], b[5], b[6], b[7]
81  }) if(val) writeTwo(val);
82  return pos;
83  }
84 
85  //Create LUTs - if argument is true, return length LUT, if it's false return the encoded QCore LUT.
86  //It could be prettified / optimized to fill in both LUTs in one go, but happens once per job and takes O(ms)
87  //so no real gain there.
88  inline auto create_lut_encode(bool length = false) {
89  std::array<uint32_t, LookUpTableSize> lut;
90  lut[0] = 0;
91  for (uint32_t i = 1; i < 1 << 16; i++) {
92  uint32_t encoded;
93  uint64_t len = encode(i, encoded);
94  lut[i] = length ? len : encoded;
95  }
96  return lut;
97  }
98 
99  //It could be prettified / optimized to fill in both LUTs in one go, but happens once per job and takes O(ms)
100  //so no real gain there.
101  template<std::size_t Length>
102  constexpr auto LutLen = lut<Length>([](uint32_t i){
103  uint32_t encoded;
104  return encode(i, encoded);
105  });
106 
107  template<std::size_t Length>
108  constexpr auto LutBTree = lut<Length>([](uint32_t i){
109  uint32_t encoded;
110  encode(i, encoded);
111  return encoded;
112  });
113 
114 
115  constexpr std::array<uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Tree = LutBTree<LookUpTableSize>;
116  constexpr std::array<uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Length = LutLen<LookUpTableSize>;
117 
118 }
119 #endif
ITkPixEncoding::ITkPixV2QCoreEncodingLUT_Length
constexpr std::array< uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Length
Definition: ITkPixQCoreEncodingLUT.h:116
plotBeamSpotCompare.x1
x1
Definition: plotBeamSpotCompare.py:216
trigbs_pickEvents.ranges
ranges
Definition: trigbs_pickEvents.py:60
S1
struct TBPatternUnitContext S1
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
WriteCellNoiseToCool.src
src
Definition: WriteCellNoiseToCool.py:513
ITkPixEncoding::encode
constexpr uint32_t encode(uint32_t decoded, uint32_t &encoded)
Definition: ITkPixQCoreEncodingLUT.h:54
ITkPixEncoding::LutLen
constexpr auto LutLen
Definition: ITkPixQCoreEncodingLUT.h:102
ITkPixEncoding::LutBTree
constexpr auto LutBTree
Definition: ITkPixQCoreEncodingLUT.h:108
ITkPixEncoding::lut
constexpr auto lut(Generator &&f)
Definition: ITkPixQCoreEncodingLUT.h:19
ITkPixEncoding
Definition: ITkPixQCoreEncodingLUT.h:14
ITkPixEncoding::create_lut_encode
auto create_lut_encode(bool length=false)
Definition: ITkPixQCoreEncodingLUT.h:88
lumiFormat.i
int i
Definition: lumiFormat.py:85
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
hist_file_dump.f
f
Definition: hist_file_dump.py:135
ITkPixEncoding::one_bit
constexpr uint32_t one_bit(uint32_t v)
Definition: ITkPixQCoreEncodingLUT.h:30
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
ITkPixEncoding::LookUpTableSize
constexpr std::size_t LookUpTableSize(1<< 16)
python.PyAthena.v
v
Definition: PyAthena.py:154
ITkPixEncoding::prepByte
constexpr void prepByte(uint32_t word, uint32_t b[8])
Definition: ITkPixQCoreEncodingLUT.h:38
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
calibdata.copy
bool copy
Definition: calibdata.py:27
length
double length(const pvec &v)
Definition: FPGATrackSimLLPDoubletHoughTransformTool.cxx:26
ITkPixEncoding::ITkPixV2QCoreEncodingLUT_Tree
constexpr std::array< uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Tree
Definition: ITkPixQCoreEncodingLUT.h:115