Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:16:16

0001 #include "FWCore/Framework/interface/stream/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/Utilities/interface/InputTag.h"
0005 
0006 #include "DataFormats/PatCandidates/interface/Muon.h"
0007 #include "DataFormats/MuonReco/interface/Muon.h"
0008 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0009 
0010 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0011 #include "DataFormats/Common/interface/getRef.h"
0012 #include "DataFormats/Common/interface/RefToPtr.h"
0013 
0014 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0015 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0016 
0017 namespace pat {
0018   typedef edm::Ptr<pat::PackedCandidate> PackedCandidatePtr;
0019 }
0020 
0021 class PATMuonMerger : public edm::stream::EDProducer<> {
0022 public:
0023   explicit PATMuonMerger(const edm::ParameterSet& iConfig);
0024   ~PATMuonMerger() override {}
0025 
0026   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0027   void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0028 
0029 private:
0030   edm::InputTag muons_;
0031   StringCutObjectSelector<pat::Muon, false> muonsCut_;
0032   edm::InputTag pfCandidate_;
0033   StringCutObjectSelector<pat::PackedCandidate, false> pfCandidateCut_;
0034   edm::InputTag lostTrack_;
0035   StringCutObjectSelector<pat::PackedCandidate, false> lostTrackCut_;
0036 
0037   edm::EDGetTokenT<std::vector<pat::Muon>> muonToken_;
0038   edm::EDGetTokenT<std::vector<pat::PackedCandidate>> pfCandToken_;
0039   edm::EDGetTokenT<std::vector<pat::PackedCandidate>> lostTrackToken_;
0040 };
0041 
0042 PATMuonMerger::PATMuonMerger(const edm::ParameterSet& iConfig)
0043     : muons_(iConfig.getParameter<edm::InputTag>("muons")),
0044       muonsCut_(iConfig.getParameter<std::string>("muonCut")),
0045       pfCandidate_(iConfig.getParameter<edm::InputTag>("pfCandidates")),
0046       pfCandidateCut_(iConfig.getParameter<std::string>("pfCandidatesCut")),
0047       lostTrack_(iConfig.getParameter<edm::InputTag>("otherTracks")),
0048       lostTrackCut_(iConfig.getParameter<std::string>("lostTrackCut")) {
0049   muonToken_ = consumes<std::vector<pat::Muon>>(muons_);
0050   pfCandToken_ = consumes<std::vector<pat::PackedCandidate>>(pfCandidate_);
0051   lostTrackToken_ = consumes<std::vector<pat::PackedCandidate>>(lostTrack_);
0052   produces<std::vector<pat::Muon>>();
0053 }
0054 
0055 void PATMuonMerger::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0056   // mergedMuons
0057   edm::ParameterSetDescription desc;
0058   desc.add<std::string>("muonCut", "");
0059   desc.add<edm::InputTag>("otherTracks", edm::InputTag("lostTracks"));
0060   desc.add<edm::InputTag>("pfCandidates", edm::InputTag("packedPFCandidates"));
0061   desc.add<std::string>("pfCandidatesCut", "");
0062   desc.add<edm::InputTag>("muons", edm::InputTag("slimmedMuons"));
0063   desc.add<std::string>("lostTrackCut", "");
0064   descriptions.add("mergedMuonsNoCuts", desc);
0065 }
0066 
0067 void PATMuonMerger::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0068   edm::Handle<std::vector<pat::Muon>> muons;
0069   edm::Handle<std::vector<pat::PackedCandidate>> pfCands;
0070   edm::Handle<std::vector<pat::PackedCandidate>> lostTracks;
0071 
0072   iEvent.getByToken(muonToken_, muons);
0073   iEvent.getByToken(pfCandToken_, pfCands);
0074   iEvent.getByToken(lostTrackToken_, lostTracks);
0075 
0076   auto out = std::make_unique<std::vector<pat::Muon>>();
0077   out->reserve(muons->size() + pfCands->size() + lostTracks->size());
0078 
0079   // copy all muons
0080   for (auto& muon : *muons) {
0081     if (!muonsCut_(muon))
0082       continue;
0083     out->push_back(muon);
0084   }
0085 
0086   // add other pfCandidates, removing duplicates
0087   for (unsigned int pf = 0; pf < pfCands->size(); ++pf) {
0088     auto pfCand = pfCands->at(pf);
0089     if (!pfCandidateCut_(pfCand))
0090       continue;
0091     reco::CandidatePtr pfCandPtr(pfCands, pf);
0092     bool isPFMuon = false;
0093     for (auto& muon : *muons) {
0094       for (unsigned int i = 0, n = muon.numberOfSourceCandidatePtrs(); i < n; ++i) {
0095         reco::CandidatePtr ptr = muon.sourceCandidatePtr(i);
0096         if (ptr.isNonnull() && ptr == pfCandPtr) {
0097           isPFMuon = true;
0098           break;
0099         }
0100       }
0101       if (isPFMuon)
0102         break;
0103     }
0104     if (isPFMuon) {
0105       continue;
0106     }
0107 
0108     // now make a reco::Muon and recast to pat::Muon
0109     double energy = sqrt(pfCand.p() * pfCand.p() + 0.011163691);
0110     math::XYZTLorentzVector p4(pfCand.px(), pfCand.py(), pfCand.pz(), energy);
0111     reco::Muon mu(pfCand.charge(), p4, pfCand.vertex());
0112     pat::Muon aMu(mu);
0113     out->push_back(aMu);
0114   }
0115 
0116   // adding now lost tracks, removing duplicates
0117   for (auto& lostTrack : *lostTracks) {
0118     if (!lostTrackCut_(lostTrack))
0119       continue;
0120     if (std::abs(lostTrack.pdgId()) == 13)
0121       continue;
0122 
0123     // now make a reco::Muon and recast to pat::Muon
0124     double energy = sqrt(lostTrack.p() * lostTrack.p() + 0.011163691);
0125     math::XYZTLorentzVector p4(lostTrack.px(), lostTrack.py(), lostTrack.pz(), energy);
0126     reco::Muon mu(lostTrack.charge(), p4, lostTrack.vertex());
0127     pat::Muon aMu(mu);
0128     out->push_back(aMu);
0129   }
0130   iEvent.put(std::move(out));
0131 }
0132 
0133 #include "FWCore/Framework/interface/MakerMacros.h"
0134 DEFINE_FWK_MODULE(PATMuonMerger);