CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/FWCore/ParameterSet/src/ParameterSetConverter.cc

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------
00002 
00003 ParameterSetConverter.cc
00004 
00005 ----------------------------------------------------------------------*/
00006   //------------------------------------------------------------
00007   // Adapts parameter sets by value (fileFormatVersion_.value() < 12) to parameter sets by reference
00008   // Adapts untracked @trigger_paths (fileFormatVersion_.value() < 13) to tracked @trigger_paths.
00009 
00010 #include "FWCore/ParameterSet/interface/ParameterSetConverter.h"
00011 #include <iterator>
00012 #include "FWCore/ParameterSet/interface/Registry.h"
00013 #include "FWCore/ParameterSet/interface/split.h"
00014 #include "FWCore/Utilities/interface/Algorithms.h"
00015 
00016 namespace edm {
00017 
00018   namespace {
00019     void
00020     insertIntoReplace(ParameterSetConverter::StringMap& replace,
00021                 std::string const& fromPrefix,
00022                 std::string const& from,
00023                 std::string const& fromPostfix,
00024                 std::string const& toPrefix,
00025                 std::string const& to,
00026                 std::string const& toPostfix) {
00027       replace.insert(std::make_pair(fromPrefix+from+fromPostfix, toPrefix+to+toPostfix));
00028     }
00029   }
00030 
00031   MainParameterSet::MainParameterSet(ParameterSetID const& oldID, std::string const& psetString) :
00032       oldID_(oldID),
00033       parameterSet_(psetString),
00034       paths_(parameterSet_.getParameter<StringVector>("@paths")),
00035       endPaths_(),
00036       triggerPaths_() {
00037     if (parameterSet_.existsAs<StringVector>("@end_paths")) {
00038       endPaths_ = (parameterSet_.getParameter<StringVector>("@end_paths"));
00039     }
00040     for (StringVector::const_iterator i = paths_.begin(), e = paths_.end(); i != e; ++i) {
00041       if (!search_all(endPaths_, *i)) {
00042         triggerPaths_.insert(*i);
00043       }
00044     }
00045   }
00046 
00047   MainParameterSet::~MainParameterSet() {}
00048 
00049   TriggerPath::TriggerPath(ParameterSet const& pset) :
00050       parameterSet_(pset),
00051       tPaths_(parameterSet_.getParameter<StringVector>("@trigger_paths")),
00052       triggerPaths_() {
00053     for (StringVector::const_iterator i = tPaths_.begin(), e = tPaths_.end(); i != e; ++i) {
00054       triggerPaths_.insert(*i);
00055     }
00056   }
00057 
00058   TriggerPath::~TriggerPath() {}
00059 
00060   //------------------------------------------------------------
00061 
00062   ParameterSetConverter::ParameterSetConverter(ParameterSetMap const& psetMap,
00063                                                 ParameterSetIdConverter& idConverter,
00064                                                 bool alreadyByReference) :
00065     parameterSets_(),
00066     mainParameterSets_(),
00067     triggerPaths_(),
00068     replace_(),
00069     parameterSetIdConverter_(idConverter)
00070   {
00071     for (ParameterSetMap::const_iterator i = psetMap.begin(), iEnd = psetMap.end(); i != iEnd; ++i) {
00072       parameterSets_.push_back(std::make_pair(i->second.pset(), i->first));
00073     }
00074     if (alreadyByReference) {
00075       noConvertParameterSets();
00076     } else {
00077       replace_.insert(std::make_pair(std::string("=+p({})"), std::string("=+q({})")));
00078       convertParameterSets();
00079     }
00080     for (std::vector<MainParameterSet>::iterator j = mainParameterSets_.begin(), jEnd = mainParameterSets_.end(); j != jEnd; ++j) {
00081       for (std::vector<TriggerPath>::iterator i = triggerPaths_.begin(), iEnd = triggerPaths_.end(); i != iEnd; ++i) {
00082         if (i->triggerPaths_ == j->triggerPaths_) {
00083           j->parameterSet_.addParameter("@trigger_paths", i->parameterSet_);
00084           break;
00085         }
00086       }
00087     }
00088     for (std::vector<MainParameterSet>::iterator i = mainParameterSets_.begin(), iEnd = mainParameterSets_.end(); i != iEnd; ++i) {
00089       ParameterSet& pset = i->parameterSet_;
00090       pset.registerIt();
00091       ParameterSetID newID(pset.id());
00092       if (i->oldID_ != newID && i->oldID_ != ParameterSetID()) {
00093         parameterSetIdConverter_.insert(std::make_pair(i->oldID_, newID));
00094       }
00095     }
00096   }
00097 
00098   ParameterSetConverter::~ParameterSetConverter() {}
00099 
00100   void
00101   ParameterSetConverter::noConvertParameterSets() {
00102     for (StringWithIDList::iterator i = parameterSets_.begin(), iEnd = parameterSets_.end(); i != iEnd; ++i) {
00103       if (i->first.find("@all_sources") != std::string::npos) {
00104         mainParameterSets_.push_back(MainParameterSet(i->second, i->first));
00105       } else {
00106         ParameterSet pset(i->first);
00107         pset.setID(i->second);
00108         pset::Registry::instance()->insertMapped(pset);
00109         if (i->first.find("@trigger_paths") != std::string::npos) {
00110           triggerPaths_.push_back(pset);
00111         }
00112       } 
00113     }
00114   }
00115 
00116   void
00117   ParameterSetConverter::convertParameterSets() {
00118     std::string const comma(",");
00119     std::string const rparam(")");
00120     std::string const rvparam("})");
00121     std::string const loldparam("=+P(");
00122     std::string const loldvparam("=+p({");
00123     std::string const lparam("=+Q(");
00124     std::string const lvparam("=+q({");
00125     bool doItAgain = false;
00126     for (StringMap::const_iterator j = replace_.begin(), jEnd = replace_.end(); j != jEnd; ++j) {
00127       for (StringWithIDList::iterator i = parameterSets_.begin(), iEnd = parameterSets_.end(); i != iEnd; ++i) {
00128         for (std::string::size_type it = i->first.find(j->first); it != std::string::npos; it = i->first.find(j->first)) {
00129           i->first.replace(it, j->first.size(), j->second);
00130           doItAgain = true;
00131         }
00132       }
00133     }
00134     for (StringWithIDList::iterator i = parameterSets_.begin(), iEnd = parameterSets_.end(); i != iEnd; ++i) {
00135       if (i->first.find("+P") == std::string::npos && i->first.find("+p") == std::string::npos) {
00136         if (i->first.find("@all_sources") != std::string::npos) {
00137           mainParameterSets_.push_back(MainParameterSet(i->second, i->first));
00138         } else {
00139           ParameterSet pset(i->first);
00140           pset.registerIt();
00141           std::string& from = i->first;
00142           std::string to;
00143           ParameterSetID newID(pset.id());
00144           newID.toString(to);
00145           insertIntoReplace(replace_, loldparam, from, rparam, lparam, to, rparam);
00146           insertIntoReplace(replace_, comma, from, comma, comma, to, comma);
00147           insertIntoReplace(replace_, comma, from, rvparam, comma, to, rvparam);
00148           insertIntoReplace(replace_, loldvparam, from, comma, lvparam, to, comma);
00149           insertIntoReplace(replace_, loldvparam, from, rvparam, lvparam, to, rvparam);
00150           if (i->second != newID && i->second != ParameterSetID()) {
00151             parameterSetIdConverter_.insert(std::make_pair(i->second, newID));
00152           }
00153           if (i->first.find("@trigger_paths") != std::string::npos) {
00154             triggerPaths_.push_back(pset);
00155           }
00156         }
00157         StringWithIDList::iterator icopy = i;
00158         ++i;
00159         parameterSets_.erase(icopy);
00160         --i;
00161         doItAgain = true;
00162       }
00163     }
00164     if (!doItAgain && !parameterSets_.empty()) {
00165       for (StringWithIDList::iterator i = parameterSets_.begin(), iEnd = parameterSets_.end(); i != iEnd; ++i) {
00166         std::list<std::string> pieces;
00167         split(std::back_inserter(pieces), i->first, '<', ';', '>');
00168         for (std::list<std::string>::iterator i= pieces.begin(), e = pieces.end(); i != e; ++i) {
00169           std::string removeName = i->substr(i->find('+'));
00170           if (removeName.size() >= 4) {
00171             if (removeName[1] == 'P') {
00172               std::string psetString(removeName.begin()+3, removeName.end()-1);
00173               parameterSets_.push_back(std::make_pair(psetString, ParameterSetID()));
00174               doItAgain = true;
00175             } else if (removeName[1] == 'p') {
00176               std::string pvec = std::string(removeName.begin()+3, removeName.end()-1);
00177               StringList temp;
00178               split(std::back_inserter(temp), pvec, '{', ',', '}');
00179               for (StringList::const_iterator j = temp.begin(), f = temp.end(); j != f; ++j) {
00180                 parameterSets_.push_back(std::make_pair(*j, ParameterSetID()));
00181               }
00182               doItAgain = true;
00183             }
00184           }     
00185         }
00186       }
00187     }
00188     if (doItAgain) {
00189       convertParameterSets();
00190     }
00191   }  
00192 }