Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-26 01:51:27

0001 #include "FWCore/Framework/interface/global/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0006 #include "DataFormats/NanoAOD/interface/FlatTable.h"
0007 #include "DataFormats/Common/interface/View.h"
0008 #include "DataFormats/Candidate/interface/Candidate.h"
0009 #include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
0010 #include <DataFormats/Math/interface/deltaR.h>
0011 #include "DataFormats/JetReco/interface/GenJetCollection.h"
0012 
0013 #include <vector>
0014 #include <iostream>
0015 
0016 class PackedCandMCMatchTableProducer : public edm::global::EDProducer<> {
0017 public:
0018   PackedCandMCMatchTableProducer(edm::ParameterSet const& params)
0019       : objName_(params.getParameter<std::string>("objName")),
0020         branchName_(params.getParameter<std::string>("branchName")),
0021         doc_(params.getParameter<std::string>("docString")),
0022         src_(consumes<reco::CandidateView>(params.getParameter<edm::InputTag>("src"))),
0023         genPartsToken_(
0024             consumes<edm::View<pat::PackedGenParticle>>(params.getParameter<edm::InputTag>("genparticles"))) {
0025     produces<nanoaod::FlatTable>();
0026   }
0027 
0028   ~PackedCandMCMatchTableProducer() override {}
0029 
0030   void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
0031     edm::Handle<reco::CandidateView> cands;
0032     iEvent.getByToken(src_, cands);
0033     unsigned int ncand = cands->size();
0034 
0035     auto tab = std::make_unique<nanoaod::FlatTable>(ncand, objName_, false, true);
0036 
0037     edm::Handle<edm::View<pat::PackedGenParticle>> genParts;
0038     iEvent.getByToken(genPartsToken_, genParts);
0039 
0040     std::vector<int> key(ncand, -1), flav(ncand, 0);
0041     for (unsigned int i = 0; i < ncand; ++i) {
0042       auto cand = cands->ptrAt(i);
0043 
0044       auto iter = std::find_if(genParts->begin(), genParts->end(), [cand](pat::PackedGenParticle genp) {
0045         return (genp.charge() == cand->charge()) && (deltaR(genp.eta(), genp.phi(), cand->eta(), cand->phi()) < 0.02) &&
0046                (abs(genp.pt() - cand->pt()) / cand->pt() < 0.2);
0047       });
0048       if (iter != genParts->end()) {
0049         key[i] = iter - genParts->begin();
0050       }
0051     }
0052     tab->addColumn<int>(branchName_ + "Idx", key, "Index into GenCands list for " + doc_);
0053     iEvent.put(std::move(tab));
0054   }
0055 
0056   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0057     edm::ParameterSetDescription desc;
0058     desc.add<std::string>("objName")->setComment("name of the nanoaod::FlatTable to extend with this table");
0059     desc.add<std::string>("branchName")
0060         ->setComment(
0061             "name of the column to write (the final branch in the nanoaod will be <objName>_<branchName>Idx and "
0062             "<objName>_<branchName>Flav");
0063     desc.add<std::string>("docString")->setComment("documentation to forward to the output");
0064     desc.add<edm::InputTag>("src")->setComment(
0065         "physics object collection for the reconstructed objects (e.g. leptons)");
0066     desc.addOptional<edm::InputTag>("genparticles")->setComment("Collection of genParticles to be stored.");
0067     descriptions.add("packedCandMcMatchTable", desc);
0068   }
0069 
0070 protected:
0071   const std::string objName_, branchName_, doc_;
0072   const edm::EDGetTokenT<reco::CandidateView> src_;
0073   edm::EDGetTokenT<edm::View<pat::PackedGenParticle>> genPartsToken_;
0074 };
0075 
0076 #include "FWCore/Framework/interface/MakerMacros.h"
0077 DEFINE_FWK_MODULE(PackedCandMCMatchTableProducer);