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