CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
BaseMVAValueMapProducer< T > Class Template Reference

#include <PhysicsTools/PatAlgos/plugins/BaseMVAValueMapProducer.cc>

Inheritance diagram for BaseMVAValueMapProducer< T >:
edm::stream::EDProducer<>

Public Member Functions

 BaseMVAValueMapProducer (const edm::ParameterSet &iConfig)
 
void setValue (const std::string var, float val)
 
 ~BaseMVAValueMapProducer () override
 
- Public Member Functions inherited from edm::stream::EDProducer<>
 EDProducer ()=default
 
bool hasAbilityToProduceInLumis () const final
 
bool hasAbilityToProduceInRuns () const final
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 
static edm::ParameterSetDescription getDescription ()
 

Private Member Functions

void beginStream (edm::StreamID) override
 
void endStream () override
 
virtual void fillAdditionalVariables (const T &)
 
void produce (edm::Event &, const edm::EventSetup &) override
 
virtual void readAdditionalCollections (edm::Event &, const edm::EventSetup &)
 to be implemented in derived classes, filling values for additional variables More...
 

Private Attributes

std::string backend_
 
bool batch_eval_
 
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
 
tensorflow::GraphDef * graph_
 
std::string inputTensorName_
 
bool isClassifier_
 
std::string name_
 
bool onnx_
 
std::unique_ptr< cms::Ort::ONNXRuntimeort_
 
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
 
std::vector< std::string > output_names_
 
std::string outputTensorName_
 
std::map< std::string, size_t > positions_
 
TMVA::Reader * reader_
 
tensorflow::Session * session_
 
std::string singleThreadPool_
 
edm::EDGetTokenT< edm::View< T > > src_
 
bool tf_
 
bool tmva_
 
std::vector< float > values_
 
std::vector< std::string > variablesOrder_
 
std::string weightfilename_
 

Additional Inherited Members

- Public Types inherited from edm::stream::EDProducer<>
typedef CacheContexts< T... > CacheTypes
 
typedef CacheTypes::GlobalCache GlobalCache
 
typedef AbilityChecker< T... > HasAbility
 
typedef CacheTypes::LuminosityBlockCache LuminosityBlockCache
 
typedef LuminosityBlockContextT< LuminosityBlockCache, RunCache, GlobalCacheLuminosityBlockContext
 
typedef CacheTypes::LuminosityBlockSummaryCache LuminosityBlockSummaryCache
 
typedef CacheTypes::RunCache RunCache
 
typedef RunContextT< RunCache, GlobalCacheRunContext
 
typedef CacheTypes::RunSummaryCache RunSummaryCache
 

Detailed Description

template<typename T>
class BaseMVAValueMapProducer< T >

Description: [one line class summary]

Implementation: [Notes on implementation]

Definition at line 57 of file BaseMVAValueMapProducer.h.

Constructor & Destructor Documentation

template<typename T>
BaseMVAValueMapProducer< T >::BaseMVAValueMapProducer ( const edm::ParameterSet iConfig)
inlineexplicit

Definition at line 59 of file BaseMVAValueMapProducer.h.

60  : src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
61  variablesOrder_(iConfig.getParameter<std::vector<std::string>>("variablesOrder")),
62  name_(iConfig.getParameter<std::string>("name")),
63  backend_(iConfig.getParameter<std::string>("backend")),
64  weightfilename_(iConfig.getParameter<edm::FileInPath>("weightFile").fullPath()),
65  isClassifier_(iConfig.getParameter<bool>("isClassifier")),
66  tmva_(backend_ == "TMVA"),
67  tf_(backend_ == "TF"),
68  onnx_(backend_ == "ONNX"),
69  batch_eval_(iConfig.getParameter<bool>("batch_eval")) {
70  if (tmva_)
71  reader_ = new TMVA::Reader();
72  edm::ParameterSet const& varsPSet = iConfig.getParameter<edm::ParameterSet>("variables");
73  for (const std::string& vname : varsPSet.getParameterNamesForType<std::string>()) {
74  funcs_.emplace_back(
75  std::pair<std::string, StringObjectFunction<T, true>>(vname, varsPSet.getParameter<std::string>(vname)));
76  }
77 
78  values_.resize(variablesOrder_.size());
79  size_t i = 0;
80  for (const auto& v : variablesOrder_) {
81  positions_[v] = i;
82  if (tmva_)
83  reader_->AddVariable(v, (&values_.front()) + i);
84  i++;
85  }
86  // reader_.BookMVA(name_,iConfig.getParameter<edm::FileInPath>("weightFile").fullPath() );
87  if (tmva_) {
89  } else if (tf_) {
92  size_t nThreads = iConfig.getParameter<unsigned int>("nThreads");
94  } else if (onnx_) {
95  ort_ = std::make_unique<cms::Ort::ONNXRuntime>(weightfilename_);
96  } else {
97  throw cms::Exception("ConfigError") << "Only 'TF', 'ONNX' and 'TMVA' backends are supported\n";
98  }
99  if (tf_ || onnx_) {
100  inputTensorName_ = iConfig.getParameter<std::string>("inputTensorName");
101  outputTensorName_ = iConfig.getParameter<std::string>("outputTensorName");
102  output_names_ = iConfig.getParameter<std::vector<std::string>>("outputNames");
103  for (const auto& s : iConfig.getParameter<std::vector<std::string>>("outputFormulas")) {
104  output_formulas_.push_back(StringObjectFunction<std::vector<float>>(s));
105  }
106  }
107  if (tmva_)
108  produces<edm::ValueMap<float>>();
109  else {
110  for (const auto& n : output_names_) {
111  produces<edm::ValueMap<float>>(n);
112  }
113  }
114  }
Session * createSession(SessionOptions &sessionOptions)
Definition: TensorFlow.cc:87
T getParameter(std::string const &) const
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
GraphDef * loadGraphDef(const std::string &pbFile)
Definition: TensorFlow.cc:68
tensorflow::GraphDef * graph_
std::unique_ptr< cms::Ort::ONNXRuntime > ort_
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:169
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
std::vector< std::string > variablesOrder_
edm::EDGetTokenT< edm::View< T > > src_
void setLogging(const std::string &level="3")
Definition: TensorFlow.cc:14
tensorflow::Session * session_
std::map< std::string, size_t > positions_
TMVA::IMethod * loadTMVAWeights(TMVA::Reader *reader, const std::string &method, const std::string &weightFile, bool verbose=false)
std::string fullPath() const
Definition: FileInPath.cc:163
std::vector< std::string > output_names_
template<typename T>
BaseMVAValueMapProducer< T >::~BaseMVAValueMapProducer ( )
inlineoverride

Definition at line 115 of file BaseMVAValueMapProducer.h.

115 {}

Member Function Documentation

template<typename T>
void BaseMVAValueMapProducer< T >::beginStream ( edm::StreamID  )
inlineoverrideprivate

Definition at line 126 of file BaseMVAValueMapProducer.h.

126 {};
template<typename T>
void BaseMVAValueMapProducer< T >::endStream ( )
inlineoverrideprivate

Definition at line 128 of file BaseMVAValueMapProducer.h.

128 {};
template<typename T>
virtual void BaseMVAValueMapProducer< T >::fillAdditionalVariables ( const T )
inlineprivatevirtual

Reimplemented in BJetEnergyRegressionMVA.

Definition at line 132 of file BaseMVAValueMapProducer.h.

Referenced by BaseMVAValueMapProducer< T >::produce().

132 {}
template<typename T >
void BaseMVAValueMapProducer< T >::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 283 of file BaseMVAValueMapProducer.h.

References edm::ConfigurationDescriptions::add(), BaseMVAValueMapProducer< T >::getDescription(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by BaseMVAValueMapProducer< pat::Jet >::setValue().

283  {
285  std::string modname;
286  if (typeid(T) == typeid(pat::Jet))
287  modname += "Jet";
288  else if (typeid(T) == typeid(pat::Muon))
289  modname += "Muon";
290  else if (typeid(T) == typeid(pat::Electron))
291  modname += "Ele";
292  modname += "BaseMVAValueMapProducer";
293  descriptions.add(modname, desc);
294 }
static edm::ParameterSetDescription getDescription()
Analysis-level electron class.
Definition: Electron.h:52
Analysis-level calorimeter jet class.
Definition: Jet.h:80
void add(std::string const &label, ParameterSetDescription const &psetDescription)
long double T
Analysis-level muon class.
Definition: Muon.h:51
template<typename T >
edm::ParameterSetDescription BaseMVAValueMapProducer< T >::getDescription ( )
static

Definition at line 257 of file BaseMVAValueMapProducer.h.

References edm::ParameterSetDescription::add(), edm::ParameterSetDescription::setAllowAnything(), edm::ParameterDescriptionNode::setComment(), AlCaHLTBitMon_QueryRunRegistry::string, and objects.autophobj::variables.

Referenced by BJetEnergyRegressionMVA::fillDescriptions(), BaseMVAValueMapProducer< T >::fillDescriptions(), and BaseMVAValueMapProducer< pat::Jet >::setValue().

257  {
259  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
260  desc.add<std::vector<std::string>>("variablesOrder")->setComment("ordered list of MVA input variable names");
261  desc.add<std::string>("name")->setComment("output score variable name");
262  desc.add<bool>("isClassifier")->setComment("is a classifier discriminator");
264  variables.setAllowAnything();
265  desc.add<edm::ParameterSetDescription>("variables", variables)->setComment("list of input variable definitions");
266  desc.add<edm::FileInPath>("weightFile")->setComment("xml weight file");
267  desc.add<std::string>("backend", "TMVA")->setComment("TMVA, TF or ONNX");
268  desc.add<std::string>("inputTensorName", "")->setComment("Name of tensorflow input tensor in the model");
269  desc.add<std::string>("outputTensorName", "")->setComment("Name of tensorflow output tensor in the model");
270  desc.add<std::vector<std::string>>("outputNames", std::vector<std::string>())
271  ->setComment("Names of the output values to be used in the output valuemap");
272  desc.add<std::vector<std::string>>("outputFormulas", std::vector<std::string>())
273  ->setComment("Formulas to be used to post process the output");
274  desc.add<unsigned int>("nThreads", 1)->setComment("number of threads");
275  desc.add<std::string>("singleThreadPool", "no_threads");
276  desc.add<bool>("batch_eval", false)->setComment("Run inference in batch instead of per-object");
277  desc.add<bool>("disableONNXGraphOpt", false)->setComment("Disable ONNX runtime graph optimization");
278 
279  return desc;
280 }
void setComment(std::string const &value)
void setAllowAnything()
allow any parameter label/value pairs
ParameterDescriptionBase * add(U const &iLabel, T const &value)
template<typename T >
void BaseMVAValueMapProducer< T >::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
)
overrideprivate

Definition at line 160 of file BaseMVAValueMapProducer.h.

References BaseMVAValueMapProducer< T >::batch_eval_, data, pat::helper::ParametrizationHelper::dimension(), edm::helper::Filler< Map >::fill(), BaseMVAValueMapProducer< T >::fillAdditionalVariables(), objects.autophobj::filler, BaseMVAValueMapProducer< T >::funcs_, edm::Event::getByToken(), mps_fire::i, haddnano::inputs, BaseMVAValueMapProducer< T >::inputTensorName_, edm::helper::Filler< Map >::insert(), createfilelist::int, BaseMVAValueMapProducer< T >::isClassifier_, gen::k, funct::m, eostools::move(), BaseMVAValueMapProducer< T >::name_, connectstrParser::o, BaseMVAValueMapProducer< T >::onnx_, BaseMVAValueMapProducer< T >::ort_, BaseMVAValueMapProducer< T >::output_formulas_, BaseMVAValueMapProducer< T >::output_names_, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, BaseMVAValueMapProducer< T >::outputTensorName_, AlCaHLTBitMon_ParallelJobs::p, BaseMVAValueMapProducer< T >::positions_, edm::Event::put(), BaseMVAValueMapProducer< T >::readAdditionalCollections(), BaseMVAValueMapProducer< T >::reader_, tensorflow::run(), BaseMVAValueMapProducer< T >::session_, BaseMVAValueMapProducer< T >::setValue(), TrackRefitter_38T_cff::src, BaseMVAValueMapProducer< T >::src_, BaseMVAValueMapProducer< T >::tf_, BaseMVAValueMapProducer< T >::tmva_, findQualityFiles::v, and BaseMVAValueMapProducer< T >::values_.

Referenced by BaseMVAValueMapProducer< pat::Jet >::beginStream().

160  {
162  iEvent.getByToken(src_, src);
163  readAdditionalCollections(iEvent, iSetup);
164  std::vector<std::vector<float>> mvaOut((tmva_) ? 1 : output_names_.size());
165  for (auto& v : mvaOut)
166  v.reserve(src->size());
167 
168  if (batch_eval_) {
169  if (!src->empty()) {
170  std::vector<float> data;
171  data.reserve(src->size() * positions_.size());
172  for (auto const& o : *src) {
173  for (auto const& p : funcs_) {
174  setValue(p.first, p.second(o));
175  }
177  data.insert(data.end(), values_.begin(), values_.end());
178  }
179 
180  std::vector<float> outputs;
181  if (tf_) {
182  //currently support only one input sensor to reuse the TMVA like config
183  tensorflow::TensorShape input_size{(long long int)src->size(), (long long int)positions_.size()};
184  tensorflow::NamedTensorList input_tensors;
185  input_tensors.resize(1);
186  input_tensors[0] =
187  tensorflow::NamedTensor(inputTensorName_, tensorflow::Tensor(tensorflow::DT_FLOAT, input_size));
188 
189  for (unsigned i = 0; i < data.size(); ++i) {
190  input_tensors[0].second.flat<float>()(i) = data[i];
191  }
192  std::vector<tensorflow::Tensor> output_tensors;
193  tensorflow::run(session_, input_tensors, {outputTensorName_}, &output_tensors);
194  for (unsigned i = 0; i < output_tensors.at(0).NumElements(); ++i) {
195  outputs.push_back(output_tensors.at(0).flat<float>()(i));
196  }
197  } else if (onnx_) {
199  outputs = ort_->run({inputTensorName_}, inputs, {}, {outputTensorName_}, src->size())[0];
200  }
201 
202  const unsigned outdim = outputs.size() / src->size();
203  for (unsigned i = 0; i < src->size(); ++i) {
204  std::vector<float> tmpOut(outputs.begin() + i * outdim, outputs.begin() + (i + 1) * outdim);
205  for (size_t k = 0; k < output_names_.size(); k++) {
206  mvaOut[k].push_back(output_formulas_[k](tmpOut));
207  }
208  }
209  }
210  } else {
211  for (auto const& o : *src) {
212  for (auto const& p : funcs_) {
213  setValue(p.first, p.second(o));
214  }
216  if (tmva_) {
217  mvaOut[0].push_back(isClassifier_ ? reader_->EvaluateMVA(name_) : reader_->EvaluateRegression(name_)[0]);
218  } else {
219  std::vector<float> tmpOut;
220  if (tf_) {
221  //currently support only one input sensor to reuse the TMVA like config
222  tensorflow::TensorShape input_size{1, (long long int)positions_.size()};
223  tensorflow::NamedTensorList input_tensors;
224  input_tensors.resize(1);
225  input_tensors[0] =
226  tensorflow::NamedTensor(inputTensorName_, tensorflow::Tensor(tensorflow::DT_FLOAT, input_size));
227  for (size_t j = 0; j < values_.size(); j++) {
228  input_tensors[0].second.matrix<float>()(0, j) = values_[j];
229  }
230  std::vector<tensorflow::Tensor> outputs;
231  tensorflow::run(session_, input_tensors, {outputTensorName_}, &outputs);
232  for (int k = 0; k < outputs.at(0).matrix<float>().dimension(1); k++)
233  tmpOut.push_back(outputs.at(0).matrix<float>()(0, k));
234  } else if (onnx_) {
236  tmpOut = ort_->run({inputTensorName_}, inputs, {}, {outputTensorName_})[0];
237  }
238 
239  for (size_t k = 0; k < output_names_.size(); k++)
240  mvaOut[k].push_back(output_formulas_[k](tmpOut));
241  }
242  }
243  }
244 
245  size_t k = 0;
246  for (auto& m : mvaOut) {
247  std::unique_ptr<edm::ValueMap<float>> mvaV(new edm::ValueMap<float>());
249  filler.insert(src, m.begin(), m.end());
250  filler.fill();
251  iEvent.put(std::move(mvaV), (tmva_) ? "" : output_names_[k]);
252  k++;
253  }
254 }
virtual void fillAdditionalVariables(const T &)
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:26
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::vector< std::vector< float > > FloatArrays
Definition: ONNXRuntime.h:23
void setValue(const std::string var, float val)
std::unique_ptr< cms::Ort::ONNXRuntime > ort_
std::pair< std::string, Tensor > NamedTensor
Definition: TensorFlow.h:25
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
int k[5][pyjets_maxn]
edm::EDGetTokenT< edm::View< T > > src_
tensorflow::Session * session_
std::map< std::string, size_t > positions_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, const std::vector< std::string > &targetNodes, std::vector< Tensor > *outputs)
Definition: TensorFlow.cc:210
uint32_t dimension(pat::CandKinResolution::Parametrization parametrization)
Returns the number of free parameters in a parametrization (3 or 4)
def move(src, dest)
Definition: eostools.py:511
std::vector< std::string > output_names_
virtual void readAdditionalCollections(edm::Event &, const edm::EventSetup &)
to be implemented in derived classes, filling values for additional variables
template<typename T>
virtual void BaseMVAValueMapProducer< T >::readAdditionalCollections ( edm::Event ,
const edm::EventSetup  
)
inlineprivatevirtual

to be implemented in derived classes, filling values for additional variables

Reimplemented in BJetEnergyRegressionMVA.

Definition at line 131 of file BaseMVAValueMapProducer.h.

Referenced by BaseMVAValueMapProducer< T >::produce().

131 {}
template<typename T>
void BaseMVAValueMapProducer< T >::setValue ( const std::string  var,
float  val 
)
inline

Definition at line 117 of file BaseMVAValueMapProducer.h.

Referenced by BaseMVAValueMapProducer< T >::produce().

117  {
118  if (positions_.find(var) != positions_.end())
120  }
std::map< std::string, size_t > positions_

Member Data Documentation

template<typename T>
std::string BaseMVAValueMapProducer< T >::backend_
private

Definition at line 146 of file BaseMVAValueMapProducer.h.

template<typename T>
bool BaseMVAValueMapProducer< T >::batch_eval_
private

Definition at line 152 of file BaseMVAValueMapProducer.h.

Referenced by BaseMVAValueMapProducer< T >::produce().

template<typename T>
std::vector<std::pair<std::string, StringObjectFunction<T, true> > > BaseMVAValueMapProducer< T >::funcs_
private
template<typename T>
tensorflow::GraphDef* BaseMVAValueMapProducer< T >::graph_
private
template<typename T>
std::string BaseMVAValueMapProducer< T >::inputTensorName_
private
template<typename T>
bool BaseMVAValueMapProducer< T >::isClassifier_
private

Definition at line 148 of file BaseMVAValueMapProducer.h.

Referenced by BaseMVAValueMapProducer< T >::produce().

template<typename T>
std::string BaseMVAValueMapProducer< T >::name_
private
template<typename T>
bool BaseMVAValueMapProducer< T >::onnx_
private
template<typename T>
std::unique_ptr<cms::Ort::ONNXRuntime> BaseMVAValueMapProducer< T >::ort_
private
template<typename T>
std::vector<StringObjectFunction<std::vector<float> > > BaseMVAValueMapProducer< T >::output_formulas_
private
template<typename T>
std::vector<std::string> BaseMVAValueMapProducer< T >::output_names_
private
template<typename T>
std::string BaseMVAValueMapProducer< T >::outputTensorName_
private
template<typename T>
std::map<std::string, size_t> BaseMVAValueMapProducer< T >::positions_
private
template<typename T>
TMVA::Reader* BaseMVAValueMapProducer< T >::reader_
private
template<typename T>
tensorflow::Session* BaseMVAValueMapProducer< T >::session_
private
template<typename T>
std::string BaseMVAValueMapProducer< T >::singleThreadPool_
private

Definition at line 142 of file BaseMVAValueMapProducer.h.

template<typename T>
edm::EDGetTokenT<edm::View<T> > BaseMVAValueMapProducer< T >::src_
private

Definition at line 134 of file BaseMVAValueMapProducer.h.

Referenced by BaseMVAValueMapProducer< T >::produce().

template<typename T>
bool BaseMVAValueMapProducer< T >::tf_
private
template<typename T>
bool BaseMVAValueMapProducer< T >::tmva_
private
template<typename T>
std::vector<float> BaseMVAValueMapProducer< T >::values_
private
template<typename T>
std::vector<std::string> BaseMVAValueMapProducer< T >::variablesOrder_
private
template<typename T>
std::string BaseMVAValueMapProducer< T >::weightfilename_
private