Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-07-16 02:43:01

0001 #include "PhysicsTools/TensorFlow/interface/TensorFlow.h"
0002 
0003 #include "L1Trigger/L1TMuonEndCapPhase2/interface/EMTFContext.h"
0004 #include "L1Trigger/L1TMuonEndCapPhase2/interface/Utils/DataUtils.h"
0005 #include "L1Trigger/L1TMuonEndCapPhase2/interface/Utils/DebugUtils.h"
0006 
0007 #include "L1Trigger/L1TMuonEndCapPhase2/interface/Algo/OutputLayer.h"
0008 
0009 using namespace emtf::phase2;
0010 using namespace emtf::phase2::algo;
0011 
0012 OutputLayer::OutputLayer(const EMTFContext& context) : context_(context) {}
0013 
0014 void OutputLayer::apply(const int& endcap,
0015                         const int& sector,
0016                         const int& bx,
0017                         const std::map<int, int>& seg_to_hit,
0018                         const std::vector<track_t>& tracks,
0019                         const bool& displaced_en,
0020                         EMTFTrackCollection& out_tracks) const {
0021   const int endcap_pm = (endcap == 2) ? -1 : endcap;  // 1: +endcap, -1: -endcap
0022 
0023   for (auto& track : tracks) {  // Begin loop tracks
0024     // Fill Site/Hit Vectors
0025     int hit_count = 0;
0026 
0027     EMTFTrack::site_hits_t site_hits;
0028     EMTFTrack::site_segs_t site_segs;
0029     EMTFTrack::site_mask_t site_mask;
0030     EMTFTrack::site_mask_t site_rm_mask;
0031 
0032     for (unsigned int i = 0; i < v3::kNumTrackSites; i++) {
0033       // Get attached segments
0034       const auto& site_seg_id = track.site_segs[i];
0035       const auto& site_bit = track.site_mask[i];
0036       const auto& site_rm_bit = track.site_rm_mask[i];
0037 
0038       // Increase hit count
0039       if (site_bit == 1) {
0040         hit_count += 1;
0041       }
0042 
0043       // Convert segment to hit
0044       int hit_id = 0;
0045 
0046       if ((site_bit == 1) || (site_rm_bit == 1)) {
0047         hit_id = seg_to_hit.at(site_seg_id);
0048       }
0049 
0050       // Save Info
0051       site_hits.push_back(hit_id);
0052       site_segs.push_back(site_seg_id);
0053       site_mask.push_back(site_bit);
0054       site_rm_mask.push_back(site_rm_bit);
0055     }
0056 
0057     // Short-Circuit: Only keep tracks with hits
0058     if (!track.valid && hit_count == 0) {
0059       continue;
0060     }
0061 
0062     // Fill Feature Vector
0063     EMTFTrack::features_t model_features;
0064 
0065     for (unsigned int i = 0; i < v3::kNumTrackFeatures; i++) {
0066       model_features.push_back(track.features[i]);
0067     }
0068 
0069     // Find EMTF/GMT variables
0070     const int emtf_mode_v1 = findEMTFModeV1(track.site_mask);
0071     const int emtf_mode_v2 = findEMTFModeV2(track.site_mask);
0072     const int emtf_quality = findEMTFQuality(track, emtf_mode_v1, emtf_mode_v2);
0073 
0074     // Init Parameters
0075     auto& out_trk = out_tracks.emplace_back();
0076 
0077     out_trk.setEndcap(endcap_pm);
0078     out_trk.setSector(sector);
0079     out_trk.setBx(bx);
0080     out_trk.setUnconstrained(displaced_en ? true : false);
0081     out_trk.setValid(track.valid);
0082 
0083     out_trk.setModelPtAddress(track.pt_address);
0084     out_trk.setModelRelsAddress(track.rels_address);
0085     out_trk.setModelDxyAddress(track.dxy_address);
0086     out_trk.setModelPattern(track.pattern);
0087     out_trk.setModelQual(track.quality);
0088     out_trk.setModelPhi(track.phi);
0089     out_trk.setModelEta(track.theta);
0090     out_trk.setModelFeatures(model_features);
0091 
0092     out_trk.setEmtfQ(track.q);
0093     out_trk.setEmtfPt(track.pt);
0094     out_trk.setEmtfRels(track.rels);
0095     out_trk.setEmtfD0(std::abs(track.dxy));
0096     out_trk.setEmtfZ0(0);    // not yet implemented
0097     out_trk.setEmtfBeta(0);  // not yet implemented
0098     out_trk.setEmtfModeV1(emtf_mode_v1);
0099     out_trk.setEmtfModeV2(emtf_mode_v2);
0100     out_trk.setEmtfQuality(emtf_quality);
0101 
0102     out_trk.setSiteHits(site_hits);
0103     out_trk.setSiteSegs(site_segs);
0104     out_trk.setSiteMask(site_mask);
0105     out_trk.setSiteRMMask(site_rm_mask);
0106   }  // End loop tracks
0107 }
0108 
0109 int OutputLayer::findEMTFModeV1(const track_t::site_mask_t& x) const {
0110   int mode = 0;
0111 
0112   if (x[0] or x[9] or x[1] or x[5] or x[11]) {  // ME1/1, GE1/1, ME1/2, RE1/2, ME0
0113     mode |= (1 << 3);
0114   }
0115 
0116   if (x[2] or x[10] or x[6]) {  // ME2, GE2/1, RE2/2
0117     mode |= (1 << 2);
0118   }
0119 
0120   if (x[3] or x[7]) {  // ME3, RE3
0121     mode |= (1 << 1);
0122   }
0123 
0124   if (x[4] or x[8]) {  // ME4, RE4
0125     mode |= (1 << 0);
0126   }
0127 
0128   return mode;
0129 }
0130 
0131 // SingleMu (12)
0132 // - at least one station-1 segment (ME1/1, GE1/1, ME1/2, RE1/2, ME0)
0133 //   with one of the following requirements on stations 2,3,4
0134 //   a. if there is ME1/2 or RE1/2,
0135 //      i.  if there is ME1/2, require 1 more CSC station
0136 //      ii. else, require 1 more CSC station + 1 more station
0137 //   b. if there is ME1/1 or GE1/1,
0138 //      i.  if there is ME1/1, require 1 more CSC station + 1 more station
0139 //      ii. else, require 2 more CSC stations
0140 //   c. if there is ME0,
0141 //      i.  if there is ME1/1, require 1 more station in stations 3,4
0142 //      ii. else, require 1 more CSC station + 1 more station
0143 //
0144 // DoubleMu (8)
0145 // - at least one station-1 segment (ME1/1, GE1/1, ME1/2, RE1/2, ME0)
0146 //   with one of the following requirements on stations 2,3,4
0147 //   a. if there is ME1/1 or ME1/2, require 1 more station
0148 //   b. if there is GE1/1 or RE1/2, require 1 more CSC station
0149 //   c. if there is ME0,
0150 //      i.  if there is ME1/1, require 1 more station
0151 //      ii. else, require 1 more CSC station
0152 //
0153 // TripleMu (4)
0154 // - at least two stations
0155 //   a. if there is ME1/1 or ME1/2, require 1 more station
0156 //   b. if there is GE1/1 or RE1/2, require 1 more CSC station
0157 //   c. if there is ME0,
0158 //      i.  if there is ME1/1, require 1 more station
0159 //      ii. else, require 1 more CSC station
0160 //   d. else, require 2 more CSC stations
0161 //
0162 // SingleHit (0)
0163 // - at least one station
0164 //
0165 // Note that SingleMu, DoubleMu, TripleMu, SingleHit are mutually-exclusive categories.
0166 int OutputLayer::findEMTFModeV2(const track_t::site_mask_t& x) const {
0167   int mode = 0;
0168   int cnt_ye11 = x[0] + x[9];                                          // ME1/1, GE1/1
0169   int cnt_ye12 = x[1] + x[5];                                          // ME1/2, RE1/2
0170   int cnt_ye22 = x[2] + x[10] + x[6];                                  // ME2, GE2/1, RE2/2
0171   int cnt_ye23 = x[3] + x[7];                                          // ME3, RE3
0172   int cnt_ye24 = x[4] + x[8];                                          // ME4, RE4
0173   int cnt_ye2a = (cnt_ye22 != 0) + (cnt_ye23 != 0) + (cnt_ye24 != 0);  //
0174   int cnt_ye2b = (cnt_ye23 != 0) + (cnt_ye24 != 0);                    //
0175   int cnt_me11 = x[0];                                                 // ME1/1 only
0176   int cnt_me12 = x[1];                                                 // ME1/2 only
0177   int cnt_me14 = x[11];                                                // ME0 only
0178   int cnt_me2a = (x[2] != 0) + (x[3] != 0) + (x[4] != 0);              //
0179 
0180   // SingleMu (12)
0181   {
0182     bool rule_a_i = (cnt_me12 != 0) and (cnt_me2a >= 1);
0183     bool rule_a_ii = (cnt_ye12 != 0) and (cnt_me2a >= 1) and (cnt_ye2a >= 2);
0184     bool rule_b_i = (cnt_me11 != 0) and (cnt_me2a >= 1) and (cnt_ye2a >= 2);
0185     bool rule_b_ii = (cnt_ye11 != 0) and (cnt_me2a >= 2);
0186     bool rule_c_i = (cnt_me14 != 0) and (cnt_me11 != 0) and (cnt_ye2b >= 1);
0187     bool rule_c_ii = (cnt_me14 != 0) and (cnt_me2a >= 1) and (cnt_ye2a >= 2);
0188 
0189     if (rule_a_i or rule_a_ii or rule_b_i or rule_b_ii or rule_c_i or rule_c_ii) {
0190       mode |= (1 << 3);
0191       mode |= (1 << 2);
0192     }
0193   }
0194 
0195   // DoubleMu (8)
0196   if (mode < (1 << 3)) {
0197     bool rule_a_i = (cnt_me12 != 0) and (cnt_ye2a >= 1);
0198     bool rule_a_ii = (cnt_me11 != 0) and (cnt_ye2a >= 1);
0199     bool rule_b_i = (cnt_ye12 != 0) and (cnt_me2a >= 1);
0200     bool rule_b_ii = (cnt_ye11 != 0) and (cnt_me2a >= 1);
0201     bool rule_c_i = (cnt_me14 != 0) and (cnt_me11 != 0) and (cnt_ye2a >= 1);
0202     bool rule_c_ii = (cnt_me14 != 0) and (cnt_me2a >= 1);
0203 
0204     if (rule_a_i or rule_a_ii or rule_b_i or rule_b_ii or rule_c_i or rule_c_ii) {
0205       mode |= (1 << 3);
0206     }
0207   }
0208 
0209   // TripleMu (4)
0210   if (mode < (1 << 2)) {
0211     bool rule_a_i = (cnt_me12 != 0) and (cnt_ye2a >= 1);
0212     bool rule_a_ii = (cnt_me11 != 0) and (cnt_ye2a >= 1);
0213     bool rule_b_i = (cnt_ye12 != 0) and (cnt_me2a >= 1);
0214     bool rule_b_ii = (cnt_ye11 != 0) and (cnt_me2a >= 1);
0215     bool rule_c_i = (cnt_me14 != 0) and (cnt_me11 != 0) and (cnt_ye2a >= 1);
0216     bool rule_c_ii = (cnt_me14 != 0) and (cnt_me2a >= 1);
0217     bool rule_d = (cnt_me2a >= 2);
0218 
0219     if (rule_a_i or rule_a_ii or rule_b_i or rule_b_ii or rule_c_i or rule_c_ii or rule_d) {
0220       mode |= (1 << 2);
0221     }
0222   }
0223 
0224   return mode;
0225 }
0226 
0227 int OutputLayer::findEMTFQuality(const track_t& track, const int& mode_v1, const int& mode_v2) const {
0228   // Short-Circuit: Invalid track
0229   if (track.valid == 0) {
0230     return 0;
0231   }
0232 
0233   // Short-Circuit: Single Station
0234   bool is_single_station = (mode_v1 == 0);
0235   is_single_station |= (mode_v1 == 1);
0236   is_single_station |= (mode_v1 == 2);
0237   is_single_station |= (mode_v1 == 4);
0238   is_single_station |= (mode_v1 == 8);
0239 
0240   if (is_single_station) {
0241     return 0;
0242   }
0243 
0244   // Calculate Quality Based on ModeV2
0245   if (mode_v2 == 0) {
0246     // Single Hit
0247     if ((0 <= track.quality) && (track.quality <= 3)) {
0248       return 1;
0249     } else if ((4 <= track.quality) && (track.quality <= 7)) {
0250       return 2;
0251     } else if (7 < track.quality) {
0252       return 3;
0253     }
0254 
0255     return 0;
0256   } else if (mode_v2 == 4) {
0257     // Triple Muon Quality
0258     if ((8 <= track.quality) && (track.quality <= 11)) {
0259       return 5;
0260     } else if ((12 <= track.quality) && (track.quality <= 15)) {
0261       return 6;
0262     } else if (15 < track.quality) {
0263       return 7;
0264     }
0265 
0266     return 4;
0267   } else if (mode_v2 == 8) {
0268     // Double Muon Quality
0269     bool valid_mode = (mode_v1 == 9);
0270     valid_mode |= (mode_v1 == 10);
0271     valid_mode |= (mode_v1 == 12);
0272 
0273     if (valid_mode) {
0274       if ((16 <= track.quality) && (track.quality <= 23)) {
0275         return 9;
0276       } else if ((24 <= track.quality) && (track.quality <= 31)) {
0277         return 10;
0278       } else if (31 < track.quality) {
0279         return 11;
0280       }
0281     }
0282 
0283     return 8;
0284   } else if (mode_v2 == 12) {
0285     // Single Muon Quality
0286     bool valid_mode = (mode_v1 == 11);
0287     valid_mode |= (mode_v1 == 13);
0288     valid_mode |= (mode_v1 == 14);
0289     valid_mode |= (mode_v1 == 15);
0290 
0291     if (valid_mode) {
0292       if ((32 <= track.quality) && (track.quality <= 39)) {
0293         return 13;
0294       } else if ((40 <= track.quality) && (track.quality <= 51)) {
0295         return 14;
0296       } else if (51 < track.quality) {
0297         return 15;
0298       }
0299     }
0300 
0301     return 12;
0302   }
0303 
0304   // Invalid track
0305   return 0;
0306 }