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...
 
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 39 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:196
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:199
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:202
unsigned int nVars
total number of variables to expect while computing discriminator
Definition: MVAComputer.h:196
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:199
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:202
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:196
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:199
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:202
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:196
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:199
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 66 of file MVAComputer.h.

67  {
68  typedef typename Container_t::iterator Iterator_t;
69  return this->template deriv<Iterator_t>(
70  values.begin(), values.end());
71  }
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 58 of file MVAComputer.h.

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

evaluate discriminator from flattened variable array

Definition at line 158 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, cmsHarvester::loop, GetRecoTauVFromDQM_MC_cff::next, python.connectstrParser::o, hltrates_dqm_sourceclient-live_cfg::offset, output, ntuplemaker::status, AlCaHLTBitMon_QueryRunRegistry::string, edm::typeDemangle(), findQualityFiles::v, and varProcessors.

159 {
160 
161  double *output = ctx.values() + ctx.n();
162  int *outConf = ctx.conf() + inputVariables.size();
163 
164 #ifdef DEBUG_EVAL
165  std::cout << "Input" << std::endl;
166  double *v = ctx.values();
167  for(int *o = ctx.conf(); o < outConf; o++) {
168  std::cout << "\tVar " << (o - ctx.conf()) << std::endl;
169  for(int i = o[0]; i < o[1]; i++)
170  std::cout << "\t\t" << *v++ << std::endl;
171  }
172 #endif
173  std::vector<Processor>::const_iterator iter = varProcessors.begin();
174  while(iter != varProcessors.end()) {
175  std::vector<Processor>::const_iterator loop = iter;
176  int *loopOutConf = outConf;
177  int *loopStart = 0;
178  double *loopOutput = output;
179  VarProcessor::LoopCtx loopCtx;
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
190  edm::typeDemangle(typeid(*iter->processor).name(), demangledName);
191  std::cout << demangledName << std::endl;
192 #endif
193  if (status != VarProcessor::kSkip)
194  ctx.eval(&*iter->processor, outConf, output,
195  loopStart ? loopStart : loopOutConf,
196  loopCtx,
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, loopCtx, 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
int loop
CMSSW
std::vector< InputVar > inputVariables
vector of input variables
Definition: MVAComputer.h:190
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:156
unsigned int output
index of the variable in the &quot;conf&quot; array to return as result
Definition: MVAComputer.h:199
int j
Definition: DBlmapReader.cc:9
std::string typeDemangle(char const *mangledName)
std::vector< Processor > varProcessors
vector of variable processors
Definition: MVAComputer.h:193
tuple cout
Definition: gather_cfg.py:121
tuple status
Definition: ntuplemaker.py:245
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, and mergeVDriftHistosByStation::name.

147 {
148 
149  std::vector<InputVar>::const_iterator pos = std::lower_bound(inputVariables.begin(), inputVariables.end(), name);
150 
151  if (pos == inputVariables.end() || pos->var.getName() != name)
152  return -1;
153  else
154  return pos->index;
155 }
std::vector< InputVar > inputVariables
vector of input variables
Definition: MVAComputer.h:190
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(), contentValuesCheck::ss, 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, output, PhysicsTools::Calibration::MVAComputer::output, PhysicsTools::VarProcessor::ConfigCtx::size(), AlCaHLTBitMon_QueryRunRegistry::string, MetTreeProducer::var(), PhysicsTools::MVAComputer::InputVar::var, makeLayoutFileForGui::variables, and varProcessors.

Referenced by MVAComputer().

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

Referenced by MVAComputer().

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

vector of variable processors

Definition at line 193 of file MVAComputer.h.

Referenced by evalInternal(), and setup().