Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 
0003 // system include files
0004 #include <memory>
0005 #include <iostream>
0006 
0007 // user include files
0008 #include "FWCore/Framework/interface/Frameworkfwd.h"
0009 #include "FWCore/Framework/interface/global/EDFilter.h"
0010 
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/MakerMacros.h"
0013 
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 
0016 #include "DataFormats/MuonReco/interface/Muon.h"
0017 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0018 #include "DataFormats/TrackReco/interface/Track.h"
0019 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0020 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0021 
0022 //
0023 // class declaration
0024 //
0025 
0026 class InconsistentMuonPFCandidateFilter : public edm::global::EDFilter<> {
0027 public:
0028   explicit InconsistentMuonPFCandidateFilter(const edm::ParameterSet&);
0029 
0030 private:
0031   bool filter(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0032 
0033   // ----------member data ---------------------------
0034 
0035   edm::EDGetTokenT<reco::PFCandidateCollection> tokenPFCandidates_;
0036   const double ptMin_;
0037   const double maxPTDiff_;
0038 
0039   const bool taggingMode_, debug_;
0040 };
0041 
0042 //
0043 // constants, enums and typedefs
0044 //
0045 
0046 //
0047 // static data member definitions
0048 //
0049 
0050 //
0051 // constructors and destructor
0052 //
0053 InconsistentMuonPFCandidateFilter::InconsistentMuonPFCandidateFilter(const edm::ParameterSet& iConfig)
0054     : tokenPFCandidates_(consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("PFCandidates"))),
0055       ptMin_(iConfig.getParameter<double>("ptMin")),
0056       maxPTDiff_(iConfig.getParameter<double>("maxPTDiff")),
0057       taggingMode_(iConfig.getParameter<bool>("taggingMode")),
0058       debug_(iConfig.getParameter<bool>("debug")) {
0059   produces<bool>();
0060   produces<reco::PFCandidateCollection>("muons");
0061 }
0062 
0063 //
0064 // member functions
0065 //
0066 
0067 // ------------ method called on each new Event  ------------
0068 bool InconsistentMuonPFCandidateFilter::filter(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0069   using namespace std;
0070   using namespace edm;
0071 
0072   Handle<reco::PFCandidateCollection> pfCandidates;
0073   iEvent.getByToken(tokenPFCandidates_, pfCandidates);
0074 
0075   bool foundMuon = false;
0076 
0077   auto pOutputCandidateCollection = std::make_unique<reco::PFCandidateCollection>();
0078 
0079   for (unsigned i = 0; i < pfCandidates->size(); i++) {
0080     const reco::PFCandidate& cand = (*pfCandidates)[i];
0081 
0082     if (cand.particleId() != reco::PFCandidate::mu)
0083       continue;
0084     if (cand.pt() < ptMin_)
0085       continue;
0086 
0087     const reco::MuonRef muon = cand.muonRef();
0088     if (muon->isTrackerMuon() && muon->isGlobalMuon() &&
0089         fabs(muon->innerTrack()->pt() / muon->globalTrack()->pt() - 1) <= maxPTDiff_)
0090       continue;
0091 
0092     foundMuon = true;
0093 
0094     pOutputCandidateCollection->push_back(cand);
0095 
0096     if (debug_) {
0097       cout << cand << endl;
0098       cout << "\t"
0099            << "tracker pT = ";
0100       if (muon->isTrackerMuon())
0101         cout << muon->innerTrack()->pt();
0102       else
0103         cout << "(n/a)";
0104       cout << endl;
0105       cout << "\t"
0106            << "global fit pT = ";
0107       if (muon->isGlobalMuon())
0108         cout << muon->globalTrack()->pt();
0109       else
0110         cout << "(n/a)";
0111       cout << endl;
0112     }
0113   }  // end loop over PF candidates
0114 
0115   iEvent.put(std::move(pOutputCandidateCollection), "muons");
0116 
0117   bool pass = !foundMuon;
0118 
0119   iEvent.put(std::make_unique<bool>(pass));
0120 
0121   return taggingMode_ || pass;
0122 }
0123 
0124 //define this as a plug-in
0125 DEFINE_FWK_MODULE(InconsistentMuonPFCandidateFilter);