CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForNoPileUpPFMEt.cc

Go to the documentation of this file.
00001 #include "PhysicsTools/PatUtils/plugins/ShiftedPFCandidateProducerForNoPileUpPFMEt.h"
00002 
00003 #include "JetMETCorrections/Objects/interface/JetCorrector.h"
00004 #include "JetMETCorrections/Objects/interface/JetCorrectionsRecord.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 
00007 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
00008 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
00009 #include "DataFormats/JetReco/interface/PFJet.h"
00010 #include "DataFormats/JetReco/interface/PFJetCollection.h"
00011 #include "DataFormats/Common/interface/Handle.h"
00012 #include "DataFormats/Math/interface/deltaR.h"
00013 
00014 ShiftedPFCandidateProducerForNoPileUpPFMEt::ShiftedPFCandidateProducerForNoPileUpPFMEt(const edm::ParameterSet& cfg)
00015   : moduleLabel_(cfg.getParameter<std::string>("@module_label"))
00016 {
00017   srcPFCandidates_ = cfg.getParameter<edm::InputTag>("srcPFCandidates");
00018   srcJets_ = cfg.getParameter<edm::InputTag>("srcJets");
00019 
00020   jetCorrUncertaintyTag_ = cfg.getParameter<std::string>("jetCorrUncertaintyTag");
00021   if ( cfg.exists("jetCorrInputFileName") ) {
00022     jetCorrInputFileName_ = cfg.getParameter<edm::FileInPath>("jetCorrInputFileName");
00023     if ( jetCorrInputFileName_.location() == edm::FileInPath::Unknown) throw cms::Exception("ShiftedJetProducerT") 
00024       << " Failed to find JEC parameter file = " << jetCorrInputFileName_ << " !!\n";
00025     std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_  
00026               << " from file = " << jetCorrInputFileName_.fullPath() << "." << std::endl;
00027     jetCorrParameters_ = new JetCorrectorParameters(jetCorrInputFileName_.fullPath().data(), jetCorrUncertaintyTag_);
00028     jecUncertainty_ = new JetCorrectionUncertainty(*jetCorrParameters_);
00029   } else {
00030     std::cout << "Reading JEC parameters = " << jetCorrUncertaintyTag_
00031               << " from DB/SQLlite file." << std::endl;
00032     jetCorrPayloadName_ = cfg.getParameter<std::string>("jetCorrPayloadName");
00033   }
00034 
00035   minJetPt_ = cfg.getParameter<double>("minJetPt");
00036 
00037   shiftBy_ = cfg.getParameter<double>("shiftBy");
00038   
00039   unclEnUncertainty_ = cfg.getParameter<double>("unclEnUncertainty");
00040 
00041   produces<reco::PFCandidateCollection>();
00042 }
00043 
00044 ShiftedPFCandidateProducerForNoPileUpPFMEt::~ShiftedPFCandidateProducerForNoPileUpPFMEt()
00045 {
00046 // nothing to be done yet...
00047 }
00048 
00049 void ShiftedPFCandidateProducerForNoPileUpPFMEt::produce(edm::Event& evt, const edm::EventSetup& es)
00050 {
00051   edm::Handle<reco::PFCandidateCollection> originalPFCandidates;
00052   evt.getByLabel(srcPFCandidates_, originalPFCandidates);
00053 
00054   edm::Handle<reco::PFJetCollection> jets;
00055   evt.getByLabel(srcJets_, jets);
00056 
00057   std::vector<const reco::PFJet*> selectedJets;
00058   for ( reco::PFJetCollection::const_iterator jet = jets->begin();
00059         jet != jets->end(); ++jet ) {
00060     if ( jet->pt() > minJetPt_ ) selectedJets.push_back(&(*jet));
00061   }
00062 
00063   if ( jetCorrPayloadName_ != "" ) {
00064       edm::ESHandle<JetCorrectorParametersCollection> jetCorrParameterSet;
00065       es.get<JetCorrectionsRecord>().get(jetCorrPayloadName_, jetCorrParameterSet); 
00066       const JetCorrectorParameters& jetCorrParameters = (*jetCorrParameterSet)[jetCorrUncertaintyTag_];
00067       delete jecUncertainty_;
00068       jecUncertainty_ = new JetCorrectionUncertainty(jetCorrParameters);
00069     }
00070 
00071   std::auto_ptr<reco::PFCandidateCollection> shiftedPFCandidates(new reco::PFCandidateCollection);
00072 
00073   for ( reco::PFCandidateCollection::const_iterator originalPFCandidate = originalPFCandidates->begin();
00074         originalPFCandidate != originalPFCandidates->end(); ++originalPFCandidate ) {
00075     
00076     const reco::PFJet* jet_matched = 0;
00077     for ( std::vector<const reco::PFJet*>::iterator jet = selectedJets.begin();
00078           jet != selectedJets.end(); ++jet ) {
00079       std::vector<reco::PFCandidatePtr> jetConstituents = (*jet)->getPFConstituents();
00080       for ( std::vector<reco::PFCandidatePtr>::const_iterator jetConstituent = jetConstituents.begin();
00081             jetConstituent != jetConstituents.end() && !jet_matched; ++jetConstituent ) {
00082         if ( deltaR(originalPFCandidate->p4(), (*jetConstituent)->p4()) < 1.e-2 ) jet_matched = (*jet);
00083       }
00084     }
00085 
00086     double shift = 0.;
00087     if ( jet_matched ) {
00088       jecUncertainty_->setJetEta(jet_matched->eta());
00089       jecUncertainty_->setJetPt(jet_matched->pt());
00090       
00091       shift = jecUncertainty_->getUncertainty(true);
00092     } else {
00093       shift = unclEnUncertainty_;
00094     }
00095 
00096     shift *= shiftBy_;
00097     
00098     reco::Candidate::LorentzVector shiftedPFCandidateP4 = originalPFCandidate->p4();
00099     shiftedPFCandidateP4 *= (1. + shift);
00100     
00101     reco::PFCandidate shiftedPFCandidate(*originalPFCandidate);      
00102     shiftedPFCandidate.setP4(shiftedPFCandidateP4);
00103     
00104     shiftedPFCandidates->push_back(shiftedPFCandidate);
00105   }
00106   
00107   evt.put(shiftedPFCandidates);
00108 }
00109 
00110 #include "FWCore/Framework/interface/MakerMacros.h"
00111 
00112 DEFINE_FWK_MODULE(ShiftedPFCandidateProducerForNoPileUpPFMEt);
00113 
00114 
00115