ATLAS Offline Software
ITkPixEncoder.cxx
Go to the documentation of this file.
1 /*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 /*
5 * Author: Ondra Kovanda, ondrej.kovanda at cern.ch
6 * Date: 03/2024
7 * Description: ITkPix* encoding base class
8 */
9 
10 #include "ITkPixEncoder.h"
11 #include "ITkPixQCoreEncodingLUT.h"
12 
13 //Constructor sets up the geometry for all future loops
14 
15 ITkPixEncoder::ITkPixEncoder(const unsigned nCol, const unsigned nRow, const unsigned nColInCCol,
16  const unsigned nRowInQRow, const unsigned nEventsPerStream, const bool plainHitMap,
17  const bool dropToT): m_nCol(nCol/nColInCCol), m_nRow(nRow/nRowInQRow),
18  m_nColInCCol(nColInCCol), m_nRowInQRow(nRowInQRow), m_nEventsPerStream(nEventsPerStream),
19  m_plainHitMap(plainHitMap), m_dropToT(dropToT){
20  //nop
21 }
22 
24  //This adds 'length' lowest bits to the current block. If the current
25  //block gets filled, push it to the output and start a new block with
26  //the rest of the bits that didn't make it. Need to keep track of the
27  //remaining space in the current word. Also, we only have 63 bits for
28  //the added data, as the first bit is EoS.
29  //Case 1: there's enough space for the entire information to be added
30  if (length <= (63 - m_currBit)){
31 
32  //The position at which the new bits should be inserted into the block
33  //is (length of the block - 1) - (currently last bit) - (length)
34  //We need to keep the first bit for EoS, hence the -1
35  m_currBlock |= (value << (63 - m_currBit - length));
36  m_currBit += length;
37  return;
38  }
39 
40  //Case 2: there's not enough space, so we put what we can, push the block
41  //to the output, and the rest to a new block
42  else {
43 
44  //How much space do we have?
45  uint8_t remainingBits = 63 - m_currBit;
46 
47  //Add that many bits
48  m_currBlock |= ((value >> (length - remainingBits)));
49 
50  //Push the current block to the output and reset it and the current
51  //bit counter. The block needs to be split in 32-bit halves before the output
52  pushWords32();
53 
54  //What are we left with?
55  uint8_t leftoverBits = length - remainingBits;
56 
57  //Now we can add the remainder
58  //Make sure that the remainder we're adding is really only the bits we've not yet
59  //added, otherwise there can be a rogue "1" in the EoS bit, which was already added
60  //in the previous block
61  addBits64((value & (0xFFFFFFFFFFFFFFFF >> (64 - leftoverBits))), leftoverBits);
62 
63  }
64 }
65 
67  //whenever the current block is ready for output,
68  //split it into two 32-bit words and push them to
69  //the output container. Reset the current bloc/bit
70  uint32_t word1 = m_currBlock >> 32;
71  uint32_t word2 = m_currBlock & 0xFFFFFFFF;
72  m_words.push_back(word1);
73  m_words.push_back(word2);
74  m_currBlock = 0x0ULL;
75  m_currBit = 0;
76 }
77 
78 void ITkPixEncoder::encodeQCore(const unsigned nCCol, const unsigned nQRow){
79  //produce hit map and ToTs
80  //First, get the top-left pixel in the QCore
81  unsigned col = nCCol * m_nColInCCol;
82  unsigned row = nQRow * m_nRowInQRow;
83 
84  //now loop, store ToTs, and build index of the
85  //compressed hit map in the LUT
86  uint16_t lutIndex = 0x0000;
87  std::vector<uint16_t> tots;
88  tots.reserve(16);
89  int pix = 0;
90  for (unsigned pixRow = row; pixRow < row + m_nRowInQRow; pixRow++){
91  for (unsigned pixCol = col; pixCol < col + m_nColInCCol; pixCol++){
92  if (m_hitMap(pixCol, pixRow)){
93  lutIndex |= 0x1 << pix;
94  tots.push_back(m_hitMap(pixCol, pixRow) - 1);
95  }
96  pix++;
97  }
98  }
99 
100  //now add the binary-tree encoded & compressed map
101  //from the LUT to the stream. If, instead, the plain
102  //hit map is requested, add the index (which is the
103  //plain hit map in fact)
104 
106 
107  //if dropToT is requested, we can return here
108  if (m_dropToT) return;
109 
110  //and add the four-bit ToT information for each hit
111  for (auto& tot : tots){
112  addBits64(tot, 4);
113  }
114 }
115 
116 bool ITkPixEncoder::hitInQCore(const unsigned CCol, const unsigned QRow){
117  //Was there a hit in this QCore?
118 
119  unsigned col = CCol * m_nColInCCol;
120  unsigned row = QRow * m_nRowInQRow;
121 
122  for (unsigned pixRow = row; pixRow < row + m_nRowInQRow; pixRow++){
123  for (unsigned pixCol = col; pixCol < col + m_nColInCCol; pixCol++){
124  if (m_hitMap(pixCol, pixRow)) return true;
125  }
126  }
127 
128  return false;
129 }
130 
132  //Fill in a helper map of hit QCores and a vector of last qrow in each ccol
133  m_hitQCores = std::vector<std::vector<bool>>(m_nCCol, std::vector<bool>(m_nQRow, false));
134  m_lastQRow = std::vector<unsigned> (m_nCCol, 0);
135 
136  for (unsigned CCol = 0; CCol < m_nCCol; CCol++){
137  for (unsigned QRow = 0; QRow < m_nQRow; QRow++){
138  //if there's a hit in the qcore, flag the helper map
139  m_hitQCores[CCol][QRow] = hitInQCore(CCol, QRow);
140 
141  //and keep track of the last qrow in each CCol, so that we can
142  //easily set the isLast bit. Numbering starts at 1, in order to
143  //keep the m_lastQRow[CCol] == 0 case denoting "no hit" in the CCol
144  //to save some looping/ifs later on
145  if (m_hitQCores[CCol][QRow]) m_lastQRow[CCol] = QRow + 1;
146  }
147  }
148 
149 }
150 
152  //This produces the bits for one event.
153  //First, scan the map and produce helpers
154  scanHitMap();
155 
156  for (unsigned CCol = 0; CCol < m_nCCol; CCol++){
157  //if there are no hits in this CCol, continue
158  if (m_lastQRow[CCol] == 0) continue;
159  //add the 6-bit (CCol + 1) address
160  addBits64(CCol + 1, 6);
161 
162  int previousQRow = -666;
163  for (unsigned QRow = 0; QRow < m_nQRow; QRow++){
164  //if there's no hit in this row, continue
165  if (!m_hitQCores[CCol][QRow]) continue;
166 
167  //add the isLast bit
168  QRow + 1 == m_lastQRow[CCol] ? addBits64(0x1, 1) : addBits64(0x0, 1);
169 
170  //add the isNeighbor bit. If false, add the QRow address as well.
171  if (QRow == (uint)previousQRow + 1){
172  addBits64(0x1, 1);
173  }
174  else {
175  addBits64(0x0, 1);
176  addBits64(QRow, 8);
177  };
178 
179  //add the map and ToT
180  encodeQCore(CCol, QRow);
181 
182  //update the previous QRow
183  previousQRow = QRow;
184  }
185  }
186 }
187 
188 void ITkPixEncoder::streamTag(const uint8_t nStream){
189  //this adds the 8-bit 'global' stream tag
190  addBits64(nStream, 8);
191 }
192 
194  //this adds 11 bits of interal tagging between events.
195  //does the tag always need to start with 111?
196  uint16_t tag = nEvt | (0b111 << 8);
197  addBits64(tag, 11);
198 }
ITkPixEncoding::ITkPixV2QCoreEncodingLUT_Length
constexpr std::array< uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Length
Definition: ITkPixQCoreEncodingLUT.h:116
ITkPixEncoder::streamTag
void streamTag(const uint8_t nStream)
Definition: ITkPixEncoder.cxx:188
query_example.row
row
Definition: query_example.py:24
ITkPixEncoder.h
plotBeamSpotCompare.x1
x1
Definition: plotBeamSpotCompare.py:216
ITkPixEncoder::m_lastQRow
std::vector< unsigned > m_lastQRow
Definition: ITkPixEncoder.h:63
xAOD::word1
word1
Definition: eFexEMRoI_v1.cxx:82
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:557
ITkPixEncoder::m_hitMap
HitMap m_hitMap
Definition: ITkPixEncoder.h:69
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
athena.value
value
Definition: athena.py:124
ITkPixEncoder::m_hitQCores
std::vector< std::vector< bool > > m_hitQCores
Definition: ITkPixEncoder.h:62
uint
unsigned int uint
Definition: LArOFPhaseFill.cxx:20
ITkPixEncoder::m_dropToT
bool m_dropToT
Definition: ITkPixEncoder.h:66
ITkPixEncoder::m_nRowInQRow
unsigned m_nRowInQRow
Definition: ITkPixEncoder.h:52
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
ITkPixEncoder::m_words
std::vector< uint32_t > m_words
Definition: ITkPixEncoder.h:55
ITkPixEncoder::m_nColInCCol
unsigned m_nColInCCol
Definition: ITkPixEncoder.h:52
ITkPixEncoder::hitInQCore
bool hitInQCore(const unsigned CCol, const unsigned QRow)
Definition: ITkPixEncoder.cxx:116
ITkPixEncoder::intTag
void intTag(const uint16_t nEvt)
Definition: ITkPixEncoder.cxx:193
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
ITkPixEncoder::m_plainHitMap
bool m_plainHitMap
Definition: ITkPixEncoder.h:66
ITkPixEncoder::m_currBit
uint8_t m_currBit
Definition: ITkPixEncoder.h:61
ITkPixEncoder::ITkPixEncoder
ITkPixEncoder(const unsigned nCol=400, const unsigned nRow=384, const unsigned nColInCCol=8, const unsigned nRowInQRow=2, const unsigned nEventsPerStream=16, const bool plainHitMap=false, const bool dropToT=false)
Definition: ITkPixEncoder.cxx:15
LB_AnalMapSplitter.tot
tot
Definition: LB_AnalMapSplitter.py:46
ITkPixQCoreEncodingLUT.h
ITkPixEncoder::m_nCCol
unsigned m_nCCol
Definition: ITkPixEncoder.h:52
ITkPixEncoder::pushWords32
void pushWords32()
Definition: ITkPixEncoder.cxx:66
ITkPixEncoder::m_currBlock
uint64_t m_currBlock
Definition: ITkPixEncoder.h:60
query_example.col
col
Definition: query_example.py:7
ITkPixEncoder::encodeQCore
void encodeQCore(const unsigned nCCol, const unsigned nQRow)
Definition: ITkPixEncoder.cxx:78
ITkPixEncoder::addBits64
void addBits64(const uint64_t value, const uint8_t length)
Definition: ITkPixEncoder.cxx:23
ITkPixEncoder::encodeEvent
void encodeEvent()
Definition: ITkPixEncoder.cxx:151
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
ITkPixEncoder::m_nQRow
unsigned m_nQRow
Definition: ITkPixEncoder.h:52
pix
Definition: PixelMapping.cxx:16
length
double length(const pvec &v)
Definition: FPGATrackSimLLPDoubletHoughTransformTool.cxx:26
ITkPixEncoder::scanHitMap
void scanHitMap()
Definition: ITkPixEncoder.cxx:131
ITkPixEncoding::ITkPixV2QCoreEncodingLUT_Tree
constexpr std::array< uint32_t, LookUpTableSize > ITkPixV2QCoreEncodingLUT_Tree
Definition: ITkPixQCoreEncodingLUT.h:115