Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:51

0001 #include <memory>
0002 
0003 #include "CommonTools/UtilAlgos/interface/StringCutObjectSelector.h"
0004 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
0005 #include "DataFormats/PatCandidates/interface/Muon.h"
0006 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/stream/EDProducer.h"
0009 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012 
0013 namespace pat {
0014 
0015   class PackedCandidateMuonSelectorProducer : public edm::stream::EDProducer<> {
0016   public:
0017     explicit PackedCandidateMuonSelectorProducer(const edm::ParameterSet& iConfig)
0018         : muonToken_(consumes<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
0019           candidateToken_(consumes<pat::PackedCandidateCollection>(iConfig.getParameter<edm::InputTag>("candidates"))),
0020           candidate2PFToken_(consumes<edm::Association<reco::PFCandidateCollection>>(
0021               iConfig.getParameter<edm::InputTag>("candidates"))),
0022           track2LostTrackToken_(consumes<edm::Association<pat::PackedCandidateCollection>>(
0023               iConfig.getParameter<edm::InputTag>("lostTracks"))),
0024           muonSelectors_(iConfig.getParameter<std::vector<std::string>>("muonSelectors")),
0025           muonIDs_(iConfig.getParameter<std::vector<std::string>>("muonIDs")) {
0026       for (const auto& sel : muonSelectors_) {
0027         produces<pat::PackedCandidateRefVector>("lostTracks" + sel);
0028         produces<pat::PackedCandidateRefVector>("pfCandidates" + sel);
0029       }
0030       for (const auto& sel : muonIDs_) {
0031         muonIDMap_[sel] = std::make_unique<StringCutObjectSelector<reco::Muon>>("passed('" + sel + "')");
0032         produces<pat::PackedCandidateRefVector>("lostTracks" + sel);
0033         produces<pat::PackedCandidateRefVector>("pfCandidates" + sel);
0034       }
0035     }
0036     ~PackedCandidateMuonSelectorProducer() override = default;
0037 
0038     void produce(edm::Event&, const edm::EventSetup&) override;
0039 
0040     static void fillDescriptions(edm::ConfigurationDescriptions&);
0041 
0042   private:
0043     const edm::EDGetTokenT<reco::MuonCollection> muonToken_;
0044     const edm::EDGetTokenT<pat::PackedCandidateCollection> candidateToken_;
0045     const edm::EDGetTokenT<pat::PackedCandidateCollection> lostTrackToken_;
0046     const edm::EDGetTokenT<edm::Association<reco::PFCandidateCollection>> candidate2PFToken_;
0047     const edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>> track2LostTrackToken_;
0048     const std::vector<std::string> muonSelectors_;
0049     const std::vector<std::string> muonIDs_;
0050     std::map<std::string, std::unique_ptr<StringCutObjectSelector<reco::Muon>>> muonIDMap_;
0051   };
0052 
0053 }  // namespace pat
0054 
0055 void pat::PackedCandidateMuonSelectorProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0056   const auto& muons = iEvent.get(muonToken_);
0057   const auto& candidates = iEvent.getHandle(candidateToken_);
0058   const auto& candidate2PF = iEvent.get(candidate2PFToken_);
0059   const auto& track2LostTrack = iEvent.get(track2LostTrackToken_);
0060 
0061   std::map<std::string, std::unique_ptr<pat::PackedCandidateRefVector>> lostTrackMap, candMap;
0062   for (const auto& sel : muonSelectors_) {
0063     lostTrackMap.emplace(sel, new pat::PackedCandidateRefVector());
0064     candMap.emplace(sel, new pat::PackedCandidateRefVector());
0065   }
0066   for (const auto& sel : muonIDs_) {
0067     lostTrackMap.emplace(sel, new pat::PackedCandidateRefVector());
0068     candMap.emplace(sel, new pat::PackedCandidateRefVector());
0069   }
0070 
0071   // loop over muons
0072   for (const auto& muon : muons) {
0073     const auto& muonTrack = muon.innerTrack();
0074     // ignore muons without high purity inner track
0075     if (muonTrack.isNull() || !muonTrack->quality(reco::TrackBase::qualityByName("highPurity")))
0076       continue;
0077 
0078     // find lost track associated to muon
0079     const auto& lostTrack = track2LostTrack[muonTrack];
0080     if (lostTrack.isNonnull()) {
0081       for (const auto& sel : muonSelectors_) {
0082         if (muon::isGoodMuon(muon, muon::selectionTypeFromString(sel)))
0083           lostTrackMap[sel]->push_back(lostTrack);
0084       }
0085       for (const auto& sel : muonIDs_) {
0086         if ((*muonIDMap_.at(sel))(muon))
0087           lostTrackMap[sel]->push_back(lostTrack);
0088       }
0089       continue;
0090     }
0091 
0092     // find PF candidate associated to muon
0093     for (size_t i = 0; i < candidates->size(); i++) {
0094       const auto& cand = pat::PackedCandidateRef(candidates, i);
0095       const auto& candTrack = candidate2PF[cand]->trackRef();
0096       // check if candidate and muon are compatible
0097       if (candTrack.isNonnull() && muonTrack == candTrack) {
0098         for (const auto& sel : muonSelectors_) {
0099           if (muon::isGoodMuon(muon, muon::selectionTypeFromString(sel)))
0100             candMap[sel]->push_back(cand);
0101         }
0102         for (const auto& sel : muonIDs_) {
0103           if ((*muonIDMap_.at(sel))(muon))
0104             candMap[sel]->push_back(cand);
0105         }
0106         break;
0107       }
0108     }
0109   }
0110 
0111   for (auto& s : lostTrackMap) {
0112     iEvent.put(std::move(s.second), "lostTracks" + s.first);
0113   }
0114   for (auto& s : candMap) {
0115     iEvent.put(std::move(s.second), "pfCandidates" + s.first);
0116   }
0117 }
0118 
0119 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0120 void pat::PackedCandidateMuonSelectorProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0121   edm::ParameterSetDescription desc;
0122   desc.add<edm::InputTag>("muons", edm::InputTag("muons"))->setComment("muon input collection");
0123   desc.add<edm::InputTag>("candidates", edm::InputTag("packedPFCandidates"))
0124       ->setComment("packed PF candidate input collection");
0125   desc.add<edm::InputTag>("lostTracks", edm::InputTag("lostTracks"))->setComment("lost track input collection");
0126   desc.add<std::vector<std::string>>("muonSelectors", {"AllTrackerMuons", "TMOneStationTight"})
0127       ->setComment("muon selectors");
0128   desc.add<std::vector<std::string>>("muonIDs", {})->setComment("muon IDs");
0129   descriptions.add("packedCandidateMuonID", desc);
0130 }
0131 
0132 #include "FWCore/Framework/interface/MakerMacros.h"
0133 using namespace pat;
0134 DEFINE_FWK_MODULE(PackedCandidateMuonSelectorProducer);