ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PyAthenaAud.cxx
Go to the documentation of this file.
1 
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
23 
24 // STL includes
25 
26 // FrameWork includes
27 #include "GaudiKernel/System.h"
28 #include "GaudiKernel/INamedInterface.h"
29 #include "GaudiKernel/ServiceHandle.h"
31 
32 namespace PyAthena {
33 
35 // Public methods:
37 
38 // Constructors
40 
41 Aud::Aud( const std::string& name, ISvcLocator* svcLocator ) :
42  ::Auditor( name, svcLocator ),
43  m_self ( nullptr )
44 {}
45 
46 // Destructor
49 {
50  if ( m_self ) {
52  Py_DECREF( m_self );
53  m_self = nullptr;
54  }
55 }
56 
57 // Athena Auditor's Hooks
61 {
62  return PyAthena::callPyMethod( m_self, "sysInitialize" );
63 }
64 
67 {
69  ( "PyAthena::PyComponentMgr/PyComponentMgr", name() );
70  if ( !pyMgr.retrieve().isSuccess() ) {
71  return StatusCode::FAILURE;
72  }
73 
74  // first retrieve our python object cousin...
75  m_self = pyMgr->pyObject( this );
76 
77  if ( m_self == Py_None ) {
78  return StatusCode::FAILURE;
79  }
80 
81  // re-route to usual sysInit...
82  return ::Auditor::sysInitialize();
83 }
84 
87 {
88  return PyAthena::callPyMethod( m_self, "sysFinalize" );
89 }
90 
92 // Const methods:
94 
95 const char*
97 {
98  static const std::string tname = System::typeinfoName(typeid(*this));
99  return tname.c_str();
100 }
101 
103 // Non-const methods:
105 
106 void
107 Aud::before(StandardEventType evt, INamedInterface* comp)
108 {
109  py_before (evt, comp->name());
110 }
111 
112 void
113 Aud::before(StandardEventType evt, const std::string& comp)
114 {
115  py_before (evt, comp);
116 }
117 
118 void
119 Aud::before(CustomEventTypeRef evt, INamedInterface* comp)
120 {
121  py_before (evt, comp->name());
122 }
123 
124 void
125 Aud::before(CustomEventTypeRef evt, const std::string& comp)
126 {
127  py_before (evt, comp);
128 }
129 
130 void
131 Aud::after(StandardEventType evt, INamedInterface* comp, const StatusCode& sc)
132 {
133  py_after (evt, comp->name(), sc);
134 }
135 
136 void
137 Aud::after(StandardEventType evt, const std::string& comp, const StatusCode& sc)
138 {
139  py_after (evt, comp, sc);
140 }
141 
142 void
143 Aud::after(CustomEventTypeRef evt, INamedInterface* comp, const StatusCode& sc)
144 {
145  py_after (evt, comp->name(), sc);
146 }
147 
148 void
149 Aud::after(CustomEventTypeRef evt, const std::string& comp,
150  const StatusCode& sc)
151 {
152  py_after (evt, comp, sc);
153 }
154 
156 void
157 Aud::py_before (IAuditor::StandardEventType evt, const std::string& component)
158 {
159  const char* evtname = 0;
160  switch (evt) {
161  case Initialize: evtname = "initialize"; break;
162  case ReInitialize: evtname = "reinitialize"; break;
163  case Execute: evtname = "execute"; break;
164  case Finalize: evtname = "finalize"; break;
165  case Start: evtname = "start"; break;
166  case Stop: evtname = "stop"; break;
167  case ReStart: evtname = "restart"; break;
168  }
169  return PyAthena::pyAudit (m_self, "before", evtname, component.c_str());
170 }
171 
172 void
173 Aud::py_before(IAuditor::CustomEventTypeRef evt, const std::string& component)
174 {
175  return PyAthena::pyAudit (m_self, "before", evt.c_str(), component.c_str());
176 }
177 
178 void
179 Aud::py_after(IAuditor::StandardEventType evt,
180  const std::string& component,
181  const StatusCode& sc)
182 {
183  const char* evtname = 0;
184  switch (evt) {
185  case Initialize: evtname = "initialize"; break;
186  case ReInitialize: evtname = "reinitialize"; break;
187  case Execute: evtname = "execute"; break;
188  case Finalize: evtname = "finalize"; break;
189  case Start: evtname = "start"; break;
190  case Stop: evtname = "stop"; break;
191  case ReStart: evtname = "restart"; break;
192  }
193  return PyAthena::pyAudit (m_self, "after", evtname, component.c_str(), sc);
194 }
195 
196 void
197 Aud::py_after(IAuditor::CustomEventTypeRef evt,
198  const std::string& component,
199  const StatusCode& sc)
200 {
201  return PyAthena::pyAudit (m_self,
202  "after", evt.c_str(), component.c_str(), sc);
203 }
204 
205 
207 // Non-const methods:
209 
210 bool
212 {
213  // now we tell the PyObject which C++ object it is the cousin of.
215  PyObject* pyobj = TPython::CPPInstance_FromVoidPtr
216  ( (void*)this, this->typeName() );
217  if ( !pyobj ) {
218  PyErr_Clear();
219  // try PyAthena::Aud
220  pyobj = TPython::CPPInstance_FromVoidPtr ((void*)this, "PyAthena::Aud");
221  MsgStream msg( msgSvc(), name() );
222  msg << MSG::INFO
223  << "could not dyncast component [" << name() << "] to a python "
224  << "object of type [" << this->typeName() << "] (probably a missing "
225  << "dictionary)" << endmsg
226  << "fallback to [PyAthena::Aud]..."
227  << endmsg;
228  }
229  if ( !pyobj ) {
230  PyErr_Clear();
231  MsgStream msg( msgSvc(), name() );
232  msg << MSG::WARNING << "Could not dyncast component ["
233  << name() << "] to a pyobject of type ["
234  << this->typeName() << "]"
235  << endmsg;
236  } else {
237  if ( -1 == PyObject_SetAttrString(o, "_cppHandle", pyobj) ) {
238  PyErr_Clear();
239  MsgStream msg( msgSvc(), name() );
240  msg << MSG::WARNING
241  << "Could not attach C++ handle [" << name() << "] to its python "
242  << "cousin !"
243  << endmsg;
244  if ( -1 == PyObject_SetAttrString(o, "_cppHandle", Py_None) ) {
245  PyErr_Clear();
246  msg << MSG::WARNING
247  << "could not attach a dummy C++ handle [" << name() << "] to its "
248  << "python cousin !"
249  << endmsg;
250  }
251  } else {
252  return true;
253  }
254  }
255  return false;
256 }
257 
258 } //> end namespace PyAthena
plotting.yearwise_luminosity_vs_mu.comp
comp
Definition: yearwise_luminosity_vs_mu.py:23
PyAthena::Aud::initialize
virtual StatusCode initialize() override
Gaudi Aud Implementation.
Definition: PyAthenaAud.cxx:60
PyAthena::Aud::py_before
virtual void py_before(IAuditor::StandardEventType, const std::string &)
Audit the start of a standard "event".
Definition: PyAthenaAud.cxx:157
PyAthena::Aud::py_after
virtual void py_after(IAuditor::StandardEventType, const std::string &, const StatusCode &)
Audit the end of a standard "event".
Definition: PyAthenaAud.cxx:179
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
PyAthena::Aud::finalize
virtual StatusCode finalize() override
Definition: PyAthenaAud.cxx:86
PyAthena::Aud::setPyAttr
virtual bool setPyAttr(PyObject *pyobj) override
attach the C++ component to its python cousin
Definition: PyAthenaAud.cxx:211
PyAthena::Aud::~Aud
virtual ~Aud()
Destructor:
Definition: PyAthenaAud.cxx:48
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
RootUtils::PyGILStateEnsure
Definition: PyAthenaGILStateEnsure.h:20
PyAthena::Aud::after
virtual void after(StandardEventType, INamedInterface *, const StatusCode &) override
Definition: PyAthenaAud.cxx:131
PyAthena::Aud::Aud
Aud()
Default constructor:
LArPulseShapeRunConfig.Execute
Execute
Definition: LArPulseShapeRunConfig.py:62
Athena::typeinfoName
std::string typeinfoName(const std::type_info &ti)
Convert a type_info to a demangled string.
Definition: AthenaKernel/src/ClassName.cxx:23
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
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:96
PyAthena::Aud::sysInitialize
virtual StatusCode sysInitialize() override
Definition: PyAthenaAud.cxx:66
PyAthenaAud.h
PyAthenaUtils.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
jobOptions.Initialize
Initialize
Definition: jobOptions.pA.py:28
IPyComponentMgr.h
PyAthena
Definition: IPyComponent.h:28
PyAthenaGILStateEnsure.h
PyAthena::Aud::m_self
PyObject * m_self
Pointer to self (from the python world)
Definition: PyAthenaAud.h:112
PyAthena::Aud::before
virtual void before(StandardEventType, INamedInterface *) override
Auditor interface
Definition: PyAthenaAud.cxx:107
PyObject
_object PyObject
Definition: IPyComponent.h:26
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
ServiceHandle
Definition: ClusterMakerTool.h:37