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#include <boost/python.hpp>
9
10#include <framework/modules/core/InteractiveModule.h>
11
12#include <framework/logging/Logger.h>
13
14
15using namespace Belle2;
16
17//-----------------------------------------------------------------
18// Register the Module
19//-----------------------------------------------------------------
20REG_MODULE(Interactive);
21
22//-----------------------------------------------------------------
23// Implementation
24//-----------------------------------------------------------------
25
27{
28 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.");
29}
30
31InteractiveModule::~InteractiveModule() = default;
32
34{
35 //In case of exceptions in the embed() call, the imports may get lost (why?)
36 //reimporting them ensures we can call embed() again in the next event
37 if (PyRun_SimpleString("import interactive") == -1) {
38 B2FATAL("'import interactive' failed.");
39 }
40 if (PyRun_SimpleString("from ROOT import Belle2") == -1) {
41 B2FATAL("'from ROOT import Belle2' failed.");
42 }
43
44 B2INFO("Opening (I)Python shell, press Ctrl+D to close it. Press Ctrl+C first to exit basf2.");
45 if (PyRun_SimpleString("interactive.embed()") == -1) {
46 B2ERROR("embed() failed!");
47 }
48}
virtual void event() override
This method is the core of the module.
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.