CMS 3D CMS Logo

ShiftedPFCandidateProducerByMatchedObject.cc
Go to the documentation of this file.
1 
15 
17 
20 
24 
25 #include <string>
26 #include <vector>
27 
29 public:
32 
33 private:
34  void produce(edm::Event&, const edm::EventSetup&) override;
35 
39 
44 
45  struct objectEntryType {
47  const reco::Candidate::LorentzVector& unshiftedObjectP4,
48  double dRmatch)
49  : shiftedObjectP4_(shiftedObjectP4),
50  unshiftedObjectP4_(unshiftedObjectP4),
51  dRmatch_(dRmatch),
53  if (unshiftedObjectP4.energy() > 0.) {
54  shift_ = (shiftedObjectP4.energy() / unshiftedObjectP4.energy()) - 1.;
55  isValidMatch_ = true;
56  }
57  }
61  double dRmatch_;
62  double shift_;
64  };
65 
66  std::vector<objectEntryType> objects_;
67 };
68 
69 const double dRDefault = 1000;
70 
72  srcPFCandidates_ = consumes<reco::PFCandidateCollection>(cfg.getParameter<edm::InputTag>("srcPFCandidates"));
73  srcUnshiftedObjects_ = consumes<edm::View<reco::Candidate> >(cfg.getParameter<edm::InputTag>("srcUnshiftedObjects"));
74  srcShiftedObjects_ = consumes<edm::View<reco::Candidate> >(cfg.getParameter<edm::InputTag>("srcShiftedObjects"));
75 
76  dRmatch_PFCandidate_ = cfg.getParameter<double>("dRmatch_PFCandidate");
78  dRmatch_Object_ = cfg.exists("dRmatch_Object") ? cfg.getParameter<double>("dRmatch_Object") : 0.1;
80  produces<reco::PFCandidateCollection>();
81 }
82 
84  // nothing to be done yet...
85 }
86 
88  edm::Handle<reco::PFCandidateCollection> originalPFCandidates;
89  evt.getByToken(srcPFCandidates_, originalPFCandidates);
90 
92 
93  edm::Handle<CandidateView> unshiftedObjects;
94  evt.getByToken(srcUnshiftedObjects_, unshiftedObjects);
95 
96  edm::Handle<CandidateView> shiftedObjects;
97  evt.getByToken(srcShiftedObjects_, shiftedObjects);
98 
99  objects_.clear();
100 
101  CandidateView::const_iterator shiftedObjectP4_matched;
102  bool isMatched_Object = false;
103  double dR2bestMatch_Object = dRDefault;
104  for (CandidateView::const_iterator unshiftedObject = unshiftedObjects->begin();
105  unshiftedObject != unshiftedObjects->end();
106  ++unshiftedObject) {
107  isMatched_Object = false;
108  dR2bestMatch_Object = dRDefault;
109 
110  for (CandidateView::const_iterator shiftedObject = shiftedObjects->begin(); shiftedObject != shiftedObjects->end();
111  ++shiftedObject) {
112  double dR2 = deltaR2(unshiftedObject->p4(), shiftedObject->p4());
113  if (dR2 < dR2match_Object_ && dR2 < dR2bestMatch_Object) {
114  shiftedObjectP4_matched = shiftedObject;
115  isMatched_Object = true;
116  dR2bestMatch_Object = dR2;
117  }
118  }
119  if (isMatched_Object) {
120  objects_.push_back(
121  objectEntryType(shiftedObjectP4_matched->p4(), unshiftedObject->p4(), sqrt(dR2bestMatch_Object)));
122  }
123  }
124 
125  auto shiftedPFCandidates = std::make_unique<reco::PFCandidateCollection>();
126 
127  for (reco::PFCandidateCollection::const_iterator originalPFCandidate = originalPFCandidates->begin();
128  originalPFCandidate != originalPFCandidates->end();
129  ++originalPFCandidate) {
130  double shift = 0.;
131  bool applyShift = false;
132  double dR2bestMatch_PFCandidate = dRDefault;
133  for (std::vector<objectEntryType>::const_iterator object = objects_.begin(); object != objects_.end(); ++object) {
134  if (!object->isValidMatch_)
135  continue;
136  double dR2 = deltaR2(originalPFCandidate->p4(), object->unshiftedObjectP4_);
137  if (dR2 < dR2match_PFCandidate_ && dR2 < dR2bestMatch_PFCandidate) {
138  shift = object->shift_;
139  applyShift = true;
140  dR2bestMatch_PFCandidate = dR2;
141  }
142  }
143 
144  reco::Candidate::LorentzVector shiftedPFCandidateP4 = originalPFCandidate->p4();
145  if (applyShift) {
146  double shiftedPx = (1. + shift) * originalPFCandidate->px();
147  double shiftedPy = (1. + shift) * originalPFCandidate->py();
148  double shiftedPz = (1. + shift) * originalPFCandidate->pz();
149  double mass = originalPFCandidate->mass();
150  double shiftedEn = sqrt(shiftedPx * shiftedPx + shiftedPy * shiftedPy + shiftedPz * shiftedPz + mass * mass);
151  shiftedPFCandidateP4.SetPxPyPzE(shiftedPx, shiftedPy, shiftedPz, shiftedEn);
152  }
153 
154  reco::PFCandidate shiftedPFCandidate(*originalPFCandidate);
155  shiftedPFCandidate.setP4(shiftedPFCandidateP4);
156 
157  shiftedPFCandidates->push_back(shiftedPFCandidate);
158  }
159 
160  evt.put(std::move(shiftedPFCandidates));
161 }
162 
164 
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
objectEntryType(const reco::Candidate::LorentzVector &shiftedObjectP4, const reco::Candidate::LorentzVector &unshiftedObjectP4, double dRmatch)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:528
edm::EDGetTokenT< edm::View< reco::Candidate > > srcShiftedObjects_
ALPAKA_FN_ACC static ALPAKA_FN_INLINE float dR2(Position4 pos1, Position4 pos2)
T sqrt(T t)
Definition: SSEVec.h:19
edm::EDGetTokenT< reco::PFCandidateCollection > srcPFCandidates_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< edm::View< reco::Candidate > > srcUnshiftedObjects_
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:36
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
static unsigned int const shift
void produce(edm::Event &, const edm::EventSetup &) override
void setP4(const LorentzVector &p4) final
set 4-momentum
def move(src, dest)
Definition: eostools.py:511
edm::View< Candidate > CandidateView
view of a collection containing candidates
Definition: CandidateFwd.h:23