From 48c03919784b77023f9418b6cae2e6d3158d14e0 Mon Sep 17 00:00:00 2001 From: lars Date: Sun, 8 Oct 2023 10:21:06 +0200 Subject: [PATCH] index out of bounds workaround --- bind/G4Calo.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/bind/G4Calo.py b/bind/G4Calo.py index 90780ef..d978d44 100644 --- a/bind/G4Calo.py +++ b/bind/G4Calo.py @@ -2,6 +2,7 @@ from minicalo import GeometryDescriptor from minicalo import G4System as _G4System import plotly.graph_objects as go +import pandas as pd import numpy as np import os import subprocess @@ -26,7 +27,10 @@ class __G4System(_G4System): # conversion from root to pandas dataframe ttree = uproot.open("_1234567890_Hits.root") - df = ttree["Hits;1"].arrays(library="pd") + try: + df = ttree["Hits;1"].arrays(library="pd") + except: + df = index_out_of_bounds_workaround(ttree["Hits;1"]) return df def displayEvent(self): @@ -240,4 +244,21 @@ col_dict = { "G4_BRASS": 'slategrey', } +def index_out_of_bounds_workaround(tbranch): + # ugly workaround for index out of bounds error + dfs = [] + entries = tbranch['sensor_energy'].num_entries + + # check each entry + for entry in range(entries): + try: + df = tbranch.arrays(entry_start=entry,entry_stop=entry+1, library='pd') + dfs.append(df) + except: + continue + + df = pd.concat(dfs) + return df.reset_index(drop=True) + + G4System = __G4System()#singleton instance \ No newline at end of file