CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
 
 ExpressionVariable (const CachingVariableFactoryArg &arg)
 
 ~ExpressionVariable ()
 
- Public Member Functions inherited from CachingVariable
void addDescriptionLine (const std::string &s)
 
 CachingVariable (std::string m, std::string n, const edm::ParameterSet &iConfig)
 
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::InputTag src_
 

Additional Inherited Members

- Public Types inherited from CachingVariable
typedef std::pair< bool,
valueType
evalType
 
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 276 of file CachingVariable.h.

Constructor & Destructor Documentation

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

Definition at line 278 of file CachingVariable.h.

References CachingVariable::addDescriptionLine(), alignCSCRings::e, edm::ParameterSet::exists(), ExpressionVariable< Object, label >::f_, ExpressionVariable< Object, label >::forder_, edm::ParameterSet::getParameter(), CachingVariable::CachingVariableFactoryArg::iConfig, ExpressionVariable< Object, label >::index_, edm::ParameterSet::insert(), CachingVariable::CachingVariableFactoryArg::m, CachingVariable::CachingVariableFactoryArg::n, CachingVariable::name(), corrVsCorr::selection, ExpressionVariable< Object, label >::selector_, ExpressionVariable< Object, label >::src_, AlCaHLTBitMon_QueryRunRegistry::string, and findQualityFiles::v.

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

Definition at line 382 of file CachingVariable.h.

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

382  {
383  if (f_) delete f_;
384  if (forder_) delete forder_;
385  if (selector_) delete selector_;
386  }
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
inlinevirtual

Reimplemented from CachingVariable.

Definition at line 388 of file CachingVariable.h.

References ExpressionVariable< Object, label >::f_, ExpressionVariable< Object, label >::forder_, edm::Event::getByLabel(), i, ExpressionVariable< Object, label >::index_, LogDebug, CachingVariable::method(), python.connectstrParser::o, ExpressionVariable< Object, label >::selector_, python.multivaluedict::sort(), and ExpressionVariable< Object, label >::src_.

388  {
389  if (!f_) {
390  edm::LogError(method())<<" no parser attached.";
391  return std::make_pair(false,0);
392  }
394  iEvent.getByLabel(src_,oH);
395  if (index_>=oH->size()){
396  LogDebug(method())<<"fail to get object at index: "<<index_<<" in collection: "<<src_;
397  return std::make_pair(false,0);
398  }
399 
400  //get the ordering right first. if required
401  if (selector_ || forder_){
402  std::vector<const Object*> copyToSort(0);
403  copyToSort.reserve(oH->size());
404  for (unsigned int i=0;i!=oH->size();++i){
405  if (selector_ && !((*selector_)((*oH)[i]))) continue;
406  copyToSort.push_back(&(*oH)[i]);
407  }
408  if (index_ >= copyToSort.size()) return std::make_pair(false,0);
409  if (forder_) std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(forder_));
410 
411  const Object * o = copyToSort[index_];
412  return std::make_pair(true,(*f_)(*o));
413  }
414  else{
415  const Object & o = (*oH)[index_];
416  return std::make_pair(true,(*f_)(o));
417  }
418  }
#define LogDebug(id)
int i
Definition: DBlmapReader.cc:9
StringCutObjectSelector< Object > * selector_
StringObjectFunction< Object > * f_
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:390
StringObjectFunction< Object > * forder_
edm::InputTag src_
const std::string & method() const

Member Data Documentation

template<typename Object , const char * label>
StringObjectFunction<Object>* ExpressionVariable< Object, label >::f_
private
template<typename Object , const char * label>
StringObjectFunction<Object>* ExpressionVariable< Object, label >::forder_
private
template<typename Object , const char * label>
unsigned int ExpressionVariable< Object, label >::index_
private
template<typename Object , const char * label>
StringCutObjectSelector<Object>* ExpressionVariable< Object, label >::selector_
private
template<typename Object , const char * label>
edm::InputTag ExpressionVariable< Object, label >::src_
private