CMS 3D CMS Logo

PATTauDiscriminationAgainstElectronMVA6.cc
Go to the documentation of this file.
1 /* class PATTauDiscriminationAgainstElectronMVA6
2  * created : Apr 14 2016,
3  * revised : ,
4  * Authorss : Anne-Catherine Le Bihan (IPHC)
5  */
6 
9 
12 
15 
20 
21 #include <iostream>
22 #include <sstream>
23 #include <fstream>
24 
25 using namespace pat;
26 
28 {
29  public:
32  mva_(),
33  category_output_()
34  {
35  mva_ = std::make_unique<AntiElectronIDMVA6>(cfg);
36 
37  srcElectrons = cfg.getParameter<edm::InputTag>("srcElectrons");
38  electronToken = consumes<pat::ElectronCollection>(srcElectrons);
39  vetoEcalCracks_ = cfg.getParameter<bool>("vetoEcalCracks");
40  verbosity_ = cfg.getParameter<int>("verbosity");
41 
42  // add category index
43  produces<PATTauDiscriminator>("category");
44  }
45 
46  void beginEvent(const edm::Event&, const edm::EventSetup&) override;
47 
48  double discriminate(const TauRef&) const override;
49 
50  void endEvent(edm::Event&) override;
51 
53 
54  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
55 private:
56  bool isInEcalCrack(double) const;
57 
59  std::unique_ptr<AntiElectronIDMVA6> mva_;
60 
65 
66  std::unique_ptr<PATTauDiscriminator> category_output_;
67 
69 
71 };
72 
74 {
75  mva_->beginEvent(evt, es);
76 
77  evt.getByToken(Tau_token, taus_);
78  category_output_.reset(new PATTauDiscriminator(TauRefProd(taus_)));
79 
80  evt.getByToken(electronToken, Electrons);
81 }
82 
84 {
85  double mvaValue = 1.;
86  double category = -1.;
87  bool isGsfElectronMatched = false;
88  float deltaRDummy = 9.9;
89  const float ECALBarrelEndcapEtaBorder = 1.479;
90  float tauEtaAtEcalEntrance = theTauRef->etaAtEcalEntrance();
91  float leadChargedPFCandEtaAtEcalEntrance = theTauRef->etaAtEcalEntranceLeadChargedCand();
92 
93  if( (*theTauRef).leadChargedHadrCand().isNonnull()) {
94  int numSignalPFGammaCandsInSigCone = 0;
95  const reco::CandidatePtrVector signalGammaCands = theTauRef->signalGammaCands();
96  for ( const auto & gamma : signalGammaCands ) {
97  double dR = deltaR(gamma->p4(), theTauRef->leadChargedHadrCand()->p4());
98  double signalrad = std::max(0.05, std::min(0.10, 3.0/std::max(1.0, theTauRef->pt())));
99  // gammas inside the tau signal cone
100  if (dR < signalrad) {
101  numSignalPFGammaCandsInSigCone += 1;
102  }
103  }
104  // loop over the electrons
105  for ( const auto & theElectron : *Electrons ) {
106  if ( theElectron.pt() > 10. ) { // CV: only take electrons above some minimal energy/Pt into account...
107  double deltaREleTau = deltaR(theElectron.p4(), theTauRef->p4());
108  deltaRDummy = deltaREleTau;
109  if( deltaREleTau < 0.3 ){
110  double mva_match = mva_->MVAValue(*theTauRef, theElectron);
111  bool hasGsfTrack = false;
112  pat::PackedCandidate const* packedLeadTauCand = dynamic_cast<pat::PackedCandidate const*>(theTauRef->leadChargedHadrCand().get());
113  if( abs(packedLeadTauCand->pdgId()) == 11 )
114  hasGsfTrack = true;
115  if ( !hasGsfTrack )
116  hasGsfTrack = theElectron.gsfTrack().isNonnull();
117 
118  // veto taus that go to Ecal crack
119  if ( vetoEcalCracks_ && (isInEcalCrack(tauEtaAtEcalEntrance) || isInEcalCrack(leadChargedPFCandEtaAtEcalEntrance)) ) {
120  // add category index
121  category_output_->setValue(tauIndex_, category);
122  // return MVA output value
123  return -99;
124  }
125  // Veto taus that go to Ecal crack
126  if( std::abs(tauEtaAtEcalEntrance) < ECALBarrelEndcapEtaBorder ){ // Barrel
127  if( numSignalPFGammaCandsInSigCone == 0 && hasGsfTrack ){
128  category = 5.;
129  }
130  else if ( numSignalPFGammaCandsInSigCone >= 1 && hasGsfTrack ) {
131  category = 7.;
132  }
133  } else { // Endcap
134  if ( numSignalPFGammaCandsInSigCone == 0 && hasGsfTrack ) {
135  category = 13.;
136  }
137  else if ( numSignalPFGammaCandsInSigCone >= 1 && hasGsfTrack ) {
138  category = 15.;
139  }
140  }
141  mvaValue = std::min(mvaValue, mva_match);
142  isGsfElectronMatched = true;
143  } // deltaR < 0.3
144  } // electron pt > 10
145  } // end of loop over electrons
146 
147  if ( !isGsfElectronMatched ) {
148  mvaValue = mva_->MVAValue(*theTauRef);
149  bool hasGsfTrack = false;
150  pat::PackedCandidate const* packedLeadTauCand = dynamic_cast<pat::PackedCandidate const*>(theTauRef->leadChargedHadrCand().get());
151  if( abs(packedLeadTauCand->pdgId()) == 11 ) hasGsfTrack = true;
152 
153  // veto taus that go to Ecal crack
154  if ( vetoEcalCracks_ && (isInEcalCrack(tauEtaAtEcalEntrance) || isInEcalCrack(leadChargedPFCandEtaAtEcalEntrance)) ) {
155  // add category index
156  category_output_->setValue(tauIndex_, category);
157  // return MVA output value
158  return -99;
159  }
160  // veto taus that go to Ecal crack
161  if( std::abs(tauEtaAtEcalEntrance) < ECALBarrelEndcapEtaBorder ){ // Barrel
162  if( numSignalPFGammaCandsInSigCone == 0 && !hasGsfTrack ){
163  category = 0.;
164  }
165  else if ( numSignalPFGammaCandsInSigCone >= 1 && !hasGsfTrack ) {
166  category = 2.;
167  }
168  } else { // Endcap
169  if ( numSignalPFGammaCandsInSigCone == 0 && !hasGsfTrack ) {
170  category = 8.;
171  }
172  else if ( numSignalPFGammaCandsInSigCone >= 1 && !hasGsfTrack ) {
173  category = 10.;
174  }
175  }
176  }
177  }
178  if ( verbosity_ ) {
179  edm::LogPrint("PATTauAgainstEleMVA6") << "<PATTauDiscriminationAgainstElectronMVA6::discriminate>:" ;
180  edm::LogPrint("PATTauAgainstEleMVA6") << " tau: Pt = " << theTauRef->pt() << ", eta = " << theTauRef->eta() << ", phi = " << theTauRef->phi();
181  edm::LogPrint("PATTauAgainstEleMVA6") << " deltaREleTau = " << deltaRDummy << ", isGsfElectronMatched = " << isGsfElectronMatched;
182  edm::LogPrint("PATTauAgainstEleMVA6") << " #Prongs = " << theTauRef->signalChargedHadrCands().size();
183  edm::LogPrint("PATTauAgainstEleMVA6") << " MVA = " << mvaValue << ", category = " << category;
184  }
185  // add category index
186  category_output_->setValue(tauIndex_, category);
187  // return MVA output value
188  return mvaValue;
189 }
190 
192 {
193  // add all category indices to event
194  evt.put(std::move(category_output_), "category");
195 }
196 
197 bool
199 {
200  double absEta = fabs(eta);
201  return (absEta > 1.460 && absEta < 1.558);
202 }
203 
204 void
206  // patTauDiscriminationAgainstElectronMVA6
208  desc.add<double>("minMVANoEleMatchWOgWOgsfBL", 0.0);
209  desc.add<double>("minMVANoEleMatchWgWOgsfBL", 0.0);
210  desc.add<bool>("vetoEcalCracks", true);
211  desc.add<bool>("usePhiAtEcalEntranceExtrapolation", false);
212  desc.add<std::string>("mvaName_wGwGSF_EC", "gbr_wGwGSF_EC");
213  desc.add<double>("minMVAWgWgsfBL", 0.0);
214  desc.add<std::string>("mvaName_woGwGSF_EC", "gbr_woGwGSF_EC");
215  desc.add<double>("minMVAWOgWgsfEC", 0.0);
216  desc.add<std::string>("mvaName_wGwGSF_BL", "gbr_wGwGSF_BL");
217  desc.add<std::string>("mvaName_woGwGSF_BL", "gbr_woGwGSF_BL");
218  desc.add<bool>("returnMVA", true);
219  desc.add<bool>("loadMVAfromDB", true);
220  {
222  psd0.add<std::string>("BooleanOperator", "and");
223  {
225  psd1.add<double>("cut");
226  psd1.add<edm::InputTag>("Producer");
227  psd0.addOptional<edm::ParameterSetDescription>("leadTrack", psd1);
228  }
229  desc.add<edm::ParameterSetDescription>("Prediscriminants", psd0);
230  }
231  desc.add<std::string>("mvaName_NoEleMatch_woGwoGSF_BL", "gbr_NoEleMatch_woGwoGSF_BL");
232  desc.add<edm::InputTag>("srcElectrons", edm::InputTag("slimmedElectrons"));
233  desc.add<double>("minMVANoEleMatchWOgWOgsfEC", 0.0);
234  desc.add<std::string>("mvaName_NoEleMatch_wGwoGSF_BL", "gbr_NoEleMatch_wGwoGSF_BL");
235  desc.add<edm::InputTag>("PATTauProducer", edm::InputTag("slimmedTaus"));
236  desc.add<double>("minMVAWOgWgsfBL", 0.0);
237  desc.add<double>("minMVAWgWgsfEC", 0.0);
238  desc.add<int>("verbosity", 0);
239  desc.add<std::string>("mvaName_NoEleMatch_wGwoGSF_EC", "gbr_NoEleMatch_wGwoGSF_EC");
240  desc.add<std::string>("method", "BDTG");
241  desc.add<std::string>("mvaName_NoEleMatch_woGwoGSF_EC", "gbr_NoEleMatch_woGwoGSF_EC");
242  desc.add<double>("minMVANoEleMatchWgWOgsfEC", 0.0);
243  descriptions.add("patTauDiscriminationAgainstElectronMVA6", desc);
244 }
245 
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
int pdgId() const override
PDG identifier.
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
Definition: HeavyIon.h:7
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< pat::ElectronCollection > electronToken
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:243
T min(T a, T b)
Definition: MathUtil.h:58
ParameterDescriptionBase * add(U const &iLabel, T const &value)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void beginEvent(const edm::Event &, const edm::EventSetup &) override
def move(src, dest)
Definition: eostools.py:511