CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ProcNormalize.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MVAComputer
4 // Class : ProcNormalize
5 //
6 
7 // Implementation:
8 // Normalizes the input variables. n values in each input variable
9 // is normalized to n values for each input variables. The normalization
10 // consists of a range normalization step (min...max) and mapping step
11 // that equalizes using the probability distribution (via PDF).
12 //
13 // Author: Christophe Saout
14 // Created: Sat Apr 24 15:18 CEST 2007
15 //
16 
17 #include <vector>
18 
23 
24 using namespace PhysicsTools;
25 
26 namespace { // anonymous
27 
28 class ProcNormalize : public VarProcessor {
29  public:
30  typedef VarProcessor::Registry::Registry<ProcNormalize,
32 
33  ProcNormalize(const char *name,
35  const MVAComputer *computer);
36  virtual ~ProcNormalize() {}
37 
38  virtual void configure(ConfIterator iter, unsigned int n) override;
39  virtual void eval(ValueIterator iter, unsigned int n) const override;
40  virtual std::vector<double> deriv(
41  ValueIterator iter, unsigned int n) const override;
42 
43  private:
44  struct Map {
45  Map(const Calibration::HistogramF &pdf) :
46  min(pdf.range().min), width(pdf.range().width())
47  {
48  std::vector<double> values(
49  pdf.values().begin() + 1,
50  pdf.values().end() - 1);
51  spline.set(values.size(), &values.front());
52  }
53 
54  double min, width;
55  Spline spline;
56  };
57 
58  void findMap(ValueIterator iter, unsigned int n,
59  std::vector<Map>::const_iterator &begin,
60  std::vector<Map>::const_iterator &end) const;
61 
62  std::vector<Map> maps;
63  int categoryIdx;
64  unsigned int nCategories;
65 };
66 
67 static ProcNormalize::Registry registry("ProcNormalize");
68 
69 ProcNormalize::ProcNormalize(const char *name,
71  const MVAComputer *computer) :
72  VarProcessor(name, calib, computer),
73  maps(calib->distr.begin(), calib->distr.end()),
74  categoryIdx(calib->categoryIdx),
75  nCategories(1)
76 {
77 }
78 
79 void ProcNormalize::configure(ConfIterator iter, unsigned int n)
80 {
81  if (categoryIdx >= 0) {
82  if ((int)n < categoryIdx + 1)
83  return;
84  nCategories = maps.size() / (n - 1);
85  if (nCategories * (n - 1) != maps.size())
86  return;
87  } else if (n != maps.size())
88  return;
89 
90  int i = 0;
91  while(iter) {
92  if (categoryIdx == i++)
93  iter++(Variable::FLAG_NONE);
94  else
95  iter << iter++(Variable::FLAG_ALL);
96  }
97 }
98 
99 void ProcNormalize::findMap(ValueIterator iter, unsigned int n,
100  std::vector<Map>::const_iterator &begin,
101  std::vector<Map>::const_iterator &end) const
102 {
103  if (categoryIdx >= 0) {
104  ValueIterator iter2 = iter;
105  for(int i = 0; i < categoryIdx; i++)
106  ++iter2;
107 
108  int cat = (int)*iter2;
109  if (cat < 0 || (unsigned int)cat >= nCategories) {
110  for(; iter; ++iter)
111  iter();
112  return;
113  }
114 
115  begin = maps.begin() + cat * (n - 1);
116  end = begin + (n - 1);
117  } else {
118  begin = maps.begin();
119  end = maps.end();
120  }
121 
122 }
123 
124 void ProcNormalize::eval(ValueIterator iter, unsigned int n) const
125 {
126  std::vector<Map>::const_iterator map, last;
127  findMap(iter, n, map, last);
128 
129  for(int i = 0; map != last; ++iter, i++) {
130  if (i == categoryIdx)
131  continue;
132  for(double *value = iter.begin();
133  value < iter.end(); value++) {
134  double val = *value;
135  val = (val - map->min) / map->width;
136  val = map->spline.integral(val);
137  iter << val;
138  }
139  iter();
140  ++map;
141  }
142 }
143 
144 std::vector<double> ProcNormalize::deriv(ValueIterator iter,
145  unsigned int n) const
146 {
147  std::vector<Map>::const_iterator map, last;
148  findMap(iter, n, map, last);
149 
150  unsigned int size = 0;
151  for(ValueIterator iter2 = iter; iter2; ++iter2)
152  size += iter2.size();
153 
154  std::vector<double> result(size * size);
155 
156  unsigned int j = 0;
157  for(int i = 0; map != last; ++iter, i++) {
158  if (i == categoryIdx) {
159  j += iter.size();
160  continue;
161  }
162 
163  for(double *value = iter.begin();
164  value < iter.end(); value++, j++) {
165  double val = *value;
166  val = (val - map->min) / map->width;
167  val = map->spline.eval(val) *
168  (map->spline.numberOfEntries() - 1) /
169  (map->width * map->spline.getArea());
170  result[j * size + j] = val;
171  }
172  ++map;
173  }
174 
175  return result;
176 }
177 
178 } // anonymous namespace
int i
Definition: DBlmapReader.cc:9
template to generate a registry singleton for a type.
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
def cat
Definition: eostools.py:400
tuple result
Definition: query.py:137
int j
Definition: DBlmapReader.cc:9
#define end
Definition: vmac.h:37
T min(T a, T b)
Definition: MathUtil.h:58
const std::vector< Value_t > & values() const
Definition: Histogram.h:86
#define begin
Definition: vmac.h:30
static Interceptor::Registry registry("Interceptor")
tuple size
Write out results.
A simple class for cubic splines.
Definition: Spline.h:25
Common base class for variable processors.
Definition: VarProcessor.h:36