CMS 3D CMS Logo

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