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 (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
 
void setCache (valueType &v) const
 
void setNotCompute () const
 
- Protected Attributes inherited from CachingVariable
evalType cache_
 
edm::ParameterSet conf_
 
Description d_
 
std::string holderName_
 
std::string method_
 
std::string name_
 

Detailed Description

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

Definition at line 256 of file CachingVariable.h.

Constructor & Destructor Documentation

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

Definition at line 258 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_, and v.

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

Definition at line 362 of file CachingVariable.h.

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

362  {
363  if (f_) delete f_;
364  if (forder_) delete forder_;
365  if (selector_) delete selector_;
366  }
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 368 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_.

368  {
369  if (!f_) {
370  edm::LogError(method())<<" no parser attached.";
371  return std::make_pair(false,0);
372  }
374  iEvent.getByLabel(src_,oH);
375  if (index_>=oH->size()){
376  LogDebug(method())<<"fail to get object at index: "<<index_<<" in collection: "<<src_;
377  return std::make_pair(false,0);
378  }
379 
380  //get the ordering right first. if required
381  if (selector_ || forder_){
382  std::vector<const Object*> copyToSort(0);
383  copyToSort.reserve(oH->size());
384  for (unsigned int i=0;i!=oH->size();++i){
385  if (selector_ && !((*selector_)((*oH)[i]))) continue;
386  copyToSort.push_back(&(*oH)[i]);
387  }
388  if (index_ >= copyToSort.size()) return std::make_pair(false,0);
389  if (forder_) std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(forder_));
390 
391  const Object * o = copyToSort[index_];
392  return std::make_pair(true,(*f_)(*o));
393  }
394  else{
395  const Object & o = (*oH)[index_];
396  return std::make_pair(true,(*f_)(o));
397  }
398  }
#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:356
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