CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/PhysicsTools/PatUtils/interface/ShiftedJetProducerT.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_PatUtils_ShiftedJetProducerT_h
00002 #define PhysicsTools_PatUtils_ShiftedJetProducerT_h
00003 
00019 #include "FWCore/Framework/interface/EDProducer.h"
00020 #include "FWCore/Framework/interface/Event.h"
00021 #include "FWCore/Framework/interface/EventSetup.h"
00022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00023 #include "FWCore/ParameterSet/interface/FileInPath.h"
00024 #include "FWCore/Utilities/interface/InputTag.h"
00025 
00026 #include "JetMETCorrections/Objects/interface/JetCorrector.h"
00027 #include "JetMETCorrections/Objects/interface/JetCorrectionsRecord.h"
00028 #include "JetMETCorrections/Type1MET/interface/JetCorrExtractorT.h"
00029 #include "CondFormats/JetMETObjects/interface/JetCorrectorParameters.h"
00030 #include "CondFormats/JetMETObjects/interface/JetCorrectionUncertainty.h"
00031 #include "FWCore/Framework/interface/ESHandle.h"
00032 
00033 #include "PhysicsTools/PatUtils/interface/SmearedJetProducerT.h"
00034 
00035 #include <TMath.h>
00036 
00037 #include <string>
00038 
00039 template <typename T, typename Textractor>
00040 class ShiftedJetProducerT : public edm::EDProducer  
00041 {
00042   typedef std::vector<T> JetCollection;
00043 
00044  public:
00045 
00046   explicit ShiftedJetProducerT(const edm::ParameterSet& cfg)
00047     : moduleLabel_(cfg.getParameter<std::string>("@module_label")),
00048       src_(cfg.getParameter<edm::InputTag>("src")),
00049       jetCorrPayloadName_(""),
00050       jetCorrParameters_(0),
00051       jecUncertainty_(0),
00052       jecUncertaintyValue_(-1.)
00053   {
00054     if ( cfg.exists("jecUncertaintyValue") ) {
00055       jecUncertaintyValue_ = cfg.getParameter<double>("jecUncertaintyValue");
00056     } else {
00057       jetCorrUncertaintyTag_ = cfg.getParameter<std::string>("jetCorrUncertaintyTag");
00058       if ( cfg.exists("jetCorrInputFileName") ) {
00059         jetCorrInputFileName_ = cfg.getParameter<edm::FileInPath>("jetCorrInputFileName");
00060         if ( jetCorrInputFileName_.location() == edm::FileInPath::Unknown) throw cms::Exception("ShiftedJetProducerT")
00061           << " Failed to find JEC parameter file = " << jetCorrInputFileName_ << " !!\n";
00062         std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_  
00063                   << " from file = " << jetCorrInputFileName_.fullPath() << "." << std::endl;
00064         jetCorrParameters_ = new JetCorrectorParameters(jetCorrInputFileName_.fullPath().data(), jetCorrUncertaintyTag_);
00065         jecUncertainty_ = new JetCorrectionUncertainty(*jetCorrParameters_);
00066       } else {
00067         std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_
00068                   << " from DB/SQLlite file." << std::endl;
00069         jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
00070       }
00071     }
00072 
00073     addResidualJES_ = cfg.getParameter<bool>("addResidualJES");
00074     jetCorrLabelUpToL3_ = ( cfg.exists("jetCorrLabelUpToL3") ) ?
00075       cfg.getParameter<std::string>("jetCorrLabelUpToL3") : "";
00076     jetCorrLabelUpToL3Res_ = ( cfg.exists("jetCorrLabelUpToL3Res") ) ?
00077       cfg.getParameter<std::string>("jetCorrLabelUpToL3Res") : "";
00078     jetCorrEtaMax_ = ( cfg.exists("jetCorrEtaMax") ) ?
00079       cfg.getParameter<double>("jetCorrEtaMax") : 9.9;
00080 
00081     shiftBy_ = cfg.getParameter<double>("shiftBy");
00082 
00083     verbosity_ = ( cfg.exists("verbosity") ) ?
00084       cfg.getParameter<int>("verbosity") : 0;
00085 
00086     produces<JetCollection>();
00087   }
00088   ~ShiftedJetProducerT()
00089   {
00090     delete jetCorrParameters_;
00091     delete jecUncertainty_;
00092   }
00093     
00094  private:
00095 
00096   void produce(edm::Event& evt, const edm::EventSetup& es)
00097   {
00098     if ( verbosity_ ) {
00099       std::cout << "<ShiftedJetProducerT::produce>:" << std::endl;
00100       std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
00101       std::cout << " src = " << src_.label() << std::endl;
00102     }
00103 
00104     edm::Handle<JetCollection> originalJets;
00105     evt.getByLabel(src_, originalJets);
00106 
00107     std::auto_ptr<JetCollection> shiftedJets(new JetCollection);
00108     
00109     if ( jetCorrPayloadName_ != "" ) {
00110       edm::ESHandle<JetCorrectorParametersCollection> jetCorrParameterSet;
00111       es.get<JetCorrectionsRecord>().get(jetCorrPayloadName_, jetCorrParameterSet); 
00112       const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_];
00113       delete jecUncertainty_;
00114       jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters);
00115     }
00116 
00117     for ( typename JetCollection::const_iterator originalJet = originalJets->begin();
00118           originalJet != originalJets->end(); ++originalJet ) {
00119       reco::Candidate::LorentzVector originalJetP4 = originalJet->p4();
00120       if ( verbosity_ ) {
00121         std::cout << "originalJet: Pt = " << originalJetP4.pt() << ", eta = " << originalJetP4.eta() << ", phi = " << originalJetP4.phi() << std::endl;
00122       }
00123 
00124       double shift = 0.;
00125       if ( jecUncertaintyValue_ != -1. ) {
00126         shift = jecUncertaintyValue_;
00127       } else {
00128         jecUncertainty_->setJetEta(originalJetP4.eta());
00129         jecUncertainty_->setJetPt(originalJetP4.pt());
00130         
00131         shift = jecUncertainty_->getUncertainty(true);
00132       }
00133       if ( verbosity_ ) {
00134         std::cout << "shift = " << shift << std::endl;
00135       }
00136 
00137       if ( addResidualJES_ ) {
00138         static SmearedJetProducer_namespace::RawJetExtractorT<T> rawJetExtractor;
00139         reco::Candidate::LorentzVector rawJetP4 = rawJetExtractor(*originalJet);
00140         if ( rawJetP4.E() > 1.e-1 ) {
00141           reco::Candidate::LorentzVector corrJetP4upToL3 = 
00142             jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3_, &evt, &es, jetCorrEtaMax_, &rawJetP4);
00143           reco::Candidate::LorentzVector corrJetP4upToL3Res = 
00144             jetCorrExtractor_(*originalJet, jetCorrLabelUpToL3Res_, &evt, &es, jetCorrEtaMax_, &rawJetP4);
00145           if ( corrJetP4upToL3.E() > 1.e-1 && corrJetP4upToL3Res.E() > 1.e-1 ) {
00146             double residualJES = (corrJetP4upToL3Res.E()/corrJetP4upToL3.E()) - 1.;
00147             shift = TMath::Sqrt(shift*shift + residualJES*residualJES);
00148           }
00149         }
00150       }
00151 
00152       shift *= shiftBy_;
00153       if ( verbosity_ ) {
00154         std::cout << "shift*shiftBy = " << shift << std::endl;
00155       }
00156 
00157       T shiftedJet(*originalJet);
00158       shiftedJet.setP4((1. + shift)*originalJetP4);
00159       if ( verbosity_ ) {
00160         std::cout << "shiftedJet: Pt = " << shiftedJet.pt() << ", eta = " << shiftedJet.eta() << ", phi = " << shiftedJet.phi() << std::endl;
00161       }
00162     
00163       shiftedJets->push_back(shiftedJet);
00164     }
00165   
00166     evt.put(shiftedJets);
00167   }
00168 
00169   std::string moduleLabel_;
00170 
00171   edm::InputTag src_; 
00172 
00173   edm::FileInPath jetCorrInputFileName_;
00174   std::string jetCorrPayloadName_;
00175   std::string jetCorrUncertaintyTag_;
00176   JetCorrectorParameters* jetCorrParameters_;
00177   JetCorrectionUncertainty* jecUncertainty_;
00178 
00179   bool addResidualJES_;
00180   std::string jetCorrLabelUpToL3_;    // L1+L2+L3 correction
00181   std::string jetCorrLabelUpToL3Res_; // L1+L2+L3+Residual correction
00182   double jetCorrEtaMax_; // do not use JEC factors for |eta| above this threshold (recommended default = 4.7),
00183                          // in order to work around problem with CMSSW_4_2_x JEC factors at high eta,
00184                          // reported in
00185                          //  https://hypernews.cern.ch/HyperNews/CMS/get/jes/270.html
00186                          //  https://hypernews.cern.ch/HyperNews/CMS/get/JetMET/1259/1.html
00187   Textractor jetCorrExtractor_; 
00188 
00189   double jecUncertaintyValue_;
00190 
00191   double shiftBy_; // set to +1.0/-1.0 for up/down variation of energy scale
00192 
00193   int verbosity_; // flag to enabled/disable debug output
00194 };
00195 
00196 #endif
00197 
00198  
00199