File indexing completed on 2024-10-09 23:02:39
0001 #include "PhysicsTools/UtilAlgos/interface/CachingVariable.h"
0002 #include "PhysicsTools/UtilAlgos/interface/VariableHelper.h"
0003
0004 CachingVariable::evalType VarSplitter::eval(const edm::Event& iEvent) const {
0005 const CachingVariable* var = edm::Service<VariableHelperService>()->get().variable(var_);
0006 if (!var->compute(iEvent))
0007 return std::make_pair(false, 0);
0008
0009 double v = (*var)(iEvent);
0010 if (v < slots_.front()) {
0011 if (useUnderFlow_)
0012 return std::make_pair(true, 0);
0013 else
0014 return std::make_pair(false, 0);
0015 }
0016 if (v >= slots_.back()) {
0017 if (useOverFlow_)
0018 return std::make_pair(true, (double)maxIndex());
0019 else
0020 return std::make_pair(false, 0);
0021 }
0022 unsigned int i = 1;
0023 for (; i < slots_.size(); ++i)
0024 if (v < slots_[i])
0025 break;
0026
0027 if (useUnderFlow_)
0028 return std::make_pair(true, (double)i);
0029
0030 else
0031 return std::make_pair(true, (double)i - 1);
0032 }
0033
0034 CachingVariable::evalType VariablePower::eval(const edm::Event& iEvent) const {
0035 const CachingVariable* var = edm::Service<VariableHelperService>()->get().variable(var_);
0036 if (!var->compute(iEvent))
0037 return std::make_pair(false, 0);
0038
0039 double v = (*var)(iEvent);
0040 double p = exp(power_ * log(v));
0041 return std::make_pair(true, p);
0042 }
0043
0044 VariableComputer::VariableComputer(const CachingVariable::CachingVariableFactoryArg& arg, edm::ConsumesCollector& iC)
0045 : arg_(arg) {
0046 if (arg_.iConfig.exists("separator"))
0047 separator_ = arg_.iConfig.getParameter<std::string>("separator");
0048 else
0049 separator_ = "_";
0050
0051 name_ = arg_.n;
0052 method_ = arg_.iConfig.getParameter<std::string>("computer");
0053 }
0054
0055 void VariableComputer::declare(std::string var, edm::ConsumesCollector& iC) {
0056 if (iCompute_.find(var) != iCompute_.end()) {
0057 edm::LogError("VariableComputer") << "redeclaring: " << var << " skipping.";
0058 return;
0059 }
0060 std::string aName = name_ + separator_ + var;
0061 ComputedVariable* newVar = new ComputedVariable(method_, aName, arg_.iConfig, this, iC);
0062 iCompute_[var] = newVar;
0063 arg_.m.insert(std::make_pair(aName, newVar));
0064 }
0065 void VariableComputer::assign(std::string var, double& value) const {
0066 std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.find(var);
0067 if (it == iCompute_.end()) {
0068 std::stringstream ss;
0069 ss << "cannot assign: " << var << ". List of variable declared:\n";
0070 for (std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.begin(); it != iCompute_.end();
0071 ++it)
0072 ss << it->first << std::endl;
0073 edm::LogError("VariableComputer") << ss.str();
0074 } else
0075 it->second->setCache(value);
0076 }
0077
0078 void VariableComputer::doesNotCompute() const {
0079 for (std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.begin(); it != iCompute_.end();
0080 ++it)
0081 it->second->setNotCompute();
0082 }
0083 void VariableComputer::doesNotCompute(std::string var) const {
0084 std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.find(var);
0085 if (it == iCompute_.end()) {
0086 std::stringstream ss;
0087 ss << "cannot act on: " << var << ". List of variable declared:\n";
0088 for (std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.begin(); it != iCompute_.end();
0089 ++it)
0090 ss << it->first << std::endl;
0091 edm::LogError("VariableComputer") << ss.str();
0092 } else
0093 it->second->setNotCompute();
0094 }
0095
0096 ComputedVariable::ComputedVariable(const CachingVariableFactoryArg& arg, edm::ConsumesCollector& iC)
0097 : CachingVariable("ComputedVariable", arg.n, arg.iConfig, iC),
0098 myComputer{VariableComputerFactory::get()->create(arg.iConfig.getParameter<std::string>("computer"), arg, iC)} {}
0099
0100 VariableComputerTest::VariableComputerTest(const CachingVariable::CachingVariableFactoryArg& arg,
0101 edm::ConsumesCollector& iC)
0102 : VariableComputer(arg, iC) {
0103 declare("toto", iC);
0104 declare("tutu", iC);
0105 declare("much", iC);
0106 }
0107
0108 void VariableComputerTest::compute(const edm::Event& iEvent) const {
0109
0110
0111 double toto = 3;
0112 double tutu = 4;
0113
0114
0115 assign("toto", toto);
0116 assign("tutu", tutu);
0117 doesNotCompute("much");
0118 }