Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2-3-Compatibility of Python Code #75

Merged
merged 7 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ jobs:
run: |
docker run -it --name CI_container -v /home/runner/work/podio/podio:/Package -e VIEW=${{ matrix.LCG }} -v /cvmfs:/cvmfs -d clicdp/cc7-lcg96b /bin/bash
- name: Run Python Checks
continue-on-error: ${{matrix.LCG == 'LCG_96b/x86_64-centos7-gcc9-opt' }}
run: |
docker exec CI_container /bin/bash -c "./Package/.github/scripts/runPythonChecks.sh"
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ jobs:
run: |
docker run -it --name CI_container -v /home/runner/work/podio/podio:/Package -e VIEW=${{ matrix.LCG }} -v /cvmfs:/cvmfs -d clicdp/cc7-lcg96b /bin/bash
- name: Compile and test
continue-on-error: ${{matrix.LCG == 'LCG_96bpython3/x86_64-centos7-gcc8-opt' }}
run: |
docker exec CI_container /bin/bash -c "./Package/.github/scripts/compile_and_test.sh"
227 changes: 116 additions & 111 deletions python/EventStore.py
Original file line number Diff line number Diff line change
@@ -1,126 +1,131 @@


from __future__ import absolute_import, unicode_literals, print_function

from ROOT import gSystem
gSystem.Load("libpodioRootIO")
gSystem.Load("libpodioRootIO") # noqa: E402
from ROOT import podio


def iterator(self):
'''dynamically added iterator'''
entries = self.size()
for entry in xrange(entries):
yield self.at(entry)
'''dynamically added iterator'''
entries = self.size()
for entry in range(entries):
yield self.at(entry)


def size(self):
return self.size()
return self.size()


def getitem(self, key):
return self.at(key)
return self.at(key)


class EventStore(object):
'''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
'''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, treename=None):
'''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.
treename: not used at the moment
'''
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 += "\n\t%s" % 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 iterator generator to be able to loop on the collection
coll.__iter__ = iterator
# adding length function
coll.__len__ = size
# enabling the use of [] notation on the collection
coll.__getitem__ = getitem
return coll

def isValid(self):
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
else:
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.
'''
def __init__(self, filenames, treename=None):
'''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.
treename: not used at the moment
'''
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 += "\n\t%s" %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 iterator generator to be able to loop on the collection
coll.__iter__ = iterator
# adding length function
coll.__len__ = size
# enabling the use of [] notation on the collection
coll.__getitem__ = getitem
return coll

def isValid(self):
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
else:
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 nev, store in self.stores:
self.current_store = store
for entry in xrange(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, store in self.stores:
nevts_all_files += nev
return nevts_all_files
for nev, store in self.stores:
self.current_store = store
for entry 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, store in self.stores:
nevts_all_files += nev
return nevts_all_files
Loading