ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
AthenaPython
src
PyAthenaAud.cxx
Go to the documentation of this file.
1
2
3
/*
4
Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5
*/
6
7
// PyAthenaAud.cxx
8
// Implementation file for class PyAthena::Aud
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/PyAthenaAud.h
"
22
#include "
RootUtils/PyAthenaGILStateEnsure.h
"
23
24
// STL includes
25
#include <algorithm>
26
27
// FrameWork includes
28
#include "GaudiKernel/System.h"
29
#include "GaudiKernel/INamedInterface.h"
30
#include "GaudiKernel/ServiceHandle.h"
31
#include "
AthenaPython/IPyComponentMgr.h
"
32
33
namespace
PyAthena
{
34
36
// Public methods:
38
39
// Constructors
41
42
Aud::Aud
(
const
std::string& name, ISvcLocator* svcLocator ) :
43
Gaudi
::Auditor( name, svcLocator ),
44
m_self
( nullptr )
45
{}
46
47
// Destructor
49
Aud::~Aud
()
50
{
51
if
(
m_self
) {
52
RootUtils::PyGILStateEnsure
ensure;
53
Py_DECREF(
m_self
);
54
m_self
=
nullptr
;
55
}
56
}
57
58
// Athena Auditor's Hooks
60
StatusCode
61
Aud::initialize
()
62
{
63
return
PyAthena::callPyMethod(
m_self
,
"sysInitialize"
);
64
}
65
66
StatusCode
67
Aud::sysInitialize
()
68
{
69
ServiceHandle<IPyComponentMgr>
pyMgr
70
(
"PyAthena::PyComponentMgr/PyComponentMgr"
, name() );
71
if
( !pyMgr.retrieve().isSuccess() ) {
72
return
StatusCode::FAILURE;
73
}
74
75
// first retrieve our python object cousin...
76
m_self
= pyMgr->pyObject(
this
);
77
78
if
(
m_self
== Py_None ) {
79
return
StatusCode::FAILURE;
80
}
81
82
// re-route to usual sysInit...
83
return
Gaudi::Auditor::sysInitialize();
84
}
85
86
StatusCode
87
Aud::finalize
()
88
{
89
return
PyAthena::callPyMethod(
m_self
,
"sysFinalize"
);
90
}
91
93
// Const methods:
95
96
const
char
*
97
Aud::typeName
()
const
98
{
99
static
const
std::string tname = System::typeinfoName(
typeid
(*
this
));
100
return
tname.c_str();
101
}
102
104
// Non-const methods:
106
107
void
108
Aud::before
(
const
std::string& evt,
const
std::string& name,
109
const
EventContext&)
110
{
111
std::string evtname = evt;
112
std::transform(evt.begin(), evt.end(), evtname.begin(),
::tolower
);
113
PyAthena::pyAudit (
m_self
,
"before"
, evtname.c_str(), name.c_str());
114
}
115
116
void
117
Aud::after
(
const
std::string& evt,
const
std::string& name,
118
const
EventContext&,
const
StatusCode&
sc
)
119
{
120
std::string evtname = evt;
121
std::transform(evt.begin(), evt.end(), evtname.begin(),
::tolower
);
122
PyAthena::pyAudit (
m_self
,
"after"
, evtname.c_str(), name.c_str(),
sc
);
123
}
124
126
// Non-const methods:
128
129
bool
130
Aud::setPyAttr
(
PyObject
* o )
131
{
132
// now we tell the PyObject which C++ object it is the cousin of.
133
RootUtils::PyGILStateEnsure
ensure;
134
PyObject
* pyobj = TPython::CPPInstance_FromVoidPtr
135
( (
void
*)
this
, this->
typeName
() );
136
if
( !pyobj ) {
137
PyErr_Clear();
138
// try PyAthena::Aud
139
pyobj = TPython::CPPInstance_FromVoidPtr ((
void
*)
this
,
"PyAthena::Aud"
);
140
MsgStream
msg
( msgSvc(), name() );
141
msg
<< MSG::INFO
142
<<
"could not dyncast component ["
<< name() <<
"] to a python "
143
<<
"object of type ["
<< this->
typeName
() <<
"] (probably a missing "
144
<<
"dictionary)"
<<
endmsg
145
<<
"fallback to [PyAthena::Aud]..."
146
<<
endmsg
;
147
}
148
if
( !pyobj ) {
149
PyErr_Clear();
150
MsgStream
msg
( msgSvc(), name() );
151
msg
<< MSG::WARNING <<
"Could not dyncast component ["
152
<< name() <<
"] to a pyobject of type ["
153
<< this->
typeName
() <<
"]"
154
<<
endmsg
;
155
}
else
{
156
if
( -1 == PyObject_SetAttrString(o,
"_cppHandle"
, pyobj) ) {
157
PyErr_Clear();
158
MsgStream
msg
( msgSvc(), name() );
159
msg
<< MSG::WARNING
160
<<
"Could not attach C++ handle ["
<< name() <<
"] to its python "
161
<<
"cousin !"
162
<<
endmsg
;
163
if
( -1 == PyObject_SetAttrString(o,
"_cppHandle"
, Py_None) ) {
164
PyErr_Clear();
165
msg
<< MSG::WARNING
166
<<
"could not attach a dummy C++ handle ["
<< name() <<
"] to its "
167
<<
"python cousin !"
168
<<
endmsg
;
169
}
170
}
else
{
171
return
true
;
172
}
173
}
174
return
false
;
175
}
176
177
}
//> end namespace PyAthena
endmsg
#define endmsg
Definition
AnalysisConfig_Ntuple.cxx:61
tolower
void tolower(std::string &s)
Definition
AthenaSummarySvc.cxx:100
IPyComponentMgr.h
PyObject
_object PyObject
Definition
IPyComponent.h:27
sc
static Double_t sc
Definition
LArPhysWaveHECTool.cxx:37
PyAthenaAud.h
PyAthenaGILStateEnsure.h
PyAthenaUtils.h
PyAthena::Aud::after
virtual void after(const std::string &evt, const std::string &name, const EventContext &ctx, const StatusCode &sc) override
Definition
PyAthenaAud.cxx:117
PyAthena::Aud::~Aud
virtual ~Aud()
Destructor:
Definition
PyAthenaAud.cxx:49
PyAthena::Aud::sysInitialize
virtual StatusCode sysInitialize() override
Definition
PyAthenaAud.cxx:67
PyAthena::Aud::initialize
virtual StatusCode initialize() override
Gaudi Aud Implementation.
Definition
PyAthenaAud.cxx:61
PyAthena::Aud::m_self
PyObject * m_self
Pointer to self (from the python world).
Definition
PyAthenaAud.h:90
PyAthena::Aud::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
PyAthenaAud.cxx:97
PyAthena::Aud::before
virtual void before(const std::string &evt, const std::string &name, const EventContext &ctx) override
Auditor interface
Definition
PyAthenaAud.cxx:108
PyAthena::Aud::finalize
virtual StatusCode finalize() override
Definition
PyAthenaAud.cxx:87
PyAthena::Aud::Aud
Aud()
Default constructor:
PyAthena::Aud::setPyAttr
virtual bool setPyAttr(PyObject *pyobj) override
attach the C++ component to its python cousin
Definition
PyAthenaAud.cxx:130
RootUtils::PyGILStateEnsure
Definition
PyAthenaGILStateEnsure.h:20
ServiceHandle
Definition
ClusterMakerTool.h:36
Gaudi
=============================================================================
Definition
CaloGPUClusterAndCellDataMonitorOptions.h:273
PyAthena
Definition
IPyComponent.h:28
msg
MsgStream & msg
Definition
testRead.cxx:32
Generated on
for ATLAS Offline Software by
1.17.0