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
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
ApeEstimator_cff.width
width
Definition: ApeEstimator_cff.py:24
mps_fire.i
i
Definition: mps_fire.py:355
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
min
T min(T a, T b)
Definition: MathUtil.h:58
eostools.cat
def cat(path)
Definition: eostools.py:401
PhysicsTools::Calibration::Histogram::values
const std::vector< Value_t > & values() const
Definition: Histogram.h:82
PhysicsTools::MVAComputer
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
end
#define end
Definition: vmac.h:39
dqmdumpme.last
last
Definition: dqmdumpme.py:56
PhysicsTools::VarProcessor
Common base class for variable processors.
Definition: VarProcessor.h:36
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
Histogram.h
PhysicsTools::Calibration::ProcNormalize
Definition: MVAComputer.h:137
calib
Definition: CalibElectron.h:12
PhysicsTools
Definition: Histogram.h:13
createfilelist.int
int
Definition: createfilelist.py:10
VarProcessor.h
value
Definition: value.py:1
PhysicsTools::Calibration::Histogram< float >
mvaElectronID_Fall17_iso_V1_cff.nCategories
nCategories
Definition: mvaElectronID_Fall17_iso_V1_cff.py:86
heppy_batch.val
val
Definition: heppy_batch.py:351
PhysicsTools::ProcessRegistryImpl
template to generate a registry singleton for a type.
Definition: ProcessRegistry.h:25
relativeConstraints.value
value
Definition: relativeConstraints.py:53
PhysicsTools::Spline
A simple class for cubic splines.
Definition: Spline.h:25
Calibration.h
HLT_2018_cff.computer
computer
Definition: HLT_2018_cff.py:50520
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
mps_fire.result
result
Definition: mps_fire.py:303
genParticles_cff.map
map
Definition: genParticles_cff.py:11
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
begin
#define begin
Definition: vmac.h:32
Spline.h
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443