Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:33:59

0001 #include "FWCore/Framework/interface/global/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "FWCore/Utilities/interface/InputTag.h"
0006 
0007 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0008 
0009 #include "DataFormats/MuonReco/interface/Muon.h"
0010 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0011 
0012 #include "DataFormats/VertexReco/interface/Vertex.h"
0013 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0014 
0015 #include <memory>
0016 #include <numeric>
0017 #include <vector>
0018 
0019 ////////////////////////////////////////////////////////////////////////////////
0020 // class definition
0021 ////////////////////////////////////////////////////////////////////////////////
0022 class MuonFromPVSelector : public edm::global::EDProducer<> {
0023 public:
0024   explicit MuonFromPVSelector(edm::ParameterSet const&);
0025   void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0026 
0027 private:
0028   double max_dxy_;
0029   double max_dz_;
0030   edm::EDGetTokenT<std::vector<reco::Vertex>> v_recoVertexToken_;
0031   edm::EDGetTokenT<std::vector<reco::Muon>> v_recoMuonToken_;
0032 };
0033 
0034 ////////////////////////////////////////////////////////////////////////////////
0035 // construction
0036 ////////////////////////////////////////////////////////////////////////////////
0037 
0038 MuonFromPVSelector::MuonFromPVSelector(edm::ParameterSet const& iConfig)
0039     : max_dxy_{iConfig.getParameter<double>("max_dxy")},
0040       max_dz_{iConfig.getParameter<double>("max_dz")},
0041       v_recoVertexToken_{consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("srcVertex"))},
0042       v_recoMuonToken_{consumes<std::vector<reco::Muon>>(iConfig.getParameter<edm::InputTag>("srcMuon"))} {
0043   produces<std::vector<reco::Muon>>();
0044 }
0045 
0046 ////////////////////////////////////////////////////////////////////////////////
0047 // implementation of member functions
0048 ////////////////////////////////////////////////////////////////////////////////
0049 
0050 void MuonFromPVSelector::produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const {
0051   auto goodMuons = std::make_unique<std::vector<reco::Muon>>();
0052 
0053   edm::Handle<std::vector<reco::Vertex>> vertices;
0054   iEvent.getByToken(v_recoVertexToken_, vertices);
0055 
0056   edm::Handle<std::vector<reco::Muon>> muons;
0057   iEvent.getByToken(v_recoMuonToken_, muons);
0058 
0059   if (!vertices->empty()) {
0060     auto const& pv = vertices->front();
0061     std::copy_if(std::cbegin(*muons), std::cend(*muons), std::back_inserter(*goodMuons), [&pv, this](auto const& muon) {
0062       return muon.innerTrack().isNonnull() && std::abs(muon.innerTrack()->dxy(pv.position())) < max_dxy_ &&
0063              std::abs(muon.innerTrack()->dz(pv.position())) < max_dz_;
0064     });
0065   }
0066 
0067   iEvent.put(std::move(goodMuons));
0068 }
0069 
0070 DEFINE_FWK_MODULE(MuonFromPVSelector);