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::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  name_(iConfig.getParameter<std::string>("name")),
90  backend_(iConfig.getParameter<std::string>("backend")),
91  weightfilename_(iConfig.getParameter<edm::FileInPath>("weightFile").fullPath()),
92  tmva_(backend_ == "TMVA"),
93  tf_(backend_ == "TF"),
94  onnx_(backend_ == "ONNX"),
95  batch_eval_(iConfig.getParameter<bool>("batch_eval")) {
96  if (tmva_) {
97  reader_ = new TMVA::Reader();
98  isClassifier_ = iConfig.getParameter<bool>("isClassifier");
99  }
100 
101  std::vector<edm::ParameterSet> const& varsPSet = iConfig.getParameter<std::vector<edm::ParameterSet>>("variables");
102  values_.resize(varsPSet.size());
103  size_t i = 0;
104  for (const edm::ParameterSet& var_pset : varsPSet) {
105  const std::string& vname = var_pset.getParameter<std::string>("name");
106  if (var_pset.existsAs<std::string>("expr"))
107  funcs_.emplace_back(
108  std::pair<std::string, StringObjectFunction<T, true>>(vname, var_pset.getParameter<std::string>("expr")));
109  positions_[vname] = i;
110  if (tmva_)
111  reader_->AddVariable(vname, (&values_.front()) + i);
112  i++;
113  }
114 
115  if (tmva_) {
117  }
118  if (tf_ || onnx_) {
119  inputTensorName_ = iConfig.getParameter<std::string>("inputTensorName");
120  outputTensorName_ = iConfig.getParameter<std::string>("outputTensorName");
121  output_names_ = iConfig.getParameter<std::vector<std::string>>("outputNames");
122  for (const auto& s : iConfig.getParameter<std::vector<std::string>>("outputFormulas")) {
123  output_formulas_.push_back(StringObjectFunction<std::vector<float>>(s));
124  }
125  }
126 
127  if (tmva_)
128  produces<edm::ValueMap<float>>();
129  else {
130  for (const auto& n : output_names_) {
131  produces<edm::ValueMap<float>>(n);
132  }
133  }
134  }
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
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_
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 135 of file BaseMVAValueMapProducer.h.

135 {}

Member Function Documentation

◆ beginStream()

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

Definition at line 149 of file BaseMVAValueMapProducer.h.

149 {};

◆ endStream()

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

Definition at line 151 of file BaseMVAValueMapProducer.h.

151 {};

◆ fillAdditionalVariables()

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

Reimplemented in BJetEnergyRegressionMVA, and EvaluateMuonMVAID.

Definition at line 155 of file BaseMVAValueMapProducer.h.

155 {}

◆ fillDescriptions()

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

Definition at line 328 of file BaseMVAValueMapProducer.h.

328  {
331  if (typeid(T) == typeid(pat::Jet))
332  modname += "Jet";
333  else if (typeid(T) == typeid(pat::Muon))
334  modname += "Muon";
335  else if (typeid(T) == typeid(pat::Electron))
336  modname += "Ele";
337  modname += "BaseMVAValueMapProducer";
338  descriptions.add(modname, desc);
339 }
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 286 of file BaseMVAValueMapProducer.h.

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

286  {
288  desc.add<edm::InputTag>("src")->setComment("input physics object collection");
289 
290  desc.add<std::string>("name")->setComment("output score variable name");
291  desc.add<edm::FileInPath>("weightFile")->setComment("xml weight file, or TF/ONNX model file");
292  desc.add<bool>("batch_eval", false)->setComment("Run inference in batch instead of per-object");
293 
295  variable.add<std::string>("name")->setComment("name of the variable, either created by expr, or internally by code");
296  variable.addOptional<std::string>("expr")->setComment(
297  "a function to define the content of the model input, absence of it means the leaf is computed internally");
298  variable.setComment("a PSet to define an entry to the ML model");
299  desc.addVPSet("variables", variable);
300 
302  "inputTensorName", "", true, edm::Comment("Name of tensorflow input tensor in the model"));
304  "outputTensorName", "", true, edm::Comment("Name of tensorflow output tensor in the model"));
306  "outputNames",
307  std::vector<std::string>(),
308  true,
309  edm::Comment("Names of the output values to be used in the output valuemap"));
311  "outputFormulas",
312  std::vector<std::string>(),
313  true,
314  edm::Comment("Formulas to be used to post process the output"));
316  "disableONNXGraphOpt", false, true, edm::Comment("Disable ONNX runtime graph optimization"));
317 
319  "backend", "TMVA", true, edm::Comment("the backend to evaluate the model:tmva, tf or onnx")),
321  "isClassifier", true, true, edm::Comment("a classification or regression")) or
322  "TF" >> (itn and otn and on and of) or "ONNX" >> (itn and otn and on and of and dog));
323 
324  return desc;
325 }
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12

◆ globalEndJob()

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

Definition at line 283 of file BaseMVAValueMapProducer.h.

283 {}

◆ initializeGlobalCache()

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

Definition at line 273 of file BaseMVAValueMapProducer.h.

273  {
274  std::string backend = cfg.getParameter<std::string>("backend");
275  bool disableONNXGraphOpt = false;
276  if (backend == "ONNX")
277  disableONNXGraphOpt = cfg.getParameter<bool>("disableONNXGraphOpt");
278  return std::make_unique<BaseMVACache>(
279  cfg.getParameter<edm::FileInPath>("weightFile").fullPath(), backend, disableONNXGraphOpt);
280 }

◆ produce()

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

Definition at line 178 of file BaseMVAValueMapProducer.h.

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

154 {}

◆ setValue()

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

Definition at line 137 of file BaseMVAValueMapProducer.h.

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

Member Data Documentation

◆ backend_

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

Definition at line 164 of file BaseMVAValueMapProducer.h.

◆ batch_eval_

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

Definition at line 170 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

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

◆ weightfilename_

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