From 5ebcf963fc56d2070ce85f42ce2ad5780df2c507 Mon Sep 17 00:00:00 2001 From: lars Date: Fri, 13 Oct 2023 15:55:27 +0200 Subject: [PATCH] unique filenames, circumvent Geant thread in filename, del root& save pkl --- bind/G4Calo.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/bind/G4Calo.py b/bind/G4Calo.py index 385ae4d..eca3820 100644 --- a/bind/G4Calo.py +++ b/bind/G4Calo.py @@ -5,8 +5,10 @@ import plotly.graph_objects as go import pandas as pd import numpy as np import os +import glob import subprocess import uproot +import time from IPython.display import Image, display @@ -21,18 +23,39 @@ class __G4System(_G4System): minEnergy_GeV: float, maxEnergy_GeV: float = -1.0, ): + if maxEnergy_GeV < 0: maxEnergy_GeV = minEnergy_GeV - _G4System.run_batch(self, nEvents, particleSpec, minEnergy_GeV, maxEnergy_GeV) + + # filename without file ending(!) + filename = "_" + str(time.perf_counter_ns()) + ".root" + _G4System.run_batch(self, nEvents, particleSpec, minEnergy_GeV, maxEnergy_GeV, filename=filename) + + # TO FIX: Geant4 adds "t" to the filename, circumvent this for one thread, but this is not a good solution + file = glob.glob(filename.replace(".root", "*.root")) + print(file) + if len(file)>1: + raise Exception("More than one file found! - Check mulithreading") + else: + filename = file[0] # conversion from root to pandas dataframe - ttree = uproot.open("_1234567890_Hits.root") + + # TO FIX: circumvent index out of bounds error + ttree = uproot.open(filename) try: df = ttree["Hits;1"].arrays(library="pd") except: df = index_out_of_bounds_workaround(ttree["Hits;1"]) + + # save to pickle and delete root file + df.to_pickle(filename.replace(".root", ".pkl")) + os.system("rm "+filename) + return df + + def displayEvent(self, logE = False, renderer=None): # for loop over all layers to_plot = []