CMS 3D CMS Logo

PatTauAnalyzer.cc
Go to the documentation of this file.
11 
12 #include <TH1.h>
13 #include <TMath.h>
14 
15 #include <string>
16 
17 class PatTauAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
18 public:
19  explicit PatTauAnalyzer(const edm::ParameterSet&);
20  ~PatTauAnalyzer() override;
21 
22  //--- methods inherited from EDAnalyzer base-class
23  void beginJob() override;
24  void analyze(const edm::Event&, const edm::EventSetup&) override;
25  void endJob() override;
26 
27 private:
28  //--- configuration parameters
31 
33 
37 
38  //--- generator level histograms
40  TH1* hGenTauPt_;
43 
44  //--- reconstruction level histograms
46  TH1* hTauJetPt_;
49 
51 
53 
56 
61 
66 
71 };
72 
73 const reco::GenParticle* getGenTau(const pat::Tau& patTau) {
74  std::vector<reco::GenParticleRef> associatedGenParticles = patTau.genParticleRefs();
75  for (std::vector<reco::GenParticleRef>::const_iterator it = associatedGenParticles.begin();
76  it != associatedGenParticles.end();
77  ++it) {
78  if (it->isAvailable()) {
79  const reco::GenParticleRef& genParticle = (*it);
80  if (genParticle->pdgId() == -15 || genParticle->pdgId() == +15)
81  return genParticle.get();
82  }
83  }
84 
85  return nullptr;
86 }
87 
89  //std::cout << "<PatTauAnalyzer::PatTauAnalyzer>:" << std::endl;
90  usesResource(TFileService::kSharedResource);
91 
92  //--- read name of pat::Tau collection
93  src_ = cfg.getParameter<edm::InputTag>("src");
94  //std::cout << " src = " << src_ << std::endl;
95  srcToken_ = consumes<pat::TauCollection>(src_);
96 
97  //--- fill histograms for all tau-jet candidates or for "real" taus only ?
98  requireGenTauMatch_ = cfg.getParameter<bool>("requireGenTauMatch");
99  //std::cout << " requireGenTauMatch = " << requireGenTauMatch_ << std::endl;
100 
101  //--- read names of tau id. discriminators
102  discrByLeadTrack_ = cfg.getParameter<std::string>("discrByLeadTrack");
103  //std::cout << " discrByLeadTrack = " << discrByLeadTrack_ << std::endl;
104 
105  discrByIso_ = cfg.getParameter<std::string>("discrByIso");
106  //std::cout << " discrByIso = " << discrByIso_ << std::endl;
107 
108  discrByTaNC_ = cfg.getParameter<std::string>("discrByTaNC");
109  //std::cout << " discrByTaNC = " << discrByTaNC_ << std::endl;
110 }
111 
113  //std::cout << "<PatTauAnalyzer::~PatTauAnalyzer>:" << std::endl;
114 
115  //--- clean-up memory;
116  // delete all histograms
117  /*
118  deletion of histograms taken care of by TFileService;
119  do not delete them here (if the histograms are deleted here,
120  they will not appear in the ROOT file written by TFileService)
121 
122  delete hGenTauEnergy_;
123  delete hGenTauPt_;
124  delete hGenTauEta_;
125  delete hGenTauPhi_;
126  delete hTauJetEnergy_;
127  delete hTauJetPt_;
128  delete hTauJetEta_;
129  delete hTauJetPhi_;
130  delete hNumTauJets_;
131  delete hTauLeadTrackPt_;
132  delete hTauNumSigConeTracks_;
133  delete hTauNumIsoConeTracks_;
134  delete hTauDiscrByIso_;
135  delete hTauDiscrByTaNC_;
136  delete hTauDiscrAgainstElectrons_;
137  delete hTauDiscrAgainstMuons_;
138  delete hTauJetEnergyIsoPassed_;
139  delete hTauJetPtIsoPassed_;
140  delete hTauJetEtaIsoPassed_;
141  delete hTauJetPhiIsoPassed_;
142  delete hTauJetEnergyTaNCpassed_;
143  delete hTauJetPtTaNCpassed_;
144  delete hTauJetEtaTaNCpassed_;
145  delete hTauJetPhiTaNCpassed_;
146  */
147 }
148 
150  //--- retrieve handle to auxiliary service
151  // used for storing histograms into ROOT file
153 
154  //--- book generator level histograms
155  hGenTauEnergy_ = fs->make<TH1F>("GenTauEnergy", "GenTauEnergy", 30, 0., 150.);
156  hGenTauPt_ = fs->make<TH1F>("GenTauPt", "GenTauPt", 30, 0., 150.);
157  hGenTauEta_ = fs->make<TH1F>("GenTauEta", "GenTauEta", 24, -3., +3.);
158  hGenTauPhi_ = fs->make<TH1F>("GenTauPhi", "GenTauPhi", 18, -TMath::Pi(), +TMath::Pi());
159 
160  //--- book reconstruction level histograms
161  // for tau-jet Energy, Pt, Eta, Phi
162  hTauJetEnergy_ = fs->make<TH1F>("TauJetEnergy", "TauJetEnergy", 30, 0., 150.);
163  hTauJetPt_ = fs->make<TH1F>("TauJetPt", "TauJetPt", 30, 0., 150.);
164  //
165  // TO-DO: add histograms for eta and phi of the tau-jet candidate
166  //
167  // NOTE:
168  // 1.) please use
169  // "TauJetEta" and "TauJetPhi"
170  // for the names of the histograms and choose the exact same binning
171  // as is used for the histograms
172  // "TauJetEtaIsoPassed" and "TauJetPhiIsoPassed"
173  // below
174  //
175  // 2.) please check the histograms
176  // hTauJetEta_ and hTauJetPt_
177  // have already been defined in PatTauAnalyzer.h
178  //
179  //hTauJetEta_ =...
180  //hTauJetPt_ =...
181 
182  //... for number of tau-jet candidates
183  hNumTauJets_ = fs->make<TH1F>("NumTauJets", "NumTauJets", 10, -0.5, 9.5);
184 
185  //... for Pt of highest Pt track within signal cone tau-jet...
186  hTauLeadTrackPt_ = fs->make<TH1F>("TauLeadTrackPt", "TauLeadTrackPt", 40, 0., 100.);
187 
188  //... for total number of tracks within signal/isolation cones
189  hTauNumSigConeTracks_ = fs->make<TH1F>("TauNumSigConeTracks", "TauNumSigConeTracks", 10, -0.5, 9.5);
190  hTauNumIsoConeTracks_ = fs->make<TH1F>("TauNumIsoConeTracks", "TauNumIsoConeTracks", 20, -0.5, 19.5);
191 
192  //... for values of tau id. discriminators based on track isolation cut/
193  // neural network-based tau id.
194  hTauDiscrByIso_ = fs->make<TH1F>("TauDiscrByIso", "TauDiscrByIso", 103, -0.015, 1.015);
195  hTauDiscrByTaNC_ = fs->make<TH1F>("TauDiscrByTaNC", "TauDiscrByTaNC", 103, -0.015, 1.015);
196 
197  //... for values of tau id. discriminators against (unidentified) electrons and muons
199  fs->make<TH1F>("TauDiscrAgainstElectrons", "TauDiscrAgainstElectrons", 103, -0.015, 1.015);
200  hTauDiscrAgainstMuons_ = fs->make<TH1F>("TauDiscrAgainstMuons", "TauDiscrAgainstMuons", 103, -0.015, 1.015);
201 
202  //... for Energy, Pt, Eta, Phi of tau-jets passing the discriminatorByIsolation selection
203  hTauJetEnergyIsoPassed_ = fs->make<TH1F>("TauJetEnergyIsoPassed", "TauJetEnergyIsoPassed", 30, 0., 150.);
204  hTauJetPtIsoPassed_ = fs->make<TH1F>("TauJetPtIsoPassed", "TauJetPtIsoPassed", 30, 0., 150.);
205  hTauJetEtaIsoPassed_ = fs->make<TH1F>("TauJetEtaIsoPassed", "TauJetEtaIsoPassed", 24, -3., +3.);
206  hTauJetPhiIsoPassed_ = fs->make<TH1F>("TauJetPhiIsoPassed", "TauJetPhiIsoPassed", 18, -TMath::Pi(), +TMath::Pi());
207 
208  //... for Energy, Pt, Eta, Phi of tau-jets passing the discriminatorByTaNC ("Tau Neural Classifier") selection
209  hTauJetEnergyTaNCpassed_ = fs->make<TH1F>("TauJetEnergyTaNCpassed", "TauJetEnergyTaNCpassed", 30, 0., 150.);
210  hTauJetPtTaNCpassed_ = fs->make<TH1F>("TauJetPtTaNCpassed", "TauJetPtTaNCpassed", 30, 0., 150.);
211  hTauJetEtaTaNCpassed_ = fs->make<TH1F>("TauJetEtaTaNCpassed", "TauJetEtaTaNCpassed", 24, -3., +3.);
212  hTauJetPhiTaNCpassed_ = fs->make<TH1F>("TauJetPhiTaNCpassed", "TauJetPhiTaNCpassed", 18, -TMath::Pi(), +TMath::Pi());
213 }
214 
216  //std::cout << "<PatTauAnalyzer::analyze>:" << std::endl;
217 
220 
221  hNumTauJets_->Fill(patTaus->size());
222 
223  for (pat::TauCollection::const_iterator patTau = patTaus->begin(); patTau != patTaus->end(); ++patTau) {
224  //--- skip fake taus in case configuration parameters set to do so...
225  const reco::GenParticle* genTau = getGenTau(*patTau);
226  if (requireGenTauMatch_ && !genTau)
227  continue;
228 
229  //--- fill generator level histograms
230  if (genTau) {
231  hGenTauEnergy_->Fill(genTau->energy());
232  hGenTauPt_->Fill(genTau->pt());
233  hGenTauEta_->Fill(genTau->eta());
234  hGenTauPhi_->Fill(genTau->phi());
235  }
236 
237  //--- fill reconstruction level histograms
238  // for Pt of highest Pt track within signal cone tau-jet...
239  hTauJetEnergy_->Fill(patTau->energy());
240  hTauJetPt_->Fill(patTau->pt());
241  //
242  // TO-DO:
243  // 1.) fill histograms
244  // hTauJetEta_ and hTauJetPhi_
245  // with the pseudo-rapidity and azimuthal angle
246  // of the tau-jet candidate respectively
247  // hTauJetEta_->...
248  // hTauJetPhi_->...
249  //
250  // 2.) fill histogram
251  // hTauLeadTrackPt_
252  // with the transverse momentum of the highest Pt ("leading") track within the tau-jet
253  //
254  // NOTE:
255  // 1.) please have a look at
256  // http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/CMSSW/DataFormats/Candidate/interface/Particle.h?revision=1.28&view=markup
257  // to find the methods for accessing eta and phi of the tau-jet
258  //
259  // 2.) please have a look at
260  // http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/CMSSW/DataFormats/PatCandidates/interface/Tau.h?revision=1.25&view=markup
261  // to find the method for accessing the leading track
262  //
263  // 3.) the method pat::Tau::leadTrack returns a reference (reco::TrackRef) to a reco::Track object
264  // this reference can be null (in case no high Pt track has been reconstructed within the tau-jet),
265  // so a check if the leadTrack exists is needed before dereferencing the reco::TrackRef via operator->
266  //
267  // if ( patTau->leadTrack().isAvailable() ) hTauLeadTrackPt_->Fill(patTau->leadTrack()->pt());
268 
269  //... for total number of tracks within signal/isolation cones
270  hTauNumSigConeTracks_->Fill(patTau->signalTracks().size());
271  hTauNumIsoConeTracks_->Fill(patTau->isolationTracks().size());
272 
273  //... for values of tau id. discriminators based on track isolation cut/
274  // neural network-based tau id.
275  // (combine with requirement of at least one "leading" track of Pt > 5. GeV
276  // within the signal cone of the tau-jet)
277  float discrByIso = (patTau->tauID(discrByLeadTrack_.data()) > 0.5) ? patTau->tauID(discrByIso_.data()) : 0.;
278  hTauDiscrByIso_->Fill(discrByIso);
279  float discrByTaNC = (patTau->tauID(discrByLeadTrack_.data()) > 0.5) ? patTau->tauID(discrByTaNC_.data()) : 0.;
280  hTauDiscrByTaNC_->Fill(discrByTaNC);
281 
282  //... for values of tau id. discriminators against (unidentified) electrons and muons
283  //
284  // TO-DO: fill histogram
285  // hTauDiscrAgainstElectrons_
286  // with the value of the discriminatorAgainstElectronsLoose
287  //
288  // NOTE:
289  // 1.) please have a look at
290  // http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/CMSSW/DataFormats/PatCandidates/interface/Tau.h?revision=1.25&view=markup
291  // to find the method for accessing the tau id. information
292  //
293  // 2.) please have a look at
294  // http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/CMSSW/PhysicsTools/PatAlgos/python/tools/tauTools.py?revision=1.43&view=markup
295  // and convince yourself that the string "againstElectronLoose" needs to be passed as argument
296  // of the pat::Tau::tauID method
297  //
298  // hTauDiscrAgainstElectrons_->Fill...
299  hTauDiscrAgainstMuons_->Fill(patTau->tauID("againstMuonLoose"));
300 
301  //... for Energy, Pt, Eta, Phi of tau-jets passing the discriminatorByIsolation selection
302  if (discrByIso > 0.5) {
303  hTauJetEnergyIsoPassed_->Fill(patTau->energy());
304  hTauJetPtIsoPassed_->Fill(patTau->pt());
305  hTauJetEtaIsoPassed_->Fill(patTau->eta());
306  hTauJetPhiIsoPassed_->Fill(patTau->phi());
307  }
308 
309  //... for Energy, Pt, Eta, Phi of tau-jets passing the discriminatorByTaNC ("Tau Neural Classifier") selection
310  if (discrByTaNC > 0.5) {
311  hTauJetEnergyTaNCpassed_->Fill(patTau->energy());
312  hTauJetPtTaNCpassed_->Fill(patTau->pt());
313  hTauJetEtaTaNCpassed_->Fill(patTau->eta());
314  hTauJetPhiTaNCpassed_->Fill(patTau->phi());
315  }
316  }
317 }
318 
320  //--- nothing to be done yet...
321 }
322 
324 
static const std::string kSharedResource
Definition: TFileService.h:76
const double Pi
TH1 * hTauDiscrAgainstMuons_
~PatTauAnalyzer() override
std::string discrByLeadTrack_
double pt() const final
transverse momentum
TH1 * hTauNumIsoConeTracks_
const reco::GenParticle * getGenTau(const pat::Tau &patTau)
PatTauAnalyzer(const edm::ParameterSet &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:540
TH1 * hTauDiscrAgainstElectrons_
TH1 * hTauJetEnergyIsoPassed_
void beginJob() override
std::string discrByIso_
TH1 * hTauJetPtTaNCpassed_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TH1 * hTauJetEnergyTaNCpassed_
Analysis-level tau class.
Definition: Tau.h:53
TH1 * hTauJetPtIsoPassed_
edm::InputTag src_
TH1 * hTauNumSigConeTracks_
std::string discrByTaNC_
void endJob() override
TH1 * hTauJetPhiTaNCpassed_
void analyze(const edm::Event &, const edm::EventSetup &) override
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:232
edm::EDGetTokenT< pat::TauCollection > srcToken_
TH1 * hTauJetEtaIsoPassed_
TH1 * hTauJetPhiIsoPassed_
double phi() const final
momentum azimuthal angle
std::vector< reco::GenParticleRef > genParticleRefs() const
Definition: PATObject.h:779
TH1 * hTauJetEtaTaNCpassed_
double energy() const final
energy
double eta() const final
momentum pseudorapidity