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 264 of file CachingVariable.h.

Constructor & Destructor Documentation

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

Definition at line 266 of file CachingVariable.h.

References edm::ConsumesCollector::consumes(), MillePedeFileConverter_cfg::e, edm::ParameterSet::exists(), jets_cff::expr, edm::ParameterSet::getParameter(), CachingVariable::CachingVariableFactoryArg::iConfig, edm::ParameterSet::insert(), CachingVariable::CachingVariableFactoryArg::m, CachingVariable::CachingVariableFactoryArg::n, dataset::name, corrVsCorr::selection, AlCaHLTBitMon_QueryRunRegistry::string, and findQualityFiles::v.

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

Definition at line 371 of file CachingVariable.h.

371  {
372  if (f_) delete f_;
373  if (forder_) delete forder_;
374  if (selector_) delete selector_;
375  }
StringCutObjectSelector< Object > * selector_
StringObjectFunction< Object > * f_
StringObjectFunction< Object > * forder_

Member Function Documentation

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

Reimplemented from CachingVariable.

Definition at line 377 of file CachingVariable.h.

References edm::Event::getByToken(), mps_fire::i, LogDebug, AlcaSiPixelAliHarvester0T_cff::method, and connectstrParser::o.

377  {
378  if (!f_) {
379  edm::LogError(method())<<" no parser attached.";
380  return std::make_pair(false,0);
381  }
383  iEvent.getByToken(src_,oH);
384  if (index_>=oH->size()){
385  LogDebug(method())<<"fail to get object at index: "<<index_<<" in collection: "<<srcTag_;
386  return std::make_pair(false,0);
387  }
388 
389  //get the ordering right first. if required
390  if (selector_ || forder_){
391  std::vector<const Object*> copyToSort(0);
392  copyToSort.reserve(oH->size());
393  for (unsigned int i=0;i!=oH->size();++i){
394  if (selector_ && !((*selector_)((*oH)[i]))) continue;
395  copyToSort.push_back(&(*oH)[i]);
396  }
397  if (index_ >= copyToSort.size()) return std::make_pair(false,0);
398  if (forder_) std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(forder_));
399 
400  const Object * o = copyToSort[index_];
401  return std::make_pair(true,(*f_)(*o));
402  }
403  else{
404  const Object & o = (*oH)[index_];
405  return std::make_pair(true,(*f_)(o));
406  }
407  }
#define LogDebug(id)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
edm::InputTag srcTag_
StringCutObjectSelector< Object > * selector_
StringObjectFunction< Object > * f_
edm::EDGetTokenT< edm::View< Object > > src_
StringObjectFunction< Object > * forder_
const std::string & method() const

Member Data Documentation

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

Definition at line 413 of file CachingVariable.h.

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

Definition at line 414 of file CachingVariable.h.

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

Definition at line 412 of file CachingVariable.h.

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

Definition at line 415 of file CachingVariable.h.

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

Definition at line 411 of file CachingVariable.h.

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

Definition at line 410 of file CachingVariable.h.