Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-28 03:10:11

0001 /** \class ShiftedJetProducerByMatchedObject
0002  *
0003  * Vary energy of jets coinciding in eta-phi with selected electrons/muons/tau-jets
0004  * by electron/muon/tau-jet energy uncertainty.
0005  *
0006  * \author Christian Veelken, LLR
0007  *
0008  */
0009 
0010 #include "DataFormats/Candidate/interface/Candidate.h"
0011 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0012 #include "DataFormats/Common/interface/Handle.h"
0013 #include "DataFormats/Common/interface/View.h"
0014 #include "DataFormats/Math/interface/deltaR.h"
0015 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0016 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/Framework/interface/EventSetup.h"
0019 #include "FWCore/Framework/interface/stream/EDProducer.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/Utilities/interface/InputTag.h"
0022 
0023 #include <string>
0024 #include <vector>
0025 
0026 template <typename T>
0027 class ShiftedJetProducerByMatchedObjectT : public edm::stream::EDProducer<> {
0028   typedef std::vector<T> JetCollection;
0029 
0030 public:
0031   explicit ShiftedJetProducerByMatchedObjectT(const edm::ParameterSet&);
0032   ~ShiftedJetProducerByMatchedObjectT() override;
0033 
0034 private:
0035   void produce(edm::Event&, const edm::EventSetup&) override;
0036 
0037   std::string moduleLabel_;
0038 
0039   edm::EDGetTokenT<JetCollection> srcJets_;
0040   edm::EDGetTokenT<edm::View<reco::Candidate> > srcUnshiftedObjects_;
0041   edm::EDGetTokenT<edm::View<reco::Candidate> > srcShiftedObjects_;
0042 
0043   double dRmatch_Jet_;
0044   double dRmatch_Object_;
0045 
0046   double dR2match_Jet_;
0047   double dR2match_Object_;
0048 
0049   struct objectEntryType {
0050     objectEntryType(const reco::Candidate::LorentzVector& shiftedObjectP4,
0051                     const reco::Candidate::LorentzVector& unshiftedObjectP4,
0052                     double dRmatch)
0053         : shiftedObjectP4_(shiftedObjectP4),
0054           unshiftedObjectP4_(unshiftedObjectP4),
0055           dRmatch_(dRmatch),
0056           isValidMatch_(false) {
0057       if (unshiftedObjectP4.energy() > 0.) {
0058         shift_ = (shiftedObjectP4.energy() / unshiftedObjectP4.energy()) - 1.;
0059         isValidMatch_ = true;
0060       }
0061     }
0062     ~objectEntryType() {}
0063     reco::Candidate::LorentzVector shiftedObjectP4_;
0064     reco::Candidate::LorentzVector unshiftedObjectP4_;
0065     double shift_;
0066     double dRmatch_;
0067     bool isValidMatch_;
0068   };
0069 
0070   std::vector<objectEntryType> objects_;
0071 };
0072 
0073 template <typename T>
0074 ShiftedJetProducerByMatchedObjectT<T>::ShiftedJetProducerByMatchedObjectT(const edm::ParameterSet& cfg)
0075     : moduleLabel_(cfg.getParameter<std::string>("@module_label")) {
0076   srcJets_ = consumes<JetCollection>(cfg.getParameter<edm::InputTag>("srcJets"));
0077   srcUnshiftedObjects_ = consumes<edm::View<reco::Candidate> >(cfg.getParameter<edm::InputTag>("srcUnshiftedObjects"));
0078   srcShiftedObjects_ = consumes<edm::View<reco::Candidate> >(cfg.getParameter<edm::InputTag>("srcShiftedObjects"));
0079 
0080   dRmatch_Jet_ = cfg.getParameter<double>("dRmatch_Jet");
0081   dRmatch_Object_ = cfg.exists("dRmatch_Object") ? cfg.getParameter<double>("dRmatch_Object") : 0.1;
0082 
0083   dR2match_Jet_ = dRmatch_Jet_ * dRmatch_Jet_;
0084   dR2match_Object_ = dRmatch_Object_ * dRmatch_Object_;
0085 
0086   produces<JetCollection>();
0087 }
0088 
0089 template <typename T>
0090 ShiftedJetProducerByMatchedObjectT<T>::~ShiftedJetProducerByMatchedObjectT() {
0091   // nothing to be done yet...
0092 }
0093 
0094 template <typename T>
0095 void ShiftedJetProducerByMatchedObjectT<T>::produce(edm::Event& evt, const edm::EventSetup& es) {
0096   edm::Handle<JetCollection> originalJets;
0097   evt.getByToken(srcJets_, originalJets);
0098 
0099   edm::Handle<reco::CandidateView> unshiftedObjects;
0100   evt.getByToken(srcUnshiftedObjects_, unshiftedObjects);
0101 
0102   edm::Handle<reco::CandidateView> shiftedObjects;
0103   evt.getByToken(srcShiftedObjects_, shiftedObjects);
0104 
0105   objects_.clear();
0106 
0107   std::vector<bool> match(shiftedObjects->size(), false);
0108   int prevMatch = -1;
0109   int cnt = 0;
0110 
0111   for (reco::CandidateView::const_iterator unshiftedObject = unshiftedObjects->begin();
0112        unshiftedObject != unshiftedObjects->end();
0113        ++unshiftedObject) {
0114     bool isMatched_Object = false;
0115     double dR2bestMatch_Object = std::numeric_limits<double>::max();
0116     prevMatch = -1;
0117     cnt = 0;
0118 
0119     reco::Candidate::LorentzVector shiftedObjectP4_matched;
0120     for (reco::CandidateView::const_iterator shiftedObject = shiftedObjects->begin();
0121          shiftedObject != shiftedObjects->end();
0122          ++shiftedObject) {
0123       if (match[cnt])
0124         continue;
0125 
0126       double dR2 = deltaR2(unshiftedObject->p4(), shiftedObject->p4());
0127       if (dR2 < dR2match_Object_ && dR2 < dR2bestMatch_Object) {
0128         shiftedObjectP4_matched = shiftedObject->p4();
0129         isMatched_Object = true;
0130         dR2bestMatch_Object = dR2;
0131 
0132         prevMatch = cnt;
0133       }
0134       cnt++;
0135     }
0136     if (isMatched_Object) {
0137       //Ambiguity removal
0138       match[prevMatch] = true;
0139       objects_.push_back(objectEntryType(shiftedObjectP4_matched, unshiftedObject->p4(), sqrt(dR2bestMatch_Object)));
0140     }
0141   }
0142 
0143   match.assign(objects_.size(), false);
0144 
0145   auto shiftedJets = std::make_unique<JetCollection>();
0146 
0147   for (typename JetCollection::const_iterator originalJet = originalJets->begin(); originalJet != originalJets->end();
0148        ++originalJet) {
0149     double shift = 0.;
0150     bool applyShift = false;
0151     double dR2bestMatch_Jet = std::numeric_limits<double>::max();
0152     prevMatch = -1;
0153     cnt = 0;
0154 
0155     for (typename std::vector<objectEntryType>::const_iterator object = objects_.begin(); object != objects_.end();
0156          ++object) {
0157       if (!object->isValidMatch_)
0158         continue;
0159       if (match[cnt])
0160         continue;
0161 
0162       double dR2 = deltaR2(originalJet->p4(), object->unshiftedObjectP4_);
0163       if (dR2 < dR2match_Jet_ && dR2 < dR2bestMatch_Jet) {
0164         shift = object->shift_;
0165         applyShift = true;
0166         dR2bestMatch_Jet = dR2;
0167 
0168         prevMatch = cnt;
0169       }
0170       cnt++;
0171     }
0172 
0173     reco::Candidate::LorentzVector shiftedJetP4 = originalJet->p4();
0174     if (applyShift) {
0175       //Ambiguity removal
0176       match[prevMatch] = true;
0177 
0178       shiftedJetP4 *= (1. + shift);
0179     }
0180 
0181     T shiftedJet(*originalJet);
0182     shiftedJet.setP4(shiftedJetP4);
0183 
0184     shiftedJets->push_back(shiftedJet);
0185   }
0186 
0187   evt.put(std::move(shiftedJets));
0188 }
0189 
0190 #include "DataFormats/JetReco/interface/CaloJet.h"
0191 #include "DataFormats/JetReco/interface/PFJet.h"
0192 
0193 typedef ShiftedJetProducerByMatchedObjectT<reco::CaloJet> ShiftedCaloJetProducerByMatchedObject;
0194 typedef ShiftedJetProducerByMatchedObjectT<reco::PFJet> ShiftedPFJetProducerByMatchedObject;
0195 
0196 #include "FWCore/Framework/interface/MakerMacros.h"
0197 
0198 DEFINE_FWK_MODULE(ShiftedCaloJetProducerByMatchedObject);
0199 DEFINE_FWK_MODULE(ShiftedPFJetProducerByMatchedObject);