CMS 3D CMS Logo

PFRecoTauDiscriminationAgainstElectronMVA5.cc
Go to the documentation of this file.
1 /* class PFRecoTauDiscriminationAgainstElectronMVA5
2  * created : Aug 13 2013,
3  * revised : ,
4  * Authorss : Ivo Naranjo (LLR Ecole-Polytechnique)
5  */
6 
9 
12 
20 
21 #include <TMath.h>
22 
23 #include <iostream>
24 #include <sstream>
25 #include <fstream>
26 
27 using namespace reco;
28 
30 {
31  public:
34  mva_(0),
35  category_output_()
36  {
37  mva_ = new AntiElectronIDMVA5(cfg);
38 
39  srcGsfElectrons_ = cfg.getParameter<edm::InputTag>("srcGsfElectrons");
40  GsfElectrons_token = consumes<reco::GsfElectronCollection>(srcGsfElectrons_);
41 
42  verbosity_ = ( cfg.exists("verbosity") ) ?
43  cfg.getParameter<int>("verbosity") : 0;
44 
45  // add category index
46  produces<PFTauDiscriminator>("category");
47  }
48 
49  void beginEvent(const edm::Event&, const edm::EventSetup&);
50 
51  double discriminate(const PFTauRef&) const;
52 
53  void endEvent(edm::Event&);
54 
56  {
57  delete mva_;
58  }
59 
60 private:
61  bool isInEcalCrack(double) const;
62 
64 
66  float* mvaInput_;
67 
72 
73  std::unique_ptr<PFTauDiscriminator> category_output_;
74 
76 };
77 
79 {
80  mva_->beginEvent(evt, es);
81 
82  evt.getByToken(Tau_token, taus_);
83  category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
84 
85  evt.getByToken(GsfElectrons_token, gsfElectrons_);
86 }
87 
89 {
90  double mvaValue = 1.;
91  double category = -1.;
92  bool isGsfElectronMatched = false;
93 
94  float deltaRDummy = 9.9;
95 
96  float tauEtaAtEcalEntrance = -99.;
97  float sumEtaTimesEnergy = 0.;
98  float sumEnergy = 0.;
99  const std::vector<reco::PFCandidatePtr>& signalPFCands = thePFTauRef->signalPFCands();
100  for ( std::vector<reco::PFCandidatePtr>::const_iterator pfCandidate = signalPFCands.begin();
101  pfCandidate != signalPFCands.end(); ++pfCandidate ) {
102  sumEtaTimesEnergy += ((*pfCandidate)->positionAtECALEntrance().eta()*(*pfCandidate)->energy());
103  sumEnergy += (*pfCandidate)->energy();
104  }
105  if ( sumEnergy > 0. ) {
106  tauEtaAtEcalEntrance = sumEtaTimesEnergy/sumEnergy;
107  }
108 
109  float leadChargedPFCandEtaAtEcalEntrance = -99.;
110  float leadChargedPFCandPt = -99.;
111  for ( std::vector<reco::PFCandidatePtr>::const_iterator pfCandidate = signalPFCands.begin();
112  pfCandidate != signalPFCands.end(); ++pfCandidate ) {
113  const reco::Track* track = 0;
114  if ( (*pfCandidate)->trackRef().isNonnull() ) track = (*pfCandidate)->trackRef().get();
115  else if ( (*pfCandidate)->muonRef().isNonnull() && (*pfCandidate)->muonRef()->innerTrack().isNonnull() ) track = (*pfCandidate)->muonRef()->innerTrack().get();
116  else if ( (*pfCandidate)->muonRef().isNonnull() && (*pfCandidate)->muonRef()->globalTrack().isNonnull() ) track = (*pfCandidate)->muonRef()->globalTrack().get();
117  else if ( (*pfCandidate)->muonRef().isNonnull() && (*pfCandidate)->muonRef()->outerTrack().isNonnull() ) track = (*pfCandidate)->muonRef()->outerTrack().get();
118  else if ( (*pfCandidate)->gsfTrackRef().isNonnull() ) track = (*pfCandidate)->gsfTrackRef().get();
119  if ( track ) {
120  if ( track->pt() > leadChargedPFCandPt ) {
121  leadChargedPFCandEtaAtEcalEntrance = (*pfCandidate)->positionAtECALEntrance().eta();
122  leadChargedPFCandPt = track->pt();
123  }
124  }
125  }
126 
127  if( (*thePFTauRef).leadPFChargedHadrCand().isNonnull()) {
128  for ( reco::GsfElectronCollection::const_iterator theGsfElectron = gsfElectrons_->begin();
129  theGsfElectron != gsfElectrons_->end(); ++theGsfElectron ) {
130  if ( theGsfElectron->pt() > 10. ) { // CV: only take electrons above some minimal energy/Pt into account...
131  double deltaREleTau = deltaR(theGsfElectron->p4(), thePFTauRef->p4());
132  deltaRDummy = deltaREleTau;
133  if ( deltaREleTau < 0.3 ) {
134  double mva_match = mva_->MVAValue(*thePFTauRef, *theGsfElectron);
135  size_t numSignalPFGammaCands = thePFTauRef->signalPFGammaCands().size();
136  bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
137 
139  if ( isInEcalCrack(tauEtaAtEcalEntrance) || isInEcalCrack(leadChargedPFCandEtaAtEcalEntrance) ) {
140  // add category index
141  category_output_->setValue(tauIndex_, category);
142  // return MVA output value
143  return -99;
144  }
146 
147  if ( TMath::Abs(tauEtaAtEcalEntrance) < 1.479 ) { // Barrel
148  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
149  category = 4.;
150  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
151  category = 5.;
152  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
153  category = 6.;
154  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
155  category = 7.;
156  }
157  } else { // Endcap
158  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
159  category = 12.;
160  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
161  category = 13.;
162  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
163  category = 14.;
164  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
165  category = 15.;
166  }
167  }
168 
169  mvaValue = TMath::Min(mvaValue, mva_match);
170  isGsfElectronMatched = true;
171  } // deltaR < 0.3
172  } // electron pt > 10
173  } // end of loop over electrons
174 
175  if ( !isGsfElectronMatched ) {
176  mvaValue = mva_->MVAValue(*thePFTauRef);
177  size_t numSignalPFGammaCands = thePFTauRef->signalPFGammaCands().size();
178  bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
179 
181  if ( isInEcalCrack(tauEtaAtEcalEntrance) || isInEcalCrack(leadChargedPFCandEtaAtEcalEntrance) ) {
182  // add category index
183  category_output_->setValue(tauIndex_, category);
184  // return MVA output value
185  return -99;
186  }
188 
189  if ( TMath::Abs(tauEtaAtEcalEntrance) < 1.479 ) { // Barrel
190  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
191  category = 0.;
192  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
193  category = 1.;
194  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
195  category = 2.;
196  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
197  category = 3.;
198  }
199  } else { // Endcap
200  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
201  category = 8.;
202  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
203  category = 9.;
204  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
205  category = 10.;
206  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
207  category = 11.;
208  }
209  }
210  }
211  }
212 
213  if ( verbosity_ ) {
214  edm::LogPrint("PFTauAgainstEleMVA5") << "<PFRecoTauDiscriminationAgainstElectronMVA5::discriminate>:" ;
215  edm::LogPrint("PFTauAgainstEleMVA5") << " tau: Pt = " << thePFTauRef->pt() << ", eta = " << thePFTauRef->eta() << ", phi = " << thePFTauRef->phi();
216  edm::LogPrint("PFTauAgainstEleMVA5") << " deltaREleTau = " << deltaRDummy << ", isGsfElectronMatched = " << isGsfElectronMatched;
217  edm::LogPrint("PFTauAgainstEleMVA5") << " #Prongs = " << thePFTauRef->signalPFChargedHadrCands().size();
218  edm::LogPrint("PFTauAgainstEleMVA5") << " MVA = " << mvaValue << ", category = " << category;
219  }
220 
221  // add category index
222  category_output_->setValue(tauIndex_, category);
223  // return MVA output value
224  return mvaValue;
225 }
226 
228 {
229  // add all category indices to event
230  evt.put(std::move(category_output_), "category");
231 }
232 
233 bool
235 {
236  double absEta = fabs(eta);
237  return (absEta > 1.460 && absEta < 1.558);
238 }
239 
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
edm::RefProd< TauCollection > TauRefProd
Definition: Tau.h:39
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:252
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
T Min(T a, T b)
Definition: MathUtil.h:39
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:646
double pt() const
track transverse momentum
Definition: TrackBase.h:616
T Abs(T a)
Definition: MathUtil.h:49
auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
void beginEvent(const edm::Event &, const edm::EventSetup &)
fixed size matrix
edm::EDGetTokenT< reco::GsfElectronCollection > GsfElectrons_token
def move(src, dest)
Definition: eostools.py:510