File indexing completed on 2024-04-06 12:06:45
0001 #ifndef PhysicsTools_TagAndProbe_TriggerMatchProducer_h
0002 #define PhysicsTools_TagAndProbe_TriggerMatchProducer_h
0003
0004
0005 #include <memory>
0006
0007
0008 #include "FWCore/Framework/interface/Frameworkfwd.h"
0009 #include "DataFormats/Candidate/interface/ShallowCloneCandidate.h"
0010 #include "DataFormats/Common/interface/TriggerResults.h"
0011 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0012 #include "DataFormats/HLTReco/interface/TriggerObject.h"
0013 #include "DataFormats/Math/interface/deltaR.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "FWCore/Framework/interface/MakerMacros.h"
0016 #include "FWCore/Framework/interface/stream/EDProducer.h"
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "FWCore/ServiceRegistry/interface/Service.h"
0020 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0021 #include "HLTrigger/HLTcore/interface/HLTPrescaleProvider.h"
0022
0023 #include <string>
0024 #include <cmath>
0025 #include <TString.h>
0026 #include <TRegexp.h>
0027
0028
0029 template <class object>
0030 class TriggerMatchProducer : public edm::stream::EDProducer<> {
0031 public:
0032 explicit TriggerMatchProducer(const edm::ParameterSet&);
0033 ~TriggerMatchProducer() override;
0034
0035 private:
0036 void beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) override;
0037 void produce(edm::Event&, const edm::EventSetup&) override;
0038
0039
0040
0041 edm::InputTag _inputProducer;
0042 edm::EDGetTokenT<edm::View<object> > _inputProducerToken;
0043 edm::InputTag triggerEventTag_;
0044 edm::EDGetTokenT<trigger::TriggerEvent> triggerEventToken_;
0045 edm::InputTag triggerResultsTag_;
0046 edm::EDGetTokenT<edm::TriggerResults> triggerResultsToken_;
0047 std::string hltTag_;
0048 double delRMatchingCut_;
0049 std::string filterName_;
0050 bool storeRefCollection_;
0051
0052
0053 bool changed_;
0054 HLTPrescaleProvider hltPrescaleProvider_;
0055 };
0056
0057 template <class object>
0058 TriggerMatchProducer<object>::TriggerMatchProducer(const edm::ParameterSet& iConfig)
0059 : hltPrescaleProvider_(iConfig, consumesCollector(), *this) {
0060 _inputProducer = iConfig.template getParameter<edm::InputTag>("InputProducer");
0061 _inputProducerToken = consumes<edm::View<object> >(_inputProducer);
0062
0063
0064 const edm::InputTag dTriggerEventTag("hltTriggerSummaryAOD", "", "HLT");
0065 triggerEventTag_ = iConfig.getUntrackedParameter<edm::InputTag>("triggerEventTag", dTriggerEventTag);
0066 triggerEventToken_ = consumes<trigger::TriggerEvent>(triggerEventTag_);
0067
0068 const edm::InputTag dTriggerResults("TriggerResults", "", "HLT");
0069
0070 triggerResultsTag_ = iConfig.getUntrackedParameter<edm::InputTag>("triggerResultsTag", dTriggerResults);
0071 triggerResultsToken_ = consumes<edm::TriggerResults>(triggerResultsTag_);
0072
0073
0074 hltTag_ = iConfig.getUntrackedParameter<std::string>("hltTag", "HLT_Ele*");
0075
0076 delRMatchingCut_ = iConfig.getUntrackedParameter<double>("triggerDelRMatch", 0.30);
0077
0078
0079
0080
0081
0082 produces<edm::PtrVector<object> >();
0083 produces<edm::RefToBaseVector<object> >("R");
0084
0085 filterName_ = "";
0086 }
0087
0088 template <class object>
0089 TriggerMatchProducer<object>::~TriggerMatchProducer() {}
0090
0091
0092
0093
0094
0095
0096 template <class object>
0097 void TriggerMatchProducer<object>::produce(edm::Event& event, const edm::EventSetup& eventSetup) {
0098 using namespace std;
0099 using namespace edm;
0100 using namespace reco;
0101 using namespace trigger;
0102
0103 HLTConfigProvider const& hltConfig = hltPrescaleProvider_.hltConfigProvider();
0104
0105
0106 std::unique_ptr<edm::RefToBaseVector<object> > outColRef(new edm::RefToBaseVector<object>);
0107 std::unique_ptr<edm::PtrVector<object> > outColPtr(new edm::PtrVector<object>);
0108
0109
0110 edm::Handle<edm::View<object> > candHandle;
0111 try {
0112 event.getByToken(_inputProducerToken, candHandle);
0113 } catch (cms::Exception& ex) {
0114 edm::LogError("TriggerMatchProducer") << "Error! Can't get collection: " << _inputProducer;
0115 throw ex;
0116 }
0117
0118
0119 edm::Handle<trigger::TriggerEvent> trgEvent;
0120 event.getByToken(triggerEventToken_, trgEvent);
0121 edm::Handle<edm::TriggerResults> pTrgResults;
0122 event.getByToken(triggerResultsToken_, pTrgResults);
0123
0124
0125 std::vector<std::string> activeHLTPathsInThisEvent = hltConfig.triggerNames();
0126 std::map<std::string, bool> triggerInMenu;
0127 std::map<std::string, bool> triggerUnprescaled;
0128
0129
0130
0131
0132
0133
0134
0135 for (std::vector<std::string>::const_iterator iHLT = activeHLTPathsInThisEvent.begin();
0136 iHLT != activeHLTPathsInThisEvent.end();
0137 ++iHLT) {
0138
0139
0140 if (TString(*iHLT).Contains(TRegexp(hltTag_))) {
0141 triggerInMenu[*iHLT] = true;
0142 if (hltPrescaleProvider_.prescaleValue<double>(event, eventSetup, *iHLT) == 1)
0143 triggerUnprescaled[*iHLT] = true;
0144 }
0145 }
0146
0147
0148 if (not trgEvent.isValid()) {
0149 edm::LogInfo("info") << "******** Following Trigger Summary Object Not Found: " << triggerEventTag_;
0150
0151 event.put(std::move(outColRef), "R");
0152 event.put(std::move(outColPtr));
0153 return;
0154 }
0155
0156
0157
0158 edm::InputTag filterTag;
0159
0160 const trigger::TriggerObjectCollection& TOC(trgEvent->getObjects());
0161
0162 std::vector<int> index;
0163 std::vector<std::string> filters;
0164
0165
0166 for (std::map<std::string, bool>::const_iterator iMyHLT = triggerInMenu.begin(); iMyHLT != triggerInMenu.end();
0167 ++iMyHLT) {
0168 if (!(iMyHLT->second && triggerUnprescaled[iMyHLT->first]))
0169 continue;
0170
0171 int triggerIndex = -1;
0172 edm::InputTag filterTag;
0173 try {
0174 filters = hltConfig.moduleLabels(iMyHLT->first);
0175 triggerIndex = hltConfig.triggerIndex(iMyHLT->first);
0176 } catch (std::exception const&) {
0177 cout << "bad trigger\n";
0178 }
0179
0180 if (triggerIndex == -1 || !(pTrgResults->wasrun(triggerIndex)) || !(pTrgResults->accept(triggerIndex)) ||
0181 (pTrgResults->error(triggerIndex))) {
0182 continue;
0183 }
0184
0185 for (std::vector<std::string>::iterator filter = filters.begin(); filter != filters.end(); ++filter) {
0186 edm::InputTag testTag(*filter, "", triggerEventTag_.process());
0187 int testindex = trgEvent->filterIndex(testTag);
0188 if (!(testindex >= trgEvent->sizeFilters())) {
0189 filterName_ = *filter;
0190 filterTag = testTag;
0191 }
0192 }
0193
0194
0195 index.push_back(trgEvent->filterIndex(filterTag));
0196
0197
0198
0199
0200 }
0201
0202
0203 edm::PtrVector<object> ptrVect;
0204 edm::RefToBaseVector<object> refs;
0205 for (size_t i = 0; i < candHandle->size(); ++i) {
0206 ptrVect.push_back(candHandle->ptrAt(i));
0207 refs.push_back(candHandle->refAt(i));
0208 }
0209
0210 unsigned int counter = 0;
0211 for (typename edm::View<object>::const_iterator j = candHandle->begin(); j != candHandle->end(); ++j, ++counter) {
0212 bool hltTrigger = false;
0213 for (unsigned int idx = 0; idx < index.size(); ++idx) {
0214 if (hltTrigger)
0215 continue;
0216 const trigger::Keys& KEYS(trgEvent->filterKeys(index[idx]));
0217 const size_type nK(KEYS.size());
0218
0219
0220
0221 for (int ipart = 0; ipart != nK; ++ipart) {
0222 const trigger::TriggerObject& TO = TOC[KEYS[ipart]];
0223 double dRval = deltaR(j->eta(), j->phi(), TO.eta(), TO.phi());
0224 hltTrigger = dRval < delRMatchingCut_;
0225 if (hltTrigger)
0226 break;
0227 }
0228 }
0229
0230 if (hltTrigger) {
0231 outColRef->push_back(refs[counter]);
0232 outColPtr->push_back(ptrVect[counter]);
0233 }
0234 }
0235 event.put(std::move(outColRef), "R");
0236 event.put(std::move(outColPtr));
0237 }
0238
0239
0240 template <class object>
0241 void TriggerMatchProducer<object>::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0242
0243 if (!hltPrescaleProvider_.init(iRun, iSetup, triggerEventTag_.process(), changed_)) {
0244 edm::LogError("TriggerMatchProducer") << "Error! Can't initialize HLTConfigProvider";
0245 throw cms::Exception("HLTConfigProvider::init() returned non 0");
0246 }
0247
0248
0249
0250
0251 }
0252 #endif