CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/RecoTauTag/TauTagTools/plugins/PFTauMVAInputDiscriminantTranslator.cc

Go to the documentation of this file.
00001 /*
00002  * PFTauMVAInputDiscriminantTranslator
00003  *
00004  * Translate a list of given MVA (i.e. TaNC)
00005  * variables into standard PFTauDiscriminators
00006  * to facilitate embeddeing them into pat::Taus
00007  *
00008  * Author: Evan K. Friis (UC Davis)
00009  *
00010  */
00011 
00012 #include <boost/shared_ptr.hpp>
00013 #include <boost/foreach.hpp>
00014 
00015 #include <memory>
00016 #include <string>
00017 #include "FWCore/Framework/interface/Frameworkfwd.h"
00018 #include "FWCore/Framework/interface/EDProducer.h"
00019 #include "FWCore/Framework/interface/Event.h"
00020 #include "FWCore/Framework/interface/MakerMacros.h"
00021 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00022 
00023 #include "PhysicsTools/MVAComputer/interface/Variable.h"
00024 #include "DataFormats/TauReco/interface/PFTauDiscriminator.h"
00025 #include "RecoTauTag/RecoTau/interface/RecoTauDiscriminantPlugins.h"
00026 
00027 using namespace reco;
00028 
00029 class PFTauMVAInputDiscriminantTranslator : public edm::EDProducer {
00030   public:
00031     struct DiscriminantInfo {
00032       PhysicsTools::AtomicId name;
00033       std::string collName;
00034       size_t index;
00035       float defaultValue;
00036       boost::shared_ptr<reco::tau::RecoTauDiscriminantPlugin> plugin;
00037     };
00038 
00039     PFTauMVAInputDiscriminantTranslator(const edm::ParameterSet&);
00040     void produce(edm::Event&, const edm::EventSetup&);
00041 
00042   private:
00043     edm::InputTag pfTauSource_;
00044     std::vector<DiscriminantInfo> discriminators_;
00045 };
00046 
00047 PFTauMVAInputDiscriminantTranslator::PFTauMVAInputDiscriminantTranslator(
00048     const edm::ParameterSet& pset) {
00049   typedef std::vector<edm::ParameterSet> VPSet;
00050   pfTauSource_ = pset.getParameter<edm::InputTag>("pfTauSource");
00051   VPSet discriminants = pset.getParameter<VPSet>("discriminants");
00052 
00053   for(VPSet::const_iterator iDisc = discriminants.begin();
00054       iDisc != discriminants.end(); ++iDisc) {
00055     // WTF IS GOING ON HERE
00056     std::string name = iDisc->getParameter<std::string>("name");
00057     double defaultValue = (iDisc->exists("default")) ?
00058         iDisc->getParameter<double>("default") : 0.;
00059     // check if we are getting multiple indices
00060     bool requestMultiple = iDisc->exists("indices");
00061     if(requestMultiple) {
00062       // make a discrimiantor for each desired index
00063       std::vector<uint32_t> indices =
00064           iDisc->getParameter<std::vector<uint32_t> >("indices");
00065       for(std::vector<uint32_t>::const_iterator index = indices.begin();
00066           index != indices.end(); ++index) {
00067         DiscriminantInfo newDisc;
00068         newDisc.name = name;
00069         newDisc.index = *index;
00070         newDisc.defaultValue = defaultValue;
00071         // make a nice colleciton name
00072         std::stringstream collectionName;
00073         collectionName << name << *index;
00074         newDisc.collName = collectionName.str();
00075         // Build the plugin
00076         edm::ParameterSet fakePSet;
00077         newDisc.plugin.reset(
00078             RecoTauDiscriminantPluginFactory::get()->create(
00079                 reco::tau::discPluginName(name), fakePSet));
00080         discriminators_.push_back(newDisc);
00081       }
00082     } else {
00083       //single discriminant
00084       DiscriminantInfo newDisc;
00085       newDisc.name = name;
00086       newDisc.collName = name;
00087       newDisc.index = 0;
00088       newDisc.defaultValue = defaultValue;
00089       // Build the plugin
00090       edm::ParameterSet fakePSet;
00091       newDisc.plugin.reset(
00092           RecoTauDiscriminantPluginFactory::get()->create(
00093               reco::tau::discPluginName(name), fakePSet));
00094       discriminators_.push_back(newDisc);
00095     }
00096   }
00097   // register products
00098   BOOST_FOREACH(const DiscriminantInfo& disc, discriminators_) {
00099     produces<PFTauDiscriminator>(disc.collName);
00100   }
00101 }
00102 
00103 void PFTauMVAInputDiscriminantTranslator::produce(edm::Event& evt,
00104                                                   const edm::EventSetup& es) {
00105   // Handle to get PFTaus to associated to
00106   edm::Handle<PFTauCollection> pfTaus;
00107   evt.getByLabel(pfTauSource_, pfTaus);
00108 
00109   BOOST_FOREACH(const DiscriminantInfo& disc, discriminators_) {
00110     // output for this discriminator
00111     std::auto_ptr<PFTauDiscriminator> output(
00112         new PFTauDiscriminator(edm::RefProd<PFTauCollection>(pfTaus)));
00113     // loop over taus
00114     for(size_t itau = 0; itau < pfTaus->size(); ++itau) {
00115       PFTauRef tauRef(pfTaus, itau);
00116       // discriminator result
00117       std::vector<double> result = (*disc.plugin)(tauRef);
00118       // The desired index
00119       double selected_result = disc.defaultValue;
00120       if (result.size()-1 < disc.index) {
00121         selected_result = result[disc.index];
00122       }
00123       output->setValue(itau, selected_result);
00124     }
00125     evt.put(output, disc.collName);
00126   }
00127 }
00128 
00129 DEFINE_FWK_MODULE(PFTauMVAInputDiscriminantTranslator);