CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecoTauMVAHelper.cc
Go to the documentation of this file.
3 
4 #include <boost/foreach.hpp>
5 #include <boost/bind.hpp>
6 
10 
12 
13 namespace reco { namespace tau {
14 
16  const std::string eslabel):
17  name_(name), eslabel_(eslabel) {}
18 
20  const edm::EventSetup &es) {
21  // Update the event info for all of our discriminators
22  BOOST_FOREACH(PluginMap::value_type plugin, plugins_) {
23  plugin.second->setup(evt, es);
24  }
25  // Update our MVA from the DB
28  if (eslabel_.size()) {
29  es.get<TauTagMVAComputerRcd>().get(eslabel_, handle);
30  } else {
31  es.get<TauTagMVAComputerRcd>().get(handle);
32  }
33  const MVAComputerContainer *container = handle.product();
34  // Load our MVA
35  bool reload = computer_.update(container, name_.c_str());
36  // If the MVA changed, update our list of discriminant plugins
37  if (reload && computer_.get())
38  loadDiscriminantPlugins(container->find(name_));
39 }
40 
43  typedef std::vector<PhysicsTools::Calibration::Variable> VarList;
44  // List of input variables for this MVA.
45  const VarList &vars = comp.inputSet;
46  // Load the plugin for each of the Var if needed
47  BOOST_FOREACH(const VarList::value_type& var, vars) {
48  // Check to make sure it isn't a magic variable
49  if (std::strncmp(var.name.c_str(), "__", 2) != 0) {
50  // If we haven't added yet, build it.
51  PhysicsTools::AtomicId varId(var.name);
52  if (!plugins_.count(varId)) {
53  edm::ParameterSet fakePSet;
54  fakePSet.addParameter("name", "MVA_" + var.name);
55  plugins_.insert(
57  reco::tau::discPluginName(var.name), fakePSet));
58  }
59  }
60  }
61 }
62 
64  // Loop over the relevant discriminators and the output
65  for (PluginMap::const_iterator plugin = plugins_.begin();
66  plugin != plugins_.end(); ++plugin) {
67  PhysicsTools::AtomicId id = plugin->first;
68  std::vector<double> pluginOutput = (plugin->second)->operator()(tau);
69  // Check for nans
70  for(size_t instance = 0; instance < pluginOutput.size(); ++instance) {
71  if (std::isnan(pluginOutput[instance])) {
72  edm::LogError("CorruptedMVAInput") << "A nan was detected in"
73  << " the tau MVA variable " << id << " returning zero instead!";
74  pluginOutput[instance] = 0.0;
75  }
76  }
77  //std::cout << "id: " << id << " first: " << pluginOutput[0] << std::endl;
78  // Build values and copy into values vector
79  std::for_each(pluginOutput.begin(), pluginOutput.end(),
81  boost::ref(values_), id, _1));
82  }
83 }
84 
85 // Get values
88  values_.clear();
89  fillValues(tau);
90  return values_;
91 }
92 
93 // Apply the MVA to a given tau
95  // Clear output
96  values_.clear();
97  // Build the values
98  fillValues(tau);
99  // Call the MVA
100  return computer_->eval(values_);
101 }
102 
104  double weight) const {
105  static const PhysicsTools::AtomicId kTargetId("__TARGET__");
106  static const PhysicsTools::AtomicId kWeightId("__WEIGHT__");
107  if (!computer_)
108  return;
109  values_.clear();
110  values_.add(kTargetId, target);
111  values_.add(kWeightId, weight);
112  // Build the discriminant values
113  fillValues(tau);
115 }
116 
117 }} // end namespace reco::tau
void setEvent(const edm::Event &evt, const edm::EventSetup &es)
double eval(Iterator_t first, Iterator_t last) const
evaluate variables given by a range of iterators given by first and last
void fillValues(const reco::PFTauRef &tau) const
Container::value_type value_type
Cheap generic unique keyword identifier class.
Definition: AtomicId.h:32
std::string discPluginName(const std::string &mvaName)
PhysicsTools::Variable::ValueList values_
bool isnan(float x)
Definition: math.h:13
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:138
tuple handle
Definition: patZpeak.py:22
double operator()(const PFTauRef &tau) const
const PhysicsTools::Variable::ValueList & discriminants(const PFTauRef &tau) const
Helper class that can contain an list of identifier-value pairs.
Definition: Variable.h:82
bool update(const Calibration::MVAComputer *computer)
void train(const PFTauRef &tau, bool target, double weight=1.0) const
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
RecoTauMVAHelper(const std::string &name, const std::string eslabel="")
std::vector< Variable > inputSet
Definition: MVAComputer.h:177
PhysicsTools::MVAComputerCache computer_
void loadDiscriminantPlugins(const PhysicsTools::Calibration::MVAComputer &computer)
SurfaceDeformation * create(int type, const std::vector< double > &params)
T get(const Candidate &c)
Definition: component.h:56
void add(AtomicId id, double value)
Definition: Variable.h:107