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_->setJetPhi(jet.phi());
165  jecCor_->setJetA(jet.jetArea());
166  jecCor_->setRho(rho);
167  jec = jecCor_->getCorrection();
168  }
169  // If it was requested AND the input is an uncorrected jet apply the JEC
170  bool applyJec = gc->applyJec() && (ispat || !gc->inputIsCorrected());
171  std::unique_ptr<reco::Jet> corrJet;
172 
173  if (applyJec) {
174  float scale = jec;
175  if (ispat) {
176  corrJet = std::make_unique<pat::Jet>(patjet->correctedJet(0));
177  } else {
178  corrJet.reset(dynamic_cast<reco::Jet*>(jet.clone()));
179  }
180  corrJet->scaleEnergy(scale);
181  }
182  const reco::Jet* theJet = (applyJec ? corrJet.get() : &jet);
183 
184  PileupJetIdentifier puIdentifier;
185  if (gc->produceJetIds()) {
186  // Compute the input variables
188  puIdentifier = ialgo->computeIdVariables(
189  theJet, jec, &(*vtx), *vertexes, rho, constituentWeights, gc->applyConstituentWeight());
190  ids.push_back(puIdentifier);
191  } else {
192  // Or read it from the value map
193  puIdentifier = (*vmap)[jets.refAt(i)];
194  puIdentifier.jetPt(theJet->pt()); // make sure JEC is applied when computing the MVA
195  puIdentifier.jetEta(theJet->eta());
196  puIdentifier.jetPhi(theJet->phi());
197  ialgo->set(puIdentifier);
198  puIdentifier = ialgo->computeMva();
199  }
200 
201  if (gc->runMvas()) {
202  // Compute the MVA and WP
203  mvas[algoi->first].push_back(puIdentifier.mva());
204  idflags[algoi->first].push_back(puIdentifier.idFlag());
205  for (++algoi; algoi != algos_.end(); ++algoi) {
206  ialgo = algoi->second.get();
207  ialgo->set(puIdentifier);
208  PileupJetIdentifier id = ialgo->computeMva();
209  mvas[algoi->first].push_back(id.mva());
210  idflags[algoi->first].push_back(id.idFlag());
211  }
212  }
213  }
214 
215  // Produce the output value maps
216  if (gc->runMvas()) {
217  for (const auto& ialgo : algos_) {
218  // MVA
219  vector<float>& mva = mvas[ialgo.first];
220  auto mvaout = std::make_unique<ValueMap<float>>();
221  ValueMap<float>::Filler mvafiller(*mvaout);
222  mvafiller.insert(jetHandle, mva.begin(), mva.end());
223  mvafiller.fill();
224  iEvent.put(std::move(mvaout), ialgo.first + "Discriminant");
225 
226  // WP
227  vector<int>& idflag = idflags[ialgo.first];
228  auto idflagout = std::make_unique<ValueMap<int>>();
229  ValueMap<int>::Filler idflagfiller(*idflagout);
230  idflagfiller.insert(jetHandle, idflag.begin(), idflag.end());
231  idflagfiller.fill();
232  iEvent.put(std::move(idflagout), ialgo.first + "Id");
233  }
234  }
235  // input variables
236  if (gc->produceJetIds()) {
237  assert(jetHandle->size() == ids.size());
238  auto idsout = std::make_unique<ValueMap<StoredPileupJetIdentifier>>();
240  idsfiller.insert(jetHandle, ids.begin(), ids.end());
241  idsfiller.fill();
242  iEvent.put(std::move(idsout));
243  }
244 }
245 
246 // ------------------------------------------------------------------------------------------
248  //The following says we do not know what parameters are allowed so do no validation
249  // Please change this to state exactly what you do use, even if it is no parameters
251  desc.setUnknown();
252  descriptions.addDefault(desc);
253 }
254 
255 // ------------------------------------------------------------------------------------------
257  GBRForestsAndConstants const* gc = globalCache();
258 
259  //jet energy correction levels to apply on raw jet
260  std::vector<std::string> jecLevels;
261  jecLevels.push_back("L1FastJet");
262  jecLevels.push_back("L2Relative");
263  jecLevels.push_back("L3Absolute");
264  if (isData && !gc->residualsFromTxt())
265  jecLevels.push_back("L2L3Residual");
266 
267  //check the corrector parameters needed according to the correction levels
268  auto const& parameters = iSetup.getData(parameters_token_);
269  for (std::vector<std::string>::const_iterator ll = jecLevels.begin(); ll != jecLevels.end(); ++ll) {
270  const JetCorrectorParameters& ip = parameters[*ll];
271  jetCorPars_.push_back(ip);
272  }
273  if (isData && gc->residualsFromTxt()) {
275  }
276 
277  //instantiate the jet corrector
278  jecCor_ = std::make_unique<FactorizedJetCorrector>(jetCorPars_);
279 }
280 //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:307
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
double pt() const final
transverse momentum
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:98
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_
const std::string & fullPath() const
Definition: FileInPath.cc:144
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