File indexing completed on 2024-04-06 12:19:25
0001 #ifndef JetMETCorrections_Type1MET_CaloJetMETcorrInputProducer_h
0002 #define JetMETCorrections_Type1MET_CaloJetMETcorrInputProducer_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include "FWCore/Framework/interface/global/EDProducer.h"
0018 #include "FWCore/Framework/interface/Event.h"
0019 #include "FWCore/Framework/interface/EventSetup.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/Utilities/interface/InputTag.h"
0022 #include "FWCore/Utilities/interface/Exception.h"
0023
0024 #include "DataFormats/Common/interface/Ref.h"
0025 #include "DataFormats/Common/interface/RefToBase.h"
0026 #include "DataFormats/JetReco/interface/Jet.h"
0027 #include "DataFormats/Common/interface/View.h"
0028 #include "DataFormats/METReco/interface/MET.h"
0029 #include "DataFormats/PatCandidates/interface/Jet.h"
0030
0031 #include "JetMETCorrections/Type1MET/interface/JetCorrExtractorT.h"
0032
0033 #include <string>
0034 #include <type_traits>
0035
0036 namespace CaloJetMETcorrInputProducer_namespace {
0037 template <typename T>
0038 class InputTypeCheckerT {
0039 public:
0040 void operator()(const T&) const {}
0041 bool isPatJet(const T&) const { return std::is_base_of<class pat::Jet, T>::value; }
0042 };
0043
0044 template <typename T>
0045 class RawJetExtractorT
0046
0047 {
0048 public:
0049 RawJetExtractorT() {}
0050
0051 reco::Candidate::LorentzVector operator()(const T& jet) const { return jet.p4(); }
0052 };
0053 }
0054
0055 template <typename T, typename Textractor>
0056 class CaloJetMETcorrInputProducerT final : public edm::global::EDProducer<> {
0057 public:
0058 explicit CaloJetMETcorrInputProducerT(const edm::ParameterSet& cfg)
0059 : moduleLabel_(cfg.getParameter<std::string>("@module_label")), offsetCorrLabel_("") {
0060 token_ = consumes<std::vector<T> >(cfg.getParameter<edm::InputTag>("src"));
0061
0062 if (cfg.exists("offsetCorrLabel")) {
0063 offsetCorrLabel_ = cfg.getParameter<edm::InputTag>("offsetCorrLabel");
0064 offsetCorrToken_ = consumes<reco::JetCorrector>(offsetCorrLabel_);
0065 }
0066 jetCorrLabel_ = cfg.getParameter<edm::InputTag>("jetCorrLabel");
0067 jetCorrToken_ = consumes<reco::JetCorrector>(jetCorrLabel_);
0068
0069 jetCorrEtaMax_ = (cfg.exists("jetCorrEtaMax")) ? cfg.getParameter<double>("jetCorrEtaMax") : 9.9;
0070
0071 type1JetPtThreshold_ = cfg.getParameter<double>("type1JetPtThreshold");
0072
0073 skipEM_ = cfg.getParameter<bool>("skipEM");
0074 if (skipEM_) {
0075 skipEMfractionThreshold_ = cfg.getParameter<double>("skipEMfractionThreshold");
0076 }
0077
0078 if (cfg.exists("srcMET")) {
0079 srcMET_ = cfg.getParameter<edm::InputTag>("srcMET");
0080 metToken_ = consumes<edm::View<reco::MET> >(cfg.getParameter<edm::InputTag>("srcMET"));
0081 }
0082
0083 produces<CorrMETData>("type1");
0084 produces<CorrMETData>("type2");
0085 produces<CorrMETData>("offset");
0086 }
0087 ~CaloJetMETcorrInputProducerT() override {}
0088
0089 private:
0090 void produce(edm::StreamID, edm::Event& evt, const edm::EventSetup& es) const override {
0091 std::unique_ptr<CorrMETData> type1Correction(new CorrMETData());
0092 std::unique_ptr<CorrMETData> unclEnergySum(new CorrMETData());
0093 std::unique_ptr<CorrMETData> offsetEnergySum(new CorrMETData());
0094
0095 edm::Handle<reco::JetCorrector> jetCorr;
0096 evt.getByToken(jetCorrToken_, jetCorr);
0097
0098 typedef std::vector<T> JetCollection;
0099 edm::Handle<JetCollection> jets;
0100 evt.getByToken(token_, jets);
0101
0102 typedef edm::View<reco::MET> METView;
0103 edm::Handle<METView> met;
0104 if (!srcMET_.label().empty()) {
0105 evt.getByToken(metToken_, met);
0106 if (met->size() != 1)
0107 throw cms::Exception("CaloJetMETcorrInputProducer::produce")
0108 << "Failed to find unique MET in the event, src = " << srcMET_.label() << " !!\n";
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 unclEnergySum->mex = -met->front().px();
0119 unclEnergySum->mey = -met->front().py();
0120 unclEnergySum->sumet = met->front().sumEt();
0121 }
0122
0123 int numJets = jets->size();
0124 for (int jetIndex = 0; jetIndex < numJets; ++jetIndex) {
0125 const T& rawJet = jets->at(jetIndex);
0126
0127 const CaloJetMETcorrInputProducer_namespace::InputTypeCheckerT<T> checkInputType{};
0128 checkInputType(rawJet);
0129
0130 const CaloJetMETcorrInputProducer_namespace::RawJetExtractorT<T> rawJetExtractor;
0131 reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(rawJet);
0132
0133 reco::Candidate::LorentzVector corrJetP4;
0134 if (checkInputType.isPatJet(rawJet))
0135 corrJetP4 = jetCorrExtractor_(rawJet, jetCorrLabel_.label(), jetCorrEtaMax_);
0136 else
0137 corrJetP4 = jetCorrExtractor_(rawJet, jetCorr.product(), jetCorrEtaMax_);
0138
0139 if (corrJetP4.pt() > type1JetPtThreshold_) {
0140 unclEnergySum->mex -= rawJetP4.px();
0141 unclEnergySum->mey -= rawJetP4.py();
0142 unclEnergySum->sumet -= rawJetP4.Et();
0143
0144 if (skipEM_ && rawJet.emEnergyFraction() > skipEMfractionThreshold_)
0145 continue;
0146
0147 reco::Candidate::LorentzVector rawJetP4offsetCorr = rawJetP4;
0148 if (!offsetCorrLabel_.label().empty()) {
0149 edm::Handle<reco::JetCorrector> offsetCorr;
0150 evt.getByToken(offsetCorrToken_, offsetCorr);
0151 if (checkInputType.isPatJet(rawJet))
0152 rawJetP4offsetCorr = jetCorrExtractor_(rawJet, offsetCorrLabel_.label(), jetCorrEtaMax_);
0153 else
0154 rawJetP4offsetCorr = jetCorrExtractor_(rawJet, offsetCorr.product(), jetCorrEtaMax_);
0155
0156 offsetEnergySum->mex += (rawJetP4.px() - rawJetP4offsetCorr.px());
0157 offsetEnergySum->mey += (rawJetP4.py() - rawJetP4offsetCorr.py());
0158 offsetEnergySum->sumet += (rawJetP4.Et() - rawJetP4offsetCorr.Et());
0159 }
0160
0161
0162
0163 type1Correction->mex -= (corrJetP4.px() - rawJetP4offsetCorr.px());
0164 type1Correction->mey -= (corrJetP4.py() - rawJetP4offsetCorr.py());
0165 type1Correction->sumet += (corrJetP4.Et() - rawJetP4offsetCorr.Et());
0166 }
0167 }
0168
0169
0170
0171
0172
0173
0174 evt.put(std::move(type1Correction), "type1");
0175 evt.put(std::move(unclEnergySum), "type2");
0176 evt.put(std::move(offsetEnergySum), "offset");
0177 }
0178
0179 std::string moduleLabel_;
0180
0181 edm::EDGetTokenT<std::vector<T> > token_;
0182
0183 edm::InputTag offsetCorrLabel_;
0184 edm::EDGetTokenT<reco::JetCorrector> offsetCorrToken_;
0185 edm::InputTag jetCorrLabel_;
0186 edm::EDGetTokenT<reco::JetCorrector>
0187 jetCorrToken_;
0188 Textractor jetCorrExtractor_;
0189
0190 double jetCorrEtaMax_;
0191
0192
0193
0194
0195
0196 double type1JetPtThreshold_;
0197
0198
0199
0200 bool skipEM_;
0201
0202 double skipEMfractionThreshold_;
0203
0204 edm::InputTag srcMET_;
0205 edm::EDGetTokenT<edm::View<reco::MET> > metToken_;
0206 };
0207
0208 #endif