File indexing completed on 2024-09-07 04:37:23
0001 #include "FWCore/Framework/interface/global/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/Utilities/interface/InputTag.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006
0007 #include "DataFormats/Common/interface/View.h"
0008 #include "DataFormats/Candidate/interface/Candidate.h"
0009 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0010 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0011 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0012
0013 #include "DataFormats/Math/interface/deltaR.h"
0014
0015 class PFMatchedCandidateRefExtractor : public edm::global::EDProducer<> {
0016 public:
0017 explicit PFMatchedCandidateRefExtractor(const edm::ParameterSet& iConfig);
0018 ~PFMatchedCandidateRefExtractor() override;
0019
0020 void produce(edm::StreamID iID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0021
0022 private:
0023 edm::EDGetTokenT<edm::View<reco::Candidate>> col1Token_;
0024 edm::EDGetTokenT<edm::View<reco::Candidate>> col2Token_;
0025 std::string moduleLabel_;
0026 edm::EDGetTokenT<edm::View<reco::PFCandidate>> pfCandToken_;
0027
0028 bool extractPFCands_;
0029 };
0030
0031 PFMatchedCandidateRefExtractor::PFMatchedCandidateRefExtractor(const edm::ParameterSet& iConfig)
0032 : col1Token_(consumes<edm::View<reco::Candidate>>(iConfig.getParameter<edm::InputTag>("col1"))),
0033 col2Token_(consumes<edm::View<reco::Candidate>>(iConfig.getParameter<edm::InputTag>("col2"))),
0034 moduleLabel_(iConfig.getParameter<std::string>("@module_label")) {
0035
0036
0037 extractPFCands_ = iConfig.getParameter<bool>("extractPFCandidates");
0038
0039 produces<edm::PtrVector<reco::Candidate>>("col1");
0040 produces<edm::PtrVector<reco::Candidate>>("col2");
0041 if (extractPFCands_) {
0042 pfCandToken_ = mayConsume<edm::View<reco::PFCandidate>>(iConfig.getParameter<edm::InputTag>("pfCandCollection"));
0043 produces<edm::PtrVector<reco::PFCandidate>>("pfCandCol1");
0044 produces<edm::PtrVector<reco::PFCandidate>>("pfCandCol2");
0045 }
0046 }
0047
0048 PFMatchedCandidateRefExtractor::~PFMatchedCandidateRefExtractor() {}
0049
0050
0051 void PFMatchedCandidateRefExtractor::produce(edm::StreamID iID, edm::Event& iEvent, const edm::EventSetup&) const {
0052 std::unique_ptr<edm::PtrVector<reco::Candidate>> outcol1(new edm::PtrVector<reco::Candidate>());
0053 std::unique_ptr<edm::PtrVector<reco::Candidate>> outcol2(new edm::PtrVector<reco::Candidate>());
0054
0055 std::unique_ptr<edm::PtrVector<reco::PFCandidate>> outPFCandCol1(new edm::PtrVector<reco::PFCandidate>());
0056 std::unique_ptr<edm::PtrVector<reco::PFCandidate>> outPFCandCol2(new edm::PtrVector<reco::PFCandidate>());
0057
0058 edm::Handle<edm::View<reco::Candidate>> col1Handle;
0059 edm::Handle<edm::View<reco::Candidate>> col2Handle;
0060 edm::Handle<edm::View<reco::PFCandidate>> pfCandHandle;
0061
0062 iEvent.getByToken(col1Token_, col1Handle);
0063 iEvent.getByToken(col2Token_, col2Handle);
0064 if (extractPFCands_) {
0065 iEvent.getByToken(pfCandToken_, pfCandHandle);
0066 }
0067
0068 for (size_t iC1 = 0; iC1 < col1Handle->size(); iC1++) {
0069 for (size_t iC2 = 0; iC2 < col2Handle->size(); iC2++) {
0070 bool matched = (col1Handle->ptrAt(iC1) == col2Handle->ptrAt(iC2));
0071 if (!matched && deltaR2(col1Handle->ptrAt(iC1)->p4(), col2Handle->ptrAt(iC2)->p4()) < 0.000001) {
0072 matched = true;
0073 }
0074
0075 if (matched) {
0076 outcol1->push_back(col1Handle->ptrAt(iC1));
0077 outcol2->push_back(col2Handle->ptrAt(iC2));
0078
0079 if (!extractPFCands_)
0080 continue;
0081 std::set<reco::CandidatePtr> sc1s;
0082 std::set<reco::CandidatePtr> sc2s;
0083 for (size_t ics1 = 0; ics1 < col1Handle->ptrAt(iC1)->numberOfSourceCandidatePtrs(); ics1++) {
0084 sc1s.insert(col1Handle->ptrAt(iC1)->sourceCandidatePtr(ics1));
0085 }
0086 for (size_t ics2 = 0; ics2 < col2Handle->ptrAt(iC2)->numberOfSourceCandidatePtrs(); ics2++) {
0087 sc2s.insert(col2Handle->ptrAt(iC2)->sourceCandidatePtr(ics2));
0088 }
0089
0090 for (size_t ic = 0; ic < pfCandHandle->size(); ++ic) {
0091 reco::PFCandidatePtr c = pfCandHandle->ptrAt(ic);
0092
0093 bool match1 = (sc1s.find(c) != sc1s.end());
0094 bool match2 = (sc2s.find(c) != sc2s.end());
0095
0096 if (!match1) {
0097 for (size_t ics1 = 0; ics1 < sc1s.size(); ics1++) {
0098 if (deltaR2(c->p4(),
0099 col1Handle->ptrAt(iC1)->sourceCandidatePtr(ics1)->p4()) <
0100 0.0000001) {
0101 match1 = true;
0102 break;
0103 }
0104 }
0105 }
0106 if (!match2) {
0107 for (size_t ics2 = 0; ics2 < sc2s.size(); ics2++) {
0108 if (deltaR2(c->p4(),
0109 col2Handle->ptrAt(iC2)->sourceCandidatePtr(ics2)->p4()) < 0.00001) {
0110 match2 = true;
0111 break;
0112 }
0113 }
0114 }
0115
0116 if (match1) {
0117 outPFCandCol1->push_back(c);
0118 }
0119 if (match2) {
0120 outPFCandCol2->push_back(c);
0121 }
0122 }
0123
0124 }
0125 }
0126 }
0127
0128 iEvent.put(std::move(outcol1), "col1");
0129 iEvent.put(std::move(outcol2), "col2");
0130 if (extractPFCands_) {
0131 iEvent.put(std::move(outPFCandCol1), "pfCandCol1");
0132 iEvent.put(std::move(outPFCandCol2), "pfCandCol2");
0133 }
0134 }
0135
0136
0137 DEFINE_FWK_MODULE(PFMatchedCandidateRefExtractor);