Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /** \class ShiftedPFCandidateProducerByMatchedObject
0002  *
0003  * Vary energy of PFCandidates coinciding in eta-phi with selected electrons/muons/tau-jets/jets
0004  * by electron/muon/tau-jet/jet energy uncertainty.
0005  *
0006  * \author Christian Veelken, LLR
0007  *
0008  */
0009 
0010 #include "FWCore/Framework/interface/stream/EDProducer.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/EventSetup.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/Utilities/interface/InputTag.h"
0015 
0016 #include "DataFormats/Candidate/interface/Candidate.h"
0017 
0018 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0019 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0020 
0021 #include "DataFormats/Common/interface/Handle.h"
0022 #include "DataFormats/Common/interface/View.h"
0023 #include "DataFormats/Math/interface/deltaR.h"
0024 
0025 #include <string>
0026 #include <vector>
0027 
0028 class ShiftedPFCandidateProducerByMatchedObject : public edm::stream::EDProducer<> {
0029 public:
0030   explicit ShiftedPFCandidateProducerByMatchedObject(const edm::ParameterSet&);
0031   ~ShiftedPFCandidateProducerByMatchedObject() override;
0032 
0033 private:
0034   void produce(edm::Event&, const edm::EventSetup&) override;
0035 
0036   edm::EDGetTokenT<reco::PFCandidateCollection> srcPFCandidates_;
0037   edm::EDGetTokenT<edm::View<reco::Candidate> > srcUnshiftedObjects_;
0038   edm::EDGetTokenT<edm::View<reco::Candidate> > srcShiftedObjects_;
0039 
0040   double dRmatch_PFCandidate_;
0041   double dR2match_PFCandidate_;
0042   double dRmatch_Object_;
0043   double dR2match_Object_;
0044 
0045   struct objectEntryType {
0046     objectEntryType(const reco::Candidate::LorentzVector& shiftedObjectP4,
0047                     const reco::Candidate::LorentzVector& unshiftedObjectP4,
0048                     double dRmatch)
0049         : shiftedObjectP4_(shiftedObjectP4),
0050           unshiftedObjectP4_(unshiftedObjectP4),
0051           dRmatch_(dRmatch),
0052           isValidMatch_(false) {
0053       if (unshiftedObjectP4.energy() > 0.) {
0054         shift_ = (shiftedObjectP4.energy() / unshiftedObjectP4.energy()) - 1.;
0055         isValidMatch_ = true;
0056       }
0057     }
0058     ~objectEntryType() {}
0059     reco::Candidate::LorentzVector shiftedObjectP4_;
0060     reco::Candidate::LorentzVector unshiftedObjectP4_;
0061     double dRmatch_;
0062     double shift_;
0063     bool isValidMatch_;
0064   };
0065 
0066   std::vector<objectEntryType> objects_;
0067 };
0068 
0069 const double dRDefault = 1000;
0070 
0071 ShiftedPFCandidateProducerByMatchedObject::ShiftedPFCandidateProducerByMatchedObject(const edm::ParameterSet& cfg) {
0072   srcPFCandidates_ = consumes<reco::PFCandidateCollection>(cfg.getParameter<edm::InputTag>("srcPFCandidates"));
0073   srcUnshiftedObjects_ = consumes<edm::View<reco::Candidate> >(cfg.getParameter<edm::InputTag>("srcUnshiftedObjects"));
0074   srcShiftedObjects_ = consumes<edm::View<reco::Candidate> >(cfg.getParameter<edm::InputTag>("srcShiftedObjects"));
0075 
0076   dRmatch_PFCandidate_ = cfg.getParameter<double>("dRmatch_PFCandidate");
0077   dR2match_PFCandidate_ = dRmatch_PFCandidate_ * dRmatch_PFCandidate_;
0078   dRmatch_Object_ = cfg.exists("dRmatch_Object") ? cfg.getParameter<double>("dRmatch_Object") : 0.1;
0079   dR2match_Object_ = dRmatch_Object_ * dRmatch_Object_;
0080   produces<reco::PFCandidateCollection>();
0081 }
0082 
0083 ShiftedPFCandidateProducerByMatchedObject::~ShiftedPFCandidateProducerByMatchedObject() {
0084   // nothing to be done yet...
0085 }
0086 
0087 void ShiftedPFCandidateProducerByMatchedObject::produce(edm::Event& evt, const edm::EventSetup& es) {
0088   edm::Handle<reco::PFCandidateCollection> originalPFCandidates;
0089   evt.getByToken(srcPFCandidates_, originalPFCandidates);
0090 
0091   typedef edm::View<reco::Candidate> CandidateView;
0092 
0093   edm::Handle<CandidateView> unshiftedObjects;
0094   evt.getByToken(srcUnshiftedObjects_, unshiftedObjects);
0095 
0096   edm::Handle<CandidateView> shiftedObjects;
0097   evt.getByToken(srcShiftedObjects_, shiftedObjects);
0098 
0099   objects_.clear();
0100 
0101   CandidateView::const_iterator shiftedObjectP4_matched;
0102   bool isMatched_Object = false;
0103   double dR2bestMatch_Object = dRDefault;
0104   for (CandidateView::const_iterator unshiftedObject = unshiftedObjects->begin();
0105        unshiftedObject != unshiftedObjects->end();
0106        ++unshiftedObject) {
0107     isMatched_Object = false;
0108     dR2bestMatch_Object = dRDefault;
0109 
0110     for (CandidateView::const_iterator shiftedObject = shiftedObjects->begin(); shiftedObject != shiftedObjects->end();
0111          ++shiftedObject) {
0112       double dR2 = deltaR2(unshiftedObject->p4(), shiftedObject->p4());
0113       if (dR2 < dR2match_Object_ && dR2 < dR2bestMatch_Object) {
0114         shiftedObjectP4_matched = shiftedObject;
0115         isMatched_Object = true;
0116         dR2bestMatch_Object = dR2;
0117       }
0118     }
0119     if (isMatched_Object) {
0120       objects_.push_back(
0121           objectEntryType(shiftedObjectP4_matched->p4(), unshiftedObject->p4(), sqrt(dR2bestMatch_Object)));
0122     }
0123   }
0124 
0125   auto shiftedPFCandidates = std::make_unique<reco::PFCandidateCollection>();
0126 
0127   for (reco::PFCandidateCollection::const_iterator originalPFCandidate = originalPFCandidates->begin();
0128        originalPFCandidate != originalPFCandidates->end();
0129        ++originalPFCandidate) {
0130     double shift = 0.;
0131     bool applyShift = false;
0132     double dR2bestMatch_PFCandidate = dRDefault;
0133     for (std::vector<objectEntryType>::const_iterator object = objects_.begin(); object != objects_.end(); ++object) {
0134       if (!object->isValidMatch_)
0135         continue;
0136       double dR2 = deltaR2(originalPFCandidate->p4(), object->unshiftedObjectP4_);
0137       if (dR2 < dR2match_PFCandidate_ && dR2 < dR2bestMatch_PFCandidate) {
0138         shift = object->shift_;
0139         applyShift = true;
0140         dR2bestMatch_PFCandidate = dR2;
0141       }
0142     }
0143 
0144     reco::Candidate::LorentzVector shiftedPFCandidateP4 = originalPFCandidate->p4();
0145     if (applyShift) {
0146       double shiftedPx = (1. + shift) * originalPFCandidate->px();
0147       double shiftedPy = (1. + shift) * originalPFCandidate->py();
0148       double shiftedPz = (1. + shift) * originalPFCandidate->pz();
0149       double mass = originalPFCandidate->mass();
0150       double shiftedEn = sqrt(shiftedPx * shiftedPx + shiftedPy * shiftedPy + shiftedPz * shiftedPz + mass * mass);
0151       shiftedPFCandidateP4.SetPxPyPzE(shiftedPx, shiftedPy, shiftedPz, shiftedEn);
0152     }
0153 
0154     reco::PFCandidate shiftedPFCandidate(*originalPFCandidate);
0155     shiftedPFCandidate.setP4(shiftedPFCandidateP4);
0156 
0157     shiftedPFCandidates->push_back(shiftedPFCandidate);
0158   }
0159 
0160   evt.put(std::move(shiftedPFCandidates));
0161 }
0162 
0163 #include "FWCore/Framework/interface/MakerMacros.h"
0164 
0165 DEFINE_FWK_MODULE(ShiftedPFCandidateProducerByMatchedObject);