CMS 3D CMS Logo

MakeParameterSets.cc
Go to the documentation of this file.
3 
7 
8 using namespace boost::python;
9 
10 static
11 void
13  std::string initCommand("from FWCore.ParameterSet.Types import makeCppPSet\n"
14  "execfile('");
15  initCommand += fileName + "')";
16 
17  handle<>(PyRun_String(initCommand.c_str(),
18  Py_file_input,
19  mainNamespace.ptr(),
20  mainNamespace.ptr()));
21  std::string command("makeCppPSet(locals(), topPSet)");
22  handle<>(PyRun_String(command.c_str(),
23  Py_eval_input,
24  mainNamespace.ptr(),
25  mainNamespace.ptr()));
26 }
27 
28 static
29 void
32  command += "\nfrom FWCore.ParameterSet.Types import makeCppPSet\nmakeCppPSet(locals(), topPSet)";
33  handle<>(PyRun_String(command.c_str(),
34  Py_file_input,
35  mainNamespace.ptr(),
36  mainNamespace.ptr()));
37 }
38 
39 namespace edm {
40 
41  std::shared_ptr<ParameterSet>
43  PythonProcessDesc pythonProcessDesc(config);
44  return pythonProcessDesc.parameterSet();
45  }
46 
47  std::shared_ptr<ParameterSet>
48  readConfig(std::string const& config, int argc, char* argv[]) {
49  PythonProcessDesc pythonProcessDesc(config, argc, argv);
50  return pythonProcessDesc.parameterSet();
51  }
52 
53  void
54  makeParameterSets(std::string const& configtext,
55  std::shared_ptr<ParameterSet>& main) {
56  PythonProcessDesc pythonProcessDesc(configtext);
57  main = pythonProcessDesc.parameterSet();
58  }
59 
60  std::shared_ptr<ParameterSet>
63 
64  boost::python::object mainModule = object(handle<>(borrowed(PyImport_AddModule(const_cast<char*>("__main__")))));
65 
66  boost::python::object mainNamespace = mainModule.attr("__dict__");
67  PythonParameterSet theProcessPSet;
68  mainNamespace["topPSet"] = ptr(&theProcessPSet);
69 
70  try {
71  // if it ends with py, it's a file
72  if(module.substr(module.size()-3) == ".py") {
73  makePSetsFromFile(module,mainNamespace);
74  } else {
75  makePSetsFromString(module,mainNamespace);
76  }
77  }
78  catch( error_already_set ) {
79  pythonToCppException("Configuration");
80  Py_Finalize();
81  }
82  auto returnValue = std::make_shared<ParameterSet>();
83  theProcessPSet.pset().swap(*returnValue);
84  return returnValue;
85  }
86 } // namespace edm
std::shared_ptr< ParameterSet > readPSetsFrom(std::string const &fileOrString)
static void makePSetsFromString(std::string const &module, boost::python::object &mainNamespace)
void pythonToCppException(const std::string &iType)
Definition: PythonWrapper.cc:6
Definition: config.py:1
void swap(ParameterSet &other)
std::shared_ptr< edm::ParameterSet > parameterSet() const
edm::ParameterSet & pset()
std::shared_ptr< ParameterSet > readConfig(std::string const &config)
list command
Definition: mps_check.py:23
HLT enums.
Definition: main.py:1
void initializeModule()
void makeParameterSets(std::string const &configtext, std::shared_ptr< ParameterSet > &main)
essentially the same as the previous method
Definition: vlib.h:208
static void makePSetsFromFile(std::string const &fileName, boost::python::object &mainNamespace)