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 maxlevel keyword argument in the draw function.
  • 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 maxlevel or apply clipping to mitigate this issue.
Note that

You can also download this example as a Jupyter notebook and a plain Julia source file.

Loading the necessary Julia modules

  • Geant4 and Geant4.SystemOfUnits for the Geant4 interface and units
  • GLMakie for the OpenGL-based rendering backend of Makie
using Geant4
using Geant4.SystemOfUnits
using GLMakie

Set 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}(0x0000000009bf17b0))

Create and render scenes

  • You can adjust the maxlevel keyword argument in the draw! function to control the level of detail in the rendering.
  • The clip keyword argument can be used to apply clipping planes to the geometry for better visibility of internal structures. In this example, we use clip=:xy to clip along the X=0 and Y=0 planes. Other possible values are :x, :y, :z for single-plane, :xz, :yz, :xyz clipping for double and triple planes, and :none for no clipping.
  • Lighting options and camera position can be adjusted. See the Lighting and Camera3D documentation 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)
end
Drawing logical volume: CMSE
Collecting LV meshes with clip=xy...
  1.900582 seconds (6.70 M allocations: 327.548 MiB, 7.49% gc time, 67.53% compilation time)
Drawning 4340 meshes with total of 546726 points in 11 steps.
  3.687716 seconds (7.72 M allocations: 1.979 GiB, 29.08% gc time, 74.08% compilation time)
Drawing logical volume: Tracker
Collecting LV meshes with clip=xy...
  2.167881 seconds (10.70 M allocations: 556.275 MiB, 27.39% gc time, 38.76% compilation time)
Drawning 3867 meshes with total of 932850 points in 11 steps.
  2.057844 seconds (7.81 M allocations: 3.607 GiB, 62.16% gc time)
Drawing logical volume: MUON
Collecting LV meshes with clip=xy...
  0.739891 seconds (1.88 M allocations: 90.351 MiB, 4.49% gc time, 81.10% compilation time)
Drawning 4260 meshes with total of 175743 points in 7 steps.
  0.668875 seconds (679.75 k allocations: 512.342 MiB, 82.06% gc time)
Drawing logical volume: EBAR
Collecting LV meshes with clip=xy...
  3.548053 seconds (27.87 M allocations: 1.388 GiB, 24.61% gc time, 21.05% compilation time)
Drawning 59228 meshes with total of 3383754 points in 6 steps.
  5.183092 seconds (20.23 M allocations: 12.840 GiB, 49.53% 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.