CMS 3D CMS Logo

Block.cc
Go to the documentation of this file.
1 #include <iomanip>
2 
4 
6 
7 #define EDM_ML_DEBUG 1
8 
9 namespace l1t {
10 
11  const std::vector<unsigned int>
13  {
14  // The "0b" prefix indicates binary; the block header id is stored in decimal.
15  // Bits are the left-most bit (D15) of every 16-bit word in the format document.
16  // Bottom-to-top in the document maps to left-to-right in each of the block_patterns_
17  0b000111111111, // Event Record Header : block->header().getID() = 511
18  // Left-most bits of 0xA and 0x9 are both 1 in binary
19  0b0010, // Block of Counters : block->header().getID() = 2
20  0b0011, // ME Data Record : block->header().getID() = 3
21  0b0100, // RPC Data Record : block->header().getID() = 4
22  0b01100101, // SP Output Data Record : block->header().getID() = 101
23  0b11111111 // Event Record Trailer : block->header().getID() = 255
24  // Left-most bits of 0xF and 0xE are both 1 in binary
25  };
26 
27  uint32_t
29  {
30  if (type_ == MP7) {
31  LogTrace("L1T") << "Writing MP7 link header";
32  return ((id_ & ID_mask) << ID_shift) | ((size_ & size_mask) << size_shift) | ((capID_ & capID_mask) << capID_shift) | ((flags_ & flags_mask) << flags_shift);
33  }
34  // if (type_ == MTF7) {
35  // LogTrace("L1T") << "Writing MTF7 link header";
36  // return ((id_ & ID_mask) << ID_shift) | ((size_ & size_mask) << size_shift) | ((capID_ & capID_mask) << capID_shift);
37  // }
38  LogTrace("L1T") << "Writing CTP7 link header";
39  return ((id_ & CTP7_mask) << CTP7_shift);
40  }
41 
42  BxBlocks
43  Block::getBxBlocks(unsigned int payloadWordsPerBx, bool bxHeader) const
44  {
45  BxBlocks bxBlocks;
46 
47  // For MP7 format
48  unsigned int wordsPerBx = payloadWordsPerBx;
49  if (bxHeader) {
50  ++wordsPerBx;
51  }
52  // Calculate how many BxBlock objects can be made with the available payload
53  unsigned int nBxBlocks = payload_.size() / wordsPerBx;
54  for (size_t bxCtr = 0; bxCtr < nBxBlocks; ++bxCtr) {
55  size_t startIdx = bxCtr * wordsPerBx;
56  auto startBxBlock = payload_.cbegin()+startIdx;
57  // Pick the words from the block payload that correspond to the BX and add a BxBlock to the BxBlocks
58  if (bxHeader) {
59  bxBlocks.emplace_back(startBxBlock, startBxBlock+wordsPerBx);
60  } else {
61  bxBlocks.emplace_back(bxCtr, nBxBlocks, startBxBlock, startBxBlock+wordsPerBx);
62  }
63  }
64 
65  return bxBlocks;
66  }
67 
68  std::unique_ptr<Block>
70  {
71  if (end_ - data_ < getHeaderSize()) {
72  LogDebug("L1T") << "Reached end of payload";
73  return std::auto_ptr<Block>();
74  }
75 
76  if (data_[0] == 0xffffffff) {
77  LogDebug("L1T") << "Skipping padding word";
78  ++data_;
79  return getBlock();
80  }
81 
82  auto header = getHeader();
83 
84  if (end_ - data_ < header.getSize()) {
85  edm::LogError("L1T")
86  << "Expecting a block size of " << header.getSize()
87  << " but only " << (end_ - data_) << " words remaining";
88  return std::auto_ptr<Block>();
89  }
90 
91  LogTrace("L1T") << "Creating block with size " << header.getSize();
92 
93  auto res = std::unique_ptr<Block>(new Block(header, data_, data_ + header.getSize()));
94  data_ += header.getSize();
95  return res;
96  }
97 
98  MP7Payload::MP7Payload(const uint32_t * data, const uint32_t * end, bool legacy_mc) : Payload(data, end)
99  {
100  // For legacy MC (74 first MC campaigns) skip one empty word that was
101  // reserved for the header. With data, read out infrastructure
102  // version and algorithm version.
103  if (legacy_mc) {
104  LogTrace("L1T") << "Skipping " << std::hex << *data_;
105  ++data_;
106  } else {
107  infra_ = data_[0];
108  algo_ = data_[1];
109  data_ += 2;
110  }
111  }
112 
115  {
116  LogTrace("L1T") << "Getting header from " << std::hex << std::setw(8) << *data_;
117 
118  return BlockHeader(data_++);
119  }
120 
121  MTF7Payload::MTF7Payload(const uint32_t * data, const uint32_t * end) : Payload(data, end)
122  {
123  const uint16_t * data16 = reinterpret_cast<const uint16_t*>(data);
124  const uint16_t * end16 = reinterpret_cast<const uint16_t*>(end);
125 
126  if (end16 - data16 < header_size + counter_size + trailer_size) {
127  edm::LogError("L1T") << "MTF7 payload smaller than allowed!";
128  data_ = end_;
129  } else if (
130  ((data16[0] >> 12) != 0x9) || ((data16[1] >> 12) != 0x9) ||
131  ((data16[2] >> 12) != 0x9) || ((data16[3] >> 12) != 0x9) ||
132  ((data16[4] >> 12) != 0xA) || ((data16[5] >> 12) != 0xA) ||
133  ((data16[6] >> 12) != 0xA) || ((data16[7] >> 12) != 0xA) ||
134  ((data16[8] >> 9) != 0b1000000) || ((data16[9] >> 11) != 0) ||
135  ((data16[10] >> 11) != 0) || ((data16[11] >> 11) != 0)) {
136  edm::LogError("L1T") << "MTF7 payload has invalid header!";
137  data_ = end_;
138  } else if (
139  ((data16[12] >> 15) != 0) || ((data16[13] >> 15) != 1) ||
140  ((data16[14] >> 15) != 0) || ((data16[15] >> 15) != 0)) {
141  edm::LogError("L1T") << "MTF7 payload has invalid counter block!";
142  data_ = end_;
143  } else if (
144  false) {
145  // TODO: check trailer
146  }
147  }
148 
149  int
150  MTF7Payload::count(unsigned int pattern, unsigned int length) const
151  {
152  unsigned int mask = 0;
153  for (; length > 0; length--)
154  mask = (mask << 4) | 0xf;
155 
156  int count = 0;
157  for (const auto& p: block_patterns_)
158  count += (p & mask) == pattern;
159  return count;
160  }
161 
162  bool
163  MTF7Payload::valid(unsigned int pattern) const
164  {
165  for (const auto& p: block_patterns_) {
166  if (p == pattern)
167  return true;
168  }
169  return false;
170  }
171 
172  std::unique_ptr<Block>
174  {
175  if (end_ - data_ < 2)
176  return std::auto_ptr<Block>(nullptr);
177 
178  const uint16_t * data16 = reinterpret_cast<const uint16_t*>(data_);
179  const uint16_t * end16 = reinterpret_cast<const uint16_t*>(end_);
180 
181  // Read in blocks equivalent to 64 bit words, trying to match the
182  // pattern of first bits to what is deemed valid.
183  std::vector<uint32_t> payload;
184  unsigned int pattern = 0;
185  unsigned int i = 0;
186  for (; i < max_block_length_ and data16 + (i + 1) * 4 <= end16; ++i) {
187  for (int j = 0; j < 4; ++j) {
188  auto n = i * 4 + j;
189  pattern |= (data16[n] >> 15) << n;
190  payload.push_back(data16[n]);
191  }
192 
193  if (count(pattern, i + 1) == 1 and valid(pattern))
194  break;
195  }
196 
197  if (not valid(pattern)) {
198  edm::LogWarning("L1T") << "MTF7 block with unrecognized id 0x" << std::hex << pattern;
199  return std::auto_ptr<Block>(nullptr);
200  }
201 
202  data_ += (i + 1) * 2;
203  return std::unique_ptr<Block>(new Block(pattern, payload, 0, MTF7));
204  }
205 
206  CTP7Payload::CTP7Payload(const uint32_t * data, const uint32_t * end) : Payload(data, end)
207  {
208  ++data_;
209  size_ = (*data >> size_shift) & size_mask;
210  ++data_;
211  }
212 
215  {
216  return BlockHeader(data_++, size_);
217  }
218 }
#define LogDebug(id)
uint32_t raw(block_t type=MP7) const
Definition: Block.cc:28
type
Definition: HCALResponse.h:21
static const unsigned int capID_mask
Definition: Block.h:39
static const unsigned int size_shift
Definition: Block.h:133
BlockHeader getHeader() override
Definition: Block.cc:214
static const unsigned int CTP7_mask
Definition: Block.h:33
MP7Payload(const uint32_t *data, const uint32_t *end, bool legacy_mc=false)
Definition: Block.cc:98
Definition: Block.h:11
std::vector< l1t::Jet >::iterator end_
static const unsigned int trailer_size
Definition: Block.h:114
delete x;
Definition: CaloConfig.h:22
int count(unsigned int pattern, unsigned int length) const
Definition: Block.cc:150
static const unsigned int ID_shift
Definition: Block.h:34
block_t type_
Definition: Block.h:47
const uint32_t * end_
Definition: Block.h:90
unsigned int flags_
Definition: Block.h:46
static const unsigned int ID_mask
Definition: Block.h:35
virtual std::unique_ptr< Block > getBlock()
Definition: Block.cc:69
Definition: Electron.h:4
unsigned size_
Definition: Block.h:135
payload
payload postfix for testing
block_t
Definition: Block.h:11
CTP7Payload(const uint32_t *data, const uint32_t *end)
Definition: Block.cc:206
static const unsigned int counter_size
Definition: Block.h:113
bool valid(unsigned int pattern) const
Definition: Block.cc:163
static const unsigned int size_mask
Definition: Block.h:37
#define end
Definition: vmac.h:37
unsigned infra_
Definition: Block.h:93
unsigned int size_
Definition: Block.h:44
#define LogTrace(id)
MTF7Payload(const uint32_t *data, const uint32_t *end)
Definition: Block.cc:121
unsigned int capID_
Definition: Block.h:45
const uint32_t * data_
Definition: Block.h:89
std::vector< BxBlock > BxBlocks
Definition: BxBlock.h:67
BlockHeader getHeader() override
Definition: Block.cc:114
static const unsigned int size_mask
Definition: Block.h:132
static const unsigned int CTP7_shift
Definition: Block.h:32
static const unsigned int flags_mask
Definition: Block.h:41
BxBlocks getBxBlocks(unsigned int payloadWordsPerBx, bool bxHeader) const
Definition: Block.cc:43
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
unsigned int id_
Definition: Block.h:43
static const unsigned int header_size
Definition: Block.h:112
static const unsigned int capID_shift
Definition: Block.h:38
static const unsigned int max_block_length_
Definition: Block.h:118
static const unsigned int size_shift
Definition: Block.h:36
static const unsigned int flags_shift
Definition: Block.h:40
unsigned algo_
Definition: Block.h:92
static const std::vector< unsigned int > block_patterns_
Definition: Block.h:119
std::unique_ptr< Block > getBlock() override
Definition: Block.cc:173