File indexing completed on 2024-04-06 12:24:04
0001 #include "FWCore/Framework/interface/global/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/Utilities/interface/InputTag.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006
0007 #include "DataFormats/Common/interface/View.h"
0008 #include "DataFormats/JetReco/interface/PFJet.h"
0009 #include "DataFormats/PatCandidates/interface/Jet.h"
0010 #include "DataFormats/Candidate/interface/Candidate.h"
0011 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0012
0013 template <typename T>
0014 class JetCollectionReducerT : public edm::global::EDProducer<> {
0015 public:
0016 explicit JetCollectionReducerT(const edm::ParameterSet& iConfig);
0017 ~JetCollectionReducerT() override {}
0018
0019 void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0020
0021 private:
0022 edm::EDGetTokenT<std::vector<T>> jetColToken_;
0023 bool writeEmptyCollection_;
0024 std::vector<edm::EDGetTokenT<edm::View<reco::Candidate>>> collections_;
0025 };
0026
0027 template <typename T>
0028 JetCollectionReducerT<T>::JetCollectionReducerT(const edm::ParameterSet& iConfig)
0029 : jetColToken_(consumes<std::vector<T>>(iConfig.getParameter<edm::InputTag>("jetCollection"))),
0030 writeEmptyCollection_(iConfig.getParameter<bool>("writeEmptyCollection")) {
0031 std::vector<edm::InputTag> filtersDecTags = iConfig.getParameter<std::vector<edm::InputTag>>("triggeringCollections");
0032 for (std::vector<edm::InputTag>::const_iterator inputTag = filtersDecTags.begin(); inputTag != filtersDecTags.end();
0033 ++inputTag) {
0034 collections_.push_back(consumes<edm::View<reco::Candidate>>(*inputTag));
0035 }
0036
0037 produces<std::vector<T>>();
0038 }
0039
0040
0041 template <typename T>
0042 void JetCollectionReducerT<T>::produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup&) const {
0043 std::unique_ptr<std::vector<T>> outJets(new std::vector<T>());
0044
0045 bool filterDecision = false;
0046 edm::Handle<edm::View<reco::Candidate>> tmpCol;
0047 for (std::vector<edm::EDGetTokenT<edm::View<reco::Candidate>>>::const_iterator filter = collections_.begin();
0048 filter != collections_.end();
0049 filter++) {
0050 iEvent.getByToken(*filter, tmpCol);
0051 if (!tmpCol->empty()) {
0052 filterDecision = true;
0053 break;
0054 }
0055 }
0056
0057 if (!filterDecision) {
0058 if (writeEmptyCollection_)
0059 iEvent.put(std::move(outJets));
0060 return;
0061 }
0062
0063 edm::Handle<std::vector<T>> jetColHandle;
0064 iEvent.getByToken(jetColToken_, jetColHandle);
0065
0066
0067 for (size_t ij = 0; ij < jetColHandle->size(); ij++) {
0068 outJets->push_back((*jetColHandle)[ij]);
0069 }
0070
0071 iEvent.put(std::move(outJets));
0072 }
0073
0074
0075 typedef JetCollectionReducerT<pat::Jet> PATJetCollectionReducer;
0076 typedef JetCollectionReducerT<reco::PFJet> PFJetCollectionReducer;
0077 DEFINE_FWK_MODULE(PATJetCollectionReducer);
0078 DEFINE_FWK_MODULE(PFJetCollectionReducer);