Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:47

0001 #include "L1Trigger/DTTriggerPhase2/interface/MPCorFilter.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 
0004 using namespace edm;
0005 using namespace std;
0006 using namespace cmsdt;
0007 
0008 // ============================================================================
0009 // Constructors and destructor
0010 // ============================================================================
0011 MPCorFilter::MPCorFilter(const ParameterSet &pset)
0012     : MPFilter(pset), debug_(pset.getUntrackedParameter<bool>("debug")) {}
0013 
0014 // ============================================================================
0015 // Main methods (initialise, run, finish)
0016 // ============================================================================
0017 void MPCorFilter::initialise(const edm::EventSetup &iEventSetup) {}
0018 
0019 void MPCorFilter::run(edm::Event &iEvent,
0020                       const edm::EventSetup &iEventSetup,
0021                       std::vector<metaPrimitive> &inSLMPaths,
0022                       std::vector<metaPrimitive> &inCorMPaths,
0023                       std::vector<metaPrimitive> &outMPaths) {
0024   if (debug_)
0025     LogDebug("MPCorFilter") << "MPCorFilter: run";
0026 
0027   std::vector<metaPrimitive> SL1metaPrimitives;
0028   std::vector<metaPrimitive> SL2metaPrimitives;
0029   std::vector<metaPrimitive> SL3metaPrimitives;
0030   std::vector<metaPrimitive> CormetaPrimitives;
0031   uint32_t sl1Id_rawid = -1, sl2Id_rawid = -1, sl3Id_rawid = -1;
0032   if (!inSLMPaths.empty()) {
0033     int dum_sl_rawid = inSLMPaths[0].rawId;
0034     DTSuperLayerId dumSlId(dum_sl_rawid);
0035 
0036     max_drift_tdc = maxdriftinfo_[dumSlId.wheel() + 2][dumSlId.station() - 1][dumSlId.sector() - 1];
0037     DTChamberId ChId(dumSlId.wheel(), dumSlId.station(), dumSlId.sector());
0038     DTSuperLayerId sl1Id(ChId.rawId(), 1);
0039     sl1Id_rawid = sl1Id.rawId();
0040     DTSuperLayerId sl2Id(ChId.rawId(), 2);
0041     sl2Id_rawid = sl2Id.rawId();
0042     DTSuperLayerId sl3Id(ChId.rawId(), 3);
0043     sl3Id_rawid = sl3Id.rawId();
0044 
0045     for (const auto &metaprimitiveIt : inSLMPaths) {
0046       if (metaprimitiveIt.rawId == sl1Id_rawid) {
0047         SL1metaPrimitives.push_back(metaprimitiveIt);
0048       } else if (metaprimitiveIt.rawId == sl3Id_rawid)
0049         SL3metaPrimitives.push_back(metaprimitiveIt);
0050       else if (metaprimitiveIt.rawId == sl2Id_rawid)
0051         SL2metaPrimitives.push_back(metaprimitiveIt);
0052     }
0053   }
0054   auto filteredMPs = filter(SL1metaPrimitives, SL2metaPrimitives, SL3metaPrimitives, inCorMPaths);
0055   for (auto &mp : filteredMPs)
0056     outMPaths.push_back(mp);
0057 }
0058 
0059 void MPCorFilter::finish(){};
0060 
0061 ///////////////////////////
0062 ///  OTHER METHODS
0063 
0064 std::vector<metaPrimitive> MPCorFilter::filter(std::vector<metaPrimitive> SL1mps,
0065                                                std::vector<metaPrimitive> SL2mps,
0066                                                std::vector<metaPrimitive> SL3mps,
0067                                                std::vector<metaPrimitive> Cormps) {
0068   std::map<int, valid_cor_tp_arr_t> mp_valid_per_bx;
0069   std::map<int, int> imp_per_bx_sl1;
0070   for (auto &mp : SL1mps) {
0071     int BX = mp.t0 / 25;
0072     if (mp_valid_per_bx.find(BX) == mp_valid_per_bx.end()) {
0073       mp_valid_per_bx[BX] = valid_cor_tp_arr_t(12);
0074     }
0075 
0076     if (imp_per_bx_sl1.find(BX) == imp_per_bx_sl1.end()) {
0077       imp_per_bx_sl1[BX] = 0;
0078     }
0079 
0080     auto coarsed = coarsify(mp, 1);
0081     mp_valid_per_bx[BX][imp_per_bx_sl1[BX]] = valid_cor_tp_t({true, mp, coarsed[3], coarsed[4], coarsed[5]});
0082     imp_per_bx_sl1[BX] += 2;
0083   }
0084   std::map<int, int> imp_per_bx_sl3;
0085   for (auto &mp : SL3mps) {
0086     int BX = mp.t0 / 25;
0087     if (mp_valid_per_bx.find(BX) == mp_valid_per_bx.end()) {
0088       mp_valid_per_bx[BX] = valid_cor_tp_arr_t(12);
0089     }
0090 
0091     if (imp_per_bx_sl3.find(BX) == imp_per_bx_sl3.end()) {
0092       imp_per_bx_sl3[BX] = 1;
0093     }
0094 
0095     auto coarsed = coarsify(mp, 3);
0096     mp_valid_per_bx[BX][imp_per_bx_sl3[BX]] = valid_cor_tp_t({true, mp, coarsed[3], coarsed[4], coarsed[5]});
0097     imp_per_bx_sl3[BX] += 2;
0098   }
0099 
0100   for (auto &mp : Cormps) {
0101     int BX = mp.t0 / 25;
0102     if (mp_valid_per_bx.find(BX) == mp_valid_per_bx.end()) {
0103       mp_valid_per_bx[BX] = valid_cor_tp_arr_t(12);
0104     }
0105     auto coarsed = coarsify(mp, 0);
0106     if (isDead(mp, coarsed, mp_valid_per_bx))
0107       continue;
0108     auto index = killTps(mp, coarsed, BX, mp_valid_per_bx);
0109     mp_valid_per_bx[BX][index] = valid_cor_tp_t({true, mp, coarsed[3], coarsed[4], coarsed[5]});
0110   }
0111 
0112   std::vector<metaPrimitive> outTPs;
0113   for (auto &elem : mp_valid_per_bx) {
0114     for (auto &mp_valid : elem.second) {
0115       if (mp_valid.valid) {
0116         outTPs.push_back(mp_valid.mp);
0117       }
0118     }
0119   }
0120 
0121   for (auto &mp : SL2mps)
0122     outTPs.push_back(mp);
0123   return outTPs;
0124 }
0125 
0126 std::vector<int> MPCorFilter::coarsify(cmsdt::metaPrimitive mp, int sl) {
0127   float pos_ch_f = mp.x;
0128 
0129   // translating into tdc counts
0130   int pos_ch = int(round(pos_ch_f));
0131   int slope = (int)(mp.tanPhi);
0132 
0133   std::vector<int> t0_slv, t0_coarse, pos_slv, pos_coarse, slope_slv, slope_coarse;
0134   vhdl_int_to_unsigned(mp.t0, t0_slv);
0135   vhdl_int_to_signed(pos_ch, pos_slv);
0136   vhdl_int_to_signed(slope, slope_slv);
0137 
0138   vhdl_resize_unsigned(t0_slv, WIDTH_FULL_TIME);
0139   vhdl_resize_signed(pos_slv, WIDTH_FULL_POS);
0140   vhdl_resize_signed(slope_slv, WIDTH_FULL_SLOPE);
0141 
0142   t0_coarse = vhdl_slice(t0_slv, FSEG_T0_BX_LSB + 4, FSEG_T0_DISCARD_LSB);
0143   pos_coarse = vhdl_slice(pos_slv, WIDTH_FULL_POS - 1, FSEG_POS_DISCARD_LSB);
0144   slope_coarse = vhdl_slice(slope_slv, WIDTH_FULL_SLOPE - 1, FSEG_SLOPE_DISCARD_LSB);
0145 
0146   std::vector<int> results;
0147   int t0_coarse_int = vhdl_unsigned_to_int(t0_coarse);
0148   int pos_coarse_int = vhdl_signed_to_int(pos_coarse);
0149   int slope_coarse_int = vhdl_signed_to_int(slope_coarse);
0150 
0151   for (int index = 0; index <= 2; index++) {
0152     auto aux_t0_coarse_int =
0153         (t0_coarse_int + (index - 1)) % (int)std::pow(2, FSEG_T0_BX_LSB + 4 - (FSEG_T0_DISCARD_LSB));
0154     auto aux_pos_coarse_int = pos_coarse_int + (index - 1);
0155     auto aux_slope_coarse_int = slope_coarse_int + (index - 1);
0156     results.push_back(aux_t0_coarse_int);
0157     results.push_back(aux_pos_coarse_int);
0158     results.push_back(aux_slope_coarse_int);
0159   }
0160   return results;
0161 }
0162 
0163 int MPCorFilter::match(cmsdt::metaPrimitive mp, std::vector<int> coarsed, valid_cor_tp_t valid_cor_tp2) {
0164   bool matched = ((coarsed[0] == valid_cor_tp2.coarsed_t0 || coarsed[3] == valid_cor_tp2.coarsed_t0 ||
0165                    coarsed[6] == valid_cor_tp2.coarsed_t0) &&
0166                   (coarsed[1] == valid_cor_tp2.coarsed_pos || coarsed[4] == valid_cor_tp2.coarsed_pos ||
0167                    coarsed[7] == valid_cor_tp2.coarsed_pos) &&
0168                   (coarsed[2] == valid_cor_tp2.coarsed_slope || coarsed[5] == valid_cor_tp2.coarsed_slope ||
0169                    coarsed[8] == valid_cor_tp2.coarsed_slope)) &&
0170                  (abs(mp.t0 / 25 - valid_cor_tp2.mp.t0 / 25) <= 1);
0171   return ((int)matched) * 2 + (int)(mp.quality > valid_cor_tp2.mp.quality) +
0172          (int)(mp.quality == valid_cor_tp2.mp.quality) * (int)(get_chi2(mp) < get_chi2(valid_cor_tp2.mp));
0173 }
0174 
0175 bool MPCorFilter::isDead(cmsdt::metaPrimitive mp,
0176                          std::vector<int> coarsed,
0177                          std::map<int, valid_cor_tp_arr_t> tps_per_bx) {
0178   for (auto &elem : tps_per_bx) {
0179     for (auto &mp_valid : elem.second) {
0180       if (!mp_valid.valid)
0181         continue;
0182       int isMatched = match(mp, coarsed, mp_valid);
0183       if (isMatched == 2)
0184         return true;  // matched and quality <= stored tp
0185     }
0186   }
0187   return false;
0188 }
0189 
0190 int MPCorFilter::killTps(cmsdt::metaPrimitive mp,
0191                          std::vector<int> coarsed,
0192                          int bx,
0193                          std::map<int, valid_cor_tp_arr_t> &tps_per_bx) {
0194   int index_to_occupy = -1;
0195   int index_to_kill = -1;
0196   for (auto &elem : tps_per_bx) {
0197     if (abs(bx - elem.first) > 2)
0198       continue;
0199     for (size_t i = 0; i < elem.second.size(); i++) {
0200       if (elem.second[i].valid == 1) {
0201         int isMatched = match(mp, coarsed, elem.second[i]);
0202         if (isMatched == 3) {
0203           elem.second[i].valid = false;
0204           if (elem.first == bx && index_to_kill == -1)
0205             index_to_kill = i;
0206         }
0207       } else if (elem.first == bx && index_to_occupy == -1)
0208         index_to_occupy = i;
0209     }
0210   }
0211   // My first option is to replace the one from my BX that I killed first
0212   if (index_to_kill != -1)
0213     return index_to_kill;
0214   // If I wasn't able to kill anyone from my BX, I fill the first empty space
0215   return index_to_occupy;
0216 }
0217 
0218 int MPCorFilter::get_chi2(cmsdt::metaPrimitive mp) {
0219   // chi2 is coarsified to the index of the chi2's highest bit set to 1
0220 
0221   int chi2 = (int)round(mp.chi2 / (std::pow(((float)CELL_SEMILENGTH / (float)max_drift_tdc), 2) / 100));
0222 
0223   std::vector<int> chi2_unsigned, chi2_unsigned_msb;
0224   vhdl_int_to_unsigned(chi2, chi2_unsigned);
0225 
0226   for (int i = (int)chi2_unsigned.size() - 1; i >= 0; i--) {
0227     if (chi2_unsigned[i] == 1) {
0228       return i;
0229     }
0230   }
0231   return -1;
0232 }
0233 
0234 void MPCorFilter::printmP(metaPrimitive mP) {
0235   DTSuperLayerId slId(mP.rawId);
0236   LogDebug("MPCorFilter") << slId << "\t"
0237                           << " " << setw(2) << left << mP.wi1 << " " << setw(2) << left << mP.wi2 << " " << setw(2)
0238                           << left << mP.wi3 << " " << setw(2) << left << mP.wi4 << " " << setw(5) << left << mP.tdc1
0239                           << " " << setw(5) << left << mP.tdc2 << " " << setw(5) << left << mP.tdc3 << " " << setw(5)
0240                           << left << mP.tdc4 << " " << setw(10) << right << mP.x << " " << setw(9) << left << mP.tanPhi
0241                           << " " << setw(5) << left << mP.t0 << " " << setw(13) << left << mP.chi2;
0242 }