CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
VarProcessor.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MVAComputer
4 // Class : VarProcessor
5 //
6 
7 // Implementation:
8 // Base class for variable processors. Basically only passes calls
9 // through to virtual methods in the actual implementation daughter class.
10 //
11 // Author: Christophe Saout
12 // Created: Sat Apr 24 15:18 CEST 2007
13 // $Id: VarProcessor.cc,v 1.7 2010/02/19 10:28:37 saout Exp $
14 //
15 
17 
21 
25 
26 // #define DEBUG_DERIV
27 
28 #ifdef DEBUG_DERIV
29 # include <Reflex/Tools.h>
30 #endif
31 
33  "PhysicsToolsMVAComputer");
34 
35 namespace PhysicsTools {
36 
39  const MVAComputer *computer) :
40  computer(computer),
41  inputVars(Calibration::convert(calib->inputVars)),
42  nInputVars(inputVars.bits())
43 {
44 }
45 
47 {
48  inputVars = BitSet(0);
49  nInputVars = 0;
50 }
51 
53 {
54  ConfigCtx::size_type pos = config.size();
55  if (pos != inputVars.size())
56  return;
57 
59  configure(iter, nInputVars);
60 
61  VarProcessor *loop = config.loop ? config.loop : this;
62  ConfigCtx::Context *ctx =
63  loop->configureLoop(config.ctx, config.begin(),
64  config.begin() + pos, config.end());
65 
66  if (ctx != config.ctx) {
67  delete config.ctx;
68  config.ctx = ctx;
69  }
70 
71  if (config.loop && !ctx)
72  config.loop = 0;
73  else if (!config.loop && ctx)
74  config.loop = this;
75 }
76 
77 VarProcessor::ConfigCtx::ConfigCtx(std::vector<Variable::Flags> flags) :
78  loop(0), ctx(0)
79 {
80  for(std::vector<Variable::Flags>::const_iterator iter = flags.begin();
81  iter != flags.end(); ++iter)
82  configs.push_back(Config(*iter, 1));
83 }
84 
88 {
89  return 0;
90 }
91 
92 template<>
95  const char *name, const Calibration::VarProcessor *calib,
96  const MVAComputer *parent)
97 {
98  VarProcessor *result = ProcessRegistry::create(name, calib, parent);
99  if (!result) {
100  // try to load the shared library and retry
101  try {
102  delete VarProcessor::PluginFactory::get()->create(
103  std::string("VarProcessor/") + name);
104  result = ProcessRegistry::create(name, calib, parent);
105  } catch(const cms::Exception &e) {
106  // caller will have to deal with the null pointer
107  // in principle this will just give a slightly more
108  // descriptive error message (and will rethrow anyhow)
109 
110  edm::LogError("CannotBuildMVAProc")
111  << "Caught exception when building processor: "
112  << name << " message: " << std::endl
113  << e.what() << std::endl;
114  throw e;
115  }
116  }
117  return result;
118 }
119 
120 void VarProcessor::deriv(double *input, int *conf, double *output,
121  int *outConf, int *loop, unsigned int offset,
122  unsigned int in, unsigned int out_,
123  std::vector<double> &deriv) const
124 {
126  output, outConf, loop, offset);
127 
128  eval(iter, nInputVars);
129 
130  std::vector<double> matrix = this->deriv(iter, nInputVars);
131 
132  unsigned int size = 0;
133  while(iter)
134  size += (iter++).size();
135  unsigned int out = matrix.empty() ? 0 : (matrix.size() / size);
136 
137  if (matrix.size() != out * size ||
138  (out > 1 && (int)out != outConf[out_] - outConf[0]))
139  throw cms::Exception("VarProcessor")
140  << "Derivative matrix implausible size in "
141  << typeid(*this).name() << "."
142  << std::endl;
143 
144 #ifdef DEBUG_DERIV
145  if (!matrix.empty()) {
146  std::cout << "---------------- "
147  << ROOT::Reflex::Tools::Demangle(typeid(*this))
148  << std::endl;
149  for(unsigned int i = 0; i < out; i++) {
150  for(unsigned int j = 0; j < size; j++)
151  std::cout << matrix.at(i*size+j) << "\t";
152  std::cout << std::endl;
153  }
154  std::cout << "----------------" << std::endl;
155  }
156 
157  std::cout << "======= in = " << in << ", size = " << size
158  << ", out = " << out << ", matrix = " << matrix.size()
159  << std::endl;
160 #endif
161 
162  unsigned int sz = (outConf[out_] - in) * in;
163  unsigned int oldSz = deriv.size();
164  if (oldSz < sz)
165  deriv.resize(sz);
166 
167  double *begin = &deriv.front() + (outConf[0] - in + offset) * in;
168  double *end = begin + out * in;
169  if (begin < &deriv.front() + oldSz)
170  std::fill(begin, end, 0.0);
171 
172  if (matrix.empty())
173  return;
174 
175  double *m0 = &matrix.front();
177  for(unsigned int i = 0; i < nInputVars; i++, ++cur) {
178 #ifdef DEBUG_DERIV
179  std::cout << " inputvar " << i << std::endl;
180 #endif
181  int *curConf = conf + cur();
182  unsigned int pos = *curConf;
183 #ifdef DEBUG_DERIV
184  std::cout << " -> cur = " << cur() << ", pos = "
185  << pos << std::endl;
186 #endif
187  if (loop && curConf >= loop) {
188  pos += offset;
189  loop = 0;
190  }
191 
192  unsigned int n = loop ? (curConf[1] - curConf[0]) : 1;
193  for(unsigned int j = 0; j < n; m0++, j++, pos++) {
194 #ifdef DEBUG_DERIV
195  std::cout << " multip " << j << std::endl;
196 #endif
197  double *p = begin;
198  if (pos >= in) {
199 #ifdef DEBUG_DERIV
200  std::cout << " deriv " << (pos - in)
201  << std::endl;
202 #endif
203  const double *q = &deriv.front() +
204  (pos - in) * in;
205  for(const double *m = m0; p < end;
206  m += size, q -= in)
207  for(unsigned int k = 0; k < in; k++)
208  *p++ += *m * *q++;
209  } else {
210 #ifdef DEBUG_DERIV
211  std::cout << " in " << pos << std::endl;
212 #endif
213  for(const double *m = m0; p < end;
214  m += size, p += in)
215  p[pos] += *m;
216  }
217  }
218  }
219 
220 #ifdef DEBUG_DERIV
221  std::cout << "================" << std::endl;
222  for(const double *p = &deriv.front();
223  p != &deriv.front() + deriv.size();) {
224  for(unsigned int j = 0; j < in; j++)
225  std::cout << *p++ << "\t";
226  std::cout << std::endl;
227  }
228  std::cout << "================" << std::endl;
229 #endif
230 }
231 
232 } // namespace PhysicsTools
virtual char const * what() const
Definition: Exception.cc:97
virtual ConfigCtx::Context * configureLoop(ConfigCtx::Context *ctx, ConfigCtx::iterator begin, ConfigCtx::iterator cur, ConfigCtx::iterator end)
virtual loop configure method
Definition: VarProcessor.cc:86
int i
Definition: DBlmapReader.cc:9
list parent
Definition: dbtoconf.py:74
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision bits
static Base_t * create(const char *name, const CalibBase_t *calib, Parent_t *parent)
create an instance of name, given a calibration calib and parent parent
void configure(ConfigCtx &config)
called from the discriminator computer to configure processor
Definition: VarProcessor.cc:52
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
void deriv(double *input, int *conf, double *output, int *outConf, int *loop, unsigned int offset, unsigned int in, unsigned int out, std::vector< double > &deriv) const
run the processor evaluation pass on this processor and compute derivatives
ConfigCtx(std::vector< Variable::Flags > flags)
Definition: VarProcessor.cc:77
void eval(double *input, int *conf, double *output, int *outConf, int *loop, unsigned int offset) const
run the processor evaluation pass on this processor
Definition: VarProcessor.h:99
void convert(uint32 i, char_uint32 v)
Definition: MsgTools.h:46
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:40
Iterator to loop over the input/output variable configuration.
Definition: VarProcessor.h:128
BitSet inputVars
bit set to select the input variables to be passed to this processor
Definition: VarProcessor.h:301
tuple result
Definition: query.py:137
int j
Definition: DBlmapReader.cc:9
A compact container for storing single bits.
Definition: BitSet.h:30
#define end
Definition: vmac.h:38
unsigned int offset(bool)
tuple conf
Definition: dbtoconf.py:185
tuple input
Definition: collect_tpl.py:10
int k[5][pyjets_maxn]
tuple out
Definition: dbtoconf.py:99
#define EDM_REGISTER_PLUGINFACTORY(_factory_, _category_)
size_t size() const
returns the number of all bits in the container
Definition: BitSet.h:174
virtual LoopStatus loop(double *output, int *outConf, unsigned int nOutput, unsigned int &nOffset) const
Definition: VarProcessor.h:114
VarProcessor(const char *name, const Calibration::VarProcessor *calib, const MVAComputer *computer)
Definition: VarProcessor.cc:37
#define begin
Definition: vmac.h:31
Iterator iter() const
create iterator over all set bits
Definition: BitSet.h:180
tuple cout
Definition: gather_cfg.py:41
Iterates over all set bits of a BitSet.
Definition: BitSet.h:78
Generic registry template for polymorphic processor implementations.
Helper class for discriminator computer set-up procedure.
Definition: VarProcessor.h:53
SurfaceDeformation * create(int type, const std::vector< double > &params)
tuple size
Write out results.
T get(const Candidate &c)
Definition: component.h:56
Common base class for variable processors.
Definition: VarProcessor.h:39
const_iterator begin() const
Definition: VarProcessor.h:77