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< edm::GlobalCache< BaseMVACache > >

Public Member Functions

 BaseMVAValueMapProducer (const edm::ParameterSet &iConfig, const BaseMVACache *cache)
 
void setValue (const std::string var, float val)
 
 ~BaseMVAValueMapProducer () override
 
- Public Member Functions inherited from edm::stream::EDProducer< edm::GlobalCache< BaseMVACache > >
 EDProducer ()=default
 
 EDProducer (const EDProducer &)=delete
 
bool hasAbilityToProduceInBeginLumis () const final
 
bool hasAbilityToProduceInBeginProcessBlocks () const final
 
bool hasAbilityToProduceInBeginRuns () const final
 
bool hasAbilityToProduceInEndLumis () const final
 
bool hasAbilityToProduceInEndProcessBlocks () const final
 
bool hasAbilityToProduceInEndRuns () const final
 
const EDProduceroperator= (const EDProducer &)=delete
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 
static edm::ParameterSetDescription getDescription ()
 
static void globalEndJob (const BaseMVACache *cache)
 
static std::unique_ptr< BaseMVACacheinitializeGlobalCache (const edm::ParameterSet &cfg)
 

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_
 
std::string inputTensorName_
 
bool isClassifier_
 
std::string name_
 
bool onnx_
 
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_
 
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< edm::GlobalCache< BaseMVACache > >
using CacheTypes = CacheContexts< T... >
 
using GlobalCache = typename CacheTypes::GlobalCache
 
using HasAbility = AbilityChecker< T... >
 
using InputProcessBlockCache = typename CacheTypes::InputProcessBlockCache
 
using LuminosityBlockCache = typename CacheTypes::LuminosityBlockCache
 
using LuminosityBlockContext = LuminosityBlockContextT< LuminosityBlockCache, RunCache, GlobalCache >
 
using LuminosityBlockSummaryCache = typename CacheTypes::LuminosityBlockSummaryCache
 
using RunCache = typename CacheTypes::RunCache
 
using RunContext = RunContextT< RunCache, GlobalCache >
 
using RunSummaryCache = typename CacheTypes::RunSummaryCache
 

Detailed Description

template<typename T>
class BaseMVAValueMapProducer< T >

Description: [one line class summary]

Implementation: [Notes on implementation]

Definition at line 78 of file BaseMVAValueMapProducer.h.

Constructor & Destructor Documentation

◆ BaseMVAValueMapProducer()

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

Definition at line 80 of file BaseMVAValueMapProducer.h.

81  : src_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("src"))),
82  variablesOrder_(iConfig.getParameter<std::vector<std::string>>("variablesOrder")),
83  name_(iConfig.getParameter<std::string>("name")),
84  backend_(iConfig.getParameter<std::string>("backend")),
85  weightfilename_(iConfig.getParameter<edm::FileInPath>("weightFile").fullPath()),
86  isClassifier_(iConfig.getParameter<bool>("isClassifier")),
87  tmva_(backend_ == "TMVA"),
88  tf_(backend_ == "TF"),
89  onnx_(backend_ == "ONNX"),
90  batch_eval_(iConfig.getParameter<bool>("batch_eval")) {
91  if (!(tmva_ || tf_ || onnx_)) {
92  throw cms::Exception("ConfigError") << "Only 'TF', 'ONNX' and 'TMVA' backends are supported\n";
93  }
94 
95  if (tmva_)
96  reader_ = new TMVA::Reader();
97  edm::ParameterSet const& varsPSet = iConfig.getParameter<edm::ParameterSet>("variables");
98  for (const std::string& vname : varsPSet.getParameterNamesForType<std::string>()) {
99  funcs_.emplace_back(
100  std::pair<std::string, StringObjectFunction<T, true>>(vname, varsPSet.getParameter<std::string>(vname)));
101  }
102 
103  values_.resize(variablesOrder_.size());
104  size_t i = 0;
105  for (const auto& v : variablesOrder_) {
106  positions_[v] = i;
107  if (tmva_)
108  reader_->AddVariable(v, (&values_.front()) + i);
109  i++;
110  }
111  // reader_.BookMVA(name_,iConfig.getParameter<edm::FileInPath>("weightFile").fullPath() );
112  if (tmva_) {
114  }
115  if (tf_ || onnx_) {
116  inputTensorName_ = iConfig.getParameter<std::string>("inputTensorName");
117  outputTensorName_ = iConfig.getParameter<std::string>("outputTensorName");
118  output_names_ = iConfig.getParameter<std::vector<std::string>>("outputNames");
119  for (const auto& s : iConfig.getParameter<std::vector<std::string>>("outputFormulas")) {
120  output_formulas_.push_back(StringObjectFunction<std::vector<float>>(s));
121  }
122  }
123 
124  if (tmva_)
125  produces<edm::ValueMap<float>>();
126  else {
127  for (const auto& n : output_names_) {
128  produces<edm::ValueMap<float>>(n);
129  }
130  }
131  }
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
std::string fullPath() const
Definition: FileInPath.cc:161
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
std::vector< std::string > variablesOrder_
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:179
edm::EDGetTokenT< edm::View< T > > src_
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::vector< std::string > output_names_

◆ ~BaseMVAValueMapProducer()

template<typename T>
BaseMVAValueMapProducer< T >::~BaseMVAValueMapProducer ( )
inlineoverride

Definition at line 132 of file BaseMVAValueMapProducer.h.

132 {}

Member Function Documentation

◆ beginStream()

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

Definition at line 146 of file BaseMVAValueMapProducer.h.

146 {};

◆ endStream()

template<typename T>
void BaseMVAValueMapProducer< T >::endStream ( )
inlineoverrideprivate

Definition at line 148 of file BaseMVAValueMapProducer.h.

148 {};

◆ fillAdditionalVariables()

template<typename T>
virtual void BaseMVAValueMapProducer< T >::fillAdditionalVariables ( const T )
inlineprivatevirtual

Reimplemented in BJetEnergyRegressionMVA.

Definition at line 152 of file BaseMVAValueMapProducer.h.

152 {}

◆ fillDescriptions()

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

Definition at line 303 of file BaseMVAValueMapProducer.h.

303  {
306  if (typeid(T) == typeid(pat::Jet))
307  modname += "Jet";
308  else if (typeid(T) == typeid(pat::Muon))
309  modname += "Muon";
310  else if (typeid(T) == typeid(pat::Electron))
311  modname += "Ele";
312  modname += "BaseMVAValueMapProducer";
313  descriptions.add(modname, desc);
314 }
static edm::ParameterSetDescription getDescription()
Analysis-level electron class.
Definition: Electron.h:51
Analysis-level calorimeter jet class.
Definition: Jet.h:77
void add(std::string const &label, ParameterSetDescription const &psetDescription)
long double T
Analysis-level muon class.
Definition: Muon.h:51

◆ getDescription()

template<typename T >
edm::ParameterSetDescription BaseMVAValueMapProducer< T >::getDescription ( )
static

Definition at line 280 of file BaseMVAValueMapProducer.h.

Referenced by BJetEnergyRegressionMVA::fillDescriptions().

280  {
282  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
283  desc.add<std::vector<std::string>>("variablesOrder")->setComment("ordered list of MVA input variable names");
284  desc.add<std::string>("name")->setComment("output score variable name");
285  desc.add<bool>("isClassifier")->setComment("is a classifier discriminator");
287  variables.setAllowAnything();
288  desc.add<edm::ParameterSetDescription>("variables", variables)->setComment("list of input variable definitions");
289  desc.add<edm::FileInPath>("weightFile")->setComment("xml weight file");
290  desc.add<std::string>("backend", "TMVA")->setComment("TMVA, TF or ONNX");
291  desc.add<std::string>("inputTensorName", "")->setComment("Name of tensorflow input tensor in the model");
292  desc.add<std::string>("outputTensorName", "")->setComment("Name of tensorflow output tensor in the model");
293  desc.add<std::vector<std::string>>("outputNames", std::vector<std::string>())
294  ->setComment("Names of the output values to be used in the output valuemap");
295  desc.add<std::vector<std::string>>("outputFormulas", std::vector<std::string>())
296  ->setComment("Formulas to be used to post process the output");
297  desc.add<bool>("batch_eval", false)->setComment("Run inference in batch instead of per-object");
298 
299  return desc;
300 }

◆ globalEndJob()

template<typename T >
void BaseMVAValueMapProducer< T >::globalEndJob ( const BaseMVACache cache)
static

Definition at line 277 of file BaseMVAValueMapProducer.h.

277 {}

◆ initializeGlobalCache()

template<typename T >
std::unique_ptr< BaseMVACache > BaseMVAValueMapProducer< T >::initializeGlobalCache ( const edm::ParameterSet cfg)
static

Definition at line 271 of file BaseMVAValueMapProducer.h.

271  {
272  return std::make_unique<BaseMVACache>(cfg.getParameter<edm::FileInPath>("weightFile").fullPath(),
273  cfg.getParameter<std::string>("backend"));
274 }

◆ produce()

template<typename T >
void BaseMVAValueMapProducer< T >::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
)
overrideprivate

Definition at line 176 of file BaseMVAValueMapProducer.h.

176  {
178  iEvent.getByToken(src_, src);
180  std::vector<std::vector<float>> mvaOut((tmva_) ? 1 : output_names_.size());
181  for (auto& v : mvaOut)
182  v.reserve(src->size());
183 
184  if (batch_eval_) {
185  if (!src->empty()) {
186  std::vector<float> data;
187  data.reserve(src->size() * positions_.size());
188  for (auto const& o : *src) {
189  for (auto const& p : funcs_) {
190  setValue(p.first, p.second(o));
191  }
193  data.insert(data.end(), values_.begin(), values_.end());
194  }
195 
196  std::vector<float> outputs;
197  if (tf_) {
198  tensorflow::TensorShape input_size{(long long int)src->size(), (long long int)positions_.size()};
199  tensorflow::NamedTensorList input_tensors;
200  input_tensors.resize(1);
201  input_tensors[0] =
202  tensorflow::NamedTensor(inputTensorName_, tensorflow::Tensor(tensorflow::DT_FLOAT, input_size));
203  for (unsigned i = 0; i < data.size(); ++i) {
204  input_tensors[0].second.flat<float>()(i) = data[i];
205  }
206  std::vector<tensorflow::Tensor> output_tensors;
207  tensorflow::run(globalCache()->getTFSession(), input_tensors, {outputTensorName_}, &output_tensors);
208  for (unsigned i = 0; i < output_tensors.at(0).NumElements(); ++i) {
209  outputs.push_back(output_tensors.at(0).flat<float>()(i));
210  }
211  } else if (onnx_) {
213  outputs =
214  globalCache()->getONNXSession().run({inputTensorName_}, inputs, {}, {outputTensorName_}, src->size())[0];
215  }
216 
217  const unsigned outdim = outputs.size() / src->size();
218  for (unsigned i = 0; i < src->size(); ++i) {
219  std::vector<float> tmpOut(outputs.begin() + i * outdim, outputs.begin() + (i + 1) * outdim);
220  for (size_t k = 0; k < output_names_.size(); k++) {
221  mvaOut[k].push_back(output_formulas_[k](tmpOut));
222  }
223  }
224  }
225  } else {
226  for (auto const& o : *src) {
227  for (auto const& p : funcs_) {
228  setValue(p.first, p.second(o));
229  }
231  if (tmva_) {
232  mvaOut[0].push_back(isClassifier_ ? reader_->EvaluateMVA(name_) : reader_->EvaluateRegression(name_)[0]);
233  } else {
234  std::vector<float> tmpOut;
235  if (tf_) {
236  //currently support only one input sensor to reuse the TMVA like config
237  tensorflow::TensorShape input_size{1, (long long int)positions_.size()};
238  tensorflow::NamedTensorList input_tensors;
239  input_tensors.resize(1);
240  input_tensors[0] =
241  tensorflow::NamedTensor(inputTensorName_, tensorflow::Tensor(tensorflow::DT_FLOAT, input_size));
242  for (size_t j = 0; j < values_.size(); j++) {
243  input_tensors[0].second.matrix<float>()(0, j) = values_[j];
244  }
245  std::vector<tensorflow::Tensor> outputs;
246  tensorflow::run(globalCache()->getTFSession(), input_tensors, {outputTensorName_}, &outputs);
247  for (int k = 0; k < outputs.at(0).matrix<float>().dimension(1); k++)
248  tmpOut.push_back(outputs.at(0).matrix<float>()(0, k));
249  } else if (onnx_) {
251  tmpOut = globalCache()->getONNXSession().run({inputTensorName_}, inputs, {}, {outputTensorName_})[0];
252  }
253  for (size_t k = 0; k < output_names_.size(); k++)
254  mvaOut[k].push_back(output_formulas_[k](tmpOut));
255  }
256  }
257  }
258 
259  size_t k = 0;
260  for (auto& m : mvaOut) {
261  std::unique_ptr<edm::ValueMap<float>> mvaV(new edm::ValueMap<float>());
263  filler.insert(src, m.begin(), m.end());
264  filler.fill();
265  iEvent.put(std::move(mvaV), (tmva_) ? "" : output_names_[k]);
266  k++;
267  }
268 }
virtual void fillAdditionalVariables(const T &)
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:30
std::vector< StringObjectFunction< std::vector< float > > > output_formulas_
std::vector< std::vector< float > > FloatArrays
Definition: ONNXRuntime.h:23
void setValue(const std::string var, float val)
std::pair< std::string, Tensor > NamedTensor
Definition: TensorFlow.h:29
int iEvent
Definition: GenABIO.cc:224
std::vector< std::pair< std::string, StringObjectFunction< T, true > > > funcs_
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
Definition: TensorFlow.cc:213
edm::EDGetTokenT< edm::View< T > > src_
std::map< std::string, size_t > positions_
deadvectors [0] push_back({0.0175431, 0.538005, 6.80997, 13.29})
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
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

◆ readAdditionalCollections()

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 151 of file BaseMVAValueMapProducer.h.

151 {}

◆ setValue()

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

Definition at line 134 of file BaseMVAValueMapProducer.h.

134  {
135  if (positions_.find(var) != positions_.end())
137  }
std::map< std::string, size_t > positions_

Member Data Documentation

◆ backend_

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

Definition at line 162 of file BaseMVAValueMapProducer.h.

◆ batch_eval_

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

Definition at line 168 of file BaseMVAValueMapProducer.h.

◆ funcs_

template<typename T>
std::vector<std::pair<std::string, StringObjectFunction<T, true> > > BaseMVAValueMapProducer< T >::funcs_
private

◆ inputTensorName_

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

◆ isClassifier_

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

Definition at line 164 of file BaseMVAValueMapProducer.h.

◆ name_

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

◆ onnx_

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

◆ output_formulas_

template<typename T>
std::vector<StringObjectFunction<std::vector<float> > > BaseMVAValueMapProducer< T >::output_formulas_
private

◆ output_names_

template<typename T>
std::vector<std::string> BaseMVAValueMapProducer< T >::output_names_
private

◆ outputTensorName_

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

◆ positions_

template<typename T>
std::map<std::string, size_t> BaseMVAValueMapProducer< T >::positions_
private

◆ reader_

template<typename T>
TMVA::Reader* BaseMVAValueMapProducer< T >::reader_
private

◆ src_

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

Definition at line 154 of file BaseMVAValueMapProducer.h.

◆ tf_

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

◆ tmva_

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

◆ values_

template<typename T>
std::vector<float> BaseMVAValueMapProducer< T >::values_
private

◆ variablesOrder_

template<typename T>
std::vector<std::string> BaseMVAValueMapProducer< T >::variablesOrder_
private

◆ weightfilename_

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