CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
PhysicsTools::MVAComputer Class Reference

Main interface class to the generic discriminator computer framework. More...

#include <MVAComputer.h>

Inheritance diagram for PhysicsTools::MVAComputer:
GenericMVAComputer

Classes

struct  DerivContext
 
struct  EvalContext
 
class  InputVar
 input variable configuration object More...
 
class  Processor
 variable processor container More...
 

Public Member Functions

template<typename Iterator_t >
double deriv (Iterator_t first, Iterator_t last) const
 
template<typename Container_t >
double deriv (Container_t &values) const
 
template<typename Iterator_t >
double eval (Iterator_t first, Iterator_t last) const
 evaluate variables given by a range of iterators given by first and last More...
 
template<typename Container_t >
double eval (const Container_t &values) const
 evaluate variables in iterable container values More...
 
 MVAComputer (const Calibration::MVAComputer *calib)
 construct a discriminator computer from a const calibation object More...
 
 MVAComputer (Calibration::MVAComputer *calib, bool owned=false)
 construct a discriminator computer from a calibation object More...
 
 MVAComputer (const char *filename)
 construct a discriminator computer from a calibration file More...
 
 MVAComputer (std::istream &is)
 construct a discriminator computer from C++ input stream More...
 
 ~MVAComputer ()
 

Static Public Member Functions

static Calibration::MVAComputerreadCalibration (const char *filename)
 read calibration object from plain file More...
 
static Calibration::MVAComputerreadCalibration (std::istream &is)
 read calibration object from plain C++ input stream More...
 
static void writeCalibration (const char *filename, const Calibration::MVAComputer *calib)
 write calibration object to file More...
 
static void writeCalibration (std::ostream &os, const Calibration::MVAComputer *calib)
 write calibration object to pain C++ output stream More...
 

Private Member Functions

template<class T >
void evalInternal (T &ctx) const
 evaluate discriminator from flattened variable array More...
 
unsigned int getVariableId (AtomicId name) const
 map variable identifier name to the numerical position in the array More...
 
void setup (const Calibration::MVAComputer *calib)
 construct processors from calibration and setup variables More...
 

Private Attributes

std::vector< InputVarinputVariables
 vector of input variables More...
 
unsigned int nVars
 total number of variables to expect while computing discriminator More...
 
unsigned int output
 index of the variable in the "conf" array to return as result More...
 
std::auto_ptr
< Calibration::MVAComputer
owned
 in case calibration object is owned by the MVAComputer More...
 
std::vector< ProcessorvarProcessors
 vector of variable processors More...
 

Detailed Description

Main interface class to the generic discriminator computer framework.

The MVAComputer class represents an instance of the modular discriminator computer. It is constructed from a "calibration" object which contains all the required histograms, matrices and other trainina data required for computing the discriminator. The calibration data also defines the names and properties of variables that can passed to that instance of the discriminator computer. The evaluation methods then calculates the discriminator from a applicable set of input variables, i.e. vector of key-value pairs.

Definition at line 40 of file MVAComputer.h.

Constructor & Destructor Documentation

PhysicsTools::MVAComputer::MVAComputer ( const Calibration::MVAComputer calib)

construct a discriminator computer from a const calibation object

Definition at line 37 of file MVAComputer.cc.

References setup().

37  :
38  nVars(0), output(0)
39 {
40  setup(calib);
41 }
unsigned int nVars
total number of variables to expect while computing discriminator
Definition: MVAComputer.h:195
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:198
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
void setup(const Calibration::MVAComputer *calib)
construct processors from calibration and setup variables
Definition: MVAComputer.cc:63
PhysicsTools::MVAComputer::MVAComputer ( Calibration::MVAComputer calib,
bool  owned = false 
)

construct a discriminator computer from a calibation object

Definition at line 43 of file MVAComputer.cc.

References setup().

43  :
44  nVars(0), output(0)
45 {
46  if (owned)
47  this->owned.reset(calib);
48  setup(calib);
49 }
std::auto_ptr< Calibration::MVAComputer > owned
in case calibration object is owned by the MVAComputer
Definition: MVAComputer.h:201
unsigned int nVars
total number of variables to expect while computing discriminator
Definition: MVAComputer.h:195
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:198
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
void setup(const Calibration::MVAComputer *calib)
construct processors from calibration and setup variables
Definition: MVAComputer.cc:63
PhysicsTools::MVAComputer::~MVAComputer ( )

Definition at line 141 of file MVAComputer.cc.

142 {
143 }
PhysicsTools::MVAComputer::MVAComputer ( const char *  filename)

construct a discriminator computer from a calibration file

Definition at line 51 of file MVAComputer.cc.

References owned, and setup().

51  :
53 {
54  setup(owned.get());
55 }
std::auto_ptr< Calibration::MVAComputer > owned
in case calibration object is owned by the MVAComputer
Definition: MVAComputer.h:201
static Calibration::MVAComputer * readCalibration(const char *filename)
read calibration object from plain file
Definition: MVAComputer.cc:230
unsigned int nVars
total number of variables to expect while computing discriminator
Definition: MVAComputer.h:195
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:198
tuple filename
Definition: lut2db_cfg.py:20
void setup(const Calibration::MVAComputer *calib)
construct processors from calibration and setup variables
Definition: MVAComputer.cc:63
PhysicsTools::MVAComputer::MVAComputer ( std::istream &  is)

construct a discriminator computer from C++ input stream

Definition at line 57 of file MVAComputer.cc.

References owned, and setup().

57  :
58  nVars(0), output(0), owned(readCalibration(is))
59 {
60  setup(owned.get());
61 }
std::auto_ptr< Calibration::MVAComputer > owned
in case calibration object is owned by the MVAComputer
Definition: MVAComputer.h:201
static Calibration::MVAComputer * readCalibration(const char *filename)
read calibration object from plain file
Definition: MVAComputer.cc:230
unsigned int nVars
total number of variables to expect while computing discriminator
Definition: MVAComputer.h:195
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:198
void setup(const Calibration::MVAComputer *calib)
construct processors from calibration and setup variables
Definition: MVAComputer.cc:63

Member Function Documentation

template<typename Iterator_t >
double PhysicsTools::MVAComputer::deriv ( Iterator_t  first,
Iterator_t  last 
) const
template<typename Container_t >
double PhysicsTools::MVAComputer::deriv ( Container_t &  values) const
inline

Definition at line 67 of file MVAComputer.h.

68  {
69  typedef typename Container_t::iterator Iterator_t;
70  return this->template deriv<Iterator_t>(
71  values.begin(), values.end());
72  }
template<typename Iterator_t >
double PhysicsTools::MVAComputer::eval ( Iterator_t  first,
Iterator_t  last 
) const
template<typename Container_t >
double PhysicsTools::MVAComputer::eval ( const Container_t &  values) const
inline

evaluate variables in iterable container values

Definition at line 59 of file MVAComputer.h.

60  {
61  typedef typename Container_t::const_iterator Iterator_t;
62  return this->template eval<Iterator_t>(
63  values.begin(), values.end());
64  }
template<class T >
template void PhysicsTools::MVAComputer::evalInternal ( T ctx) const
private

evaluate discriminator from flattened variable array

Definition at line 160 of file MVAComputer.cc.

References gather_cfg::cout, i, inputVariables, j, PhysicsTools::VarProcessor::kNext, PhysicsTools::VarProcessor::kReset, PhysicsTools::VarProcessor::kSkip, PhysicsTools::VarProcessor::kStop, python.cmstools::loop(), GetRecoTauVFromDQM_MC_cff::next, python.connectstrParser::o, evf::evtn::offset(), output, ntuplemaker::status, v, and varProcessors.

161 {
162  double *output = ctx.values() + ctx.n();
163  int *outConf = ctx.conf() + inputVariables.size();
164 
165 #ifdef DEBUG_EVAL
166  std::cout << "Input" << std::endl;
167  double *v = ctx.values();
168  for(int *o = ctx.conf(); o < outConf; o++) {
169  std::cout << "\tVar " << (o - ctx.conf()) << std::endl;
170  for(int i = o[0]; i < o[1]; i++)
171  std::cout << "\t\t" << *v++ << std::endl;
172  }
173 #endif
174  std::vector<Processor>::const_iterator iter = varProcessors.begin();
175  while(iter != varProcessors.end()) {
176  std::vector<Processor>::const_iterator loop = iter;
177  int *loopOutConf = outConf;
178  int *loopStart = 0;
179  double *loopOutput = output;
180 
182  unsigned int offset = 0;
183  while(status != VarProcessor::kStop) {
184  std::vector<Processor>::const_iterator next = iter + 1;
185  unsigned int nextOutput = (next != varProcessors.end())
186  ? next->nOutput : 0;
187 
188 #ifdef DEBUG_EVAL
189  std::cout << ROOT::Reflex::Tools::Demangle(
190  typeid(*iter->processor)) << std::endl;
191 #endif
192  if (status != VarProcessor::kSkip)
193  ctx.eval(&*iter->processor, outConf, output,
194  loopStart ? loopStart : loopOutConf,
195  offset, iter->nOutput);
196 
197 #ifdef DEBUG_EVAL
198  for(unsigned int i = 0; i < iter->nOutput;
199  i++, outConf++) {
200  std::cout << "\tVar " << (outConf - ctx.conf())
201  << std::endl;
202  for(int j = outConf[0]; j < outConf[1]; j++)
203  std::cout << "\t\t" << *output++
204  << std::endl;
205  }
206 #else
207  int orig = *outConf;
208  outConf += iter->nOutput;
209  output += *outConf - orig;
210 #endif
211 
212  status = loop->processor->loop(output, outConf,
213  nextOutput, offset);
214 
215  if (status == VarProcessor::kReset) {
216  outConf = loopOutConf;
217  output = loopOutput;
218  loopStart = 0;
219  offset = 0;
220  iter = loop;
221  } else {
222  if (loop == iter)
223  loopStart = outConf;
224  iter = next;
225  }
226  }
227  }
228 }
int i
Definition: DBlmapReader.cc:9
std::vector< InputVar > inputVariables
vector of input variables
Definition: MVAComputer.h:189
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:198
int j
Definition: DBlmapReader.cc:9
unsigned int offset(bool)
std::vector< Processor > varProcessors
vector of variable processors
Definition: MVAComputer.h:192
tuple cout
Definition: gather_cfg.py:121
tuple status
Definition: ntuplemaker.py:245
mathSSE::Vec4< T > v
unsigned int PhysicsTools::MVAComputer::getVariableId ( AtomicId  name) const
private

map variable identifier name to the numerical position in the array

Definition at line 145 of file MVAComputer.cc.

References inputVariables, mergeVDriftHistosByStation::name, and pos.

146 {
147  std::vector<InputVar>::const_iterator pos =
148  std::lower_bound(inputVariables.begin(), inputVariables.end(),
149  name);
150 
151  if (pos == inputVariables.end() || pos->var.getName() != name)
152  throw cms::Exception("InvalidVariable")
153  << "Input variable " << (const char*)name
154  << " not found." << std::endl;
155 
156  return pos->index;
157 }
std::vector< InputVar > inputVariables
vector of input variables
Definition: MVAComputer.h:189
Calibration::MVAComputer * PhysicsTools::MVAComputer::readCalibration ( const char *  filename)
static

read calibration object from plain file

Definition at line 230 of file MVAComputer.cc.

References mergeVDriftHistosByStation::file.

Referenced by PhysicsTools::MVAComputerESSourceBase::produce().

231 {
232  std::ifstream file(filename);
233  return readCalibration(file);
234 }
static Calibration::MVAComputer * readCalibration(const char *filename)
read calibration object from plain file
Definition: MVAComputer.cc:230
tuple filename
Definition: lut2db_cfg.py:20
Calibration::MVAComputer * PhysicsTools::MVAComputer::readCalibration ( std::istream &  is)
static

read calibration object from plain C++ input stream

Definition at line 236 of file MVAComputer.cc.

References calib, edm::hlt::Exception, errorMatrix2Lands::header, ext::basic_izstreambase< Item_t, Traits_t, Allocator_t >::rdbuf(), and STANDALONE_HEADER.

237 {
238  if (!is.good())
239  throw cms::Exception("InvalidFileState")
240  << "Stream passed to MVAComputer::readCalibration "
241  "has an invalid state." << std::endl;
242 
243  char header[sizeof STANDALONE_HEADER - 1] = { 0, };
244  if (is.readsome(header, sizeof header) != sizeof header ||
245  std::memcmp(header, STANDALONE_HEADER, sizeof header) != 0)
246  throw cms::Exception("InvalidFileFormat")
247  << "Stream passed to MVAComputer::readCalibration "
248  "is not a valid calibration file." << std::endl;
249 
250  TClass *rootClass =
251  TClass::GetClass("PhysicsTools::Calibration::MVAComputer");
252  if (!rootClass)
253  throw cms::Exception("DictionaryMissing")
254  << "CondFormats dictionary for "
255  "PhysicsTools::Calibration::MVAComputer missing"
256  << std::endl;
257 
258  ext::izstream izs(&is);
259  std::ostringstream ss;
260  ss << izs.rdbuf();
261  std::string buf = ss.str();
262 
263  TBufferFile buffer(TBuffer::kRead, buf.size(), const_cast<void*>(
264  static_cast<const void*>(buf.c_str())), kFALSE);
265  buffer.InitMap();
266 
267  std::auto_ptr<Calibration::MVAComputer> calib(
268  new Calibration::MVAComputer());
269  buffer.StreamObject(static_cast<void*>(calib.get()), rootClass);
270 
271  return calib.release();
272 }
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
#define STANDALONE_HEADER
Definition: MVAComputer.cc:33
void PhysicsTools::MVAComputer::setup ( const Calibration::MVAComputer calib)
private

construct processors from calibration and setup variables

Definition at line 63 of file MVAComputer.cc.

References PhysicsTools::VarProcessor::ConfigCtx::begin(), calib, HDQMDatabaseProducer::config, PhysicsTools::VarProcessor::configure(), filterCSVwithJSON::copy, PhysicsTools::ProcessRegistry< Base_t, CalibBase_t, Parent_t >::Factory::create(), PhysicsTools::VarProcessor::ConfigCtx::end(), edm::hlt::Exception, PhysicsTools::Variable::FLAG_ALL, PhysicsTools::Variable::FLAG_MULTIPLE, flags, PhysicsTools::Calibration::MVAComputer::getProcessors(), i, PhysicsTools::MVAComputer::InputVar::index, PhysicsTools::TrainMVAComputerCalibration::initFlags(), PhysicsTools::Calibration::MVAComputer::inputSet, inputVariables, j, PhysicsTools::VarProcessor::Config::mask, mergeVDriftHistosByStation::name, nVars, PhysicsTools::VarProcessor::Config::origin, PhysicsTools::Calibration::MVAComputer::output, output, pos, PhysicsTools::VarProcessor::ConfigCtx::size(), PhysicsTools::MVAComputer::InputVar::var, makeLayoutFileForGui::variables, and varProcessors.

Referenced by MVAComputer().

64 {
65  nVars = calib->inputSet.size();
66  output = calib->output;
67 
68  std::vector<Variable::Flags> flags(nVars, Variable::FLAG_ALL);
69  const TrainMVAComputerCalibration *trainCalib =
70  dynamic_cast<const TrainMVAComputerCalibration*>(calib);
71  if (trainCalib)
72  trainCalib->initFlags(flags);
73 
74  VarProcessor::ConfigCtx config(flags);
75  std::vector<Calibration::VarProcessor*> processors =
76  calib->getProcessors();
77 
78  for(std::vector<Calibration::VarProcessor*>::const_iterator iter =
79  processors.begin();
80  iter != processors.end(); ++iter) {
81  std::string name = (*iter)->getInstanceName();
82  VarProcessor *processor =
83  VarProcessor::create(name.c_str(), *iter, this);
84  if (!processor)
85  throw cms::Exception("UnknownProcessor")
86  << name << " could not be instantiated."
87  << std::endl;
88 
89  VarProcessor::ConfigCtx::iterator::difference_type pos =
90  config.end() - config.begin();
91  processor->configure(config);
92  unsigned int nOutput = (config.end() - config.begin()) - pos;
93  if (!nOutput)
94  throw cms::Exception("InvalidProcessor")
95  << name << " rejected input variable "
96  "configuration" << std::endl;
97 
98  varProcessors.push_back(Processor(processor, nOutput));
99  }
100 
101  for(VarProcessor::ConfigCtx::iterator iter = config.begin() + nVars;
102  iter != config.end(); iter++) {
103  VarProcessor::Config *origin = &config[iter->origin];
104  if (iter->origin >= nVars)
105  iter->origin = origin->origin;
106 
107  if (iter->mask & Variable::FLAG_MULTIPLE) {
108  iter->mask = (Variable::Flags)(iter->mask &
109  origin->mask);
110  config[iter->origin].origin++;
111  }
112  }
113 
114  nVars = config.size();
115 
116  if (output >= nVars)
117  // FIXME || config[output].mask != Variable::FLAG_NONE)
118  throw cms::Exception("InvalidOutput")
119  << "Output variable at index " << output
120  << " invalid." << std::endl;
121 
122  std::set<InputVar> variables;
123  unsigned int i = 0;
124  for(std::vector<Calibration::Variable>::const_iterator iter =
125  calib->inputSet.begin(); iter != calib->inputSet.end();
126  ++iter, i++) {
127  InputVar var;
128  var.var = Variable(iter->name, config[i].mask);
129  var.index = i;
130  variables.insert(var);
131  }
132 
133  inputVariables.resize(i);
134  std::copy(variables.begin(), variables.end(),
135  inputVariables.begin());
136 
137  for(unsigned int j = 0; j < i; j++)
138  inputVariables[j].multiplicity = config[j].origin;
139 }
int i
Definition: DBlmapReader.cc:9
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
std::vector< InputVar > inputVariables
vector of input variables
Definition: MVAComputer.h:189
unsigned int nVars
total number of variables to expect while computing discriminator
Definition: MVAComputer.h:195
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:198
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
int j
Definition: DBlmapReader.cc:9
static Base_t * create(const char *name, const CalibBase_t *calib, Parent_t *parent=0)
std::vector< Processor > varProcessors
vector of variable processors
Definition: MVAComputer.h:192
void PhysicsTools::MVAComputer::writeCalibration ( const char *  filename,
const Calibration::MVAComputer calib 
)
static

write calibration object to file

Definition at line 274 of file MVAComputer.cc.

References mergeVDriftHistosByStation::file.

Referenced by PhysicsTools::MVATrainerFileSave::endJob().

276 {
277  std::ofstream file(filename);
279 }
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
static void writeCalibration(const char *filename, const Calibration::MVAComputer *calib)
write calibration object to file
Definition: MVAComputer.cc:274
tuple filename
Definition: lut2db_cfg.py:20
void PhysicsTools::MVAComputer::writeCalibration ( std::ostream &  os,
const Calibration::MVAComputer calib 
)
static

write calibration object to pain C++ output stream

Definition at line 281 of file MVAComputer.cc.

References edm::hlt::Exception, and STANDALONE_HEADER.

283 {
284  if (!os.good())
285  throw cms::Exception("InvalidFileState")
286  << "Stream passed to MVAComputer::writeCalibration "
287  "has an invalid state." << std::endl;
288 
289  os << STANDALONE_HEADER;
290 
291  TClass *rootClass =
292  TClass::GetClass("PhysicsTools::Calibration::MVAComputer");
293  if (!rootClass)
294  throw cms::Exception("DictionaryMissing")
295  << "CondFormats dictionary for "
296  "PhysicsTools::Calibration::MVAComputer missing"
297  << std::endl;
298 
299  TBufferFile buffer(TBuffer::kWrite);
300  buffer.StreamObject(const_cast<void*>(static_cast<const void*>(calib)),
301  rootClass);
302 
303  ext::ozstream ozs(&os);
304  ozs.write(buffer.Buffer(), buffer.Length());
305  ozs.flush();
306 }
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
#define STANDALONE_HEADER
Definition: MVAComputer.cc:33

Member Data Documentation

std::vector<InputVar> PhysicsTools::MVAComputer::inputVariables
private

vector of input variables

Definition at line 189 of file MVAComputer.h.

Referenced by evalInternal(), getVariableId(), and setup().

unsigned int PhysicsTools::MVAComputer::nVars
private

total number of variables to expect while computing discriminator

Definition at line 195 of file MVAComputer.h.

Referenced by setup().

unsigned int PhysicsTools::MVAComputer::output
private

index of the variable in the "conf" array to return as result

Definition at line 198 of file MVAComputer.h.

Referenced by evalInternal(), PhysicsTools::MVAComputer::EvalContext::output(), PhysicsTools::MVAComputer::DerivContext::output(), and setup().

std::auto_ptr<Calibration::MVAComputer> PhysicsTools::MVAComputer::owned
private

in case calibration object is owned by the MVAComputer

Definition at line 201 of file MVAComputer.h.

Referenced by MVAComputer().

std::vector<Processor> PhysicsTools::MVAComputer::varProcessors
private

vector of variable processors

Definition at line 192 of file MVAComputer.h.

Referenced by evalInternal(), and setup().