Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-05 03:15:09

0001 #include "FWCore/Framework/interface/Frameworkfwd.h"
0002 #include "FWCore/Framework/interface/stream/EDProducer.h"
0003 
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/Utilities/interface/StreamID.h"
0009 
0010 #include "DataFormats/PatCandidates/interface/Jet.h"
0011 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0012 #include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
0013 
0014 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0015 
0016 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0017 #include "DataFormats/NanoAOD/interface/FlatTable.h"
0018 
0019 template <typename T, typename C = std::vector<typename T::ConstituentTypeFwdPtr>>
0020 class SimpleJetConstituentTableProducer : public edm::stream::EDProducer<> {
0021 public:
0022   using ConstituentsOutput = C;
0023   using ConstituentValueType = typename C::value_type;
0024 
0025   explicit SimpleJetConstituentTableProducer(const edm::ParameterSet &);
0026   ~SimpleJetConstituentTableProducer() override;
0027 
0028   ConstituentValueType const initptr(edm::Ptr<reco::Candidate> const &) const;
0029   static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0030 
0031 private:
0032   void produce(edm::Event &, const edm::EventSetup &) override;
0033 
0034   const std::string name_;
0035   const std::string candIdxName_;
0036   const std::string candIdxDoc_;
0037 
0038   edm::EDGetTokenT<edm::View<T>> jet_token_;
0039   edm::EDGetTokenT<reco::CandidateView> cand_token_;
0040 
0041   const StringCutObjectSelector<T> jetCut_;
0042   const StringCutObjectSelector<ConstituentValueType> jetConstCut_;
0043 };
0044 
0045 //
0046 // constructors and destructor
0047 //
0048 template <typename T, typename C>
0049 SimpleJetConstituentTableProducer<T, C>::SimpleJetConstituentTableProducer(const edm::ParameterSet &iConfig)
0050     : name_(iConfig.getParameter<std::string>("name")),
0051       candIdxName_(iConfig.getParameter<std::string>("candIdxName")),
0052       candIdxDoc_(iConfig.getParameter<std::string>("candIdxDoc")),
0053       jet_token_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("jets"))),
0054       cand_token_(consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("candidates"))),
0055       jetCut_(iConfig.getParameter<std::string>("jetCut")),
0056       jetConstCut_(iConfig.getParameter<std::string>("jetConstCut")) {
0057   produces<nanoaod::FlatTable>(name_);
0058   produces<ConstituentsOutput>();
0059 }
0060 
0061 template <typename T, typename C>
0062 SimpleJetConstituentTableProducer<T, C>::~SimpleJetConstituentTableProducer() {}
0063 
0064 template <typename T, typename C>
0065 void SimpleJetConstituentTableProducer<T, C>::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0066   // elements in all these collections must have the same order!
0067   auto outCands = std::make_unique<ConstituentsOutput>();
0068 
0069   auto jets = iEvent.getHandle(jet_token_);
0070 
0071   edm::Handle<reco::CandidateView> cands_;
0072   iEvent.getByToken(cand_token_, cands_);
0073   auto candPtrs = cands_->ptrs();
0074 
0075   // First, select jets
0076   std::vector<T> jetsPassCut;
0077   for (unsigned jetIdx = 0; jetIdx < jets->size(); ++jetIdx) {
0078     const auto &jet = jets->at(jetIdx);
0079     if (!jetCut_(jet))
0080       continue;
0081     jetsPassCut.push_back(jets->at(jetIdx));
0082   }
0083 
0084   // Then loop over selected jets
0085   std::vector<int> parentJetIdx;
0086   std::vector<int> candIdx;
0087   for (unsigned jetIdx = 0; jetIdx < jetsPassCut.size(); ++jetIdx) {
0088     const auto &jet = jetsPassCut.at(jetIdx);
0089 
0090     // Loop over jet constituents
0091     std::vector<reco::CandidatePtr> const &daughters = jet.daughterPtrVector();
0092     for (const auto &dauPtr : daughters) {
0093       // Apply cut on jet constituent
0094       typename C::value_type cand = initptr(dauPtr);
0095       if (!jetConstCut_(cand))
0096         continue;
0097 
0098       // Find jet constituent in candidate collection
0099       auto candInNewList = std::find(candPtrs.begin(), candPtrs.end(), dauPtr);
0100       if (candInNewList == candPtrs.end()) {
0101         continue;
0102       }
0103 
0104       outCands->push_back(cand);
0105       parentJetIdx.push_back(jetIdx);
0106       candIdx.push_back(candInNewList - candPtrs.begin());
0107     }
0108   }  // end jet loop
0109 
0110   auto candTable = std::make_unique<nanoaod::FlatTable>(outCands->size(), name_, false);
0111   // We fill from here only stuff that cannot be created with the SimpleFlatTableProducer
0112   candTable->template addColumn<int>(candIdxName_, candIdx, candIdxDoc_);
0113 
0114   std::string parentJetIdxName("jetIdx");
0115   std::string parentJetIdxDoc("Index of the parent jet");
0116   if constexpr (std::is_same<T, reco::GenJet>::value) {
0117     parentJetIdxName = "genJetIdx";
0118     parentJetIdxDoc = "Index of the parent gen jet";
0119   }
0120   candTable->template addColumn<int>(parentJetIdxName, parentJetIdx, parentJetIdxDoc);
0121 
0122   iEvent.put(std::move(candTable), name_);
0123   iEvent.put(std::move(outCands));
0124 }
0125 
0126 template <>
0127 edm::Ptr<pat::PackedCandidate> const
0128 SimpleJetConstituentTableProducer<pat::Jet, std::vector<edm::Ptr<pat::PackedCandidate>>>::initptr(
0129     edm::Ptr<reco::Candidate> const &dau) const {
0130   edm::Ptr<pat::PackedCandidate> retval(dau);
0131   return retval;
0132 }
0133 
0134 template <>
0135 edm::Ptr<pat::PackedGenParticle> const
0136 SimpleJetConstituentTableProducer<reco::GenJet, std::vector<edm::Ptr<pat::PackedGenParticle>>>::initptr(
0137     edm::Ptr<reco::Candidate> const &dau) const {
0138   edm::Ptr<pat::PackedGenParticle> retval(dau);
0139   return retval;
0140 }
0141 
0142 template <typename T, typename C>
0143 void SimpleJetConstituentTableProducer<T, C>::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0144   edm::ParameterSetDescription desc;
0145   desc.add<std::string>("name", "FatJetPFCand");
0146   desc.add<std::string>("candIdxName", "PFCandIdx");
0147   desc.add<std::string>("candIdxDoc", "Index in PFCand table");
0148   desc.add<edm::InputTag>("jets", edm::InputTag("finalJetsAK8"));
0149   desc.add<edm::InputTag>("candidates", edm::InputTag("packedPFCandidates"));
0150   desc.add<std::string>("jetCut", "");
0151   desc.add<std::string>("jetConstCut", "");
0152   descriptions.addWithDefaultLabel(desc);
0153 }
0154 
0155 typedef SimpleJetConstituentTableProducer<pat::Jet, std::vector<edm::Ptr<pat::PackedCandidate>>>
0156     SimplePatJetConstituentTableProducer;
0157 typedef SimpleJetConstituentTableProducer<reco::GenJet, std::vector<edm::Ptr<pat::PackedGenParticle>>>
0158     SimpleGenJetConstituentTableProducer;
0159 
0160 DEFINE_FWK_MODULE(SimplePatJetConstituentTableProducer);
0161 DEFINE_FWK_MODULE(SimpleGenJetConstituentTableProducer);