Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /**
0002   \class    pat::PATPFParticleProducer PATPFParticleProducer.h "PhysicsTools/PatAlgos/interface/PATPFParticleProducer.h"
0003   \brief    Produces pat::PFParticle's
0004 
0005    The PATPFParticleProducer produces analysis-level pat::PFParticle's starting from
0006    a collection of objects of reco::PFCandidate.
0007 
0008   \author   Steven Lowette, Roger Wolf
0009   \version  $Id: PATPFParticleProducer.h,v 1.8 2012/05/26 10:42:53 gpetrucc Exp $
0010 */
0011 
0012 #include "CommonTools/Utils/interface/PtComparator.h"
0013 #include "DataFormats/Common/interface/Association.h"
0014 #include "DataFormats/Common/interface/View.h"
0015 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0016 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
0017 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0018 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0019 #include "DataFormats/PatCandidates/interface/PFParticle.h"
0020 #include "DataFormats/PatCandidates/interface/UserData.h"
0021 #include "FWCore/Framework/interface/Event.h"
0022 #include "FWCore/Framework/interface/stream/EDProducer.h"
0023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0024 #include "FWCore/ParameterSet/interface/FileInPath.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/Utilities/interface/InputTag.h"
0027 #include "PhysicsTools/PatAlgos/interface/EfficiencyLoader.h"
0028 #include "PhysicsTools/PatAlgos/interface/KinResolutionsLoader.h"
0029 #include "PhysicsTools/PatAlgos/interface/MultiIsolator.h"
0030 #include "PhysicsTools/PatAlgos/interface/PATUserDataHelper.h"
0031 
0032 #include <vector>
0033 #include <memory>
0034 
0035 namespace pat {
0036 
0037   class LeptonLRCalc;
0038 
0039   class PATPFParticleProducer : public edm::stream::EDProducer<> {
0040   public:
0041     explicit PATPFParticleProducer(const edm::ParameterSet& iConfig);
0042     ~PATPFParticleProducer() override;
0043 
0044     void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0045 
0046   private:
0047     // configurables
0048     edm::EDGetTokenT<edm::View<reco::PFCandidate> > pfCandidateToken_;
0049     bool embedPFCandidate_;
0050     bool addGenMatch_;
0051     bool embedGenMatch_;
0052     std::vector<edm::EDGetTokenT<edm::Association<reco::GenParticleCollection> > > genMatchTokens_;
0053     // tools
0054     GreaterByPt<PFParticle> pTComparator_;
0055 
0056     bool addEfficiencies_;
0057     pat::helper::EfficiencyLoader efficiencyLoader_;
0058 
0059     bool addResolutions_;
0060     pat::helper::KinResolutionsLoader resolutionLoader_;
0061 
0062     bool useUserData_;
0063     pat::PATUserDataHelper<pat::PFParticle> userDataHelper_;
0064   };
0065 
0066 }  // namespace pat
0067 
0068 using namespace pat;
0069 
0070 PATPFParticleProducer::PATPFParticleProducer(const edm::ParameterSet& iConfig)
0071     : userDataHelper_(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector()) {
0072   // general configurables
0073   pfCandidateToken_ = consumes<edm::View<reco::PFCandidate> >(iConfig.getParameter<edm::InputTag>("pfCandidateSource"));
0074 
0075   // MC matching configurables
0076   addGenMatch_ = iConfig.getParameter<bool>("addGenMatch");
0077   if (addGenMatch_) {
0078     embedGenMatch_ = iConfig.getParameter<bool>("embedGenMatch");
0079     if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
0080       genMatchTokens_.push_back(consumes<edm::Association<reco::GenParticleCollection> >(
0081           iConfig.getParameter<edm::InputTag>("genParticleMatch")));
0082     } else {
0083       genMatchTokens_ = edm::vector_transform(
0084           iConfig.getParameter<std::vector<edm::InputTag> >("genParticleMatch"),
0085           [this](edm::InputTag const& tag) { return consumes<edm::Association<reco::GenParticleCollection> >(tag); });
0086     }
0087   }
0088 
0089   // Efficiency configurables
0090   addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
0091   if (addEfficiencies_) {
0092     efficiencyLoader_ =
0093         pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"), consumesCollector());
0094   }
0095 
0096   // Resolution configurables
0097   addResolutions_ = iConfig.getParameter<bool>("addResolutions");
0098   if (addResolutions_) {
0099     resolutionLoader_ =
0100         pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"), consumesCollector());
0101   }
0102 
0103   // Check to see if the user wants to add user data
0104   useUserData_ = false;
0105   if (iConfig.exists("userData")) {
0106     useUserData_ = true;
0107   }
0108 
0109   // produces vector of muons
0110   produces<std::vector<PFParticle> >();
0111 }
0112 
0113 PATPFParticleProducer::~PATPFParticleProducer() {}
0114 
0115 void PATPFParticleProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0116   // Get the collection of PFCandidates from the event
0117   edm::Handle<edm::View<reco::PFCandidate> > pfCandidates;
0118   iEvent.getByToken(pfCandidateToken_, pfCandidates);
0119 
0120   // prepare the MC matching
0121   std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchTokens_.size());
0122   if (addGenMatch_) {
0123     for (size_t j = 0, nd = genMatchTokens_.size(); j < nd; ++j) {
0124       iEvent.getByToken(genMatchTokens_[j], genMatches[j]);
0125     }
0126   }
0127 
0128   if (efficiencyLoader_.enabled())
0129     efficiencyLoader_.newEvent(iEvent);
0130   if (resolutionLoader_.enabled())
0131     resolutionLoader_.newEvent(iEvent, iSetup);
0132 
0133   // loop over PFCandidates
0134   std::vector<PFParticle>* patPFParticles = new std::vector<PFParticle>();
0135   for (edm::View<reco::PFCandidate>::const_iterator itPFParticle = pfCandidates->begin();
0136        itPFParticle != pfCandidates->end();
0137        ++itPFParticle) {
0138     // construct the PFParticle from the ref -> save ref to original object
0139     unsigned int idx = itPFParticle - pfCandidates->begin();
0140     edm::RefToBase<reco::PFCandidate> pfCandidatesRef = pfCandidates->refAt(idx);
0141 
0142     PFParticle aPFParticle(pfCandidatesRef);
0143 
0144     if (addGenMatch_) {
0145       for (size_t i = 0, n = genMatches.size(); i < n; ++i) {
0146         reco::GenParticleRef genPFParticle = (*genMatches[i])[pfCandidatesRef];
0147         aPFParticle.addGenParticleRef(genPFParticle);
0148       }
0149       if (embedGenMatch_)
0150         aPFParticle.embedGenParticle();
0151     }
0152 
0153     if (efficiencyLoader_.enabled()) {
0154       efficiencyLoader_.setEfficiencies(aPFParticle, pfCandidatesRef);
0155     }
0156 
0157     if (resolutionLoader_.enabled()) {
0158       resolutionLoader_.setResolutions(aPFParticle);
0159     }
0160 
0161     if (useUserData_) {
0162       userDataHelper_.add(aPFParticle, iEvent, iSetup);
0163     }
0164 
0165     // add sel to selected
0166     patPFParticles->push_back(aPFParticle);
0167   }
0168 
0169   // sort pfCandidates in pt
0170   std::sort(patPFParticles->begin(), patPFParticles->end(), pTComparator_);
0171 
0172   // put genEvt object in Event
0173   std::unique_ptr<std::vector<PFParticle> > ptr(patPFParticles);
0174   iEvent.put(std::move(ptr));
0175 }
0176 
0177 #include "FWCore/Framework/interface/MakerMacros.h"
0178 DEFINE_FWK_MODULE(PATPFParticleProducer);