File indexing completed on 2025-03-05 03:13:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <memory>
0013
0014 #include "DataFormats/Candidate/interface/Candidate.h"
0015 #include "DataFormats/Common/interface/ValueMap.h"
0016 #include "DataFormats/Math/interface/LorentzVector.h"
0017 #include "DataFormats/PatCandidates/interface/Electron.h"
0018 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
0019 #include "DataFormats/PatCandidates/interface/Muon.h"
0020 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0021 #include "FWCore/Framework/interface/Event.h"
0022 #include "FWCore/Framework/interface/Frameworkfwd.h"
0023 #include "FWCore/Framework/interface/MakerMacros.h"
0024 #include "FWCore/Framework/interface/global/EDProducer.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/Utilities/interface/StreamID.h"
0027
0028 class LeptonFSRProducer : public edm::global::EDProducer<> {
0029 public:
0030 explicit LeptonFSRProducer(const edm::ParameterSet& iConfig)
0031 : pfcands_{consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("packedPFCandidates"))},
0032 electronsForVeto_{consumes<pat::ElectronCollection>(iConfig.getParameter<edm::InputTag>("slimmedElectrons"))},
0033 muons_{consumes<edm::View<reco::Muon>>(iConfig.getParameter<edm::InputTag>("muons"))},
0034 electrons_{consumes<edm::View<reco::GsfElectron>>(iConfig.getParameter<edm::InputTag>("electrons"))},
0035 ptCutMu(iConfig.getParameter<double>("muonPtMin")),
0036 etaCutMu(iConfig.getParameter<double>("muonEtaMax")),
0037 ptCutE(iConfig.getParameter<double>("elePtMin")),
0038 etaCutE(iConfig.getParameter<double>("eleEtaMax")),
0039 photonPtCut(iConfig.getParameter<double>("photonPtMin")),
0040 drEtCut(iConfig.getParameter<double>("deltaROverEt2Max")),
0041 isoCut(iConfig.getParameter<double>("isolation")),
0042 drSafe(0.0001) {
0043 produces<std::vector<pat::GenericParticle>>();
0044 produces<edm::ValueMap<int>>("muFsrIndex");
0045 produces<edm::ValueMap<int>>("eleFsrIndex");
0046 }
0047 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0048 edm::ParameterSetDescription desc;
0049 desc.add<edm::InputTag>("packedPFCandidates", edm::InputTag("packedPFCandidates"))
0050 ->setComment("packed pf candidates where to look for photons");
0051 desc.add<edm::InputTag>("slimmedElectrons", edm::InputTag("slimmedElectrons"))
0052 ->setComment(
0053 "electrons to check for footprint, the electron collection must have proper linking with the "
0054 "packedCandidate collection");
0055 desc.add<edm::InputTag>("muons", edm::InputTag("slimmedMuons"))
0056 ->setComment("collection of muons to match with FSR ");
0057 desc.add<edm::InputTag>("electrons", edm::InputTag("slimmedElectrons"))
0058 ->setComment("collection of electrons to match with FSR ");
0059 desc.add<double>("muonPtMin", 3.)->setComment("minimum pt of the muon to look for a near photon");
0060 desc.add<double>("muonEtaMax", 2.4)->setComment("max eta of the muon to look for a near photon");
0061 desc.add<double>("elePtMin", 5.)->setComment("minimum pt of the electron to look for a near photon");
0062 desc.add<double>("eleEtaMax", 2.5)->setComment("max eta of the electron to look for a near photon");
0063 desc.add<double>("photonPtMin", 2.0)->setComment("minimum photon Pt");
0064 desc.add<double>("deltaROverEt2Max", 0.05)->setComment("max ratio of deltaR(lep,photon) over et2 of the photon");
0065 desc.add<double>("isolation", 2.0)->setComment("photon relative isolation cut");
0066
0067 descriptions.addWithDefaultLabel(desc);
0068 }
0069 ~LeptonFSRProducer() override = default;
0070
0071 private:
0072 void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0073
0074 double computeRelativeIsolation(const pat::PackedCandidate& photon,
0075 const pat::PackedCandidateCollection& pfcands,
0076 const double& isoConeMax2,
0077 const double& isoConeMin2) const;
0078
0079 bool electronFootprintVeto(pat::PackedCandidateRef& pfcandRef,
0080 edm::Handle<pat::ElectronCollection> electronsForVeto) const;
0081
0082
0083 const edm::EDGetTokenT<pat::PackedCandidateCollection> pfcands_;
0084 const edm::EDGetTokenT<pat::ElectronCollection> electronsForVeto_;
0085 const edm::EDGetTokenT<edm::View<reco::Muon>> muons_;
0086 const edm::EDGetTokenT<edm::View<reco::GsfElectron>> electrons_;
0087 const double ptCutMu;
0088 const double etaCutMu;
0089 const double ptCutE;
0090 const double etaCutE;
0091 const double photonPtCut;
0092 const double drEtCut;
0093 const double isoCut;
0094 const double drSafe;
0095 };
0096
0097 void LeptonFSRProducer::produce(edm::StreamID streamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0098 using namespace std;
0099
0100 edm::Handle<pat::PackedCandidateCollection> pfcands;
0101 iEvent.getByToken(pfcands_, pfcands);
0102 edm::Handle<edm::View<reco::Muon>> muons;
0103 iEvent.getByToken(muons_, muons);
0104 edm::Handle<edm::View<reco::GsfElectron>> electrons;
0105 iEvent.getByToken(electrons_, electrons);
0106 edm::Handle<pat::ElectronCollection> electronsForVeto;
0107 iEvent.getByToken(electronsForVeto_, electronsForVeto);
0108
0109
0110 auto fsrPhotons = std::make_unique<std::vector<pat::GenericParticle>>();
0111
0112 std::vector<int> muPhotonIdxs(muons->size(), -1);
0113 std::vector<double> muPhotonDRET2(muons->size(), 1e9);
0114
0115 std::vector<int> elePhotonIdxs(electrons->size(), -1);
0116 std::vector<double> elePhotonDRET2(electrons->size(), 1e9);
0117
0118
0119
0120
0121
0122 for (auto pc = pfcands->begin(); pc != pfcands->end(); pc++) {
0123
0124 if (abs(pc->pdgId()) != 22 || pc->pt() < photonPtCut || abs(pc->eta()) > 2.5)
0125 continue;
0126
0127
0128
0129
0130 double dRMin(0.5);
0131 int closestMu = -1;
0132 int closestEle = -1;
0133 double photon_relIso03 = 1e9;
0134 bool skipPhoton = false;
0135
0136 for (auto muon = muons->begin(); muon != muons->end(); ++muon) {
0137 if (muon->pt() < ptCutMu || std::abs(muon->eta()) > etaCutMu)
0138 continue;
0139
0140 int muonIdx = muon - muons->begin();
0141 double dR = deltaR(muon->eta(), muon->phi(), pc->eta(), pc->phi());
0142 if (dR < dRMin && dR > drSafe && dR < drEtCut * pc->pt() * pc->pt()) {
0143
0144 photon_relIso03 = computeRelativeIsolation(*pc, *pfcands, 0.3 * 0.3, drSafe * drSafe);
0145 if (photon_relIso03 > isoCut) {
0146 skipPhoton = true;
0147 break;
0148 }
0149
0150 pat::PackedCandidateRef pfcandRef = pat::PackedCandidateRef(pfcands, pc - pfcands->begin());
0151 skipPhoton = electronFootprintVeto(pfcandRef, electronsForVeto);
0152 if (skipPhoton)
0153 break;
0154
0155
0156 dRMin = dR;
0157 closestMu = muonIdx;
0158 }
0159 }
0160
0161 if (skipPhoton)
0162 continue;
0163
0164 for (auto ele = electrons->begin(); ele != electrons->end(); ++ele) {
0165 if (ele->pt() < ptCutE || std::abs(ele->eta()) > etaCutE)
0166 continue;
0167
0168 int eleIdx = ele - electrons->begin();
0169 double dR = deltaR(ele->eta(), ele->phi(), pc->eta(), pc->phi());
0170 if (dR < dRMin && dR > drSafe && dR < drEtCut * pc->pt() * pc->pt()) {
0171
0172 if (photon_relIso03 > 1e8) {
0173 photon_relIso03 = computeRelativeIsolation(*pc, *pfcands, 0.3 * 0.3, drSafe * drSafe);
0174 }
0175 if (photon_relIso03 > isoCut) {
0176 break;
0177 }
0178
0179 pat::PackedCandidateRef pfcandRef = pat::PackedCandidateRef(pfcands, pc - pfcands->begin());
0180 if (electronFootprintVeto(pfcandRef, electronsForVeto)) {
0181 break;
0182 }
0183
0184
0185 dRMin = dR;
0186 closestEle = eleIdx;
0187 closestMu = -1;
0188 }
0189 }
0190
0191 if (closestMu >= 0 || closestEle >= 0) {
0192
0193 double dRET2 = dRMin / pc->pt() / pc->pt();
0194 int iPhoton = fsrPhotons->size();
0195 fsrPhotons->push_back(pat::GenericParticle(*pc));
0196 fsrPhotons->back().addUserFloat("relIso03", photon_relIso03);
0197 fsrPhotons->back().addUserFloat("dROverEt2", dRET2);
0198
0199 if (closestMu >= 0) {
0200 fsrPhotons->back().addUserCand("associatedMuon", reco::CandidatePtr(muons, closestMu));
0201
0202 if (dRET2 < muPhotonDRET2[closestMu]) {
0203 muPhotonDRET2[closestMu] = dRET2;
0204 muPhotonIdxs[closestMu] = iPhoton;
0205 }
0206 } else if (closestEle >= 0) {
0207
0208 fsrPhotons->back().addUserCand("associatedElectron", reco::CandidatePtr(electrons, closestEle));
0209 if (dRET2 < elePhotonDRET2[closestEle]) {
0210 elePhotonDRET2[closestEle] = dRET2;
0211 elePhotonIdxs[closestEle] = iPhoton;
0212 }
0213 }
0214 }
0215 }
0216
0217 iEvent.put(std::move(fsrPhotons));
0218
0219 {
0220 std::unique_ptr<edm::ValueMap<int>> bareIdx(new edm::ValueMap<int>());
0221 edm::ValueMap<int>::Filler fillerBareIdx(*bareIdx);
0222 fillerBareIdx.insert(muons, muPhotonIdxs.begin(), muPhotonIdxs.end());
0223 fillerBareIdx.fill();
0224 iEvent.put(std::move(bareIdx), "muFsrIndex");
0225 }
0226
0227 {
0228 std::unique_ptr<edm::ValueMap<int>> bareIdx(new edm::ValueMap<int>());
0229 edm::ValueMap<int>::Filler fillerBareIdx(*bareIdx);
0230 fillerBareIdx.insert(electrons, elePhotonIdxs.begin(), elePhotonIdxs.end());
0231 fillerBareIdx.fill();
0232 iEvent.put(std::move(bareIdx), "eleFsrIndex");
0233 }
0234 }
0235
0236 double LeptonFSRProducer::computeRelativeIsolation(const pat::PackedCandidate& photon,
0237 const pat::PackedCandidateCollection& pfcands,
0238 const double& isoConeMax2,
0239 const double& isoConeMin2) const {
0240 double ptsum = 0;
0241
0242 for (const auto& pfcand : pfcands) {
0243
0244 double dRIsoCone2 = deltaR2(photon.eta(), photon.phi(), pfcand.eta(), pfcand.phi());
0245 if (dRIsoCone2 > isoConeMax2 || dRIsoCone2 < isoConeMin2)
0246 continue;
0247
0248
0249 if (pfcand.charge() != 0 && abs(pfcand.pdgId()) == 211 && pfcand.pt() > 0.2 && dRIsoCone2 > drSafe * drSafe) {
0250 ptsum += pfcand.pt();
0251
0252 } else if (pfcand.charge() == 0 && (abs(pfcand.pdgId()) == 22 || abs(pfcand.pdgId()) == 130) && pfcand.pt() > 0.5 &&
0253 dRIsoCone2 > 0.01 * 0.01) {
0254 ptsum += pfcand.pt();
0255 }
0256 }
0257
0258 return ptsum / photon.pt();
0259 }
0260
0261 bool LeptonFSRProducer::electronFootprintVeto(pat::PackedCandidateRef& pfcandRef,
0262 edm::Handle<pat::ElectronCollection> electronsForVeto) const {
0263 bool skipPhoton = false;
0264 for (auto electrons_iter = electronsForVeto->begin(); electrons_iter != electronsForVeto->end(); ++electrons_iter) {
0265 for (auto const& cand : electrons_iter->associatedPackedPFCandidates()) {
0266 if (!cand.isAvailable())
0267 continue;
0268 if (cand.id() != pfcandRef.id())
0269 throw cms::Exception("Configuration")
0270 << "The electron associatedPackedPFCandidates item does not have "
0271 << "the same ID of packed candidate collection used for cleaning the electron footprint: " << cand.id()
0272 << " (" << pfcandRef.id() << ")\n";
0273 if (cand.key() == pfcandRef.key()) {
0274 skipPhoton = true;
0275 break;
0276 }
0277 }
0278 }
0279 return skipPhoton;
0280 }
0281
0282
0283 DEFINE_FWK_MODULE(LeptonFSRProducer);