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 142 of file MVAComputer.cc.

143 {
144 }
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:232
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:232
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 161 of file MVAComputer.cc.

References gather_cfg::cout, ora::ClassUtils::demangledName(), i, inputVariables, j, PhysicsTools::VarProcessor::kNext, PhysicsTools::VarProcessor::kReset, PhysicsTools::VarProcessor::kSkip, PhysicsTools::VarProcessor::kStop, python.cmstools::loop(), python.connectstrParser::o, evf::evtn::offset(), output, ntuplemaker::status, AlCaHLTBitMon_QueryRunRegistry::string, edm::typeDemangle(), findQualityFiles::v, and varProcessors.

162 {
163  double *output = ctx.values() + ctx.n();
164  int *outConf = ctx.conf() + inputVariables.size();
165 
166 #ifdef DEBUG_EVAL
167  std::cout << "Input" << std::endl;
168  double *v = ctx.values();
169  for(int *o = ctx.conf(); o < outConf; o++) {
170  std::cout << "\tVar " << (o - ctx.conf()) << std::endl;
171  for(int i = o[0]; i < o[1]; i++)
172  std::cout << "\t\t" << *v++ << std::endl;
173  }
174 #endif
175  std::vector<Processor>::const_iterator iter = varProcessors.begin();
176  while(iter != varProcessors.end()) {
177  std::vector<Processor>::const_iterator loop = iter;
178  int *loopOutConf = outConf;
179  int *loopStart = 0;
180  double *loopOutput = output;
181 
183  unsigned int offset = 0;
184  while(status != VarProcessor::kStop) {
185  std::vector<Processor>::const_iterator next = iter + 1;
186  unsigned int nextOutput = (next != varProcessors.end())
187  ? next->nOutput : 0;
188 
189 #ifdef DEBUG_EVAL
191  edm::typeDemangle(typeid(*iter->processor).name(), demangledName);
192  std::cout << demangledName << std::endl;
193 #endif
194  if (status != VarProcessor::kSkip)
195  ctx.eval(&*iter->processor, outConf, output,
196  loopStart ? loopStart : loopOutConf,
197  offset, iter->nOutput);
198 
199 #ifdef DEBUG_EVAL
200  for(unsigned int i = 0; i < iter->nOutput;
201  i++, outConf++) {
202  std::cout << "\tVar " << (outConf - ctx.conf())
203  << std::endl;
204  for(int j = outConf[0]; j < outConf[1]; j++)
205  std::cout << "\t\t" << *output++
206  << std::endl;
207  }
208 #else
209  int orig = *outConf;
210  outConf += iter->nOutput;
211  output += *outConf - orig;
212 #endif
213 
214  status = loop->processor->loop(output, outConf,
215  nextOutput, offset);
216 
217  if (status == VarProcessor::kReset) {
218  outConf = loopOutConf;
219  output = loopOutput;
220  loopStart = 0;
221  offset = 0;
222  iter = loop;
223  } else {
224  if (loop == iter)
225  loopStart = outConf;
226  iter = next;
227  }
228  }
229  }
230 }
int i
Definition: DBlmapReader.cc:9
std::vector< InputVar > inputVariables
vector of input variables
Definition: MVAComputer.h:189
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:82
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::string typeDemangle(char const *mangledName)
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
unsigned int PhysicsTools::MVAComputer::getVariableId ( AtomicId  name) const
private

map variable identifier name to the numerical position in the array

Definition at line 146 of file MVAComputer.cc.

References inputVariables, mergeVDriftHistosByStation::name, and pos.

147 {
148  std::vector<InputVar>::const_iterator pos =
149  std::lower_bound(inputVariables.begin(), inputVariables.end(),
150  name);
151 
152  if (pos == inputVariables.end() || pos->var.getName() != name)
153  throw cms::Exception("InvalidVariable")
154  << "Input variable " << (const char*)name
155  << " not found." << std::endl;
156 
157  return pos->index;
158 }
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 232 of file MVAComputer.cc.

References mergeVDriftHistosByStation::file.

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

233 {
234  std::ifstream file(filename);
235  return readCalibration(file);
236 }
static Calibration::MVAComputer * readCalibration(const char *filename)
read calibration object from plain file
Definition: MVAComputer.cc:232
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 238 of file MVAComputer.cc.

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

239 {
240  if (!is.good())
241  throw cms::Exception("InvalidFileState")
242  << "Stream passed to MVAComputer::readCalibration "
243  "has an invalid state." << std::endl;
244 
245  char header[sizeof STANDALONE_HEADER - 1] = { 0, };
246  if (is.readsome(header, sizeof header) != sizeof header ||
247  std::memcmp(header, STANDALONE_HEADER, sizeof header) != 0)
248  throw cms::Exception("InvalidFileFormat")
249  << "Stream passed to MVAComputer::readCalibration "
250  "is not a valid calibration file." << std::endl;
251 
252  TClass *rootClass =
253  TClass::GetClass("PhysicsTools::Calibration::MVAComputer");
254  if (!rootClass)
255  throw cms::Exception("DictionaryMissing")
256  << "CondFormats dictionary for "
257  "PhysicsTools::Calibration::MVAComputer missing"
258  << std::endl;
259 
260  ext::izstream izs(&is);
261  std::ostringstream ss;
262  ss << izs.rdbuf();
263  std::string buf = ss.str();
264 
265  TBufferFile buffer(TBuffer::kRead, buf.size(), const_cast<void*>(
266  static_cast<const void*>(buf.c_str())), kFALSE);
267  buffer.InitMap();
268 
269  std::auto_ptr<Calibration::MVAComputer> calib(
270  new Calibration::MVAComputer());
271  buffer.StreamObject(static_cast<void*>(calib.get()), rootClass);
272 
273  return calib.release();
274 }
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, PhysicsTools::MVAComputer::InputVar::multiplicity, mergeVDriftHistosByStation::name, nVars, PhysicsTools::VarProcessor::Config::origin, PhysicsTools::Calibration::MVAComputer::output, output, pos, PhysicsTools::VarProcessor::ConfigCtx::size(), AlCaHLTBitMon_QueryRunRegistry::string, 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  var.multiplicity = 0;
131  variables.insert(var);
132  }
133 
134  inputVariables.resize(i);
135  std::copy(variables.begin(), variables.end(),
136  inputVariables.begin());
137 
138  for(unsigned int j = 0; j < i; j++)
139  inputVariables[j].multiplicity = config[j].origin;
140 }
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 276 of file MVAComputer.cc.

References mergeVDriftHistosByStation::file.

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

278 {
279  std::ofstream file(filename);
281 }
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
static void writeCalibration(const char *filename, const Calibration::MVAComputer *calib)
write calibration object to file
Definition: MVAComputer.cc:276
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 283 of file MVAComputer.cc.

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

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