|
| 1 | +#include "FWCore/Framework/interface/global/EDProducer.h" |
| 2 | +#include "FWCore/Framework/interface/Event.h" |
| 3 | +#include "FWCore/ParameterSet/interface/ParameterSet.h" |
| 4 | +#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" |
| 5 | +#include "FWCore/ParameterSet/interface/ParameterSetDescription.h" |
| 6 | +#include "DataFormats/NanoAOD/interface/FlatTable.h" |
| 7 | +#include "DataFormats/Common/interface/View.h" |
| 8 | +#include "DataFormats/Candidate/interface/Candidate.h" |
| 9 | +#include "DataFormats/PatCandidates/interface/PackedGenParticle.h" |
| 10 | +#include "DataFormats/HepMCCandidate/interface/GenParticle.h" |
| 11 | +#include <DataFormats/Math/interface/deltaR.h> |
| 12 | +#include "DataFormats/JetReco/interface/GenJetCollection.h" |
| 13 | +#include "PhysicsTools/JetMCUtils/interface/CandMCTag.h" |
| 14 | + |
| 15 | +#include <vector> |
| 16 | +#include <iostream> |
| 17 | + |
| 18 | +class GenCandMotherTableProducer : public edm::global::EDProducer<> { |
| 19 | +public: |
| 20 | + GenCandMotherTableProducer(edm::ParameterSet const ¶ms) |
| 21 | + : objName_(params.getParameter<std::string>("objName")), |
| 22 | + branchName_(params.getParameter<std::string>("branchName")), |
| 23 | + src_(consumes<edm::View<pat::PackedGenParticle>>(params.getParameter<edm::InputTag>("src"))), |
| 24 | + candMap_(consumes<edm::Association<reco::GenParticleCollection>>(params.getParameter<edm::InputTag>("mcMap"))) { |
| 25 | + produces<nanoaod::FlatTable>(); |
| 26 | + } |
| 27 | + |
| 28 | + ~GenCandMotherTableProducer() override {} |
| 29 | + |
| 30 | + void produce(edm::StreamID id, edm::Event &iEvent, const edm::EventSetup &iSetup) const override { |
| 31 | + edm::Handle<edm::View<pat::PackedGenParticle>> cands; |
| 32 | + iEvent.getByToken(src_, cands); |
| 33 | + unsigned int ncand = cands->size(); |
| 34 | + |
| 35 | + auto tab = std::make_unique<nanoaod::FlatTable>(ncand, objName_, false, true); |
| 36 | + |
| 37 | + edm::Handle<edm::Association<reco::GenParticleCollection>> map; |
| 38 | + iEvent.getByToken(candMap_, map); |
| 39 | + |
| 40 | + std::vector<int> key(ncand, -1), fromB(ncand, 0), fromC(ncand, 0); |
| 41 | + for (unsigned int i = 0; i < ncand; ++i) { |
| 42 | + reco::GenParticleRef motherRef = cands->at(i).motherRef(); |
| 43 | + reco::GenParticleRef match = (*map)[motherRef]; |
| 44 | + |
| 45 | + if (match.isNull()) |
| 46 | + continue; |
| 47 | + |
| 48 | + key[i] = match.key(); |
| 49 | + fromB[i] = isFromB(cands->at(i)); |
| 50 | + fromC[i] = isFromC(cands->at(i)); |
| 51 | + } |
| 52 | + |
| 53 | + tab->addColumn<int>(branchName_ + "MotherIdx", key, "Mother index into GenPart list"); |
| 54 | + tab->addColumn<uint8_t>("isFromB", fromB, "Is from B hadron: no: 0, any: 1, final: 2"); |
| 55 | + tab->addColumn<uint8_t>("isFromC", fromC, "Is from C hadron: no: 0, any: 1, final: 2"); |
| 56 | + iEvent.put(std::move(tab)); |
| 57 | + } |
| 58 | + |
| 59 | + bool isFinalB(const reco::Candidate &particle) const { |
| 60 | + if (!CandMCTagUtils::hasBottom(particle)) |
| 61 | + return false; |
| 62 | + |
| 63 | + // check if any of the daughters is also a b hadron |
| 64 | + unsigned int npart = particle.numberOfDaughters(); |
| 65 | + |
| 66 | + for (size_t i = 0; i < npart; ++i) { |
| 67 | + if (CandMCTagUtils::hasBottom(*particle.daughter(i))) |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + int isFromB(const reco::Candidate &particle) const { |
| 75 | + int fromB = 0; |
| 76 | + |
| 77 | + unsigned int npart = particle.numberOfMothers(); |
| 78 | + for (size_t i = 0; i < npart; ++i) { |
| 79 | + const reco::Candidate &mom = *particle.mother(i); |
| 80 | + if (CandMCTagUtils::hasBottom(mom)) { |
| 81 | + fromB = isFinalB(mom) ? 2 : 1; |
| 82 | + break; |
| 83 | + } else |
| 84 | + fromB = isFromB(mom); |
| 85 | + } |
| 86 | + return fromB; |
| 87 | + } |
| 88 | + |
| 89 | + bool isFinalC(const reco::Candidate &particle) const { |
| 90 | + if (!CandMCTagUtils::hasCharm(particle)) |
| 91 | + return false; |
| 92 | + |
| 93 | + // check if any of the daughters is also a c hadron |
| 94 | + unsigned int npart = particle.numberOfDaughters(); |
| 95 | + |
| 96 | + for (size_t i = 0; i < npart; ++i) { |
| 97 | + if (CandMCTagUtils::hasCharm(*particle.daughter(i))) |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + int isFromC(const reco::Candidate &particle) const { |
| 105 | + int fromC = 0; |
| 106 | + |
| 107 | + unsigned int npart = particle.numberOfMothers(); |
| 108 | + for (size_t i = 0; i < npart; ++i) { |
| 109 | + const reco::Candidate &mom = *particle.mother(i); |
| 110 | + if (CandMCTagUtils::hasCharm(mom)) { |
| 111 | + fromC = isFinalC(mom) ? 2 : 1; |
| 112 | + break; |
| 113 | + } else |
| 114 | + fromC = isFromC(mom); |
| 115 | + } |
| 116 | + return fromC; |
| 117 | + } |
| 118 | + |
| 119 | + static void fillDescriptions(edm::ConfigurationDescriptions &descriptions) { |
| 120 | + edm::ParameterSetDescription desc; |
| 121 | + desc.add<std::string>("objName", "GenCands") |
| 122 | + ->setComment("name of the nanoaod::FlatTable to extend with this table"); |
| 123 | + desc.add<std::string>("branchName", "GenPart") |
| 124 | + ->setComment( |
| 125 | + "name of the column to write (the final branch in the nanoaod will be <objName>_<branchName>Idx and " |
| 126 | + "<objName>_<branchName>Flav"); |
| 127 | + desc.add<edm::InputTag>("src", edm::InputTag("packedGenParticles")) |
| 128 | + ->setComment("collection of the packedGenParticles, with association to prunedGenParticles"); |
| 129 | + desc.add<edm::InputTag>("mcMap", edm::InputTag("finalGenParticles")) |
| 130 | + ->setComment( |
| 131 | + "tag to an edm::Association<GenParticleCollection> mapping prunedGenParticles to finalGenParticles"); |
| 132 | + desc.addOptional<edm::InputTag>("genparticles", edm::InputTag("finalGenparticles")) |
| 133 | + ->setComment("Collection of genParticles to be mapped."); |
| 134 | + descriptions.add("genCandMotherTable", desc); |
| 135 | + } |
| 136 | + |
| 137 | +protected: |
| 138 | + const std::string objName_, branchName_, doc_; |
| 139 | + const edm::EDGetTokenT<edm::View<pat::PackedGenParticle>> src_; |
| 140 | + const edm::EDGetTokenT<edm::Association<reco::GenParticleCollection>> candMap_; |
| 141 | + edm::EDGetTokenT<reco::GenParticleCollection> genPartsToken_; |
| 142 | +}; |
| 143 | + |
| 144 | +#include "FWCore/Framework/interface/MakerMacros.h" |
| 145 | +DEFINE_FWK_MODULE(GenCandMotherTableProducer); |
0 commit comments