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 // $Id: ProcNormalize.cc,v 1.9 2009/06/03 09:50:14 saout Exp $
16 //
17 
18 #include <vector>
19 
24 
25 using namespace PhysicsTools;
26 
27 namespace { // anonymous
28 
29 class ProcNormalize : public VarProcessor {
30  public:
31  typedef VarProcessor::Registry::Registry<ProcNormalize,
33 
34  ProcNormalize(const char *name,
36  const MVAComputer *computer);
37  virtual ~ProcNormalize() {}
38 
39  virtual void configure(ConfIterator iter, unsigned int n);
40  virtual void eval(ValueIterator iter, unsigned int n) const;
41  virtual std::vector<double> deriv(
42  ValueIterator iter, unsigned int n) const;
43 
44  private:
45  struct Map {
46  Map(const Calibration::HistogramF &pdf) :
47  min(pdf.range().min), width(pdf.range().width())
48  {
49  std::vector<double> values(
50  pdf.values().begin() + 1,
51  pdf.values().end() - 1);
52  spline.set(values.size(), &values.front());
53  }
54 
55  double min, width;
56  Spline spline;
57  };
58 
59  void findMap(ValueIterator iter, unsigned int n,
60  std::vector<Map>::const_iterator &begin,
61  std::vector<Map>::const_iterator &end) const;
62 
63  std::vector<Map> maps;
64  int categoryIdx;
65  unsigned int nCategories;
66 };
67 
68 static ProcNormalize::Registry registry("ProcNormalize");
69 
70 ProcNormalize::ProcNormalize(const char *name,
72  const MVAComputer *computer) :
73  VarProcessor(name, calib, computer),
74  maps(calib->distr.begin(), calib->distr.end()),
75  categoryIdx(calib->categoryIdx),
76  nCategories(1)
77 {
78 }
79 
80 void ProcNormalize::configure(ConfIterator iter, unsigned int n)
81 {
82  if (categoryIdx >= 0) {
83  if ((int)n < categoryIdx + 1)
84  return;
85  nCategories = maps.size() / (n - 1);
86  if (nCategories * (n - 1) != maps.size())
87  return;
88  } else if (n != maps.size())
89  return;
90 
91  int i = 0;
92  while(iter) {
93  if (categoryIdx == i++)
94  iter++(Variable::FLAG_NONE);
95  else
96  iter << iter++(Variable::FLAG_ALL);
97  }
98 }
99 
100 void ProcNormalize::findMap(ValueIterator iter, unsigned int n,
101  std::vector<Map>::const_iterator &begin,
102  std::vector<Map>::const_iterator &end) const
103 {
104  if (categoryIdx >= 0) {
105  ValueIterator iter2 = iter;
106  for(int i = 0; i < categoryIdx; i++)
107  ++iter2;
108 
109  int cat = (int)*iter2;
110  if (cat < 0 || (unsigned int)cat >= nCategories) {
111  for(; iter; ++iter)
112  iter();
113  return;
114  }
115 
116  begin = maps.begin() + cat * (n - 1);
117  end = begin + (n - 1);
118  } else {
119  begin = maps.begin();
120  end = maps.end();
121  }
122 
123 }
124 
125 void ProcNormalize::eval(ValueIterator iter, unsigned int n) const
126 {
127  std::vector<Map>::const_iterator map, last;
128  findMap(iter, n, map, last);
129 
130  for(int i = 0; map != last; ++iter, i++) {
131  if (i == categoryIdx)
132  continue;
133  for(double *value = iter.begin();
134  value < iter.end(); value++) {
135  double val = *value;
136  val = (val - map->min) / map->width;
137  val = map->spline.integral(val);
138  iter << val;
139  }
140  iter();
141  ++map;
142  }
143 }
144 
145 std::vector<double> ProcNormalize::deriv(ValueIterator iter,
146  unsigned int n) const
147 {
148  std::vector<Map>::const_iterator map, last;
149  findMap(iter, n, map, last);
150 
151  unsigned int size = 0;
152  for(ValueIterator iter2 = iter; iter2; ++iter2)
153  size += iter2.size();
154 
155  std::vector<double> result(size * size);
156 
157  unsigned int j = 0;
158  for(int i = 0; map != last; ++iter, i++) {
159  if (i == categoryIdx) {
160  j += iter.size();
161  continue;
162  }
163 
164  for(double *value = iter.begin();
165  value < iter.end(); value++, j++) {
166  double val = *value;
167  val = (val - map->min) / map->width;
168  val = map->spline.eval(val) *
169  (map->spline.numberOfEntries() - 1) /
170  (map->width * map->spline.getArea());
171  result[j * size + j] = val;
172  }
173  ++map;
174  }
175 
176  return result;
177 }
178 
179 } // anonymous namespace
int i
Definition: DBlmapReader.cc:9
#define min(a, b)
Definition: mlp_lapack.h:161
detail::ThreadSafeRegistry< ParameterSetID, ParameterSet, ProcessParameterSetIDCache > Registry
Definition: Registry.h:37
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:40
tuple result
Definition: query.py:137
int j
Definition: DBlmapReader.cc:9
#define end
Definition: vmac.h:38
const std::vector< Value_t > & values() const
Definition: Histogram.h:78
#define begin
Definition: vmac.h:31
template to generate a registry singleton for a type.
static Interceptor::Registry registry("Interceptor")
tuple size
Write out results.
A simple class for cubic splines.
Definition: Spline.h:26
Common base class for variable processors.
Definition: VarProcessor.h:39