Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <boost/foreach.hpp>
00011 #include <string>
00012
00013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/Framework/interface/EventSetup.h"
00016 #include "FWCore/Framework/interface/EDAnalyzer.h"
00017
00018 #include "RecoTauTag/RecoTau/interface/RecoTauMVAHelper.h"
00019 #include "RecoTauTag/RecoTau/interface/RecoTauCommonUtilities.h"
00020
00021 #include "DataFormats/TauReco/interface/PFTauFwd.h"
00022 #include "DataFormats/TauReco/interface/PFTau.h"
00023 #include "DataFormats/TauReco/interface/PFTauDiscriminator.h"
00024 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00025
00026 class RecoTauMVATrainer : public edm::EDAnalyzer {
00027 public:
00028 explicit RecoTauMVATrainer(const edm::ParameterSet &pset);
00029 virtual ~RecoTauMVATrainer() {};
00030 virtual void analyze(const edm::Event &evt, const edm::EventSetup &es);
00031 private:
00032 reco::tau::RecoTauMVAHelper mva_;
00033 edm::InputTag signalSrc_;
00034 edm::InputTag backgroundSrc_;
00035 bool applyWeights_;
00036 edm::InputTag signalWeightsSrc_;
00037 edm::InputTag backgroundWeightsSrc_;
00038 };
00039
00040 RecoTauMVATrainer::RecoTauMVATrainer(const edm::ParameterSet &pset)
00041 : mva_(pset.getParameter<std::string>("computerName"),
00042 pset.getParameter<std::string>("dbLabel")),
00043 signalSrc_(pset.getParameter<edm::InputTag>("signalSrc")),
00044 backgroundSrc_(pset.getParameter<edm::InputTag>("backgroundSrc")) {
00045
00046 applyWeights_ = false;
00047 if (pset.exists("signalWeights")) {
00048 applyWeights_ = true;
00049 signalWeightsSrc_ = pset.getParameter<edm::InputTag>("signalWeights");
00050 backgroundWeightsSrc_ = pset.getParameter<edm::InputTag>("backgroundWeights");
00051 }
00052 }
00053
00054 namespace {
00055
00056
00057 void uploadTrainingData(reco::tau::RecoTauMVAHelper *helper,
00058 const edm::Handle<reco::CandidateView>& taus,
00059 const edm::Handle<reco::PFTauDiscriminator>& weights,
00060 bool isSignal) {
00061
00062 reco::PFTauRefVector tauRefs =
00063 reco::tau::castView<reco::PFTauRefVector>(taus);
00064
00065 BOOST_FOREACH(reco::PFTauRef tau, tauRefs) {
00066
00067 double weight = (weights.isValid()) ? (*weights)[tau] : 1.0;
00068 helper->train(tau, isSignal, weight);
00069 }
00070 }
00071
00072 }
00073
00074
00075 void RecoTauMVATrainer::analyze(const edm::Event &evt,
00076 const edm::EventSetup &es) {
00077
00078 mva_.setEvent(evt, es);
00079
00080
00081 edm::Handle<reco::CandidateView> signal;
00082 edm::Handle<reco::CandidateView> background;
00083
00084 bool signalExists = true;
00085 try {
00086 evt.getByLabel(signalSrc_, signal);
00087 if (!signal.isValid())
00088 signalExists = false;
00089 } catch(...) {
00090 signalExists = false;
00091 }
00092
00093 bool backgroundExists = true;
00094 try {
00095 evt.getByLabel(backgroundSrc_, background);
00096 if (!background.isValid())
00097 backgroundExists = false;
00098 } catch(...) {
00099 backgroundExists = false;
00100 }
00101
00102
00103 edm::Handle<reco::PFTauDiscriminator> signalWeights;
00104 edm::Handle<reco::PFTauDiscriminator> backgroundWeights;
00105 if (applyWeights_ && signalExists)
00106 evt.getByLabel(signalWeightsSrc_, signalWeights);
00107 if (applyWeights_ && backgroundExists)
00108 evt.getByLabel(backgroundWeightsSrc_, backgroundWeights);
00109
00110 if (signalExists)
00111 uploadTrainingData(&mva_, signal, signalWeights, true);
00112 if (backgroundExists)
00113 uploadTrainingData(&mva_, background, backgroundWeights, false);
00114 }
00115
00116 #include "FWCore/Framework/interface/MakerMacros.h"
00117 DEFINE_FWK_MODULE(RecoTauMVATrainer);