Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:01

0001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0002 
0003 #include "L1Trigger/L1TMuonEndCapPhase2/interface/EMTFContext.h"
0004 #include "L1Trigger/L1TMuonEndCapPhase2/interface/Utils/DebugUtils.h"
0005 
0006 #include "L1Trigger/L1TMuonEndCapPhase2/interface/Algo/PatternMatchingLayer.h"
0007 
0008 using namespace emtf::phase2::algo;
0009 
0010 PatternMatchingLayer::PatternMatchingLayer(const EMTFContext& context) : context_(context) {}
0011 
0012 void PatternMatchingLayer::apply(const std::vector<hitmap_t>& zone_hitmaps,
0013                                  const bool& displaced_en,
0014                                  std::vector<road_collection_t>& zone_roads) const {
0015   typedef ap_uint<v3::kHitmapNCols + v3::kPatternMatchingPadding * 2> padded_row_t;
0016   typedef ap_uint<v3::kHitmapNRows> pattern_activation_t;
0017   typedef std::array<pattern_activation_t, v3::kHitmapNCols> pattern_activation_collection_t;
0018 
0019   const padded_row_t padded_one = 1;
0020 
0021   auto& model = context_.model_;
0022 
0023   for (unsigned int i_zone = 0; i_zone < zone_hitmaps.size(); ++i_zone) {  // Loop Zones
0024     auto& hitmap = zone_hitmaps[i_zone];
0025     auto* model_pc = &(model.zones_[i_zone].prompt_patterns);
0026     auto* model_ql = &(model.zones_[i_zone].prompt_quality_lut);
0027 
0028     if (displaced_en) {
0029       model_pc = &(model.zones_[i_zone].disp_patterns);
0030       model_ql = &(model.zones_[i_zone].disp_quality_lut);
0031     }
0032 
0033     // Initialize roads
0034     auto& roads = zone_roads.emplace_back();
0035 
0036     for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
0037       roads[i_col].pattern = 0;
0038       roads[i_col].quality = 0;
0039     }
0040 
0041     // Apply patterns
0042     for (unsigned int i_pattern = 0; i_pattern < model_pc->size(); ++i_pattern) {  // Loop Patterns
0043       auto& model_pat = (*model_pc)[i_pattern];
0044 
0045       // Initialize activations
0046       pattern_activation_collection_t pac;
0047 
0048       for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
0049         pac[i_col] = 0;
0050       }
0051 
0052       // Build activations
0053       for (unsigned int i_row = 0; i_row < v3::kHitmapNRows; ++i_row) {  // Loop Rows
0054         // Pad the row with zeros to cover cases where
0055         // pattern range is out of range
0056         const auto& hitmap_row = hitmap[i_row];
0057         auto& model_pat_row = model_pat[i_row];
0058 
0059         // Pad the hitmap row on both sides using kMaxPadding
0060         // We binary shift it to the left by kMaxPadding
0061         // effectively padding it to the right, and since
0062         // the bitwidth already includes both paddings
0063         // the left is also padded
0064         padded_row_t padded_hm_row = hitmap_row;
0065         padded_hm_row = padded_hm_row << v3::kPatternMatchingPadding;
0066 
0067         // Convert the model pattern row to a padded row
0068         padded_row_t padded_pat_row = 0;
0069 
0070         unsigned int offset = model_pat_row.begin;
0071 
0072         unsigned int bw = model_pat_row.end - model_pat_row.begin + 1;  // Add 1 since it's an inclusive range
0073 
0074         for (unsigned int i_bit = 0; i_bit < bw; ++i_bit)
0075           padded_pat_row |= (padded_one << (offset + i_bit));
0076 
0077         // Slide the pattern row across the hitmap and check for 'activations'
0078         for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
0079           // "AND" both rows together if the result is greater than 0
0080           // there is an activation
0081           padded_row_t result = padded_pat_row & padded_hm_row;
0082 
0083           if (result > 0)
0084             pac[i_col] = pac[i_col] | (1 << i_row);
0085 
0086           // Shift the pattern row to the left, i.e. slide it across
0087           padded_pat_row = padded_pat_row << 1;
0088         }
0089       }  // End Loop Rows
0090 
0091       // Compare Activations
0092       // Update the road if the column's road has a smaller
0093       // quality than the new activation's quality.
0094       // Note: Since this is in a loop going from smallest pattern number
0095       // to the largest, cases where the quality is the same,
0096       // but the pattern number is larger the smaller one will be preferred
0097       for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
0098         auto& activation = pac[i_col];
0099         auto quality = (*model_ql)[activation];
0100 
0101         auto& current_road = roads[i_col];
0102 
0103         if (current_road.quality < quality) {
0104           current_road.pattern = i_pattern;
0105           current_road.quality = quality;
0106         }
0107       }
0108     }  // End Loop Patterns in Zones
0109 
0110     // Debug Info
0111     if (this->context_.config_.verbosity_ > 1) {
0112       for (unsigned int i_col = 0; i_col < v3::kHitmapNCols; ++i_col) {
0113         if (roads[i_col].quality == 0) {
0114           continue;
0115         }
0116 
0117         edm::LogInfo("L1TEMTFpp") << "Road"
0118                                   << " zone " << i_zone << " col " << i_col << " pat " << roads[i_col].pattern
0119                                   << " qual " << roads[i_col].quality << std::endl;
0120       }
0121     }
0122   }  // End Loop Zones
0123 }