From f12b7940c991e40d7534a468ef0197f030a4d260 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Sat, 22 Apr 2023 14:34:18 +0200 Subject: [PATCH] Add python bindings for the datamodel classes --- CMakeLists.txt | 1 + README.md | 22 ++++++++++++++++++---- python/CMakeLists.txt | 1 + python/edm4hep/__init__.py | 1 + python/edm4hep/datamodel.py | 14 ++++++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 python/CMakeLists.txt create mode 100644 python/edm4hep/__init__.py create mode 100644 python/edm4hep/datamodel.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a40859ae..c0cc5f1b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,7 @@ add_subdirectory(edm4hep) add_subdirectory(utils) add_subdirectory(test) add_subdirectory(tools) +add_subdirectory(python) #--- create uninstall target --------------------------------------------------- include(cmake/EDM4HEPUninstall.cmake) diff --git a/README.md b/README.md index a0f69f31a..d3a43c4af 100644 --- a/README.md +++ b/README.md @@ -72,10 +72,24 @@ make test The library files and dictionaries (`libedm4hep.so`, ...) must be put somewhere in `LD_LIBRARY_PATH`. - - - - +## Python bindings +There are python bindings for all the classes in the datamodel. Make sure that +`install/lib` is in `LD_LIBRARY_PATH` and `install/python` is in `PYTHONPATH`: +```python +from edm4hep import Datamodel +model = Datamodel() # factory class to provide dynamically loaded classes +particle = model.MCParticle() # default initialized particle +particle.getCharge() # 0.0 + +series = model.TimeSeries(1, 2, 3) # classes can be constructed with non-default parameters +series.getCellID() # 1 +series.getTime() # 2.0 +series.getInterval() # 3.0 + +mc = model.MutableMCParticle() # mutable classes can also be modified +mc.setPDG(2212) +mc.getPDG() # 2212 +``` ## Contributing diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt new file mode 100644 index 000000000..1aac87e08 --- /dev/null +++ b/python/CMakeLists.txt @@ -0,0 +1 @@ +install(DIRECTORY edm4hep DESTINATION python) diff --git a/python/edm4hep/__init__.py b/python/edm4hep/__init__.py new file mode 100644 index 000000000..7cbe77d91 --- /dev/null +++ b/python/edm4hep/__init__.py @@ -0,0 +1 @@ +from .datamodel import Datamodel diff --git a/python/edm4hep/datamodel.py b/python/edm4hep/datamodel.py new file mode 100644 index 000000000..c143c0b67 --- /dev/null +++ b/python/edm4hep/datamodel.py @@ -0,0 +1,14 @@ +import importlib +import ROOT +ROOT.gSystem.Load('libedm4hepDict.so') + + +class Datamodel: + def __init__(self): + self.edm4hep = importlib.import_module('ROOT').edm4hep + + def __getattr__(self, name): + try: + return getattr(self.edm4hep, name) + except AttributeError: + raise AttributeError(f'EDM4hep has no class named {name}')