Skip to content

DigitizerHitsCollectionActor: ROOT file not finalized with complex phantom and multiple detector modules #1035

@salif-git

Description

@salif-git

Good Day

I'm finalizing a work on the VERITON-CT, by a monte carlo simulation and i had an issue with the root who are not finalizing the writing. we have describe the issue on this file.
Bug_opengate_ROOT_pour_developpeur_Cpp.docx

["""
Minimal reproducible example for DigitizerHitsCollectionActor ROOT file bug.
opengate version: 10.1.0
Python: 3.13
OS: Ubuntu 24.04

Bug: ROOT file generated by DigitizerHitsCollectionActor is not properly
finalized when the simulation includes a complex phantom (TubsVolume +
SphereVolume) and/or multiple detector modules.
The file header shows End=252 regardless of actual file size.
uproot.open(file).keys() returns [].

Expected: End == file size, uproot reads data correctly.
"""

import opengate as gate
import uproot
import struct
import os

mm = gate.g4_units.mm
keV = gate.g4_units.keV
Bq = gate.g4_units.Bq
MBq = Bq * 1e6
g_cm3 = gate.g4_units.g_cm3
sec = gate.g4_units.s

os.makedirs("test_bug_report", exist_ok=True)

sim = gate.Simulation()
sim.verbose_level = "WARNING"
sim.number_of_threads = 2
sim.output_dir = "test_bug_report"

# CZT material
mat_db = sim.volume_manager.material_database
mat_db.add_material_nb_atoms("CZT", ["Cd", "Zn", "Te"], [9, 1, 10], 5.78 * g_cm3)

# World
world = sim.world
world.size = [600 * mm, 600 * mm, 600 * mm]
world.material = "G4_AIR"

# Single CZT detector
czt = sim.add_volume("BoxVolume", "CZT_0")
czt.size = [35 * mm, 5 * mm, 315 * mm]
czt.material = "CZT"
czt.translation = [185 * mm, 0, 0]

# === NEMA IQ phantom (causes the bug) ===
cyl = sim.add_volume("TubsVolume", "NEMA_cylinder")
cyl.rmin = 0
cyl.rmax = 100 * mm
cyl.dz = 90 * mm
cyl.material = "G4_WATER"
cyl.translation = [0, 0, 0]

sph = sim.add_volume("SphereVolume", "NEMA_sphere_22mm")
sph.rmin = 0
sph.rmax = 11 * mm
sph.material = "G4_WATER"
sph.mother = "NEMA_cylinder"
sph.translation = [0, 57 * mm, 0]

# DigitizerHitsCollectionActor
hits = sim.add_actor("DigitizerHitsCollectionActor", "Hits")
hits.attached_to = "CZT_0"
hits.attributes = ["KineticEnergy", "PostPosition"]
hits.output_filename = "hits_bug_test.root"

# Source in phantom
src = sim.add_source("GenericSource", "src")
src.particle = "gamma"
src.energy.mono = 140.5 * keV
src.activity = 0.1 * MBq
src.direction.type = "iso"
src.position.type = "sphere"
src.position.radius = 11 * mm
src.position.translation = [0, 57 * mm, 0]

sim.physics_manager.physics_list_name = "G4EmStandardPhysics_option4"
sim.run_timing_intervals = [[0, 10 * sec]]
sim.run()

# Check ROOT file
fname = "test_bug_report/hits_bug_test.root"
fsize = os.path.getsize(fname)
with open(fname, "rb") as f:
    end_value = struct.unpack(">i", f.read(20)[12:16])[0]

print(f"File size : {fsize} bytes")
print(f"ROOT End  : {end_value} bytes")
print(f"Keys      : {uproot.open(fname).keys()}")

if end_value == 252:
    print("\nBUG CONFIRMED: ROOT file not finalized (End=252)")
    print("Expected: End == file size and Keys non-empty")
else:
    print("\nOK: ROOT file correctly finalized")]

Is there something you can do for fixing this issue.

BR//

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions