CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MVAJetPuIdProducer.cc
Go to the documentation of this file.
1 #include <memory>
9 
11 
16 
20 
22 
24 public:
25  explicit MVAJetPuIdProducer(const edm::ParameterSet &);
26 
27  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
28 
29 private:
30  void produce(edm::Event &, const edm::EventSetup &) override;
31 
32  void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData);
33 
37  std::vector<std::pair<std::string, MVAJetPuId *>> algos_;
38 
42  std::vector<JetCorrectorParameters> jetCorPars_;
43 
48 };
49 
51  runMvas_ = iConfig.getParameter<bool>("runMvas");
52  produceJetIds_ = iConfig.getParameter<bool>("produceJetIds");
53  jets_ = iConfig.getParameter<edm::InputTag>("jets");
54  vertexes_ = iConfig.getParameter<edm::InputTag>("vertexes");
55  jetids_ = iConfig.getParameter<edm::InputTag>("jetids");
56  inputIsCorrected_ = iConfig.getParameter<bool>("inputIsCorrected");
57  applyJec_ = iConfig.getParameter<bool>("applyJec");
58  jec_ = iConfig.getParameter<std::string>("jec");
59  rho_ = iConfig.getParameter<edm::InputTag>("rho");
60  residualsFromTxt_ = iConfig.getParameter<bool>("residualsFromTxt");
61  if (residualsFromTxt_)
62  residualsTxt_ = iConfig.getParameter<edm::FileInPath>("residualsTxt");
63  std::vector<edm::ParameterSet> algos = iConfig.getParameter<std::vector<edm::ParameterSet>>("algos");
64 
65  jecCor_ = nullptr;
66 
67  if (!runMvas_)
68  assert(algos.size() == 1);
69 
70  if (produceJetIds_) {
71  produces<edm::ValueMap<StoredPileupJetIdentifier>>("");
72  }
73  for (std::vector<edm::ParameterSet>::iterator it = algos.begin(); it != algos.end(); ++it) {
74  std::string label = it->getParameter<std::string>("label");
75  algos_.push_back(std::make_pair(label, new MVAJetPuId(*it)));
76  if (runMvas_) {
77  produces<edm::ValueMap<float>>(label + "Discriminant");
78  produces<edm::ValueMap<int>>(label + "Id");
79  }
80  }
81 
82  input_jet_token_ = consumes<edm::View<reco::Jet>>(jets_);
83  input_vertex_token_ = consumes<reco::VertexCollection>(vertexes_);
84  input_vm_pujetid_token_ = consumes<edm::ValueMap<StoredPileupJetIdentifier>>(jetids_);
85  input_rho_token_ = consumes<double>(rho_);
86 }
87 
89  using namespace edm;
90  using namespace std;
91  using namespace reco;
92 
93  Handle<View<Jet>> jetHandle;
94  iEvent.getByToken(input_jet_token_, jetHandle);
95  const View<Jet> &jets = *jetHandle;
96  Handle<VertexCollection> vertexHandle;
97  if (produceJetIds_) {
98  iEvent.getByToken(input_vertex_token_, vertexHandle);
99  }
100  const VertexCollection &vertexes = *(vertexHandle.product());
102  if (!produceJetIds_) {
103  iEvent.getByToken(input_vm_pujetid_token_, vmap);
104  }
105  edm::Handle<double> rhoH;
106  double rho = 0.;
107 
108  vector<StoredPileupJetIdentifier> ids;
109  map<string, vector<float>> mvas;
110  map<string, vector<int>> idflags;
111 
112  VertexCollection::const_iterator vtx;
113  if (produceJetIds_) {
114  vtx = vertexes.begin();
115  while (vtx != vertexes.end() && (vtx->isFake() || vtx->ndof() < 4)) {
116  ++vtx;
117  }
118  if (vtx == vertexes.end()) {
119  vtx = vertexes.begin();
120  }
121  }
122 
123  for (unsigned int i = 0; i < jets.size(); ++i) {
124  vector<pair<string, MVAJetPuId *>>::iterator algoi = algos_.begin();
125  MVAJetPuId *ialgo = algoi->second;
126 
127  const Jet &jet = jets[i];
128 
129  float jec = 0.;
130  if (applyJec_) {
131  if (rho == 0.) {
132  iEvent.getByToken(input_rho_token_, rhoH);
133  rho = *rhoH;
134  }
135  jecCor_->setJetPt(jet.pt());
136  jecCor_->setJetEta(jet.eta());
137  jecCor_->setJetA(jet.jetArea());
138  jecCor_->setRho(rho);
139  jec = jecCor_->getCorrection();
140  }
141 
142  bool applyJec = applyJec_ || !inputIsCorrected_;
143  reco::Jet *corrJet = nullptr;
144  if (applyJec) {
145  float scale = jec;
146  corrJet = dynamic_cast<reco::Jet *>(jet.clone());
147  corrJet->scaleEnergy(scale);
148  }
149  const reco::Jet *theJet = (applyJec ? corrJet : &jet);
150  PileupJetIdentifier puIdentifier;
151  if (produceJetIds_) {
152  puIdentifier = ialgo->computeIdVariables(theJet, jec, &(*vtx), vertexes, runMvas_);
153  ids.push_back(puIdentifier);
154  } else {
155  puIdentifier = (*vmap)[jets.refAt(i)];
156  puIdentifier.jetPt(theJet->pt()); /*make sure JEC is applied when computing the MVA*/
157  puIdentifier.jetEta(theJet->eta());
158  puIdentifier.jetPhi(theJet->phi());
159  ialgo->set(puIdentifier);
160  puIdentifier = ialgo->computeMva();
161  }
162 
163  if (runMvas_) {
164  for (; algoi != algos_.end(); ++algoi) {
165  ialgo = algoi->second;
166  ialgo->set(puIdentifier);
167  PileupJetIdentifier id = ialgo->computeMva();
168  mvas[algoi->first].push_back(id.mva());
169  idflags[algoi->first].push_back(id.idFlag());
170  }
171  }
172 
173  if (corrJet) {
174  delete corrJet;
175  }
176  }
177 
178  if (runMvas_) {
179  for (vector<pair<string, MVAJetPuId *>>::iterator ialgo = algos_.begin(); ialgo != algos_.end(); ++ialgo) {
180  vector<float> &mva = mvas[ialgo->first];
181  auto mvaout = std::make_unique<ValueMap<float>>();
182  ValueMap<float>::Filler mvafiller(*mvaout);
183  mvafiller.insert(jetHandle, mva.begin(), mva.end());
184  mvafiller.fill();
185  iEvent.put(std::move(mvaout), ialgo->first + "Discriminant");
186 
187  vector<int> &idflag = idflags[ialgo->first];
188  auto idflagout = std::make_unique<ValueMap<int>>();
189  ValueMap<int>::Filler idflagfiller(*idflagout);
190  idflagfiller.insert(jetHandle, idflag.begin(), idflag.end());
191  idflagfiller.fill();
192  iEvent.put(std::move(idflagout), ialgo->first + "Id");
193  }
194  }
195  if (produceJetIds_) {
196  assert(jetHandle->size() == ids.size());
197  auto idsout = std::make_unique<ValueMap<StoredPileupJetIdentifier>>();
199  idsfiller.insert(jetHandle, ids.begin(), ids.end());
200  idsfiller.fill();
201  iEvent.put(std::move(idsout));
202  }
203 }
204 
207  desc.add<bool>("runMvas", true);
208  desc.add<bool>("inputIsCorrected", true);
209  desc.add<edm::InputTag>("vertexes", edm::InputTag("hltPixelVertices"));
210  desc.add<bool>("produceJetIds", true);
211  desc.add<std::string>("jec", "AK4PF");
212  desc.add<bool>("residualsFromTxt", false);
213  desc.add<bool>("applyJec", false);
214  desc.add<edm::InputTag>("jetids", edm::InputTag(""));
215  desc.add<edm::InputTag>("rho", edm::InputTag("hltFixedGridRhoFastjetAll"));
216  desc.add<edm::InputTag>("jets", edm::InputTag("hltAK4PFJetsCorrected"));
218  vpsd1.add<std::vector<std::string>>("tmvaVariables",
219  {
220  "rho",
221  "nParticles",
222  "nCharged",
223  "majW",
224  "minW",
225  "frac01",
226  "frac02",
227  "frac03",
228  "frac04",
229  "ptD",
230  "beta",
231  "betaStar",
232  "dR2Mean",
233  "pull",
234  "jetR",
235  "jetRchg",
236  });
237  vpsd1.add<std::string>("tmvaMethod", "JetID");
238  vpsd1.add<bool>("cutBased", false);
239  vpsd1.add<std::string>("tmvaWeights", "RecoJets/JetProducers/data/MVAJetPuID.weights.xml.gz");
240  vpsd1.add<std::vector<std::string>>("tmvaSpectators",
241  {
242  "jetEta",
243  "jetPt",
244  });
245  vpsd1.add<std::string>("label", "CATEv0");
246  vpsd1.add<int>("version", -1);
247  {
249  psd0.add<std::vector<double>>("Pt2030_Tight",
250  {
251  0.73,
252  0.05,
253  -0.26,
254  -0.42,
255  });
256  psd0.add<std::vector<double>>("Pt2030_Loose",
257  {
258  -0.63,
259  -0.6,
260  -0.55,
261  -0.45,
262  });
263  psd0.add<std::vector<double>>("Pt3050_Medium",
264  {
265  0.1,
266  -0.36,
267  -0.54,
268  -0.54,
269  });
270  psd0.add<std::vector<double>>("Pt1020_Tight",
271  {
272  -0.83,
273  -0.81,
274  -0.74,
275  -0.81,
276  });
277  psd0.add<std::vector<double>>("Pt2030_Medium",
278  {
279  0.1,
280  -0.36,
281  -0.54,
282  -0.54,
283  });
284  psd0.add<std::vector<double>>("Pt010_Tight",
285  {
286  -0.83,
287  -0.81,
288  -0.74,
289  -0.81,
290  });
291  psd0.add<std::vector<double>>("Pt1020_Loose",
292  {
293  -0.95,
294  -0.96,
295  -0.94,
296  -0.95,
297  });
298  psd0.add<std::vector<double>>("Pt010_Medium",
299  {
300  -0.83,
301  -0.92,
302  -0.9,
303  -0.92,
304  });
305  psd0.add<std::vector<double>>("Pt1020_Medium",
306  {
307  -0.83,
308  -0.92,
309  -0.9,
310  -0.92,
311  });
312  psd0.add<std::vector<double>>("Pt010_Loose",
313  {
314  -0.95,
315  -0.96,
316  -0.94,
317  -0.95,
318  });
319  psd0.add<std::vector<double>>("Pt3050_Loose",
320  {
321  -0.63,
322  -0.6,
323  -0.55,
324  -0.45,
325  });
326  psd0.add<std::vector<double>>("Pt3050_Tight",
327  {
328  0.73,
329  0.05,
330  -0.26,
331  -0.42,
332  });
333  vpsd1.add<edm::ParameterSetDescription>("JetIdParams", psd0);
334  }
335  vpsd1.add<double>("impactParTkThreshold", 1.0);
336  std::vector<edm::ParameterSet> temp1;
337  temp1.reserve(1);
338 
339  desc.addVPSet("algos", vpsd1, temp1);
340 
341  descriptions.add("MVAJetPuIdProducer", desc);
342 }
343 
void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
virtual void scaleEnergy(double fScale)
scale energy of the jet
double pt() const final
transverse momentum
FactorizedJetCorrector * jecCor_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Base class for all types of Jets.
Definition: Jet.h:20
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
assert(be >=bs)
const float & jetPt() const
edm::FileInPath residualsTxt_
char const * label
int iEvent
Definition: GenABIO.cc:224
MVAJetPuIdProducer(const edm::ParameterSet &)
vector< PseudoJet > jets
def move
Definition: eostools.py:511
PileupJetIdentifier computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, double rho, bool calculateMva=false)
Definition: MVAJetPuId.cc:196
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< JetCorrectorParameters > jetCorPars_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void produce(edm::Event &, const edm::EventSetup &) override
tuple algos
Definition: Puppi_cff.py:75
edm::EDGetTokenT< edm::View< reco::Jet > > input_jet_token_
const float & jetPhi() const
std::vector< std::pair< std::string, MVAJetPuId * > > algos_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void set(const PileupJetIdentifier &)
Definition: MVAJetPuId.cc:143
virtual float jetArea() const
get jet area
Definition: Jet.h:103
PileupJetIdentifier computeMva()
Definition: MVAJetPuId.cc:191
edm::EDGetTokenT< double > input_rho_token_
edm::EDGetTokenT< edm::ValueMap< StoredPileupJetIdentifier > > input_vm_pujetid_token_
double phi() const final
momentum azimuthal angle
CompositePtrCandidate * clone() const override
returns a clone of the candidate
edm::EDGetTokenT< reco::VertexCollection > input_vertex_token_
const float & jetEta() const
double eta() const final
momentum pseudorapidity