Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix crash when a writer is in the global namespace #539

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Changes from 4 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
26 changes: 26 additions & 0 deletions python/podio/root_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Python module for reading root files containing podio Frames"""

import atexit
from ROOT import gSystem
gSystem.Load('libpodioRootIO') # noqa: E402
from ROOT import podio # noqa: E402 # pylint: disable=wrong-import-position
Expand All @@ -9,6 +10,29 @@
from podio.base_writer import BaseWriterMixin # pylint: disable=wrong-import-position


class AllWriters:
"""Class to manage all writers in the program
so that they can be properly finished at the end of the program
"""
writers = []

def add(self, writer):
"""Add a writer to the list of managed writers"""
self.writers.append(writer)

def finish(self):
"""Finish all managed writers"""
for writer in self.writers:
try:
writer._writer.finish() # pylint: disable=protected-access
except AttributeError:
pass


_all_writers = AllWriters()
atexit.register(_all_writers.finish)


class Reader(BaseReaderMixin):
"""Reader class for reading podio root files."""

Expand Down Expand Up @@ -77,6 +101,7 @@ def __init__(self, filename):
filename (str): The name of the output file
"""
self._writer = podio.ROOTFrameWriter(filename)
_all_writers.add(self)


class RNTupleWriter(BaseWriterMixin):
Expand All @@ -88,3 +113,4 @@ def __init__(self, filename):
filename (str): The name of the output file
"""
self._writer = podio.ROOTNTupleWriter(filename)
_all_writers.add(self)