CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecoTauMVATrainer.cc
Go to the documentation of this file.
1 /*
2  * RecoTauMVATrainer
3  *
4  * Pass either signal or background events (with an option weight) to the
5  * MVATrainer interface.
6  *
7  * Author: Evan K. Friis
8  */
9 
10 #include <boost/foreach.hpp>
11 #include <string>
12 
17 
20 
25 
27  public:
28  explicit RecoTauMVATrainer(const edm::ParameterSet &pset);
29  virtual ~RecoTauMVATrainer() {};
30  virtual void analyze(const edm::Event &evt, const edm::EventSetup &es);
31  private:
38 };
39 
41  : mva_(pset.getParameter<std::string>("computerName"),
42  pset.getParameter<std::string>("dbLabel"),
43  pset.getParameter<edm::ParameterSet>("discriminantOptions")),
44  signalSrc_(pset.getParameter<edm::InputTag>("signalSrc")),
45  backgroundSrc_(pset.getParameter<edm::InputTag>("backgroundSrc")) {
46  // Check if we want to apply weights
47  applyWeights_ = false;
48  if (pset.exists("signalWeights")) {
49  applyWeights_ = true;
50  signalWeightsSrc_ = pset.getParameter<edm::InputTag>("signalWeights");
51  backgroundWeightsSrc_ = pset.getParameter<edm::InputTag>("backgroundWeights");
52  }
53  }
54 
55 namespace {
56 
57 // Upload a view to the MVA
58 void uploadTrainingData(reco::tau::RecoTauMVAHelper *helper,
61  bool isSignal) {
62  // Convert to a vector of refs
63  reco::PFTauRefVector tauRefs =
64  reco::tau::castView<reco::PFTauRefVector>(taus);
65  // Loop over our taus and pass each to the MVA interface
66  BOOST_FOREACH(reco::PFTauRef tau, tauRefs) {
67  // Lookup the weight if desired
68  double weight = (weights.isValid()) ? (*weights)[tau] : 1.0;
69  helper->train(tau, isSignal, weight);
70  }
71 }
72 
73 }
74 
75 
77  const edm::EventSetup &es) {
78  // Make sure the MVA is up to date from the DB
79  mva_.setEvent(evt, es);
80 
81  // Get a view to our taus
84 
85  bool signalExists = true;
86  try {
87  evt.getByLabel(signalSrc_, signal);
88  if (!signal.isValid())
89  signalExists = false;
90  } catch(...) {
91  signalExists = false;
92  }
93 
94  bool backgroundExists = true;
95  try {
96  evt.getByLabel(backgroundSrc_, background);
97  if (!background.isValid())
98  backgroundExists = false;
99  } catch(...) {
100  backgroundExists = false;
101  }
102 
103  // Get weights if desired
105  edm::Handle<reco::PFTauDiscriminator> backgroundWeights;
106  if (applyWeights_ && signalExists)
107  evt.getByLabel(signalWeightsSrc_, signalWeights);
108  if (applyWeights_ && backgroundExists)
109  evt.getByLabel(backgroundWeightsSrc_, backgroundWeights);
110 
111  if (signalExists)
112  uploadTrainingData(&mva_, signal, signalWeights, true);
113  if (backgroundExists)
114  uploadTrainingData(&mva_, background, backgroundWeights, false);
115 }
116 
T getParameter(std::string const &) const
void setEvent(const edm::Event &evt, const edm::EventSetup &es)
edm::InputTag signalWeightsSrc_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
edm::InputTag backgroundSrc_
bool isValid() const
Definition: HandleBase.h:76
RecoTauMVATrainer(const edm::ParameterSet &pset)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:355
virtual ~RecoTauMVATrainer()
virtual void analyze(const edm::Event &evt, const edm::EventSetup &es)
edm::InputTag signalSrc_
void train(const PFTauRef &tau, bool target, double weight=1.0) const
reco::tau::RecoTauMVAHelper mva_
edm::InputTag backgroundWeightsSrc_