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:
32  typedef std::vector<edm::InputTag> VInputTag;
40 };
41 
43  : mva_(pset.getParameter<std::string>("computerName"),
44  pset.getParameter<std::string>("dbLabel"),
45  pset.getParameter<edm::ParameterSet>("discriminantOptions")),
46  signalSrc_(pset.getParameter<edm::InputTag>("signalSrc")),
47  backgroundSrc_(pset.getParameter<edm::InputTag>("backgroundSrc")) {
48  // Check if we want to apply weights
49  applyWeights_ = false;
50  if (pset.exists("signalWeights")) {
51  applyWeights_ = true;
52  signalWeightsSrc_ = pset.getParameter<edm::InputTag>("signalWeights");
53  backgroundWeightsSrc_ = pset.getParameter<edm::InputTag>("backgroundWeights");
54  }
55  if (pset.exists("eventWeights")) {
56  eventWeights_ = pset.getParameter<VInputTag>("eventWeights");
57  }
58  }
59 
60 namespace {
61 
62 // Upload a view to the MVA
63 void uploadTrainingData(reco::tau::RecoTauMVAHelper *helper,
66  bool isSignal, double eventWeight) {
67  // Convert to a vector of refs
68  reco::PFTauRefVector tauRefs =
69  reco::tau::castView<reco::PFTauRefVector>(taus);
70  // Loop over our taus and pass each to the MVA interface
71  BOOST_FOREACH(reco::PFTauRef tau, tauRefs) {
72  // Lookup the weight if desired
73  double weight = (weights.isValid()) ? (*weights)[tau] : 1.0;
74  helper->train(tau, isSignal, weight*eventWeight);
75  }
76 }
77 
78 }
79 
80 
82  const edm::EventSetup &es) {
83  // Get a view to our taus
86 
87  bool signalExists = false;
88  evt.getByLabel(signalSrc_, signal);
89  if (signal.isValid() && signal->size())
90  signalExists = true;
91 
92  bool backgroundExists = false;
93  evt.getByLabel(backgroundSrc_, background);
94  if (background.isValid() && background->size())
95  backgroundExists = true;
96 
97  // Check if we have anything to do
98  bool somethingToDo = signalExists || backgroundExists;
99  if (!somethingToDo)
100  return;
101 
102  // Make sure the MVA is up to date from the DB
103  mva_.setEvent(evt, es);
104 
105  // Get event weights if specified
106  double eventWeight = 1.0;
107  BOOST_FOREACH(const edm::InputTag& weightTag, eventWeights_) {
108  edm::Handle<double> weightHandle;
109  evt.getByLabel(weightTag, weightHandle);
110  if (weightHandle.isValid())
111  eventWeight *= *weightHandle;
112  }
113 
114  // Get weights if desired
116  edm::Handle<reco::PFTauDiscriminator> backgroundWeights;
117  if (applyWeights_ && signalExists)
118  evt.getByLabel(signalWeightsSrc_, signalWeights);
119  if (applyWeights_ && backgroundExists)
120  evt.getByLabel(backgroundWeightsSrc_, backgroundWeights);
121 
122  if (signalExists)
123  uploadTrainingData(&mva_, signal, signalWeights, true, eventWeight);
124  if (backgroundExists)
125  uploadTrainingData(&mva_, background, backgroundWeights,
126  false, eventWeight);
127 }
128 
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
std::vector< edm::InputTag > VInputTag
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:356
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_