-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make python code a proper module * Reorganize CMake config for shared libraries * Refactor library and dict generation in cmake config * Add basic version of python bindings for working with Frames
- Loading branch information
Showing
38 changed files
with
909 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,3 +61,4 @@ spack* | |
|
||
# Populated by cmake before build | ||
/include/podio/podioVersion.h | ||
/python/podio/__init__.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,6 @@ | ||
"""Python EventStore for reading files with podio generated datamodels""" | ||
"""Legacy import wrapper for EventStore.""" | ||
|
||
import warnings | ||
warnings.warn("You are using the legacy EventStore import. Switch to 'from podio import EventStore'", FutureWarning) | ||
|
||
from ROOT import gSystem | ||
gSystem.Load("libpodioPythonStore") # noqa: E402 | ||
from ROOT import podio # noqa: E402 # pylint: disable=wrong-import-position | ||
|
||
|
||
def size(self): | ||
"""Override size function that can be attached as __len__ method to | ||
collections""" | ||
return self.size() | ||
|
||
|
||
def getitem(self, key): | ||
"""Override getitem function that can be attached as __getitem__ method to | ||
collections (see below why this is necessary sometimes)""" | ||
return self.at(key) | ||
|
||
|
||
class EventStore: | ||
'''Interface to events in an podio root file. | ||
Example of use: | ||
events = EventStore(["example.root", "example1.root"]) | ||
for iev, store in islice(enumerate(events), 0, 2): | ||
particles = store.get("GenParticle"); | ||
for i, p in islice(enumerate(particles), 0, 5): | ||
print "particle ", i, p.ID(), p.P4().Pt | ||
''' | ||
|
||
def __init__(self, filenames): | ||
'''Create an event list from the podio root file. | ||
Parameters: | ||
filenames: list of root files | ||
you can of course provide a list containing a single | ||
root file. you could use the glob module to get all | ||
files matching a wildcard pattern. | ||
''' | ||
if isinstance(filenames, str): | ||
filenames = (filenames,) | ||
self.files = filenames | ||
self.stores = [] | ||
self.current_store = None | ||
for fname in self.files: | ||
store = podio.PythonEventStore(fname) | ||
if store.isZombie(): | ||
raise ValueError(fname + ' does not exist.') | ||
store.name = fname | ||
if self.current_store is None: | ||
self.current_store = store | ||
self.stores.append((store.getEntries(), store)) | ||
|
||
def __str__(self): | ||
result = "Content:" | ||
for item in self.current_store.getCollectionNames(): | ||
result += f"\n\t{item}" | ||
return result | ||
|
||
def get(self, name): | ||
'''Returns a collection. | ||
Parameters: | ||
name: name of the collection in the podio root file. | ||
''' | ||
coll = self.current_store.get(name) | ||
# adding length function | ||
coll.__len__ = size | ||
# enabling the use of [] notation on the collection | ||
# cppyy defines the __getitem__ method if the underlying c++ class has an operator[] | ||
# method. For some reason they do not conform to the usual signature and only | ||
# pass one argument to the function they call. Here we simply check if we have to | ||
# define the __getitem__ for the collection. | ||
if not hasattr(coll, '__getitem__'): | ||
coll.__getitem__ = getitem | ||
return coll | ||
|
||
def collections(self): | ||
"""Return list of all collection names.""" | ||
return [str(c) for c in self.current_store.getCollectionNames()] | ||
|
||
def metadata(self): | ||
"""Get the metadata of the current event as GenericParameters""" | ||
return self.current_store.getEventMetaData() | ||
|
||
def isValid(self): | ||
"""Check if the EventStore is in a valid state""" | ||
return self.current_store is not None and self.current_store.isValid() | ||
|
||
# def __getattr__(self, name): | ||
# '''missing attributes are taken from self.current_store''' | ||
# if name != 'current_store': | ||
# return getattr(self.current_store, name) | ||
# else: | ||
# return None | ||
|
||
def current_filename(self): | ||
'''Returns the name of the current file.''' | ||
if self.current_store is None: | ||
return None | ||
return self.current_store.fname | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exception_type, exception_val, trace): | ||
for store in self.stores: | ||
store[1].close() | ||
|
||
def __iter__(self): | ||
'''iterate on events in the tree. | ||
''' | ||
for _, store in self.stores: | ||
self.current_store = store | ||
for _ in range(store.getEntries()): | ||
yield store | ||
store.endOfEvent() | ||
|
||
def __getitem__(self, evnum): | ||
'''Get event number evnum''' | ||
current_store = None | ||
rel_evnum = evnum | ||
for nev, store in self.stores: | ||
if rel_evnum < nev: | ||
current_store = store | ||
break | ||
rel_evnum -= nev | ||
if current_store is None: | ||
raise ValueError('event number too large: ' + str(evnum)) | ||
self.current_store = current_store | ||
self.current_store.goToEvent(rel_evnum) | ||
return self | ||
|
||
def __len__(self): | ||
'''Returns the total number of events in all files.''' | ||
nevts_all_files = 0 | ||
for nev, _ in self.stores: | ||
nevts_all_files += nev | ||
return nevts_all_files | ||
from podio import EventStore # noqa: F401 # pylint: disable=wrong-import-position, unused-import |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = '@podio_VERSION@' |
Oops, something went wrong.