CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFRecoTauDiscriminationAgainstElectronMVA3.cc
Go to the documentation of this file.
1 /* class PFRecoTauDiscriminationAgainstElectronMVA3
2  * created : Oct 18 2012,
3  * revised : ,
4  * Authorss : Ivo Naranjo (LLR Ecole-Polytechnique)
5  */
6 
14 
15 #include <TMath.h>
16 
17 #include <iostream>
18 #include <sstream>
19 #include <fstream>
20 
21 using namespace reco;
22 
24 {
25  public:
28  moduleLabel_(iConfig.getParameter<std::string>("@module_label")),
29  mva_(0),
30  category_output_(0)
31  {
32  //std::cout << "<PFRecoTauDiscriminationAgainstElectronMVA3::PFRecoTauDiscriminationAgainstElectronMVA3>:" << std::endl;
33  //std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
34 
35  method_ = iConfig.getParameter<std::string>("method");
36  inputFileName1prongNoEleMatchWOgWOgsfBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchWOgWOgsfBL");
37  inputFileName1prongNoEleMatchWOgWgsfBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchWOgWgsfBL");
38  inputFileName1prongNoEleMatchWgWOgsfBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchWgWOgsfBL");
39  inputFileName1prongNoEleMatchWgWgsfBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchWgWgsfBL");
40  inputFileName1prongWOgWOgsfBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongWOgWOgsfBL");
41  inputFileName1prongWOgWgsfBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongWOgWgsfBL");
42  inputFileName1prongWgWOgsfBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongWgWOgsfBL");
43  inputFileName1prongWgWgsfBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongWgWgsfBL");
44  inputFileName1prongNoEleMatchWOgWOgsfEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchWOgWOgsfEC");
45  inputFileName1prongNoEleMatchWOgWgsfEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchWOgWgsfEC");
46  inputFileName1prongNoEleMatchWgWOgsfEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchWgWOgsfEC");
47  inputFileName1prongNoEleMatchWgWgsfEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchWgWgsfEC");
48  inputFileName1prongWOgWOgsfEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongWOgWOgsfEC");
49  inputFileName1prongWOgWgsfEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongWOgWgsfEC");
50  inputFileName1prongWgWOgsfEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongWgWOgsfEC");
51  inputFileName1prongWgWgsfEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongWgWgsfEC");
52 
53  returnMVA_ = iConfig.getParameter<bool>("returnMVA");
54  minMVA1prongNoEleMatchWOgWOgsfBL_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchWOgWOgsfBL");
55  minMVA1prongNoEleMatchWOgWgsfBL_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchWOgWgsfBL");
56  minMVA1prongNoEleMatchWgWOgsfBL_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchWgWOgsfBL");
57  minMVA1prongNoEleMatchWgWgsfBL_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchWgWgsfBL");
58  minMVA1prongWOgWOgsfBL_ = iConfig.getParameter<double>("minMVA1prongWOgWOgsfBL");
59  minMVA1prongWOgWgsfBL_ = iConfig.getParameter<double>("minMVA1prongWOgWgsfBL");
60  minMVA1prongWgWOgsfBL_ = iConfig.getParameter<double>("minMVA1prongWgWOgsfBL");
61  minMVA1prongWgWgsfBL_ = iConfig.getParameter<double>("minMVA1prongWgWgsfBL");
62  minMVA1prongNoEleMatchWOgWOgsfEC_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchWOgWOgsfEC");
63  minMVA1prongNoEleMatchWOgWgsfEC_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchWOgWgsfEC");
64  minMVA1prongNoEleMatchWgWOgsfEC_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchWgWOgsfEC");
65  minMVA1prongNoEleMatchWgWgsfEC_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchWgWgsfEC");
66  minMVA1prongWOgWOgsfEC_ = iConfig.getParameter<double>("minMVA1prongWOgWOgsfEC");
67  minMVA1prongWOgWgsfEC_ = iConfig.getParameter<double>("minMVA1prongWOgWgsfEC");
68  minMVA1prongWgWOgsfEC_ = iConfig.getParameter<double>("minMVA1prongWgWOgsfEC");
69  minMVA1prongWgWgsfEC_ = iConfig.getParameter<double>("minMVA1prongWgWgsfEC");
70  minMVA3prongMatch_ = iConfig.getParameter<double>("minMVA3prongMatch");
71  minMVA3prongNoMatch_ = iConfig.getParameter<double>("minMVA3prongNoMatch");
72 
73  srcGsfElectrons_ = iConfig.getParameter<edm::InputTag>("srcGsfElectrons");
74 
75  mva_ = new AntiElectronIDMVA3();
76  // CV: working version of file compression not implemented yet
77 // mva_->Initialize_from_string(method_,
78 // readZippedFile(inputFileName1prongNoEleMatchWOgWOgsfBL_.fullPath()),
79 // readZippedFile(inputFileName1prongNoEleMatchWOgWgsfBL_.fullPath()),
80 // readZippedFile(inputFileName1prongNoEleMatchWgWOgsfBL_.fullPath()),
81 // readZippedFile(inputFileName1prongNoEleMatchWgWgsfBL_.fullPath()),
82 // readZippedFile(inputFileName1prongWOgWOgsfBL_.fullPath()),
83 // readZippedFile(inputFileName1prongWOgWgsfBL_.fullPath()),
84 // readZippedFile(inputFileName1prongWgWOgsfBL_.fullPath()),
85 // readZippedFile(inputFileName1prongWgWgsfBL_.fullPath()),
86 // readZippedFile(inputFileName1prongNoEleMatchWOgWOgsfEC_.fullPath()),
87 // readZippedFile(inputFileName1prongNoEleMatchWOgWgsfEC_.fullPath()),
88 // readZippedFile(inputFileName1prongNoEleMatchWgWOgsfEC_.fullPath()),
89 // readZippedFile(inputFileName1prongNoEleMatchWgWgsfEC_.fullPath()),
90 // readZippedFile(inputFileName1prongWOgWOgsfEC_.fullPath()),
91 // readZippedFile(inputFileName1prongWOgWgsfEC_.fullPath()),
92 // readZippedFile(inputFileName1prongWgWOgsfEC_.fullPath()),
93 // readZippedFile(inputFileName1prongWgWgsfEC_.fullPath()));
94  mva_->Initialize_from_file(method_,
95  inputFileName1prongNoEleMatchWOgWOgsfBL_.fullPath(),
96  inputFileName1prongNoEleMatchWOgWgsfBL_.fullPath(),
97  inputFileName1prongNoEleMatchWgWOgsfBL_.fullPath(),
98  inputFileName1prongNoEleMatchWgWgsfBL_.fullPath(),
99  inputFileName1prongWOgWOgsfBL_.fullPath(),
100  inputFileName1prongWOgWgsfBL_.fullPath(),
101  inputFileName1prongWgWOgsfBL_.fullPath(),
102  inputFileName1prongWgWgsfBL_.fullPath(),
103  inputFileName1prongNoEleMatchWOgWOgsfEC_.fullPath(),
104  inputFileName1prongNoEleMatchWOgWgsfEC_.fullPath(),
105  inputFileName1prongNoEleMatchWgWOgsfEC_.fullPath(),
106  inputFileName1prongNoEleMatchWgWgsfEC_.fullPath(),
107  inputFileName1prongWOgWOgsfEC_.fullPath(),
108  inputFileName1prongWOgWgsfEC_.fullPath(),
109  inputFileName1prongWgWOgsfEC_.fullPath(),
110  inputFileName1prongWgWgsfEC_.fullPath());
111 
112  // add category index
113  if ( returnMVA_ ) {
114  produces<PFTauDiscriminator>("category");
115  }
116  }
117 
118  void beginEvent(const edm::Event&, const edm::EventSetup&);
119 
120  double discriminate(const PFTauRef&);
121 
122  void endEvent(edm::Event&);
123 
125  {
126  delete mva_;
127  }
128 
129  private:
130  bool isInEcalCrack(double) const;
131  std::string readZippedFile(const std::string& fileName)
132  {
133  //std::cout << "<PFRecoTauDiscriminationAgainstElectronMVA3::readZippedFile>:" << std::endl;
134  //std::cout << " fileName = " << fileName << std::endl;
135  // CV: code adapted from PhysicsTools/MVAComputer/src/MVAComputer.cc
136  std::ifstream file;
137  file.open(fileName.c_str());
138  if ( !file.good() ) throw cms::Exception("InvalidFileState")
139  << "Failed to open MVA file = " << fileName << " !!\n";
140  std::ostringstream buffer_zipped;
141  while ( file.good() ) {
142  buffer_zipped << (char)file.get();
143  }
144  file.close();
145  //std::cout << " buffer (zipped) = " << buffer_zipped.str() << std::endl;
146  ext::izstream gunzip(&file);
147  std::ostringstream buffer_unzipped;
148  buffer_unzipped << gunzip.rdbuf();
149  //std::cout << " buffer (unzipped) = " << buffer_unzipped.str() << std::endl;
150  return buffer_unzipped.str();
151  }
152 
153  std::string moduleLabel_;
154  std::string method_ ;
172  bool returnMVA_ ;
194  std::auto_ptr<PFTauDiscriminator> category_output_;
195  size_t tauIndex_;
196 };
197 
199 {
200  if ( returnMVA_ ) {
201  evt.getByLabel(TauProducer_, taus_);
202  category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
203  tauIndex_ = 0;
204  }
205  evt.getByLabel(srcGsfElectrons_, gsfElectrons_);
206 }
207 
209 {
210  double mva = 1.;
211  double workingPoint = 1.;
212  double category = -1.;
213  bool isGsfElectronMatched = false;
214 
215  //float deltaRTestigo = 9.9;
216  //float mvaCutTestigo = 999;
217 
218  if( (*thePFTauRef).leadPFChargedHadrCand().isNonnull()) {
219  for ( reco::GsfElectronCollection::const_iterator theGsfElectron = gsfElectrons_->begin();
220  theGsfElectron != gsfElectrons_->end(); ++theGsfElectron ) {
221  if ( theGsfElectron->pt() > 10. ) { // CV: only take electrons above some minimal energy/Pt into account...
222  double deltaREleTau = deltaR(theGsfElectron->p4(), thePFTauRef->p4());
223  //deltaRTestigo = deltaREleTau;
224  if ( deltaREleTau < 0.3 ) {
225  double mva_match = mva_->MVAValue(*thePFTauRef, *theGsfElectron);
226  double workingPoint_match = 0.;
227  size_t numSignalPFGammaCands = thePFTauRef->signalPFGammaCands().size();
228  bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
229 
230  if ( thePFTauRef->signalPFChargedHadrCands().size() == 1 ) {
232  Float_t TauEtaAtEcalEntrance = -99.;
233  float sumEtaTimesEnergy = 0;
234  float sumEnergy = 0;
235  for(unsigned int j = 0 ; j < ((*thePFTauRef).signalPFCands()).size() ; j++){
236  reco::PFCandidateRef pfcandidate = ((*thePFTauRef).signalPFCands()).at(j);
237  sumEtaTimesEnergy += pfcandidate->positionAtECALEntrance().eta()*pfcandidate->energy();
238  sumEnergy += pfcandidate->energy();
239  }
240  if(sumEnergy>0)TauEtaAtEcalEntrance = sumEtaTimesEnergy/sumEnergy;
241 
242  if (isInEcalCrack(TauEtaAtEcalEntrance)){
243  if ( returnMVA_ ) {
244  // add category index
245  category_output_->setValue(tauIndex_, category);
246  ++tauIndex_;
247  // return MVA output value
248  return -99;
249  } else {
250  //return Workingpoint 0
251  return 0;
252  }
253  }
255 
256  double mvaCut = 999.;
257  if ( TMath::Abs(thePFTauRef->eta()) < 1.5 ) { // Barrel
258  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
259  category = 4.;
260  mvaCut = minMVA1prongWOgWOgsfBL_;
261  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
262  category = 5.;
263  mvaCut = minMVA1prongWOgWgsfBL_;
264  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
265  category = 6.;
266  mvaCut = minMVA1prongWgWOgsfBL_;
267  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
268  category = 7.;
269  mvaCut = minMVA1prongWgWgsfBL_;
270  }
271  } else { // Endcap
272  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
273  category = 12.;
274  mvaCut = minMVA1prongWOgWOgsfEC_;
275  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
276  category = 13.;
277  mvaCut = minMVA1prongWOgWgsfEC_;
278  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
279  category = 14.;
280  mvaCut = minMVA1prongWgWOgsfEC_;
281  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
282  category = 15.;
283  mvaCut = minMVA1prongWgWgsfEC_;
284  }
285  }
286  workingPoint_match = (mva_match > mvaCut);
287  //mvaCutTestigo = mvaCut;
288  } else {
289  category = 16.;
290  workingPoint_match = (mva_match > minMVA3prongMatch_);
291  }
292  mva = TMath::Min(mva, mva_match);
293  workingPoint = TMath::Min(workingPoint, workingPoint_match);
294  isGsfElectronMatched = true;
295  }//deltaR<0.3
296  }//electron pt>10
297  }//loop electrons
298 
299  if ( !isGsfElectronMatched ) {
300  mva = mva_->MVAValue(*thePFTauRef);
301  size_t numSignalPFGammaCands = thePFTauRef->signalPFGammaCands().size();
302  bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
303  if ( thePFTauRef->signalPFChargedHadrCands().size() == 1 ) {
305  Float_t TauEtaAtEcalEntrance = -99.;
306  float sumEtaTimesEnergy = 0;
307  float sumEnergy = 0;
308  for(unsigned int j = 0 ; j < ((*thePFTauRef).signalPFCands()).size() ; j++){
309  reco::PFCandidateRef pfcandidate = ((*thePFTauRef).signalPFCands()).at(j);
310  sumEtaTimesEnergy += pfcandidate->positionAtECALEntrance().eta()*pfcandidate->energy();
311  sumEnergy += pfcandidate->energy();
312  }
313  if(sumEnergy>0)TauEtaAtEcalEntrance = sumEtaTimesEnergy/sumEnergy;
314 
315  if (isInEcalCrack(TauEtaAtEcalEntrance)){
316  if ( returnMVA_ ) {
317  // add category index
318  category_output_->setValue(tauIndex_, category);
319  ++tauIndex_;
320  // return MVA output value
321  return -99;
322  } else {
323  //return Workingpoint 0
324  return 0;
325  }
326  }
328 
329  double mvaCut = 999.;
330  if ( TMath::Abs(thePFTauRef->eta()) < 1.5 ) { // Barrel
331  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
332  category = 0.;
333  mvaCut = minMVA1prongNoEleMatchWOgWOgsfBL_;
334  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
335  category = 1.;
336  mvaCut = minMVA1prongNoEleMatchWOgWgsfBL_;
337  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
338  category = 2.;
339  mvaCut = minMVA1prongNoEleMatchWgWOgsfBL_;
340  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
341  category = 3.;
342  mvaCut = minMVA1prongNoEleMatchWgWgsfBL_;
343  }
344  } else { // Endcap
345  if ( numSignalPFGammaCands == 0 && !hasGsfTrack ) {
346  category = 8.;
347  mvaCut = minMVA1prongNoEleMatchWOgWOgsfEC_;
348  } else if ( numSignalPFGammaCands == 0 && hasGsfTrack ) {
349  category = 9.;
350  mvaCut = minMVA1prongNoEleMatchWOgWgsfEC_;
351  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
352  category = 10.;
353  mvaCut = minMVA1prongNoEleMatchWgWOgsfEC_;
354  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack ) {
355  category = 11.;
356  mvaCut = minMVA1prongNoEleMatchWgWgsfEC_;
357  }
358  }
359  workingPoint = (mva > mvaCut);
360  //mvaCutTestigo = mvaCut;
361  } else {
362  category = 17.;
363  workingPoint = (mva > minMVA3prongNoMatch_);
364  }
365  }
366  }
367 
368  // std::cout<<" Taus : "<<TauProducer_<<std::endl;
369  // std::cout << "<PFRecoTauDiscriminationAgainstElectronMVA3::discriminate>:" << std::endl;
370  // std::cout << " tau: Pt = " << thePFTauRef->pt() << ", eta = " << thePFTauRef->eta() << ", phi = " << thePFTauRef->phi() << std::endl;
371  // std::cout << " mva = " << mva << " mvaCut = " << mvaCutTestigo <<" isGsfElectronMatched = "<<isGsfElectronMatched<< std::endl;
372  // std::cout << " category = " << category << " : workingPoint = " << workingPoint << std::endl;
373  // std::cout << " deltaREleTau = " << deltaRTestigo << std::endl;
374  // std::cout << " charged hadron in tau : "<<(*thePFTauRef).leadPFChargedHadrCand().isNonnull()<< std::endl;
375  // std::cout << " Prongs in tau : "<<thePFTauRef->signalPFChargedHadrCands().size()<< std::endl;
376 
377  if ( returnMVA_ ) {
378  // add category index
379  category_output_->setValue(tauIndex_, category);
380  ++tauIndex_;
381  // return MVA output value
382  return mva;
383  } else {
384  return workingPoint;
385  }
386 }
387 
389 {
390  // add all category indices to event
391  if ( returnMVA_ ) {
392  evt.put(category_output_, "category");
393  }
394 }
395 
396 bool
398 {
399  eta = fabs(eta);
400  return (eta>1.460 && eta<1.558);
401 }
402 
T getParameter(std::string const &) const
T eta() const
double deltaR(double eta1, double phi1, double eta2, double phi2)
Definition: deltaR.h:19
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:250
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
int j
Definition: DBlmapReader.cc:9
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
DEFINE_FWK_MODULE(CosmicTrackingParticleSelector)
ZIStreamBuf_t * rdbuf()
Definition: zstream.h:122
void beginEvent(const edm::Event &, const edm::EventSetup &)
std::string fullPath() const
Definition: FileInPath.cc:171
tuple size
Write out results.
list at
Definition: asciidump.py:428