CMS 3D CMS Logo

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:
31 
32  ProcNormalize(const char *name, const Calibration::ProcNormalize *calib, const MVAComputer *computer);
33  ~ProcNormalize() override {}
34 
35  void configure(ConfIterator iter, unsigned int n) override;
36  void eval(ValueIterator iter, unsigned int n) const override;
37  std::vector<double> deriv(ValueIterator iter, unsigned int n) const override;
38 
39  private:
40  struct Map {
41  Map(const Calibration::HistogramF &pdf) : min(pdf.range().min), width(pdf.range().width()) {
42  std::vector<double> values(pdf.values().begin() + 1, pdf.values().end() - 1);
43  spline.set(values.size(), &values.front());
44  }
45 
46  double min, width;
47  Spline spline;
48  };
49 
50  void findMap(ValueIterator iter,
51  unsigned int n,
52  std::vector<Map>::const_iterator &begin,
53  std::vector<Map>::const_iterator &end) const;
54 
55  std::vector<Map> maps;
56  int categoryIdx;
57  unsigned int nCategories;
58  };
59 
60  ProcNormalize::Registry registry("ProcNormalize");
61 
62  ProcNormalize::ProcNormalize(const char *name, const Calibration::ProcNormalize *calib, const MVAComputer *computer)
64  maps(calib->distr.begin(), calib->distr.end()),
65  categoryIdx(calib->categoryIdx),
66  nCategories(1) {}
67 
68  void ProcNormalize::configure(ConfIterator iter, unsigned int n) {
69  if (categoryIdx >= 0) {
70  if ((int)n < categoryIdx + 1)
71  return;
72  nCategories = maps.size() / (n - 1);
73  if (nCategories * (n - 1) != maps.size())
74  return;
75  } else if (n != maps.size())
76  return;
77 
78  int i = 0;
79  while (iter) {
80  if (categoryIdx == i++)
81  iter++(Variable::FLAG_NONE);
82  else
83  iter << iter++(Variable::FLAG_ALL);
84  }
85  }
86 
87  void ProcNormalize::findMap(ValueIterator iter,
88  unsigned int n,
89  std::vector<Map>::const_iterator &begin,
90  std::vector<Map>::const_iterator &end) const {
91  if (categoryIdx >= 0) {
92  ValueIterator iter2 = iter;
93  for (int i = 0; i < categoryIdx; i++)
94  ++iter2;
95 
96  int cat = (int)*iter2;
97  if (cat < 0 || (unsigned int)cat >= nCategories) {
98  for (; iter; ++iter)
99  iter();
100  return;
101  }
102 
103  begin = maps.begin() + cat * (n - 1);
104  end = begin + (n - 1);
105  } else {
106  begin = maps.begin();
107  end = maps.end();
108  }
109  }
110 
111  void ProcNormalize::eval(ValueIterator iter, unsigned int n) const {
112  std::vector<Map>::const_iterator map, last;
113  findMap(iter, n, map, last);
114 
115  for (int i = 0; map != last; ++iter, i++) {
116  if (i == categoryIdx)
117  continue;
118  for (double *value = iter.begin(); value < iter.end(); value++) {
119  double val = *value;
120  val = (val - map->min) / map->width;
121  val = map->spline.integral(val);
122  iter << val;
123  }
124  iter();
125  ++map;
126  }
127  }
128 
129  std::vector<double> ProcNormalize::deriv(ValueIterator iter, unsigned int n) const {
130  std::vector<Map>::const_iterator map, last;
131  findMap(iter, n, map, last);
132 
133  unsigned int size = 0;
134  for (ValueIterator iter2 = iter; iter2; ++iter2)
135  size += iter2.size();
136 
137  std::vector<double> result(size * size);
138 
139  unsigned int j = 0;
140  for (int i = 0; map != last; ++iter, i++) {
141  if (i == categoryIdx) {
142  j += iter.size();
143  continue;
144  }
145 
146  for (double *value = iter.begin(); value < iter.end(); value++, j++) {
147  double val = *value;
148  val = (val - map->min) / map->width;
149  val = map->spline.eval(val) * (map->spline.numberOfEntries() - 1) / (map->width * map->spline.getArea());
150  result[j * size + j] = val;
151  }
152  ++map;
153  }
154 
155  return result;
156  }
157 
158 } // anonymous namespace
size
Write out results.
const std::vector< Value_t > & values() const
Definition: Histogram.h:82
template to generate a registry singleton for a type.
def cat(path)
Definition: eostools.py:401
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
Definition: value.py:1
A simple class for cubic splines.
Definition: Spline.h:25
Common base class for variable processors.
Definition: VarProcessor.h:36