ATLAS Offline Software
Loading...
Searching...
No Matches
PyAthenaAlg.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7// PyAthenaAlg.cxx
8// Implementation file for class PyAthena::Alg
9// Author: S.Binet<binet@cern.ch>
10// Modified: Wim Lavrijsen <WLavrijsen@lbl.gov>
12
13// Python includes
14#include "Python.h"
15
16// PyROOT includes
17#include "TPython.h"
18
19// AthenaPython includes
23
24// STL includes
25
26// FrameWork includes
27#include "GaudiKernel/System.h"
28#include "GaudiKernel/MsgStream.h"
29#include "GaudiKernel/ServiceHandle.h"
31
32namespace PyAthena {
33
35// Public methods:
37
38// Constructors
40Alg::Alg( const std::string& name, ISvcLocator* svcLocator ) :
41 AlgBase_t( name, svcLocator ),
42 m_self ( nullptr )
43{}
44
45// Destructor
48{
49 ATH_MSG_DEBUG("Calling destructor");
50 if ( m_self ) {
52 Py_DECREF( m_self );
53 m_self = nullptr;
54 }
55}
56
57// Framework's Hooks
59StatusCode
61{
62 ATH_MSG_INFO("Initializing " << name() << "...");
63 return PyAthena::callPyMethod( m_self, "sysInitialize" );
64}
65
66StatusCode
68{
69 ATH_MSG_INFO("Re-Initializing " << name() << "...");
70 return PyAthena::callPyMethod( m_self, "sysReinitialize" );
71}
72
73StatusCode
75{
76 return PyAthena::callPyMethod( m_self, "sysStart" );
77}
78
79StatusCode
81{
82 return PyAthena::callPyMethod( m_self, "sysStop" );
83}
84
85StatusCode
87{
88 ATH_MSG_INFO("Finalizing " << name() << "...");
89 return PyAthena::callPyMethod( m_self, "sysFinalize" );
90}
91
92StatusCode
93Alg::execute(const EventContext& ctx)
94{
96 PyObject* pycontext = PyCapsule_New ( const_cast<EventContext*>(&ctx), nullptr, nullptr);
97
98 StatusCode sc = PyAthena::callPyMethod( m_self, "sysExecute", pycontext );
99 Py_DECREF (pycontext);
100 return sc;
101}
102
103StatusCode
105{
107 ( "PyAthena::PyComponentMgr/PyComponentMgr", name() );
108 if ( !pyMgr.retrieve().isSuccess() ) {
109 ATH_MSG_ERROR("Could not retrieve service [" << pyMgr.typeAndName()
110 << "] !!");
111 return StatusCode::FAILURE;
112 }
113
114 // first retrieve our python object cousin...
115 m_self = pyMgr->pyObject( this );
116
117 if ( m_self == Py_None ) {
118 ATH_MSG_ERROR("Wrapped PyObject is NONE !");
119 return StatusCode::FAILURE;
120 }
121
122 // re-route to usual sysInit...
124}
125
127// Const methods:
129
130const char*
132{
133 static const std::string tname = System::typeinfoName(typeid(*this));
134 return tname.c_str();
135}
136
138// Non-const methods:
140
141bool
143{
144 // now we tell the PyObject which C++ object it is the cousin of.
146 PyObject* pyobj = TPython::CPPInstance_FromVoidPtr
147 ( (void*)this, this->typeName() );
148 if ( !pyobj ) {
149 PyErr_Clear();
150 // try PyAthena::Alg
151 pyobj = TPython::CPPInstance_FromVoidPtr ((void*)this, "PyAthena::Alg");
153 ("could not dyncast component [" << name() << "] to a python "
154 << "object of type [" << this->typeName() << "] (probably a missing "
155 << "dictionary)" << endmsg
156 << "fallback to [PyAthena::Alg]...");
157 }
158 if ( !pyobj ) {
159 PyErr_Clear();
160 ATH_MSG_WARNING("Could not dyncast component ["
161 << name() << "] to a pyobject of type ["
162 << this->typeName() << "]");
163 } else {
164 if ( -1 == PyObject_SetAttrString(o, "_cppHandle", pyobj) ) {
165 PyErr_Clear();
167 ("Could not attach C++ handle [" << name() << "] to its python "
168 << "cousin !");
169 if ( -1 == PyObject_SetAttrString(o, "_cppHandle", Py_None) ) {
170 PyErr_Clear();
172 ("could not attach a dummy C++ handle [" << name() << "] to its "
173 "python cousin !");
174 }
175 } else {
176 return true;
177 }
178 }
179 return false;
180}
181
182
183} //> end namespace PyAthena
#define endmsg
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
_object PyObject
static Double_t sc
virtual StatusCode sysInitialize() override
Override sysInitialize.
virtual StatusCode reinitialize() override
PyObject * m_self
Pointer to self (from the python world).
Definition PyAthenaAlg.h:96
virtual StatusCode stop() override
virtual StatusCode sysInitialize() override
Override sysInitialize.
virtual bool setPyAttr(PyObject *pyobj) override
attach the C++ component to its python cousin
virtual const char * typeName() const override
return the std::type_info name of the underlying py-component This is used by concrete implementation...
virtual StatusCode start() override
Alg()
Default constructor:
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
virtual StatusCode finalize() override
virtual ~Alg()
Destructor:
virtual StatusCode initialize() override
::AthAlgorithm AlgBase_t
Definition PyAthenaAlg.h:29