Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:36:46

0001 #include "L1Trigger/DTTriggerPhase2/interface/MPQualityEnhancerFilterBayes.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 
0004 using namespace edm;
0005 using namespace std;
0006 using namespace cmsdt;
0007 ;
0008 
0009 // ============================================================================
0010 // Constructors and destructor
0011 // ============================================================================
0012 MPQualityEnhancerFilterBayes::MPQualityEnhancerFilterBayes(const ParameterSet &pset)
0013     : MPFilter(pset), debug_(pset.getUntrackedParameter<bool>("debug")) {}
0014 
0015 // ============================================================================
0016 // Main methods (initialise, run, finish)
0017 // ============================================================================
0018 void MPQualityEnhancerFilterBayes::initialise(const edm::EventSetup &iEventSetup) {}
0019 
0020 void MPQualityEnhancerFilterBayes::run(edm::Event &iEvent,
0021                                        const edm::EventSetup &iEventSetup,
0022                                        std::vector<metaPrimitive> &inMPaths,
0023                                        std::vector<metaPrimitive> &outMPaths) {
0024   filterCousins(inMPaths, outMPaths);
0025   if (debug_) {
0026     LogDebug("MPQualityEnhancerFilterBayes") << "Ended Cousins Filter. The final primitives before Refiltering are: ";
0027     for (unsigned int i = 0; i < outMPaths.size(); i++) {
0028       printmP(outMPaths[i]);
0029     }
0030     LogDebug("MPQualityEnhancerFilterBayes") << "Total Primitives = " << outMPaths.size();
0031   }
0032 }
0033 
0034 void MPQualityEnhancerFilterBayes::finish() {}
0035 
0036 ///////////////////////////
0037 ///  OTHER METHODS
0038 int MPQualityEnhancerFilterBayes::areCousins(metaPrimitive mp, metaPrimitive second_mp) {
0039   DTSuperLayerId mpId(mp.rawId);
0040   DTSuperLayerId second_mpId(second_mp.rawId);
0041 
0042   int output = 0;
0043   if (mpId.wheel() != second_mpId.wheel() || mpId.station() != second_mpId.station() ||
0044       mpId.sector() != second_mpId.sector()) {
0045     return output;
0046   }
0047 
0048   if (mp.wi1 == second_mp.wi1 and mp.tdc1 == second_mp.tdc1 and mp.wi1 != -1 and mp.tdc1 != -1)
0049     output = 1;
0050   else if (mp.wi2 == second_mp.wi2 and mp.tdc2 == second_mp.tdc2 and mp.wi2 != -1 and mp.tdc2 != -1)
0051     output = 2;
0052   else if (mp.wi3 == second_mp.wi3 and mp.tdc3 == second_mp.tdc3 and mp.wi3 != -1 and mp.tdc3 != -1)
0053     output = 3;
0054   else if (mp.wi4 == second_mp.wi4 and mp.tdc4 == second_mp.tdc4 and mp.wi4 != -1 and mp.tdc4 != -1)
0055     output = 4;
0056   else if (mp.wi5 == second_mp.wi5 and mp.tdc5 == second_mp.tdc5 and mp.wi5 != -1 and mp.tdc5 != -1)
0057     output = 5;
0058   else if (mp.wi6 == second_mp.wi6 and mp.tdc6 == second_mp.tdc6 and mp.wi6 != -1 and mp.tdc6 != -1)
0059     output = 6;
0060   else if (mp.wi7 == second_mp.wi7 and mp.tdc7 == second_mp.tdc7 and mp.wi7 != -1 and mp.tdc7 != -1)
0061     output = 7;
0062   else if (mp.wi8 == second_mp.wi8 and mp.tdc8 == second_mp.tdc8 and mp.wi8 != -1 and mp.tdc8 != -1)
0063     output = 8;
0064 
0065   return output;
0066 }
0067 
0068 ///////////////////////////
0069 ///  OTHER METHODS
0070 bool MPQualityEnhancerFilterBayes::areSame(metaPrimitive mp, metaPrimitive second_mp) {
0071   if (mp.rawId != second_mp.rawId)
0072     return false;
0073   if (mp.wi1 != second_mp.wi1 or mp.tdc1 != second_mp.tdc1)
0074     return false;
0075   if (mp.wi2 != second_mp.wi2 or mp.tdc2 != second_mp.tdc2)
0076     return false;
0077   if (mp.wi3 != second_mp.wi3 or mp.tdc3 != second_mp.tdc3)
0078     return false;
0079   if (mp.wi4 != second_mp.wi4 or mp.tdc4 != second_mp.tdc4)
0080     return false;
0081   if (mp.wi5 != second_mp.wi5 or mp.tdc5 != second_mp.tdc5)
0082     return false;
0083   if (mp.wi6 != second_mp.wi6 or mp.tdc6 != second_mp.tdc6)
0084     return false;
0085   if (mp.wi7 != second_mp.wi7 or mp.tdc7 != second_mp.tdc7)
0086     return false;
0087   if (mp.wi8 != second_mp.wi8 or mp.tdc8 != second_mp.tdc8)
0088     return false;
0089   return true;
0090 }
0091 
0092 int MPQualityEnhancerFilterBayes::shareSL(metaPrimitive mp, metaPrimitive second_mp) {
0093   DTSuperLayerId mpId(mp.rawId);
0094   DTSuperLayerId second_mpId(second_mp.rawId);
0095 
0096   int output = 0;
0097   if (mpId.wheel() != second_mpId.wheel() || mpId.station() != second_mpId.station() ||
0098       mpId.sector() != second_mpId.sector()) {
0099     return output;
0100   }
0101 
0102   int SL1 = 0;
0103   int SL3 = 0;
0104 
0105   int SL1_shared = 0;
0106   int SL3_shared = 0;
0107 
0108   if (mp.wi1 != -1 and mp.tdc1 != -1) {
0109     ++SL1;
0110     if (mp.wi1 == second_mp.wi1 and mp.tdc1 == second_mp.tdc1) {
0111       ++SL1_shared;
0112     }
0113   }
0114   if (mp.wi2 != -1 and mp.tdc2 != -1) {
0115     ++SL1;
0116     if (mp.wi2 == second_mp.wi2 and mp.tdc2 == second_mp.tdc2) {
0117       ++SL1_shared;
0118     }
0119   }
0120   if (mp.wi3 != -1 and mp.tdc3 != -1) {
0121     ++SL1;
0122     if (mp.wi3 == second_mp.wi3 and mp.tdc3 == second_mp.tdc3) {
0123       ++SL1_shared;
0124     }
0125   }
0126   if (mp.wi4 != -1 and mp.tdc4 != -1) {
0127     ++SL1;
0128     if (mp.wi4 == second_mp.wi4 and mp.tdc4 == second_mp.tdc4) {
0129       ++SL1_shared;
0130     }
0131   }
0132 
0133   if (mp.wi5 != -1 and mp.tdc5 != -1) {
0134     ++SL3;
0135     if (mp.wi5 == second_mp.wi5 and mp.tdc5 == second_mp.tdc5) {
0136       ++SL3_shared;
0137     }
0138   }
0139   if (mp.wi6 != -1 and mp.tdc6 != -1) {
0140     ++SL3;
0141     if (mp.wi6 == second_mp.wi6 and mp.tdc6 == second_mp.tdc6) {
0142       ++SL3_shared;
0143     }
0144   }
0145   if (mp.wi7 != -1 and mp.tdc7 != -1) {
0146     ++SL3;
0147     if (mp.wi7 == second_mp.wi7 and mp.tdc7 == second_mp.tdc7) {
0148       ++SL3_shared;
0149     }
0150   }
0151   if (mp.wi8 != -1 and mp.tdc8 != -1) {
0152     ++SL3;
0153     if (mp.wi8 == second_mp.wi8 and mp.tdc8 == second_mp.tdc8) {
0154       ++SL3_shared;
0155     }
0156   }
0157 
0158   // If the two mp share all hits in a SL, we consider that they share that SL
0159   if (SL1_shared == SL1 || SL3_shared == SL3)
0160     output = 1;
0161 
0162   return output;
0163 }
0164 
0165 int MPQualityEnhancerFilterBayes::BX(metaPrimitive mp) {
0166   int bx;
0167   bx = (int)round(mp.t0 / (float)LHC_CLK_FREQ);
0168   return bx;
0169 }
0170 
0171 // Is this really needed?
0172 int MPQualityEnhancerFilterBayes::rango(metaPrimitive mp) {
0173   // Correlated
0174   if (mp.quality > 5)
0175     return 2;
0176   // uncorrelated
0177   else
0178     return 1;
0179 }
0180 
0181 void MPQualityEnhancerFilterBayes::filterCousins(std::vector<metaPrimitive> &inMPaths,
0182                                                  std::vector<metaPrimitive> &outMPaths) {
0183   // At the beginning, we want to keep all mpaths
0184   std::vector<bool> keep_this(inMPaths.size(), true);
0185 
0186   // If we have just one mpath, save it
0187   if (inMPaths.size() == 1) {
0188     if (debug_) {
0189       printmP(inMPaths[0]);
0190     }
0191     outMPaths.push_back(inMPaths[0]);
0192   }
0193   // More than one mpath
0194   else if (inMPaths.size() > 1) {
0195     for (int i = 0; i < int(inMPaths.size()); i++) {
0196       if (debug_) {
0197         printmP(inMPaths[i]);
0198       }
0199       // If we have already decided to reject the candidate, skip it
0200       if (keep_this[i] == false)
0201         continue;
0202       for (int j = i + 1; j < int(inMPaths.size()); j++) {
0203         // If we have already decided to reject the candidate, skip it
0204         if (keep_this[j] == false)
0205           continue;
0206         // Case they are the same, keep the first one
0207         if (areSame(inMPaths[i], inMPaths[j]) == true)
0208           keep_this[i] = false;
0209         // Case they are cousins, keep the best one
0210         if (areCousins(inMPaths[i], inMPaths[j]) != 0) {
0211           // In case both are correlated, they have to share a full SL
0212           if (inMPaths[i].quality > 5 && inMPaths[j].quality > 5 && shareSL(inMPaths[i], inMPaths[j]) == 0) {
0213             continue;
0214           }
0215 
0216           // Compare only if rango is the same (both correlated or both not-correlated)
0217           // if (rango(inMPaths[i]) != rango(inMPaths[j])) continue;
0218 
0219           // If rango is the same, keep higher quality one
0220           // Still, keep lower-quality one if it has lower Chi2
0221           // and if its BX is different to the higher-quality one
0222           if (inMPaths[i].quality > inMPaths[j].quality) {
0223             if ((inMPaths[i].chi2 < inMPaths[j].chi2) || BX(inMPaths[i]) == BX(inMPaths[j]))
0224               keep_this[j] = false;
0225           } else if (inMPaths[i].quality < inMPaths[j].quality) {
0226             if ((inMPaths[i].chi2 > inMPaths[j].chi2) || BX(inMPaths[i]) == BX(inMPaths[j]))
0227               keep_this[i] = false;
0228           } else {  // if they have same quality
0229             // If quality is 8, keep both
0230             // if (inMPaths[i].quality >= 8) continue;
0231             // Otherwise, keep the one with better Chi2
0232             // and also the one with worse Chi2 if its BX is different
0233             // else{
0234             if (inMPaths[i].chi2 > inMPaths[j].chi2 && BX(inMPaths[i]) == BX(inMPaths[j]))
0235               keep_this[i] = false;
0236             else if (inMPaths[i].chi2 < inMPaths[j].chi2 && BX(inMPaths[i]) == BX(inMPaths[j]))
0237               keep_this[j] = false;
0238             else
0239               continue;
0240             //}
0241           }
0242         }
0243       }
0244     }
0245     // Finally, fill the output with accepted candidates
0246     for (int i = 0; i < int(inMPaths.size()); i++)
0247       if (keep_this[i] == true)
0248         outMPaths.push_back(inMPaths[i]);
0249   }
0250 }
0251 
0252 void MPQualityEnhancerFilterBayes::printmP(metaPrimitive mP) {
0253   DTSuperLayerId slId(mP.rawId);
0254   LogDebug("MPQualityEnhancerFilterBayes")
0255       << slId << "\t"
0256       << " " << setw(2) << left << mP.wi1 << " " << setw(2) << left << mP.wi2 << " " << setw(2) << left << mP.wi3 << " "
0257       << setw(2) << left << mP.wi4 << " " << setw(5) << left << mP.tdc1 << " " << setw(5) << left << mP.tdc2 << " "
0258       << setw(5) << left << mP.tdc3 << " " << setw(5) << left << mP.tdc4 << " " << setw(10) << right << mP.x << " "
0259       << setw(9) << left << mP.tanPhi << " " << setw(5) << left << mP.t0 << " " << setw(13) << left << mP.chi2
0260       << " r:" << rango(mP);
0261 }