|
| 1 | +#include <string> |
| 2 | +#include <memory> |
| 3 | + |
| 4 | +#include "FWCore/Framework/interface/Frameworkfwd.h" |
| 5 | +#include "FWCore/Framework/interface/stream/EDProducer.h" |
| 6 | +#include "FWCore/Framework/interface/Event.h" |
| 7 | +#include "FWCore/Framework/interface/MakerMacros.h" |
| 8 | + |
| 9 | +#include "FWCore/ParameterSet/interface/ParameterSet.h" |
| 10 | +#include "FWCore/Utilities/interface/StreamID.h" |
| 11 | + |
| 12 | +#include "DataFormats/Common/interface/View.h" |
| 13 | +#include "DataFormats/Common/interface/ValueMap.h" |
| 14 | +#include "DataFormats/PatCandidates/interface/PackedCandidate.h" |
| 15 | +#include "DataFormats/Candidate/interface/CandidateFwd.h" |
| 16 | +#include "DataFormats/NanoAOD/interface/FlatTable.h" |
| 17 | + |
| 18 | +class PFCandidatesVarProducer : public edm::stream::EDProducer<> { |
| 19 | +public: |
| 20 | + explicit PFCandidatesVarProducer(const edm::ParameterSet&); |
| 21 | + ~PFCandidatesVarProducer() override; |
| 22 | + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); |
| 23 | + |
| 24 | +private: |
| 25 | + void produce(edm::Event&, const edm::EventSetup&) override; |
| 26 | + void PutValueMapInEvent(edm::Event&, |
| 27 | + const edm::Handle<edm::View<reco::Candidate>>&, |
| 28 | + const std::vector<float>&, |
| 29 | + std::string); |
| 30 | + edm::EDGetTokenT<edm::View<reco::Candidate>> pfcands_token_; |
| 31 | + edm::EDGetTokenT<edm::ValueMap<float>> puppi_value_map_token_; |
| 32 | +}; |
| 33 | + |
| 34 | +PFCandidatesVarProducer::PFCandidatesVarProducer(const edm::ParameterSet& iConfig) |
| 35 | + : pfcands_token_(consumes<edm::View<reco::Candidate>>(iConfig.getParameter<edm::InputTag>("src"))), |
| 36 | + puppi_value_map_token_(consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("puppi_value_map"))) { |
| 37 | + produces<edm::ValueMap<float>>("ptWeighted"); |
| 38 | + produces<edm::ValueMap<float>>("massWeighted"); |
| 39 | +} |
| 40 | + |
| 41 | +PFCandidatesVarProducer::~PFCandidatesVarProducer() {} |
| 42 | + |
| 43 | +void PFCandidatesVarProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) { |
| 44 | + // Get PF candidates |
| 45 | + edm::Handle<edm::View<reco::Candidate>> pfcands_handle; |
| 46 | + iEvent.getByToken(pfcands_token_, pfcands_handle); |
| 47 | + const edm::View<reco::Candidate>* pfcands = pfcands_handle.product(); |
| 48 | + |
| 49 | + // Get puppi value map |
| 50 | + edm::Handle<edm::ValueMap<float>> puppi_value_map_; |
| 51 | + iEvent.getByToken(puppi_value_map_token_, puppi_value_map_); |
| 52 | + |
| 53 | + std::vector<float> ptWeighted(pfcands->size(), 0.f); |
| 54 | + std::vector<float> massWeighted(pfcands->size(), 0.f); |
| 55 | + |
| 56 | + for (std::size_t pfcand_idx = 0; pfcand_idx < pfcands->size(); pfcand_idx++) { |
| 57 | + const auto& cand = (*pfcands)[pfcand_idx]; |
| 58 | + |
| 59 | + reco::CandidatePtr candPtr(pfcands_handle, pfcand_idx); |
| 60 | + float puppiWeightVal = (*puppi_value_map_)[candPtr]; |
| 61 | + |
| 62 | + ptWeighted[pfcand_idx] = puppiWeightVal * cand.pt(); |
| 63 | + massWeighted[pfcand_idx] = puppiWeightVal * cand.mass(); |
| 64 | + } |
| 65 | + |
| 66 | + PutValueMapInEvent(iEvent, pfcands_handle, ptWeighted, "ptWeighted"); |
| 67 | + PutValueMapInEvent(iEvent, pfcands_handle, massWeighted, "massWeighted"); |
| 68 | +} |
| 69 | +void PFCandidatesVarProducer::PutValueMapInEvent(edm::Event& iEvent, |
| 70 | + const edm::Handle<edm::View<reco::Candidate>>& coll, |
| 71 | + const std::vector<float>& vec_var, |
| 72 | + std::string VMName) { |
| 73 | + std::unique_ptr<edm::ValueMap<float>> VM(new edm::ValueMap<float>()); |
| 74 | + edm::ValueMap<float>::Filler fillerVM(*VM); |
| 75 | + fillerVM.insert(coll, vec_var.begin(), vec_var.end()); |
| 76 | + fillerVM.fill(); |
| 77 | + iEvent.put(std::move(VM), VMName); |
| 78 | +} |
| 79 | + |
| 80 | +void PFCandidatesVarProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { |
| 81 | + edm::ParameterSetDescription desc; |
| 82 | + desc.add<edm::InputTag>("src", edm::InputTag("packedPFCandidates")); |
| 83 | + desc.add<edm::InputTag>("puppi_value_map", edm::InputTag("packedpuppi")); |
| 84 | + desc.add<bool>("fallback_puppi_weight", false); |
| 85 | + descriptions.addWithDefaultLabel(desc); |
| 86 | +} |
| 87 | + |
| 88 | +DEFINE_FWK_MODULE(PFCandidatesVarProducer); |
0 commit comments