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 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::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.

References setup().

37 : nVars(0), output(0) { setup(calib); }
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 "conf" 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:51

◆ 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.

References owned, and setup().

39  : nVars(0), output(0) {
40  if (owned)
41  this->owned.reset(calib);
42  setup(calib);
43  }
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 "conf" array to return as result
Definition: MVAComputer.h:199
std::unique_ptr< Calibration::MVAComputer > owned
in case calibration object is owned by the MVAComputer
Definition: MVAComputer.h:202
void setup(const Calibration::MVAComputer *calib)
construct processors from calibration and setup variables
Definition: MVAComputer.cc:51

◆ ~MVAComputer()

PhysicsTools::MVAComputer::~MVAComputer ( )

Definition at line 118 of file MVAComputer.cc.

118 {}

◆ 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.

References owned, and setup().

46  setup(owned.get());
47  }
static Calibration::MVAComputer * readCalibration(const char *filename)
read calibration object from plain file
Definition: MVAComputer.cc:195
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 "conf" array to return as result
Definition: MVAComputer.h:199
std::unique_ptr< Calibration::MVAComputer > owned
in case calibration object is owned by the MVAComputer
Definition: MVAComputer.h:202
void setup(const Calibration::MVAComputer *calib)
construct processors from calibration and setup variables
Definition: MVAComputer.cc:51

◆ 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.

References owned, and setup().

49 : nVars(0), output(0), owned(readCalibration(is)) { setup(owned.get()); }
static Calibration::MVAComputer * readCalibration(const char *filename)
read calibration object from plain file
Definition: MVAComputer.cc:195
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 "conf" array to return as result
Definition: MVAComputer.h:199
std::unique_ptr< Calibration::MVAComputer > owned
in case calibration object is owned by the MVAComputer
Definition: MVAComputer.h:202
void setup(const Calibration::MVAComputer *calib)
construct processors from calibration and setup variables
Definition: MVAComputer.cc:51

Member Function Documentation

◆ deriv() [1/2]

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

◆ deriv() [2/2]

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

Definition at line 64 of file MVAComputer.h.

References contentValuesCheck::values.

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

◆ eval() [1/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().

◆ eval() [2/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.

References contentValuesCheck::values.

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

◆ evalInternal()

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

evaluate discriminator from flattened variable array

Definition at line 130 of file MVAComputer.cc.

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.

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

◆ getVariableId()

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

map variable identifier name to the numerical position in the array

Definition at line 120 of file MVAComputer.cc.

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

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

◆ readCalibration() [1/2]

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

read calibration object from plain file

Definition at line 195 of file MVAComputer.cc.

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

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

195  {
196  std::ifstream file(filename);
197  return readCalibration(file);
198  }
static Calibration::MVAComputer * readCalibration(const char *filename)
read calibration object from plain file
Definition: MVAComputer.cc:195

◆ readCalibration() [2/2]

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

read calibration object from plain C++ input stream

Definition at line 200 of file MVAComputer.cc.

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

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

◆ 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.

References l1tEGammaCrystalsEmulatorProducer_cfi::calib, DiMuonV_cfg::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_2024v13_cff::flags, mps_fire::i, PhysicsTools::TrainMVAComputerCalibration::initFlags(), inputVariables, dqmiolumiharvest::j, PhysicsTools::VarProcessor::Config::mask, Skims_PA_cff::name, nVars, PhysicsTools::VarProcessor::Config::origin, AlCaHLTBitMon_QueryRunRegistry::string, trigObjTnPSource_cfi::var, hcalRecHitTable_cff::variables, and varProcessors.

Referenced by MVAComputer().

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<const Calibration::VarProcessor *> processors = calib->getProcessors();
62 
63  for (const auto *calibProc : processors) {
64  std::string name = calibProc->getInstanceName();
65  VarProcessor *processor = VarProcessor::create(name.c_str(), calibProc, this);
66  if (!processor)
67  throw cms::Exception("UnknownProcessor") << name << " could not be instantiated." << std::endl;
68 
69  VarProcessor::ConfigCtx::iterator::difference_type pos = config.end() - config.begin();
70  processor->configure(config);
71  unsigned int nOutput = (config.end() - config.begin()) - pos;
72  if (!nOutput)
73  throw cms::Exception("InvalidProcessor") << name
74  << " rejected input variable "
75  "configuration"
76  << std::endl;
77 
78  varProcessors.push_back(Processor(processor, nOutput));
79  }
80 
81  for (VarProcessor::ConfigCtx::iterator iter = config.begin() + nVars; iter != config.end(); iter++) {
82  VarProcessor::Config *origin = &config[iter->origin];
83  if (iter->origin >= nVars)
84  iter->origin = origin->origin;
85 
86  if (iter->mask & Variable::FLAG_MULTIPLE) {
87  iter->mask = (Variable::Flags)(iter->mask & origin->mask);
88  config[iter->origin].origin++;
89  }
90  }
91 
92  nVars = config.size();
93 
94  if (output >= nVars)
95  // FIXME || config[output].mask != Variable::FLAG_NONE)
96  throw cms::Exception("InvalidOutput") << "Output variable at index " << output << " invalid." << std::endl;
97 
98  std::set<InputVar> variables;
99  unsigned int i = 0;
100  for (std::vector<Calibration::Variable>::const_iterator iter = calib->inputSet.begin();
101  iter != calib->inputSet.end();
102  ++iter, i++) {
103  InputVar var;
104  var.var = Variable(iter->name, config[i].mask);
105  var.index = i;
106  var.multiplicity = 0;
107  variables.insert(var);
108  }
109 
110  inputVariables.resize(i);
111 
112  std::copy(variables.begin(), variables.end(), inputVariables.begin());
113 
114  for (unsigned int j = 0; j < i; j++)
115  inputVariables[j].multiplicity = config[j].origin;
116  }
std::vector< InputVar > inputVariables
vector of input variables
Definition: MVAComputer.h:190
Definition: config.py:1
unsigned int nVars
total number of variables to expect while computing discriminator
Definition: MVAComputer.h:196
Config
Definition: helper.py:10
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
dictionary config
Read in AllInOne config in JSON format.
Definition: DiMuonV_cfg.py:30
Definition: output.py:1

◆ writeCalibration() [1/2]

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

write calibration object to file

Definition at line 235 of file MVAComputer.cc.

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

235  {
236  std::ofstream file(filename);
238  }
static void writeCalibration(const char *filename, const Calibration::MVAComputer *calib)
write calibration object to file
Definition: MVAComputer.cc:235

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

References edmScanValgrind::buffer, Exception, l1tGTMenu_BTagSeeds_cff::os, and STANDALONE_HEADER.

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

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 collectionMerger.collectionMerger::analyze(), evalInternal(), and PhysicsTools::MVAComputer::DerivContext::output().

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