CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MakeParameterSets.cc
Go to the documentation of this file.
3 
7 
8 using namespace boost::python;
9 
10 static
11 void
12 makePSetsFromFile(std::string const& fileName, boost::python::object& mainNamespace) {
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
30 makePSetsFromString(std::string const& module, boost::python::object& mainNamespace) {
31  std::string command = module;
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  boost::shared_ptr<ParameterSet>
42  readConfig(std::string const& config) {
43  PythonProcessDesc pythonProcessDesc(config);
44  return pythonProcessDesc.parameterSet();
45  }
46 
47  boost::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  boost::shared_ptr<ParameterSet>& main) {
56  PythonProcessDesc pythonProcessDesc(configtext);
57  main = pythonProcessDesc.parameterSet();
58  }
59 
60  boost::shared_ptr<ParameterSet>
61  readPSetsFrom(std::string const& module) {
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  boost::shared_ptr<ParameterSet> returnValue(new ParameterSet);
83  theProcessPSet.pset().swap(*returnValue);
84  return returnValue;
85  }
86 } // namespace edm
void pythonToCppException(const std::string &iType)
Definition: PythonManager.cc:5
static void makePSetsFromString(std::string const &module, boost::python::object &mainNamespace)
int main(int argc, char **argv)
void swap(ParameterSet &other)
boost::shared_ptr< edm::ParameterSet > parameterSet()
boost::shared_ptr< ParameterSet > readConfig(std::string const &config)
boost::shared_ptr< ParameterSet > readPSetsFrom(std::string const &fileOrString)
edm::ParameterSet & pset()
tuple argc
Definition: dir2webdir.py:41
list object
Definition: dbtoconf.py:77
void initializeModule()
Definition: vlib.h:209
void makeParameterSets(std::string const &configtext, boost::shared_ptr< ParameterSet > &main)
essentially the same as the previous method
static void makePSetsFromFile(std::string const &fileName, boost::python::object &mainNamespace)