CMS 3D CMS Logo

L1JetRecoTreeProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1Trigger/L1TNtuples
4 // Class: L1JetRecoTreeProducer
5 //
14 // system include files
15 #include <memory>
16 
17 // framework
26 
27 // cond formats
31 
32 // data formats
42 
43 
44 // ROOT output stuff
47 #include "TH1.h"
48 #include "TTree.h"
49 #include "TF1.h"
50 #include <TVector2.h>
51 
52 //local data formats
55 
56 //
57 // class declaration
58 //
59 
61 public:
63  ~L1JetRecoTreeProducer() override;
64 
65 
66 private:
67  void beginJob(void) override ;
68  void analyze(const edm::Event&, const edm::EventSetup&) override;
69  void endJob() override;
70 
77 
80 
81  bool pfJetID(const reco::PFJet& jet);
82  bool caloJetID(const reco::CaloJet& jet);
83 
84 public:
87 
88 private:
89 
90  // output file
92 
93  // tree
94  TTree * tree_;
95 
96  // EDM input tags
102 
106 
108 
109  // debug stuff
112  double jetetaMax_;
113  unsigned int maxCl_;
114  unsigned int maxJet_;
115  unsigned int maxVtx_;
116  unsigned int maxTrk_;
117 
125 
127 };
128 
129 
140 {
141 
142  caloJetToken_ = consumes<reco::CaloJetCollection>(iConfig.getUntrackedParameter("caloJetToken",edm::InputTag("ak4CaloJets")));
143  pfJetToken_ = consumes<reco::PFJetCollection>(iConfig.getUntrackedParameter("pfJetToken",edm::InputTag("ak4PFJetsCHS")));
144  caloJetIDToken_ = consumes<edm::ValueMap<reco::JetID> >(iConfig.getUntrackedParameter("caloJetIDToken",edm::InputTag("ak4JetID")));
145  pfJECToken_ = consumes<reco::JetCorrector>(iConfig.getUntrackedParameter<edm::InputTag>("pfJECToken",edm::InputTag("ak4PFCHSL1FastL2L3ResidualCorrector")));
146  caloJECToken_ = consumes<reco::JetCorrector>(iConfig.getUntrackedParameter<edm::InputTag>("caloJECToken",edm::InputTag("ak4CaloL1FastL2L3ResidualCorrector")));
147 
148  pfMetToken_ = consumes<reco::PFMETCollection>(iConfig.getUntrackedParameter("pfMetToken",edm::InputTag("pfMetT1")));
149  caloMetToken_ = consumes<reco::CaloMETCollection>(iConfig.getUntrackedParameter("caloMetToken",edm::InputTag("caloMet")));
150  caloMetBEToken_ = consumes<reco::CaloMETCollection>(iConfig.getUntrackedParameter("caloMetBEToken",edm::InputTag("caloMetBE")));
151 
152  muonToken_ = consumes<reco::MuonCollection>(iConfig.getUntrackedParameter("muonToken",edm::InputTag("muons")));
153 
154  jetptThreshold_ = iConfig.getParameter<double> ("jetptThreshold");
155  jetetaMax_ = iConfig.getParameter<double> ("jetetaMax");
156  maxJet_ = iConfig.getParameter<unsigned int>("maxJet");
157 
160 
161  // set up output
162  tree_=fs_->make<TTree>("JetRecoTree", "JetRecoTree");
163  tree_->Branch("Jet", "L1Analysis::L1AnalysisRecoJetDataFormat", &jet_data, 32000, 3);
164  tree_->Branch("Sums", "L1Analysis::L1AnalysisRecoMetDataFormat", &met_data, 32000, 3);
165 
166 }
167 
168 
170 {
171 
172  // do anything here that needs to be done at desctruction time
173  // (e.g. close files, deallocate resources etc.)
174 
175 }
176 
177 
178 //
179 // member functions
180 //
181 
182 // ------------ method called to for each event ------------
184 {
185 
186  jet_data->Reset();
187  met_data->Reset();
188 
189  // get jets
191  iEvent.getByToken(pfJetToken_, pfJets);
192 
193  // get calo jets
195  iEvent.getByToken(caloJetToken_, caloJets);
196 
197  //get sums
199  iEvent.getByToken(pfMetToken_, pfMet);
200 
201  // get jet ID
203  iEvent.getByToken(caloJetIDToken_,jetsID);
204 
206  iEvent.getByToken(pfJECToken_, pfJetCorr);
207 
209  iEvent.getByToken(caloJECToken_, caloJetCorr);
210 
212  iEvent.getByToken(caloMetToken_, caloMet);
213 
215  iEvent.getByToken(caloMetBEToken_, caloMetBE);
216 
217  // get muons
219  iEvent.getByToken(muonToken_, muons);
220 
221 
222  if (pfJets.isValid()) {
223 
224  jet_data->nJets=0;
225 
226  doPFJets(pfJets);
227 
228  }
229  else {
230  if (!pfJetsMissing_) {edm::LogWarning("MissingProduct") << "PFJets not found. Branch will not be filled" << std::endl;}
231  pfJetsMissing_ = true;
232  }
233 
234  if (pfJetCorr.isValid()) {
235 
236  doPFJetCorr(pfJets,pfJetCorr);
237 
238  }
239  else {
240  if (!pfJetCorrMissing_) {edm::LogWarning("MissingProduct") << "PF Jet Corrector not found. Branch will not be filled" << std::endl;}
241  pfJetCorrMissing_ = true;
242  }
243 
244  if (caloJets.isValid()) {
245 
246  jet_data->nCaloJets=0;
247 
248  doCaloJets(caloJets);
249 
250  }
251  else {
252  if (!caloJetsMissing_) {edm::LogWarning("MissingProduct") << "Calo Jets not found. Branch will not be filled" << std::endl;}
253  caloJetsMissing_ = true;
254  }
255 
256  if (caloJetCorr.isValid()) {
257 
258  doCaloJetCorr(caloJets,caloJetCorr);
259 
260  }
261  else {
262  if (!caloJetCorrMissing_) {edm::LogWarning("MissingProduct") << "Calo Jet Corrector not found. Branch will not be filled" << std::endl;}
263  caloJetCorrMissing_ = true;
264  }
265 
266  if (!jetsID.isValid()){
267 
268  if (!caloJetIDMissing_) {edm::LogWarning("MissingProduct") << "Calo Jet ID not found. Branch will not be filled" << std::endl;}
269  caloJetIDMissing_ = true;
270  }
271 
272  if (pfMet.isValid()) {
273 
274  doPFMet(pfMet);
275 
276  if (muons.isValid()) {
277 
278  doPFMetNoMu(pfMet,muons);
279 
280  }
281  else {
282  if (!muonsMissing_) {edm::LogWarning("MissingProduct") << "Muons not found. PFMetNoMu branch will not be filled" << std::endl;}
283  muonsMissing_ = true;
284  }
285  }
286  else{
287  if (!pfMetMissing_) {edm::LogWarning("MissingProduct") << "PFMet not found. Branch will not be filled" << std::endl;}
288  pfMetMissing_ = true;
289  }
290 
291  if (caloMet.isValid()) {
292 
293  doCaloMet(caloMet);
294 
295  }
296  else {
297  if (!caloMetMissing_) {edm::LogWarning("MissingProduct") << "CaloMet not found. Branch will not be filled" << std::endl;}
298  caloMetMissing_ = true;
299  }
300 
301  if (caloMetBE.isValid()) {
302 
303  doCaloMetBE(caloMetBE);
304 
305  }
306  else {
307  if (!caloMetBEMissing_) {edm::LogWarning("MissingProduct") << "CaloMetBE not found. Branch will not be filled" << std::endl;}
308  caloMetBEMissing_ = true;
309  }
310 
311  tree_->Fill();
312 
313 }
314 
315 
316 void
318 
319  for( auto it=caloJets->begin();
320  it!=caloJets->end() && jet_data->nCaloJets < maxJet_;
321  ++it) {
322 
323  if(!caloJetIDMissing_)
324  if(!caloJetID(*it)) continue;
325 
326  jet_data->caloEt.push_back(it->et());
327  jet_data->caloEta.push_back(it->eta());
328  jet_data->caloPhi.push_back(it->phi());
329  jet_data->caloE.push_back(it->energy());
330 
331  jet_data->eEMF.push_back(it->emEnergyFraction());
332  jet_data->eEmEB.push_back(it->emEnergyInEB());
333  jet_data->eEmEE.push_back(it->emEnergyInEE());
334  jet_data->eEmHF.push_back(it->emEnergyInHF());
335  jet_data->eHadHB.push_back(it->hadEnergyInHB());
336  jet_data->eHadHE.push_back(it->hadEnergyInHE());
337  jet_data->eHadHO.push_back(it->hadEnergyInHO());
338  jet_data->eHadHF.push_back(it->hadEnergyInHF());
339  jet_data->eMaxEcalTow.push_back(it->maxEInEmTowers());
340  jet_data->eMaxHcalTow.push_back(it->maxEInHadTowers());
341  jet_data->towerArea.push_back(it->towersArea());
342  jet_data->n60.push_back(it->n60());
343 
344  jet_data->nCaloJets++;
345 
346  }
347 
348 
349 }
350 
351 
352 void
354 
355 
356  for( auto it=pfJets->begin();
357  it!=pfJets->end() && jet_data->nJets < maxJet_;
358  ++it) {
359 
360  if(!pfJetID(*it)) continue;
361 
362 
363  jet_data->et.push_back(it->et());
364  jet_data->eta.push_back(it->eta());
365  jet_data->phi.push_back(it->phi());
366  jet_data->e.push_back(it->energy());
367 
368  jet_data->chef.push_back(it->chargedHadronEnergyFraction());
369  jet_data->nhef.push_back(it->neutralHadronEnergyFraction());
370  jet_data->pef.push_back(it->photonEnergyFraction());
371  jet_data->eef.push_back(it->electronEnergyFraction());
372  jet_data->mef.push_back(it->muonEnergyFraction());
373  jet_data->hfhef.push_back(it->HFHadronEnergyFraction());
374  jet_data->hfemef.push_back(it->HFEMEnergyFraction());
375  jet_data->chMult.push_back(it->chargedHadronMultiplicity());
376  jet_data->nhMult.push_back(it->neutralHadronMultiplicity());
377  jet_data->phMult.push_back(it->photonMultiplicity());
378  jet_data->elMult.push_back(it->electronMultiplicity());
379  jet_data->muMult.push_back(it->muonMultiplicity());
380  jet_data->hfhMult.push_back(it->HFHadronMultiplicity());
381  jet_data->hfemMult.push_back(it->HFEMMultiplicity());
382 
383  jet_data->cemef.push_back(it->chargedEmEnergyFraction());
384  jet_data->cmef.push_back(it->chargedMuEnergyFraction());
385  jet_data->nemef.push_back(it->neutralEmEnergyFraction());
386  jet_data->cMult.push_back(it->chargedMultiplicity());
387  jet_data->nMult.push_back(it->neutralMultiplicity());
388 
389  jet_data->nJets++;
390 
391  }
392 
393 }
394 
395 
396 void
398 
399 
400  float corrFactor = 1.;
401  unsigned int nJets = 0;
402 
403  float mHx = 0;
404  float mHy = 0;
405 
406  met_data->Ht = 0;
407  met_data->mHt = -999.;
408  met_data->mHtPhi = -999.;
409 
410 
411  for( auto it=pfJets->begin();
412  it!=pfJets->end() && nJets < maxJet_;
413  ++it) {
414 
415  corrFactor = pfJetCorr.product()->correction(*it);
416 
417  jet_data->etCorr.push_back(it->et()*corrFactor);
418  jet_data->corrFactor.push_back(corrFactor);
419 
420  nJets++;
421 
422  if (it->pt()*corrFactor > jetptThreshold_ && fabs(it->eta())<jetetaMax_) {
423  mHx += -1.*it->px()*corrFactor;
424  mHy += -1.*it->py()*corrFactor;
425  met_data->Ht += it->pt()*corrFactor;
426  }
427 
428  }
429 
430  TVector2 tv2 = TVector2(mHx,mHy);
431  met_data->mHt = tv2.Mod();
432  met_data->mHtPhi = tv2.Phi();
433 
434 
435 }
436 
437 
438 
439 
440 void
442 
443 
444  float caloCorrFactor = 1.;
445  unsigned int nCaloJets = 0;
446 
447  met_data->caloHt = 0;
448 
449  for( auto it=caloJets->begin();
450  it!=caloJets->end() && nCaloJets < maxJet_;
451  ++it) {
452 
453  caloCorrFactor = caloJetCorr.product()->correction(*it);
454 
455  jet_data->caloEtCorr.push_back(it->et()*caloCorrFactor);
456  jet_data->caloCorrFactor.push_back(caloCorrFactor);
457 
458  nCaloJets++;
459 
460  if (it->pt()*caloCorrFactor > jetptThreshold_ && fabs(it->eta())<jetetaMax_) {
461  met_data->caloHt += it->pt()*caloCorrFactor;
462  }
463 
464  }
465 }
466 
467 void
469 
470  const reco::PFMETCollection *metCol = pfMet.product();
471  const reco::PFMET theMet = metCol->front();
472 
473  met_data->met = theMet.et();
474  met_data->metPhi = theMet.phi();
475  met_data->sumEt = theMet.sumEt();
476  met_data->metPx = theMet.px();
477  met_data->metPy = theMet.py();
478 
479 }
480 
481 void
483 
484  const reco::PFMETCollection *metCol = pfMet.product();
485  const reco::PFMET theMet = metCol->front();
486  reco::PFMET thePFMetNoMu = metCol->front();
487 
488  double pfMetNoMuPx = theMet.px();
489  double pfMetNoMuPy = theMet.py();
490 
491  double muPx(0.), muPy(0.);
492 
493  for( auto it=muons->begin();
494  it!=muons->end(); ++it) {
495  if(it->isPFMuon()){
496  muPx += it->px();
497  muPy += it->py();
498  }
499  }
500 
501  pfMetNoMuPx += muPx;
502  pfMetNoMuPy += muPy;
503 
504  math::XYZTLorentzVector pfMetNoMuP4(pfMetNoMuPx,pfMetNoMuPy,0,hypot(pfMetNoMuPx,pfMetNoMuPy));
505 
506 
507  thePFMetNoMu.setP4(pfMetNoMuP4);
508 
509  met_data->pfMetNoMu = thePFMetNoMu.et();
510  met_data->pfMetNoMuPhi = thePFMetNoMu.phi();
511  met_data->pfMetNoMuPx = thePFMetNoMu.px();
512  met_data->pfMetNoMuPy = thePFMetNoMu.py();
513 
514 }
515 
516 
517 void
519 
520  const reco::CaloMETCollection *metCol = caloMet.product();
521  const reco::CaloMET theMet = metCol->front();
522 
523  met_data->caloMet = theMet.et();
524  met_data->caloMetPhi = theMet.phi();
525  met_data->caloSumEt = theMet.sumEt();
526 
527 }
528 
529 void
531 
532  const reco::CaloMETCollection *metCol = caloMetBE.product();
533  const reco::CaloMET theMet = metCol->front();
534 
535  met_data->caloMetBE = theMet.et();
536  met_data->caloMetPhiBE = theMet.phi();
537  met_data->caloSumEtBE = theMet.sumEt();
538 
539 }
540 
541 bool
543 
544  bool tmp = true;
545  if (fabs(jet.eta()) < 2.7) {
546  tmp &= jet.neutralHadronEnergyFraction() < 0.9 ;
547  tmp &= jet.neutralEmEnergyFraction() < 0.9 ;
548  tmp &= (jet.chargedMultiplicity() + jet.neutralMultiplicity()) > 1 ;
549  tmp &= jet.muonEnergyFraction() < 0.8 ;
550  tmp &= jet.chargedHadronEnergyFraction() > 0.0 ;
551  tmp &= jet.chargedMultiplicity() > 0 ;
552  tmp &= jet.chargedEmEnergyFraction() < 0.9 ;
553  }
554  if (fabs(jet.eta()) > 2.7 && fabs(jet.eta()) < 3.0){
555  tmp &= jet.neutralEmEnergyFraction() > 0.01 ;
556  tmp &= jet.neutralHadronEnergyFraction() < 0.98 ;
557  tmp &= jet.neutralMultiplicity() > 2 ;
558  }
559  if (fabs(jet.eta()) > 3.0) {
560  tmp &= jet.neutralEmEnergyFraction() < 0.9 ;
561  tmp &= jet.neutralMultiplicity() > 10 ;
562  }
563 
564  // our custom selection
565  //tmp &= jet.muonMultiplicity() == 0;
566  //tmp &= jet.electronMultiplicity() == 0;
567 
568  return tmp;
569 
570 }
571 
572 bool
574 
575  bool tmp = true;
576 
577 
578  return tmp;
579 
580 }
581 
582 
583 // ------------ method called once each job just before starting event loop ------------
584 void
586 {
587 }
588 
589 // ------------ method called once each job just after ending the event loop ------------
590 void
592 }
593 
594 //define this as a plug-in
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
void doPFMet(edm::Handle< reco::PFMETCollection > pfMet)
bool pfJetID(const reco::PFJet &jet)
double eta() const final
momentum pseudorapidity
Jets made from CaloTowers.
Definition: CaloJet.h:29
L1Analysis::L1AnalysisRecoMetDataFormat * met_data
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
edm::Service< TFileService > fs_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
float chargedHadronEnergyFraction() const
chargedHadronEnergyFraction
Definition: PFJet.h:100
double px() const final
x coordinate of momentum vector
void doPFJetCorr(edm::Handle< reco::PFJetCollection > pfJets, edm::Handle< reco::JetCorrector > pfJetCorr)
edm::EDGetTokenT< reco::CaloMETCollection > caloMetToken_
void analyze(const edm::Event &, const edm::EventSetup &) override
void doPFJets(edm::Handle< reco::PFJetCollection > pfJets)
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
double correction(const LorentzVector &fJet) const
get correction using Jet information only
Definition: JetCorrector.h:49
caloMetBE
____________________________________________________________________________||
Definition: CaloMET_cfi.py:19
edm::EDGetTokenT< reco::JetCorrector > pfJECToken_
void doCaloMetBE(edm::Handle< reco::CaloMETCollection > caloMetBE)
int chargedMultiplicity() const
chargedMultiplicity
Definition: PFJet.h:155
Jets made from PFObjects.
Definition: PFJet.h:21
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
L1Analysis::L1AnalysisRecoJetDataFormat * jet_data
float neutralEmEnergyFraction() const
neutralEmEnergyFraction
Definition: PFJet.h:152
int iEvent
Definition: GenABIO.cc:230
double sumEt() const
Definition: MET.h:56
double et() const final
transverse energy
void beginJob(void) override
pfMet
____________________________________________________________________________||
Definition: PFMET_cfi.py:4
float neutralHadronEnergyFraction() const
neutralHadronEnergyFraction
Definition: PFJet.h:104
edm::EDGetTokenT< edm::ValueMap< reco::JetID > > caloJetIDToken_
edm::EDGetTokenT< reco::PFJetCollection > pfJetToken_
void doCaloJetCorr(edm::Handle< reco::CaloJetCollection > caloJets, edm::Handle< reco::JetCorrector > caloJetCorr)
bool isValid() const
Definition: HandleBase.h:74
caloMet
____________________________________________________________________________||
Definition: CaloMET_cfi.py:4
void doPFMetNoMu(edm::Handle< reco::PFMETCollection > pfMet, edm::Handle< reco::MuonCollection >)
void doCaloMet(edm::Handle< reco::CaloMETCollection > caloMet)
int neutralMultiplicity() const
neutralMultiplicity
Definition: PFJet.h:157
edm::EDGetTokenT< reco::PFMETCollection > pfMetToken_
float chargedEmEnergyFraction() const
chargedEmEnergyFraction
Definition: PFJet.h:144
std::vector< reco::CaloMET > CaloMETCollection
collection of CaloMET objects
T const * product() const
Definition: Handle.h:81
L1JetRecoTreeProducer(const edm::ParameterSet &)
double py() const final
y coordinate of momentum vector
void doCaloJets(edm::Handle< reco::CaloJetCollection > caloJets)
bool caloJetID(const reco::CaloJet &jet)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
edm::EDGetTokenT< reco::MuonCollection > muonToken_
edm::EDGetTokenT< reco::JetCorrector > caloJECToken_
std::vector< reco::PFMET > PFMETCollection
collection of PFMET objects
edm::EDGetTokenT< reco::CaloJetCollection > caloJetToken_
float muonEnergyFraction() const
muonEnergyFraction
Definition: PFJet.h:116
double phi() const final
momentum azimuthal angle
void setP4(const LorentzVector &p4) final
set 4-momentum
edm::EDGetTokenT< reco::CaloMETCollection > caloMetBEToken_