CMS 3D CMS Logo

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");
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);
140  }
141 
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 
MVAJetPuIdProducer::jecCor_
FactorizedJetCorrector * jecCor_
Definition: MVAJetPuIdProducer.cc:41
MVAJetPuIdProducer::input_vertex_token_
edm::EDGetTokenT< reco::VertexCollection > input_vertex_token_
Definition: MVAJetPuIdProducer.cc:45
reco::Jet::scaleEnergy
virtual void scaleEnergy(double fScale)
scale energy of the jet
mps_fire.i
i
Definition: mps_fire.py:428
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
reco::Jet
Base class for all types of Jets.
Definition: Jet.h:20
MVAJetPuIdProducer::jetids_
edm::InputTag jetids_
Definition: MVAJetPuIdProducer.cc:34
MVAJetPuId::computeIdVariables
PileupJetIdentifier computeIdVariables(const reco::Jet *jet, float jec, const reco::Vertex *, const reco::VertexCollection &, double rho, bool calculateMva=false)
Definition: MVAJetPuId.cc:196
edm::Handle::product
T const * product() const
Definition: Handle.h:70
L1EGammaCrystalsEmulatorProducer_cfi.scale
scale
Definition: L1EGammaCrystalsEmulatorProducer_cfi.py:10
MVAJetPuIdProducer::jets_
edm::InputTag jets_
Definition: MVAJetPuIdProducer.cc:34
MVAJetPuIdProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: MVAJetPuIdProducer.cc:205
ESHandle.h
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
MVAJetPuIdProducer::initJetEnergyCorrector
void initJetEnergyCorrector(const edm::EventSetup &iSetup, bool isData)
reco::VertexCollection
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89287
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
MVAJetPuIdProducer::input_jet_token_
edm::EDGetTokenT< edm::View< reco::Jet > > input_jet_token_
Definition: MVAJetPuIdProducer.cc:44
cms::cuda::assert
assert(be >=bs)
EDProducer.h
Jet.h
MVAJetPuIdProducer::algos_
std::vector< std::pair< std::string, MVAJetPuId * > > algos_
Definition: MVAJetPuIdProducer.cc:37
jets_cff.vertexes
vertexes
Definition: jets_cff.py:700
FactorizedJetCorrector::getCorrection
float getCorrection()
Definition: FactorizedJetCorrector.cc:37
singleTopDQM_cfi.jets
jets
Definition: singleTopDQM_cfi.py:42
reco::LeafCandidate::pt
double pt() const final
transverse momentum
Definition: LeafCandidate.h:146
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
beam_dqm_sourceclient-live_cfg.mva
mva
Definition: beam_dqm_sourceclient-live_cfg.py:124
edm::Handle
Definition: AssociativeIterator.h:50
MVAJetPuId::computeMva
PileupJetIdentifier computeMva()
Definition: MVAJetPuId.cc:191
MVAJetPuIdProducer::inputIsCorrected_
bool inputIsCorrected_
Definition: MVAJetPuIdProducer.cc:36
FileInPath.h
PileupJetIdentifier.h
JetCorrectionsRecord.h
MVAJetPuIdProducer::rho_
edm::InputTag rho_
Definition: MVAJetPuIdProducer.cc:34
edm::FileInPath
Definition: FileInPath.h:64
MakerMacros.h
MVAJetPuId::set
void set(const PileupJetIdentifier &)
Definition: MVAJetPuId.cc:143
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Jet
Definition: Jet.py:1
jets_cff.algos
algos
Definition: jets_cff.py:700
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
MVAJetPuIdProducer::input_rho_token_
edm::EDGetTokenT< double > input_rho_token_
Definition: MVAJetPuIdProducer.cc:47
MVAJetPuId.h
JetCorrectorParameters.h
HLTBitAnalyser_cfi.isData
isData
Definition: HLTBitAnalyser_cfi.py:29
MVAJetPuIdProducer::residualsTxt_
edm::FileInPath residualsTxt_
Definition: MVAJetPuIdProducer.cc:40
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
DDAxes::rho
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
Vertex.h
edm::View
Definition: CaloClusterFwd.h:14
FactorizedJetCorrector
Definition: FactorizedJetCorrector.h:19
MVAJetPuIdProducer::applyJec_
bool applyJec_
Definition: MVAJetPuIdProducer.cc:36
edm::ParameterSet
Definition: ParameterSet.h:47
FactorizedJetCorrector::setRho
void setRho(float fRho)
Definition: FactorizedJetCorrector.cc:58
Event.h
reco::LeafCandidate::eta
double eta() const final
momentum pseudorapidity
Definition: LeafCandidate.h:152
MVAJetPuIdProducer::MVAJetPuIdProducer
MVAJetPuIdProducer(const edm::ParameterSet &)
Definition: MVAJetPuIdProducer.cc:50
iEvent
int iEvent
Definition: GenABIO.cc:224
MVAJetPuIdProducer::jetCorPars_
std::vector< JetCorrectorParameters > jetCorPars_
Definition: MVAJetPuIdProducer.cc:42
FactorizedJetCorrector.h
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::stream::EDProducer
Definition: EDProducer.h:38
PileupJetIdentifier
Definition: PileupJetIdentifier.h:74
edm::EventSetup
Definition: EventSetup.h:57
MVAJetPuIdProducer::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: MVAJetPuIdProducer.cc:88
MVAJetPuIdProducer::vertexes_
edm::InputTag vertexes_
Definition: MVAJetPuIdProducer.cc:34
ValueMap.h
StoredPileupJetIdentifier::jetEta
const float & jetEta() const
Definition: PileupJetIdentifier.h:31
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
reco::LeafCandidate::phi
double phi() const final
momentum azimuthal angle
Definition: LeafCandidate.h:148
MVAJetPuIdProducer::residualsFromTxt_
bool residualsFromTxt_
Definition: MVAJetPuIdProducer.cc:39
extraflags_cff.vtx
vtx
Definition: extraflags_cff.py:18
PileupJetIdentifier::jetPhi
const float & jetPhi() const
Definition: PileupJetIdentifier.h:96
FactorizedJetCorrector::setJetPt
void setJetPt(float fPt)
Definition: FactorizedJetCorrector.cc:48
Frameworkfwd.h
metsig::jet
Definition: SignAlgoResolutions.h:47
edm::ValueMap
Definition: ValueMap.h:107
MVAJetPuIdProducer::produceJetIds_
bool produceJetIds_
Definition: MVAJetPuIdProducer.cc:36
MVAJetPuIdProducer::input_vm_pujetid_token_
edm::EDGetTokenT< edm::ValueMap< StoredPileupJetIdentifier > > input_vm_pujetid_token_
Definition: MVAJetPuIdProducer.cc:46
MVAJetPuIdProducer::jec_
std::string jec_
Definition: MVAJetPuIdProducer.cc:35
FactorizedJetCorrector::setJetA
void setJetA(float fA)
Definition: FactorizedJetCorrector.cc:56
EventSetup.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
jetMETDQMOfflineSource_cff.jec
jec
Definition: jetMETDQMOfflineSource_cff.py:27
MVAJetPuIdProducer
Definition: MVAJetPuIdProducer.cc:23
edm::helper::Filler
Definition: ValueMap.h:22
StoredPileupJetIdentifier::jetPt
const float & jetPt() const
Definition: PileupJetIdentifier.h:32
ParameterSet.h
FactorizedJetCorrector::setJetEta
void setJetEta(float fEta)
Definition: FactorizedJetCorrector.cc:46
edm::Event
Definition: Event.h:73
MVAJetPuIdProducer::runMvas_
bool runMvas_
Definition: MVAJetPuIdProducer.cc:36
MVAJetPuId
Definition: MVAJetPuId.h:16
edm::InputTag
Definition: InputTag.h:15
jetMETDQMOfflineSource_cff.applyJec
applyJec
Definition: jetMETDQMOfflineSource_cff.py:28
label
const char * label
Definition: PFTauDecayModeTools.cc:11