Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:04:25

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 //  \author Markus Frank
0011 //  \date   2015-11-07
0012 //
0013 //==========================================================================
0014 #ifndef DDG4_PYTHON_DDPYTHON_H
0015 #define DDG4_PYTHON_DDPYTHON_H 1
0016 
0017 // C/C++ include files
0018 #include <string>
0019 
0020 // ROOT include file(s)
0021 #include <TPyReturn.h>
0022 
0023 /// Namespace for the AIDA detector description toolkit
0024 namespace dd4hep  {
0025 
0026   struct DDPythonGlobalState;
0027 
0028   /// Python interface class for callbacks and GIL.
0029   /**
0030    *  \author  M.Frank
0031    *  \version 1.0
0032    *  \ingroup DD4HEP_SIMULATION
0033    */
0034   class DDPython {
0035   protected:
0036     void* context;
0037     /// Standard constructor
0038     DDPython( ); 
0039   protected:
0040     static bool isMainThread();
0041   public:
0042     struct GILState {
0043       int state;
0044       explicit GILState(int);
0045       ~GILState();
0046     };
0047     struct BlockThreads {
0048       explicit BlockThreads(int);
0049       ~BlockThreads();
0050     };
0051     struct AllowThreads {
0052       explicit AllowThreads(int);
0053       ~AllowThreads();
0054     };
0055 
0056     /// Save thread state
0057     static void allowThreads();
0058     static void restoreThread();
0059 
0060     /// Object instantiator
0061     static DDPython instance();
0062     static void shutdown();
0063     static void setMainThread();
0064 
0065     /// Release python object
0066     static void releaseObject(PyObject*& obj);
0067 
0068     /// Release python object
0069     static void assignObject(PyObject*& obj, PyObject* new_obj);
0070 
0071     /// Start the interpreter in normal mode without hacks like 'python.exe' does.
0072     static int run_interpreter(int argc, char** argv);
0073 
0074     /// Copy constructor 
0075     DDPython(const DDPython& ) {}
0076 
0077     /// Destructor
0078     ~DDPython( );
0079 
0080     int  setArgs(int argc, char** argv)  const;
0081     int  runFile(const std::string& fname)  const;
0082     int  execute(const std::string& cmd)  const;
0083     int  evaluate(const std::string& cmd)  const;
0084 
0085     /// Call a python object with argument (typically a dictionary)
0086     /** Note:
0087      *  - Typical call from python -> C -> python
0088      *  - Errors are printed and then cleared. A return code of NULL is passed
0089      *    to the caller in the event of an error condition
0090      *  - The returned object is NOT owned by the caller.
0091      *  - No GIL state handling by the caller necessary
0092      */
0093     PyObject* call(PyObject* method, PyObject* args);
0094 
0095     /// Call a python object with argument (typically a dictionary). 
0096     /** Note:
0097      *  - Typical call from C -> python -> C
0098      *  - Errors are printed and then cleared. A return code of NULL is passed
0099      *    to the caller in the event of an error condition
0100      *  - The returned object TPyReturn must be destructed (ie. leave scope)
0101      *    BEFORE the GIL is released.
0102      *  - The caller MUST ensure the GIL state in case of multi-threading!
0103      */
0104     TPyReturn callC(PyObject* method, PyObject* args);
0105 
0106     /// Invoke command prompt
0107     void prompt()  const;
0108 
0109     /// Callback after forking.
0110     void afterFork()  const;
0111 
0112   private:
0113 
0114   };
0115 }
0116 
0117 #endif // DDG4_PYTHON_DDPYTHON_H