ATLAS Offline Software
PyAthenaAud.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2019 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 
155 // Obsolete methods
156 void
157 Aud::beforeInitialize(INamedInterface* comp)
158 {
160 }
161 
162 void
163 Aud::afterInitialize(INamedInterface* comp)
164 {
165  py_after (IAuditor::Initialize, comp->name(), StatusCode::SUCCESS);
166 }
167 
168 void
169 Aud::beforeReinitialize(INamedInterface* comp)
170 {
171  py_before (IAuditor::ReInitialize, comp->name());
172 }
173 
174 void
175 Aud::afterReinitialize(INamedInterface* comp)
176 {
177  py_after (IAuditor::ReInitialize, comp->name(), StatusCode::SUCCESS);
178 }
179 
180 void
181 Aud::beforeExecute(INamedInterface* comp)
182 {
183  py_before (IAuditor::Execute, comp->name());
184 }
185 
186 void
187 Aud::afterExecute(INamedInterface* comp, const StatusCode& sc)
188 {
189  py_after (IAuditor::Execute, comp->name(), sc);
190 }
191 
192 void
193 Aud::beforeFinalize(INamedInterface* comp)
194 {
195  py_before (IAuditor::Finalize, comp->name());
196 }
197 
198 void
199 Aud::afterFinalize(INamedInterface* comp)
200 {
201  py_after (IAuditor::Finalize, comp->name(), StatusCode::SUCCESS);
202 }
203 
205 void
206 Aud::py_before (IAuditor::StandardEventType evt, const std::string& component)
207 {
208  const char* evtname = 0;
209  switch (evt) {
210  case Initialize: evtname = "initialize"; break;
211  case ReInitialize: evtname = "reinitialize"; break;
212  case Execute: evtname = "execute"; break;
213  case Finalize: evtname = "finalize"; break;
214  case Start: evtname = "start"; break;
215  case Stop: evtname = "stop"; break;
216  case ReStart: evtname = "restart"; break;
217  }
218  return PyAthena::pyAudit (m_self, "before", evtname, component.c_str());
219 }
220 
221 void
222 Aud::py_before(IAuditor::CustomEventTypeRef evt, const std::string& component)
223 {
224  return PyAthena::pyAudit (m_self, "before", evt.c_str(), component.c_str());
225 }
226 
227 void
228 Aud::py_after(IAuditor::StandardEventType evt,
229  const std::string& component,
230  const StatusCode& sc)
231 {
232  const char* evtname = 0;
233  switch (evt) {
234  case Initialize: evtname = "initialize"; break;
235  case ReInitialize: evtname = "reinitialize"; break;
236  case Execute: evtname = "execute"; break;
237  case Finalize: evtname = "finalize"; break;
238  case Start: evtname = "start"; break;
239  case Stop: evtname = "stop"; break;
240  case ReStart: evtname = "restart"; break;
241  }
242  return PyAthena::pyAudit (m_self, "after", evtname, component.c_str(), sc);
243 }
244 
245 void
246 Aud::py_after(IAuditor::CustomEventTypeRef evt,
247  const std::string& component,
248  const StatusCode& sc)
249 {
250  return PyAthena::pyAudit (m_self,
251  "after", evt.c_str(), component.c_str(), sc);
252 }
253 
254 
256 // Non-const methods:
258 
259 bool
261 {
262  // now we tell the PyObject which C++ object it is the cousin of.
264  PyObject* pyobj = TPython::CPPInstance_FromVoidPtr
265  ( (void*)this, this->typeName() );
266  if ( !pyobj ) {
267  PyErr_Clear();
268  // try PyAthena::Aud
269  pyobj = TPython::CPPInstance_FromVoidPtr ((void*)this, "PyAthena::Aud");
270  MsgStream msg( msgSvc(), name() );
271  msg << MSG::INFO
272  << "could not dyncast component [" << name() << "] to a python "
273  << "object of type [" << this->typeName() << "] (probably a missing "
274  << "dictionary)" << endmsg
275  << "fallback to [PyAthena::Aud]..."
276  << endmsg;
277  }
278  if ( !pyobj ) {
279  PyErr_Clear();
280  MsgStream msg( msgSvc(), name() );
281  msg << MSG::WARNING << "Could not dyncast component ["
282  << name() << "] to a pyobject of type ["
283  << this->typeName() << "]"
284  << endmsg;
285  } else {
286  if ( -1 == PyObject_SetAttrString(o, "_cppHandle", pyobj) ) {
287  PyErr_Clear();
288  MsgStream msg( msgSvc(), name() );
289  msg << MSG::WARNING
290  << "Could not attach C++ handle [" << name() << "] to its python "
291  << "cousin !"
292  << endmsg;
293  if ( -1 == PyObject_SetAttrString(o, "_cppHandle", Py_None) ) {
294  PyErr_Clear();
295  msg << MSG::WARNING
296  << "could not attach a dummy C++ handle [" << name() << "] to its "
297  << "python cousin !"
298  << endmsg;
299  }
300  } else {
301  return true;
302  }
303  }
304  return false;
305 }
306 
307 } //> end namespace PyAthena
plotting.yearwise_luminosity_vs_mu.comp
comp
Definition: yearwise_luminosity_vs_mu.py:24
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:206
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:228
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:260
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
PyAthena::Aud::beforeFinalize
virtual void beforeFinalize(INamedInterface *) override
Definition: PyAthenaAud.cxx:193
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::beforeReinitialize
virtual void beforeReinitialize(INamedInterface *) override
Definition: PyAthenaAud.cxx:169
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::afterReinitialize
virtual void afterReinitialize(INamedInterface *) override
Definition: PyAthenaAud.cxx:175
PyAthena::Aud::sysInitialize
virtual StatusCode sysInitialize() override
Definition: PyAthenaAud.cxx:66
PyAthenaAud.h
PyAthenaUtils.h
PyAthena::Aud::beforeExecute
virtual void beforeExecute(INamedInterface *) override
Definition: PyAthenaAud.cxx:181
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
PyAthena::Aud::beforeInitialize
virtual void beforeInitialize(INamedInterface *) override
Definition: PyAthenaAud.cxx:157
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:123
PyAthena::Aud::afterFinalize
virtual void afterFinalize(INamedInterface *) override
Definition: PyAthenaAud.cxx:199
PyAthena::Aud::before
virtual void before(StandardEventType, INamedInterface *) override
Auditor interface
Definition: PyAthenaAud.cxx:107
PyObject
_object PyObject
Definition: IPyComponent.h:26
PyAthena::Aud::afterInitialize
virtual void afterInitialize(INamedInterface *) override
Definition: PyAthenaAud.cxx:163
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
PyAthena::Aud::afterExecute
virtual void afterExecute(INamedInterface *, const StatusCode &) override
Definition: PyAthenaAud.cxx:187
ServiceHandle
Definition: ClusterMakerTool.h:37