~durga/maus/rel709

« back to all changes in this revision

Viewing changes to src/py_cpp/PyGlobals.cc

  • Committer: Durga Rajaram
  • Date: 2013-10-01 00:19:57 UTC
  • mfrom: (659.1.74 rc)
  • Revision ID: durga@fnal.gov-20131001001957-iswih60vis9rodw0
Tags: MAUS-v0.7.1
MAUS-v0.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#include <string>
19
19
 
 
20
#include "src/legacy/Config/MiceModule.hh"
20
21
#include "src/common_cpp/Utils/JsonWrapper.hh"
21
22
#include "src/common_cpp/Utils/Globals.hh"
22
23
#include "src/common_cpp/Globals/GlobalsManager.hh"
23
24
 
 
25
#include "src/py_cpp/PyMiceModule.hh"
 
26
 
24
27
#include "src/py_cpp/PyGlobals.hh"
25
28
 
26
29
namespace MAUS {
27
30
namespace PyGlobals {
28
31
std::string Birth_DocString =
29
 
  std::string("Initialise MAUS globals.\n\n")+
30
 
  std::string("Initialise the MAUS globals such as error handling, C++ ")+
31
 
  std::string("logging, field maps and GEANT4 interfaces. ")+
32
 
  std::string("Takes one argument which should be the json configuration ")+
33
 
  std::string("datacards formatted as a string. Throws an exception if ")+
34
 
  std::string("globals are already initialised.");
 
32
std::string("Initialise MAUS globals.\n\n")+
 
33
std::string("Initialise the MAUS globals such as error handling, C++\n")+
 
34
std::string("logging, field maps and GEANT4 interfaces.\n")+
 
35
std::string("  Takes one argument which should be the json configuration\n")+
 
36
std::string("datacards formatted as a string. Throws an exception if\n")+
 
37
std::string("globals are already initialised.");
35
38
 
36
39
std::string Death_DocString =
37
 
  std::string("Destruct MAUS globals.\n\nIgnores all arguments. ")+
38
 
  std::string("Throws an exception if globals are not initialised already.");
 
40
std::string("Destruct MAUS globals.\n\n  Ignores all arguments.\n")+
 
41
std::string("Throws an exception if globals are not initialised already.\n");
39
42
 
40
43
std::string HasInstance_DocString =
41
 
  std::string("Check if MAUS globals have been initialised. Ignores all ")+
42
 
  std::string("arguments. Returns 1 if globals have been initialised, else 0");
 
44
std::string("Check if MAUS globals have been initialised.\n\n")+
 
45
std::string("  Ignores all arguments.\n")+
 
46
std::string("Returns 1 if globals have been initialised, else 0.");
43
47
 
44
48
std::string GetConfigurationCards_DocString =
45
 
  std::string("Return the configuration cards as a string. Throws an exception if globals have")+
46
 
  std::string(" not been initialised.");
 
49
std::string("Return the configuration cards as a string.\n\n")+
 
50
std::string("Throws an exception if globals have not been initialised.");
 
51
 
 
52
std::string GetMonteCarloMiceModules_DocString =
 
53
std::string("Get the geometry used for simulation.\n\n")+
 
54
std::string("  Ignores all arguments.\n")+
 
55
std::string("Return a deepcopy of the simulation geometry as a MiceModule\n")+
 
56
std::string("object. Note that the actual geometry will only be updated by a\n")+
 
57
std::string("call to set_monte_carlo_mice_modules(...)\n")+
 
58
std::string("Throws an exception if globals have not been initialised.");
 
59
 
 
60
std::string SetMonteCarloMiceModules_DocString =
 
61
std::string("Set the simulation geometry and fields.\n\n")+
 
62
std::string("  - modules (MiceModule) the new geometry object.\n")+
 
63
std::string("Returns None. Throws an exception if globals have not been\n")+
 
64
std::string("initialised.");
47
65
 
48
66
std::string GetVersionNumber_DocString =
49
 
  std::string("Return the MAUS version number as a string like x.y.z.\n\n")+
50
 
  std::string("Return the MAUS version number as a string like x.y.z where.")+
51
 
  std::string("x is the major version number, y is the minor version number,")+
52
 
  std::string("and z is the patch number. Ignores all arguments.");
 
67
std::string("Return the MAUS version number as a string like x.y.z.\n\n")+
 
68
std::string("Return the MAUS version number as a string like x.y.z where\n")+
 
69
std::string("x is the major version number, y is the minor version number,\n")+
 
70
std::string("and z is the patch number. Ignores all arguments.");
53
71
 
54
72
static PyMethodDef methods[] = {
55
73
{"birth", (PyCFunction)Birth,
60
78
    METH_VARARGS, HasInstance_DocString.c_str()},
61
79
{"get_configuration_cards", (PyCFunction)GetConfigurationCards,
62
80
    METH_VARARGS, GetConfigurationCards_DocString.c_str()},
 
81
{"get_monte_carlo_mice_modules", (PyCFunction)GetMonteCarloMiceModules,
 
82
    METH_VARARGS|METH_KEYWORDS, GetMonteCarloMiceModules_DocString.c_str()},
 
83
{"set_monte_carlo_mice_modules", (PyCFunction)SetMonteCarloMiceModules,
 
84
    METH_VARARGS|METH_KEYWORDS, SetMonteCarloMiceModules_DocString.c_str()},
63
85
{"get_version_number", (PyCFunction)GetVersionNumber,
64
86
    METH_VARARGS, GetVersionNumber_DocString.c_str()},
65
87
    {NULL, NULL, 0, NULL}
142
164
    return version_py;
143
165
}
144
166
 
 
167
PyObject* GetMonteCarloMiceModules
 
168
                             (PyObject* dummy, PyObject* args, PyObject *kwds) {
 
169
    if (!Globals::HasInstance()) {
 
170
        PyErr_SetString(PyExc_RuntimeError,
 
171
                  "Attempt to get MC mice modules but globals not birthed");
 
172
        return NULL;
 
173
    }
 
174
    PyObject* py_mod = MAUS::PyMiceModule::create_empty_module();
 
175
    if (py_mod == NULL) {
 
176
        return NULL;
 
177
    }
 
178
    MiceModule* mod_orig = Globals::GetMonteCarloMiceModules();
 
179
    MiceModule* mod = mod_orig->deepCopy(*mod_orig, false);
 
180
    int success = PyMiceModule::set_mice_module(py_mod, mod);
 
181
    if (success == 0) {
 
182
        delete mod;
 
183
        return NULL;
 
184
    }
 
185
    return py_mod;
 
186
}
 
187
 
 
188
PyObject* SetMonteCarloMiceModules
 
189
                             (PyObject* self, PyObject* args, PyObject *kwds) {
 
190
    if (!Globals::HasInstance()) {
 
191
        PyErr_SetString(PyExc_RuntimeError,
 
192
                  "Attempt to set MC mice modules but globals not birthed");
 
193
        return NULL;
 
194
    }
 
195
    PyObject* py_mod = NULL;
 
196
    static char *kwlist[] = {const_cast<char*>("module"), NULL};
 
197
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &py_mod)) {
 
198
        return NULL;
 
199
    }
 
200
 
 
201
    MiceModule* mod = MiceModule::deepCopy
 
202
                               (*PyMiceModule::get_mice_module(py_mod), false);
 
203
    if (mod == NULL) {
 
204
        return NULL;
 
205
    }
 
206
    try {
 
207
        GlobalsManager::SetMonteCarloMiceModules(mod);
 
208
    } catch(std::exception& exc) {
 
209
        std::string message = std::string("Failed to set MiceModules\n")+
 
210
                              std::string((&exc)->what());
 
211
        PyErr_SetString(PyExc_ValueError, message.c_str());
 
212
        return NULL;
 
213
    }
 
214
    Py_INCREF(Py_None);
 
215
    return Py_None;
 
216
}
 
217
 
145
218
PyMODINIT_FUNC initglobals(void) {
146
219
  Py_Initialize();
147
220
  PyObject* maus_module = Py_InitModule("globals", methods);
148
221
  if (maus_module == NULL) return;
 
222
  MAUS::PyMiceModule::import_PyMiceModule();
149
223
}
150
224
}  // namespace PyGlobals
151
225
}  // namespace MAUS