CMS 3D CMS Logo

PatternMatchingLayer.cc
Go to the documentation of this file.
2 
5 
7 
8 using namespace emtf::phase2::algo;
9 
11 
12 void PatternMatchingLayer::apply(const std::vector<hitmap_t>& zone_hitmaps,
13  const bool& displaced_en,
14  std::vector<road_collection_t>& zone_roads) const {
15  typedef ap_uint<v3::kHitmapNCols + v3::kPatternMatchingPadding * 2> padded_row_t;
16  typedef ap_uint<v3::kHitmapNRows> pattern_activation_t;
17  typedef std::array<pattern_activation_t, v3::kHitmapNCols> pattern_activation_collection_t;
18 
19  const padded_row_t padded_one = 1;
20 
21  auto& model = context_.model_;
22 
23  for (unsigned int i_zone = 0; i_zone < zone_hitmaps.size(); ++i_zone) { // Loop Zones
24  auto& hitmap = zone_hitmaps[i_zone];
25  auto* model_pc = &(model.zones_[i_zone].prompt_patterns);
26  auto* model_ql = &(model.zones_[i_zone].prompt_quality_lut);
27 
28  if (displaced_en) {
29  model_pc = &(model.zones_[i_zone].disp_patterns);
30  model_ql = &(model.zones_[i_zone].disp_quality_lut);
31  }
32 
33  // Initialize roads
34  auto& roads = zone_roads.emplace_back();
35 
36  for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
37  roads[i_col].pattern = 0;
38  roads[i_col].quality = 0;
39  }
40 
41  // Apply patterns
42  for (unsigned int i_pattern = 0; i_pattern < model_pc->size(); ++i_pattern) { // Loop Patterns
43  auto& model_pat = (*model_pc)[i_pattern];
44 
45  // Initialize activations
46  pattern_activation_collection_t pac;
47 
48  for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
49  pac[i_col] = 0;
50  }
51 
52  // Build activations
53  for (unsigned int i_row = 0; i_row < v3::kHitmapNRows; ++i_row) { // Loop Rows
54  // Pad the row with zeros to cover cases where
55  // pattern range is out of range
56  const auto& hitmap_row = hitmap[i_row];
57  auto& model_pat_row = model_pat[i_row];
58 
59  // Pad the hitmap row on both sides using kMaxPadding
60  // We binary shift it to the left by kMaxPadding
61  // effectively padding it to the right, and since
62  // the bitwidth already includes both paddings
63  // the left is also padded
64  padded_row_t padded_hm_row = hitmap_row;
65  padded_hm_row = padded_hm_row << v3::kPatternMatchingPadding;
66 
67  // Convert the model pattern row to a padded row
68  padded_row_t padded_pat_row = 0;
69 
70  unsigned int offset = model_pat_row.begin;
71 
72  unsigned int bw = model_pat_row.end - model_pat_row.begin + 1; // Add 1 since it's an inclusive range
73 
74  for (unsigned int i_bit = 0; i_bit < bw; ++i_bit)
75  padded_pat_row |= (padded_one << (offset + i_bit));
76 
77  // Slide the pattern row across the hitmap and check for 'activations'
78  for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
79  // "AND" both rows together if the result is greater than 0
80  // there is an activation
81  padded_row_t result = padded_pat_row & padded_hm_row;
82 
83  if (result > 0)
84  pac[i_col] = pac[i_col] | (1 << i_row);
85 
86  // Shift the pattern row to the left, i.e. slide it across
87  padded_pat_row = padded_pat_row << 1;
88  }
89  } // End Loop Rows
90 
91  // Compare Activations
92  // Update the road if the column's road has a smaller
93  // quality than the new activation's quality.
94  // Note: Since this is in a loop going from smallest pattern number
95  // to the largest, cases where the quality is the same,
96  // but the pattern number is larger the smaller one will be preferred
97  for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
98  auto& activation = pac[i_col];
99  auto quality = (*model_ql)[activation];
100 
101  auto& current_road = roads[i_col];
102 
103  if (current_road.quality < quality) {
104  current_road.pattern = i_pattern;
105  current_road.quality = quality;
106  }
107  }
108  } // End Loop Patterns in Zones
109 
110  // Debug Info
111  if (this->context_.config_.verbosity_ > 1) {
112  for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
113  if (roads[i_col].quality == 0) {
114  continue;
115  }
116 
117  edm::LogInfo("L1TEMTFpp") << "Road"
118  << " zone " << i_zone << " col " << i_col << " pat " << roads[i_col].pattern
119  << " qual " << roads[i_col].quality << std::endl;
120  }
121  }
122  } // End Loop Zones
123 }
constexpr int kHitmapNRows
Definition: EMTFConstants.h:47
constexpr int kHitmapNCols
Definition: EMTFConstants.h:48
constexpr int kPatternMatchingPadding
Definition: EMTFConstants.h:56
void apply(const std::vector< hitmap_t > &, const bool &, std::vector< road_collection_t > &) const
string quality
Log< level::Info, false > LogInfo
EMTFConfiguration config_
Definition: EMTFContext.h:39