~ajdobbs/maus/event-selection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
 *
 * MAUS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MAUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <string>

#include "src/common_cpp/DataStructure/RunHeader.hh"
#include "src/common_cpp/JsonCppProcessors/RunHeaderProcessor.hh"
#include "src/common_cpp/DataStructure/RunFooter.hh"
#include "src/common_cpp/JsonCppProcessors/RunFooterProcessor.hh"

#include "src/common_cpp/Utils/Globals.hh"
#include "src/common_cpp/Utils/RunActionManager.hh"

#include "src/py_cpp/PyRunActionManager.hh"

namespace MAUS {
namespace PyRunActionManager {
std::string StartOfRun_DocString =
  std::string("start_of_run(run_number)\n\n")+
  std::string("Call the start of run action for all RunActions registered ")+
  std::string("with the run action manager.\n\n")+
  std::string("\\param run_number is an integer corresponding to the run ")+
  std::string("number of the new run.\n\n")+
  std::string("\\returns string representation of the RunHeader (json format)");

std::string EndOfRun_DocString =
  std::string("end_of_run(run_number)\n\n")+
  std::string("Call the end of run action for all RunActions registered ")+
  std::string("with the run action manager.\n\n")+
  std::string("\\param run_number is an integer corresponding to the run ")+
  std::string("number of the new run.\n\n")+
  std::string("\\returns string representation of the RunFooter (json format)");

PyObject* StartOfRun(PyObject *dummy, PyObject *args) {
  int run_number;
  std::string head_str;
  if (!PyArg_ParseTuple(args, "i", &run_number)) {
    PyErr_SetString(PyExc_TypeError,
           "Failed to interpret start_of_run argument as an integer");
    return NULL;
  }
  try {
    RunActionManager* maus_run_action_manager =
                          Globals::GetInstance()->GetRunActionManager();
    if (maus_run_action_manager == NULL) {
      PyErr_SetString(PyExc_RuntimeError,
         "Error - somehow MAUS library was initialised but run action is not.");
      return NULL;
    }
    RunHeader* run_header = new RunHeader();
    run_header->SetRunNumber(run_number);
    maus_run_action_manager->StartOfRun(run_header);
    Json::Value* head_json = RunHeaderProcessor().CppToJson(*run_header, "");
    Py_BuildValue("s", head_str.c_str());
    head_str = JsonWrapper::JsonToString(*head_json);
    delete head_json;
    delete run_header;
  } catch (std::exception& exc) {
    PyErr_SetString(PyExc_RuntimeError, (&exc)->what());
    return NULL;
  }
  return Py_BuildValue("s", head_str.c_str());
}

PyObject* EndOfRun(PyObject *dummy, PyObject *args) {
  int run_number;
  std::string foot_str;
  if (!PyArg_ParseTuple(args, "i", &run_number)) {
    PyErr_SetString(PyExc_TypeError,
           "Failed to interpret end_of_run argument as an integer");
    return NULL;
  }
  try {
    RunActionManager* maus_run_action_manager =
                          Globals::GetInstance()->GetRunActionManager();
    if (maus_run_action_manager == NULL) {
      PyErr_SetString(PyExc_RuntimeError,
         "Error - somehow MAUS library was initialised but run action is not.");
      return NULL;
    }
    RunFooter* run_footer = new RunFooter();
    run_footer->SetRunNumber(run_number);
    maus_run_action_manager->EndOfRun(run_footer);
    Json::Value* foot_json = RunFooterProcessor().CppToJson(*run_footer, "");
    Py_BuildValue("s", foot_str.c_str());
    foot_str = JsonWrapper::JsonToString(*foot_json);
    delete foot_json;
    delete run_footer;
  } catch (std::exception& exc) {
    PyErr_SetString(PyExc_RuntimeError, (&exc)->what());
    return NULL;
  }
  return Py_BuildValue("s", foot_str.c_str());
}

static PyMethodDef methods[] = {
{"start_of_run", (PyCFunction)StartOfRun,
                         METH_VARARGS, StartOfRun_DocString.c_str()},
{"end_of_run", (PyCFunction)EndOfRun,
                         METH_VARARGS, EndOfRun_DocString.c_str()},
{NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initrun_action_manager(void) {
  Py_Initialize();
  PyObject* maus_module = Py_InitModule("run_action_manager", methods);
  if (maus_module == NULL) return;
}
}  // namespace PyRunActionManager
}  // namespace MAUS