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

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

139 {}

Member Function Documentation

◆ beginStream()

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

Definition at line 153 of file BaseMVAValueMapProducer.h.

153 {};

◆ endStream()

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

Definition at line 155 of file BaseMVAValueMapProducer.h.

155 {};

◆ fillAdditionalVariables()

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

Reimplemented in BJetEnergyRegressionMVA, and EvaluateMuonMVAID.

Definition at line 159 of file BaseMVAValueMapProducer.h.

159 {}

◆ fillDescriptions()

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

Definition at line 312 of file BaseMVAValueMapProducer.h.

312  {
315  if (typeid(T) == typeid(pat::Jet))
316  modname += "Jet";
317  else if (typeid(T) == typeid(pat::Muon))
318  modname += "Muon";
319  else if (typeid(T) == typeid(pat::Electron))
320  modname += "Ele";
321  modname += "BaseMVAValueMapProducer";
322  descriptions.add(modname, desc);
323 }
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 288 of file BaseMVAValueMapProducer.h.

Referenced by EvaluateMuonMVAID::fillDescriptions(), and BJetEnergyRegressionMVA::fillDescriptions().

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

◆ globalEndJob()

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

Definition at line 285 of file BaseMVAValueMapProducer.h.

285 {}

◆ initializeGlobalCache()

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

Definition at line 278 of file BaseMVAValueMapProducer.h.

278  {
279  return std::make_unique<BaseMVACache>(cfg.getParameter<edm::FileInPath>("weightFile").fullPath(),
280  cfg.getParameter<std::string>("backend"),
281  cfg.getParameter<bool>("disableONNXGraphOpt"));
282 }

◆ produce()

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

Definition at line 183 of file BaseMVAValueMapProducer.h.

183  {
185  iEvent.getByToken(src_, src);
187  std::vector<std::vector<float>> mvaOut((tmva_) ? 1 : output_names_.size());
188  for (auto& v : mvaOut)
189  v.reserve(src->size());
190 
191  if (batch_eval_) {
192  if (!src->empty()) {
193  std::vector<float> data;
194  data.reserve(src->size() * positions_.size());
195  for (auto const& o : *src) {
196  for (auto const& p : funcs_) {
197  setValue(p.first, p.second(o));
198  }
200  data.insert(data.end(), values_.begin(), values_.end());
201  }
202 
203  std::vector<float> outputs;
204  if (tf_) {
205  tensorflow::TensorShape input_size{(long long int)src->size(), (long long int)positions_.size()};
206  tensorflow::NamedTensorList input_tensors;
207  input_tensors.resize(1);
208  input_tensors[0] =
209  tensorflow::NamedTensor(inputTensorName_, tensorflow::Tensor(tensorflow::DT_FLOAT, input_size));
210  for (unsigned i = 0; i < data.size(); ++i) {
211  input_tensors[0].second.flat<float>()(i) = data[i];
212  }
213  std::vector<tensorflow::Tensor> output_tensors;
214  tensorflow::run(globalCache()->getTFSession(), input_tensors, {outputTensorName_}, &output_tensors);
215  for (unsigned i = 0; i < output_tensors.at(0).NumElements(); ++i) {
216  outputs.push_back(output_tensors.at(0).flat<float>()(i));
217  }
218  } else if (onnx_) {
220  outputs =
221  globalCache()->getONNXSession().run({inputTensorName_}, inputs, {}, {outputTensorName_}, src->size())[0];
222  }
223 
224  const unsigned outdim = outputs.size() / src->size();
225  for (unsigned i = 0; i < src->size(); ++i) {
226  std::vector<float> tmpOut(outputs.begin() + i * outdim, outputs.begin() + (i + 1) * outdim);
227  for (size_t k = 0; k < output_names_.size(); k++) {
228  mvaOut[k].push_back(output_formulas_[k](tmpOut));
229  }
230  }
231  }
232  } else {
233  for (auto const& o : *src) {
234  for (auto const& p : funcs_) {
235  setValue(p.first, p.second(o));
236  }
238  if (tmva_) {
239  mvaOut[0].push_back(isClassifier_ ? reader_->EvaluateMVA(name_) : reader_->EvaluateRegression(name_)[0]);
240  } else {
241  std::vector<float> tmpOut;
242  if (tf_) {
243  //currently support only one input sensor to reuse the TMVA like config
244  tensorflow::TensorShape input_size{1, (long long int)positions_.size()};
245  tensorflow::NamedTensorList input_tensors;
246  input_tensors.resize(1);
247  input_tensors[0] =
248  tensorflow::NamedTensor(inputTensorName_, tensorflow::Tensor(tensorflow::DT_FLOAT, input_size));
249  for (size_t j = 0; j < values_.size(); j++) {
250  input_tensors[0].second.matrix<float>()(0, j) = values_[j];
251  }
252  std::vector<tensorflow::Tensor> outputs;
253  tensorflow::run(globalCache()->getTFSession(), input_tensors, {outputTensorName_}, &outputs);
254  for (int k = 0; k < outputs.at(0).matrix<float>().dimension(1); k++)
255  tmpOut.push_back(outputs.at(0).matrix<float>()(0, k));
256  } else if (onnx_) {
258  tmpOut = globalCache()->getONNXSession().run({inputTensorName_}, inputs, {}, {outputTensorName_})[0];
259  }
260  for (size_t k = 0; k < output_names_.size(); k++)
261  mvaOut[k].push_back(output_formulas_[k](tmpOut));
262  }
263  }
264  }
265 
266  size_t k = 0;
267  for (auto& m : mvaOut) {
268  std::unique_ptr<edm::ValueMap<float>> mvaV(new edm::ValueMap<float>());
270  filler.insert(src, m.begin(), m.end());
271  filler.fill();
272  iEvent.put(std::move(mvaV), (tmva_) ? "" : output_names_[k]);
273  k++;
274  }
275 }
virtual void fillAdditionalVariables(const T &)
std::vector< NamedTensor > NamedTensorList
Definition: TensorFlow.h:31
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:30
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:259
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 158 of file BaseMVAValueMapProducer.h.

158 {}

◆ setValue()

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

Definition at line 141 of file BaseMVAValueMapProducer.h.

141  {
142  if (positions_.find(var) != positions_.end())
144  }
std::map< std::string, size_t > positions_

Member Data Documentation

◆ backend_

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

Definition at line 169 of file BaseMVAValueMapProducer.h.

◆ batch_eval_

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

Definition at line 175 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 171 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 161 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