CMS 3D CMS Logo

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 Container_t >
double deriv (Container_t &values) const
 
template<typename Iterator_t >
double deriv (Iterator_t first, Iterator_t last) const
 
template<typename Container_t >
double eval (const Container_t &values) const
 evaluate variables in iterable container values More...
 
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...
 
 MVAComputer (Calibration::MVAComputer *calib, bool owned=false)
 construct a discriminator computer from a calibation object More...
 
 MVAComputer (const Calibration::MVAComputer *calib)
 construct a discriminator computer from a const 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::unique_ptr< Calibration::MVAComputerowned
 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

◆ MVAComputer() [1/4]

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

construct a discriminator computer from a const calibation object

Definition at line 37 of file MVAComputer.cc.

37 : nVars(0), output(0) { setup(calib); }

References setup().

◆ MVAComputer() [2/4]

PhysicsTools::MVAComputer::MVAComputer ( Calibration::MVAComputer calib,
bool  owned = false 
)

construct a discriminator computer from a calibation object

Definition at line 39 of file MVAComputer.cc.

39  : nVars(0), output(0) {
40  if (owned)
41  this->owned.reset(calib);
42  setup(calib);
43  }

References owned, and setup().

◆ ~MVAComputer()

PhysicsTools::MVAComputer::~MVAComputer ( )

Definition at line 119 of file MVAComputer.cc.

119 {}

◆ MVAComputer() [3/4]

PhysicsTools::MVAComputer::MVAComputer ( const char *  filename)

construct a discriminator computer from a calibration file

Definition at line 45 of file MVAComputer.cc.

46  setup(owned.get());
47  }

References owned, and setup().

◆ MVAComputer() [4/4]

PhysicsTools::MVAComputer::MVAComputer ( std::istream &  is)

construct a discriminator computer from C++ input stream

Definition at line 49 of file MVAComputer.cc.

49 : nVars(0), output(0), owned(readCalibration(is)) { setup(owned.get()); }

References owned, and setup().

Member Function Documentation

◆ deriv() [1/2]

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

Definition at line 64 of file MVAComputer.h.

64  {
65  typedef typename Container_t::iterator Iterator_t;
66  return this->template deriv<Iterator_t>(values.begin(), values.end());
67  }

References contentValuesCheck::values.

◆ deriv() [2/2]

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

◆ eval() [1/2]

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.

58  {
59  typedef typename Container_t::const_iterator Iterator_t;
60  return this->template eval<Iterator_t>(values.begin(), values.end());
61  }

References contentValuesCheck::values.

◆ eval() [2/2]

template<typename Iterator_t >
double PhysicsTools::MVAComputer::eval ( Iterator_t  first,
Iterator_t  last 
) const

evaluate variables given by a range of iterators given by first and last

Referenced by evaluateTtFullHadSignalSel(), evaluateTtSemiLepSignalSel(), and TtSemiLepJetCombMVAComputer::produce().

◆ evalInternal()

template<class T >
template void PhysicsTools::MVAComputer::evalInternal ( T ctx) const
private

evaluate discriminator from flattened variable array

Definition at line 131 of file MVAComputer.cc.

131  {
132  double *output = ctx.values() + ctx.n();
133  int *outConf = ctx.conf() + inputVariables.size();
134 
135 #ifdef DEBUG_EVAL
136  std::cout << "Input" << std::endl;
137  double *v = ctx.values();
138  for (int *o = ctx.conf(); o < outConf; o++) {
139  std::cout << "\tVar " << (o - ctx.conf()) << std::endl;
140  for (int i = o[0]; i < o[1]; i++)
141  std::cout << "\t\t" << *v++ << std::endl;
142  }
143 #endif
144  std::vector<Processor>::const_iterator iter = varProcessors.begin();
145  while (iter != varProcessors.end()) {
146  std::vector<Processor>::const_iterator loop = iter;
147  int *loopOutConf = outConf;
148  int *loopStart = nullptr;
149  double *loopOutput = output;
150  VarProcessor::LoopCtx loopCtx;
151 
153  unsigned int offset = 0;
154  while (status != VarProcessor::kStop) {
155  std::vector<Processor>::const_iterator next = iter + 1;
156  unsigned int nextOutput = (next != varProcessors.end()) ? next->nOutput : 0;
157 
158 #ifdef DEBUG_EVAL
159  std::string demangledName;
160  edm::typeDemangle(typeid(*iter->processor).name(), demangledName);
161  std::cout << demangledName << std::endl;
162 #endif
164  ctx.eval(
165  &*iter->processor, outConf, output, loopStart ? loopStart : loopOutConf, loopCtx, offset, iter->nOutput);
166 
167 #ifdef DEBUG_EVAL
168  for (unsigned int i = 0; i < iter->nOutput; i++, outConf++) {
169  std::cout << "\tVar " << (outConf - ctx.conf()) << std::endl;
170  for (int j = outConf[0]; j < outConf[1]; j++)
171  std::cout << "\t\t" << *output++ << std::endl;
172  }
173 #else
174  int orig = *outConf;
175  outConf += iter->nOutput;
176  output += *outConf - orig;
177 #endif
178 
179  status = loop->processor->loop(output, outConf, nextOutput, loopCtx, offset);
180 
181  if (status == VarProcessor::kReset) {
182  outConf = loopOutConf;
183  output = loopOutput;
184  loopStart = nullptr;
185  offset = 0;
186  iter = loop;
187  } else {
188  if (loop == iter)
189  loopStart = outConf;
190  iter = next;
191  }
192  }
193  }
194  }

References gather_cfg::cout, mps_fire::i, inputVariables, dqmiolumiharvest::j, PhysicsTools::VarProcessor::kNext, PhysicsTools::VarProcessor::kReset, PhysicsTools::VarProcessor::kSkip, PhysicsTools::VarProcessor::kStop, heppy_loop::loop, GetRecoTauVFromDQM_MC_cff::next, EcalTangentSkim_cfg::o, hltrates_dqm_sourceclient-live_cfg::offset, output, mps_update::status, AlCaHLTBitMon_QueryRunRegistry::string, edm::typeDemangle(), findQualityFiles::v, and varProcessors.

◆ getVariableId()

int PhysicsTools::MVAComputer::getVariableId ( AtomicId  name) const
private

map variable identifier name to the numerical position in the array

Definition at line 121 of file MVAComputer.cc.

121  {
122  std::vector<InputVar>::const_iterator pos = std::lower_bound(inputVariables.begin(), inputVariables.end(), name);
123 
124  if (pos == inputVariables.end() || pos->var.getName() != name)
125  return -1;
126  else
127  return pos->index;
128  }

References inputVariables, pfDeepBoostedJetPreprocessParams_cfi::lower_bound, and Skims_PA_cff::name.

◆ readCalibration() [1/2]

Calibration::MVAComputer * PhysicsTools::MVAComputer::readCalibration ( const char *  filename)
static

read calibration object from plain file

Definition at line 196 of file MVAComputer.cc.

196  {
197  std::ifstream file(filename);
198  return readCalibration(file);
199  }

References FrontierConditions_GlobalTag_cff::file, and corrVsCorr::filename.

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

◆ readCalibration() [2/2]

Calibration::MVAComputer * PhysicsTools::MVAComputer::readCalibration ( std::istream &  is)
static

read calibration object from plain C++ input stream

Definition at line 201 of file MVAComputer.cc.

201  {
202  if (!is.good())
203  throw cms::Exception("InvalidFileState") << "Stream passed to MVAComputer::readCalibration "
204  "has an invalid state."
205  << std::endl;
206 
207  char header[sizeof STANDALONE_HEADER - 1] = {
208  0,
209  };
210  if (is.readsome(header, sizeof header) != sizeof header ||
211  std::memcmp(header, STANDALONE_HEADER, sizeof header) != 0)
212  throw cms::Exception("InvalidFileFormat") << "Stream passed to MVAComputer::readCalibration "
213  "is not a valid calibration file."
214  << std::endl;
215 
216  TClass *rootClass = TClass::GetClass("PhysicsTools::Calibration::MVAComputer");
217  if (!rootClass)
218  throw cms::Exception("DictionaryMissing") << "CondFormats dictionary for "
219  "PhysicsTools::Calibration::MVAComputer missing"
220  << std::endl;
221 
222  ext::izstream izs(&is);
223  std::ostringstream ss;
224  ss << izs.rdbuf();
225  std::string buf = ss.str();
226 
227  TBufferFile buffer(TBuffer::kRead, buf.size(), const_cast<void *>(static_cast<const void *>(buf.c_str())), kFALSE);
228  buffer.InitMap();
229 
230  std::unique_ptr<Calibration::MVAComputer> calib(new Calibration::MVAComputer());
231  buffer.StreamObject(static_cast<void *>(calib.get()), rootClass);
232 
233  return calib.release();
234  }

References visDQMUpload::buf, edmScanValgrind::buffer, L1EGammaCrystalsEmulatorProducer_cfi::calib, Exception, RecoTauValidation_cfi::header, ext::basic_izstreambase< Item_t, Traits_t, Allocator_t >::rdbuf(), contentValuesCheck::ss, STANDALONE_HEADER, and AlCaHLTBitMon_QueryRunRegistry::string.

◆ setup()

void PhysicsTools::MVAComputer::setup ( const Calibration::MVAComputer calib)
private

construct processors from calibration and setup variables

Definition at line 51 of file MVAComputer.cc.

51  {
52  nVars = calib->inputSet.size();
53  output = calib->output;
54 
55  std::vector<Variable::Flags> flags(nVars, Variable::FLAG_ALL);
56  const TrainMVAComputerCalibration *trainCalib = dynamic_cast<const TrainMVAComputerCalibration *>(calib);
57  if (trainCalib)
58  trainCalib->initFlags(flags);
59 
60  VarProcessor::ConfigCtx config(flags);
61  std::vector<Calibration::VarProcessor *> processors = calib->getProcessors();
62 
63  for (std::vector<Calibration::VarProcessor *>::const_iterator iter = processors.begin(); iter != processors.end();
64  ++iter) {
65  std::string name = (*iter)->getInstanceName();
66  VarProcessor *processor = VarProcessor::create(name.c_str(), *iter, this);
67  if (!processor)
68  throw cms::Exception("UnknownProcessor") << name << " could not be instantiated." << std::endl;
69 
70  VarProcessor::ConfigCtx::iterator::difference_type pos = config.end() - config.begin();
71  processor->configure(config);
72  unsigned int nOutput = (config.end() - config.begin()) - pos;
73  if (!nOutput)
74  throw cms::Exception("InvalidProcessor") << name
75  << " rejected input variable "
76  "configuration"
77  << std::endl;
78 
79  varProcessors.push_back(Processor(processor, nOutput));
80  }
81 
82  for (VarProcessor::ConfigCtx::iterator iter = config.begin() + nVars; iter != config.end(); iter++) {
83  VarProcessor::Config *origin = &config[iter->origin];
84  if (iter->origin >= nVars)
85  iter->origin = origin->origin;
86 
87  if (iter->mask & Variable::FLAG_MULTIPLE) {
88  iter->mask = (Variable::Flags)(iter->mask & origin->mask);
89  config[iter->origin].origin++;
90  }
91  }
92 
93  nVars = config.size();
94 
95  if (output >= nVars)
96  // FIXME || config[output].mask != Variable::FLAG_NONE)
97  throw cms::Exception("InvalidOutput") << "Output variable at index " << output << " invalid." << std::endl;
98 
99  std::set<InputVar> variables;
100  unsigned int i = 0;
101  for (std::vector<Calibration::Variable>::const_iterator iter = calib->inputSet.begin();
102  iter != calib->inputSet.end();
103  ++iter, i++) {
104  InputVar var;
105  var.var = Variable(iter->name, config[i].mask);
106  var.index = i;
107  var.multiplicity = 0;
108  variables.insert(var);
109  }
110 
111  inputVariables.resize(i);
112 
113  std::copy(variables.begin(), variables.end(), inputVariables.begin());
114 
115  for (unsigned int j = 0; j < i; j++)
116  inputVariables[j].multiplicity = config[j].origin;
117  }

References submitPVResolutionJobs::config, PhysicsTools::VarProcessor::configure(), filterCSVwithJSON::copy, PhysicsTools::ProcessRegistry< Base_t, CalibBase_t, Parent_t >::Factory::create(), Exception, PhysicsTools::Variable::FLAG_ALL, PhysicsTools::Variable::FLAG_MULTIPLE, HLT_FULL_cff::flags, mps_fire::i, PhysicsTools::TrainMVAComputerCalibration::initFlags(), inputVariables, dqmiolumiharvest::j, PhysicsTools::VarProcessor::Config::mask, Skims_PA_cff::name, nVars, PhysicsTools::VarProcessor::Config::origin, output, AlCaHLTBitMon_QueryRunRegistry::string, trigObjTnPSource_cfi::var, L1TEGammaDiff_cfi::variables, and varProcessors.

Referenced by MVAComputer().

◆ writeCalibration() [1/2]

void PhysicsTools::MVAComputer::writeCalibration ( const char *  filename,
const Calibration::MVAComputer calib 
)
static

write calibration object to file

Definition at line 236 of file MVAComputer.cc.

236  {
237  std::ofstream file(filename);
239  }

References FrontierConditions_GlobalTag_cff::file, and corrVsCorr::filename.

◆ writeCalibration() [2/2]

void PhysicsTools::MVAComputer::writeCalibration ( std::ostream &  os,
const Calibration::MVAComputer calib 
)
static

write calibration object to pain C++ output stream

Definition at line 241 of file MVAComputer.cc.

241  {
242  if (!os.good())
243  throw cms::Exception("InvalidFileState") << "Stream passed to MVAComputer::writeCalibration "
244  "has an invalid state."
245  << std::endl;
246 
247  os << STANDALONE_HEADER;
248 
249  TClass *rootClass = TClass::GetClass("PhysicsTools::Calibration::MVAComputer");
250  if (!rootClass)
251  throw cms::Exception("DictionaryMissing") << "CondFormats dictionary for "
252  "PhysicsTools::Calibration::MVAComputer missing"
253  << std::endl;
254 
255  TBufferFile buffer(TBuffer::kWrite);
256  buffer.StreamObject(const_cast<void *>(static_cast<const void *>(calib)), rootClass);
257 
258  ext::ozstream ozs(&os);
259  ozs.write(buffer.Buffer(), buffer.Length());
260  ozs.flush();
261  }

References edmScanValgrind::buffer, Exception, and STANDALONE_HEADER.

Member Data Documentation

◆ inputVariables

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().

◆ nVars

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().

◆ output

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 PhysicsTools::MVAComputer::EvalContext::eval(), evalInternal(), PhysicsTools::MVAComputer::DerivContext::output(), and setup().

◆ owned

std::unique_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().

◆ varProcessors

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

vector of variable processors

Definition at line 193 of file MVAComputer.h.

Referenced by evalInternal(), and setup().

PhysicsTools::Variable::Flags
Flags
Definition: Variable.h:35
mps_fire.i
i
Definition: mps_fire.py:428
PhysicsTools::VarProcessor::LoopStatus
LoopStatus
Definition: VarProcessor.h:132
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
mps_update.status
status
Definition: mps_update.py:69
L1TEGammaDiff_cfi.variables
variables
Definition: L1TEGammaDiff_cfi.py:5
edm::typeDemangle
std::string typeDemangle(char const *mangledName)
Definition: TypeDemangler.cc:93
gather_cfg.cout
cout
Definition: gather_cfg.py:144
pos
Definition: PixelAliasList.h:18
PhysicsTools::MVAComputer::readCalibration
static Calibration::MVAComputer * readCalibration(const char *filename)
read calibration object from plain file
Definition: MVAComputer.cc:196
PhysicsTools::MVAComputer::nVars
unsigned int nVars
total number of variables to expect while computing discriminator
Definition: MVAComputer.h:196
findQualityFiles.v
v
Definition: findQualityFiles.py:179
PhysicsTools::MVAComputer::setup
void setup(const Calibration::MVAComputer *calib)
construct processors from calibration and setup variables
Definition: MVAComputer.cc:51
PhysicsTools::MVAComputer::output
unsigned int output
index of the variable in the "conf" array to return as result
Definition: MVAComputer.h:199
EcalTangentSkim_cfg.o
o
Definition: EcalTangentSkim_cfg.py:42
PhysicsTools::VarProcessor::kReset
Definition: VarProcessor.h:132
edmScanValgrind.buffer
buffer
Definition: edmScanValgrind.py:171
trigObjTnPSource_cfi.var
var
Definition: trigObjTnPSource_cfi.py:21
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
config
Definition: config.py:1
PhysicsTools::Variable::FLAG_ALL
Definition: Variable.h:35
PhysicsTools::MVAComputer::writeCalibration
static void writeCalibration(const char *filename, const Calibration::MVAComputer *calib)
write calibration object to file
Definition: MVAComputer.cc:236
submitPVResolutionJobs.config
config
parse the configuration file
Definition: submitPVResolutionJobs.py:281
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
STANDALONE_HEADER
#define STANDALONE_HEADER
Definition: MVAComputer.cc:33
PhysicsTools::Variable::FLAG_MULTIPLE
Definition: Variable.h:35
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
calib
Definition: CalibElectron.h:12
pfDeepBoostedJetPreprocessParams_cfi.lower_bound
lower_bound
Definition: pfDeepBoostedJetPreprocessParams_cfi.py:15
PhysicsTools::ProcessRegistry::Factory::create
static Base_t * create(const char *name, const CalibBase_t *calib, Parent_t *parent=0)
PhysicsTools::VarProcessor::kSkip
Definition: VarProcessor.h:132
heppy_loop.loop
loop
Definition: heppy_loop.py:28
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
PhysicsTools::MVAComputer::owned
std::unique_ptr< Calibration::MVAComputer > owned
in case calibration object is owned by the MVAComputer
Definition: MVAComputer.h:202
visDQMUpload.buf
buf
Definition: visDQMUpload.py:154
ext::basic_izstream
Definition: zstream.h:135
PhysicsTools::VarProcessor::kStop
Definition: VarProcessor.h:132
L1EGammaCrystalsEmulatorProducer_cfi.calib
calib
Definition: L1EGammaCrystalsEmulatorProducer_cfi.py:6
ext::basic_ozstream
Definition: zstream.h:123
Exception
Definition: hltDiff.cc:245
PhysicsTools::VarProcessor::kNext
Definition: VarProcessor.h:132
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
PhysicsTools::MVAComputer::inputVariables
std::vector< InputVar > inputVariables
vector of input variables
Definition: MVAComputer.h:190
RecoTauValidation_cfi.header
header
Definition: RecoTauValidation_cfi.py:292
cms::Exception
Definition: Exception.h:70
PhysicsTools::VarProcessor::ConfigCtx::iterator
Config_t::iterator iterator
Definition: VarProcessor.h:61
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
PhysicsTools::MVAComputer::varProcessors
std::vector< Processor > varProcessors
vector of variable processors
Definition: MVAComputer.h:193
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
HLT_FULL_cff.flags
flags
Definition: HLT_FULL_cff.py:13150
helper.Config
Config
Definition: helper.py:10
GetRecoTauVFromDQM_MC_cff.next
next
Definition: GetRecoTauVFromDQM_MC_cff.py:31