CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PhysicsTools/UtilAlgos/src/CachingVariable.cc

Go to the documentation of this file.
00001 #include "PhysicsTools/UtilAlgos/interface/CachingVariable.h"
00002 #include "PhysicsTools/UtilAlgos/interface/VariableHelper.h"
00003 
00004 CachingVariable::evalType VarSplitter::eval(const edm::Event & iEvent) const{
00005   const CachingVariable * var=edm::Service<VariableHelperService>()->get().variable(var_);
00006   if (!var->compute(iEvent)) return std::make_pair(false,0);
00007 
00008   double v=(*var)(iEvent);
00009   if (v<slots_.front()){
00010     if (useUnderFlow_) return std::make_pair(true,0);
00011     else return std::make_pair(false,0);
00012   }
00013   if (v>=slots_.back()){
00014     if (useOverFlow_) return std::make_pair(true,(double)maxIndex());
00015     else return std::make_pair(false,0);
00016   }
00017   unsigned int i=1;
00018   for (;i<slots_.size();++i)
00019     if (v<slots_[i]) break;
00020 
00021   if (useUnderFlow_) return std::make_pair(true,(double) i);
00022   //need to substract 1 because checking on upper edges
00023   else return std::make_pair(true,(double)i-1);
00024 }
00025 
00026 CachingVariable::evalType VariablePower::eval( const edm::Event & iEvent) const {
00027   const CachingVariable * var=edm::Service<VariableHelperService>()->get().variable(var_);
00028   if (!var->compute(iEvent)) return std::make_pair(false,0);
00029 
00030   double v=(*var)(iEvent);
00031   double p=exp(power_*log(v));
00032   return std::make_pair(true,p);
00033 }
00034 
00035 VariableComputer::VariableComputer(CachingVariable::CachingVariableFactoryArg arg) : arg_(arg) {
00036   if (arg_.iConfig.exists("separator")) separator_ = arg_.iConfig.getParameter<std::string>("separator");
00037   else separator_ ="_";
00038   
00039   name_=arg_.n;
00040   method_ = arg_.iConfig.getParameter<std::string>("computer");
00041 }
00042 
00043 void VariableComputer::declare(std::string var){
00044   std::string aName = name_+separator_+var;
00045   ComputedVariable * newVar = new ComputedVariable(method_,aName,arg_.iConfig,this);
00046   if (iCompute_.find(var) != iCompute_.end()){
00047     edm::LogError("VariableComputer")<<"redeclaring: "<<var<<" skipping.";
00048     delete newVar;
00049     return;
00050   }
00051   iCompute_[var] = newVar;
00052   arg_.m.insert(std::make_pair(aName,newVar));
00053 }
00054 void VariableComputer::assign(std::string var, double & value) const{
00055   std::map<std::string ,const ComputedVariable *>::const_iterator it=iCompute_.find(var);
00056   if (it == iCompute_.end()){
00057     std::stringstream ss;
00058     ss<<"cannot assign: "<<var<<". List of variable declared:\n";
00059     for ( std::map<std::string ,const ComputedVariable *>::const_iterator it=iCompute_.begin(); it!=iCompute_.end();++it)
00060       ss<<it->first<<std::endl;
00061     edm::LogError("VariableComputer")<<ss.str();
00062   }
00063   else
00064     it->second->setCache(value);
00065 }
00066 
00067 void VariableComputer::doesNotCompute() const{
00068   for ( std::map<std::string ,const ComputedVariable *>::const_iterator it=iCompute_.begin(); it!=iCompute_.end();++it)
00069     it->second->setNotCompute();
00070 }
00071 void VariableComputer::doesNotCompute(std::string var) const{
00072   std::map<std::string ,const ComputedVariable *>::const_iterator it=iCompute_.find(var);
00073   if (it == iCompute_.end()){
00074     std::stringstream ss;
00075     ss<<"cannot act on: "<<var<<". List of variable declared:\n";
00076     for ( std::map<std::string ,const ComputedVariable *>::const_iterator it=iCompute_.begin(); it!=iCompute_.end();++it)
00077       ss<<it->first<<std::endl;
00078     edm::LogError("VariableComputer")<<ss.str();
00079   }
00080   else
00081     it->second->setNotCompute();
00082 }
00083 
00084 
00085 ComputedVariable::ComputedVariable(CachingVariableFactoryArg arg ) : 
00086   CachingVariable("ComputedVariable",arg.n,arg.iConfig){
00087   // instanciate the computer
00088     std::string computerType = arg.iConfig.getParameter<std::string>("computer");
00089     myComputer = VariableComputerFactory::get()->create(computerType,arg);
00090     //there is a memory leak here, because the object we are in is not register anywhere. since it happens once per job, this is not a big deal.
00091 }
00092 
00093 VariableComputerTest::VariableComputerTest(CachingVariable::CachingVariableFactoryArg arg) : VariableComputer(arg){
00094   declare("toto");
00095   declare("tutu");
00096   declare("much");
00097 }
00098 
00099 void VariableComputerTest::compute(const edm::Event & iEvent) const{
00100   //does some mumbo jumbo with the event.
00101   // computes a bunch of doubles
00102   double toto = 3;
00103   double tutu = 4;
00104   
00105   //set the  variables  value (which do as if they had been cached)
00106   assign("toto",toto);
00107   assign("tutu",tutu);
00108   doesNotCompute("much");
00109 }