CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/PhysicsTools/FWLite/src/VariableMapCont.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 #include <iostream>
00003 #include <fstream>
00004 #include <iomanip>
00005 #include <cassert>
00006 #include <algorithm>
00007 #include "PhysicsTools/FWLite/interface/VariableMapCont.h"
00008 
00009 using namespace std;
00010 using namespace optutl;
00011 
00012 const int                          VariableMapCont::kDefaultInteger = 0;
00013 const double                       VariableMapCont::kDefaultDouble  = 0.;
00014 const std::string                  VariableMapCont::kDefaultString  = "";
00015 const bool                         VariableMapCont::kDefaultBool    = false;
00016 const VariableMapCont::IVec        VariableMapCont::kEmptyIVec;
00017 const VariableMapCont::DVec        VariableMapCont::kEmptyDVec;
00018 const VariableMapCont::SVec        VariableMapCont::kEmptySVec;
00019 
00020 
00021 VariableMapCont::VariableMapCont()
00022 {
00023 }
00024 
00025 VariableMapCont::OptionType
00026 VariableMapCont::hasVariable (string key)
00027 {
00028    lowercaseString (key); 
00029    // Look through our maps to see if we've got it
00030    if (m_integerMap.end()    != m_integerMap.find (key))    return kInteger;
00031    if (m_doubleMap.end()     != m_doubleMap.find (key))     return kDouble;
00032    if (m_stringMap.end()     != m_stringMap.find (key))     return kString;
00033    if (m_boolMap.end()       != m_boolMap.find (key))       return kBool;
00034    if (m_integerVecMap.end() != m_integerVecMap.find (key)) return kIntegerVector;
00035    if (m_doubleVecMap.end()  != m_doubleVecMap.find (key))  return kDoubleVector;
00036    if (m_stringVecMap.end()  != m_stringVecMap.find (key))  return kStringVector;
00037    // if we're here, the answer's no.
00038    return kNone;
00039 }
00040 
00041 void 
00042 VariableMapCont::lowercaseString(string& arg)
00043 {
00044    // assumes 'toLower(ch)' modifies ch
00045    std::for_each (arg.begin(), arg.end(), VariableMapCont::toLower);
00046    // // assumes 'toLower(ch)' returns the lower case char
00047    // std::transform (arg.begin(), arg.end(), arg.begin(), 
00048    //                 VariableMapCont::toLower);
00049 }
00050 
00051 char 
00052 VariableMapCont::toLower (char& ch)
00053 {
00054    ch = tolower (ch);
00055    return ch;
00056 }
00057 
00058 void
00059 VariableMapCont::_checkKey (string &key, const string &description)
00060 {
00061    // Let's make sure we don't already have this key
00062    lowercaseString (key);
00063    if ( m_variableModifiedMap.end() != m_variableModifiedMap.find (key) )
00064    {
00065       cerr << "VariableMapCont::addVariable() Error: Key '" << key 
00066            << "' has already been defined.  Aborting." << endl;
00067       assert (0);
00068    } // found a duplicate
00069    m_variableModifiedMap[key]      = false;
00070    m_variableDescriptionMap[key]  = description;
00071 }
00072 
00073 void
00074 VariableMapCont::addOption (string key, OptionType type,
00075                         const string &description)
00076 {
00077    _checkKey (key, description);
00078    if (kInteger    == type)
00079    {
00080       m_integerMap[key]    = kDefaultInteger;
00081       return;
00082    } 
00083    if (kDouble     == type)
00084    {
00085       m_doubleMap[key]    = kDefaultDouble;
00086       return;
00087    } 
00088    if (kString     == type)
00089    {
00090       m_stringMap[key]    = kDefaultString;
00091       return;
00092    } 
00093    if (kBool       == type)
00094    {
00095       m_boolMap[key]    = kDefaultBool;
00096       return;
00097    }
00098    if (kIntegerVector == type)
00099    {
00100       m_integerVecMap[key] = kEmptyIVec;
00101       return;
00102    } 
00103    if (kDoubleVector == type)
00104    {
00105       m_doubleVecMap[key] = kEmptyDVec;
00106       return;
00107    } 
00108    if (kStringVector == type)
00109    {
00110       m_stringVecMap[key] = kEmptySVec;
00111       return;
00112    } 
00113 }
00114 
00115 void
00116 VariableMapCont::addOption (string key, OptionType type,
00117                         const string &description, int defaultValue)
00118 {
00119    _checkKey (key, description);
00120    if (kInteger != type)
00121    {
00122       cerr << "VariableMapCont::addOption() Error: Key '" << key 
00123            << "' is not defined as an integer but has an integer "
00124            << "default value. Aborting." << endl;
00125       assert (0);      
00126    }
00127    m_integerMap[key] = defaultValue;
00128 }
00129 
00130 void
00131 VariableMapCont::addOption (string key, OptionType type,
00132                         const string &description, double defaultValue)
00133 {
00134    _checkKey (key, description);
00135    if (kDouble != type)
00136    {
00137       cerr << "VariableMapCont::addOption() Error: Key '" << key 
00138            << "' is not defined as an double but has an double "
00139            << "default value. Aborting." << endl;
00140       assert (0);      
00141    }
00142    m_doubleMap[key] = defaultValue;
00143 }
00144 
00145 void
00146 VariableMapCont::addOption (string key, OptionType type,
00147                         const string &description, 
00148                         const string &defaultValue)
00149 {
00150    _checkKey (key, description);
00151    if (kString != type)
00152    {
00153       cerr << "VariableMapCont::addOption() Error: Key '" << key 
00154            << "' is not defined as an string but has an string "
00155            << "default value. Aborting." << endl;
00156       assert (0);      
00157    }
00158    m_stringMap[key] = defaultValue;
00159 }
00160 
00161 void
00162 VariableMapCont::addOption (string key, OptionType type,
00163                         const string &description, 
00164                         const char* defaultValue)
00165 {
00166    addOption (key, type, description, (string) defaultValue);
00167 }
00168 
00169 void
00170 VariableMapCont::addOption (string key, OptionType type,
00171                         const string &description, bool defaultValue)
00172 {
00173    _checkKey (key, description);
00174    if (kBool != type)
00175    {
00176       cerr << "VariableMapCont::addOption() Error: Key '" << key 
00177            << "' is not defined as an bool but has an bool "
00178            << "default value. Aborting." << endl;
00179       assert (0);      
00180    }
00181    m_boolMap[key] = defaultValue;
00182 }
00183 
00184 
00185 int &
00186 VariableMapCont::integerValue (std::string key)
00187 {
00188    lowercaseString (key);
00189    SIMapIter iter = m_integerMap.find (key);
00190    if (m_integerMap.end() == iter)
00191    {
00192       cerr << "VariableMapCont::integerValue() Error: key '"
00193            << key << "' not found.  Aborting." << endl;
00194       assert (0);
00195    }
00196    return iter->second;
00197 }
00198 
00199 double &
00200 VariableMapCont::doubleValue (std::string key)
00201 {
00202    lowercaseString (key);
00203    SDMapIter iter = m_doubleMap.find (key);
00204    if (m_doubleMap.end() == iter)
00205    {
00206       cerr << "VariableMapCont::doubleValue() Error: key '"
00207            << key << "' not found.  Aborting." << endl;
00208       assert (0);
00209    }
00210    return iter->second;
00211 }
00212 
00213 string &
00214 VariableMapCont::stringValue (std::string key)
00215 {
00216    lowercaseString (key);
00217    SSMapIter iter = m_stringMap.find (key);
00218    if (m_stringMap.end() == iter)
00219    {
00220       cerr << "VariableMapCont::stringValue() Error: key '"
00221            << key << "' not found.  Aborting." << endl;
00222       assert (0);
00223    }
00224    return iter->second;
00225 }
00226 
00227 bool &
00228 VariableMapCont::boolValue (std::string key)
00229 {
00230    lowercaseString (key);
00231    SBMapIter iter = m_boolMap.find (key);
00232    if (m_boolMap.end() == iter)
00233    {
00234       cerr << "VariableMapCont::boolValue() Error: key '"
00235            << key << "' not found.  Aborting." << endl;
00236       assert (0);
00237    }
00238    return iter->second;
00239 }
00240 
00241 VariableMapCont::IVec &
00242 VariableMapCont::integerVector (std::string key)
00243 {
00244    lowercaseString (key);
00245    SIVecMapIter iter = m_integerVecMap.find (key);
00246    if (m_integerVecMap.end() == iter)
00247    {
00248       cerr << "VariableMapCont::integerVector() Error: key '"
00249            << key << "' not found.  Aborting." << endl;
00250       assert (0);
00251    }
00252    return iter->second;
00253 }
00254 
00255 VariableMapCont::DVec &
00256 VariableMapCont::doubleVector (std::string key)
00257 {
00258    lowercaseString (key);
00259    SDVecMapIter iter = m_doubleVecMap.find (key);
00260    if (m_doubleVecMap.end() == iter)
00261    {
00262       cerr << "VariableMapCont::doubleVector() Error: key '"
00263            << key << "' not found.  Aborting." << endl;
00264       assert (0);
00265    }
00266    return iter->second;
00267 }
00268 
00269 VariableMapCont::SVec &
00270 VariableMapCont::stringVector (std::string key)
00271 {
00272    lowercaseString (key);
00273    SSVecMapIter iter = m_stringVecMap.find (key);
00274    if (m_stringVecMap.end() == iter)
00275    {
00276       cerr << "VariableMapCont::stringVector() Error: key '"
00277            << key << "' not found.  Aborting." << endl;
00278       assert (0);
00279    }
00280    return iter->second;
00281 }
00282 
00283 bool
00284 VariableMapCont::_valueHasBeenModified (const string &key)
00285 {
00286    SBMapConstIter iter = m_variableModifiedMap.find (key);
00287    if (m_variableModifiedMap.end() == iter)
00288    {
00289       // Not found.  Not a valid option
00290       cerr << "VariableMapCont::valueHasBeenModfied () Error: '" 
00291            << key << "' is not a valid key." << endl;
00292       return false;
00293    }
00294    return iter->second;
00295 }
00296 
00297 // friends
00298 ostream& operator<< (ostream& o_stream, const VariableMapCont &rhs)
00299 {
00300    return o_stream;
00301 }