CMS 3D CMS Logo

MVAModuleHelper.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_MVAComputer_MVAModuleHelper_h
2 #define PhysicsTools_MVAComputer_MVAModuleHelper_h
3 // -*- C++ -*-
4 //
5 // Package: MVAComputer
6 // Class : MVAModuleHelper
7 //
8 
9 //
10 // Author: Christophe Saout <christophe.saout@cern.ch>
11 // Created: Sat Apr 24 15:18 CEST 2007
12 //
13 
14 #include <functional>
15 #include <algorithm>
16 #include <numeric>
17 #include <cstring>
18 #include <string>
19 #include <vector>
20 #include <cmath>
21 
23 
28 
29 namespace PhysicsTools {
30 
39  template <typename Object>
42 
43  double operator()(const Object &object, const PhysicsTools::AtomicId &name) { return object.compute(name); }
44  };
45 
56  template <class Record, typename Object, class Filler = MVAModuleHelperDefaultFiller<Object> >
58  public:
60  MVAModuleHelper(const MVAModuleHelper &orig) : label(orig.label) {}
62 
63  void setEventSetup(const edm::EventSetup &setup);
64  void setEventSetup(const edm::EventSetup &setup, const char *esLabel);
65 
66  double operator()(const Object &object) const;
67 
68  void train(const Object &object, bool target, double weight = 1.0) const;
69 
70  private:
72 
75 
76  class Value {
77  public:
79  Value(const std::string &name, double value) : name(name), filler(name), value(value) {}
80 
81  inline bool update(const Object &object) const {
82  value = filler(object, name);
83  return !std::isfinite(value);
84  }
85 
86  PhysicsTools::AtomicId getName() const { return name; }
87  double getValue() const { return value; }
88 
89  private:
91  Filler filler;
92 
93  mutable double value;
94  };
95 
96  std::vector<Value> values;
97  };
98 
99  template <class Record, typename Object, class Filler>
102  setup.get<Record>().get(handle);
103  const PhysicsTools::Calibration::MVAComputerContainer *container = handle.product();
104  if (cache.update(container, label.c_str()) && cache)
105  init(container);
106  }
107 
108  template <class Record, typename Object, class Filler>
111  setup.get<Record>().get(esLabel, handle);
112  const PhysicsTools::Calibration::MVAComputerContainer *container = handle.product();
113  if (cache.update(container, label.c_str()) && cache)
114  init(container);
115  }
116 
117  template <class Record, typename Object, class Filler>
119  const std::vector<PhysicsTools::Calibration::Variable> &vars = container->find(label).inputSet;
120  values.clear();
121  for (std::vector<PhysicsTools::Calibration::Variable>::const_iterator iter = vars.begin(); iter != vars.end();
122  ++iter)
123  if (std::strncmp(iter->name.c_str(), "__", 2) != 0)
124  values.push_back(Value(iter->name));
125  }
126 
127  template <class Record, typename Object, class Filler>
128  double MVAModuleHelper<Record, Object, Filler>::operator()(const Object &object) const {
129  std::for_each(values.begin(), values.end(), std::bind(&Value::update, std::placeholders::_1, object));
130  return cache->eval(values);
131  }
132 
133  template <class Record, typename Object, class Filler>
134  void MVAModuleHelper<Record, Object, Filler>::train(const Object &object, bool target, double weight) const {
135  static const PhysicsTools::AtomicId kTargetId("__TARGET__");
136  static const PhysicsTools::AtomicId kWeightId("__WEIGHT__");
137 
138  if (!cache)
139  return;
140 
141  if (std::accumulate(
142  values.begin(),
143  values.end(),
144  0,
145  std::bind(
146  std::plus<int>(), std::placeholders::_1, std::bind(&Value::update, std::placeholders::_2, object))))
147  return;
148 
150  list.add(kTargetId, target);
151  list.add(kWeightId, weight);
152  for (typename std::vector<Value>::const_iterator iter = values.begin(); iter != values.end(); ++iter)
153  list.add(iter->getName(), iter->getValue());
154 
155  cache->eval(list);
156  }
157 
158 } // namespace PhysicsTools
159 
160 #endif // PhysicsTools_MVAComputer_MVAModuleHelper_h
PhysicsTools::AtomicId
Cheap generic unique keyword identifier class.
Definition: AtomicId.h:31
PhysicsTools::MVAModuleHelper::train
void train(const Object &object, bool target, double weight=1.0) const
Definition: MVAModuleHelper.h:134
init
int init
Definition: HydjetWrapper.h:64
PhysicsTools::MVAModuleHelper::setEventSetup
void setEventSetup(const edm::EventSetup &setup)
Definition: MVAModuleHelper.h:100
PhysicsTools::MVAModuleHelper::label
const std::string label
Definition: MVAModuleHelper.h:73
patZpeak.handle
handle
Definition: patZpeak.py:23
PhysicsTools::MVAModuleHelper::Value::Value
Value(const std::string &name)
Definition: MVAModuleHelper.h:78
PhysicsTools::MVAModuleHelper::Value::filler
Filler filler
Definition: MVAModuleHelper.h:91
PhysicsTools::MVAModuleHelper::Value::update
bool update(const Object &object) const
Definition: MVAModuleHelper.h:81
PhysicsTools::MVAModuleHelper
Template for automated variable collection and MVA computation in EDM modules.
Definition: MVAModuleHelper.h:57
PhysicsTools::Variable::ValueList
Helper class that can contain an list of identifier-value pairs.
Definition: Variable.h:77
singleTopDQM_cfi.setup
setup
Definition: singleTopDQM_cfi.py:37
PhysicsTools::MVAComputerCache
Creates and and MVAComputer from calibrations via EventSetup.
Definition: MVAComputerCache.h:28
PhysicsTools::MVAModuleHelperDefaultFiller::MVAModuleHelperDefaultFiller
MVAModuleHelperDefaultFiller(const PhysicsTools::AtomicId &name)
Definition: MVAModuleHelper.h:41
PhysicsTools::MVAModuleHelper::MVAModuleHelper
MVAModuleHelper(const MVAModuleHelper &orig)
Definition: MVAModuleHelper.h:60
PhysicsTools::MVAModuleHelper::MVAModuleHelper
MVAModuleHelper(const std::string &label)
Definition: MVAModuleHelper.h:59
vars
vars
Definition: DeepTauId.cc:164
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
PhysicsTools::MVAModuleHelper::Value::Value
Value(const std::string &name, double value)
Definition: MVAModuleHelper.h:79
PhysicsTools::MVAModuleHelper::Value::value
double value
Definition: MVAModuleHelper.h:93
PhysicsTools::MVAModuleHelper::Value::name
PhysicsTools::AtomicId name
Definition: MVAModuleHelper.h:90
edm::ESHandle
Definition: DTSurvey.h:22
AtomicId.h
PhysicsTools::MVAModuleHelper::Value::getValue
double getValue() const
Definition: MVAModuleHelper.h:87
PhysicsTools::Variable::ValueList::add
void add(AtomicId id, double value)
Definition: Variable.h:103
PhysicsTools::MVAModuleHelperDefaultFiller
Default template for MVAModuleHelper "Filler" template argument.
Definition: MVAModuleHelper.h:40
MVAComputer.h
PhysicsTools::MVAModuleHelper::init
void init(const PhysicsTools::Calibration::MVAComputerContainer *container)
Definition: MVAModuleHelper.h:118
PhysicsTools::MVAModuleHelper::cache
PhysicsTools::MVAComputerCache cache
Definition: MVAModuleHelper.h:74
utilities.cache
def cache(function)
Definition: utilities.py:3
PhysicsTools
Definition: Histogram.h:13
PhysicsTools::Calibration::MVAComputerContainer::find
virtual const MVAComputer & find(const std::string &label) const
Definition: MVAComputer.cc:162
PhysicsTools::MVAModuleHelper::values
std::vector< Value > values
Definition: MVAModuleHelper.h:96
value
Definition: value.py:1
edm::EventSetup
Definition: EventSetup.h:58
PhysicsTools::MVAModuleHelper::Value::getName
PhysicsTools::AtomicId getName() const
Definition: MVAModuleHelper.h:86
get
#define get
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
MVAComputerCache.h
PhysicsTools::MVAModuleHelper::~MVAModuleHelper
~MVAModuleHelper()
Definition: MVAModuleHelper.h:61
Record
Calibration.h
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
EventSetup.h
PhysicsTools::Calibration::MVAComputerContainer
Definition: MVAComputer.h:250
PhysicsTools::MVAModuleHelperDefaultFiller::operator()
double operator()(const Object &object, const PhysicsTools::AtomicId &name)
Definition: MVAModuleHelper.h:43
filterCSVwithJSON.target
target
Definition: filterCSVwithJSON.py:32
reco::JetExtendedAssociation::Value
reco::JetExtendedAssociation::JetExtendedData Value
Definition: JetExtendedAssociation.h:27
PhysicsTools::MVAModuleHelper::Value
Definition: MVAModuleHelper.h:76
PhysicsTools::MVAModuleHelper::operator()
double operator()(const Object &object) const
Definition: MVAModuleHelper.h:128
label
const char * label
Definition: PFTauDecayModeTools.cc:11
weight
Definition: weight.py:1
update
#define update(a, b)
Definition: TrackClassifier.cc:10