File indexing completed on 2024-04-06 11:58:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include <memory>
0022
0023
0024 #include "FWCore/Framework/interface/Frameworkfwd.h"
0025 #include "FWCore/Framework/interface/one/EDFilter.h"
0026
0027 #include "FWCore/Framework/interface/Event.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031
0032 #include "FWCore/Utilities/interface/InputTag.h"
0033
0034 #include "FWCore/Common/interface/TriggerNames.h"
0035 #include "DataFormats/Common/interface/TriggerResults.h"
0036 #include "DataFormats/Provenance/interface/ParameterSetID.h"
0037
0038 #include <string>
0039
0040
0041
0042
0043
0044 using namespace edm;
0045
0046 class PrescalerFHN : public edm::one::EDFilter<> {
0047 public:
0048 explicit PrescalerFHN(const edm::ParameterSet&);
0049
0050 private:
0051 bool filter(edm::Event&, const edm::EventSetup&) override;
0052
0053
0054 void init(const edm::TriggerResults&, const edm::TriggerNames& triggerNames);
0055
0056 edm::ParameterSetID triggerNamesID_;
0057
0058 edm::EDGetTokenT<TriggerResults> tok_trigger;
0059
0060 std::map<std::string, unsigned int> prescales;
0061 std::map<std::string, unsigned int> prescale_counter;
0062
0063 std::map<std::string, unsigned int> trigger_indices;
0064 };
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077 PrescalerFHN::PrescalerFHN(const edm::ParameterSet& iConfig) {
0078 tok_trigger = consumes<TriggerResults>(iConfig.getParameter<edm::InputTag>("TriggerResultsTag"));
0079
0080 std::vector<edm::ParameterSet> prescales_in(iConfig.getParameter<std::vector<edm::ParameterSet> >("Prescales"));
0081
0082 for (std::vector<edm::ParameterSet>::const_iterator cit = prescales_in.begin(); cit != prescales_in.end(); cit++) {
0083 std::string name(cit->getParameter<std::string>("HLTName"));
0084 unsigned int factor(cit->getParameter<unsigned int>("PrescaleFactor"));
0085
0086
0087
0088 prescales[name] = factor;
0089 prescale_counter[name] = 0;
0090 }
0091 }
0092
0093
0094
0095
0096
0097 void PrescalerFHN::init(const edm::TriggerResults& result, const edm::TriggerNames& triggerNames) {
0098 trigger_indices.clear();
0099
0100 for (std::map<std::string, unsigned int>::const_iterator cit = prescales.begin(); cit != prescales.end(); cit++) {
0101 auto index = triggerNames.triggerIndex(cit->first);
0102 if (index < result.size()) {
0103 trigger_indices[cit->first] = index;
0104 } else {
0105
0106 LogDebug("") << "requested HLT path does not exist: " << cit->first;
0107 }
0108 }
0109 }
0110
0111
0112 bool PrescalerFHN::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0113 using namespace edm;
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 auto const& trh = iEvent.getHandle(tok_trigger);
0124
0125 if (trh.isValid()) {
0126 LogDebug("") << "TriggerResults found, number of HLT paths: " << trh->size();
0127 } else {
0128 LogDebug("") << "TriggerResults product not found - returning result=false!";
0129 return false;
0130 }
0131
0132 const edm::TriggerNames& triggerNames = iEvent.triggerNames(*trh);
0133 if (triggerNamesID_ != triggerNames.parameterSetID()) {
0134 triggerNamesID_ = triggerNames.parameterSetID();
0135 init(*trh, triggerNames);
0136 }
0137
0138
0139
0140
0141 bool accept_event = false;
0142 for (std::map<std::string, unsigned int>::const_iterator cit = trigger_indices.begin(); cit != trigger_indices.end();
0143 cit++) {
0144 if (trh->accept(cit->second)) {
0145 prescale_counter[cit->first]++;
0146 if (prescale_counter[cit->first] >= prescales[cit->first]) {
0147 accept_event = true;
0148 prescale_counter[cit->first] = 0;
0149 }
0150 }
0151 }
0152
0153 return accept_event;
0154 }
0155
0156
0157 DEFINE_FWK_MODULE(PrescalerFHN);