Skip to content

Commit

Permalink
Add python bindings for the datamodel classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Apr 22, 2023
1 parent b0b1fd7 commit f12b794
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
install(DIRECTORY edm4hep DESTINATION python)
1 change: 1 addition & 0 deletions python/edm4hep/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .datamodel import Datamodel
14 changes: 14 additions & 0 deletions python/edm4hep/datamodel.py
Original file line number Diff line number Diff line change
@@ -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}')

0 comments on commit f12b794

Please sign in to comment.