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 
15 
23 
24 #include <TMath.h>
25 
26 #include <iostream>
27 #include <sstream>
28 #include <fstream>
29 
30 using namespace reco;
31 
33 {
34  public:
37  mva_(nullptr),
38  category_output_()
39  {
40  mva_ = new AntiElectronIDMVA5(cfg);
41 
42  srcGsfElectrons_ = cfg.getParameter<edm::InputTag>("srcGsfElectrons");
43  GsfElectrons_token = consumes<reco::GsfElectronCollection>(srcGsfElectrons_);
44 
45  verbosity_ = cfg.getParameter<int>("verbosity");
46 
47  // add category index
48  produces<PFTauDiscriminator>("category");
49  }
50 
51  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
52 
53  double discriminate(const PFTauRef&) const override;
54 
55  void endEvent(edm::Event&) override;
56 
58  {
59  delete mva_;
60  }
61 
62  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
63 private:
64  bool isInEcalCrack(double) const;
65 
67 
69  float* mvaInput_;
70 
75 
76  std::unique_ptr<PFTauDiscriminator> category_output_;
77 
79 };
80 
82 {
83  mva_->beginEvent(evt, es);
84 
85  evt.getByToken(Tau_token, taus_);
86  category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
87 
88  evt.getByToken(GsfElectrons_token, gsfElectrons_);
89 }
90 
92 {
93  double mvaValue = 1.;
94  double category = -1.;
95  bool isGsfElectronMatched = false;
96 
97  float deltaRDummy = 9.9;
98 
99  float tauEtaAtEcalEntrance = -99.;
100  float sumEtaTimesEnergy = 0.;
101  float sumEnergy = 0.;
102  const std::vector<reco::PFCandidatePtr>& signalPFCands = thePFTauRef->signalPFCands();
103  for ( std::vector<reco::PFCandidatePtr>::const_iterator pfCandidate = signalPFCands.begin();
104  pfCandidate != signalPFCands.end(); ++pfCandidate ) {
105  sumEtaTimesEnergy += ((*pfCandidate)->positionAtECALEntrance().eta()*(*pfCandidate)->energy());
106  sumEnergy += (*pfCandidate)->energy();
107  }
108  if ( sumEnergy > 0. ) {
109  tauEtaAtEcalEntrance = sumEtaTimesEnergy/sumEnergy;
110  }
111 
112  float leadChargedPFCandEtaAtEcalEntrance = -99.;
113  float leadChargedPFCandPt = -99.;
114  for ( std::vector<reco::PFCandidatePtr>::const_iterator pfCandidate = signalPFCands.begin();
115  pfCandidate != signalPFCands.end(); ++pfCandidate ) {
116  const reco::Track* track = nullptr;
117  if ( (*pfCandidate)->trackRef().isNonnull() ) track = (*pfCandidate)->trackRef().get();
118  else if ( (*pfCandidate)->muonRef().isNonnull() && (*pfCandidate)->muonRef()->innerTrack().isNonnull() ) track = (*pfCandidate)->muonRef()->innerTrack().get();
119  else if ( (*pfCandidate)->muonRef().isNonnull() && (*pfCandidate)->muonRef()->globalTrack().isNonnull() ) track = (*pfCandidate)->muonRef()->globalTrack().get();
120  else if ( (*pfCandidate)->muonRef().isNonnull() && (*pfCandidate)->muonRef()->outerTrack().isNonnull() ) track = (*pfCandidate)->muonRef()->outerTrack().get();
121  else if ( (*pfCandidate)->gsfTrackRef().isNonnull() ) track = (*pfCandidate)->gsfTrackRef().get();
122  if ( track ) {
123  if ( track->pt() > leadChargedPFCandPt ) {
124  leadChargedPFCandEtaAtEcalEntrance = (*pfCandidate)->positionAtECALEntrance().eta();
125  leadChargedPFCandPt = track->pt();
126  }
127  }
128  }
129 
130  if( (*thePFTauRef).leadPFChargedHadrCand().isNonnull()) {
131  for ( reco::GsfElectronCollection::const_iterator theGsfElectron = gsfElectrons_->begin();
132  theGsfElectron != gsfElectrons_->end(); ++theGsfElectron ) {
133  if ( theGsfElectron->pt() > 10. ) { // CV: only take electrons above some minimal energy/Pt into account...
134  double deltaREleTau = deltaR(theGsfElectron->p4(), thePFTauRef->p4());
135  deltaRDummy = deltaREleTau;
136  if ( deltaREleTau < 0.3 ) {
137  double mva_match = mva_->MVAValue(*thePFTauRef, *theGsfElectron);
138  size_t numSignalPFGammaCands = thePFTauRef->signalPFGammaCands().size();
139  bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
140 
142  if ( isInEcalCrack(tauEtaAtEcalEntrance) || isInEcalCrack(leadChargedPFCandEtaAtEcalEntrance) ) {
143  // add category index
144  category_output_->setValue(tauIndex_, category);
145  // return MVA output value
146  return -99;
147  }
149 
150  if ( TMath::Abs(tauEtaAtEcalEntrance) < 1.479 ) { // Barrel
151  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
152  category = 4.;
153  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
154  category = 5.;
155  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
156  category = 6.;
157  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
158  category = 7.;
159  }
160  } else { // Endcap
161  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
162  category = 12.;
163  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
164  category = 13.;
165  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
166  category = 14.;
167  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
168  category = 15.;
169  }
170  }
171 
172  mvaValue = TMath::Min(mvaValue, mva_match);
173  isGsfElectronMatched = true;
174  } // deltaR < 0.3
175  } // electron pt > 10
176  } // end of loop over electrons
177 
178  if ( !isGsfElectronMatched ) {
179  mvaValue = mva_->MVAValue(*thePFTauRef);
180  size_t numSignalPFGammaCands = thePFTauRef->signalPFGammaCands().size();
181  bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
182 
184  if ( 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 ( TMath::Abs(tauEtaAtEcalEntrance) < 1.479 ) { // Barrel
193  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
194  category = 0.;
195  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
196  category = 1.;
197  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
198  category = 2.;
199  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
200  category = 3.;
201  }
202  } else { // Endcap
203  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
204  category = 8.;
205  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
206  category = 9.;
207  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
208  category = 10.;
209  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
210  category = 11.;
211  }
212  }
213  }
214  }
215 
216  if ( verbosity_ ) {
217  edm::LogPrint("PFTauAgainstEleMVA5") << "<PFRecoTauDiscriminationAgainstElectronMVA5::discriminate>:" ;
218  edm::LogPrint("PFTauAgainstEleMVA5") << " tau: Pt = " << thePFTauRef->pt() << ", eta = " << thePFTauRef->eta() << ", phi = " << thePFTauRef->phi();
219  edm::LogPrint("PFTauAgainstEleMVA5") << " deltaREleTau = " << deltaRDummy << ", isGsfElectronMatched = " << isGsfElectronMatched;
220  edm::LogPrint("PFTauAgainstEleMVA5") << " #Prongs = " << thePFTauRef->signalChargedHadrCands().size();
221  edm::LogPrint("PFTauAgainstEleMVA5") << " MVA = " << mvaValue << ", category = " << category;
222  }
223 
224  // add category index
225  category_output_->setValue(tauIndex_, category);
226  // return MVA output value
227  return mvaValue;
228 }
229 
231 {
232  // add all category indices to event
233  evt.put(std::move(category_output_), "category");
234 }
235 
236 bool
238 {
239  double absEta = fabs(eta);
240  return (absEta > 1.460 && absEta < 1.558);
241 }
242 
243 void
245  // pfRecoTauDiscriminationAgainstElectronMVA5
247  desc.add<double>("minMVANoEleMatchWOgWOgsfBL", 0.0);
248  desc.add<edm::InputTag>("PFTauProducer", edm::InputTag("pfTauProducer"));
249  desc.add<std::string>("mvaName_woGwoGSF_EC", "gbr_woGwoGSF_EC");
250  desc.add<double>("minMVANoEleMatchWgWOgsfBL", 0.0);
251  desc.add<std::string>("mvaName_woGwGSF_EC", "gbr_woGwGSF_EC");
252  desc.add<std::string>("mvaName_wGwGSF_EC", "gbr_wGwGSF_EC");
253  desc.add<double>("minMVAWgWgsfBL", 0.0);
254  desc.add<double>("minMVAWgWOgsfBL", 0.0);
255  desc.add<double>("minMVANoEleMatchWgWgsfBL", 0.0);
256  desc.add<double>("minMVAWOgWgsfEC", 0.0);
257  desc.add<std::string>("mvaName_wGwGSF_BL", "gbr_wGwGSF_BL");
258  desc.add<std::string>("mvaName_woGwGSF_BL", "gbr_woGwGSF_BL");
259  desc.add<bool>("returnMVA", true);
260  desc.add<double>("minMVANoEleMatchWgWOgsfEC", 0.0);
261  desc.add<bool>("loadMVAfromDB", true);
262  {
264  psd0.add<std::string>("BooleanOperator", "and");
265  {
267  psd1.add<double>("cut");
268  psd1.add<edm::InputTag>("Producer");
269  psd0.addOptional<edm::ParameterSetDescription>("leadTrack", psd1);
270  }
271  desc.add<edm::ParameterSetDescription>("Prediscriminants", psd0);
272  }
273  desc.add<std::string>("mvaName_wGwoGSF_EC", "gbr_wGwoGSF_EC");
274  desc.add<std::string>("mvaName_NoEleMatch_woGwoGSF_BL", "gbr_NoEleMatch_woGwoGSF_BL");
275  desc.add<double>("minMVAWOgWOgsfEC", 0.0);
276  desc.add<std::string>("mvaName_woGwoGSF_BL", "gbr_woGwoGSF_BL");
277  desc.add<std::string>("mvaName_wGwoGSF_BL", "gbr_wGwoGSF_BL");
278  desc.add<double>("minMVANoEleMatchWOgWOgsfEC", 0.0);
279  desc.add<std::string>("mvaName_NoEleMatch_woGwGSF_BL", "gbr_NoEleMatch_woGwGSF_BL");
280  desc.add<double>("minMVANoEleMatchWgWgsfEC", 0.0);
281  desc.add<double>("minMVAWOgWgsfBL", 0.0);
282  desc.add<double>("minMVANoEleMatchWOgWgsfEC", 0.0);
283  desc.add<double>("minMVAWgWOgsfEC", 0.0);
284  desc.add<std::string>("mvaName_NoEleMatch_wGwGSF_EC", "gbr_NoEleMatch_wGwGSF_EC");
285  desc.add<double>("minMVAWgWgsfEC", 0.0);
286  desc.add<int>("verbosity", 0);
287  desc.add<double>("minMVANoEleMatchWOgWgsfBL", 0.0);
288  desc.add<std::string>("mvaName_NoEleMatch_wGwoGSF_EC", "gbr_NoEleMatch_wGwoGSF_EC");
289  desc.add<std::string>("method", "BDTG");
290  desc.add<std::string>("mvaName_NoEleMatch_wGwGSF_BL", "gbr_NoEleMatch_wGwGSF_BL");
291  desc.add<std::string>("mvaName_NoEleMatch_wGwoGSF_BL", "gbr_NoEleMatch_wGwoGSF_BL");
292  desc.add<edm::InputTag>("srcGsfElectrons", edm::InputTag("gedGsfElectrons"));
293  desc.add<double>("minMVAWOgWOgsfBL", 0.0);
294  desc.add<std::string>("mvaName_NoEleMatch_woGwoGSF_EC", "gbr_NoEleMatch_woGwoGSF_EC");
295  desc.add<std::string>("mvaName_NoEleMatch_woGwGSF_EC", "gbr_NoEleMatch_woGwGSF_EC");
296  descriptions.add("pfRecoTauDiscriminationAgainstElectronMVA5", desc);
297 }
298 
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
edm::RefProd< TauCollection > TauRefProd
Definition: Tau.h:40
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
#define nullptr
T Min(T a, T b)
Definition: MathUtil.h:39
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:690
double pt() const
track transverse momentum
Definition: TrackBase.h:660
T Abs(T a)
Definition: MathUtil.h:49
ParameterDescriptionBase * add(U const &iLabel, T const &value)
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void beginEvent(const edm::Event &, const edm::EventSetup &) override
fixed size matrix
edm::EDGetTokenT< reco::GsfElectronCollection > GsfElectrons_token
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
def move(src, dest)
Definition: eostools.py:511