CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ProcMultiply.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MVAComputer
4 // Class :
5 //
6 
7 // Implementation:
8 // Multiplies n input variables to produce one output variable.
9 //
10 // Author: Christophe Saout
11 // Created: Sat Apr 24 15:18 CEST 2007
12 // $Id: ProcMultiply.cc,v 1.5 2009/06/03 09:50:14 saout Exp $
13 //
14 
15 #include <stdlib.h>
16 #include <algorithm>
17 #include <iterator>
18 #include <vector>
19 
21 
24 
25 using namespace PhysicsTools;
26 
27 namespace { // anonymous
28 
29 class ProcMultiply : public VarProcessor {
30  public:
31  typedef VarProcessor::Registry::Registry<ProcMultiply,
33 
34  ProcMultiply(const char *name,
36  const MVAComputer *computer);
37  virtual ~ProcMultiply() {}
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  typedef std::vector<unsigned int> Config;
46 
47  unsigned int in;
48  std::vector<Config> out;
49 };
50 
51 static ProcMultiply::Registry registry("ProcMultiply");
52 
53 ProcMultiply::ProcMultiply(const char *name,
55  const MVAComputer *computer) :
56  VarProcessor(name, calib, computer),
57  in(calib->in)
58 {
59  std::copy(calib->out.begin(), calib->out.end(),
60  std::back_inserter(out));
61 }
62 
63 void ProcMultiply::configure(ConfIterator iter, unsigned int n)
64 {
65  if (in != n)
66  return;
67 
68  for(unsigned int i = 0; i < in; i++)
69  iter++(Variable::FLAG_NONE);
70 
71  for(unsigned int i = 0; i < out.size(); i++)
72  iter << Variable::FLAG_NONE;
73 }
74 
75 void ProcMultiply::eval(ValueIterator iter, unsigned int n) const
76 {
77  double *values = (double*)alloca(in * sizeof(double));
78  for(double *pos = values; iter; iter++, pos++) {
79  if (iter.size() != 1)
80  throw cms::Exception("ProcMultiply")
81  << "Special input variable encountered "
82  "at index " << (pos - values) << "."
83  << std::endl;
84  *pos = *iter;
85  }
86 
87  for(std::vector<Config>::const_iterator config = out.begin();
88  config != out.end(); ++config) {
89  double product = 1.0;
90  for(std::vector<unsigned int>::const_iterator var =
91  config->begin();
92  var != config->end(); var++)
93  product *= values[*var];
94 
95  iter(product);
96  }
97 }
98 
99 std::vector<double> ProcMultiply::deriv(
100  ValueIterator iter, unsigned int n) const
101 {
102  std::vector<double> values;
103  std::vector<unsigned int> offsets;
104  unsigned int size = 0;
105  while(iter) {
106  offsets.push_back(size);
107  size += iter.size();
108  values.push_back(*iter++);
109  }
110 
111  std::vector<double> result(out.size() * size, 0.0);
112  unsigned int k = 0;
113  for(std::vector<Config>::const_iterator config = out.begin();
114  config != out.end(); ++config, k++) {
115  for(unsigned int i = 0; i < config->size(); i++) {
116  double product = 1.0;
117  for(unsigned int j = 0; j < config->size(); j++)
118  if (i != j)
119  product *= values[(*config)[j]];
120 
121  result[k * size + offsets[i]] = product;
122  }
123  }
124 
125  return result;
126 }
127 
128 } // anonymous namespace
int i
Definition: DBlmapReader.cc:9
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
int k[5][pyjets_maxn]
tuple out
Definition: dbtoconf.py:99
template to generate a registry singleton for a type.
static Interceptor::Registry registry("Interceptor")
tuple size
Write out results.
Common base class for variable processors.
Definition: VarProcessor.h:39