Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-25 05:52:15

0001 //
0002 //
0003 
0004 #include "FWCore/Framework/interface/global/EDProducer.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 
0008 #include "DataFormats/PatCandidates/interface/Muon.h"
0009 #include "DataFormats/VertexReco/interface/Vertex.h"
0010 
0011 #include <vector>
0012 #include <memory>
0013 #include <cmath>
0014 
0015 class MuonSelectorVertex : public edm::global::EDProducer<> {
0016 public:
0017   explicit MuonSelectorVertex(const edm::ParameterSet& iConfig);
0018   void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0019 
0020 private:
0021   const edm::EDGetTokenT<std::vector<pat::Muon>> muonSource_;
0022   const edm::EDGetTokenT<std::vector<reco::Vertex>> vertexSource_;
0023   const double maxDZ_;
0024 };
0025 
0026 MuonSelectorVertex::MuonSelectorVertex(const edm::ParameterSet& iConfig)
0027     : muonSource_(consumes<std::vector<pat::Muon>>(iConfig.getParameter<edm::InputTag>("muonSource"))),
0028       vertexSource_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("vertexSource"))),
0029       maxDZ_(iConfig.getParameter<double>("maxDZ")) {
0030   produces<std::vector<pat::Muon>>();
0031 }
0032 
0033 void MuonSelectorVertex::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0034   const auto& muons = iEvent.get(muonSource_);
0035   const auto& vertices = iEvent.get(vertexSource_);
0036 
0037   auto selectedMuons = std::make_unique<std::vector<pat::Muon>>();
0038 
0039   if (!vertices.empty()) {
0040     for (const auto& muon : muons) {
0041       if (std::abs(muon.vertex().z() - vertices.at(0).z()) < maxDZ_) {
0042         selectedMuons->push_back(muon);
0043       }
0044     }
0045   }
0046 
0047   iEvent.put(std::move(selectedMuons));
0048 }
0049 
0050 #include "FWCore/Framework/interface/MakerMacros.h"
0051 DEFINE_FWK_MODULE(MuonSelectorVertex);