CMS 3D CMS Logo

MakePyBind11ParameterSets.cc
Go to the documentation of this file.
3 
7 #include <pybind11/embed.h>
8 
9 static void makePSetsFromFile(std::string const& fileName) {
10  std::string initCommand(
11  "from FWCore.ParameterSet.Types import makeCppPSet\n"
12  "exec(open('");
13  initCommand += fileName + "').read())";
14  pybind11::exec(initCommand);
15  pybind11::exec("makeCppPSet(locals(), topPSet)");
16 }
17 
18 static void makePSetsFromString(std::string const& module) {
19  std::string command = module;
20  command += "\nfrom FWCore.ParameterSet.Types import makeCppPSet\nmakeCppPSet(locals(), topPSet)";
21  pybind11::exec(command);
22 }
23 
24 namespace edm {
25  namespace cmspybind11 {
26  std::unique_ptr<ParameterSet> readConfig(std::string const& config) {
27  PyBind11ProcessDesc pythonProcessDesc(config);
28  return pythonProcessDesc.parameterSet();
29  }
30 
31  std::unique_ptr<ParameterSet> readConfig(std::string const& config, int argc, char* argv[]) {
32  PyBind11ProcessDesc pythonProcessDesc(config, argc, argv);
33  return pythonProcessDesc.parameterSet();
34  }
35 
36  void makeParameterSets(std::string const& configtext, std::unique_ptr<ParameterSet>& main) {
37  PyBind11ProcessDesc pythonProcessDesc(configtext);
38  main = pythonProcessDesc.parameterSet();
39  }
40 
41  std::unique_ptr<ParameterSet> readPSetsFrom(std::string const& module) {
42  pybind11::scoped_interpreter guard{};
44  std::unique_ptr<ParameterSet> retVal;
45  {
46  Python11ParameterSet theProcessPSet;
47  pybind11::object mainModule = pybind11::module::import("__main__");
48  mainModule.attr("topPSet") = pybind11::cast(&theProcessPSet);
49 
50  try {
51  // if it ends with py, it's a file
52  if (module.substr(module.size() - 3) == ".py") {
53  makePSetsFromFile(module);
54  } else {
55  makePSetsFromString(module);
56  }
57  } catch (pybind11::error_already_set const& e) {
58  pythonToCppException("Configuration", e.what());
59  }
60  retVal = std::make_unique<edm::ParameterSet>(ParameterSet(theProcessPSet.pset()));
61  }
62  return retVal;
63  }
64  } // namespace cmspybind11
65 } // namespace edm
edm::python::initializePyBind11Module
void initializePyBind11Module()
Definition: initializePyBind11Module.cc:14
cmsBatch.argv
argv
Definition: cmsBatch.py:279
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
Python11ParameterSet.h
makePSetsFromString
static void makePSetsFromString(std::string const &module)
Definition: MakePyBind11ParameterSets.cc:18
edm::pythonToCppException
void pythonToCppException(const std::string &iType, const std::string &error)
Definition: PyBind11Wrapper.cc:6
dir2webdir.argc
argc
Definition: dir2webdir.py:39
edm::cmspybind11::makeParameterSets
void makeParameterSets(std::string const &configtext, std::unique_ptr< ParameterSet > &main)
essentially the same as the previous method
Definition: MakePyBind11ParameterSets.cc:36
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::cmspybind11::readPSetsFrom
std::unique_ptr< ParameterSet > readPSetsFrom(std::string const &fileOrString)
Definition: MakePyBind11ParameterSets.cc:41
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
Python11ParameterSet
Definition: Python11ParameterSet.h:19
mps_check.command
list command
Definition: mps_check.py:25
config
Definition: config.py:1
Python11ParameterSet::pset
edm::ParameterSet & pset()
Definition: Python11ParameterSet.h:111
PyBind11ProcessDesc.h
initializePyBind11Module.h
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
PyBind11ProcessDesc
Definition: PyBind11ProcessDesc.h:16
edm::ParameterSet
Definition: ParameterSet.h:47
PyBind11ProcessDesc::parameterSet
std::unique_ptr< edm::ParameterSet > parameterSet() const
Definition: PyBind11ProcessDesc.cc:92
main
Definition: main.py:1
makePSetsFromFile
static void makePSetsFromFile(std::string const &fileName)
Definition: MakePyBind11ParameterSets.cc:9
MakePyBind11ParameterSets.h
cond::persistency::import
cond::Hash import(Session &source, const cond::Hash &sourcePayloadId, const std::string &inputTypeName, const void *inputPtr, Session &destination)
Definition: CondDBImport.cc:39
ParameterSet.h
edm::cmspybind11::readConfig
std::unique_ptr< ParameterSet > readConfig(std::string const &config)
Definition: MakePyBind11ParameterSets.cc:26
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37