File indexing completed on 2024-04-06 12:26:44
0001
0002 #include <memory>
0003 #include <iostream>
0004
0005
0006 #include "FWCore/Framework/interface/Frameworkfwd.h"
0007 #include "FWCore/Framework/interface/global/EDFilter.h"
0008
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013
0014 #include "DataFormats/Math/interface/deltaR.h"
0015 #include "DataFormats/MuonReco/interface/Muon.h"
0016 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0017 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
0018 #include "DataFormats/TrackReco/interface/Track.h"
0019 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0020 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0021 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0022 #include "DataFormats/VertexReco/interface/Vertex.h"
0023 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0025
0026
0027
0028
0029
0030 class BadParticleFilter : public edm::global::EDFilter<> {
0031 public:
0032 explicit BadParticleFilter(const edm::ParameterSet&);
0033 ~BadParticleFilter() override;
0034 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0035
0036 private:
0037 bool filter(edm::StreamID iID, edm::Event&, const edm::EventSetup&) const override;
0038
0039
0040
0041 edm::EDGetTokenT<edm::View<reco::Candidate>> tokenPFCandidates_;
0042 edm::EDGetTokenT<edm::View<reco::Muon>> tokenMuons_;
0043 edm::EDGetTokenT<std::vector<reco::Vertex>> vtx_;
0044
0045 const bool taggingMode_;
0046 int algo_;
0047 const double maxDR_;
0048 const double minPtDiffRel_;
0049 const double minMuonTrackRelErr_;
0050 const double innerTrackRelErr_;
0051 const double minDzBestTrack_;
0052 const double minMuPt_;
0053 const double segmentCompatibility_;
0054
0055 double maxDR2_;
0056
0057 int filterType_;
0058 enum { kBadPFMuon = 0, kBadPFMuonSummer16, kBadChargedCandidate, kBadChargedCandidateSummer16, kBadPFMuonDz };
0059 };
0060
0061
0062
0063
0064 BadParticleFilter::BadParticleFilter(const edm::ParameterSet& iConfig)
0065 : tokenPFCandidates_(consumes<edm::View<reco::Candidate>>(iConfig.getParameter<edm::InputTag>("PFCandidates"))),
0066 tokenMuons_(consumes<edm::View<reco::Muon>>(iConfig.getParameter<edm::InputTag>("muons"))),
0067 vtx_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("vtx"))),
0068 taggingMode_(iConfig.getParameter<bool>("taggingMode")),
0069 maxDR_(iConfig.getParameter<double>("maxDR")),
0070 minPtDiffRel_(iConfig.getParameter<double>("minPtDiffRel")),
0071 minMuonTrackRelErr_(iConfig.getParameter<double>("minMuonTrackRelErr")),
0072 innerTrackRelErr_(iConfig.getParameter<double>("innerTrackRelErr")),
0073 minDzBestTrack_(iConfig.getParameter<double>("minDzBestTrack")),
0074 minMuPt_(iConfig.getParameter<double>("minMuonPt")),
0075 segmentCompatibility_(iConfig.getParameter<double>("segmentCompatibility")) {
0076 maxDR2_ = maxDR_ * maxDR_;
0077
0078 std::string filterName = iConfig.getParameter<std::string>("filterType");
0079 if (filterName == "BadPFMuon")
0080 filterType_ = kBadPFMuon;
0081 else if (filterName == "BadPFMuonSummer16")
0082 filterType_ = kBadPFMuonSummer16;
0083 else if (filterName == "BadChargedCandidate")
0084 filterType_ = kBadChargedCandidate;
0085 else if (filterName == "BadChargedCandidateSummer16")
0086 filterType_ = kBadChargedCandidateSummer16;
0087 else if (filterName == "BadPFMuonDz")
0088 filterType_ = kBadPFMuonDz;
0089 else {
0090 throw cms::Exception("BadParticleFilter") << " Filter " << filterName << " is not available, please check name \n";
0091 }
0092
0093 algo_ = 0;
0094 if (filterType_ == kBadPFMuon || filterType_ == kBadPFMuonDz) {
0095 algo_ = iConfig.getParameter<int>("algo");
0096 }
0097
0098 produces<bool>();
0099 }
0100
0101 BadParticleFilter::~BadParticleFilter() {}
0102
0103
0104
0105
0106
0107
0108 bool BadParticleFilter::filter(edm::StreamID iID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0109 using namespace std;
0110 using namespace edm;
0111
0112 typedef View<reco::Candidate> CandidateView;
0113 Handle<CandidateView> pfCandidates;
0114 iEvent.getByToken(tokenPFCandidates_, pfCandidates);
0115
0116 typedef View<reco::Muon> MuonView;
0117 Handle<MuonView> muons;
0118 iEvent.getByToken(tokenMuons_, muons);
0119
0120 auto const& aPV = iEvent.get(vtx_).at(0).position();
0121
0122 bool foundBadCandidate = false;
0123
0124 for (unsigned i = 0; i < muons->size(); ++i) {
0125
0126 const reco::Muon& muon = (*muons)[i];
0127
0128 reco::TrackRef innerMuonTrack = muon.innerTrack();
0129 reco::TrackRef bestMuonTrack = muon.muonBestTrack();
0130
0131 if (innerMuonTrack.isNull()) {
0132 continue;
0133 }
0134
0135 if (filterType_ == kBadChargedCandidate || filterType_ == kBadPFMuon || filterType_ == kBadPFMuonDz) {
0136 if (muon.pt() < minMuPt_ && innerMuonTrack->pt() < minMuPt_)
0137 continue;
0138 }
0139 if (filterType_ == kBadChargedCandidateSummer16) {
0140 if (muon.pt() < minMuPt_)
0141 continue;
0142 }
0143 if (filterType_ == kBadPFMuonSummer16) {
0144 if (innerMuonTrack->pt() < minMuPt_)
0145 continue;
0146 }
0147
0148
0149 if (filterType_ == kBadChargedCandidate || filterType_ == kBadPFMuon || filterType_ == kBadPFMuonDz) {
0150 if (muon.isGlobalMuon() == 0)
0151 continue;
0152 }
0153
0154 if (filterType_ == kBadPFMuon || filterType_ == kBadPFMuonSummer16 || filterType_ == kBadPFMuonDz) {
0155 if (!(innerMuonTrack->originalAlgo() == algo_ && innerMuonTrack->algo() == algo_))
0156 continue;
0157 }
0158
0159 if (filterType_ == kBadChargedCandidate || filterType_ == kBadPFMuon || filterType_ == kBadPFMuonDz) {
0160 if (muon::segmentCompatibility(muon) > segmentCompatibility_ &&
0161 bestMuonTrack->ptError() / bestMuonTrack->pt() < minMuonTrackRelErr_ &&
0162 innerMuonTrack->ptError() / innerMuonTrack->pt() < innerTrackRelErr_) {
0163 if (filterType_ == kBadChargedCandidate || filterType_ == kBadPFMuon) {
0164 continue;
0165 }
0166
0167 if (filterType_ == kBadPFMuonDz) {
0168 if (fabs(bestMuonTrack->dz(aPV)) < minDzBestTrack_) {
0169 continue;
0170 }
0171 }
0172 }
0173 }
0174
0175 if (filterType_ == kBadChargedCandidateSummer16 || filterType_ == kBadPFMuonSummer16) {
0176 if (innerMuonTrack->quality(reco::TrackBase::highPurity))
0177 continue;
0178 if (!(innerMuonTrack->ptError() / innerMuonTrack->pt() > minMuonTrackRelErr_))
0179 continue;
0180 }
0181
0182 for (unsigned j = 0; j < pfCandidates->size(); ++j) {
0183 const reco::Candidate& pfCandidate = (*pfCandidates)[j];
0184
0185 float dr2 = 1000;
0186 if (filterType_ == kBadChargedCandidate || filterType_ == kBadChargedCandidateSummer16) {
0187 if (!(std::abs(pfCandidate.pdgId()) == 211))
0188 continue;
0189 dr2 = deltaR2(innerMuonTrack->eta(), innerMuonTrack->phi(), pfCandidate.eta(), pfCandidate.phi());
0190 float dpt = (pfCandidate.pt() - innerMuonTrack->pt()) / (0.5 * (innerMuonTrack->pt() + pfCandidate.pt()));
0191 if ((dr2 < maxDR2_) && (std::abs(dpt) < minPtDiffRel_) &&
0192 (filterType_ == kBadChargedCandidateSummer16 || muon.isPFMuon() == 0)) {
0193 foundBadCandidate = true;
0194 break;
0195 }
0196 }
0197
0198 if (filterType_ == kBadPFMuon || filterType_ == kBadPFMuonSummer16 || filterType_ == kBadPFMuonDz) {
0199 if (!((std::abs(pfCandidate.pdgId()) == 13) && (pfCandidate.pt() > minMuPt_)))
0200 continue;
0201 dr2 = deltaR2(muon.eta(), muon.phi(), pfCandidate.eta(), pfCandidate.phi());
0202 if (dr2 < maxDR2_) {
0203 foundBadCandidate = true;
0204 break;
0205 }
0206 }
0207
0208 if (foundBadCandidate)
0209 break;
0210 }
0211 }
0212
0213 bool pass = !foundBadCandidate;
0214
0215 iEvent.put(std::make_unique<bool>(pass));
0216
0217 return taggingMode_ || pass;
0218 }
0219
0220 void BadParticleFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0221
0222 edm::ParameterSetDescription desc;
0223 desc.add<double>("innerTrackRelErr", 1.0);
0224 desc.add<double>("minDzBestTrack", -1.0);
0225 desc.add<edm::InputTag>("PFCandidates", edm::InputTag("particleFlow"));
0226 desc.add<std::string>("filterType", "BadPFMuon");
0227 desc.add<double>("segmentCompatibility", 0.3);
0228 desc.add<double>("minMuonPt", 100);
0229 desc.add<int>("algo", 14);
0230 desc.add<bool>("taggingMode", false);
0231 desc.add<edm::InputTag>("vtx", edm::InputTag("offlinePrimaryVertices"));
0232 desc.add<double>("minMuonTrackRelErr", 2.0);
0233 desc.add<double>("maxDR", 0.001);
0234 desc.add<edm::InputTag>("muons", edm::InputTag("muons"));
0235 desc.add<double>("minPtDiffRel", 0.0);
0236 descriptions.add("BadPFMuonFilter", desc);
0237 descriptions.addWithDefaultLabel(desc);
0238 }
0239
0240
0241 DEFINE_FWK_MODULE(BadParticleFilter);