Belle II Software  release-05-02-19
InteractiveModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <boost/python.hpp>
11 
12 #include <framework/modules/core/InteractiveModule.h>
13 
14 #include <framework/logging/Logger.h>
15 
16 
17 using namespace Belle2;
18 
19 //-----------------------------------------------------------------
20 // Register the Module
21 //-----------------------------------------------------------------
22 REG_MODULE(Interactive)
23 
24 //-----------------------------------------------------------------
25 // Implementation
26 //-----------------------------------------------------------------
27 
29 {
30  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.");
31 }
32 
33 InteractiveModule::~InteractiveModule() = default;
34 
36 {
37  //In case of exceptions in the embed() call, the imports may get lost (why?)
38  //reimporting them ensures we can call embed() again in the next event
39  if (PyRun_SimpleString("import interactive") == -1) {
40  B2FATAL("'import interactive' failed.");
41  }
42  if (PyRun_SimpleString("from ROOT import Belle2") == -1) {
43  B2FATAL("'from ROOT import Belle2' failed.");
44  }
45 
46  B2INFO("Opening (I)Python shell, press Ctrl+D to close it. Press Ctrl+C first to exit basf2.");
47  if (PyRun_SimpleString("interactive.embed()") == -1) {
48  B2ERROR("embed() failed!");
49  }
50 }
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::InteractiveModule
Start an interactive python shell in each call of event().
Definition: InteractiveModule.h:38
Belle2::InteractiveModule::event
virtual void event() override
This method is the core of the module.
Definition: InteractiveModule.cc:35