CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
ExpressionVariable< Object, label > Class Template Reference

#include <CachingVariable.h>

Inheritance diagram for ExpressionVariable< Object, label >:
CachingVariable

Public Member Functions

CachingVariable::evalType eval (const edm::Event &iEvent) const override
 
 ExpressionVariable (const CachingVariableFactoryArg &arg, edm::ConsumesCollector &iC)
 
 ~ExpressionVariable () override
 
- Public Member Functions inherited from CachingVariable
void addDescriptionLine (const std::string &s)
 
 CachingVariable (std::string m, std::string n, const edm::ParameterSet &iConfig, edm::ConsumesCollector &iC)
 
bool compute (const edm::Event &iEvent) const
 
const Descriptiondescription () const
 
const std::string & holderName () const
 
const std::string & method () const
 
const std::string & name () const
 
valueType operator() (const edm::Event &iEvent) const
 
void print () const
 
void setHolder (std::string hn) const
 
virtual ~CachingVariable ()
 

Private Attributes

StringObjectFunction< Object > * f_
 
StringObjectFunction< Object > * forder_
 
unsigned int index_
 
StringCutObjectSelector< Object > * selector_
 
edm::EDGetTokenT< edm::View< Object > > src_
 
edm::InputTag srcTag_
 

Additional Inherited Members

- Public Types inherited from CachingVariable
typedef std::pair< bool, valueTypeevalType
 
typedef double valueType
 
typedef std::map< std::string, const CachingVariable * > vMap
 
- Protected Member Functions inherited from CachingVariable
evalTypebaseEval (const edm::Event &iEvent) const
 
bool notSeenThisEventAlready (const edm::Event &iEvent) const
 
void setCache (valueType &v) const
 
void setNotCompute () const
 
- Protected Attributes inherited from CachingVariable
evalType cache_
 
edm::ParameterSet conf_
 
Description d_
 
edm::Event::CacheIdentifier_t eventCacheID_ = 0
 
std::string holderName_
 
std::string method_
 
std::string name_
 

Detailed Description

template<typename Object, const char * label>
class ExpressionVariable< Object, label >

Definition at line 281 of file CachingVariable.h.

Constructor & Destructor Documentation

◆ ExpressionVariable()

template<typename Object , const char * label>
ExpressionVariable< Object, label >::ExpressionVariable ( const CachingVariableFactoryArg arg,
edm::ConsumesCollector iC 
)
inline

Definition at line 283 of file CachingVariable.h.

References CachingVariable::addDescriptionLine(), edm::ConsumesCollector::consumes(), MillePedeFileConverter_cfg::e, electrons_cff::expr, ExpressionVariable< Object, label >::f_, ExpressionVariable< Object, label >::forder_, ExpressionVariable< Object, label >::index_, edm::ParameterSet::insert(), CachingVariable::name(), eventshapeDQM_cfi::order, corrVsCorr::selection, ExpressionVariable< Object, label >::selector_, ExpressionVariable< Object, label >::src_, ExpressionVariable< Object, label >::srcTag_, contentValuesCheck::ss, AlCaHLTBitMon_QueryRunRegistry::string, and findQualityFiles::v.

284  : CachingVariable(std::string(label) + "ExpressionVariable", arg.n, arg.iConfig, iC),
285  f_(nullptr),
286  forder_(nullptr) {
287  srcTag_ = edm::Service<InputTagDistributorService>()->retrieve("src", arg.iConfig);
289  //old style constructor
290  if (arg.iConfig.exists("expr") && arg.iConfig.exists("index")) {
291  std::string expr = arg.iConfig.getParameter<std::string>("expr");
292  index_ = arg.iConfig.getParameter<unsigned int>("index");
294  addDescriptionLine("calculating: " + expr);
295  std::stringstream ss;
296  ss << "on object at index: " << index_ << " of: " << srcTag_;
297 
298  if (arg.iConfig.exists("order")) {
299  std::string order = arg.iConfig.getParameter<std::string>("order");
301  ss << " after sorting according to: " << order;
302  } else
303  forder_ = nullptr;
304 
305  if (arg.iConfig.exists("selection")) {
306  std::string selection = arg.iConfig.getParameter<std::string>("selection");
308  ss << " and selecting only: " << selection;
309  } else
310  selector_ = nullptr;
311 
312  addDescriptionLine(ss.str());
313  ss.str("");
314  arg.m[arg.n] = this;
315  } else {
316  //multiple instance constructor
317  std::map<std::string, edm::Entry> indexEntry;
318  if (arg.n.find("_N") != std::string::npos) {
319  //will have to loop over indexes
320  std::vector<unsigned int> indexes = arg.iConfig.getParameter<std::vector<unsigned int> >("indexes");
321  for (unsigned int iI = 0; iI != indexes.size(); ++iI) {
322  edm::ParameterSet toUse = arg.iConfig;
323  edm::Entry e("unsigned int", indexes[iI], true);
324  std::stringstream ss;
325  //add +1 0->1, 1->2, ... in the variable label
326  ss << indexes[iI] + 1;
327  indexEntry.insert(std::make_pair(ss.str(), e));
328  }
329  } //contains "_N"
330 
331  std::map<std::string, edm::Entry> varEntry;
332  if (arg.n.find("_V") != std::string::npos) {
333  //do something fancy for multiple variable from one PSet
334  std::vector<std::string> vars = arg.iConfig.getParameter<std::vector<std::string> >("vars");
335  for (unsigned int v = 0; v != vars.size(); ++v) {
336  unsigned int sep = vars[v].find(':');
337  std::string name = vars[v].substr(0, sep);
338  std::string expr = vars[v].substr(sep + 1);
339 
340  edm::Entry e("string", expr, true);
341  varEntry.insert(std::make_pair(name, e));
342  }
343  } //contains "_V"
344 
345  std::string radical = arg.n;
346  //remove the "_V";
347  if (!varEntry.empty())
348  radical = radical.substr(0, radical.size() - 2);
349  //remove the "_N";
350  if (!indexEntry.empty())
351  radical = radical.substr(0, radical.size() - 2);
352 
353  if (varEntry.empty()) {
354  //loop only the indexes
355  for (std::map<std::string, edm::Entry>::iterator iIt = indexEntry.begin(); iIt != indexEntry.end(); ++iIt) {
356  edm::ParameterSet toUse = arg.iConfig;
357  toUse.insert(true, "index", iIt->second);
358  std::string newVname = radical + iIt->first;
359  // std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
360  // the constructor auto log the new variable in the map
362  }
363  } else {
364  for (std::map<std::string, edm::Entry>::iterator vIt = varEntry.begin(); vIt != varEntry.end(); ++vIt) {
365  if (indexEntry.empty()) {
366  edm::ParameterSet toUse = arg.iConfig;
367  toUse.insert(true, "expr", vIt->second);
368  std::string newVname = radical + vIt->first;
369  // std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
370  // the constructor auto log the new variable in the map
372  } else {
373  for (std::map<std::string, edm::Entry>::iterator iIt = indexEntry.begin(); iIt != indexEntry.end(); ++iIt) {
374  edm::ParameterSet toUse = arg.iConfig;
375  toUse.insert(true, "expr", vIt->second);
376  toUse.insert(true, "index", iIt->second);
377  std::string newVname = radical + iIt->first + vIt->first;
378  // std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
379  // the constructor auto log the new variable in the map
381  }
382  }
383  }
384  }
385  //there is a memory leak here, because the object we are in is not logged in the arg.m, the variable is not valid
386  // anyways, but reside in memory with no ways of de-allocating it.
387  // since the caching variables are actually "global" objects, it does not matter.
388  // we cannot add it to the map, otherwise, it would be considered for eventV ntupler
389  }
390  }
CachingVariable(std::string m, std::string n, const edm::ParameterSet &iConfig, edm::ConsumesCollector &iC)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
edm::InputTag srcTag_
vars
Definition: DeepTauIdBase.h:60
selection
main part
Definition: corrVsCorr.py:100
StringCutObjectSelector< Object > * selector_
void insert(bool ok_to_replace, char const *, Entry const &)
A arg
Definition: Factorize.h:31
char const * label
StringObjectFunction< Object > * f_
void addDescriptionLine(const std::string &s)
edm::EDGetTokenT< edm::View< Object > > src_
StringObjectFunction< Object > * forder_
ExpressionVariable(const CachingVariableFactoryArg &arg, edm::ConsumesCollector &iC)
const std::string & name() const

◆ ~ExpressionVariable()

template<typename Object , const char * label>
ExpressionVariable< Object, label >::~ExpressionVariable ( )
inlineoverride

Definition at line 391 of file CachingVariable.h.

References ExpressionVariable< Object, label >::f_, ExpressionVariable< Object, label >::forder_, and ExpressionVariable< Object, label >::selector_.

391  {
392  if (f_)
393  delete f_;
394  if (forder_)
395  delete forder_;
396  if (selector_)
397  delete selector_;
398  }
StringCutObjectSelector< Object > * selector_
StringObjectFunction< Object > * f_
StringObjectFunction< Object > * forder_

Member Function Documentation

◆ eval()

template<typename Object , const char * label>
CachingVariable::evalType ExpressionVariable< Object, label >::eval ( const edm::Event iEvent) const
inlineoverridevirtual

Reimplemented from CachingVariable.

Definition at line 400 of file CachingVariable.h.

References ExpressionVariable< Object, label >::f_, ExpressionVariable< Object, label >::forder_, mps_fire::i, iEvent, ExpressionVariable< Object, label >::index_, LogDebug, CachingVariable::method(), EcalTangentSkim_cfg::o, ExpressionVariable< Object, label >::selector_, jetUpdater_cfi::sort, ExpressionVariable< Object, label >::src_, and ExpressionVariable< Object, label >::srcTag_.

400  {
401  if (!f_) {
402  edm::LogError(method()) << " no parser attached.";
403  return std::make_pair(false, 0);
404  }
406  iEvent.getByToken(src_, oH);
407  if (index_ >= oH->size()) {
408  LogDebug(method()) << "fail to get object at index: " << index_ << " in collection: " << srcTag_;
409  return std::make_pair(false, 0);
410  }
411 
412  //get the ordering right first. if required
413  if (selector_ || forder_) {
414  std::vector<const Object*> copyToSort(0);
415  copyToSort.reserve(oH->size());
416  for (unsigned int i = 0; i != oH->size(); ++i) {
417  if (selector_ && !((*selector_)((*oH)[i])))
418  continue;
419  copyToSort.push_back(&(*oH)[i]);
420  }
421  if (index_ >= copyToSort.size())
422  return std::make_pair(false, 0);
423  if (forder_)
424  std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(forder_));
425 
426  const Object* o = copyToSort[index_];
427  return std::make_pair(true, (*f_)(*o));
428  } else {
429  const Object& o = (*oH)[index_];
430  return std::make_pair(true, (*f_)(o));
431  }
432  }
edm::InputTag srcTag_
StringCutObjectSelector< Object > * selector_
Log< level::Error, false > LogError
int iEvent
Definition: GenABIO.cc:224
StringObjectFunction< Object > * f_
const std::string & method() const
edm::EDGetTokenT< edm::View< Object > > src_
StringObjectFunction< Object > * forder_
#define LogDebug(id)

Member Data Documentation

◆ f_

template<typename Object , const char * label>
StringObjectFunction<Object>* ExpressionVariable< Object, label >::f_
private

◆ forder_

template<typename Object , const char * label>
StringObjectFunction<Object>* ExpressionVariable< Object, label >::forder_
private

◆ index_

template<typename Object , const char * label>
unsigned int ExpressionVariable< Object, label >::index_
private

◆ selector_

template<typename Object , const char * label>
StringCutObjectSelector<Object>* ExpressionVariable< Object, label >::selector_
private

◆ src_

template<typename Object , const char * label>
edm::EDGetTokenT<edm::View<Object> > ExpressionVariable< Object, label >::src_
private

◆ srcTag_

template<typename Object , const char * label>
edm::InputTag ExpressionVariable< Object, label >::srcTag_
private