Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-25 23:39:48

0001 /* \class PFJetSelector
0002  *
0003  * Selects jets with a configurable string-based cut,
0004  * and also writes out the constituents of the jet
0005  * into a separate collection.
0006  *
0007  * \author: Sal Rappoccio
0008  *
0009  *
0010  * for more details about the cut syntax, see the documentation
0011  * page below:
0012  *
0013  *   https://twiki.cern.ch/twiki/bin/view/CMS/SWGuidePhysicsCutParser
0014  *
0015  */
0016 
0017 #include "CommonTools/UtilAlgos/interface/StringCutObjectSelector.h"
0018 #include "DataFormats/JetReco/interface/Jet.h"
0019 #include "DataFormats/JetReco/interface/PFJet.h"
0020 #include "DataFormats/JetReco/interface/GenJet.h"
0021 #include "DataFormats/PatCandidates/interface/Jet.h"
0022 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0023 #include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
0024 #include "DataFormats/JetReco/interface/CaloJet.h"
0025 #include "FWCore/Framework/interface/stream/EDProducer.h"
0026 #include "FWCore/Framework/interface/MakerMacros.h"
0027 #include "FWCore/Framework/interface/Event.h"
0028 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0029 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0030 
0031 template <class T, typename C = std::vector<typename T::ConstituentTypeFwdPtr>>
0032 class JetConstituentSelector : public edm::stream::EDProducer<> {
0033 public:
0034   using JetsOutput = std::vector<T>;
0035   using ConstituentsOutput = C;
0036   using ValueType = typename C::value_type;
0037 
0038   JetConstituentSelector(edm::ParameterSet const& params)
0039       : srcToken_{consumes<edm::View<T>>(params.getParameter<edm::InputTag>("src"))},
0040         selector_{params.getParameter<std::string>("cut")} {
0041     produces<JetsOutput>();
0042     produces<ConstituentsOutput>("constituents");
0043   }
0044 
0045   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0046     edm::ParameterSetDescription desc;
0047     desc.add<edm::InputTag>("src", edm::InputTag(""))->setComment("InputTag used for retrieving jets in event.");
0048     desc.add<std::string>("cut", "")->setComment(
0049         "Cut used by which to select jets. For example:\n  \"pt > 100.0 && abs(rapidity()) < 2.4\".");
0050     descriptions.addWithDefaultLabel(desc);
0051   }
0052 
0053   // Default initialization is for edm::FwdPtr. Specialization (below) is for edm::Ptr.
0054   typename ConstituentsOutput::value_type const initptr(edm::Ptr<reco::Candidate> const& dau) const {
0055     return typename ConstituentsOutput::value_type(dau, dau);
0056   }
0057 
0058   void produce(edm::Event& iEvent, edm::EventSetup const& iSetup) override {
0059     auto jets = std::make_unique<JetsOutput>();
0060     auto candsOut = std::make_unique<ConstituentsOutput>();
0061 
0062     edm::Handle<edm::View<T>> h_jets;
0063     iEvent.getByToken(srcToken_, h_jets);
0064 
0065     // Now set the Ptrs with the orphan handles.
0066     for (auto const& jet : *h_jets) {
0067       // Check the selection
0068       if (selector_(jet)) {
0069         // Add the jets that pass to the output collection
0070         jets->push_back(jet);
0071 
0072         for (unsigned int ida{}; ida < jet.numberOfDaughters(); ++ida) {
0073           candsOut->emplace_back(initptr(jet.daughterPtr(ida)));
0074         }
0075       }
0076     }
0077 
0078     iEvent.put(std::move(jets));
0079     iEvent.put(std::move(candsOut), "constituents");
0080   }
0081 
0082 private:
0083   edm::EDGetTokenT<edm::View<T>> const srcToken_;
0084   StringCutObjectSelector<T> const selector_;
0085 };
0086 
0087 template <>
0088 edm::Ptr<pat::PackedCandidate> const
0089 JetConstituentSelector<pat::Jet, std::vector<edm::Ptr<pat::PackedCandidate>>>::initptr(
0090     edm::Ptr<reco::Candidate> const& dau) const {
0091   edm::Ptr<pat::PackedCandidate> retval(dau);
0092   return retval;
0093 }
0094 
0095 template <>
0096 edm::Ptr<pat::PackedGenParticle> const
0097 JetConstituentSelector<reco::GenJet, std::vector<edm::Ptr<pat::PackedGenParticle>>>::initptr(
0098     edm::Ptr<reco::Candidate> const& dau) const {
0099   edm::Ptr<pat::PackedGenParticle> retval(dau);
0100   return retval;
0101 }
0102 
0103 using PFJetConstituentSelector = JetConstituentSelector<reco::PFJet>;
0104 using GenJetConstituentSelector = JetConstituentSelector<reco::GenJet, std::vector<edm::FwdPtr<reco::GenParticle>>>;
0105 using GenJetPackedConstituentSelector =
0106     JetConstituentSelector<reco::GenJet, std::vector<edm::FwdPtr<pat::PackedGenParticle>>>;
0107 using GenJetPackedConstituentPtrSelector =
0108     JetConstituentSelector<reco::GenJet, std::vector<edm::Ptr<pat::PackedGenParticle>>>;
0109 using PatJetConstituentSelector = JetConstituentSelector<pat::Jet, std::vector<edm::FwdPtr<pat::PackedCandidate>>>;
0110 using PatJetConstituentPtrSelector = JetConstituentSelector<pat::Jet, std::vector<edm::Ptr<pat::PackedCandidate>>>;
0111 using MiniAODJetConstituentSelector =
0112     JetConstituentSelector<reco::PFJet, std::vector<edm::FwdPtr<pat::PackedCandidate>>>;
0113 using MiniAODJetConstituentPtrSelector =
0114     JetConstituentSelector<reco::PFJet, std::vector<edm::Ptr<pat::PackedCandidate>>>;
0115 
0116 DEFINE_FWK_MODULE(PFJetConstituentSelector);
0117 DEFINE_FWK_MODULE(GenJetConstituentSelector);
0118 DEFINE_FWK_MODULE(GenJetPackedConstituentPtrSelector);
0119 DEFINE_FWK_MODULE(PatJetConstituentSelector);
0120 DEFINE_FWK_MODULE(PatJetConstituentPtrSelector);
0121 DEFINE_FWK_MODULE(MiniAODJetConstituentSelector);