Gaudi and Python aware data

From Daya Bay
Jump to navigation Jump to search

You can make classes that are usable by Gaudi and Python. Below shows how to do this.

Overview

Making C++ available in Python in the context of ROOT and Gaudi involves producing so called "dictionaries" (not to be confused with the Python dict type). There are two methods employed by NuWa to generate these dictionaries. There is an automated one (using the GaudiObjDesc package) that is employed for the data model classes. This is not well suited for user code which employs a second method described here.


To use user C++ code in Python requires these basic steps:

  • Write C++ classes and functions to present an interface to the C++ code. this may only expose a sub-set of your code and is best to avoid too much "C++'ism".
  • Write a simple XML file that describes which classes and functions to make available to Python
  • Write a simple C++ header file that includes the headers that are needed to describe the user code
  • Add a line in the package's cmt/requirements file to tell CMT to generate the dictionary library.

These steps are described in more detail and are modeled after what was done for these NuWa packages:

  • Context package exposes a few classes
  • Conventions package exposes many classes, functions and enums.

Write the C++ interface

To be expanded, but in short:

  • avoid using STL containers unless needed.
  • keep interface simple
  • avoid requiring a lot of back-and-forth between C++ and Python

The dict.xml file

There is freedom to name and place this file but many packages that have chosen to place it under:

PackageName/dict/dict.xml

It basically holds a list, in XML format, of classes and functions. For a simple example see Context/dict/dict.xml. A more full example can be found in the Conventions package.

The dict.h file

Again, it can be named in many ways but typically goes in:

PackageName/dict/dict.h

The cmt/requirements file

Add the line:

apply_pattern reflex_dictionary dictionary=Context \
                                selectionfile=${Context_root}/dict/dict.xml \
                                headerfiles=${Context_root}/dict/dict.h

To your package's cmt/requirements file. Note that here you must match the name and location of your XML and header files.

Testing

An example of testing using the Context package

$ ipython
In [1]: import gaudimodule
In [2]: Context = gaudimodule.makeClass('Context')
In [3]: ctx = Context()
In [4]: print ctx.AsString()
Unknown, Unknown, Thu, 01 Jan 1970 00:00:00 +0000 (GMT) +        0 nsec, Unknown