CMS 3D CMS Logo

Public Member Functions | Private Attributes

ExpressionVariable< Object, label > Class Template Reference

#include <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_
unsigned int 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(), 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(), elec_selection::selection(), ExpressionVariable< Object, label >::selector_, ExpressionVariable< Object, label >::src_, and v.

                                                    :
    CachingVariable(std::string(label)+"ExpressionVariable",arg.n,arg.iConfig) , f_(0), forder_(0) {
    src_=edm::Service<InputTagDistributorService>()->retrieve("src",arg.iConfig);
    //old style constructor
    if (arg.iConfig.exists("expr") && arg.iConfig.exists("index")){
      std::string expr=arg.iConfig.getParameter<std::string>("expr");
      index_=arg.iConfig.getParameter<unsigned int>("index");
      f_ = new StringObjectFunction<Object>(expr);
      addDescriptionLine("calculating: "+expr);
      std::stringstream ss;
      ss<<"on object at index: "<<index_<<" of: "<<src_;

      if (arg.iConfig.exists("order")){
        std::string order=arg.iConfig.getParameter<std::string>("order");
        forder_ = new StringObjectFunction<Object>(order);
        ss<<" after sorting according to: "<<order;
      }else forder_ =0;

      if (arg.iConfig.exists("selection")){
        std::string selection=arg.iConfig.getParameter<std::string>("selection");
        selector_ = new StringCutObjectSelector<Object>(selection);
        ss<<" and selecting only: "<<selection;
      }else selector_=0;



      addDescriptionLine(ss.str());     ss.str("");
      arg.m[arg.n] = this;
    }
    else{
      //multiple instance constructor
      std::map<std::string, edm::Entry> indexEntry;
      if (arg.n.find("_N")!=std::string::npos){
        //will have to loop over indexes
        std::vector<unsigned int> indexes = arg.iConfig.getParameter<std::vector<unsigned int> >("indexes");
        for (unsigned int iI=0;iI!=indexes.size();++iI){
          edm::ParameterSet toUse = arg.iConfig;
          edm::Entry e("unsigned int",indexes[iI],true);
          std::stringstream ss;
          //add +1 0->1, 1->2, ... in the variable label
          ss<<indexes[iI]+1;
          indexEntry.insert(std::make_pair(ss.str(),e));
        }
      }//contains "_N"
      
      std::map< std::string, edm::Entry> varEntry;
      if (arg.n.find("_V")!=std::string::npos){
        //do something fancy for multiple variable from one PSet
        std::vector<std::string> vars = arg.iConfig.getParameter<std::vector<std::string> >("vars");
        for (unsigned int v=0;v!=vars.size();++v){
          unsigned int sep=vars[v].find(":");
          std::string name=vars[v].substr(0,sep);
          std::string expr=vars[v].substr(sep+1);
          
          edm::Entry e("string",expr,true);
          varEntry.insert(std::make_pair(name,e));
        }
      }//contains "_V"
      
      std::string radical = arg.n;
      //remove the "_V";
      if (!varEntry.empty())
        radical = radical.substr(0,radical.size()-2);
      //remove the "_N";
      if (!indexEntry.empty())
        radical = radical.substr(0,radical.size()-2);
      
      if(varEntry.empty()){
        //loop only the indexes
        for(std::map< std::string, edm::Entry>::iterator iIt=indexEntry.begin();iIt!=indexEntry.end();++iIt){
          edm::ParameterSet toUse = arg.iConfig;
          toUse.insert(true,"index",iIt->second);
          std::string newVname = radical+iIt->first;
          //      std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
          // the constructor auto log the new variable in the map
          new ExpressionVariable(CachingVariable::CachingVariableFactoryArg(newVname,arg.m,toUse));
        }
      }else{
        for (std::map< std::string, edm::Entry>::iterator vIt=varEntry.begin();vIt!=varEntry.end();++vIt){
          if (indexEntry.empty()){
            edm::ParameterSet toUse = arg.iConfig;
            toUse.insert(true,"expr",vIt->second);
            std::string newVname = radical+vIt->first;
            //      std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
            // the constructor auto log the new variable in the map
            new ExpressionVariable(CachingVariable::CachingVariableFactoryArg(newVname,arg.m,toUse));
          }else{
            for(std::map< std::string, edm::Entry>::iterator iIt=indexEntry.begin();iIt!=indexEntry.end();++iIt){
              edm::ParameterSet toUse = arg.iConfig;
              toUse.insert(true,"expr",vIt->second);
              toUse.insert(true,"index",iIt->second);
              std::string newVname = radical+iIt->first+vIt->first;
              //              std::cout<<"in the loop, creating variable with name: "<<newVname<<std::endl;
              // the constructor auto log the new variable in the map 
              new ExpressionVariable(CachingVariable::CachingVariableFactoryArg(newVname,arg.m,toUse));
            }}
        }
      }
      //there is a memory leak here, because the object we are in is not logged in the arg.m, the variable is not valid
      // anyways, but reside in memory with no ways of de-allocating it.
      // since the caching variables are actually "global" objects, it does not matter.
      // we cannot add it to the map, otherwise, it would be considered for eventV ntupler
    }
  }
template<typename Object , const char * label>
ExpressionVariable< Object, label >::~ExpressionVariable ( ) [inline]

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

                                                              {
    if (!f_) {
      edm::LogError(method())<<" no parser attached.";
      return std::make_pair(false,0);
    }
    edm::Handle<edm::View<Object> > oH;
    iEvent.getByLabel(src_,oH);
    if (index_>=oH->size()){
      LogDebug(method())<<"fail to get object at index: "<<index_<<" in collection: "<<src_;
      return std::make_pair(false,0);
    }

    //get the ordering right first. if required
    if (selector_ || forder_){
      std::vector<const Object*> copyToSort(0);
      copyToSort.reserve(oH->size());
      for (unsigned int i=0;i!=oH->size();++i){
        if (selector_ && !((*selector_)((*oH)[i]))) continue;
        copyToSort.push_back(&(*oH)[i]);
      }
      if (index_ >= copyToSort.size()) return std::make_pair(false,0);
      if (forder_) std::sort(copyToSort.begin(), copyToSort.end(), sortByStringFunction<Object>(forder_));
      
      const Object * o = copyToSort[index_];
      return std::make_pair(true,(*f_)(*o));
    }
    else{
      const Object & o = (*oH)[index_];
      return std::make_pair(true,(*f_)(o));
    }
  }

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]