Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*----------------------------------------------------------------------
0002 
0003 ParameterSetConverter.cc
0004 
0005 ----------------------------------------------------------------------*/
0006 //------------------------------------------------------------
0007 // Adapts parameter sets by value (fileFormatVersion_.value() < 12) to parameter sets by reference
0008 // Adapts untracked @trigger_paths (fileFormatVersion_.value() < 13) to tracked @trigger_paths.
0009 
0010 #include "FWCore/ParameterSet/interface/ParameterSetConverter.h"
0011 #include <iterator>
0012 #include "FWCore/ParameterSet/interface/Registry.h"
0013 #include "FWCore/ParameterSet/src/split.h"
0014 #include "FWCore/Utilities/interface/Algorithms.h"
0015 
0016 namespace edm {
0017 
0018   namespace {
0019     void insertIntoReplace(ParameterSetConverter::StringMap& replace,
0020                            std::string const& fromPrefix,
0021                            std::string const& from,
0022                            std::string const& fromPostfix,
0023                            std::string const& toPrefix,
0024                            std::string const& to,
0025                            std::string const& toPostfix) {
0026       replace.insert(std::make_pair(fromPrefix + from + fromPostfix, toPrefix + to + toPostfix));
0027     }
0028   }  // namespace
0029 
0030   MainParameterSet::MainParameterSet(ParameterSetID const& oldID, std::string const& psetString)
0031       : oldID_(oldID),
0032         parameterSet_(psetString),
0033         paths_(parameterSet_.getParameter<StringVector>("@paths")),
0034         endPaths_(),
0035         triggerPaths_() {
0036     if (parameterSet_.existsAs<StringVector>("@end_paths")) {
0037       endPaths_ = (parameterSet_.getParameter<StringVector>("@end_paths"));
0038     }
0039     for (StringVector::const_iterator i = paths_.begin(), e = paths_.end(); i != e; ++i) {
0040       if (!search_all(endPaths_, *i)) {
0041         triggerPaths_.insert(*i);
0042       }
0043     }
0044   }
0045 
0046   MainParameterSet::~MainParameterSet() {}
0047 
0048   TriggerPath::TriggerPath(ParameterSet const& pset)
0049       : parameterSet_(pset), tPaths_(parameterSet_.getParameter<StringVector>("@trigger_paths")), triggerPaths_() {
0050     for (StringVector::const_iterator i = tPaths_.begin(), e = tPaths_.end(); i != e; ++i) {
0051       triggerPaths_.insert(*i);
0052     }
0053   }
0054 
0055   TriggerPath::~TriggerPath() {}
0056 
0057   //------------------------------------------------------------
0058 
0059   ParameterSetConverter::ParameterSetConverter(ParameterSetMap const& psetMap,
0060                                                ParameterSetIdConverter& idConverter,
0061                                                bool alreadyByReference)
0062       : parameterSets_(), mainParameterSets_(), triggerPaths_(), replace_(), parameterSetIdConverter_(idConverter) {
0063     for (ParameterSetMap::const_iterator i = psetMap.begin(), iEnd = psetMap.end(); i != iEnd; ++i) {
0064       parameterSets_.push_back(std::make_pair(i->second.pset(), i->first));
0065     }
0066     if (alreadyByReference) {
0067       noConvertParameterSets();
0068     } else {
0069       replace_.insert(std::make_pair(std::string("=+p({})"), std::string("=+q({})")));
0070       convertParameterSets();
0071     }
0072     for (std::vector<MainParameterSet>::iterator j = mainParameterSets_.begin(), jEnd = mainParameterSets_.end();
0073          j != jEnd;
0074          ++j) {
0075       for (std::vector<TriggerPath>::iterator i = triggerPaths_.begin(), iEnd = triggerPaths_.end(); i != iEnd; ++i) {
0076         if (i->triggerPaths_ == j->triggerPaths_) {
0077           j->parameterSet_.addParameter("@trigger_paths", i->parameterSet_);
0078           break;
0079         }
0080       }
0081     }
0082     for (std::vector<MainParameterSet>::iterator i = mainParameterSets_.begin(), iEnd = mainParameterSets_.end();
0083          i != iEnd;
0084          ++i) {
0085       ParameterSet& pset = i->parameterSet_;
0086       pset.registerIt();
0087       ParameterSetID newID(pset.id());
0088       if (i->oldID_ != newID && i->oldID_ != ParameterSetID()) {
0089         parameterSetIdConverter_.insert(std::make_pair(i->oldID_, newID));
0090       }
0091     }
0092   }
0093 
0094   ParameterSetConverter::~ParameterSetConverter() {}
0095 
0096   void ParameterSetConverter::noConvertParameterSets() {
0097     for (StringWithIDList::iterator i = parameterSets_.begin(), iEnd = parameterSets_.end(); i != iEnd; ++i) {
0098       if (i->first.find("@all_sources") != std::string::npos) {
0099         mainParameterSets_.push_back(MainParameterSet(i->second, i->first));
0100       } else {
0101         ParameterSet pset(i->first);
0102         pset.setID(i->second);
0103         pset::Registry::instance()->insertMapped(pset);
0104         if (i->first.find("@trigger_paths") != std::string::npos) {
0105           triggerPaths_.push_back(pset);
0106         }
0107       }
0108     }
0109   }
0110 
0111   void ParameterSetConverter::convertParameterSets() {
0112     std::string const comma(",");
0113     std::string const rparam(")");
0114     std::string const rvparam("})");
0115     std::string const loldparam("=+P(");
0116     std::string const loldvparam("=+p({");
0117     std::string const lparam("=+Q(");
0118     std::string const lvparam("=+q({");
0119     bool doItAgain = false;
0120     for (StringMap::const_iterator j = replace_.begin(), jEnd = replace_.end(); j != jEnd; ++j) {
0121       for (StringWithIDList::iterator i = parameterSets_.begin(), iEnd = parameterSets_.end(); i != iEnd; ++i) {
0122         for (std::string::size_type it = i->first.find(j->first); it != std::string::npos;
0123              it = i->first.find(j->first)) {
0124           i->first.replace(it, j->first.size(), j->second);
0125           doItAgain = true;
0126         }
0127       }
0128     }
0129     for (StringWithIDList::iterator i = parameterSets_.begin(), iEnd = parameterSets_.end(); i != iEnd; ++i) {
0130       if (i->first.find("+P") == std::string::npos && i->first.find("+p") == std::string::npos) {
0131         if (i->first.find("@all_sources") != std::string::npos) {
0132           mainParameterSets_.push_back(MainParameterSet(i->second, i->first));
0133         } else {
0134           ParameterSet pset(i->first);
0135           pset.registerIt();
0136           std::string& from = i->first;
0137           std::string to;
0138           ParameterSetID newID(pset.id());
0139           newID.toString(to);
0140           insertIntoReplace(replace_, loldparam, from, rparam, lparam, to, rparam);
0141           insertIntoReplace(replace_, comma, from, comma, comma, to, comma);
0142           insertIntoReplace(replace_, comma, from, rvparam, comma, to, rvparam);
0143           insertIntoReplace(replace_, loldvparam, from, comma, lvparam, to, comma);
0144           insertIntoReplace(replace_, loldvparam, from, rvparam, lvparam, to, rvparam);
0145           if (i->second != newID && i->second != ParameterSetID()) {
0146             parameterSetIdConverter_.insert(std::make_pair(i->second, newID));
0147           }
0148           if (i->first.find("@trigger_paths") != std::string::npos) {
0149             triggerPaths_.push_back(pset);
0150           }
0151         }
0152         StringWithIDList::iterator icopy = i;
0153         ++i;
0154         parameterSets_.erase(icopy);
0155         --i;
0156         doItAgain = true;
0157       }
0158     }
0159     if (!doItAgain && !parameterSets_.empty()) {
0160       for (auto const& k : parameterSets_) {
0161         std::vector<std::string_view> pieces;
0162         split(std::back_inserter(pieces), k.first, '<', ';', '>');
0163         for (auto const& i : pieces) {
0164           std::string_view removeName = i.substr(i.find('+'));
0165           if (removeName.size() >= 4) {
0166             if (removeName[1] == 'P') {
0167               std::string psetString(removeName.begin() + 3, removeName.end() - 1);
0168               parameterSets_.emplace_back(std::move(psetString), ParameterSetID());
0169               doItAgain = true;
0170             } else if (removeName[1] == 'p') {
0171               std::string_view pvec = removeName.substr(3, removeName.size() - 4);
0172               std::vector<std::string_view> temp;
0173               split(std::back_inserter(temp), pvec, '{', ',', '}');
0174               for (auto const& j : temp) {
0175                 parameterSets_.emplace_back(j, ParameterSetID());
0176               }
0177               doItAgain = true;
0178             }
0179           }
0180         }
0181       }
0182     }
0183     if (doItAgain) {
0184       convertParameterSets();
0185     }
0186   }
0187 }  // namespace edm