File indexing completed on 2024-09-07 04:35:19
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "FWCore/Framework/interface/Frameworkfwd.h"
0010 #include "FWCore/Framework/interface/global/EDProducer.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012
0013 #include "FWCore/Framework/interface/Event.h"
0014
0015 #include "DataFormats/MuonReco/interface/Muon.h"
0016
0017 #include "DataFormats/Common/interface/View.h"
0018 #include "DataFormats/Common/interface/Association.h"
0019 #include <iostream>
0020
0021 class PFMuonUntagger : public edm::global::EDProducer<> {
0022 public:
0023 PFMuonUntagger(const edm::ParameterSet &);
0024 ~PFMuonUntagger() override {}
0025
0026 void produce(edm::StreamID iID, edm::Event &, const edm::EventSetup &) const override;
0027
0028 private:
0029 edm::EDGetTokenT<std::vector<reco::Muon>> muons_;
0030 std::vector<edm::EDGetTokenT<reco::CandidateView>> badmuons_;
0031
0032 template <typename H1, typename H2>
0033 void writeAssociation(
0034 edm::Event &out, const H1 &from, const H2 &to, const std::vector<int> indices, const std::string &name) const {
0035 typedef edm::Association<std::vector<reco::Muon>> AssoMap;
0036 std::unique_ptr<AssoMap> assomap(new AssoMap(to));
0037 typename AssoMap::Filler filler(*assomap);
0038 filler.insert(from, indices.begin(), indices.end());
0039 filler.fill();
0040 out.put(std::move(assomap), name);
0041 }
0042
0043 template <typename H1>
0044 void writeValueMap(edm::Event &out, const H1 &from, const std::vector<int> values, const std::string &name) const {
0045 typedef edm::ValueMap<int> IntMap;
0046 std::unique_ptr<IntMap> intmap(new IntMap());
0047 typename IntMap::Filler filler(*intmap);
0048 filler.insert(from, values.begin(), values.end());
0049 filler.fill();
0050 out.put(std::move(intmap), name);
0051 }
0052 };
0053
0054 PFMuonUntagger::PFMuonUntagger(const edm::ParameterSet &iConfig)
0055 : muons_(consumes<std::vector<reco::Muon>>(iConfig.getParameter<edm::InputTag>("muons"))) {
0056 for (const auto &src : iConfig.getParameter<std::vector<edm::InputTag>>("badmuons")) {
0057 badmuons_.push_back(consumes<reco::CandidateView>(src));
0058 }
0059
0060 produces<std::vector<reco::Muon>>();
0061 produces<edm::ValueMap<int>>("oldPF");
0062 produces<edm::Association<std::vector<reco::Muon>>>("newToOld");
0063 produces<edm::Association<std::vector<reco::Muon>>>("oldToNew");
0064 produces<std::vector<reco::Muon>>("bad");
0065 produces<edm::Association<std::vector<reco::Muon>>>("badToNew");
0066 produces<edm::Association<std::vector<reco::Muon>>>("newToBad");
0067 }
0068
0069 void PFMuonUntagger::produce(edm::StreamID iID, edm::Event &iEvent, const edm::EventSetup &) const {
0070 edm::Handle<std::vector<reco::Muon>> muons;
0071 iEvent.getByToken(muons_, muons);
0072
0073 unsigned int n = muons->size();
0074 std::unique_ptr<std::vector<reco::Muon>> copy(new std::vector<reco::Muon>(*muons));
0075 std::vector<int> oldPF(n);
0076 std::vector<int> dummyIndices(n);
0077 for (unsigned int i = 0; i < n; ++i) {
0078 oldPF[i] = (*copy)[i].isPFMuon();
0079 dummyIndices[i] = i;
0080 }
0081
0082 edm::Handle<reco::CandidateView> badmuons;
0083 for (const auto &tag : badmuons_) {
0084 iEvent.getByToken(tag, badmuons);
0085 for (unsigned int j = 0, nj = badmuons->size(); j < nj; ++j) {
0086 reco::CandidatePtr p = badmuons->ptrAt(j);
0087 if (p.isNonnull() && p.id() == muons.id()) {
0088 reco::Muon &mu = (*copy)[p.key()];
0089 mu.setType(mu.type() & ~reco::Muon::PFMuon);
0090 }
0091 }
0092 }
0093
0094 std::unique_ptr<std::vector<reco::Muon>> bad(new std::vector<reco::Muon>());
0095 std::vector<int> good2bad(n, -1), bad2good;
0096 for (unsigned int i = 0; i < n; ++i) {
0097 const reco::Muon &mu = (*copy)[i];
0098 if (oldPF[i] != mu.isPFMuon()) {
0099 bad->push_back((*muons)[i]);
0100 bad2good.push_back(i);
0101 good2bad[i] = (bad->size() - 1);
0102 }
0103 }
0104
0105
0106 edm::OrphanHandle<std::vector<reco::Muon>> newmu = iEvent.put(std::move(copy));
0107 edm::OrphanHandle<std::vector<reco::Muon>> badmu = iEvent.put(std::move(bad), "bad");
0108
0109
0110 writeAssociation(iEvent, newmu, muons, dummyIndices, "newToOld");
0111 writeAssociation(iEvent, muons, newmu, dummyIndices, "oldToNew");
0112 writeAssociation(iEvent, newmu, badmu, good2bad, "newToBad");
0113 writeAssociation(iEvent, badmu, newmu, bad2good, "badToNew");
0114
0115
0116 writeValueMap(iEvent, newmu, oldPF, "oldPF");
0117 }
0118 #include "FWCore/Framework/interface/MakerMacros.h"
0119 DEFINE_FWK_MODULE(PFMuonUntagger);