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