Files
minicalosim/bind/G4Calo.py
T
2023-09-28 11:06:41 +02:00

59 lines
1.8 KiB
Python

from minicalo import ConstructionWrapper
from minicalo import G4System as _G4System
import os
import subprocess
import uproot
from IPython.display import Image, display
class G4System(_G4System):
def run_visualize(
self, particleSpec: str, minEnergy_GeV: float, maxEnergy_GeV: float = -1.0
):
if maxEnergy_GeV < 0:
maxEnergy_GeV = minEnergy_GeV
# anpassen: run 1 event and get stuff from construction wrapper
_G4System.run_visualize(self, particleSpec, minEnergy_GeV, maxEnergy_GeV)
self.displayEvent()
def run_batch(
self,
nEvents: int,
particleSpec: str,
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)
# conversion from root to pandas dataframe
ttree = uproot.open("_1234567890_Hits.root")
df = ttree["Hits;1"].arrays(library="pd")
return df
def displayEvent(self):
f_to_conv = max(
filter(lambda x: x.endswith(".prim"), os.listdir()), key=os.path.getctime
)
# Convert the .prim file to an eps graphic.
subprocess.run(["dawn", "-d", f_to_conv], stderr=subprocess.DEVNULL)
# Convert the eps graphic to png graphic.
subprocess.run(
[
"gs",
"-DEPSCrop",
"-dSAFER",
"-sDEVICE=png256",
"-r600",
"-o",
"event_raw.png",
f_to_conv.replace(".prim", ".eps"),
],
stdout=subprocess.DEVNULL,
)
subprocess.run(["convert", "event_raw.png", "-trim", "event.png"])
display(Image("event.png", width=500))