Combining Geometry

There are ways to incorporate geometry from multiple sources in GDML. This has potentially lots of problems as each file needs to be a well formed GDML file and care has to be taken with degenerate names from the different sources. For example a volume can be extracted from GdmlFile1 and added to GdmlFile2, clearly all solids, materials and variables need to also transferred. For this example we need two GDML files, so pyg4ometry/test/pythonGeant4/T001_Box.py and pyg4ometry/test/pythonGeant4/T002_Tubs.py, so run them

1import T001_Box
2T001_Box.Test(True,True)
3
4import T002_Tubs
5T002_Tubs(True,True)

This will create two GDML files T001_Box.gdml and T002_Tubs.gdml. It is possible to find the volumes contained in each file (using the tubs gdml file as the example) by performing the following

 1import pyg4ometry
 2r = pyg4ometry.gdml.Reader("T002_Tubs.gdml")
 3reg = r.getRegistry()
 4
 5# printing the names of the logical volumes
 6print(reg.logicalVolumeDict.keys())
 7
 8# printing the names of the physical volumes
 9print(reg.physicalVolumeDict.keys())
10
11lv = reg.logicalVolume["tl"]

Now merging the tl logicalVolume (which is a simple tubs) with the box gdml file

 1import pyg4ometry
 2r1 = pyg4ometry.gdml.Reader("T001_Box.gdml")
 3reg1 = r1.getRegistry()
 4
 5r2 = pyg4ometry.gdml.Reader("T002_Tubs.gdml")
 6reg2 = r2.getRegistry()
 7
 8lv = reg2.logicalVolumeDict["tl"]
 9
10# create physical volume with placement
11pv = pyg4ometry.geant4.PhysicalVolume([0,0,0],[50,0,0], lv, "tl_pv", reg1.getWorldVolume(), reg1)
12
13reg1.addVolumeRecursive(pv)
14
15# gdml output
16w = pyg4ometry.gdml.Writer()
17w.addDetector(reg1)
18w.write("MergeRegistry.gdml")

Note

In the example two registry objects are created and objects from reg2 are merged into reg1. Of course one registry might be formed by pyg4ometry commands opposed created from a file.

Warning

The pv needs to added with addVolumeRecursive otherwise it is possible that GDML definitions which lv depends on are not transferred over.