CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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);
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  std::auto_ptr<Block>
44  {
45  if (end_ - data_ < getHeaderSize()) {
46  LogDebug("L1T") << "Reached end of payload";
47  return std::auto_ptr<Block>();
48  }
49 
50  if (data_[0] == 0xffffffff) {
51  LogDebug("L1T") << "Skipping padding word";
52  ++data_;
53  return getBlock();
54  }
55 
56  auto header = getHeader();
57 
58  if (end_ - data_ < header.getSize()) {
59  edm::LogError("L1T")
60  << "Expecting a block size of " << header.getSize()
61  << " but only " << (end_ - data_) << " words remaining";
62  return std::auto_ptr<Block>();
63  }
64 
65  LogTrace("L1T") << "Creating block with size " << header.getSize();
66 
67  auto res = std::auto_ptr<Block>(new Block(header, data_, data_ + header.getSize()));
68  data_ += header.getSize();
69  return res;
70  }
71 
72  MP7Payload::MP7Payload(const uint32_t * data, const uint32_t * end, bool legacy_mc) : Payload(data, end)
73  {
74  // For legacy MC (74 first MC campaigns) skip one empty word that was
75  // reserved for the header. With data, read out infrastructure
76  // version and algorithm version.
77  if (legacy_mc) {
78  LogTrace("L1T") << "Skipping " << std::hex << *data_;
79  ++data_;
80  } else {
81  infra_ = data_[0];
82  algo_ = data_[1];
83  data_ += 2;
84  }
85  }
86 
89  {
90  LogTrace("L1T") << "Getting header from " << std::hex << std::setw(8) << *data_;
91 
92  return BlockHeader(data_++);
93  }
94 
95  MTF7Payload::MTF7Payload(const uint32_t * data, const uint32_t * end) : Payload(data, end)
96  {
97  const uint16_t * data16 = reinterpret_cast<const uint16_t*>(data);
98  const uint16_t * end16 = reinterpret_cast<const uint16_t*>(end);
99 
100  if (end16 - data16 < header_size + counter_size + trailer_size) {
101  edm::LogError("L1T") << "MTF7 payload smaller than allowed!";
102  data_ = end_;
103  } else if (
104  ((data16[0] >> 12) != 0x9) || ((data16[1] >> 12) != 0x9) ||
105  ((data16[2] >> 12) != 0x9) || ((data16[3] >> 12) != 0x9) ||
106  ((data16[4] >> 12) != 0xA) || ((data16[5] >> 12) != 0xA) ||
107  ((data16[6] >> 12) != 0xA) || ((data16[7] >> 12) != 0xA) ||
108  ((data16[8] >> 9) != 0b1000000) || ((data16[9] >> 11) != 0) ||
109  ((data16[10] >> 11) != 0) || ((data16[11] >> 11) != 0)) {
110  edm::LogError("L1T") << "MTF7 payload has invalid header!";
111  data_ = end_;
112  } else if (
113  ((data16[12] >> 15) != 0) || ((data16[13] >> 15) != 1) ||
114  ((data16[14] >> 15) != 0) || ((data16[15] >> 15) != 0)) {
115  edm::LogError("L1T") << "MTF7 payload has invalid counter block!";
116  data_ = end_;
117  } else if (
118  false) {
119  // TODO: check trailer
120  }
121  }
122 
123  int
124  MTF7Payload::count(unsigned int pattern, unsigned int length) const
125  {
126  unsigned int mask = 0;
127  for (; length > 0; length--)
128  mask = (mask << 4) | 0xf;
129 
130  int count = 0;
131  for (const auto& p: block_patterns_)
132  count += (p & mask) == pattern;
133  return count;
134  }
135 
136  bool
137  MTF7Payload::valid(unsigned int pattern) const
138  {
139  for (const auto& p: block_patterns_) {
140  if (p == pattern)
141  return true;
142  }
143  return false;
144  }
145 
146  std::auto_ptr<Block>
148  {
149  if (end_ - data_ < 2)
150  return std::auto_ptr<Block>(0);
151 
152  const uint16_t * data16 = reinterpret_cast<const uint16_t*>(data_);
153  const uint16_t * end16 = reinterpret_cast<const uint16_t*>(end_);
154 
155  // Read in blocks equivalent to 64 bit words, trying to match the
156  // pattern of first bits to what is deemed valid.
157  std::vector<uint32_t> payload;
158  unsigned int pattern = 0;
159  unsigned int i = 0;
160  for (; i < max_block_length_ and data16 + (i + 1) * 4 <= end16; ++i) {
161  for (int j = 0; j < 4; ++j) {
162  auto n = i * 4 + j;
163  pattern |= (data16[n] >> 15) << n;
164  payload.push_back(data16[n]);
165  }
166 
167  if (count(pattern, i + 1) == 1 and valid(pattern))
168  break;
169  }
170 
171  if (not valid(pattern)) {
172  edm::LogWarning("L1T") << "MTF7 block with unrecognized id 0x" << std::hex << pattern;
173  return std::auto_ptr<Block>(0);
174  }
175 
176  data_ += (i + 1) * 2;
177  return std::auto_ptr<Block>(new Block(pattern, payload, 0, MTF7));
178  }
179 
180  CTP7Payload::CTP7Payload(const uint32_t * data, const uint32_t * end) : Payload(data, end)
181  {
182  ++data_;
183  size_ = (*data >> size_shift) & size_mask;
184  ++data_;
185  }
186 
189  {
190  return BlockHeader(data_++, size_);
191  }
192 }
#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:37
static const unsigned int size_shift
Definition: Block.h:127
int i
Definition: DBlmapReader.cc:9
virtual BlockHeader getHeader() override
Definition: Block.cc:188
static const unsigned int CTP7_mask
Definition: Block.h:31
MP7Payload(const uint32_t *data, const uint32_t *end, bool legacy_mc=false)
Definition: Block.cc:72
virtual std::auto_ptr< Block > getBlock()
Definition: Block.cc:43
virtual std::auto_ptr< Block > getBlock() override
Definition: Block.cc:147
Definition: Block.h:10
static const unsigned int trailer_size
Definition: Block.h:108
int count(unsigned int pattern, unsigned int length) const
Definition: Block.cc:124
static const unsigned int ID_shift
Definition: Block.h:32
block_t type_
Definition: Block.h:42
const uint32_t * end_
Definition: Block.h:84
static const unsigned int ID_mask
Definition: Block.h:33
unsigned size_
Definition: Block.h:129
virtual BlockHeader getHeader()=0
block_t
Definition: Block.h:10
CTP7Payload(const uint32_t *data, const uint32_t *end)
Definition: Block.cc:180
static const unsigned int counter_size
Definition: Block.h:107
bool valid(unsigned int pattern) const
Definition: Block.cc:137
virtual unsigned getHeaderSize() const =0
int j
Definition: DBlmapReader.cc:9
static const unsigned int size_mask
Definition: Block.h:35
#define end
Definition: vmac.h:37
unsigned infra_
Definition: Block.h:87
unsigned int size_
Definition: Block.h:40
#define LogTrace(id)
MTF7Payload(const uint32_t *data, const uint32_t *end)
Definition: Block.cc:95
unsigned int capID_
Definition: Block.h:41
const uint32_t * data_
Definition: Block.h:83
virtual BlockHeader getHeader() override
Definition: Block.cc:88
static const unsigned int size_mask
Definition: Block.h:126
static const unsigned int CTP7_shift
Definition: Block.h:30
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
unsigned int id_
Definition: Block.h:39
static const unsigned int header_size
Definition: Block.h:106
static const unsigned int capID_shift
Definition: Block.h:36
static const unsigned int max_block_length_
Definition: Block.h:112
static const unsigned int size_shift
Definition: Block.h:34
unsigned algo_
Definition: Block.h:86
static const std::vector< unsigned int > block_patterns_
Definition: Block.h:113