CMS 3D CMS Logo

ExpressionVariable< Object, label > Class Template Reference

#include <PhysicsTools/UtilAlgos/interface/CachingVariable.h>

Inheritance diagram for ExpressionVariable< Object, label >:

CachingVariable

List of all members.

Public Member Functions

CachingVariable::evalType eval (const edm::Event &iEvent) const
 ExpressionVariable (CachingVariableFactoryArg arg)
 ~ExpressionVariable ()

Private Attributes

StringObjectFunction< Object > * f_
StringObjectFunction< Object > * forder_
uint index_
StringCutObjectSelector< Object > * selector_
edm::InputTag src_


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(), 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(), ExpressionVariable< Object, label >::selector_, ExpressionVariable< Object, label >::src_, ss, v, and vars.

00258                                                     :
00259     CachingVariable(std::string(label)+"ExpressionVariable",arg.n,arg.iConfig) , f_(0), forder_(0) {
00260     src_=edm::Service<InputTagDistributorService>()->retrieve("src",arg.iConfig);
00261     //old style constructor
00262     if (arg.iConfig.exists("expr") && arg.iConfig.exists("index")){
00263       std::string expr=arg.iConfig.getParameter<std::string>("expr");
00264       index_=arg.iConfig.getParameter<uint>("index");
00265       f_ = new StringObjectFunction<Object>(expr);
00266       addDescriptionLine("calculating: "+expr);
00267       std::stringstream ss;
00268       ss<<"on object at index: "<<index_<<" of: "<<src_;
00269 
00270       if (arg.iConfig.exists("order")){
00271         std::string order=arg.iConfig.getParameter<std::string>("order");
00272         forder_ = new StringObjectFunction<Object>(order);
00273         ss<<" after sorting according to: "<<order;
00274       }else forder_ =0;
00275 
00276       if (arg.iConfig.exists("selection")){
00277         std::string selection=arg.iConfig.getParameter<std::string>("selection");
00278         selector_ = new StringCutObjectSelector<Object>(selection);
00279         ss<<" and selecting only: "<<selection;
00280       }else selector_=0;
00281 
00282 
00283 
00284       addDescriptionLine(ss.str());     ss.str("");
00285       arg.m[arg.n] = this;
00286     }
00287     else{
00288       //multiple instance constructor
00289       std::map<std::string, edm::Entry> indexEntry;
00290       if (arg.n.find("_N")!=std::string::npos){
00291         //will have to loop over indexes
00292         std::vector<uint> indexes = arg.iConfig.getParameter<std::vector<uint> >("indexes");
00293         for (uint iI=0;iI!=indexes.size();++iI){
00294           edm::ParameterSet toUse = arg.iConfig;
00295           edm::Entry e("uint",indexes[iI],true);
00296           std::stringstream ss;
00297           //add +1 0->1, 1->2, ... in the variable label
00298           ss<<indexes[iI]+1;
00299           indexEntry.insert(std::make_pair(ss.str(),e));
00300         }
00301       }//contains "_N"
00302       
00303       std::map< std::string, edm::Entry> varEntry;
00304       if (arg.n.find("_V")!=std::string::npos){
00305         //do something fancy for multiple variable from one PSet
00306         std::vector<std::string> vars = arg.iConfig.getParameter<std::vector<std::string> >("vars");
00307         for (uint v=0;v!=vars.size();++v){
00308           uint sep=vars[v].find(":");
00309           std::string name=vars[v].substr(0,sep);
00310           std::string expr=vars[v].substr(sep+1);
00311           
00312           edm::Entry e("string",expr,true);
00313           varEntry.insert(std::make_pair(name,e));
00314         }
00315       }//contains "_V"
00316       
00317       std::string radical = arg.n;
00318       //remove the "_V";
00319       if (!varEntry.empty())
00320         radical = radical.substr(0,radical.size()-2);
00321       //remove the "_N";
00322       if (!indexEntry.empty())
00323         radical = radical.substr(0,radical.size()-2);
00324       
00325       if(varEntry.empty()){
00326         //loop only the indexes
00327         for(std::map< std::string, edm::Entry>::iterator iIt=indexEntry.begin();iIt!=indexEntry.end();++iIt){
00328           edm::ParameterSet toUse = arg.iConfig;
00329           toUse.insert(true,"index",iIt->second);
00330           std::string newVname = radical+iIt->first;
00331           //      std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
00332           // the constructor auto log the new variable in the map
00333           new ExpressionVariable(CachingVariable::CachingVariableFactoryArg(newVname,arg.m,toUse));
00334         }
00335       }else{
00336         for (std::map< std::string, edm::Entry>::iterator vIt=varEntry.begin();vIt!=varEntry.end();++vIt){
00337           if (indexEntry.empty()){
00338             edm::ParameterSet toUse = arg.iConfig;
00339             toUse.insert(true,"expr",vIt->second);
00340             std::string newVname = radical+vIt->first;
00341             //      std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
00342             // the constructor auto log the new variable in the map
00343             new ExpressionVariable(CachingVariable::CachingVariableFactoryArg(newVname,arg.m,toUse));
00344           }else{
00345             for(std::map< std::string, edm::Entry>::iterator iIt=indexEntry.begin();iIt!=indexEntry.end();++iIt){
00346               edm::ParameterSet toUse = arg.iConfig;
00347               toUse.insert(true,"expr",vIt->second);
00348               toUse.insert(true,"index",iIt->second);
00349               std::string newVname = radical+iIt->first+vIt->first;
00350               //              std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
00351               // the constructor auto log the new variable in the map 
00352               new ExpressionVariable(CachingVariable::CachingVariableFactoryArg(newVname,arg.m,toUse));
00353             }}
00354         }
00355       }
00356       //there is a memory leak here, because the object we are in is not logged in the arg.m, the variable is not valid
00357       // anyways, but reside in memory with no ways of de-allocating it.
00358       // since the caching variables are actually "global" objects, it does not matter.
00359       // we cannot add it to the map, otherwise, it would be considered for eventV ntupler
00360     }
00361   }
  ~ExpressionVariable(){

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_.

00362                        {
00363     if (f_) delete f_;
00364     if (forder_) delete forder_;
00365     if (selector_) delete selector_;
00366   }


Member Function Documentation

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

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(), ExpressionVariable< Object, label >::selector_, python::multivaluedict::sort(), and ExpressionVariable< Object, label >::src_.

00368                                                               {
00369     if (!f_) {
00370       edm::LogError(method())<<" no parser attached.";
00371       return std::make_pair(false,0);
00372     }
00373     edm::Handle<edm::View<Object> > oH;
00374     iEvent.getByLabel(src_,oH);
00375     if (index_>=oH->size()){
00376       LogDebug(method())<<"fail to get object at index: "<<index_<<" in collection: "<<src_;
00377       return std::make_pair(false,0);
00378     }
00379 
00380     //get the ordering right first. if required
00381     if (selector_ || forder_){
00382       std::vector<const Object*> copyToSort(0);
00383       copyToSort.reserve(oH->size());
00384       for (uint i=0;i!=oH->size();++i){
00385         if (selector_ && !((*selector_)((*oH)[i]))) continue;
00386         copyToSort.push_back(&(*oH)[i]);
00387       }
00388       if (index_ >= copyToSort.size()) return std::make_pair(false,0);
00389       if (forder_) std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(forder_));
00390       
00391       const Object * o = copyToSort[index_];
00392       return std::make_pair(true,(*f_)(*o));
00393     }
00394     else{
00395       const Object & o = (*oH)[index_];
00396       return std::make_pair(true,(*f_)(o));
00397     }
00398   }


Member Data Documentation

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

Definition at line 403 of file CachingVariable.h.

Referenced by ExpressionVariable< Object, label >::eval(), ExpressionVariable< Object, label >::ExpressionVariable(), and ExpressionVariable< Object, label >::~ExpressionVariable().

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

Definition at line 404 of file CachingVariable.h.

Referenced by ExpressionVariable< Object, label >::eval(), ExpressionVariable< Object, label >::ExpressionVariable(), and ExpressionVariable< Object, label >::~ExpressionVariable().

template<typename Object, const char * label>
uint ExpressionVariable< Object, label >::index_ [private]

Definition at line 402 of file CachingVariable.h.

Referenced by ExpressionVariable< Object, label >::eval(), and ExpressionVariable< Object, label >::ExpressionVariable().

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

Definition at line 405 of file CachingVariable.h.

Referenced by ExpressionVariable< Object, label >::eval(), ExpressionVariable< Object, label >::ExpressionVariable(), and ExpressionVariable< Object, label >::~ExpressionVariable().

template<typename Object, const char * label>
edm::InputTag ExpressionVariable< Object, label >::src_ [private]

Definition at line 401 of file CachingVariable.h.

Referenced by ExpressionVariable< Object, label >::eval(), and ExpressionVariable< Object, label >::ExpressionVariable().


The documentation for this class was generated from the following file:
Generated on Tue Jun 9 18:20:33 2009 for CMSSW by  doxygen 1.5.4