Rendering GDML geometries

This example renders the CMS (2018) and LHCb (Upgrade) detectors from their GDML files, as the GDML rendering examples of Geant4.jl, here with the drawing functions of Geom4hep and the OpenGL backend of Makie (GLMakie).

  • The volumes are colored by the dominant element of their material, and the volumes of gases and vacuum (air, the envelopes of the subdetectors) are not drawn.
  • maxlevel limits the depth of the hierarchy that is drawn. The calorimeters of LHCb have millions of placements (fibres, cells) deep in the hierarchy.
  • clip=:xy cuts away the quadrant x > 0, y > 0 to see the inside of the detectors. The volumes that cross the quadrant are drawn as their subtraction with a box, computed on their meshes.

The images below are made when the documentation is built: GLMakie needs a display, and the documentation is built on a virtual one (xvfb-run) in the CI.

Note that

You can also download this example as a Jupyter notebook and a plain Julia source file. Run it in the environment of the documentation or of the examples directory of the repository, which have GLMakie (julia --project=examples GDMLRendering.jl). The images are saved in the current directory.

Table of contents

Loading the modules

Makie exports some names that are also Geom4hep shapes (Box, Sphere, ...): only the Makie names that are needed are imported.

using Geom4hep, Geom4hep.Units
using GLMakie: GLMakie, Figure, LScene, save, cam3d!, PointLight, DirectionalLight, AmbientLight, RGBf, Vec3f
using Downloads

Utilities

The names of the volumes in GDML files written by Geant4 have the address of the object appended (e.g. Tracker0x7f4a9a83fe80). find_volume finds a volume of a hierarchy by its name without this suffix, visiting each logical volume once.

function find_volume(top, name)
    seen = Set{Int}()
    stack = [top]
    while !isempty(stack)
        vol = pop!(stack)
        vol.id in seen && continue
        push!(seen, vol.id)
        replace(vol.label, r"0x[0-9a-f]+$" => "") == name && return vol
        append!(stack, (d.volume for d in reverse(vol.daughters)))
    end
    error("volume $name not found")
end
find_volume (generic function with 1 method)

render draws a volume in a figure with a black background, lights, and a camera looking at the center of the volume from a point at a distance proportional to its size (or from eye), and saves the image. See the lighting and 3D camera documentation of Makie.

function render(vol, file; maxlevel, clip=:xy, eye=nothing, lookat=nothing)
    lo, hi = extent(vol.shape)
    center = (lo .+ hi) ./ 2
    lookat = something(lookat, Tuple(center))
    eye = something(eye, Tuple(center .+ maximum(hi .- lo) .* (1.25, 0.85, -0.7)))
    fig = Figure(size=(1080, 800), backgroundcolor=:black)
    scene = LScene(fig[1, 1]; show_axis=false, scenekw=(; backgroundcolor=:black,
                   lights=[PointLight(RGBf(1, 1, 1), Vec3f(1, 1, 0)),
                           DirectionalLight(RGBf(1, 1, 1), Vec3f(1, 1, 1)),
                           AmbientLight(RGBf(0.5, 0.5, 0.5))]))
    cam3d!(scene.scene; eyeposition=Vec3f(eye...), lookat=Vec3f(lookat...), upvector=Vec3f(0, 1, 0), center=false)
    @time draw!(scene, vol; maxlevel, clip)
    save(file, fig; px_per_unit=1)
end
render (generic function with 1 method)

CMS

The GDML file of CMS (2018) is in the examples directory of the package. It is read in single precision, as on GPUs.

cms = read_gdml(joinpath(pkgdir(Geom4hep), "examples", "cms2018.gdml"), Float32)
Volume{Float32} name = OCMS0x7f4a9a758e00

The whole detector (CMSE), to the depth 5 of its hierarchy. Its envelope extends far along the beam (to the forward calorimeters), so the camera is placed explicitly. Clipping takes some time: the volumes that cross the clipped quadrant are cut with boolean operations on their meshes.

render(find_volume(cms, "CMSE"), "cms2018_CMSE.png"; maxlevel=5, eye=(24m, 18m, 10m))
 62.765081 seconds (414.25 M allocations: 416.896 GiB, 24.39% gc time, 8.19% compilation time)

The tracker, the muon system and the barrel of the electromagnetic calorimeter:

for name in ("Tracker", "MUON", "EBAR")
    render(find_volume(cms, name), "cms2018_$name.png"; maxlevel=5)
end
199.221432 seconds (603.68 M allocations: 1.841 TiB, 24.66% gc time)
  1.407200 seconds (16.09 M allocations: 2.402 GiB, 47.70% gc time)
  2.336180 seconds (62.00 M allocations: 2.707 GiB, 13.06% gc time)

The CMS detector

Tracker

Muon system

Barrel of the electromagnetic calorimeter

LHCb

The GDML file of the LHCb detector (Upgrade I, 42 MB) is downloaded once from the repository of Geant4.jl.

url = "https://raw.githubusercontent.com/JuliaHEP/Geant4.jl/master/docs/src/examples/LHCb_Upgrade_fullLHCb.gdml"
lhcbfile = joinpath(tempdir(), "LHCb_Upgrade_fullLHCb.gdml")
isfile(lhcbfile) || Downloads.download(url, lhcbfile)
lhcb = read_gdml(lhcbfile, Float32);

The world volume has the subdetectors placed directly in it, along the beam (z). The whole detector, seen from upstream, to the depth 6:

render(lhcb, "LHCb.png"; maxlevel=6, eye=(12m, 9m, -4m), lookat=(0, 0, 9m))
 37.118737 seconds (642.50 M allocations: 151.365 GiB, 25.48% gc time, 1.05% compilation time)

The vertex locator (VP), the RICH detectors, the upstream tracker (UT) and the muon system. The names of the volumes have the prefix _dd_Geometry_ and the region of the detector.

for (name, maxlevel) in (("BeforeMagnetRegion_VP_lvVP", 4), ("BeforeMagnetRegion_Rich1_lvRich1Master", 7),
                         ("BeforeMagnetRegion_UT_lvUT", 7), ("AfterMagnetRegion_Rich2_lvRich2Master", 7),
                         ("DownstreamRegion_Muon_lvMuonBack", 4))
    render(find_volume(lhcb, "_dd_Geometry_" * name), "LHCb_$(split(name, '_')[end]).png"; maxlevel)
end
 27.884809 seconds (466.99 M allocations: 121.548 GiB, 24.01% gc time)
  0.085419 seconds (1.91 M allocations: 188.518 MiB, 15.49% gc time)
  1.398324 seconds (31.59 M allocations: 1.697 GiB, 29.36% gc time)
  0.131805 seconds (3.53 M allocations: 296.864 MiB)
  1.022876 seconds (21.76 M allocations: 1.081 GiB, 31.05% gc time)

The LHCb detector

Vertex locator

RICH1

Upstream tracker

RICH2

Muon system


This page was generated using Literate.jl.