CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/GeneratorInterface/Core/src/ParameterCollector.cc

Go to the documentation of this file.
00001 #include <ostream>
00002 #include <cstdlib>
00003 #include <vector>
00004 #include <string>
00005 #include <map>
00006 
00007 #include "FWCore/Utilities/interface/EDMException.h"
00008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00009 
00010 #include "GeneratorInterface/Core/interface/ParameterCollector.h"
00011 
00012 using namespace gen;
00013 
00014 ParameterCollector::ParameterCollector()
00015 {
00016 }
00017 
00018 ParameterCollector::ParameterCollector(const edm::ParameterSet &pset)
00019 {
00020    std::vector<std::string> names =
00021                 pset.getParameterNamesForType<std::vector<std::string> >();
00022 
00023    for(std::vector<std::string>::const_iterator it = names.begin();
00024        it != names.end(); ++it)
00025       contents_[*it] = pset.getParameter<std::vector<std::string> >(*it);
00026 }
00027 
00028 ParameterCollector::~ParameterCollector()
00029 {
00030 }
00031 
00032 inline ParameterCollector::const_iterator::const_iterator(
00033                         const ParameterCollector *collector,
00034                         std::vector<std::string>::const_iterator begin,
00035                         std::vector<std::string>::const_iterator end,
00036                         bool special, std::ostream *dump)
00037    : collector_(collector), dump_(dump), special_(special)
00038 {
00039    if (begin != end)
00040       iter_.push_back(IterPair(begin, end));
00041 
00042    next();
00043 }
00044 
00045 void ParameterCollector::const_iterator::increment()
00046 {
00047    if (++iter_.back().first == iter_.back().second)
00048       iter_.pop_back();
00049 
00050    next();
00051 }
00052 
00053 void ParameterCollector::const_iterator::next()
00054 {
00055    if (iter_.empty()) {
00056       cache_.clear();
00057       return;
00058    }
00059 
00060    for(;;) {
00061       const std::string &line = *iter_.back().first;
00062 
00063       bool special = special_ && iter_.size() == 1;
00064       if ( (!line.empty() && line[0] == '+') || special) {
00065          if (++iter_.back().first == iter_.back().second) {
00066             iter_.pop_back();
00067             if (iter_.empty())
00068               special_ = false;
00069          }
00070 
00071          std::string block = special ? line : line.substr(1);
00072 
00073          std::map<std::string, std::vector<std::string> >::const_iterator
00074                                         pos = collector_->contents_.find(block);
00075            if (pos == collector_->contents_.end())
00076               throw edm::Exception(edm::errors::Configuration)
00077                  << "ParameterCollector could not find configuration lines "
00078                     "block \"" << block << "\", included via plus sign.";
00079 
00080            if (dump_)
00081               *dump_ << "\n####### " << block << " #######" << std::endl;
00082 
00083            if (!pos->second.empty())
00084               iter_.push_back(IterPair(pos->second.begin(),
00085                                        pos->second.end()));
00086       } else {
00087          cache_ = collector_->resolve(line);
00088          if (dump_)
00089             *dump_ << cache_ << std::endl;
00090          break;
00091       }
00092    }
00093 }
00094 
00095 ParameterCollector::const_iterator ParameterCollector::begin() const
00096 {
00097    std::map<std::string, std::vector<std::string> >::const_iterator
00098                                         pos = contents_.find("parameterSets");
00099    if (pos == contents_.end())
00100       throw edm::Exception(edm::errors::Configuration)
00101          << "ParameterCollector could not find \"parameterSets\" block.";
00102 
00103    return const_iterator(this, pos->second.begin(), pos->second.end(), true);
00104 }
00105 
00106 ParameterCollector::const_iterator ParameterCollector::begin(std::ostream &dump) const
00107 {
00108    std::map<std::string, std::vector<std::string> >::const_iterator
00109                                         pos = contents_.find("parameterSets");
00110    if (pos == contents_.end())
00111       throw edm::Exception(edm::errors::Configuration)
00112          << "ParameterCollector could not find \"parameterSets\" block.";
00113 
00114    return const_iterator(this, pos->second.begin(), pos->second.end(),
00115                          true, &dump);
00116 }
00117 
00118 ParameterCollector::const_iterator ParameterCollector::begin(const std::string &block) const
00119 {
00120    std::map<std::string, std::vector<std::string> >::const_iterator
00121                                                 pos = contents_.find(block);
00122    if (pos == contents_.end())
00123       throw edm::Exception(edm::errors::Configuration)
00124          << "ParameterCollector could not find \"" << block << "\" block.";
00125 
00126    return const_iterator(this, pos->second.begin(), pos->second.end());
00127 }
00128 
00129 ParameterCollector::const_iterator ParameterCollector::begin(const std::string &block, std::ostream &dump) const
00130 {
00131    std::map<std::string, std::vector<std::string> >::const_iterator
00132                                                 pos = contents_.find(block);
00133    if (pos == contents_.end())
00134       throw edm::Exception(edm::errors::Configuration)
00135          << "ParameterCollector could not find \"" << block << "\" block.";
00136 
00137    dump << "\n####### " << block << " #######" << std::endl;
00138 
00139    return const_iterator(this, pos->second.begin(), pos->second.end(),
00140                          false, &dump);
00141 }
00142 
00143 std::string ParameterCollector::resolve(const std::string &line)
00144 {
00145    std::string result(line);
00146 
00147    for(;;) {
00148       std::string::size_type pos = result.find("${");
00149       if (pos == std::string::npos)
00150          break;
00151 
00152       std::string::size_type endpos = result.find('}', pos);
00153       if (endpos == std::string::npos)
00154          break;
00155       else
00156          ++endpos;
00157 
00158       std::string var = result.substr(pos + 2, endpos - pos - 3);
00159       const char *path = std::getenv(var.c_str());
00160 
00161       result.replace(pos, endpos - pos, path ? path : "");
00162    }
00163 
00164    return result;
00165 }