Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:26

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   //need to substract 1 because checking on upper edges
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   std::string aName = name_ + separator_ + var;
0057   ComputedVariable* newVar = new ComputedVariable(method_, aName, arg_.iConfig, this, iC);
0058   if (iCompute_.find(var) != iCompute_.end()) {
0059     edm::LogError("VariableComputer") << "redeclaring: " << var << " skipping.";
0060     delete newVar;
0061     return;
0062   }
0063   iCompute_[var] = newVar;
0064   arg_.m.insert(std::make_pair(aName, newVar));
0065 }
0066 void VariableComputer::assign(std::string var, double& value) const {
0067   std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.find(var);
0068   if (it == iCompute_.end()) {
0069     std::stringstream ss;
0070     ss << "cannot assign: " << var << ". List of variable declared:\n";
0071     for (std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.begin(); it != iCompute_.end();
0072          ++it)
0073       ss << it->first << std::endl;
0074     edm::LogError("VariableComputer") << ss.str();
0075   } else
0076     it->second->setCache(value);
0077 }
0078 
0079 void VariableComputer::doesNotCompute() const {
0080   for (std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.begin(); it != iCompute_.end();
0081        ++it)
0082     it->second->setNotCompute();
0083 }
0084 void VariableComputer::doesNotCompute(std::string var) const {
0085   std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.find(var);
0086   if (it == iCompute_.end()) {
0087     std::stringstream ss;
0088     ss << "cannot act on: " << var << ". List of variable declared:\n";
0089     for (std::map<std::string, const ComputedVariable*>::const_iterator it = iCompute_.begin(); it != iCompute_.end();
0090          ++it)
0091       ss << it->first << std::endl;
0092     edm::LogError("VariableComputer") << ss.str();
0093   } else
0094     it->second->setNotCompute();
0095 }
0096 
0097 ComputedVariable::ComputedVariable(const CachingVariableFactoryArg& arg, edm::ConsumesCollector& iC)
0098     : CachingVariable("ComputedVariable", arg.n, arg.iConfig, iC),
0099       myComputer{VariableComputerFactory::get()->create(arg.iConfig.getParameter<std::string>("computer"), arg, iC)} {}
0100 
0101 VariableComputerTest::VariableComputerTest(const CachingVariable::CachingVariableFactoryArg& arg,
0102                                            edm::ConsumesCollector& iC)
0103     : VariableComputer(arg, iC) {
0104   declare("toto", iC);
0105   declare("tutu", iC);
0106   declare("much", iC);
0107 }
0108 
0109 void VariableComputerTest::compute(const edm::Event& iEvent) const {
0110   //does some mumbo jumbo with the event.
0111   // computes a bunch of doubles
0112   double toto = 3;
0113   double tutu = 4;
0114 
0115   //set the  variables  value (which do as if they had been cached)
0116   assign("toto", toto);
0117   assign("tutu", tutu);
0118   doesNotCompute("much");
0119 }