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_(nullptr),
33  category_output_()
34  {
35  mva_ = new AntiElectronIDMVA6(cfg);
36 
37  usePhiAtEcalEntranceExtrapolation_ = cfg.getParameter<bool>("usePhiAtEcalEntranceExtrapolation");
38  srcGsfElectrons_ = cfg.getParameter<edm::InputTag>("srcGsfElectrons");
39  GsfElectrons_token = consumes<reco::GsfElectronCollection>(srcGsfElectrons_);
40 
41  verbosity_ = ( cfg.exists("verbosity") ) ?
42  cfg.getParameter<int>("verbosity") : 0;
43 
44  // add category index
45  produces<PFTauDiscriminator>("category");
46  }
47 
48  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
49 
50  double discriminate(const PFTauRef&) const override;
51 
52  void endEvent(edm::Event&) override;
53 
55  {
56  delete mva_;
57  }
58 
59 private:
60  bool isInEcalCrack(double) const;
61 
63 
65  float* mvaInput_;
66 
71 
72  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  const float ECALBarrelEndcapEtaBorder = 1.479;
97  float tauEtaAtEcalEntrance = -99.;
98  float sumEtaTimesEnergy = 0.;
99  float sumEnergy = 0.;
100  const std::vector<reco::PFCandidatePtr>& signalPFCands = thePFTauRef->signalPFCands();
101  for ( std::vector<reco::PFCandidatePtr>::const_iterator pfCandidate = signalPFCands.begin();
102  pfCandidate != signalPFCands.end(); ++pfCandidate ) {
103  sumEtaTimesEnergy += ((*pfCandidate)->positionAtECALEntrance().eta()*(*pfCandidate)->energy());
104  sumEnergy += (*pfCandidate)->energy();
105  }
106  if ( sumEnergy > 0. ) {
107  tauEtaAtEcalEntrance = sumEtaTimesEnergy/sumEnergy;
108  }
109 
110  float leadChargedPFCandEtaAtEcalEntrance = -99.;
111  float leadChargedPFCandPt = -99.;
112  for ( std::vector<reco::PFCandidatePtr>::const_iterator pfCandidate = signalPFCands.begin();
113  pfCandidate != signalPFCands.end(); ++pfCandidate ) {
114  const reco::Track* track = nullptr;
115  if ( (*pfCandidate)->trackRef().isNonnull() ) track = (*pfCandidate)->trackRef().get();
116  else if ( (*pfCandidate)->muonRef().isNonnull() && (*pfCandidate)->muonRef()->innerTrack().isNonnull() ) track = (*pfCandidate)->muonRef()->innerTrack().get();
117  else if ( (*pfCandidate)->muonRef().isNonnull() && (*pfCandidate)->muonRef()->globalTrack().isNonnull() ) track = (*pfCandidate)->muonRef()->globalTrack().get();
118  else if ( (*pfCandidate)->muonRef().isNonnull() && (*pfCandidate)->muonRef()->outerTrack().isNonnull() ) track = (*pfCandidate)->muonRef()->outerTrack().get();
119  else if ( (*pfCandidate)->gsfTrackRef().isNonnull() ) track = (*pfCandidate)->gsfTrackRef().get();
120  if ( track ) {
121  if ( track->pt() > leadChargedPFCandPt ) {
122  leadChargedPFCandEtaAtEcalEntrance = (*pfCandidate)->positionAtECALEntrance().eta();
123  leadChargedPFCandPt = track->pt();
124  }
125  }
126  }
127 
128  if( (*thePFTauRef).leadPFChargedHadrCand().isNonnull()) {
129 
130  int numSignalPFGammaCandsInSigCone = 0;
131  const std::vector<reco::PFCandidatePtr>& signalPFGammaCands = thePFTauRef->signalPFGammaCands();
132 
133  for ( std::vector<reco::PFCandidatePtr>::const_iterator pfGamma = signalPFGammaCands.begin();
134  pfGamma != signalPFGammaCands.end(); ++pfGamma ) {
135 
136  double dR = deltaR((*pfGamma)->p4(), thePFTauRef->leadPFChargedHadrCand()->p4());
137  double signalrad = std::max(0.05, std::min(0.10, 3.0/std::max(1.0, thePFTauRef->pt())));
138 
139  // pfGammas inside the tau signal cone
140  if (dR < signalrad) {
141  numSignalPFGammaCandsInSigCone += 1;
142  }
143  }
144 
145  // loop over the electrons
146  for ( reco::GsfElectronCollection::const_iterator theGsfElectron = gsfElectrons_->begin();
147  theGsfElectron != gsfElectrons_->end(); ++theGsfElectron ) {
148  if ( theGsfElectron->pt() > 10. ) { // CV: only take electrons above some minimal energy/Pt into account...
149  double deltaREleTau = deltaR(theGsfElectron->p4(), thePFTauRef->p4());
150  deltaRDummy = deltaREleTau;
151  if ( deltaREleTau < 0.3 ) {
152  double mva_match = mva_->MVAValue(*thePFTauRef, *theGsfElectron, usePhiAtEcalEntranceExtrapolation_);
153  bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
154  if ( !hasGsfTrack )
155  hasGsfTrack = theGsfElectron->gsfTrack().isNonnull();
156 
158  if ( isInEcalCrack(tauEtaAtEcalEntrance) || isInEcalCrack(leadChargedPFCandEtaAtEcalEntrance) ) {
159  // add category index
160  category_output_->setValue(tauIndex_, category);
161  // return MVA output value
162  return -99;
163  }
165 
166  if ( std::abs(tauEtaAtEcalEntrance) < ECALBarrelEndcapEtaBorder ) { // Barrel
167  if ( numSignalPFGammaCandsInSigCone == 0 && hasGsfTrack ) {
168  category = 5.;
169  }
170  else if ( numSignalPFGammaCandsInSigCone >= 1 && hasGsfTrack ) {
171  category = 7.;
172  }
173  } else { // Endcap
174  if ( numSignalPFGammaCandsInSigCone == 0 && hasGsfTrack ) {
175  category = 13.;
176  }
177  else if ( numSignalPFGammaCandsInSigCone >= 1 && hasGsfTrack ) {
178  category = 15.;
179  }
180  }
181 
182  mvaValue = std::min(mvaValue, mva_match);
183  isGsfElectronMatched = true;
184  } // deltaR < 0.3
185  } // electron pt > 10
186  } // end of loop over electrons
187 
188  if ( !isGsfElectronMatched ) {
189  mvaValue = mva_->MVAValue(*thePFTauRef, usePhiAtEcalEntranceExtrapolation_);
190  bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
191 
193  if ( isInEcalCrack(tauEtaAtEcalEntrance) || isInEcalCrack(leadChargedPFCandEtaAtEcalEntrance) ) {
194  // add category index
195  category_output_->setValue(tauIndex_, category);
196  // return MVA output value
197  return -99;
198  }
200 
201  if ( std::abs(tauEtaAtEcalEntrance) < ECALBarrelEndcapEtaBorder ) { // Barrel
202  if ( numSignalPFGammaCandsInSigCone == 0 && !hasGsfTrack ) {
203  category = 0.;
204  }
205  else if ( numSignalPFGammaCandsInSigCone >= 1 && !hasGsfTrack ) {
206  category = 2.;
207  }
208  } else { // Endcap
209  if ( numSignalPFGammaCandsInSigCone == 0 && !hasGsfTrack ) {
210  category = 8.;
211  }
212  else if ( numSignalPFGammaCandsInSigCone >= 1 && !hasGsfTrack ) {
213  category = 10.;
214  }
215  }
216  }
217  }
218 
219  if ( verbosity_ ) {
220  edm::LogPrint("PFTauAgainstEleMVA6") << "<PFRecoTauDiscriminationAgainstElectronMVA6::discriminate>:" ;
221  edm::LogPrint("PFTauAgainstEleMVA6") << " tau: Pt = " << thePFTauRef->pt() << ", eta = " << thePFTauRef->eta() << ", phi = " << thePFTauRef->phi();
222  edm::LogPrint("PFTauAgainstEleMVA6") << " deltaREleTau = " << deltaRDummy << ", isGsfElectronMatched = " << isGsfElectronMatched;
223  edm::LogPrint("PFTauAgainstEleMVA6") << " #Prongs = " << thePFTauRef->signalPFChargedHadrCands().size();
224  edm::LogPrint("PFTauAgainstEleMVA6") << " MVA = " << mvaValue << ", category = " << category;
225  }
226 
227  // add category index
228  category_output_->setValue(tauIndex_, category);
229  // return MVA output value
230  return mvaValue;
231 }
232 
234 {
235  // add all category indices to event
236  evt.put(std::move(category_output_), "category");
237 }
238 
239 bool
241 {
242  double absEta = fabs(eta);
243  return (absEta > 1.460 && absEta < 1.558);
244 }
245 
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
#define nullptr
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