Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:23

0001 // -*- C++ -*-
0002 #include <iostream>
0003 #include <fstream>
0004 #include <iomanip>
0005 #include <cassert>
0006 #include <algorithm>
0007 #include "PhysicsTools/FWLite/interface/VariableMapCont.h"
0008 
0009 using namespace std;
0010 using namespace optutl;
0011 
0012 const int VariableMapCont::kDefaultInteger = 0;
0013 const double VariableMapCont::kDefaultDouble = 0.;
0014 const std::string VariableMapCont::kDefaultString = "";
0015 const bool VariableMapCont::kDefaultBool = false;
0016 const VariableMapCont::IVec VariableMapCont::kEmptyIVec;
0017 const VariableMapCont::DVec VariableMapCont::kEmptyDVec;
0018 const VariableMapCont::SVec VariableMapCont::kEmptySVec;
0019 
0020 VariableMapCont::VariableMapCont() {}
0021 
0022 VariableMapCont::OptionType VariableMapCont::hasVariable(string key) {
0023   lowercaseString(key);
0024   // Look through our maps to see if we've got it
0025   if (m_integerMap.end() != m_integerMap.find(key))
0026     return kInteger;
0027   if (m_doubleMap.end() != m_doubleMap.find(key))
0028     return kDouble;
0029   if (m_stringMap.end() != m_stringMap.find(key))
0030     return kString;
0031   if (m_boolMap.end() != m_boolMap.find(key))
0032     return kBool;
0033   if (m_integerVecMap.end() != m_integerVecMap.find(key))
0034     return kIntegerVector;
0035   if (m_doubleVecMap.end() != m_doubleVecMap.find(key))
0036     return kDoubleVector;
0037   if (m_stringVecMap.end() != m_stringVecMap.find(key))
0038     return kStringVector;
0039   // if we're here, the answer's no.
0040   return kNone;
0041 }
0042 
0043 void VariableMapCont::lowercaseString(string &arg) {
0044   // assumes 'toLower(ch)' modifies ch
0045   std::for_each(arg.begin(), arg.end(), VariableMapCont::toLower);
0046   // // assumes 'toLower(ch)' returns the lower case char
0047   // std::transform (arg.begin(), arg.end(), arg.begin(),
0048   //                 VariableMapCont::toLower);
0049 }
0050 
0051 char VariableMapCont::toLower(char &ch) {
0052   ch = tolower(ch);
0053   return ch;
0054 }
0055 
0056 void VariableMapCont::_checkKey(string &key, const string &description) {
0057   // Let's make sure we don't already have this key
0058   lowercaseString(key);
0059   if (m_variableModifiedMap.end() != m_variableModifiedMap.find(key)) {
0060     cerr << "VariableMapCont::addVariable() Error: Key '" << key << "' has already been defined.  Aborting." << endl;
0061     assert(0);
0062   }  // found a duplicate
0063   m_variableModifiedMap[key] = false;
0064   m_variableDescriptionMap[key] = description;
0065 }
0066 
0067 void VariableMapCont::addOption(string key, OptionType type, const string &description) {
0068   _checkKey(key, description);
0069   if (kInteger == type) {
0070     m_integerMap[key] = kDefaultInteger;
0071     return;
0072   }
0073   if (kDouble == type) {
0074     m_doubleMap[key] = kDefaultDouble;
0075     return;
0076   }
0077   if (kString == type) {
0078     m_stringMap[key] = kDefaultString;
0079     return;
0080   }
0081   if (kBool == type) {
0082     m_boolMap[key] = kDefaultBool;
0083     return;
0084   }
0085   if (kIntegerVector == type) {
0086     m_integerVecMap[key] = kEmptyIVec;
0087     return;
0088   }
0089   if (kDoubleVector == type) {
0090     m_doubleVecMap[key] = kEmptyDVec;
0091     return;
0092   }
0093   if (kStringVector == type) {
0094     m_stringVecMap[key] = kEmptySVec;
0095     return;
0096   }
0097 }
0098 
0099 void VariableMapCont::addOption(string key, OptionType type, const string &description, int defaultValue) {
0100   _checkKey(key, description);
0101   if (kInteger != type) {
0102     cerr << "VariableMapCont::addOption() Error: Key '" << key << "' is not defined as an integer but has an integer "
0103          << "default value. Aborting." << endl;
0104     assert(0);
0105   }
0106   m_integerMap[key] = defaultValue;
0107 }
0108 
0109 void VariableMapCont::addOption(string key, OptionType type, const string &description, double defaultValue) {
0110   _checkKey(key, description);
0111   if (kDouble != type) {
0112     cerr << "VariableMapCont::addOption() Error: Key '" << key << "' is not defined as an double but has an double "
0113          << "default value. Aborting." << endl;
0114     assert(0);
0115   }
0116   m_doubleMap[key] = defaultValue;
0117 }
0118 
0119 void VariableMapCont::addOption(string key, OptionType type, const string &description, const string &defaultValue) {
0120   _checkKey(key, description);
0121   if (kString != type) {
0122     cerr << "VariableMapCont::addOption() Error: Key '" << key << "' is not defined as an string but has an string "
0123          << "default value. Aborting." << endl;
0124     assert(0);
0125   }
0126   m_stringMap[key] = defaultValue;
0127 }
0128 
0129 void VariableMapCont::addOption(string key, OptionType type, const string &description, const char *defaultValue) {
0130   addOption(key, type, description, (string)defaultValue);
0131 }
0132 
0133 void VariableMapCont::addOption(string key, OptionType type, const string &description, bool defaultValue) {
0134   _checkKey(key, description);
0135   if (kBool != type) {
0136     cerr << "VariableMapCont::addOption() Error: Key '" << key << "' is not defined as an bool but has an bool "
0137          << "default value. Aborting." << endl;
0138     assert(0);
0139   }
0140   m_boolMap[key] = defaultValue;
0141 }
0142 
0143 int &VariableMapCont::integerValue(std::string key) {
0144   lowercaseString(key);
0145   SIMapIter iter = m_integerMap.find(key);
0146   if (m_integerMap.end() == iter) {
0147     cerr << "VariableMapCont::integerValue() Error: key '" << key << "' not found.  Aborting." << endl;
0148     assert(0);
0149   }
0150   return iter->second;
0151 }
0152 
0153 double &VariableMapCont::doubleValue(std::string key) {
0154   lowercaseString(key);
0155   SDMapIter iter = m_doubleMap.find(key);
0156   if (m_doubleMap.end() == iter) {
0157     cerr << "VariableMapCont::doubleValue() Error: key '" << key << "' not found.  Aborting." << endl;
0158     assert(0);
0159   }
0160   return iter->second;
0161 }
0162 
0163 string &VariableMapCont::stringValue(std::string key) {
0164   lowercaseString(key);
0165   SSMapIter iter = m_stringMap.find(key);
0166   if (m_stringMap.end() == iter) {
0167     cerr << "VariableMapCont::stringValue() Error: key '" << key << "' not found.  Aborting." << endl;
0168     assert(0);
0169   }
0170   return iter->second;
0171 }
0172 
0173 bool &VariableMapCont::boolValue(std::string key) {
0174   lowercaseString(key);
0175   SBMapIter iter = m_boolMap.find(key);
0176   if (m_boolMap.end() == iter) {
0177     cerr << "VariableMapCont::boolValue() Error: key '" << key << "' not found.  Aborting." << endl;
0178     assert(0);
0179   }
0180   return iter->second;
0181 }
0182 
0183 VariableMapCont::IVec &VariableMapCont::integerVector(std::string key) {
0184   lowercaseString(key);
0185   SIVecMapIter iter = m_integerVecMap.find(key);
0186   if (m_integerVecMap.end() == iter) {
0187     cerr << "VariableMapCont::integerVector() Error: key '" << key << "' not found.  Aborting." << endl;
0188     assert(0);
0189   }
0190   return iter->second;
0191 }
0192 
0193 VariableMapCont::DVec &VariableMapCont::doubleVector(std::string key) {
0194   lowercaseString(key);
0195   SDVecMapIter iter = m_doubleVecMap.find(key);
0196   if (m_doubleVecMap.end() == iter) {
0197     cerr << "VariableMapCont::doubleVector() Error: key '" << key << "' not found.  Aborting." << endl;
0198     assert(0);
0199   }
0200   return iter->second;
0201 }
0202 
0203 VariableMapCont::SVec &VariableMapCont::stringVector(std::string key) {
0204   lowercaseString(key);
0205   SSVecMapIter iter = m_stringVecMap.find(key);
0206   if (m_stringVecMap.end() == iter) {
0207     cerr << "VariableMapCont::stringVector() Error: key '" << key << "' not found.  Aborting." << endl;
0208     assert(0);
0209   }
0210   return iter->second;
0211 }
0212 
0213 bool VariableMapCont::_valueHasBeenModified(const string &key) {
0214   SBMapConstIter iter = m_variableModifiedMap.find(key);
0215   if (m_variableModifiedMap.end() == iter) {
0216     // Not found.  Not a valid option
0217     cerr << "VariableMapCont::valueHasBeenModfied () Error: '" << key << "' is not a valid key." << endl;
0218     return false;
0219   }
0220   return iter->second;
0221 }
0222 
0223 // friends
0224 ostream &operator<<(ostream &o_stream, const VariableMapCont &rhs) { return o_stream; }