File indexing completed on 2024-09-07 04:36:46
0001 #include "L1Trigger/DTTriggerPhase2/interface/MPQualityEnhancerFilter.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
0011
0012 MPQualityEnhancerFilter::MPQualityEnhancerFilter(const ParameterSet &pset)
0013 : MPFilter(pset), debug_(pset.getUntrackedParameter<bool>("debug")) {}
0014
0015
0016
0017
0018 void MPQualityEnhancerFilter::initialise(const edm::EventSetup &iEventSetup) {}
0019
0020 void MPQualityEnhancerFilter::run(edm::Event &iEvent,
0021 const edm::EventSetup &iEventSetup,
0022 std::vector<metaPrimitive> &inMPaths,
0023 std::vector<metaPrimitive> &outMPaths) {
0024 std::vector<metaPrimitive> buff;
0025 std::vector<metaPrimitive> buff2;
0026
0027 filterCousins(inMPaths, buff);
0028 if (debug_) {
0029 LogDebug("MPQualityEnhancerFilter") << "Ended Cousins Filter. The final primitives before Refiltering are: ";
0030 for (unsigned int i = 0; i < buff.size(); i++) {
0031 printmP(buff[i]);
0032 }
0033 LogDebug("MPQualityEnhancerFilter") << "Total Primitives = " << buff.size();
0034 }
0035 refilteringCousins(buff, buff2);
0036 if (debug_) {
0037 LogDebug("MPQualityEnhancerFilter") << "Ended Cousins Refilter. The final primitives before UniqueFilter are: ";
0038 for (unsigned int i = 0; i < buff2.size(); i++) {
0039 printmP(buff2[i]);
0040 }
0041 LogDebug("MPQualityEnhancerFilter") << "Total Primitives = " << buff2.size();
0042 }
0043 filterUnique(buff2, outMPaths);
0044
0045 if (debug_) {
0046 LogDebug("MPQualityEnhancerFilter") << "Ended Unique Filter. The final primitives are: ";
0047 for (unsigned int i = 0; i < outMPaths.size(); i++) {
0048 printmP(outMPaths[i]);
0049 LogDebug("MPQualityEnhancerFilter");
0050 }
0051 LogDebug("MPQualityEnhancerFilter") << "Total Primitives = " << outMPaths.size();
0052 }
0053
0054 buff.clear();
0055 buff2.clear();
0056 }
0057
0058 void MPQualityEnhancerFilter::finish() {}
0059
0060
0061
0062 int MPQualityEnhancerFilter::areCousins(metaPrimitive mp, metaPrimitive second_mp) {
0063 if (mp.rawId != second_mp.rawId)
0064 return 0;
0065 if (mp.wi1 == second_mp.wi1 and mp.wi1 != -1 and mp.tdc1 != -1)
0066 return 1;
0067 if (mp.wi2 == second_mp.wi2 and mp.wi2 != -1 and mp.tdc2 != -1)
0068 return 2;
0069 if (mp.wi3 == second_mp.wi3 and mp.wi3 != -1 and mp.tdc3 != -1)
0070 return 3;
0071 if (mp.wi4 == second_mp.wi4 and mp.wi4 != -1 and mp.tdc4 != -1)
0072 return 4;
0073 return 0;
0074 }
0075
0076 int MPQualityEnhancerFilter::rango(metaPrimitive mp) {
0077 if (mp.quality == 1 or mp.quality == 2)
0078 return 3;
0079 if (mp.quality == 3 or mp.quality == 4)
0080 return 4;
0081 return 0;
0082 }
0083
0084 void MPQualityEnhancerFilter::filterCousins(std::vector<metaPrimitive> &inMPaths,
0085 std::vector<metaPrimitive> &outMPaths) {
0086 int primo_index = 0;
0087 bool oneof4 = false;
0088 int bestI = -1;
0089 double bestChi2 = 9999;
0090 if (inMPaths.size() == 1) {
0091 if (debug_) {
0092 printmP(inMPaths[0]);
0093 }
0094 outMPaths.push_back(inMPaths[0]);
0095 } else if (inMPaths.size() > 1) {
0096 for (int i = 0; i < int(inMPaths.size()); i++) {
0097 if (debug_) {
0098 printmP(inMPaths[i]);
0099 }
0100 if (areCousins(inMPaths[i], inMPaths[i - primo_index]) == 0) {
0101 if (oneof4) {
0102 outMPaths.push_back(inMPaths[bestI]);
0103
0104 bestI = -1;
0105 bestChi2 = 9999;
0106 oneof4 = false;
0107 } else {
0108 for (int j = i - primo_index; j < i; j++) {
0109 outMPaths.push_back(inMPaths[j]);
0110 }
0111 }
0112 i--;
0113 primo_index = 0;
0114 continue;
0115 }
0116 if (rango(inMPaths[i]) == 4) {
0117 oneof4 = true;
0118 if (bestChi2 > inMPaths[i].chi2) {
0119 bestChi2 = inMPaths[i].chi2;
0120 bestI = i;
0121 }
0122 }
0123 if (areCousins(inMPaths[i], inMPaths[i + 1]) != 0) {
0124 primo_index++;
0125 } else {
0126 if (oneof4) {
0127 outMPaths.push_back(inMPaths[bestI]);
0128 bestI = -1;
0129 bestChi2 = 9999;
0130 oneof4 = false;
0131 } else {
0132 for (int j = i - primo_index; j <= i; j++) {
0133 outMPaths.push_back(inMPaths[j]);
0134 }
0135 }
0136 primo_index = 0;
0137 }
0138 }
0139 }
0140
0141 }
0142
0143 void MPQualityEnhancerFilter::refilteringCousins(std::vector<metaPrimitive> &inMPaths,
0144 std::vector<metaPrimitive> &outMPaths) {
0145 if (debug_)
0146 LogDebug("MPQualityEnhancerFilter") << "filtering: starting cousins refiltering\n";
0147 int bestI = -1;
0148 double bestChi2 = 9999;
0149 bool oneOf4 = false;
0150 int back = 0;
0151
0152 if (inMPaths.size() > 1) {
0153 for (unsigned int i = 0; i < inMPaths.size(); i++) {
0154 if (debug_) {
0155 LogDebug("MPQualityEnhancerFilter") << "filtering: starting with mp " << i << ": ";
0156 printmP(inMPaths[i]);
0157 LogDebug("MPQualityEnhancerFilter");
0158 }
0159 if (rango(inMPaths[i]) == 4 && bestChi2 > inMPaths[i].chi2) {
0160 if (debug_) {
0161 LogDebug("MPQualityEnhancerFilter") << "filtering: mp " << i << " is the best 4h primitive";
0162 }
0163 oneOf4 = true;
0164 bestI = i;
0165 bestChi2 = inMPaths[i].chi2;
0166 }
0167 if (i == inMPaths.size() - 1) {
0168 if (oneOf4) {
0169 outMPaths.push_back(inMPaths[bestI]);
0170 } else {
0171 for (unsigned int j = i - back; j <= i; j++) {
0172 outMPaths.push_back(inMPaths[j]);
0173 }
0174 }
0175 } else {
0176 if (areCousins(inMPaths[i], inMPaths[i + 1]) == 0) {
0177 if (debug_) {
0178 LogDebug("MPQualityEnhancerFilter") << "mp " << i << " and mp " << i + 1 << " are not cousins";
0179 }
0180 if (oneOf4) {
0181 outMPaths.push_back(inMPaths[bestI]);
0182 if (debug_)
0183 LogDebug("MPQualityEnhancerFilter") << "kept4 mp " << bestI;
0184 oneOf4 = false;
0185 bestI = -1;
0186 bestChi2 = 9999;
0187 } else {
0188 for (unsigned int j = i - back; j <= i; j++) {
0189 outMPaths.push_back(inMPaths[j]);
0190 if (debug_)
0191 LogDebug("MPQualityEnhancerFilter") << "kept3 mp " << j;
0192 }
0193 }
0194 back = 0;
0195 } else {
0196 back++;
0197 }
0198 }
0199 }
0200 } else if (inMPaths.size() == 1) {
0201 outMPaths.push_back(inMPaths[0]);
0202 }
0203 }
0204
0205 void MPQualityEnhancerFilter::filterUnique(std::vector<metaPrimitive> &inMPaths,
0206 std::vector<metaPrimitive> &outMPaths) {
0207 constexpr double xTh = 0.001;
0208 constexpr double tPhiTh = 0.001;
0209 constexpr double t0Th = 0.001;
0210 for (size_t i = 0; i < inMPaths.size(); i++) {
0211 bool visto = false;
0212 for (size_t j = i + 1; j < inMPaths.size(); j++) {
0213 if ((std::abs(inMPaths[i].x - inMPaths[j].x) <= xTh) &&
0214 (std::abs(inMPaths[i].tanPhi - inMPaths[j].tanPhi) <= tPhiTh) &&
0215 (std::abs(inMPaths[i].t0 - inMPaths[j].t0) <= t0Th))
0216 visto = true;
0217 }
0218 if (!visto)
0219 outMPaths.push_back(inMPaths[i]);
0220 }
0221 }
0222
0223 void MPQualityEnhancerFilter::printmP(metaPrimitive mP) {
0224 DTSuperLayerId slId(mP.rawId);
0225 LogDebug("MPQualityEnhancerFilter") << slId << "\t"
0226 << " " << setw(2) << left << mP.wi1 << " " << setw(2) << left << mP.wi2 << " "
0227 << setw(2) << left << mP.wi3 << " " << setw(2) << left << mP.wi4 << " " << setw(5)
0228 << left << mP.tdc1 << " " << setw(5) << left << mP.tdc2 << " " << setw(5) << left
0229 << mP.tdc3 << " " << setw(5) << left << mP.tdc4 << " " << setw(10) << right
0230 << mP.x << " " << setw(9) << left << mP.tanPhi << " " << setw(5) << left << mP.t0
0231 << " " << setw(13) << left << mP.chi2 << " r:" << rango(mP);
0232 }