-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
PileupJetIdProducer.cc
280 lines (246 loc) · 9.95 KB
/
PileupJetIdProducer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// -*- C++ -*-
//
// Package: PileupJetIdProducer
// Class: PileupJetIdProducer
//
/**\class PileupJetIdProducer PileupJetIdProducer.cc CMGTools/PileupJetIdProducer/src/PileupJetIdProducer.cc
Description: [one line class summary]
Implementation:
[Notes on implementation]
*/
//
// Original Author: Pasquale Musella,40 2-A12,+41227671706,
// Created: Wed Apr 18 15:48:47 CEST 2012
//
//
#include "RecoJets/JetProducers/plugins/PileupJetIdProducer.h"
#include <memory>
GBRForestsAndConstants::GBRForestsAndConstants(edm::ParameterSet const& iConfig)
: runMvas_(iConfig.getParameter<bool>("runMvas")),
produceJetIds_(iConfig.getParameter<bool>("produceJetIds")),
inputIsCorrected_(iConfig.getParameter<bool>("inputIsCorrected")),
applyJec_(iConfig.getParameter<bool>("applyJec")),
jec_(iConfig.getParameter<std::string>("jec")),
residualsFromTxt_(iConfig.getParameter<bool>("residualsFromTxt")),
applyConstituentWeight_(false) {
if (residualsFromTxt_) {
residualsTxt_ = iConfig.getParameter<edm::FileInPath>("residualsTxt");
}
std::vector<edm::ParameterSet> algos = iConfig.getParameter<std::vector<edm::ParameterSet>>("algos");
for (auto const& algoPset : algos) {
vAlgoGBRForestsAndConstants_.emplace_back(algoPset, runMvas_);
}
if (!runMvas_) {
assert(algos.size() == 1);
}
edm::InputTag srcConstituentWeights = iConfig.getParameter<edm::InputTag>("srcConstituentWeights");
if (!srcConstituentWeights.label().empty()) {
applyConstituentWeight_ = true;
}
}
// ------------------------------------------------------------------------------------------
PileupJetIdProducer::PileupJetIdProducer(const edm::ParameterSet& iConfig, GBRForestsAndConstants const* globalCache) {
if (globalCache->produceJetIds()) {
produces<edm::ValueMap<StoredPileupJetIdentifier>>("");
}
for (auto const& algoGBRForestsAndConstants : globalCache->vAlgoGBRForestsAndConstants()) {
std::string const& label = algoGBRForestsAndConstants.label();
algos_.emplace_back(label, std::make_unique<PileupJetIdAlgo>(&algoGBRForestsAndConstants));
if (globalCache->runMvas()) {
produces<edm::ValueMap<float>>(label + "Discriminant");
produces<edm::ValueMap<int>>(label + "Id");
}
}
input_jet_token_ = consumes<edm::View<reco::Jet>>(iConfig.getParameter<edm::InputTag>("jets"));
input_vertex_token_ = consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexes"));
input_vm_pujetid_token_ =
consumes<edm::ValueMap<StoredPileupJetIdentifier>>(iConfig.getParameter<edm::InputTag>("jetids"));
input_rho_token_ = consumes<double>(iConfig.getParameter<edm::InputTag>("rho"));
parameters_token_ = esConsumes(edm::ESInputTag("", globalCache->jec()));
edm::InputTag srcConstituentWeights = iConfig.getParameter<edm::InputTag>("srcConstituentWeights");
if (!srcConstituentWeights.label().empty()) {
input_constituent_weights_token_ = consumes<edm::ValueMap<float>>(srcConstituentWeights);
}
}
// ------------------------------------------------------------------------------------------
PileupJetIdProducer::~PileupJetIdProducer() {}
// ------------------------------------------------------------------------------------------
void PileupJetIdProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
GBRForestsAndConstants const* gc = globalCache();
using namespace edm;
using namespace std;
using namespace reco;
// Input jets
Handle<View<Jet>> jetHandle;
iEvent.getByToken(input_jet_token_, jetHandle);
const View<Jet>& jets = *jetHandle;
// Constituent weight (e.g PUPPI) Value Map
edm::ValueMap<float> constituentWeights;
if (!input_constituent_weights_token_.isUninitialized()) {
constituentWeights = iEvent.get(input_constituent_weights_token_);
}
// input variables
Handle<ValueMap<StoredPileupJetIdentifier>> vmap;
if (!gc->produceJetIds()) {
iEvent.getByToken(input_vm_pujetid_token_, vmap);
}
// rho
edm::Handle<double> rhoH;
double rho = 0.;
// products
vector<StoredPileupJetIdentifier> ids;
map<string, vector<float>> mvas;
map<string, vector<int>> idflags;
const VertexCollection* vertexes = nullptr;
VertexCollection::const_iterator vtx;
if (gc->produceJetIds()) {
// vertexes
Handle<VertexCollection> vertexHandle;
iEvent.getByToken(input_vertex_token_, vertexHandle);
vertexes = vertexHandle.product();
// require basic quality cuts on the vertexes
vtx = vertexes->begin();
while (vtx != vertexes->end() && (vtx->isFake() || vtx->ndof() < 4)) {
++vtx;
}
if (vtx == vertexes->end()) {
vtx = vertexes->begin();
}
}
// Loop over input jets
bool ispat = true;
for (unsigned int i = 0; i < jets.size(); ++i) {
// Pick the first algo to compute the input variables
auto algoi = algos_.begin();
PileupJetIdAlgo* ialgo = algoi->second.get();
const Jet& jet = jets.at(i);
const pat::Jet* patjet = nullptr;
if (ispat) {
patjet = dynamic_cast<const pat::Jet*>(&jet);
ispat = patjet != nullptr;
}
// Get jet energy correction
float jec = 0.;
if (gc->applyJec()) {
// If haven't done it get rho from the event
if (rho == 0.) {
iEvent.getByToken(input_rho_token_, rhoH);
rho = *rhoH;
}
// jet corrector
if (not jecCor_) {
initJetEnergyCorrector(iSetup, iEvent.isRealData());
}
if (ispat) {
jecCor_->setJetPt(patjet->correctedJet(0).pt());
} else {
jecCor_->setJetPt(jet.pt());
}
jecCor_->setJetEta(jet.eta());
jecCor_->setJetA(jet.jetArea());
jecCor_->setRho(rho);
jec = jecCor_->getCorrection();
}
// If it was requested AND the input is an uncorrected jet apply the JEC
bool applyJec = gc->applyJec() && (ispat || !gc->inputIsCorrected());
std::unique_ptr<reco::Jet> corrJet;
if (applyJec) {
float scale = jec;
if (ispat) {
corrJet = std::make_unique<pat::Jet>(patjet->correctedJet(0));
} else {
corrJet.reset(dynamic_cast<reco::Jet*>(jet.clone()));
}
corrJet->scaleEnergy(scale);
}
const reco::Jet* theJet = (applyJec ? corrJet.get() : &jet);
PileupJetIdentifier puIdentifier;
if (gc->produceJetIds()) {
// Compute the input variables
////////////////////////////// added PUPPI weight Value Map
puIdentifier = ialgo->computeIdVariables(
theJet, jec, &(*vtx), *vertexes, rho, constituentWeights, gc->applyConstituentWeight());
ids.push_back(puIdentifier);
} else {
// Or read it from the value map
puIdentifier = (*vmap)[jets.refAt(i)];
puIdentifier.jetPt(theJet->pt()); // make sure JEC is applied when computing the MVA
puIdentifier.jetEta(theJet->eta());
puIdentifier.jetPhi(theJet->phi());
ialgo->set(puIdentifier);
puIdentifier = ialgo->computeMva();
}
if (gc->runMvas()) {
// Compute the MVA and WP
mvas[algoi->first].push_back(puIdentifier.mva());
idflags[algoi->first].push_back(puIdentifier.idFlag());
for (++algoi; algoi != algos_.end(); ++algoi) {
ialgo = algoi->second.get();
ialgo->set(puIdentifier);
PileupJetIdentifier id = ialgo->computeMva();
mvas[algoi->first].push_back(id.mva());
idflags[algoi->first].push_back(id.idFlag());
}
}
}
// Produce the output value maps
if (gc->runMvas()) {
for (const auto& ialgo : algos_) {
// MVA
vector<float>& mva = mvas[ialgo.first];
auto mvaout = std::make_unique<ValueMap<float>>();
ValueMap<float>::Filler mvafiller(*mvaout);
mvafiller.insert(jetHandle, mva.begin(), mva.end());
mvafiller.fill();
iEvent.put(std::move(mvaout), ialgo.first + "Discriminant");
// WP
vector<int>& idflag = idflags[ialgo.first];
auto idflagout = std::make_unique<ValueMap<int>>();
ValueMap<int>::Filler idflagfiller(*idflagout);
idflagfiller.insert(jetHandle, idflag.begin(), idflag.end());
idflagfiller.fill();
iEvent.put(std::move(idflagout), ialgo.first + "Id");
}
}
// input variables
if (gc->produceJetIds()) {
assert(jetHandle->size() == ids.size());
auto idsout = std::make_unique<ValueMap<StoredPileupJetIdentifier>>();
ValueMap<StoredPileupJetIdentifier>::Filler idsfiller(*idsout);
idsfiller.insert(jetHandle, ids.begin(), ids.end());
idsfiller.fill();
iEvent.put(std::move(idsout));
}
}
// ------------------------------------------------------------------------------------------
void PileupJetIdProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
//The following says we do not know what parameters are allowed so do no validation
// Please change this to state exactly what you do use, even if it is no parameters
edm::ParameterSetDescription desc;
desc.setUnknown();
descriptions.addDefault(desc);
}
// ------------------------------------------------------------------------------------------
void PileupJetIdProducer::initJetEnergyCorrector(const edm::EventSetup& iSetup, bool isData) {
GBRForestsAndConstants const* gc = globalCache();
//jet energy correction levels to apply on raw jet
std::vector<std::string> jecLevels;
jecLevels.push_back("L1FastJet");
jecLevels.push_back("L2Relative");
jecLevels.push_back("L3Absolute");
if (isData && !gc->residualsFromTxt())
jecLevels.push_back("L2L3Residual");
//check the corrector parameters needed according to the correction levels
auto const& parameters = iSetup.getData(parameters_token_);
for (std::vector<std::string>::const_iterator ll = jecLevels.begin(); ll != jecLevels.end(); ++ll) {
const JetCorrectorParameters& ip = parameters[*ll];
jetCorPars_.push_back(ip);
}
if (isData && gc->residualsFromTxt()) {
jetCorPars_.push_back(JetCorrectorParameters(gc->residualsTxt().fullPath()));
}
//instantiate the jet corrector
jecCor_ = std::make_unique<FactorizedJetCorrector>(jetCorPars_);
}
//define this as a plug-in
DEFINE_FWK_MODULE(PileupJetIdProducer);