Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:26:59

0001 #include "PhysicsTools/SelectorUtils/interface/CutApplicatorBase.h"
0002 #include "PhysicsTools/SelectorUtils/interface/CutApplicatorWithEventContentBase.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "DataFormats/MuonReco/interface/Muon.h"
0005 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
0006 
0007 class MuonDzCut : public CutApplicatorWithEventContentBase {
0008 public:
0009   MuonDzCut(const edm::ParameterSet& c);
0010 
0011   result_type operator()(const reco::MuonPtr&) const final;
0012   CandidateType candidateType() const final { return MUON; }
0013   double value(const reco::CandidatePtr&) const final;
0014 
0015   void setConsumes(edm::ConsumesCollector&) final;
0016   void getEventContent(const edm::EventBase&) final;
0017 
0018 private:
0019   edm::Handle<reco::VertexCollection> vtxs_;
0020   const double maxDz_;
0021   enum BestTrackType { INNERTRACK, MUONBESTTRACK, NONE } trackType_;
0022 };
0023 DEFINE_EDM_PLUGIN(CutApplicatorFactory, MuonDzCut, "MuonDzCut");
0024 
0025 // Define constructors and initialization routines
0026 MuonDzCut::MuonDzCut(const edm::ParameterSet& c)
0027     : CutApplicatorWithEventContentBase(c), maxDz_(c.getParameter<double>("maxDz")) {
0028   const std::string trackTypeName = c.getParameter<std::string>("trackType");
0029   trackType_ = NONE;
0030   if (trackTypeName == "muonBestTrack")
0031     trackType_ = MUONBESTTRACK;
0032   else if (trackTypeName == "innerTrack")
0033     trackType_ = INNERTRACK;
0034   else {
0035     edm::LogError("MuonDzCut") << "Wrong cut id name, " << trackTypeName
0036                                << "Choose among \"muonBestTrack\", \"innerTrack\"";
0037     trackType_ = NONE;
0038   }
0039 
0040   contentTags_.emplace("vertices", c.getParameter<edm::InputTag>("vertexSrc"));
0041   contentTags_.emplace("verticesMiniAOD", c.getParameter<edm::InputTag>("vertexSrcMiniAOD"));
0042 }
0043 
0044 void MuonDzCut::setConsumes(edm::ConsumesCollector& cc) {
0045   contentTokens_.emplace("vertices", cc.consumes<reco::VertexCollection>(contentTags_["vertices"]));
0046   contentTokens_.emplace("verticesMiniAOD", cc.consumes<reco::VertexCollection>(contentTags_["verticesMiniAOD"]));
0047 }
0048 
0049 void MuonDzCut::getEventContent(const edm::EventBase& ev) {
0050   ev.getByLabel(contentTags_["vertices"], vtxs_);
0051   if (!vtxs_.isValid())
0052     ev.getByLabel(contentTags_["verticesMiniAOD"], vtxs_);
0053 }
0054 
0055 // Functors for evaluation
0056 CutApplicatorBase::result_type MuonDzCut::operator()(const reco::MuonPtr& cand) const {
0057   const auto& vtxPos = vtxs_->at(0).position();
0058 
0059   reco::TrackRef trackRef;
0060   if (trackType_ == INNERTRACK)
0061     trackRef = cand->innerTrack();
0062   else if (trackType_ == MUONBESTTRACK)
0063     trackRef = cand->muonBestTrack();
0064 
0065   return trackRef.isNonnull() and std::abs(trackRef->dz(vtxPos)) <= maxDz_;
0066 }
0067 
0068 double MuonDzCut::value(const reco::CandidatePtr& cand) const {
0069   const reco::MuonPtr muon(cand);
0070   reco::TrackRef trackRef;
0071   if (trackType_ == INNERTRACK)
0072     trackRef = muon->innerTrack();
0073   else if (trackType_ == MUONBESTTRACK)
0074     trackRef = muon->muonBestTrack();
0075   if (trackRef.isNull())
0076     return -1;
0077 
0078   const auto& vtxPos = vtxs_->at(0).position();
0079   return std::abs(trackRef->dz(vtxPos));
0080 }