Advanced tutorials
Finding volumes
Before editing geometry it is useful to find a logical volume. The registry contains all the logical volumes
using the GDML in pyg4ometry/pyg4ometry/test/gdmlG4examples/ChargeExchangeMC/
1import pyg4ometry
2r = pyg4ometry.gdml.Reader("lht.gdml")
3reg = r.getRegistry()
The registry
instance reg
has a member variable called logicalVolumeDict
so calling
reg.logicalVolumeDict.keys()
should print
In [4]: reg.logicalVolumeDict.keys()
Out[4]: odict_keys(['vMonitor', 'vMonitorBack', 'vTarget', 'vTargetInnerCover', 'vTargetColumn', 'vTargetInnerColumn',
'vTargetVacuumSpace', 'vTargetOuterCover', 'vCrystal', 'vCrystalRow', 'vCalorimeter', 'vVetoCounter',
'vOuterFerrumRing', 'vInnerFerrumRing', 'vInnerCuprumRing', 'vTargetWindow', 'vTargetWindowCap',
'vTargetWindowMylarCover', 'vTargetWindowAluminiumCover', 'vWorldVisible', 'World'])
then the LogicalVolume can be obtained simply from the dictionary
lv = reg.logicalVolumeDict['vTargetInnerColumn']
This lv
can be used for manipulating geometry, passing to visualisers etc.
Edit existing geometry
After loading some geometry it is possible to modify the memory resident geometry. This could adjusting the parameter of a given solid or PV, or replacing entirely the type of solid used for an LV. To edit geometry a LV instance is required
Complex geometry builder
Having access to geometry construction in python allows the rapid construction of
geometry using functions which return an appropriate LV. Examples of this available in
pyg4ometry/pyg4ometry/test/pythonCompoundExamples
Fluka geometry scripting
In a very similar way to geant4 geometry authoring it is possible to use pyg4ometry to create fluka output. To create a simple region consisting of a single body
1import pyg4ometry.convert as convert
2import pyg4ometry.visualisation as vi
3from pyg4ometry.fluka import RPP, Region, Zone, FlukaRegistry
4
5freg = FlukaRegistry()
6
7rpp = RPP("RPP_BODY", 0, 10, 0, 10, 0, 10, flukaregistry=freg)
8z = Zone()
9z.addIntersection(rpp)
10region = Region("RPP_REG", material="COPPER")
11region.addZone(z)
12freg.addRegion(region)
13
14greg = convert.fluka2Geant4(freg)
15greg.getWorldVolume().clipSolid()
16
17v = vi.VtkViewer()
18v.addAxes(length=20)
19v.addLogicalVolume(greg.getWorldVolume())
20v.view()
Export scene to paraview/vtk
1import pyg4ometry
Export scene to unity/unreal
The quickest way to get geometry to Unity/Unreal is to use a standard asset
format. This takes a vtkRenderer and creates a OBJ file. The vtkRenderer
managed within pyg4ometry from the vtkViewer class, once a geometry is created
(either from any source) then an OBJ file can be created. Taking the
example in pyg4ometry/pyg4ometry/test/pythonCompoundExamples/
1import pyg4ometry
2r = pyg4ometry.gdml.Reader("./Chamber.gdml")
3l = r.getRegistry().getWorldVolume()
4v = pyg4ometry.visualisation.VtkViewer()
5v.addLogicalVolume(l)
6v.exportOBJScene("Chamber")
obj
files are written Chamber.obj
and Chamber.mtl
.
For a Fluka file, first it must be converted to geant4 and then the same process should be followed.
1import pyg4ometry
2r = pyg4ometry.fluka.Reader("./Chamber.inp")
3greg = pyg4ometry.convert.fluka2geant4(r.getRegistry())
4l = greg.getWorldVolume()
5v = pyg4ometry.visualisation.VtkViewer()
6v.addLogicalVolume(l)
7v.exportOBJScene("Chamber")
As the meshing might need to changed for the visualisation application, the parameters for the meshing for each solid might need to changed.
An obj
file for an entire experiment does not help with work flows where meshes
have to be UV-ed and textured. Tools like Blender and Gaffer can be used for this workload
but require meshes for each object and their placement. To enable there is a special
writer
1import pyg4ometry
2r = pyg4ometry.gdml.Reader("./Chamber.gdml")
3l = r.getRegistry().getWorldVolume()
4w = pyg4ometry.visualisation.RenderWriter()
5w.addLogicalVolumeRecursive(l)
6w.write("./SphericalChamber")
The directory SphericalChamber
contains all the meshes in OBJ format along
with an instance file 0_instances.dat
which contains a row for each
instance of a mesh.