Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-05-31 03:10:53

0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Framework/interface/MakerMacros.h"
0003 #include "FWCore/Framework/interface/stream/EDProducer.h"
0004 #include "FWCore/PluginManager/interface/ModuleDef.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "DataFormats/Common/interface/EDProductfwd.h"
0007 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0008 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h"
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 
0011 class TrackingParticleRefMuonProducer : public edm::stream::EDProducer<> {
0012 public:
0013   TrackingParticleRefMuonProducer(const edm::ParameterSet &iConfig);
0014 
0015   void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
0016 
0017 private:
0018   edm::EDGetTokenT<TrackingParticleCollection> tpToken_;
0019   std::string skim_;
0020   double ptmin_;
0021   double pmin_;
0022 };
0023 
0024 TrackingParticleRefMuonProducer::TrackingParticleRefMuonProducer(const edm::ParameterSet &iConfig)
0025     : tpToken_(consumes<TrackingParticleCollection>(iConfig.getParameter<edm::InputTag>("src"))),
0026       skim_(iConfig.getParameter<std::string>("skim")),
0027       ptmin_(iConfig.getParameter<double>("ptmin")),
0028       pmin_(iConfig.getParameter<double>("pmin")) {
0029   edm::LogVerbatim("TrackingParticleRefMuonProducer")
0030       << "\n constructing TrackingParticleRefMuonProducer: skim = " << skim_;
0031   if (skim_ == "mu")
0032     edm::LogVerbatim("TrackingParticleRefMuonProducer") << "\t ptmin = " << ptmin_ << ", pmin = " << pmin_ << "\n";
0033   else if (skim_ == "track")
0034     edm::LogVerbatim("TrackingParticleRefMuonProducer") << "\t ptmin = " << ptmin_ << "\n";
0035   else if (skim_ == "pf")
0036     edm::LogVerbatim("TrackingParticleRefMuonProducer") << "\t ptmin = " << ptmin_ << ", pmin = " << pmin_ << "\n";
0037   else
0038     edm::LogError("TrackingParticleRefMuonProducer") << "\t undefined skim = " << skim_ << "\n";
0039 
0040   produces<TrackingParticleRefVector>();
0041 }
0042 
0043 void TrackingParticleRefMuonProducer::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0044   edm::Handle<TrackingParticleCollection> tpH;
0045   iEvent.getByToken(tpToken_, tpH);
0046 
0047   auto tpskim = std::make_unique<TrackingParticleRefVector>();
0048 
0049   if (skim_ == "mu") {
0050     for (size_t i = 0, end = tpH->size(); i < end; ++i) {
0051       auto tp = TrackingParticleRef(tpH, i);
0052 
0053       // test if the TP is a muon with pt and p above minimum thresholds
0054       bool isMu = (std::abs(tp->pdgId()) == 13);
0055       bool ptpOk = (tp->pt() > ptmin_) && (tp->p() > pmin_);
0056       if (isMu && ptpOk)
0057         tpskim->push_back(tp);
0058       else {
0059         // test if the TP has muon hits
0060         int n_muon_hits = tp->numberOfHits() - tp->numberOfTrackerHits();
0061         if (n_muon_hits > 0)
0062           tpskim->push_back(tp);
0063       }
0064     }
0065   } else if (skim_ == "track") {
0066     for (size_t i = 0, end = tpH->size(); i < end; ++i) {
0067       auto tp = TrackingParticleRef(tpH, i);
0068 
0069       // test if the pt is above a minimum cut
0070       if (tp->pt() > ptmin_)
0071         tpskim->push_back(tp);
0072     }
0073   } else if (skim_ == "pf") {
0074     for (size_t i = 0, end = tpH->size(); i < end; ++i) {
0075       auto tp = TrackingParticleRef(tpH, i);
0076 
0077       // test if p and pt are above minimum cuts
0078       if ((tp->pt() > ptmin_) && (tp->p() > pmin_))
0079         tpskim->push_back(tp);
0080     }
0081   }
0082 
0083   iEvent.put(std::move(tpskim));
0084 }
0085 
0086 DEFINE_FWK_MODULE(TrackingParticleRefMuonProducer);