File indexing completed on 2024-04-06 12:27:00
0001 #include <string>
0002 #include <vector>
0003
0004 #include "DataFormats/Common/interface/ValueMap.h"
0005 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0006 #include "DataFormats/MuonReco/interface/Muon.h"
0007 #include "DataFormats/TrackReco/interface/Track.h"
0008 #include "RecoMuon/MuonIdentification/interface/MuonCosmicsId.h"
0009
0010 #include "FWCore/Framework/interface/stream/EDProducer.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/EventSetup.h"
0013 #include "FWCore/Framework/interface/Frameworkfwd.h"
0014 #include "FWCore/Framework/interface/MakerMacros.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016
0017 #include "RecoMuon/MuonIdentification/interface/MuonCosmicCompatibilityFiller.h"
0018
0019 class CosmicsMuonIdProducer : public edm::stream::EDProducer<> {
0020 public:
0021 CosmicsMuonIdProducer(const edm::ParameterSet& iConfig)
0022 : inputMuonCollection_(iConfig.getParameter<edm::InputTag>("muonCollection")),
0023 inputTrackCollections_(iConfig.getParameter<std::vector<edm::InputTag>>("trackCollections")) {
0024 edm::ConsumesCollector iC = consumesCollector();
0025 compatibilityFiller_ =
0026 new MuonCosmicCompatibilityFiller(iConfig.getParameter<edm::ParameterSet>("CosmicCompFillerParameters"), iC);
0027
0028 produces<edm::ValueMap<unsigned int>>().setBranchAlias("cosmicsVeto");
0029 produces<edm::ValueMap<reco::MuonCosmicCompatibility>>().setBranchAlias("cosmicCompatibility");
0030
0031 muonToken_ = consumes<reco::MuonCollection>(inputMuonCollection_);
0032 for (unsigned int i = 0; i < inputTrackCollections_.size(); ++i)
0033 trackTokens_.push_back(consumes<reco::TrackCollection>(inputTrackCollections_.at(i)));
0034 }
0035 ~CosmicsMuonIdProducer() override {
0036 if (compatibilityFiller_)
0037 delete compatibilityFiller_;
0038 }
0039
0040 private:
0041 void produce(edm::Event&, const edm::EventSetup&) override;
0042 edm::InputTag inputMuonCollection_;
0043 std::vector<edm::InputTag> inputTrackCollections_;
0044 edm::EDGetTokenT<reco::MuonCollection> muonToken_;
0045 std::vector<edm::EDGetTokenT<reco::TrackCollection>> trackTokens_;
0046
0047 MuonCosmicCompatibilityFiller* compatibilityFiller_;
0048 };
0049
0050 void CosmicsMuonIdProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0051 edm::Handle<reco::MuonCollection> muons;
0052 iEvent.getByToken(muonToken_, muons);
0053
0054 std::vector<unsigned int> values;
0055 values.reserve(muons->size());
0056
0057 std::vector<reco::MuonCosmicCompatibility> compValues;
0058 compValues.reserve(muons->size());
0059
0060 for (reco::MuonCollection::const_iterator muon = muons->begin(); muon != muons->end(); ++muon) {
0061 unsigned int foundPartner(0);
0062 if (muon->innerTrack().isNonnull()) {
0063 for (unsigned int i = 0; i < inputTrackCollections_.size(); ++i) {
0064 edm::Handle<reco::TrackCollection> tracks;
0065 iEvent.getByToken(trackTokens_.at(i), tracks);
0066 if (muonid::findOppositeTrack(tracks, *muon->innerTrack()).isNonnull()) {
0067 foundPartner = i + 1;
0068 break;
0069 }
0070 }
0071 }
0072 values.push_back(foundPartner);
0073
0074 compValues.push_back(compatibilityFiller_->fillCompatibility(*muon, iEvent, iSetup));
0075 }
0076
0077
0078 auto out = std::make_unique<edm::ValueMap<unsigned int>>();
0079 edm::ValueMap<unsigned int>::Filler filler(*out);
0080 filler.insert(muons, values.begin(), values.end());
0081 filler.fill();
0082
0083 auto outC = std::make_unique<edm::ValueMap<reco::MuonCosmicCompatibility>>();
0084 edm::ValueMap<reco::MuonCosmicCompatibility>::Filler fillerC(*outC);
0085 fillerC.insert(muons, compValues.begin(), compValues.end());
0086 fillerC.fill();
0087
0088
0089 iEvent.put(std::move(out));
0090 iEvent.put(std::move(outC));
0091 }
0092 DEFINE_FWK_MODULE(CosmicsMuonIdProducer);