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.13 2013/05/23 17:02:16 gartung Exp $
14 //
15 
17 
21 
25 #include "PhysicsTools/MVAComputer/interface/ProcessRegistry.icc"
26 
27 // #define DEBUG_DERIV
28 
29 #ifdef DEBUG_DERIV
31 #endif
32 
35  "PhysicsToolsMVAComputer");
36 
37 namespace PhysicsTools {
38 
41  const MVAComputer *computer) :
42  computer(computer),
43  inputVars(Calibration::convert(calib->inputVars)),
44  nInputVars(inputVars.bits())
45 {
46 }
47 
49 {
50  inputVars = BitSet(0);
51  nInputVars = 0;
52 }
53 
55 {
56  ConfigCtx::size_type pos = config.size();
57  if (pos != inputVars.size())
58  return;
59 
61  configure(iter, nInputVars);
62 
63  VarProcessor *loop = config.loop ? config.loop : this;
64  ConfigCtx::Context *ctx =
65  loop->configureLoop(config.ctx, config.begin(),
66  config.begin() + pos, config.end());
67 
68  if (ctx != config.ctx) {
69  delete config.ctx;
70  config.ctx = ctx;
71  }
72 
73  if (config.loop && !ctx)
74  config.loop = 0;
75  else if (!config.loop && ctx)
76  config.loop = this;
77 }
78 
79 VarProcessor::ConfigCtx::ConfigCtx(const std::vector<Variable::Flags>& flags) :
80  loop(0), ctx(0)
81 {
82  for(std::vector<Variable::Flags>::const_iterator iter = flags.begin();
83  iter != flags.end(); ++iter)
84  configs.push_back(Config(*iter, 1));
85 }
86 
90 {
91  return 0;
92 }
93 
94 template<>
97  const char *name, const Calibration::VarProcessor *calib,
98  const MVAComputer *parent)
99 {
100  VarProcessor *result = ProcessRegistry::create(name, calib, parent);
101  if (!result) {
102  // try to load the shared library and retry
103  try {
104  delete VPPluginFactory::get()->create(
105  std::string("VarProcessor/") + name);
106  result = ProcessRegistry::create(name, calib, parent);
107  } catch(const cms::Exception &e) {
108  // caller will have to deal with the null pointer
109  // in principle this will just give a slightly more
110  // descriptive error message (and will rethrow anyhow)
111 
112  edm::LogError("CannotBuildMVAProc")
113  << "Caught exception when building processor: "
114  << name << " message: " << std::endl
115  << e.what() << std::endl;
116  throw e;
117  }
118  }
119  return result;
120 }
121 
122 void VarProcessor::deriv(double *input, int *conf, double *output,
123  int *outConf, int *loop, unsigned int offset,
124  unsigned int in, unsigned int out_,
125  std::vector<double> &deriv) const
126 {
128  output, outConf, loop, offset);
129 
130  eval(iter, nInputVars);
131 
132  std::vector<double> matrix = this->deriv(iter, nInputVars);
133 
134  unsigned int size = 0;
135  while(iter)
136  size += (iter++).size();
137  bool empty = matrix.empty();
138  assert(size != 0 || empty);
139  unsigned int out = empty ? 0 : (matrix.size() / size);
140 
141  if (matrix.size() != out * size ||
142  (out > 1 && (int)out != outConf[out_] - outConf[0]))
143  throw cms::Exception("VarProcessor")
144  << "Derivative matrix implausible size in "
145  << typeid(*this).name() << "."
146  << std::endl;
147 
148 #ifdef DEBUG_DERIV
149  if (!matrix.empty()) {
151  edm::typeDemangle(typeid(*this).name(), demangledName);
152  std::cout << demangledName << std::endl;
153  for(unsigned int i = 0; i < out; i++) {
154  for(unsigned int j = 0; j < size; j++)
155  std::cout << matrix.at(i*size+j) << "\t";
156  std::cout << std::endl;
157  }
158  std::cout << "----------------" << std::endl;
159  }
160 
161  std::cout << "======= in = " << in << ", size = " << size
162  << ", out = " << out << ", matrix = " << matrix.size()
163  << std::endl;
164 #endif
165 
166  unsigned int sz = (outConf[out_] - in) * in;
167  unsigned int oldSz = deriv.size();
168  if (oldSz < sz)
169  deriv.resize(sz);
170 
171  double *begin = &deriv.front() + (outConf[0] - in + offset) * in;
172  double *end = begin + out * in;
173  if (begin < &deriv.front() + oldSz)
174  std::fill(begin, end, 0.0);
175 
176  if (matrix.empty())
177  return;
178 
179  double *m0 = &matrix.front();
181  for(unsigned int i = 0; i < nInputVars; i++, ++cur) {
182 #ifdef DEBUG_DERIV
183  std::cout << " inputvar " << i << std::endl;
184 #endif
185  int *curConf = conf + cur();
186  unsigned int pos = *curConf;
187 #ifdef DEBUG_DERIV
188  std::cout << " -> cur = " << cur() << ", pos = "
189  << pos << std::endl;
190 #endif
191  if (loop && curConf >= loop) {
192  pos += offset;
193  loop = 0;
194  }
195 
196  unsigned int n = loop ? (curConf[1] - curConf[0]) : 1;
197  for(unsigned int j = 0; j < n; m0++, j++, pos++) {
198 #ifdef DEBUG_DERIV
199  std::cout << " multip " << j << std::endl;
200 #endif
201  double *p = begin;
202  if (pos >= in) {
203 #ifdef DEBUG_DERIV
204  std::cout << " deriv " << (pos - in)
205  << std::endl;
206 #endif
207  const double *q = &deriv.front() +
208  (pos - in) * in;
209  for(const double *m = m0; p < end;
210  m += size, q -= in)
211  for(unsigned int k = 0; k < in; k++)
212  *p++ += *m * *q++;
213  } else {
214 #ifdef DEBUG_DERIV
215  std::cout << " in " << pos << std::endl;
216 #endif
217  for(const double *m = m0; p < end;
218  m += size, p += in)
219  p[pos] += *m;
220  }
221  }
222  }
223 
224 #ifdef DEBUG_DERIV
225  std::cout << "================" << std::endl;
226  for(const double *p = &deriv.front();
227  p != &deriv.front() + deriv.size();) {
228  for(unsigned int j = 0; j < in; j++)
229  std::cout << *p++ << "\t";
230  std::cout << std::endl;
231  }
232  std::cout << "================" << std::endl;
233 #endif
234 }
235 
236 } // namespace PhysicsTools
237 
238 // Force instantiation of its templated static methods.
virtual char const * what() const
Definition: Exception.cc:141
virtual ConfigCtx::Context * configureLoop(ConfigCtx::Context *ctx, ConfigCtx::iterator begin, ConfigCtx::iterator cur, ConfigCtx::iterator end)
virtual loop configure method
Definition: VarProcessor.cc:88
int i
Definition: DBlmapReader.cc:9
string fill
Definition: lumiContext.py:319
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
static void unregisterProcess(const char *name)
void configure(ConfigCtx &config)
called from the discriminator computer to configure processor
Definition: VarProcessor.cc:54
static void registerProcess(const char *name, const ProcessRegistry *process)
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
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:97
ConfigCtx(const std::vector< Variable::Flags > &flags)
Definition: VarProcessor.cc:79
void convert(uint32 i, char_uint32 v)
Definition: MsgTools.h:46
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:82
edmplugin::PluginFactory< PhysicsTools::VarProcessor::PluginFunctionPrototype > VPPluginFactory
Definition: VarProcessor.cc:33
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:127
BitSet inputVars
bit set to select the input variables to be passed to this processor
Definition: VarProcessor.h:300
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
int k[5][pyjets_maxn]
tuple out
Definition: dbtoconf.py:99
#define EDM_REGISTER_PLUGINFACTORY(_factory_, _category_)
Definition: PluginFactory.h:90
size_t size() const
returns the number of all bits in the container
Definition: BitSet.h:174
std::string typeDemangle(char const *mangledName)
virtual LoopStatus loop(double *output, int *outConf, unsigned int nOutput, unsigned int &nOffset) const
Definition: VarProcessor.h:112
VarProcessor(const char *name, const Calibration::VarProcessor *calib, const MVAComputer *computer)
Definition: VarProcessor.cc:39
#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:121
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:51
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:37
const_iterator begin() const
Definition: VarProcessor.h:75