Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:47

0001 #include "FWCore/Framework/interface/stream/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "DataFormats/Candidate/interface/Candidate.h"
0006 #include "DataFormats/PatCandidates/interface/IsolatedTrack.h"
0007 #include "DataFormats/Common/interface/RefToPtr.h"
0008 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0009 
0010 #include <vector>
0011 
0012 class IsolatedTrackCleaner : public edm::stream::EDProducer<> {
0013 public:
0014   IsolatedTrackCleaner(edm::ParameterSet const& params)
0015       : tracks_(consumes<std::vector<pat::IsolatedTrack>>(params.getParameter<edm::InputTag>("tracks"))),
0016         cut_(params.getParameter<std::string>("cut")) {
0017     for (const edm::InputTag& tag : params.getParameter<std::vector<edm::InputTag>>("finalLeptons")) {
0018       leptons_.push_back(consumes<reco::CandidateView>(tag));
0019     }
0020     produces<std::vector<pat::IsolatedTrack>>();
0021   }
0022 
0023   ~IsolatedTrackCleaner() override {}
0024 
0025   void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
0026     auto out = std::make_unique<std::vector<pat::IsolatedTrack>>();
0027 
0028     std::vector<reco::CandidatePtr> leptonPfCands;
0029     edm::Handle<reco::CandidateView> leptons;
0030     for (const auto& token : leptons_) {
0031       iEvent.getByToken(token, leptons);
0032       for (const auto& lep : *leptons) {
0033         for (unsigned int i = 0, n = lep.numberOfSourceCandidatePtrs(); i < n; ++i) {
0034           auto ptr = lep.sourceCandidatePtr(i);
0035           if (ptr.isNonnull())
0036             leptonPfCands.push_back(ptr);
0037         }
0038       }
0039     }
0040     std::sort(leptonPfCands.begin(), leptonPfCands.end());
0041 
0042     edm::Handle<std::vector<pat::IsolatedTrack>> tracks;
0043     iEvent.getByToken(tracks_, tracks);
0044     for (const auto& track : *tracks) {
0045       if (!cut_(track))
0046         continue;
0047       if (track.packedCandRef().isNonnull()) {
0048         reco::CandidatePtr pfCand(edm::refToPtr(track.packedCandRef()));
0049         if (std::binary_search(leptonPfCands.begin(), leptonPfCands.end(), pfCand)) {
0050           continue;
0051         }
0052       }
0053       out->push_back(track);
0054     }
0055 
0056     iEvent.put(std::move(out));
0057   }
0058 
0059 protected:
0060   edm::EDGetTokenT<std::vector<pat::IsolatedTrack>> tracks_;
0061   StringCutObjectSelector<pat::IsolatedTrack> cut_;
0062   std::vector<edm::EDGetTokenT<reco::CandidateView>> leptons_;
0063 };
0064 
0065 #include "FWCore/Framework/interface/MakerMacros.h"
0066 DEFINE_FWK_MODULE(IsolatedTrackCleaner);