CMS 3D CMS Logo

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