PyWrap: Difference between revisions
(Created page with "==Introduction== In some cases, it would be helpful to use a piece of c++ Offline code in a python script. A good example is importing the enums from Offline into a python an...") |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 3: | Line 3: | ||
==Method== | ==Method== | ||
The wrappers are implemented using [http://www.swig.org/Doc4.0/SWIGDocumentation.html#SWIGPlus_nn2 swig]. This freeware package can interpret a c++ class header file and produce a piece of c++ code with hooks into the class, and piece of python code which can present the class to the user as a python class. | The wrappers are implemented using [http://www.swig.org/Doc4.0/SWIGDocumentation.html#SWIGPlus_nn2 swig]. This freeware package can interpret a c++ class header file and produce a piece of c++ code with hooks into the class, and piece of python code which can use the hooks to present the c++ class to the user as a python class. | ||
To | To create a wrapper, there must be a file, <code>pywrap.i</code> in the src (not inc) directory where the source class is created. For example | ||
Offline/MCDataProducts/src/pywrap.i | Offline/MCDataProducts/src/pywrap.i | ||
This file is | This file is swig interface file, and specifies the class to be wrapped. The content of this file is a bit abstruse, and in order to try to reproduce the features of c++ in python, it can get complicated. The [http://www.swig.org/Doc4.0/SWIGDocumentation.html#SWIGPlus_nn2 swig] documentation is complete and well-written, and interface content can be developed as needed. It is not unusual that one or two methods of the class will work, but maybe some don't and might require more work. | ||
The creation of the wrapper is triggered by building with the switch | The creation of the wrapper is triggered by building with the switch | ||
Line 18: | Line 18: | ||
and Muse will add this directory to the PYTHONPATH | and Muse will add this directory to the PYTHONPATH | ||
To use the wrapper, after "muse setup" of a build with the | To use the wrapper, after "muse setup" of a build with the wrappers, | ||
>>> import MCDataProducts | >>> import MCDataProducts | ||
>>> gid=MCDataProducts.GenId() | >>> gid=MCDataProducts.GenId() | ||
Line 33: | Line 33: | ||
>>> pc.findByName('Decay').id() | >>> pc.findByName('Decay').id() | ||
14 | 14 | ||
>>> | |||
>>> import DbService | |||
>>> dd=DbService.DbTool() | |||
>>> dd.printPurpose() | |||
>>> print( dd.getResult() ) | |||
PID create_user create_date comment | |||
PID 1 TEST rlc 2018-10-12 08:58:26.792518-05:00 testing | |||
PID 2 EMPTY rlc 2018-10-12 08:58:26.797844-05:00 no tables |
Latest revision as of 23:51, 29 March 2024
Introduction
In some cases, it would be helpful to use a piece of c++ Offline code in a python script. A good example is importing the enums from Offline into a python analysis routine, so numbers recorded in an ntuple could be interpreted.
Method
The wrappers are implemented using swig. This freeware package can interpret a c++ class header file and produce a piece of c++ code with hooks into the class, and piece of python code which can use the hooks to present the c++ class to the user as a python class.
To create a wrapper, there must be a file, pywrap.i
in the src (not inc) directory where the source class is created. For example
Offline/MCDataProducts/src/pywrap.i
This file is swig interface file, and specifies the class to be wrapped. The content of this file is a bit abstruse, and in order to try to reproduce the features of c++ in python, it can get complicated. The swig documentation is complete and well-written, and interface content can be developed as needed. It is not unusual that one or two methods of the class will work, but maybe some don't and might require more work.
The creation of the wrapper is triggered by building with the switch
muse build -j 20 --mu2ePyWrap
The wrapper c code will land
build/.../Offline/tmp/MCDataProducts/src/pywrap/MCDataProducts_wrap.cpp
and the wrapper will land
build/.../Offline/pywrap/MCDataProducts.py build/.../Offline/pywrap/_MCDataProducts.so
and Muse will add this directory to the PYTHONPATH
To use the wrapper, after "muse setup" of a build with the wrappers,
>>> import MCDataProducts >>> gid=MCDataProducts.GenId() >>> gid.name(1) 'particleGun' >>> gid.findByName('particleGun').id() 1 >>> gid.particleGun 1 >>> >>> pc=MCDataProducts.ProcessCode() >>> pc.name(14) 'Decay' >>> pc.findByName('Decay').id() 14 >>> >>> import DbService >>> dd=DbService.DbTool() >>> dd.printPurpose() >>> print( dd.getResult() )
PID create_user create_date comment PID 1 TEST rlc 2018-10-12 08:58:26.792518-05:00 testing PID 2 EMPTY rlc 2018-10-12 08:58:26.797844-05:00 no tables