CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFRecoTauDiscriminationAgainstElectronMVA2.cc
Go to the documentation of this file.
1 /* class PFRecoTauDiscriminationAgainstElectronMVA2
2  * created : Apr 10 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 << "<PFRecoTauDiscriminationAgainstElectronMVA2::PFRecoTauDiscriminationAgainstElectronMVA2>:" << std::endl;
33  //std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
34 
35  method_ = iConfig.getParameter<std::string>("method");
36  inputFileName1prongNoEleMatchBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchBL");
37  inputFileName1prongBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongBL");
38  inputFileName1prongStripsWOgsfBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWOgsfBL");
39  inputFileName1prongStripsWgsfWOpfEleMvaBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWgsfWOpfEleMvaBL");
40  inputFileName1prongStripsWgsfWpfEleMvaBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWgsfWpfEleMvaBL");
41  inputFileName1prongNoEleMatchEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchEC");
42  inputFileName1prongEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongEC");
43  inputFileName1prongStripsWOgsfEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWOgsfEC");
44  inputFileName1prongStripsWgsfWOpfEleMvaEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWgsfWOpfEleMvaEC");
45  inputFileName1prongStripsWgsfWpfEleMvaEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWgsfWpfEleMvaEC");
46 
47  returnMVA_ = iConfig.getParameter<bool>("returnMVA");
48  minMVA1prongNoEleMatchBL_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchBL");
49  minMVA1prongBL_ = iConfig.getParameter<double>("minMVA1prongBL");
50  minMVA1prongStripsWOgsfBL_ = iConfig.getParameter<double>("minMVA1prongStripsWOgsfBL");
51  minMVA1prongStripsWgsfWOpfEleMvaBL_ = iConfig.getParameter<double>("minMVA1prongStripsWgsfWOpfEleMvaBL");
52  minMVA1prongStripsWgsfWpfEleMvaBL_ = iConfig.getParameter<double>("minMVA1prongStripsWgsfWpfEleMvaBL");
53  minMVA1prongNoEleMatchEC_ = iConfig.getParameter<double>("minMVA1prongNoEleMatchEC");
54  minMVA1prongEC_ = iConfig.getParameter<double>("minMVA1prongEC");
55  minMVA1prongStripsWOgsfEC_ = iConfig.getParameter<double>("minMVA1prongStripsWOgsfEC");
56  minMVA1prongStripsWgsfWOpfEleMvaEC_ = iConfig.getParameter<double>("minMVA1prongStripsWgsfWOpfEleMvaEC");
57  minMVA1prongStripsWgsfWpfEleMvaEC_ = iConfig.getParameter<double>("minMVA1prongStripsWgsfWpfEleMvaEC");
58 
59  srcGsfElectrons_ = iConfig.getParameter<edm::InputTag>("srcGsfElectrons");
60 
61  mva_ = new AntiElectronIDMVA2();
62  // CV: working version of file compression not implemented yet
63  //mva_->Initialize_from_string(method_,
64  // readZippedFile(inputFileName1prongNoEleMatchBL_.fullPath()),
65  // readZippedFile(inputFileName1prongBL_.fullPath()),
66  // readZippedFile(inputFileName1prongStripsWOgsfBL_.fullPath()),
67  // readZippedFile(inputFileName1prongStripsWgsfWOpfEleMvaBL_.fullPath()),
68  // readZippedFile(inputFileName1prongStripsWgsfWpfEleMvaBL_.fullPath()),
69  // readZippedFile(inputFileName1prongNoEleMatchEC_.fullPath()),
70  // readZippedFile(inputFileName1prongEC_.fullPath()),
71  // readZippedFile(inputFileName1prongStripsWOgsfEC_.fullPath()),
72  // readZippedFile(inputFileName1prongStripsWgsfWOpfEleMvaEC_.fullPath()),
73  // readZippedFile(inputFileName1prongStripsWgsfWpfEleMvaEC_.fullPath()));
74  mva_->Initialize_from_file(method_,
75  inputFileName1prongNoEleMatchBL_.fullPath(),
76  inputFileName1prongBL_.fullPath(),
77  inputFileName1prongStripsWOgsfBL_.fullPath(),
78  inputFileName1prongStripsWgsfWOpfEleMvaBL_.fullPath(),
79  inputFileName1prongStripsWgsfWpfEleMvaBL_.fullPath(),
80  inputFileName1prongNoEleMatchEC_.fullPath(),
81  inputFileName1prongEC_.fullPath(),
82  inputFileName1prongStripsWOgsfEC_.fullPath(),
83  inputFileName1prongStripsWgsfWOpfEleMvaEC_.fullPath(),
84  inputFileName1prongStripsWgsfWpfEleMvaEC_.fullPath());
85 
86  // add category index
87  if ( returnMVA_ ) {
88  produces<PFTauDiscriminator>("category");
89  }
90  }
91 
92  void beginEvent(const edm::Event&, const edm::EventSetup&);
93 
94  double discriminate(const PFTauRef&);
95 
96  void endEvent(edm::Event&);
97 
99  {
100  delete mva_;
101  }
102 
103  private:
104 
105  std::string readZippedFile(const std::string& fileName)
106  {
107  //std::cout << "<PFRecoTauDiscriminationAgainstElectronMVA2::readZippedFile>:" << std::endl;
108  //std::cout << " fileName = " << fileName << std::endl;
109  // CV: code adapted from PhysicsTools/MVAComputer/src/MVAComputer.cc
110  std::ifstream file;
111  file.open(fileName.c_str());
112  if ( !file.good() ) throw cms::Exception("InvalidFileState")
113  << "Failed to open MVA file = " << fileName << " !!\n";
114  std::ostringstream buffer_zipped;
115  while ( file.good() ) {
116  buffer_zipped << (char)file.get();
117  }
118  file.close();
119  //std::cout << " buffer (zipped) = " << buffer_zipped.str() << std::endl;
120  ext::izstream gunzip(&file);
121  std::ostringstream buffer_unzipped;
122  buffer_unzipped << gunzip.rdbuf();
123  //std::cout << " buffer (unzipped) = " << buffer_unzipped.str() << std::endl;
124  return buffer_unzipped.str();
125  }
126 
127  std::string moduleLabel_;
128  std::string method_ ;
140  bool returnMVA_ ;
154  std::auto_ptr<PFTauDiscriminator> category_output_;
155  size_t tauIndex_;
156 };
157 
159 {
160  if ( returnMVA_ ) {
161  evt.getByLabel(TauProducer_, taus_);
162  category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
163  tauIndex_ = 0;
164  }
165  evt.getByLabel(srcGsfElectrons_, gsfElectrons_);
166 }
167 
169 {
170  double mva = 1.;
171  double workingPoint = 1.;
172  double category = -1.;
173  bool isGsfElectronMatched = false;
174  if( (*thePFTauRef).leadPFChargedHadrCand().isNonnull()) {
175  for ( reco::GsfElectronCollection::const_iterator theGsfElectron = gsfElectrons_->begin();
176  theGsfElectron != gsfElectrons_->end(); ++theGsfElectron ) {
177  if ( theGsfElectron->pt() > 10. ) { // CV: only take electrons above some minimal energy/Pt into account...
178  double deltaREleTau = deltaR(theGsfElectron->p4(), thePFTauRef->p4());
179  if ( deltaREleTau < 0.3 ) {
180  double mva_match = mva_->MVAValue(*thePFTauRef, *theGsfElectron);
181  double workingPoint_match = 0.;
182 
183  size_t numSignalPFGammaCands = thePFTauRef->signalPFGammaCands().size();
184  bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
185  bool isPFElectron = (theGsfElectron->mvaOutput().mva > -0.1);
186 
187  if ( thePFTauRef->signalPFChargedHadrCands().size() == 1 ) {
188  double mvaCut = 999.;
189  if ( TMath::Abs(thePFTauRef->eta()) < 1.5 ) { // Barrel
190  if ( numSignalPFGammaCands == 0 ) {
191  category = 1.;
192  mvaCut = minMVA1prongBL_;
193  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
194  category = 2.;
195  mvaCut = minMVA1prongStripsWOgsfBL_;
196  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack && !isPFElectron ) {
197  category = 3.;
198  mvaCut = minMVA1prongStripsWgsfWOpfEleMvaBL_;
199  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack && isPFElectron ) {
200  category = 4.;
201  mvaCut = minMVA1prongStripsWgsfWpfEleMvaBL_;
202  }
203  } else { // Endcap
204  if ( numSignalPFGammaCands == 0 ) {
205  category = 6.;
206  mvaCut = minMVA1prongEC_;
207  } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack ) {
208  category = 7.;
209  mvaCut = minMVA1prongStripsWOgsfEC_;
210  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack && !isPFElectron ) {
211  category = 8.;
212  mvaCut = minMVA1prongStripsWgsfWOpfEleMvaEC_;
213  } else if ( numSignalPFGammaCands >= 1 && hasGsfTrack && isPFElectron ) {
214  category = 9.;
215  mvaCut = minMVA1prongStripsWgsfWpfEleMvaEC_;
216  }
217  }
218  workingPoint_match = (mva_match > mvaCut);
219  } else {
220  workingPoint_match = 1.;
221  }
222 
223  mva = TMath::Min(mva, mva_match);
224  workingPoint = TMath::Min(workingPoint, workingPoint_match);
225  isGsfElectronMatched = true;
226  }
227  }
228  }
229  }
230 
231  if ( !isGsfElectronMatched ) {
232  mva = mva_->MVAValue(*thePFTauRef);
233  double mvaCut = 999.;
234  if ( TMath::Abs(thePFTauRef->eta()) < 1.5 ) { // Barrel
235  category = 0.;
236  mvaCut = minMVA1prongNoEleMatchBL_;
237  } else { // Endcap
238  category = 5.;
239  mvaCut = minMVA1prongNoEleMatchEC_;
240  }
241  workingPoint = (mva > mvaCut);
242  }
243 
244  //std::cout << "<PFRecoTauDiscriminationAgainstElectronMVA2::discriminate>:" << std::endl;
245  //std::cout << " tau: Pt = " << thePFTauRef->pt() << ", eta = " << thePFTauRef->eta() << ", phi = " << thePFTauRef->phi() << std::endl;
246  //std::cout << " mva = " << mva << ": workingPoint = " << workingPoint << std::endl;
247 
248  if ( returnMVA_ ) {
249  // add category index
250  category_output_->setValue(tauIndex_, category);
251  ++tauIndex_;
252  // return MVA output value
253  return mva;
254  } else {
255  return workingPoint;
256  }
257 }
258 
260 {
261  // add all category indices to event
262  if ( returnMVA_ ) {
263  evt.put(category_output_, "category");
264  }
265 }
266 
T getParameter(std::string const &) 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
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