File indexing completed on 2024-09-11 04:32:54
0001 #include "DQMOffline/Trigger/interface/EgHLTTrigTools.h"
0002 #include "FWCore/ParameterSet/interface/Registry.h"
0003 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0004
0005 #include <boost/algorithm/string.hpp>
0006 using namespace egHLT;
0007
0008 TrigCodes::TrigBitSet trigTools::getFiltersPassed(const std::vector<std::pair<std::string, int> >& filters,
0009 const trigger::TriggerEvent* trigEvt,
0010 const std::string& hltTag,
0011 const TrigCodes& trigCodes) {
0012 TrigCodes::TrigBitSet evtTrigs;
0013 for (auto const& filter : filters) {
0014 size_t filterNrInEvt = trigEvt->filterIndex(edm::InputTag(filter.first, "", hltTag));
0015 const TrigCodes::TrigBitSet filterCode = trigCodes.getCode(filter.first.c_str());
0016 if (filterNrInEvt <
0017 trigEvt
0018 ->sizeFilters()) {
0019 const trigger::Keys& trigKeys = trigEvt->filterKeys(filterNrInEvt);
0020 if (static_cast<int>(trigKeys.size()) >= filter.second) {
0021 evtTrigs |= filterCode;
0022 }
0023 }
0024 }
0025
0026 return evtTrigs;
0027 }
0028
0029
0030
0031
0032
0033
0034
0035
0036 std::vector<int> trigTools::getMinNrObjsRequiredByFilter(const std::vector<std::string>& filterNames) {
0037 std::vector<int> retVal(filterNames.size(), -2);
0038 const std::string mag0("@module_label");
0039 const std::string mag1("ncandcut");
0040 const std::string mag2("nZcandcut");
0041 const std::string mag3("MinN");
0042 const std::string mag4("minN");
0043
0044 std::vector<std::string> filterEntryStrings;
0045 filterEntryStrings.reserve(filterNames.size());
0046 for (auto const& filterName : filterNames) {
0047 const edm::Entry filterEntry(mag0, filterName, true);
0048 filterEntryStrings.push_back(filterEntry.toString());
0049 }
0050
0051
0052 const edm::pset::Registry* psetRegistry = edm::pset::Registry::instance();
0053 if (psetRegistry == nullptr) {
0054 retVal = std::vector<int>(filterNames.size(), -1);
0055 return retVal;
0056 }
0057 for (auto& psetIt : *psetRegistry) {
0058 const auto& mapOfPara =
0059 psetIt.second.tbl();
0060 const auto itToModLabel = mapOfPara.find(mag0);
0061 if (itToModLabel != mapOfPara.end()) {
0062 std::string itString = itToModLabel->second.toString();
0063
0064 for (unsigned int i = 0; i < filterNames.size(); i++) {
0065 if (retVal[i] == -1)
0066 continue;
0067
0068 if (itString ==
0069 filterEntryStrings[i]) {
0070 auto itToCandCut = mapOfPara.find(mag1);
0071 if (itToCandCut != mapOfPara.end() && itToCandCut->second.typeCode() == 'I')
0072 retVal[i] = itToCandCut->second.getInt32();
0073 else {
0074 itToCandCut = mapOfPara.find(mag2);
0075 if (itToCandCut != mapOfPara.end() && itToCandCut->second.typeCode() == 'I')
0076 retVal[i] = itToCandCut->second.getInt32();
0077 else {
0078 itToCandCut = mapOfPara.find(mag3);
0079 if (itToCandCut != mapOfPara.end() && itToCandCut->second.typeCode() == 'I')
0080 retVal[i] = itToCandCut->second.getInt32();
0081 else {
0082 itToCandCut = mapOfPara.find(mag4);
0083 if (itToCandCut != mapOfPara.end() && itToCandCut->second.typeCode() == 'I')
0084 retVal[i] = itToCandCut->second.getInt32();
0085 else
0086 retVal[i] = -1;
0087 }
0088 }
0089 }
0090 }
0091 }
0092 }
0093 }
0094 for (unsigned int i = 0; i < filterNames.size(); i++)
0095 if (retVal[i] == -2)
0096 retVal[i] = -1;
0097 return retVal;
0098 }
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129 void trigTools::getActiveFilters(const HLTConfigProvider& hltConfig,
0130 std::vector<std::string>& activeFilters,
0131 std::vector<std::string>& activeEleFilters,
0132 std::vector<std::string>& activeEle2LegFilters,
0133 std::vector<std::string>& activePhoFilters,
0134 std::vector<std::string>& activePho2LegFilters) {
0135 activeFilters.clear();
0136 activeEleFilters.clear();
0137 activeEle2LegFilters.clear();
0138 activePhoFilters.clear();
0139 activePho2LegFilters.clear();
0140
0141 for (size_t pathNr = 0; pathNr < hltConfig.size(); pathNr++) {
0142 const std::string& pathName = hltConfig.triggerName(pathNr);
0143
0144 if (pathName.find("HLT_") == 0) {
0145 if ((pathName.find("Photon") == 4 || pathName.find("Ele") == 4 || pathName.find("EG") != pathName.npos ||
0146 pathName.find("PAPhoton") == 4 || pathName.find("PAEle") == 4 || pathName.find("PASinglePhoton") == 4 ||
0147 pathName.find("HIPhoton") == 4 || pathName.find("HIEle") == 4 || pathName.find("HISinglePhoton") == 4 ||
0148 pathName.find("Activity") == 4 || pathName.find("Physics") == 4 || pathName.find("DiSC") == 4) &&
0149 (pathName.find("Jet") == pathName.npos && pathName.find("Muon") == pathName.npos &&
0150 pathName.find("Tau") == pathName.npos && pathName.find("HT") == pathName.npos &&
0151 pathName.find("MR") == pathName.npos && pathName.find("LEITI") == pathName.npos &&
0152 pathName.find("Jpsi") == pathName.npos && pathName.find("Ups") == pathName.npos)) {
0153
0154 const std::vector<std::string>& filters = hltConfig.saveTagsModules(pathNr);
0155
0156
0157
0158
0159 if (!filters.empty()) {
0160
0161
0162 std::vector<int> minNRFFCache = getMinNrObjsRequiredByFilter(filters);
0163
0164 for (size_t filter = 0; filter < filters.size(); filter++) {
0165
0166 if (filters[filter].find("Filter") !=
0167 filters[filter].npos) {
0168
0169 int minNRFF = minNRFFCache[filter];
0170 int minNRFFP1 = -99;
0171 if (filter < filters.size() - 1)
0172 minNRFFP1 = minNRFFCache[filter + 1];
0173 if (
0174 (minNRFF == 1 && minNRFFP1 == 2) ||
0175 (minNRFF == 1 && minNRFFP1 == 1 && filters[filter + 1].find("Mass") != filters[filter + 1].npos) ||
0176 (minNRFF == 1 && minNRFFP1 == 1 && filters[filter + 1].find("FEM") != filters[filter + 1].npos) ||
0177 (minNRFF == 1 && minNRFFP1 == 1 && filters[filter + 1].find("PFMT") != filters[filter + 1].npos) ||
0178 filter == filters.size() - 1) {
0179 activeFilters.push_back(filters[filter]);
0180
0181
0182 if (pathName.find("Photon") != pathName.npos || pathName.find("Activity") != pathName.npos ||
0183 pathName.find("Physics") != pathName.npos || pathName.find("DiSC") == 4) {
0184 activePhoFilters.push_back(filters[filter]);
0185 int posPho = pathName.find("Pho") + 1;
0186 if (pathName.find("Pho", posPho) != pathName.npos || pathName.find("SC", posPho) != pathName.npos) {
0187
0188 activePho2LegFilters.push_back(filters[filter]);
0189
0190 }
0191 }
0192 if (pathName.find("Ele") != pathName.npos || pathName.find("Activity") != pathName.npos ||
0193 pathName.find("Physics") != pathName.npos) {
0194 activeEleFilters.push_back(filters[filter]);
0195 int posEle = pathName.find("Ele") + 1;
0196 if (pathName.find("Ele", posEle) != pathName.npos || pathName.find("SC", posEle) != pathName.npos) {
0197 if ((minNRFF == 1 && minNRFFP1 == 2) ||
0198 (minNRFF == 1 && minNRFFP1 == 1 &&
0199 filters[filter + 1].find("Mass") != filters[filter + 1].npos) ||
0200 (minNRFF == 1 && minNRFFP1 == 1 &&
0201 filters[filter + 1].find("SC") != filters[filter + 1].npos) ||
0202 (minNRFF == 1 && minNRFFP1 == 1 &&
0203 filters[filter + 1].find("FEM") != filters[filter + 1].npos)) {
0204
0205 activeEle2LegFilters.push_back(filters[filter] + "::" + filters[filter + 1]);
0206
0207 }
0208 }
0209 }
0210 }
0211 }
0212 }
0213
0214
0215 }
0216 }
0217 }
0218 }
0219
0220
0221
0222
0223 std::sort(activeFilters.begin(), activeFilters.end());
0224 std::sort(activeEleFilters.begin(), activeEleFilters.end());
0225 std::sort(activeEle2LegFilters.begin(), activeEle2LegFilters.end());
0226 std::sort(activePhoFilters.begin(), activePhoFilters.end());
0227 }
0228
0229
0230
0231
0232
0233 void trigTools::filterInactiveTriggers(std::vector<std::string>& namesToFilter,
0234 std::vector<std::string>& activeFilters) {
0235
0236 std::vector<std::string> filteredNames;
0237
0238
0239
0240
0241
0242
0243
0244
0245 filteredNames = activeFilters;
0246 namesToFilter.swap(filteredNames);
0247 }
0248
0249
0250 void trigTools::filterInactiveTightLooseTriggers(std::vector<std::string>& namesToFilter,
0251 const std::vector<std::string>& activeFilters) {
0252
0253 std::vector<std::string> filteredNames;
0254
0255 for (auto& inputFilterNr : namesToFilter) {
0256 std::vector<std::string> names;
0257 boost::split(names, inputFilterNr, boost::is_any_of(std::string(":")));
0258 if (names.size() != 2)
0259 continue;
0260 if (std::binary_search(activeFilters.begin(), activeFilters.end(), names[0]) &&
0261 std::binary_search(activeFilters.begin(), activeFilters.end(), names[1])) {
0262 filteredNames.push_back(inputFilterNr);
0263 }
0264 }
0265
0266 namesToFilter.swap(filteredNames);
0267 }
0268
0269
0270
0271 class StringPairCompare {
0272 public:
0273 bool operator()(const std::pair<std::string, std::string>& lhs,
0274 const std::pair<std::string, std::string>& rhs) const {
0275 return keyLess(lhs.first, rhs.first);
0276 }
0277 bool operator()(const std::pair<std::string, std::string>& lhs,
0278 const std::pair<std::string, std::string>::first_type& rhs) const {
0279 return keyLess(lhs.first, rhs);
0280 }
0281 bool operator()(const std::pair<std::string, std::string>::first_type& lhs,
0282 const std::pair<std::string, std::string>& rhs) const {
0283 return keyLess(lhs, rhs.first);
0284 }
0285
0286 private:
0287 bool keyLess(const std::pair<std::string, std::string>::first_type& k1,
0288 const std::pair<std::string, std::string>::first_type& k2) const {
0289 return k1 < k2;
0290 }
0291 };
0292
0293 void trigTools::translateFiltersToPathNames(const HLTConfigProvider& hltConfig,
0294 const std::vector<std::string>& filters,
0295 std::vector<std::string>& paths) {
0296 paths.clear();
0297 std::vector<std::pair<std::string, std::string> > filtersAndPaths;
0298
0299 for (size_t pathNr = 0; pathNr < hltConfig.size(); pathNr++) {
0300 const std::string& pathName = hltConfig.triggerName(pathNr);
0301 if (pathName.find("HLT_") == 0) {
0302
0303 std::string lastFilter;
0304 const std::vector<std::string>& pathFilters = hltConfig.moduleLabels(pathNr);
0305 if (!pathFilters.empty()) {
0306 if (pathFilters.back() == "hltBoolEnd" && pathFilters.size() >= 2) {
0307
0308 filtersAndPaths.push_back(std::make_pair(pathFilters[pathFilters.size() - 2], pathName));
0309 } else
0310 filtersAndPaths.push_back(std::make_pair(pathFilters.back(), pathName));
0311 }
0312 }
0313 }
0314
0315 std::sort(filtersAndPaths.begin(), filtersAndPaths.end(), StringPairCompare());
0316
0317 for (auto const& filter : filters) {
0318 typedef std::vector<std::pair<std::string, std::string> >::const_iterator VecIt;
0319 std::pair<VecIt, VecIt> searchResult =
0320 std::equal_range(filtersAndPaths.begin(), filtersAndPaths.end(), filter, StringPairCompare());
0321 if (searchResult.first != searchResult.second)
0322 paths.push_back(searchResult.first->second);
0323 else
0324 paths.push_back(filter);
0325
0326
0327
0328 }
0329 }
0330
0331 std::string trigTools::getL1SeedFilterOfPath(const HLTConfigProvider& hltConfig, const std::string& path) {
0332 const std::vector<std::string>& modules = hltConfig.moduleLabels(path);
0333
0334 for (auto const& moduleName : modules) {
0335 if (moduleName.find("hltL1s") == 0)
0336 return moduleName;
0337 }
0338 std::string dummy;
0339 return dummy;
0340 }
0341
0342
0343 float trigTools::getEtThresFromName(const std::string& trigName) {
0344 size_t etStrPos = trigName.find("Et");
0345 while (etStrPos != std::string::npos && trigName.find_first_of("1234567890", etStrPos) != etStrPos + 2) {
0346 etStrPos = trigName.find("Et", etStrPos + 1);
0347 }
0348 if (etStrPos != std::string::npos && trigName.find_first_of("1234567890", etStrPos) == etStrPos + 2) {
0349 size_t endOfEtValStr = trigName.find_first_not_of("1234567890", etStrPos + 2);
0350
0351 std::istringstream etValStr(trigName.substr(etStrPos + 2, endOfEtValStr - etStrPos - 2));
0352 float etVal;
0353 etValStr >> etVal;
0354 return etVal;
0355 }
0356 return 0;
0357 }
0358
0359
0360
0361 float trigTools::getSecondEtThresFromName(
0362 const std::string& trigName) {
0363 bool isEle = false, isPhoton = false, isEG = false, isEle2 = false, isPhoton2 = false, isEG2 = false, isSC2 = false;
0364 size_t etStrPos = trigName.npos;
0365 if (trigName.find("Ele") < trigName.find("Photon") && trigName.find("Ele") < trigName.find("EG")) {
0366 etStrPos = trigName.find("Ele");
0367 isEle = true;
0368 } else if (trigName.find("EG") < trigName.find("Photon") && trigName.find("EG") < trigName.find("Ele")) {
0369 etStrPos = trigName.find("EG");
0370 isEG = true;
0371 } else if (trigName.find("Photon") < trigName.find("Ele") && trigName.find("Photon") < trigName.find("EG")) {
0372 etStrPos = trigName.find("Photon");
0373 isPhoton = true;
0374 }
0375
0376
0377
0378
0379
0380 if (etStrPos != trigName.npos &&
0381 (trigName.find("Ele", etStrPos + 1) != trigName.npos || trigName.find("EG", etStrPos + 1) != trigName.npos ||
0382 trigName.find("Photon", etStrPos + 1) != trigName.npos || trigName.find("SC", etStrPos + 1) != trigName.npos)) {
0383 if (trigName.find("Ele", etStrPos + 1) < trigName.find("Photon", etStrPos + 1) &&
0384 trigName.find("Ele", etStrPos + 1) < trigName.find("EG", etStrPos + 1) &&
0385 trigName.find("Ele", etStrPos + 1) < trigName.find("SC", etStrPos + 1)) {
0386 etStrPos = trigName.find("Ele", etStrPos + 1);
0387 isEle2 = true;
0388 } else if (trigName.find("EG", etStrPos + 1) < trigName.find("Photon", etStrPos + 1) &&
0389 trigName.find("EG", etStrPos + 1) < trigName.find("Ele", etStrPos + 1) &&
0390 trigName.find("EG", etStrPos + 1) < trigName.find("SC", etStrPos + 1)) {
0391 etStrPos = trigName.find("EG", etStrPos + 1);
0392 isEG2 = true;
0393 } else if (trigName.find("Photon", etStrPos + 1) < trigName.find("EG", etStrPos + 1) &&
0394 trigName.find("Photon", etStrPos + 1) < trigName.find("Ele", etStrPos + 1) &&
0395 trigName.find("Photon", etStrPos + 1) < trigName.find("SC", etStrPos + 1)) {
0396 etStrPos = trigName.find("Photon", etStrPos + 1);
0397 isPhoton2 = true;
0398 } else if (trigName.find("SC", etStrPos + 1) < trigName.find("EG", etStrPos + 1) &&
0399 trigName.find("SC", etStrPos + 1) < trigName.find("Ele", etStrPos + 1) &&
0400 trigName.find("SC", etStrPos + 1) < trigName.find("Photon", etStrPos + 1)) {
0401 etStrPos = trigName.find("Photon", etStrPos + 1);
0402 isSC2 = true;
0403 }
0404
0405
0406 if (isEle2) {
0407 if (etStrPos != trigName.npos &&
0408 trigName.find_first_of("1234567890", etStrPos) == etStrPos + 3) {
0409 size_t endOfEtValStr = trigName.find_first_not_of("1234567890", etStrPos + 3);
0410
0411 std::istringstream etValStr(trigName.substr(etStrPos + 3, endOfEtValStr - etStrPos - 3));
0412 float etVal;
0413 etValStr >> etVal;
0414 return etVal;
0415 }
0416 }
0417 if (isEG2 || isSC2) {
0418 if (etStrPos != trigName.npos &&
0419 trigName.find_first_of("1234567890", etStrPos) == etStrPos + 2) {
0420 size_t endOfEtValStr = trigName.find_first_not_of("1234567890", etStrPos + 2);
0421
0422 std::istringstream etValStr(trigName.substr(etStrPos + 2, endOfEtValStr - etStrPos - 2));
0423 float etVal;
0424 etValStr >> etVal;
0425 return etVal;
0426 }
0427 }
0428
0429 if (isPhoton2) {
0430 if (etStrPos != trigName.npos &&
0431 trigName.find_first_of("1234567890", etStrPos) == etStrPos + 6) {
0432 size_t endOfEtValStr = trigName.find_first_not_of("1234567890", etStrPos + 6);
0433
0434 std::istringstream etValStr(trigName.substr(etStrPos + 6, endOfEtValStr - etStrPos - 6));
0435 float etVal;
0436 etValStr >> etVal;
0437 return etVal;
0438 }
0439 }
0440 } else if (etStrPos != trigName.npos) {
0441 if (isEle) {
0442 if (etStrPos != trigName.npos &&
0443 trigName.find_first_of("1234567890", etStrPos) == etStrPos + 3) {
0444 size_t endOfEtValStr = trigName.find_first_not_of("1234567890", etStrPos + 3);
0445
0446 std::istringstream etValStr(trigName.substr(etStrPos + 3, endOfEtValStr - etStrPos - 3));
0447 float etVal;
0448 etValStr >> etVal;
0449 return etVal;
0450 }
0451 }
0452 if (isEG) {
0453 if (etStrPos != trigName.npos &&
0454 trigName.find_first_of("1234567890", etStrPos) == etStrPos + 2) {
0455 size_t endOfEtValStr = trigName.find_first_not_of("1234567890", etStrPos + 2);
0456
0457 std::istringstream etValStr(trigName.substr(etStrPos + 2, endOfEtValStr - etStrPos - 2));
0458 float etVal;
0459 etValStr >> etVal;
0460 return etVal;
0461 }
0462 }
0463
0464 if (isPhoton) {
0465 if (etStrPos != trigName.npos &&
0466 trigName.find_first_of("1234567890", etStrPos) == etStrPos + 6) {
0467 size_t endOfEtValStr = trigName.find_first_not_of("1234567890", etStrPos + 6);
0468
0469 std::istringstream etValStr(trigName.substr(etStrPos + 6, endOfEtValStr - etStrPos - 6));
0470 float etVal;
0471 etValStr >> etVal;
0472 return etVal;
0473 }
0474 }
0475 }
0476
0477 return 0;
0478 }