CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/RecoTauTag/RecoTau/plugins/RecoTauMVADiscriminator.cc

Go to the documentation of this file.
00001 /*
00002  * RecoTauMVADiscriminator
00003  *
00004  * Apply an MVA discriminator to a collection of PFTaus.  Output is a
00005  * PFTauDiscriminator.  The module takes the following options:
00006  *  > dbLabel - should match "appendToDataLabel" option of PoolDBSource
00007  *              if it exists.
00008  *  > mvas    - a vector of PSets, each of which contains nCharged, nPiZeros
00009  *              and a string giving the name of the correct MVA in the
00010  *              MVA ComputerContainer provided PoolDBSource.  This maps decay
00011  *              modes to MVA implementations.
00012  *  > defaultMVA - MVA to use if the decay mode does not match one specified in
00013  *              mvas.
00014  *  > remapOutput - TMVA gives its output from (-1, 1).  If this enabled remap
00015  *              it to (0, 1).
00016  *
00017  *  The interface to the MVA framework is handled by the RecoTauMVAHelper class.
00018  *
00019  * Author: Evan K. Friis (UC Davis)
00020  *
00021  */
00022 
00023 #include <boost/foreach.hpp>
00024 #include <boost/ptr_container/ptr_map.hpp>
00025 
00026 #include "RecoTauTag/RecoTau/interface/TauDiscriminationProducerBase.h"
00027 #include "RecoTauTag/RecoTau/interface/RecoTauMVAHelper.h"
00028 #include "RecoTauTag/RecoTau/interface/PFTauDecayModeTools.h"
00029 
00030 #include "DataFormats/TauReco/interface/PFTau.h"
00031 #include "DataFormats/TauReco/interface/PFTauFwd.h"
00032 
00033 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00035 
00036 
00037 class RecoTauMVADiscriminator : public PFTauDiscriminationProducerBase {
00038   public:
00039     explicit RecoTauMVADiscriminator(const edm::ParameterSet& pset);
00040     ~RecoTauMVADiscriminator() {}
00041 
00042     void beginEvent(const edm::Event&, const edm::EventSetup&);
00043     double discriminate(const reco::PFTauRef&);
00044 
00045   private:
00046     // Map a decay mode to an MVA getter
00047     typedef boost::ptr_map<reco::PFTau::hadronicDecayMode,
00048             reco::tau::RecoTauMVAHelper> MVAMap;
00049 
00050     std::auto_ptr<reco::tau::RecoTauMVAHelper> defaultMVA_;
00051 
00052     MVAMap mvas_;
00053     std::string dbLabel_;
00054     double unsupportedDMValue_;
00055     bool remapOutput_;
00056 };
00057 
00058 RecoTauMVADiscriminator::RecoTauMVADiscriminator(const edm::ParameterSet& pset)
00059   :PFTauDiscriminationProducerBase(pset) {
00060   std::string dbLabel;
00061   if (pset.exists("dbLabel"))
00062     dbLabel = pset.getParameter<std::string>("dbLabel");
00063 
00064   unsupportedDMValue_ = (pset.exists("unsupportedDecayModeValue")) ?
00065       pset.getParameter<double>("unsupportedDecayModeValue")
00066       : prediscriminantFailValue_;
00067 
00068   remapOutput_ = pset.getParameter<bool>("remapOutput");
00069 
00070   typedef std::vector<edm::ParameterSet> VPSet;
00071   const VPSet& mvas = pset.getParameter<VPSet>("mvas");
00072 
00073   for (VPSet::const_iterator mva = mvas.begin(); mva != mvas.end(); ++mva) {
00074     unsigned int nCharged = mva->getParameter<unsigned int>("nCharged");
00075     unsigned int nPiZeros = mva->getParameter<unsigned int>("nPiZeros");
00076     reco::PFTau::hadronicDecayMode decayMode = reco::tau::translateDecayMode(
00077         nCharged, nPiZeros);
00078     // Check to ensure this decay mode is not already added
00079     if (!mvas_.count(decayMode)) {
00080       std::string computerName = mva->getParameter<std::string>("mvaLabel");
00081       // Add it
00082       mvas_.insert(
00083           decayMode, new reco::tau::RecoTauMVAHelper(computerName, dbLabel));
00084     } else {
00085       edm::LogError("DecayModeNotUnique") << "The tau decay mode with "
00086         "nCharged/nPiZero = " << nCharged << "/" << nPiZeros << " dm: "
00087         << decayMode <<
00088         " is associated to multiple MVA implmentations, "
00089         "the second instantiation is being ignored!!!";
00090     }
00091   }
00092 
00093   // Check if we a catch-all MVA is desired.
00094   if (pset.exists("defaultMVA")) {
00095     defaultMVA_.reset(new reco::tau::RecoTauMVAHelper(
00096             pset.getParameter<std::string>("defaultMVA"),
00097             dbLabel));
00098   }
00099 
00100 }
00101 
00102 void RecoTauMVADiscriminator::beginEvent(const edm::Event& evt,
00103                                          const edm::EventSetup& es) {
00104   // Pass the event setup so the MVAHelpers can get the MVAs from the DB
00105   BOOST_FOREACH(MVAMap::value_type mva, mvas_) {
00106       mva.second->setEvent(evt, es);
00107   }
00108   if (defaultMVA_.get())
00109     defaultMVA_->setEvent(evt, es);
00110 }
00111 
00112 // Get the MVA output for a given PFTau
00113 double RecoTauMVADiscriminator::discriminate(const reco::PFTauRef& tau) {
00114   // Find the right MVA for this tau's decay mode
00115   MVAMap::iterator mva = mvas_.find(tau->decayMode());
00116   // If this DM has an associated decay mode, get and return the result.
00117   double output = unsupportedDMValue_;
00118   if (mva != mvas_.end() || defaultMVA_.get()) {
00119     if (mva != mvas_.end())
00120       output = mva->second->operator()(tau);
00121     else
00122       output = defaultMVA_->operator()(tau);
00123     // TMVA produces output from -1 to 1
00124     if (remapOutput_) {
00125       output += 1.;
00126       output /= 2.;
00127     }
00128   }
00129   return output;
00130 }
00131 
00132 #include "FWCore/Framework/interface/MakerMacros.h"
00133 DEFINE_FWK_MODULE(RecoTauMVADiscriminator);