Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:04

0001 #ifndef PhysicsTools_PatUtils_PATJetCorrExtractor_h
0002 #define PhysicsTools_PatUtils_PATJetCorrExtractor_h
0003 
0004 /** \class PATJetCorrExtractor
0005  *
0006  * Retrieve jet energy correction factor for pat::Jets (of either PF-type or Calo-type)
0007  *
0008  * NOTE: this specialization of the "generic" template defined in
0009  *         JetMETCorrections/Type1MET/interface/JetCorrExtractorT.h
0010  *       is to be used for pat::Jets only
0011  *
0012  * \author Christian Veelken, LLR
0013  *
0014  *
0015  *
0016  */
0017 
0018 #include "FWCore/Framework/interface/Event.h"
0019 #include "FWCore/Framework/interface/EventSetup.h"
0020 #include "FWCore/Utilities/interface/Exception.h"
0021 
0022 #include "JetMETCorrections/Type1MET/interface/JetCorrExtractorT.h"
0023 
0024 #include "DataFormats/Common/interface/RefToBase.h"
0025 #include "DataFormats/JetReco/interface/Jet.h"
0026 #include "DataFormats/PatCandidates/interface/Jet.h"
0027 #include "DataFormats/Candidate/interface/Candidate.h"
0028 
0029 #include <string>
0030 #include <vector>
0031 
0032 class PATJetCorrExtractor {
0033 public:
0034   reco::Candidate::LorentzVector operator()(
0035       const pat::Jet rawJet,
0036       const reco::JetCorrector* jetCorr,
0037       double jetCorrEtaMax = 9.9,
0038       const reco::Candidate::LorentzVector* const rawJetP4_specified = nullptr) const {
0039     JetCorrExtractorT<pat::Jet> jetCorrExtractor;
0040     return jetCorrExtractor(rawJet, jetCorr, jetCorrEtaMax, rawJetP4_specified);
0041   }
0042 
0043   reco::Candidate::LorentzVector operator()(
0044       const pat::Jet& jet,
0045       const std::string& jetCorrLabel,
0046       double jetCorrEtaMax = 9.9,
0047       const reco::Candidate::LorentzVector* const rawJetP4_specified = nullptr) const {
0048     reco::Candidate::LorentzVector corrJetP4;
0049 
0050     try {
0051       corrJetP4 = jet.correctedP4(jetCorrLabel);
0052       if (rawJetP4_specified != nullptr) {
0053         //MM: compensate for potential removal of constituents (as muons)
0054         //similar effect in JetMETCorrection/Type1MET/interface/JetCorrExtractor.h
0055         reco::Candidate::LorentzVector rawJetP4 = jet.correctedP4("Uncorrected");
0056         double corrFactor = corrJetP4.pt() / rawJetP4.pt();
0057         corrJetP4 = (*rawJetP4_specified);
0058         corrJetP4 *= corrFactor;
0059         if (corrFactor < 0) {
0060           edm::LogWarning("PATJetCorrExtractor") << "Negative jet energy scale correction noticed"
0061                                                  << ".\n";
0062         }
0063       }
0064     } catch (cms::Exception const&) {
0065       throw cms::Exception("InvalidRequest")
0066           << "The JEC level " << jetCorrLabel << " does not exist !!\n"
0067           << "Available levels = { " << format_vstring(jet.availableJECLevels()) << " }.\n";
0068     }
0069 
0070     return corrJetP4;
0071   }
0072 
0073 private:
0074   static std::string format_vstring(const std::vector<std::string>& v) {
0075     std::string retVal;
0076     auto ss = std::accumulate(v.begin(), v.end(), 0, [](int a, std::string const& s) { return a + s.length() + 2; });
0077     retVal.reserve(ss);
0078     for_each(v.begin(), v.end(), [&](std::string const& s) { retVal += (retVal.empty() ? "" : ", ") + s; });
0079     return retVal;
0080   }
0081 };
0082 
0083 #endif