pyg4ometry.gdml

Subpackages

Submodules

Classes

Reader

Read a GDML file.

_Material

This class provides an interface to GDML material definitions.

_Element

This class provides an interface to GDML material definitions. Because of the different options

_Isotope

This class that handles isotopes as components of composite materials. An element can be

Writer

BasicExpression

Holds an expression as a string and can use the expression parser

DefineBase

Common bits for a define. Must have a name and a registry. Adding

ScalarBase

Base class for all scalars (Constants, Quantity, Variable and 'Expression')

Constant

GDML constant define wrapper object

Quantity

GDML quantity define wrapper object

Variable

GDML variable define wrapper object

Expression

General expression, does not have an analogue in GDML

VectorBase

Position

GDML position define wrapper object

Rotation

GDML rotation define wrapper object

Scale

GDML scale define wrapper object

Matrix

GDML matrix define wrapper object

Auxiliary

Auxiliary information container object

Functions

isComment(node)

_StripPointer(name)

upgradeToStringExpression(reg, obj)

Take a float, str, ScalarBase and return string expression.

evaluateToFloat(reg, obj)

upgradeToExpression(reg, obj)

Helper functions that takes a string and returns an expression object or a string

upgradeToVector(var, reg[, type, unit, addRegistry])

Take a list [x,y,z] and create a vector

upgradeToTransformation(var, reg[, addRegistry])

Take a list of lists [[rx,ry,rz],[x,y,z]] and create a transformation [Rotation,Position]

operationReturnType(name, strExpr, v1, v2, type1, ...)

sin(arg)

Sin of a ScalarBase object, returns a Constant

cos(arg)

Cosine of a ScalarBase object, returns a Constant

tan(arg)

Tangent of a ScalarBase object, returns a Constant

asin(arg)

ArcSin of a ScalarBase object, returns a Constant

acos(arg)

ArcCos of a ScalarBase object, returns a Constant

atan(arg)

ArcTan of a ScalarBase object, returns a Constant

exp(arg)

Exponential of a ScalarBase object, returns a Constant

log(arg)

Natural logarithm of a ScalarBase object, returns a Constant

log10(arg)

Base 10 logarithm of a ScalarBase object, returns a Constant

sqrt(arg)

Square root of a ScalarBase object, returns a Constant

pow(arg, power)

arg raised to power

abs(arg)

absolute value of arg

min(arg1, arg2)

absolute value of arg

max(arg1, arg2)

absolute value of arg

MatrixFromVectors(e, v, name, registry[, eunit, vunit])

Creates a GDML Matrix from an energy and a value vector

Package Contents

pyg4ometry.gdml.isComment(node)
class pyg4ometry.gdml.Reader(fileName, registryOn=True, skipMaterials=False, reduceNISTMaterialsToPredefined=False)

Read a GDML file.

Parameters:
  • fileName (str) – path to gdml file to load

  • registryOn (bool) – whether to build a registry

  • reduceNISTMaterialsToPredefined (bool) – change NIST-named materials to predefined ones

When loading a GDML file that was exported by Geant4, the NIST materials may be fully expanded to include their full element / isotope composition. With the reduceNISTMaterialsToPredefined flag set to True, these will be ignored and the materials that have a name that matches a NIST one will be ‘reduced’ back to a predefined material by name only.

filename
registryOn
_reduceNISTMaterialsToPredefined
_skipMaterials
_physVolumeNameCount
load()
getRegistry()
parseDefines(xmldoc)
parseVector(node, type='position', addRegistry=True)
parseMaterials(xmldoc)
_makeMaterials(materials, elements, isotopes)

Construct the isotopes, elements and materials in that order. These aren’t returned, but simply constructed in and therefore exist in the registry.

parseUserInfo(xmldoc)
_parseAuxiliary(xmlnode, register=True)
parseSolids(xmldoc)
parseBox(node)
parseTube(node)
parseCutTube(node)
parseCone(node)
parsePara(node)
parseTrd(node)
parseTrap(node)
parseSphere(node)
parseOrb(node)
parseTorus(node)
parsePolycone(node)
parseGenericPolycone(node)
parsePolyhedra(node)
parseGenericPolyhedra(node)
parseEllipticalTube(node)
parseEllipsoid(node)
parseEllipticalCone(node)
parseParaboloid(node)
parseHype(node)
parseTet(node)
parseExtrudedSolid(node)
parseTwistedBox(node)
parseTwistedTrap(node)
parseTwistedTrd(node)
parseTwistedTubs(node)
parseGenericTrap(node)
parseTessellatedSolid(node)
parseUnion(node)
parseSubtraction(node)
parseIntersection(node)
parseMultiUnion(node)
parseOpticalSurface(node)
parseScaledSolid(node)
parseSolidLoop(node)
parseStructure(xmldoc, materialSubstitutionNames=None)
extractStructureNodeData(node, materialSubstitutionNames=None)
parsePhysicalVolumeChildren(node, vol)
pyg4ometry.gdml._StripPointer(name)
class pyg4ometry.gdml._Material(**kwargs)

Bases: MaterialBase

This class provides an interface to GDML material definitions.

Because of the different options for constructing a material instance the constructor is kwarg only. Proxy methods are provided to instantiate particular types of material. Those proxy methods are:

MaterialSingleElement MaterialCompound MaterialPredefined

It is possible to instantiate a material directly through kwargs. The possible kwargs are (but note some are mutually exclusive): name - string density - float atomic_number - int atomic_weight - float number_of_components - int state - string pressure - float pressure_unit - string temperature - float temperature_unit - string

density
atomic_number
atomic_weight
number_of_components
components = []
properties
_state_variables
_NIST_compounds
add_element_massfraction(element, massfraction)

Add an element as a component to a material as a fraction of the material mass. Can only add elements to materials defined as composite.

Inputs:

element - pyg4ometry.geant4.Material.Element instance massfraction - float, 0.0 < massfraction <= 1.0

add_element_natoms(element, natoms)

Add an element as a component to a material as a number of atoms in the material molecule. Can only add elements to materials defined as composite.

Inputs:

element - pyg4ometry.geant4.Material.Element instance natoms - int, number of atoms in the compound molecule

add_material(material, fractionmass)

Add a material as a component to another material (mixture) as a fraction of the mixture mass. Can only add new materials to materials defined as composite.

Inputs:

material - pyg4ometry.geant4.Material.Material instance massfraction - float, 0.0 < massfraction <= 1.0

set_pressure(value, unit='pascal')
set_temperature(value, unit='K')
property state_variables
__str__()
addProperty(name, matrix)

Add a material property from a matrix.

Parameters:
  • name (str) – key of the material property

  • matrix (Matrix) – matrix defining the value(s) of the property

addVecProperty(name, e, v, eunit='eV', vunit='')

Add a property from an energy and a value vector to this object.

Parameters:
  • name (str) – key of property

  • e (list or numpy.array - shape (1,)) – energy list/vector in units of eunit

  • v (list or numpy.array - shape (1,)) – value list/vector in units of vunit

  • eunit (str) – unit for the energy vector (default: eV)

  • vunit (str) – unit for the value vector (default: unitless)

addConstProperty(name, value, vunit='')

Add a constant scalar property to this object.

Parameters:
  • name (str) – key of property

  • value (str,float,int) – constant value for this property

  • vunit (str) – unit for the value vector (default: unitless)

class pyg4ometry.gdml._Element(**kwargs)

Bases: MaterialBase

This class provides an interface to GDML material definitions. Because of the different options for constructing a material instance the constructor is kwarg only. Proxy methods are provided to instantiate particular types of material. Those proxy methods are:

ElementSimple ElementIsotopeMixture

It is possible to instantiate a material directly through kwargs. The possible kwargs are (but note some are mutually exclusive): name - string symbol - string Z - int A - int n_comp - int

symbol
n_comp
Z
A
components = []
add_isotope(isotope, abundance)

Add an isotope as a component to an element as an abundance fraction in the element.

Inputs:

element - pyg4ometry.geant4.Material.Isotope instance abundance - float, 0.0 < abundance <= 1.0

class pyg4ometry.gdml._Isotope(name, Z, N, a, registry=None)

Bases: MaterialBase

This class that handles isotopes as components of composite materials. An element can be defined as a mixture of isotopes.

Inputs:

name - string Z - int, atomic number N - int, mass number a - float, molar weight in g/mole

Z
N
a
type = 'isotope'
class pyg4ometry.gdml.Writer(prepend='')
prepend
imp
doc
top
defines
materials
solids
structure
userinfo
setup
materials_written = []
solids_written = []
defineList = []
materialList = []
solidList = []
logicalVolumeList = []
physicalVolumeList = []
addDetector(registry)
write(filename)
writeGMADTesterNoBeamline(gmad, gdml)
writeGmadTester(filenameGmad, filenameGDML, writeDefaultLattice=False, preprocessGDML=True, energy=250)
writeDefaultLattice(filename='lattice.gmad')
checkDefineName(defineName)
checkMaterialName(materialName)
checkSolidName(solidName)
checkLogicalVolumeName(logicalVolumeName)
checkPhysicalVolumeName(physicalVolumeName)
writeVectorVariable(node, vector_var, allow_ref=True, suppress_trivial=True)

Writes an XML child node for a vector variable - position, rotation, scale. If allow_ref is enabled, it will write a ref to a registry define where possible. If suppress_trivial is enabled it won’t write vectors with all elements zero.

writeDefine(define)
writeMaterialProps(material, oe)
writeMaterial(material)
writeLogicalVolume(lv)
writeAuxiliary(aux, parent=None)
writeAssemblyVolume(lv)
writePhysicalVolume(pv)
writeReplicaVolume(instance)
writeDivisionVolume(instance)
writeParametrisedVolume(instance)
writeSkinSurface(instance)
writeBorderSurface(instance)
writeSolid(solid)

Dispatch to correct member function based on type string in SolidBase.

getValueOrExpr(var)
getValueOrExprFromInstance(instance, variable, index=None)
writeBox(instance)
writeCons(instance)
writeCutTubs(instance)
writeEllipsoid(instance)
writeEllipticalCone(instance)
writeEllipticalTube(instance)
createTwoDimVertex(x, y)
createSection(zOrder, zPosition, xOffset, yOffset, scalingFactor)
writeExtrudedSolid(instance)
createrzPoint(r, z)
writeGenericPolycone(instance)
writeGenericPolyhedra(instance)
createTriangularFacet(vertex1, vertex2, vertex3)
createQuadrangularFacet(vertex1, vertex2, vertex3, vertex4)
writeTessellatedSolid(instance)
writeHype(instance)
writeIntersection(instance)
writeOpticalSurface(instance)
writeOrb(instance)
writePara(instance)
writeParaboloid(instance)
createzPlane(rInner, rOuter, zplane)
writePolycone(instance)
writePolyhedra(instance)
writeSphere(instance)
writeGenericTrap(instance)
createPosition(name, x, y, z)
writeTet(instance)
writeTorus(instance)
writeTrap(instance)
writeTrd(instance)
writeTubs(instance)
writeTwistedBox(instance)
writeTwistedTrd(instance)
writeTwistedTrap(instance)
writeTwistedTubs(instance)
writeUnion(instance)
writeSubtraction(instance)
writeMultiUnion(instance)
writeScaled(instance)
class pyg4ometry.gdml.BasicExpression(name, expressionString, registry)

Holds an expression as a string and can use the expression parser in the supplied registry to evaluate it. A registry is required.

Parameters:
  • name (str) – Name of the expression object

  • expressionString (str) – Expression itself as a string e.g. “12.0” or “a + 3.0”

  • registry (pyg4ometry.geant4.Registry.Registry) – The registry object to give context for any variables used.

>>> r = pyg4ometry.geant4.Registry()
>>> a = BasicExpression("a", "3.0", r)
>>> float(a)
>>> str(a)
name
expressionString
parseTree = None
registry
eval()
variables(allDependents=False)
simp()
__repr__()
__float__()
str()
pyg4ometry.gdml.upgradeToStringExpression(reg, obj)

Take a float, str, ScalarBase and return string expression.

Parameters:
Returns:

String expression

Return type:

str

pyg4ometry.gdml.evaluateToFloat(reg, obj)
pyg4ometry.gdml.upgradeToExpression(reg, obj)

Helper functions that takes a string and returns an expression object or a string

pyg4ometry.gdml.upgradeToVector(var, reg, type='position', unit='', addRegistry=False)

Take a list [x,y,z] and create a vector

Parameters:
pyg4ometry.gdml.upgradeToTransformation(var, reg, addRegistry=False)

Take a list of lists [[rx,ry,rz],[x,y,z]] and create a transformation [Rotation,Position]

Parameters:
pyg4ometry.gdml.operationReturnType(name, strExpr, v1, v2, type1, type2, reg)
class pyg4ometry.gdml.DefineBase(name='', registry=None)

Common bits for a define. Must have a name and a registry. Adding to the registry can’t be done here as it must be done by the derived type.

name
registry
setName(name)

Set name of the object.

Parameters:

name (str) – name of object

setRegistry(registry)
class pyg4ometry.gdml.ScalarBase(typeName, name='', registry=None)

Bases: DefineBase

Base class for all scalars (Constants, Quantity, Variable and ‘Expression’)

expression = None
_typeName
setName(name)

Set name of scalar

Parameters:

name (str) – name of object

setExpression(expressionString)

Take a string and make it into the BasicExpression type for this object.

Parameters:

expressionString (str) – Expression to store.

setRegistry(registry)
eval()

Evaluate the expression

Returns:

numerical evaluation of Constant

Return type:

float

__repr__()
__float__()
__add__(other)
__sub__(other)
__rsub__(other)
__mul__(other)
__truediv__(other)
__rtruediv__(other)
__neg__()
__abs__()
__pow__(power)
__radd__
__rmul__
pyg4ometry.gdml.sin(arg)

Sin of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of sin

pyg4ometry.gdml.cos(arg)

Cosine of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of cos

pyg4ometry.gdml.tan(arg)

Tangent of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of tan

pyg4ometry.gdml.asin(arg)

ArcSin of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of asin

pyg4ometry.gdml.acos(arg)

ArcCos of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of acos

pyg4ometry.gdml.atan(arg)

ArcTan of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of tan

pyg4ometry.gdml.exp(arg)

Exponential of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of exp

pyg4ometry.gdml.log(arg)

Natural logarithm of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of log

pyg4ometry.gdml.log10(arg)

Base 10 logarithm of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of log10

pyg4ometry.gdml.sqrt(arg)

Square root of a ScalarBase object, returns a Constant

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of sin

pyg4ometry.gdml.pow(arg, power)

arg raised to power

Parameters:
pyg4ometry.gdml.abs(arg)

absolute value of arg

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of abs(arg)

pyg4ometry.gdml.min(arg1, arg2)

absolute value of arg

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of abs(arg)

pyg4ometry.gdml.max(arg1, arg2)

absolute value of arg

Parameters:

arg (Constant, Quantity, Variable or Expression) – Argument of abs(arg)

class pyg4ometry.gdml.Constant(name, value, registry, addRegistry=True)

Bases: ScalarBase

GDML constant define wrapper object

Parameters:
expression
__eq__(other)
__ne__(other)
__lt__(other)
__gt__(other)
__le__(other)
__ge__(other)
class pyg4ometry.gdml.Quantity(name, value, unit, type, registry, addRegistry=True)

Bases: ScalarBase

GDML quantity define wrapper object

Parameters:
  • name (str) – of constant for registry

  • value (float,str,Constant,Quantity,Variable) – expression for constant

  • unit (str) – unit of the quantity

  • type (not sure) – type of quantity

  • registry (Registry) – for storing define

  • addRegistry (bool) – add constant to registry

unit
type
expression
__repr__()
eval()

Evaluate the expression

Returns:

numerical evaluation of Constant

Return type:

float

class pyg4ometry.gdml.Variable(name, value, registry, addRegistry=True)

Bases: ScalarBase

GDML variable define wrapper object

Parameters:
expression
class pyg4ometry.gdml.Expression(name, value, registry, addRegistry=False)

Bases: ScalarBase

General expression, does not have an analogue in GDML

Parameters:
expression
__int__()
class pyg4ometry.gdml.VectorBase(typeName, name, registry)
_typeName
name
registry
x = None
y = None
z = None
unit = None
__repr__()
__add__(other)
__sub__(other)
__mul__(other)
__rmul__
__truediv__(other)
setName(name)

Set name of vector

Parameters:

name (str) – name of object

eval()

Evaluate vector

Returns:

numerical evaluation of vector

Return type:

list of floats

nonzero()

Evaluate vector

Returns:

Check if the vector is trivial (all elements zero)

Return type:

bool

__getitem__(key)
setRegistry(registry)
class pyg4ometry.gdml.Position(name, x, y, z, unit='mm', registry=None, addRegistry=True)

Bases: VectorBase

GDML position define wrapper object

Parameters:
x
y
z
class pyg4ometry.gdml.Rotation(name, rx, ry, rz, unit='rad', registry=None, addRegistry=True)

Bases: VectorBase

GDML rotation define wrapper object

Parameters:
x
y
z
class pyg4ometry.gdml.Scale(name, sx, sy, sz, unit=None, registry=None, addRegistry=True)

Bases: VectorBase

GDML scale define wrapper object

Parameters:
x
y
z
class pyg4ometry.gdml.Matrix(name, coldim, values, registry=None, addRegistry=True)

GDML matrix define wrapper object

Parameters:
  • name (str) – of matrix for registry

  • coldim – is number of columns

  • coldim – int

  • values (list of float, str, Constant, Quantity, Variable) – list of values for matrix

  • registry (Registry) – for storing define

  • addRegistry (bool) – add matrix to registry

name
coldim
registry
values = []
values_asarray
eval()

Evaluate matrix

Returns:

numerical evaluation of matrix

Return type:

numpy.array

__repr__()
__getitem__(key)
pyg4ometry.gdml.MatrixFromVectors(e, v, name, registry, eunit='eV', vunit='')

Creates a GDML Matrix from an energy and a value vector

Parameters:
  • name (str) – of matrix of registry

  • e (list or numpy.array - shape (1,)) – energy list/vector in units of eunit

  • v (list or numpy.array - shape (1,)) – value list/vector in units of vunit

  • registry (Registry) – for storing define

  • eunit (str) – unit for the energy vector (default: eV)

  • vunit (str) – unit for the value vector (default: unitless)

class pyg4ometry.gdml.Auxiliary(auxtype, auxvalue, registry=None, unit='', addRegistry=True)

Auxiliary information container object

auxtype
auxvalue
auxunit
subaux = []
registry
addSubAuxiliary(aux)

Add a sub-auxiliary inside the scope of the current auxiliary

Parameters:

aux (object, gdml.Defines.Auxiliary) – auxiliary definition