Belle II Software development
InteractiveModule.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/modules/core/InteractiveModule.h>
10#include <framework/logging/Logger.h>
11
12#include <Python.h>
13
14using namespace Belle2;
15
16//-----------------------------------------------------------------
17// Register the Module
18//-----------------------------------------------------------------
19REG_MODULE(Interactive);
20
21//-----------------------------------------------------------------
22// Implementation
23//-----------------------------------------------------------------
24
26{
27 setDescription("Start an interactive (I)python shell in each call of event(). Also imports the ROOT.Belle2 namespace for convenience, allowing you to use Belle2.PyStoreArray etc. directly.");
28}
29
30InteractiveModule::~InteractiveModule() = default;
31
33{
34 //In case of exceptions in the embed() call, the imports may get lost (why?)
35 //reimporting them ensures we can call embed() again in the next event
36 if (PyRun_SimpleString("import interactive") == -1) {
37 B2FATAL("'import interactive' failed.");
38 }
39 if (PyRun_SimpleString("from ROOT import Belle2") == -1) {
40 B2FATAL("'from ROOT import Belle2' failed.");
41 }
42
43 B2INFO("Opening (I)Python shell, press Ctrl+D to close it. Press Ctrl+C first to exit basf2.");
44 if (PyRun_SimpleString("interactive.embed()") == -1) {
45 B2ERROR("embed() failed!");
46 }
47}
virtual void event() override
This method is the core of the module.
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
Module()
Constructor.
Definition Module.cc:30
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.