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  if(!pfJetID(*it)) continue;
416 
417  corrFactor = pfJetCorr.product()->correction(*it);
418 
419  jet_data->etCorr.push_back(it->et()*corrFactor);
420  jet_data->corrFactor.push_back(corrFactor);
421 
422  nJets++;
423 
424  if (it->pt()*corrFactor > jetptThreshold_ && fabs(it->eta())<jetetaMax_) {
425  mHx += -1.*it->px()*corrFactor;
426  mHy += -1.*it->py()*corrFactor;
427  met_data->Ht += it->pt()*corrFactor;
428  }
429 
430  }
431 
432  TVector2 tv2 = TVector2(mHx,mHy);
433  met_data->mHt = tv2.Mod();
434  met_data->mHtPhi = tv2.Phi();
435 
436 
437 }
438 
439 
440 
441 
442 void
444 
445 
446  float caloCorrFactor = 1.;
447  unsigned int nCaloJets = 0;
448 
449  met_data->caloHt = 0;
450 
451  for( auto it=caloJets->begin();
452  it!=caloJets->end() && nCaloJets < maxJet_;
453  ++it) {
454 
455  if(!caloJetIDMissing_)
456  if(!caloJetID(*it)) continue;
457 
458  caloCorrFactor = caloJetCorr.product()->correction(*it);
459 
460  jet_data->caloEtCorr.push_back(it->et()*caloCorrFactor);
461  jet_data->caloCorrFactor.push_back(caloCorrFactor);
462 
463  nCaloJets++;
464 
465  if (it->pt()*caloCorrFactor > jetptThreshold_ && fabs(it->eta())<jetetaMax_) {
466  met_data->caloHt += it->pt()*caloCorrFactor;
467  }
468 
469  }
470 }
471 
472 void
474 
475  const reco::PFMETCollection *metCol = pfMet.product();
476  const reco::PFMET theMet = metCol->front();
477 
478  met_data->met = theMet.et();
479  met_data->metPhi = theMet.phi();
480  met_data->sumEt = theMet.sumEt();
481  met_data->metPx = theMet.px();
482  met_data->metPy = theMet.py();
483 
484 }
485 
486 void
488 
489  const reco::PFMETCollection *metCol = pfMet.product();
490  const reco::PFMET theMet = metCol->front();
491  reco::PFMET thePFMetNoMu = metCol->front();
492 
493  double pfMetNoMuPx = theMet.px();
494  double pfMetNoMuPy = theMet.py();
495 
496  double muPx(0.), muPy(0.);
497 
498  for( auto it=muons->begin();
499  it!=muons->end(); ++it) {
500  if(it->isPFMuon()){
501  muPx += it->px();
502  muPy += it->py();
503  }
504  }
505 
506  pfMetNoMuPx += muPx;
507  pfMetNoMuPy += muPy;
508 
509  math::XYZTLorentzVector pfMetNoMuP4(pfMetNoMuPx,pfMetNoMuPy,0,hypot(pfMetNoMuPx,pfMetNoMuPy));
510 
511 
512  thePFMetNoMu.setP4(pfMetNoMuP4);
513 
514  met_data->pfMetNoMu = thePFMetNoMu.et();
515  met_data->pfMetNoMuPhi = thePFMetNoMu.phi();
516  met_data->pfMetNoMuPx = thePFMetNoMu.px();
517  met_data->pfMetNoMuPy = thePFMetNoMu.py();
518 
519 }
520 
521 
522 void
524 
525  const reco::CaloMETCollection *metCol = caloMet.product();
526  const reco::CaloMET theMet = metCol->front();
527 
528  met_data->caloMet = theMet.et();
529  met_data->caloMetPhi = theMet.phi();
530  met_data->caloSumEt = theMet.sumEt();
531 
532 }
533 
534 void
536 
537  const reco::CaloMETCollection *metCol = caloMetBE.product();
538  const reco::CaloMET theMet = metCol->front();
539 
540  met_data->caloMetBE = theMet.et();
541  met_data->caloMetPhiBE = theMet.phi();
542  met_data->caloSumEtBE = theMet.sumEt();
543 
544 }
545 
546 bool
548 
549  bool tmp = true;
550  if (fabs(jet.eta()) < 2.7) {
551  tmp &= jet.neutralHadronEnergyFraction() < 0.9 ;
552  tmp &= jet.neutralEmEnergyFraction() < 0.9 ;
553  tmp &= (jet.chargedMultiplicity() + jet.neutralMultiplicity()) > 1 ;
554  tmp &= jet.muonEnergyFraction() < 0.8 ;
555  tmp &= jet.chargedHadronEnergyFraction() > 0.0 ;
556  tmp &= jet.chargedMultiplicity() > 0 ;
557  tmp &= jet.chargedEmEnergyFraction() < 0.9 ;
558  }
559  if (fabs(jet.eta()) > 2.7 && fabs(jet.eta()) < 3.0){
560  tmp &= jet.neutralEmEnergyFraction() > 0.01 ;
561  tmp &= jet.neutralHadronEnergyFraction() < 0.98 ;
562  tmp &= jet.neutralMultiplicity() > 2 ;
563  }
564  if (fabs(jet.eta()) > 3.0) {
565  tmp &= jet.neutralEmEnergyFraction() < 0.9 ;
566  tmp &= jet.neutralMultiplicity() > 10 ;
567  }
568 
569  // our custom selection
570  //tmp &= jet.muonMultiplicity() == 0;
571  //tmp &= jet.electronMultiplicity() == 0;
572 
573  return tmp;
574 
575 }
576 
577 bool
579 
580  bool tmp = true;
581 
582 
583  return tmp;
584 
585 }
586 
587 
588 // ------------ method called once each job just before starting event loop ------------
589 void
591 {
592 }
593 
594 // ------------ method called once each job just after ending the event loop ------------
595 void
597 }
598 
599 //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:579
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
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_