GDML rendering example (CMS)
In this example we demonstrate how to render detector geometries from a GDML file in Geant4. The example uses the CMS detector model and renders a number of logical volumes to a PNG image. The rendering is done using the GLMakie backend of the Makie.jl graphics library.
Limitations of the current implementation:
- The rending is limited to a maximum level of 5 in the geometry hierarchy to avoid excessive mesh generation and rendering times. This can be adjusted with the
maxlevelkeyword argument in thedrawfunction. - Be aware that often the Geant4 boolean processor fails to generate meshes of complex boolean solids. This is noticed by the presence of rendering artifacts when clipping is applied or getting stuck in an infinite mesh generation loop. Reduce the
maxlevelor apply clipping to mitigate this issue.
You can also download this example as a Jupyter notebook and a plain Julia source file.
Loading the necessary Julia modules
Geant4andGeant4.SystemOfUnitsfor the Geant4 interface and unitsGLMakiefor the OpenGL-based rendering backend of Makie
using Geant4
using Geant4.SystemOfUnits
using GLMakieSet the number of rotation steps for polyhedra to get smoother meshes
HepPolyhedron!SetNumberOfRotationSteps(64)
set_theme!(backgroundcolor = :black)Create the detector geometry from a GDML file
The function G4JLDetectorGDML reads a GDML file and creates the corresponding Geant4 geometry. The validate_schema=false option is used to skip GDML schema validation for faster loading.
detname = "cms2018"
detector = G4JLDetectorGDML("$(@__DIR__)/$(detname).gdml"; validate_schema=false);G4GDML: Reading '/home/runner/work/Geant4.jl/Geant4.jl/docs/src/examples/cms2018.gdml'...
G4GDML: Reading definitions...
G4GDML: Reading materials...
G4GDML: Reading solids...
G4GDML: Reading structure...
G4GDML: Reading userinfo...
G4GDML: Reading setup...
G4GDML: Reading '/home/runner/work/Geant4.jl/Geant4.jl/docs/src/examples/cms2018.gdml' done!
Stripping off GDML names of materials, solids and volumes ...
Get the logical volumes to render
CMS = GetVolume("CMSE")[]
tracker = GetVolume("Tracker")[]
muon = GetVolume("MUON")[]
ebar = GetVolume("EBAR")[]Geant4.G4LogicalVolumeDereferenced(Ptr{Nothing}(0x00000000350b2e80))Create and render scenes
- You can adjust the
maxlevelkeyword argument in thedraw!function to control the level of detail in the rendering. - The
clipkeyword argument can be used to apply clipping planes to the geometry for better visibility of internal structures. In this example, we useclip=:xyto clip along the X=0 and Y=0 planes. Other possible values are:x,:y,:zfor single-plane,:xz,:yz,:xyzclipping for double and triple planes, and:nonefor no clipping. - Lighting options and camera position can be adjusted. See the
LightingandCamera3Ddocumentation of Makie.
for lv in (CMS, tracker, muon, ebar)
lvname = GetName(lv) |> String
println("Drawing logical volume: $lvname")
fig = Figure(size=(1080, 800))
ax = LScene(fig[1, 1]; show_axis=false, scenekw=(;
lights=[ PointLight(RGBf(1, 1, 1), Vec3f(1, 1, 0)),
DirectionalLight(RGBf(1, 1, 1), Vec3f(1, 1, 1) ),
AmbientLight(RGBf(.5, .5, .5)),
]))
Camera3D(ax.scene, upvector=Vec3f(0, 1, 0), eyeposition=Vec3f(8m, 8m, 2m), lookat=Vec3f(0, 0, 0))
draw!(ax, lv; maxlevel=5, clip=:xy);
#display(fig)
save("$(detname)_$(lvname).png", fig)
endDrawing logical volume: CMSE
Collecting LV meshes with clip=xy...
1.844655 seconds (6.71 M allocations: 327.525 MiB, 6.76% gc time, 69.74% compilation time)
Drawning 4340 meshes with total of 546726 points in 11 steps.
3.889171 seconds (7.71 M allocations: 1.992 GiB, 28.86% gc time, 70.90% compilation time)
Drawing logical volume: Tracker
Collecting LV meshes with clip=xy...
2.251678 seconds (11.45 M allocations: 567.748 MiB, 29.22% gc time, 36.93% compilation time)
Drawning 3867 meshes with total of 932850 points in 11 steps.
2.382980 seconds (7.03 M allocations: 3.186 GiB, 53.97% gc time)
Drawing logical volume: MUON
Collecting LV meshes with clip=xy...
0.736734 seconds (1.88 M allocations: 90.360 MiB, 4.50% gc time, 80.86% compilation time)
Drawning 4260 meshes with total of 175743 points in 7 steps.
0.726434 seconds (681.62 k allocations: 539.226 MiB, 79.20% gc time)
Drawing logical volume: EBAR
Collecting LV meshes with clip=xy...
3.613589 seconds (28.31 M allocations: 1.394 GiB, 25.94% gc time, 21.81% compilation time)
Drawning 59228 meshes with total of 3383754 points in 6 steps.
5.306935 seconds (19.79 M allocations: 13.008 GiB, 49.34% gc time)
Show the resulting images
CMS detector rendering (maxlevel=5)

Tracker rendering (maxlevel=5)
![]()
Muon System rendering (maxlevel=5)

ECAL Barrel rendering (maxlevel=5)

This page was generated using Literate.jl.