Skip to content

Commit 0f2295a

Browse files
authored
Merge pull request #49137 from nurfikri89/from1600pre1_fixPuppiWeightInPFCandTableForReNanoV15
[NanoAOD] Fix puppi weight in PFCand table for reNanoV15
2 parents 96f9582 + dda2d9c commit 0f2295a

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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);

PhysicsTools/NanoAOD/python/jetConstituents_cff.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@
5858
jetConstituentsTablesTask = cms.Task(finalPFCandidates,pfCandidatesTable,finalJetsAK8ConstituentsTable)
5959

6060

61+
def UsePuppiWeightFromValueMapForPFCandTable(process,puppiLabel="packedpuppi"):
62+
process.packedPFCandidatesVarProducer = cms.EDProducer("PFCandidatesVarProducer",
63+
src = cms.InputTag("packedPFCandidates"),
64+
puppi_value_map = cms.InputTag(puppiLabel)
65+
)
66+
process.jetConstituentsTask.add(process.packedPFCandidatesVarProducer)
67+
68+
del process.pfCandidatesTable.variables.pt
69+
del process.pfCandidatesTable.variables.mass
70+
71+
process.pfCandidatesTable.externalVariables = cms.PSet(
72+
pt = ExtVar(cms.InputTag("packedPFCandidatesVarProducer:ptWeighted"), float, doc="Puppi-weighted pt", precision=10),
73+
mass = ExtVar(cms.InputTag("packedPFCandidatesVarProducer:massWeighted"), float, doc="Puppi-weighted mass", precision=10),
74+
)
75+
76+
return process
77+
6178
def SaveAK4JetConstituents(process, jetCut="", jetConstCut=""):
6279
"""
6380
This function can be used as a cmsDriver customization

PhysicsTools/NanoAOD/python/nano_cff.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ def nanoAOD_customizeCommon(process):
247247
reclusterAK8=nanoAOD_rePuppi_switch.reclusterAK8.value(),
248248
)
249249

250+
if not(nanoAOD_rePuppi_switch.useExistingWeights) and (nanoAOD_rePuppi_switch.reclusterAK4MET or nanoAOD_rePuppi_switch.reclusterAK8):
251+
process = UsePuppiWeightFromValueMapForPFCandTable(process)
252+
250253
# This function is defined in jetsAK4_Puppi_cff.py
251254
process = nanoAOD_addDeepInfoAK4(process,
252255
addParticleNet=nanoAOD_addDeepInfoAK4_switch.nanoAOD_addParticleNet_switch,

0 commit comments

Comments
 (0)