CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/RecoTauTag/RecoTau/plugins/PFRecoTauDiscriminationAgainstElectronMVA2.cc

Go to the documentation of this file.
00001 /* class PFRecoTauDiscriminationAgainstElectronMVA2
00002  * created : Apr 10 2012,
00003  * revised : ,
00004  * Authorss : Ivo Naranjo (LLR Ecole-Polytechnique)
00005  */
00006 
00007 #include "RecoTauTag/RecoTau/interface/TauDiscriminationProducerBase.h"
00008 #include "FWCore/ParameterSet/interface/FileInPath.h"
00009 #include "FWCore/Utilities/interface/Exception.h"
00010 #include "RecoTauTag/RecoTau/interface/AntiElectronIDMVA2.h"
00011 #include "DataFormats/Math/interface/deltaR.h"
00012 #include "PhysicsTools/MVAComputer/interface/zstream.h"
00013 #include "DataFormats/TauReco/interface/PFTauDiscriminator.h"
00014 
00015 #include <TMath.h>
00016 
00017 #include <iostream>
00018 #include <sstream>
00019 #include <fstream>
00020 
00021 using namespace reco;
00022 
00023 class PFRecoTauDiscriminationAgainstElectronMVA2 : public PFTauDiscriminationProducerBase  
00024 {
00025  public:
00026   explicit PFRecoTauDiscriminationAgainstElectronMVA2(const edm::ParameterSet& iConfig)
00027     : PFTauDiscriminationProducerBase(iConfig),
00028       moduleLabel_(iConfig.getParameter<std::string>("@module_label")),
00029       mva_(0),
00030       category_output_(0)
00031   {
00032     //std::cout << "<PFRecoTauDiscriminationAgainstElectronMVA2::PFRecoTauDiscriminationAgainstElectronMVA2>:" << std::endl;
00033     //std::cout << " moduleLabel = " << moduleLabel_ << std::endl;
00034  
00035     method_                                    = iConfig.getParameter<std::string>("method");
00036     inputFileName1prongNoEleMatchBL_           = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchBL");
00037     inputFileName1prongBL_                     = iConfig.getParameter<edm::FileInPath>("inputFileName1prongBL");
00038     inputFileName1prongStripsWOgsfBL_          = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWOgsfBL");
00039     inputFileName1prongStripsWgsfWOpfEleMvaBL_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWgsfWOpfEleMvaBL");
00040     inputFileName1prongStripsWgsfWpfEleMvaBL_  = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWgsfWpfEleMvaBL");
00041     inputFileName1prongNoEleMatchEC_           = iConfig.getParameter<edm::FileInPath>("inputFileName1prongNoEleMatchEC");
00042     inputFileName1prongEC_                     = iConfig.getParameter<edm::FileInPath>("inputFileName1prongEC");
00043     inputFileName1prongStripsWOgsfEC_          = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWOgsfEC");
00044     inputFileName1prongStripsWgsfWOpfEleMvaEC_ = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWgsfWOpfEleMvaEC");
00045     inputFileName1prongStripsWgsfWpfEleMvaEC_  = iConfig.getParameter<edm::FileInPath>("inputFileName1prongStripsWgsfWpfEleMvaEC");
00046 
00047     returnMVA_                          = iConfig.getParameter<bool>("returnMVA");
00048     minMVA1prongNoEleMatchBL_           = iConfig.getParameter<double>("minMVA1prongNoEleMatchBL");
00049     minMVA1prongBL_                     = iConfig.getParameter<double>("minMVA1prongBL");
00050     minMVA1prongStripsWOgsfBL_          = iConfig.getParameter<double>("minMVA1prongStripsWOgsfBL");
00051     minMVA1prongStripsWgsfWOpfEleMvaBL_ = iConfig.getParameter<double>("minMVA1prongStripsWgsfWOpfEleMvaBL");
00052     minMVA1prongStripsWgsfWpfEleMvaBL_  = iConfig.getParameter<double>("minMVA1prongStripsWgsfWpfEleMvaBL");
00053     minMVA1prongNoEleMatchEC_           = iConfig.getParameter<double>("minMVA1prongNoEleMatchEC");
00054     minMVA1prongEC_                     = iConfig.getParameter<double>("minMVA1prongEC");
00055     minMVA1prongStripsWOgsfEC_          = iConfig.getParameter<double>("minMVA1prongStripsWOgsfEC");
00056     minMVA1prongStripsWgsfWOpfEleMvaEC_ = iConfig.getParameter<double>("minMVA1prongStripsWgsfWOpfEleMvaEC");
00057     minMVA1prongStripsWgsfWpfEleMvaEC_  = iConfig.getParameter<double>("minMVA1prongStripsWgsfWpfEleMvaEC");
00058 
00059     srcGsfElectrons_ = iConfig.getParameter<edm::InputTag>("srcGsfElectrons");
00060 
00061     mva_ = new AntiElectronIDMVA2();
00062     // CV: working version of file compression not implemented yet
00063     //mva_->Initialize_from_string(method_,
00064     //                             readZippedFile(inputFileName1prongNoEleMatchBL_.fullPath()),
00065     //                             readZippedFile(inputFileName1prongBL_.fullPath()),
00066     //                             readZippedFile(inputFileName1prongStripsWOgsfBL_.fullPath()),
00067     //                             readZippedFile(inputFileName1prongStripsWgsfWOpfEleMvaBL_.fullPath()),
00068     //                             readZippedFile(inputFileName1prongStripsWgsfWpfEleMvaBL_.fullPath()),
00069     //                             readZippedFile(inputFileName1prongNoEleMatchEC_.fullPath()),
00070     //                             readZippedFile(inputFileName1prongEC_.fullPath()),
00071     //                             readZippedFile(inputFileName1prongStripsWOgsfEC_.fullPath()),
00072     //                             readZippedFile(inputFileName1prongStripsWgsfWOpfEleMvaEC_.fullPath()),
00073     //                             readZippedFile(inputFileName1prongStripsWgsfWpfEleMvaEC_.fullPath()));
00074     mva_->Initialize_from_file(method_,
00075                                inputFileName1prongNoEleMatchBL_.fullPath(),
00076                                inputFileName1prongBL_.fullPath(),
00077                                inputFileName1prongStripsWOgsfBL_.fullPath(),
00078                                inputFileName1prongStripsWgsfWOpfEleMvaBL_.fullPath(),
00079                                inputFileName1prongStripsWgsfWpfEleMvaBL_.fullPath(),
00080                                inputFileName1prongNoEleMatchEC_.fullPath(),
00081                                inputFileName1prongEC_.fullPath(),
00082                                inputFileName1prongStripsWOgsfEC_.fullPath(),
00083                                inputFileName1prongStripsWgsfWOpfEleMvaEC_.fullPath(),
00084                                inputFileName1prongStripsWgsfWpfEleMvaEC_.fullPath());
00085 
00086     // add category index
00087     if ( returnMVA_ ) {
00088       produces<PFTauDiscriminator>("category");
00089     }
00090   }
00091 
00092   void beginEvent(const edm::Event&, const edm::EventSetup&);
00093 
00094   double discriminate(const PFTauRef&);
00095 
00096   void endEvent(edm::Event&);
00097 
00098   ~PFRecoTauDiscriminationAgainstElectronMVA2()
00099   {
00100     delete mva_;
00101   }
00102 
00103  private:
00104 
00105   std::string readZippedFile(const std::string& fileName)
00106   {
00107     //std::cout << "<PFRecoTauDiscriminationAgainstElectronMVA2::readZippedFile>:" << std::endl;
00108     //std::cout << " fileName = " << fileName << std::endl;
00109     // CV: code adapted from PhysicsTools/MVAComputer/src/MVAComputer.cc
00110     std::ifstream file;
00111     file.open(fileName.c_str());
00112     if ( !file.good() ) throw cms::Exception("InvalidFileState")
00113       << "Failed to open MVA file = " << fileName << " !!\n";
00114     std::ostringstream buffer_zipped;
00115     while ( file.good() ) {
00116       buffer_zipped << (char)file.get();
00117     }
00118     file.close();
00119     //std::cout << " buffer (zipped) = " << buffer_zipped.str() << std::endl;
00120     ext::izstream gunzip(&file);
00121     std::ostringstream buffer_unzipped;
00122     buffer_unzipped << gunzip.rdbuf();
00123     //std::cout << " buffer (unzipped) = " << buffer_unzipped.str() << std::endl;
00124     return buffer_unzipped.str();
00125   }
00126 
00127   std::string moduleLabel_;
00128   std::string method_ ;
00129   edm::FileInPath inputFileName1prongNoEleMatchBL_;
00130   edm::FileInPath inputFileName1prongBL_;
00131   edm::FileInPath inputFileName1prongStripsWOgsfBL_;
00132   edm::FileInPath inputFileName1prongStripsWgsfWOpfEleMvaBL_;
00133   edm::FileInPath inputFileName1prongStripsWgsfWpfEleMvaBL_;
00134   edm::FileInPath inputFileName1prongNoEleMatchEC_;
00135   edm::FileInPath inputFileName1prongEC_;
00136   edm::FileInPath inputFileName1prongStripsWOgsfEC_;
00137   edm::FileInPath inputFileName1prongStripsWgsfWOpfEleMvaEC_;
00138   edm::FileInPath inputFileName1prongStripsWgsfWpfEleMvaEC_;
00139   AntiElectronIDMVA2* mva_;
00140   bool returnMVA_ ;
00141   double minMVA1prongNoEleMatchBL_ ;
00142   double minMVA1prongBL_ ;
00143   double minMVA1prongStripsWOgsfBL_ ;
00144   double minMVA1prongStripsWgsfWOpfEleMvaBL_ ;
00145   double minMVA1prongStripsWgsfWpfEleMvaBL_ ;
00146   double minMVA1prongNoEleMatchEC_ ;
00147   double minMVA1prongEC_ ;
00148   double minMVA1prongStripsWOgsfEC_;
00149   double minMVA1prongStripsWgsfWOpfEleMvaEC_ ;
00150   double minMVA1prongStripsWgsfWpfEleMvaEC_ ;
00151   edm::InputTag srcGsfElectrons_;
00152   edm::Handle<reco::GsfElectronCollection> gsfElectrons_;
00153   edm::Handle<TauCollection> taus_;
00154   std::auto_ptr<PFTauDiscriminator> category_output_;
00155   size_t tauIndex_;
00156 };
00157 
00158 void PFRecoTauDiscriminationAgainstElectronMVA2::beginEvent(const edm::Event& evt, const edm::EventSetup& es)
00159 {
00160   if ( returnMVA_ ) {
00161     evt.getByLabel(TauProducer_, taus_);
00162     category_output_.reset(new PFTauDiscriminator(TauRefProd(taus_)));
00163     tauIndex_ = 0;
00164   }
00165   evt.getByLabel(srcGsfElectrons_, gsfElectrons_);
00166 }
00167 
00168 double PFRecoTauDiscriminationAgainstElectronMVA2::discriminate(const PFTauRef& thePFTauRef)
00169 {
00170   double mva = 1.;
00171   double workingPoint = 1.;
00172   double category = -1.;
00173   bool isGsfElectronMatched = false;
00174   if( (*thePFTauRef).leadPFChargedHadrCand().isNonnull()) {
00175     for ( reco::GsfElectronCollection::const_iterator theGsfElectron = gsfElectrons_->begin();
00176           theGsfElectron != gsfElectrons_->end(); ++theGsfElectron ) {
00177       if ( theGsfElectron->pt() > 10. ) { // CV: only take electrons above some minimal energy/Pt into account...
00178         double deltaREleTau = deltaR(theGsfElectron->p4(), thePFTauRef->p4());
00179         if ( deltaREleTau < 0.3 ) {
00180           double mva_match = mva_->MVAValue(*thePFTauRef, *theGsfElectron);
00181           double workingPoint_match = 0.;
00182 
00183           size_t numSignalPFGammaCands = thePFTauRef->signalPFGammaCands().size();
00184           bool hasGsfTrack = thePFTauRef->leadPFChargedHadrCand()->gsfTrackRef().isNonnull();
00185           bool isPFElectron = (theGsfElectron->mvaOutput().mva > -0.1);
00186 
00187           if ( thePFTauRef->signalPFChargedHadrCands().size() == 1 ) {
00188             double mvaCut = 999.;
00189             if ( TMath::Abs(thePFTauRef->eta()) < 1.5 ) { // Barrel
00190               if        ( numSignalPFGammaCands == 0                                  ) {
00191                 category = 1.;
00192                 mvaCut = minMVA1prongBL_;
00193               } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack                  ) {
00194                 category = 2.;
00195                 mvaCut = minMVA1prongStripsWOgsfBL_;
00196               } else if ( numSignalPFGammaCands >= 1 &&  hasGsfTrack && !isPFElectron ) {
00197                 category = 3.;
00198                 mvaCut = minMVA1prongStripsWgsfWOpfEleMvaBL_;
00199               } else if ( numSignalPFGammaCands >= 1 &&  hasGsfTrack &&  isPFElectron ) {
00200                 category = 4.;
00201                 mvaCut = minMVA1prongStripsWgsfWpfEleMvaBL_;
00202               }
00203             } else { // Endcap
00204               if        ( numSignalPFGammaCands == 0                                  ) {
00205                 category = 6.;
00206                 mvaCut = minMVA1prongEC_;
00207               } else if ( numSignalPFGammaCands >= 1 && !hasGsfTrack                  ) {
00208                 category = 7.;
00209                 mvaCut = minMVA1prongStripsWOgsfEC_;
00210               } else if ( numSignalPFGammaCands >= 1 &&  hasGsfTrack && !isPFElectron ) {
00211                 category = 8.;
00212                 mvaCut = minMVA1prongStripsWgsfWOpfEleMvaEC_;
00213               } else if ( numSignalPFGammaCands >= 1 &&  hasGsfTrack &&  isPFElectron ) {
00214                 category = 9.;
00215                 mvaCut = minMVA1prongStripsWgsfWpfEleMvaEC_;
00216               }
00217             }
00218             workingPoint_match = (mva_match > mvaCut);
00219           } else {
00220             workingPoint_match = 1.;
00221           }
00222 
00223           mva = TMath::Min(mva, mva_match);
00224           workingPoint = TMath::Min(workingPoint, workingPoint_match);
00225           isGsfElectronMatched = true;
00226         }
00227       }
00228     }
00229   }
00230 
00231   if ( !isGsfElectronMatched ) {
00232     mva = mva_->MVAValue(*thePFTauRef);
00233     double mvaCut = 999.;
00234     if ( TMath::Abs(thePFTauRef->eta()) < 1.5 ) { // Barrel
00235       category = 0.;
00236       mvaCut = minMVA1prongNoEleMatchBL_;
00237     } else { // Endcap
00238       category = 5.;
00239       mvaCut = minMVA1prongNoEleMatchEC_;
00240     }
00241     workingPoint = (mva > mvaCut);
00242   }
00243 
00244   //std::cout << "<PFRecoTauDiscriminationAgainstElectronMVA2::discriminate>:" << std::endl;
00245   //std::cout << " tau: Pt = " << thePFTauRef->pt() << ", eta = " << thePFTauRef->eta() << ", phi = " << thePFTauRef->phi() << std::endl;
00246   //std::cout << " mva = " << mva << ": workingPoint = " << workingPoint << std::endl;
00247 
00248   if ( returnMVA_ ) {
00249     // add category index
00250     category_output_->setValue(tauIndex_, category);
00251     ++tauIndex_;
00252     // return MVA output value
00253     return mva;
00254   } else {
00255     return workingPoint;
00256   }
00257 }
00258 
00259 void PFRecoTauDiscriminationAgainstElectronMVA2::endEvent(edm::Event& evt)
00260 {
00261   // add all category indices to event
00262   if ( returnMVA_ ) {
00263     evt.put(category_output_, "category");
00264   }
00265 }
00266 
00267 DEFINE_FWK_MODULE(PFRecoTauDiscriminationAgainstElectronMVA2);