ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
AthenaPython
src
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
20
#include "
AthenaPython/PyAthenaUtils.h
"
21
#include "
AthenaPython/PyAthenaAlg.h
"
22
#include "
RootUtils/PyAthenaGILStateEnsure.h
"
23
24
// STL includes
25
26
// FrameWork includes
27
#include "GaudiKernel/System.h"
28
#include "GaudiKernel/MsgStream.h"
29
#include "GaudiKernel/ServiceHandle.h"
30
#include "
AthenaPython/IPyComponentMgr.h
"
31
32
namespace
PyAthena
{
33
35
// Public methods:
37
38
// Constructors
40
Alg::Alg
(
const
std::string& name, ISvcLocator* svcLocator ) :
41
AlgBase_t
( name, svcLocator ),
42
m_self
( nullptr )
43
{}
44
45
// Destructor
47
Alg::~Alg
()
48
{
49
ATH_MSG_DEBUG
(
"Calling destructor"
);
50
if
(
m_self
) {
51
RootUtils::PyGILStateEnsure
ensure;
52
Py_DECREF(
m_self
);
53
m_self
=
nullptr
;
54
}
55
}
56
57
// Framework's Hooks
59
StatusCode
60
Alg::initialize
()
61
{
62
ATH_MSG_INFO
(
"Initializing "
<< name() <<
"..."
);
63
return
PyAthena::callPyMethod(
m_self
,
"sysInitialize"
);
64
}
65
66
StatusCode
67
Alg::reinitialize
()
68
{
69
ATH_MSG_INFO
(
"Re-Initializing "
<< name() <<
"..."
);
70
return
PyAthena::callPyMethod(
m_self
,
"sysReinitialize"
);
71
}
72
73
StatusCode
74
Alg::start
()
75
{
76
return
PyAthena::callPyMethod(
m_self
,
"sysStart"
);
77
}
78
79
StatusCode
80
Alg::stop
()
81
{
82
return
PyAthena::callPyMethod(
m_self
,
"sysStop"
);
83
}
84
85
StatusCode
86
Alg::finalize
()
87
{
88
ATH_MSG_INFO
(
"Finalizing "
<< name() <<
"..."
);
89
return
PyAthena::callPyMethod(
m_self
,
"sysFinalize"
);
90
}
91
92
StatusCode
93
Alg::execute
(
const
EventContext& ctx)
94
{
95
RootUtils::PyGILStateEnsure
ensure;
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
103
StatusCode
104
Alg::sysInitialize
()
105
{
106
ServiceHandle<IPyComponentMgr>
pyMgr
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...
123
return
AlgBase_t::sysInitialize
();
124
}
125
127
// Const methods:
129
130
const
char
*
131
Alg::typeName
()
const
132
{
133
static
const
std::string tname = System::typeinfoName(
typeid
(*
this
));
134
return
tname.c_str();
135
}
136
138
// Non-const methods:
140
141
bool
142
Alg::setPyAttr
(
PyObject
* o )
143
{
144
// now we tell the PyObject which C++ object it is the cousin of.
145
RootUtils::PyGILStateEnsure
ensure;
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"
);
152
ATH_MSG_INFO
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();
166
ATH_MSG_WARNING
167
(
"Could not attach C++ handle ["
<< name() <<
"] to its python "
168
<<
"cousin !"
);
169
if
( -1 == PyObject_SetAttrString(o,
"_cppHandle"
, Py_None) ) {
170
PyErr_Clear();
171
ATH_MSG_WARNING
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
endmsg
#define endmsg
Definition
AnalysisConfig_Ntuple.cxx:61
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition
AthMsgStreamMacros.h:33
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition
AthMsgStreamMacros.h:31
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition
AthMsgStreamMacros.h:32
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition
AthMsgStreamMacros.h:29
IPyComponentMgr.h
PyObject
_object PyObject
Definition
IPyComponent.h:27
sc
static Double_t sc
Definition
LArPhysWaveHECTool.cxx:37
PyAthenaAlg.h
PyAthenaGILStateEnsure.h
PyAthenaUtils.h
AthCommonAlgorithm< Gaudi::Algorithm >::sysInitialize
virtual StatusCode sysInitialize() override
Definition
AthCommonAlgorithm.cxx:71
PyAthena::Alg::reinitialize
virtual StatusCode reinitialize() override
Definition
PyAthenaAlg.cxx:67
PyAthena::Alg::m_self
PyObject * m_self
Pointer to self (from the python world).
Definition
PyAthenaAlg.h:96
PyAthena::Alg::stop
virtual StatusCode stop() override
Definition
PyAthenaAlg.cxx:80
PyAthena::Alg::sysInitialize
virtual StatusCode sysInitialize() override
Override sysInitialize.
Definition
PyAthenaAlg.cxx:104
PyAthena::Alg::setPyAttr
virtual bool setPyAttr(PyObject *pyobj) override
attach the C++ component to its python cousin
Definition
PyAthenaAlg.cxx:142
PyAthena::Alg::typeName
virtual const char * typeName() const override
return the std::type_info name of the underlying py-component This is used by concrete implementation...
Definition
PyAthenaAlg.cxx:131
PyAthena::Alg::start
virtual StatusCode start() override
Definition
PyAthenaAlg.cxx:74
PyAthena::Alg::Alg
Alg()
Default constructor:
PyAthena::Alg::execute
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
Definition
PyAthenaAlg.cxx:93
PyAthena::Alg::finalize
virtual StatusCode finalize() override
Definition
PyAthenaAlg.cxx:86
PyAthena::Alg::~Alg
virtual ~Alg()
Destructor:
Definition
PyAthenaAlg.cxx:47
PyAthena::Alg::initialize
virtual StatusCode initialize() override
Definition
PyAthenaAlg.cxx:60
RootUtils::PyGILStateEnsure
Definition
PyAthenaGILStateEnsure.h:20
ServiceHandle
Definition
ClusterMakerTool.h:36
PyAthena
Definition
IPyComponent.h:28
PyAthena::AlgBase_t
::AthAlgorithm AlgBase_t
Definition
PyAthenaAlg.h:29
Generated on
for ATLAS Offline Software by
1.17.0