Logo ROOT   6.18/05
Reference Guide
writer.py
Go to the documentation of this file.
1# @(#)root/gdml:$Id$
2# Author: Witold Pokorski 05/06/2006
3# This is the application-independent part of the GDML 'writer' implementation.
4# It contains the 'writeFile' method (at the end of the file) which does the actual
5# formating and writing out of the GDML file as well as the specialized 'add-element'
6# methods for all the supported GDML elements. These methods are used to build
7# the content of the GDML document, which is then generatd using the 'writeFile' method.
8
9# The constructor of this class takes the output file (.gdml) as argument.
10# An instance of this class should be passed to the constructor of application-specific
11# 'writer binding' (in the present case ROOTwriter) as argument.
12
13# For any question or remarks concerning this code, please send an email to
14# Witold.Pokorski@cern.ch.
15
16class writer(object):
17
18 def __init__(self, fname):
19
20 self.gdmlfile = fname
21 self.define = ['define',{},[]]
22 self.materials = ['materials',{},[]]
23 self.solids = ['solids',{},[]]
24 self.structure = ['structure',{},[]]
25 self.document = ['gdml',{'xmlns:gdml':"http://cern.ch/2001/Schemas/GDML",
26 'xmlns:xsi':"http://www.w3.org/2001/XMLSchema-instance",
27 'xsi:noNamespaceSchemaLocation':"gdml.xsd"},
28 [self.define, self.materials, self.solids, self.structure]]
29
30 def addPosition(self, name, x, y, z):
31 self.define[2].append(['position',{'name':name, 'x':x, 'y':y, 'z':z, 'unit':'cm'},[]])
32
33 def addRotation(self, name, x, y, z):
34 self.define[2].append(['rotation',{'name':name, 'x':x, 'y':y, 'z':z, 'unit':'deg'},[]])
35
36 def addMaterial(self, name, a, z, rho):
37 self.materials[2].append(['material', {'name':name, 'Z':z},
38 [['D',{'value':rho},[]], ['atom',{'value':a},[]]] ] )
39
40 def addMixture(self, name, rho, elems):
41 subel = [ ['D',{'value':rho},[]] ]
42 for el in elems.keys():
43 subel.append(['fraction',{'n':elems[el],'ref':el}, []])
44
45 self.materials[2].append(['material',{'name':name},
46 subel])
47
48 def addElement(self, symb, name, z, a):
49 self.materials[2].append(['element', {'name':name, 'formula':symb, 'Z':z},
50 [['atom', {'value':a},[]] ]])
51
52 def addReflSolid(self, name, solid, dx, dy, dz, sx, sy, sz, rx, ry, rz):
53 self.solids[2].append(['reflectedSolid',{'name':name, 'solid':solid, 'dx':dx, 'dy':dy, 'dz':dz, 'sx':sx, 'sy':sy, 'sz':sz, 'rx':rx, 'ry':ry, 'rz':rz},[]])
54
55 def addBox(self, name, dx, dy, dz):
56 self.solids[2].append(['box',{'name':name, 'x':dx, 'y':dy, 'z':dz, 'lunit':'cm'},[]])
57
58 def addParaboloid(self, name, rlo, rhi, dz):
59 self.solids[2].append(['paraboloid',{'name':name, 'rlo':rlo, 'rhi':rhi, 'dz':dz, 'lunit':'cm'},[]])
60
61 def addArb8(self, name, v1x, v1y, v2x, v2y, v3x, v3y, v4x, v4y, v5x, v5y, v6x, v6y, v7x, v7y, v8x, v8y, dz):
62 self.solids[2].append(['arb8',{'name':name, 'v1x':v1x, 'v1y':v1y, 'v2x':v2x, 'v2y':v2y, 'v3x':v3x, 'v3y':v3y, 'v4x':v4x, 'v4y':v4y, 'v5x':v5x, 'v5y':v5y, 'v6x':v6x, 'v6y':v6y, 'v7x':v7x, 'v7y':v7y, 'v8x':v8x, 'v8y':v8y, 'dz':dz, 'lunit':'cm'},[]])
63
64 def addSphere(self, name, rmin, rmax, startphi, deltaphi, starttheta, deltatheta):
65 self.solids[2].append(['sphere',{'name':name, 'rmin':rmin, 'rmax':rmax,
66 'startphi':startphi, 'deltaphi':deltaphi,
67 'starttheta':starttheta, 'deltatheta':deltatheta,
68 'aunit':'deg', 'lunit':'cm'},[]])
69
70 def addCone(self, name, z, rmin1, rmin2, rmax1, rmax2, sphi, dphi):
71 self.solids[2].append(['cone',{'name':name, 'z':z, 'rmin1':rmin1, 'rmin2':rmin2,
72 'rmax1':rmax1, 'rmax2':rmax2,
73 'startphi':sphi, 'deltaphi':dphi, 'lunit':'cm', 'aunit':'deg'}, []] )
74
75 def addPara(self, name, x, y, z, alpha, theta, phi):
76 self.solids[2].append(['para',{'name':name, 'x':x, 'y':y, 'z':z,
77 'alpha':alpha, 'theta':theta, 'phi':phi, 'lunit':'cm', 'aunit':'deg'}, []] )
78
79 def addTrap(self, name, z, theta, phi, y1, x1, x2, alpha1, y2, x3, x4, alpha2):
80 self.solids[2].append(['trap', {'name':name, 'z':z, 'theta':theta, 'phi':phi,
81 'x1':x1, 'x2':x2, 'x3':x3, 'x4':x4,
82 'y1':y1, 'y2':y2, 'alpha1':alpha1, 'alpha2':alpha2, 'lunit':'cm', 'aunit':'deg'}, []])
83
84 def addTwistedTrap(self, name, z, theta, phi, y1, x1, x2, alpha1, y2, x3, x4, alpha2, twist):
85 self.solids[2].append(['twistTrap', {'name':name, 'z':z, 'theta':theta, 'phi':phi,
86 'x1':x1, 'x2':x2, 'x3':x3, 'x4':x4,
87 'y1':y1, 'y2':y2, 'alpha1':alpha1, 'alpha2':alpha2, 'twist':twist, 'aunit':'deg', 'lunit':'cm'}, []])
88
89 def addTrd(self, name, x1, x2, y1, y2, z):
90 self.solids[2].append(['trd',{'name':name, 'x1':x1, 'x2':x2,
91 'y1':y1, 'y2':y2, 'z':z, 'lunit':'cm'}, []])
92
93 def addTube(self, name, rmin, rmax, z, startphi, deltaphi):
94 self.solids[2].append(['tube',{'name':name, 'rmin':rmin, 'rmax':rmax,
95 'z':z, 'startphi':startphi, 'deltaphi':deltaphi, 'lunit':'cm', 'aunit':'deg'},[]])
96
97 def addCutTube(self, name, rmin, rmax, z, startphi, deltaphi, lowX, lowY, lowZ, highX, highY, highZ):
98 self.solids[2].append(['cutTube',{'name':name, 'rmin':rmin, 'rmax':rmax,
99 'z':z, 'startphi':startphi, 'deltaphi':deltaphi,
100 'lowX':lowX, 'lowY':lowY, 'lowZ':lowZ, 'highX':highX, 'highY':highY, 'highZ':highZ, 'lunit':'cm', 'aunit':'deg'},[]])
101
102 def addPolycone(self, name, startphi, deltaphi, zplanes):
103 zpls = []
104 for zplane in zplanes:
105 zpls.append( ['zplane',{'z':zplane[0], 'rmin':zplane[1], 'rmax':zplane[2]},[]] )
106 self.solids[2].append(['polycone',{'name':name,
107 'startphi':startphi, 'deltaphi':deltaphi, 'lunit':'cm', 'aunit':'deg'}, zpls])
108
109 def addTorus(self, name, r, rmin, rmax, startphi, deltaphi):
110 self.solids[2].append( ['torus',{'name':name, 'rtor':r, 'rmin':rmin, 'rmax':rmax,
111 'startphi':startphi, 'deltaphi':deltaphi, 'lunit':'cm', 'aunit':'deg'},[]] )
112
113 def addPolyhedra(self, name, startphi, deltaphi, numsides, zplanes):
114 zpls = []
115 for zplane in zplanes:
116 zpls.append( ['zplane',{'z':zplane[0], 'rmin':zplane[1], 'rmax':zplane[2]},[]] )
117 self.solids[2].append(['polyhedra',{'name':name,
118 'startphi':startphi, 'deltaphi':deltaphi,
119 'numsides':numsides, 'lunit':'cm', 'aunit':'deg'}, zpls])
120
121 def addXtrusion(self, name, vertices, sections):
122 elems = []
123 for vertex in vertices:
124 elems.append( ['twoDimVertex',{'x':vertex[0], 'y':vertex[1]},[]] )
125 for section in sections:
126 elems.append( ['section',{'zOrder':section[0], 'zPosition':section[1], 'xOffset':section[2], 'yOffset':section[3], 'scalingFactor':section[4]},[]] )
127 self.solids[2].append(['xtru',{'name':name, 'lunit':'cm'}, elems])
128
129 def addEltube(self, name, x, y, z):
130 self.solids[2].append( ['eltube', {'name':name, 'dx':x, 'dy':y, 'dz':z, 'lunit':'cm'},[]] )
131
132 def addHype(self, name, rmin, rmax, inst, outst, z):
133 self.solids[2].append( ['hype', {'name':name, 'rmin':rmin, 'rmax':rmax,
134 'inst':inst, 'outst':outst, 'z':z, 'lunit':'cm', 'aunit':'deg'},[]] )
135
136 def addPos(self, subels, type, name, v):
137 if v[0]!=0.0 or v[1]!=0.0 or v[2]!=0.0:
138 subels.append( [type,{'name':name, 'x':v[0], 'y':v[1], 'z':v[2], 'unit':'cm'},[]] )
139
140 def addRot(self, subels, type, name, v):
141 if v[0]!=0.0 or v[1]!=0.0 or v[2]!=0.0:
142 subels.append( [type,{'name':name, 'x':v[0], 'y':v[1], 'z':v[2], 'unit':'deg'},[]] )
143
144 def addUnion(self, name, lname, ltr, lrot, rname, rtr, rrot):
145 subels = [['first',{'ref':lname},[]],
146 ['second',{'ref':rname},[]]]
147 self.addPos(subels, 'position', rname+'pos', rtr)
148 self.addRot(subels, 'rotation', rname+'rot', rrot)
149 self.addPos(subels, 'firstposition', lname+'pos', ltr)
150 self.addRot(subels, 'firstrotation', lname+'rot', lrot)
151 self.solids[2].append( ['union',{'name':name}, subels])
152
153 def addSubtraction(self, name, lname, ltr, lrot, rname, rtr, rrot):
154 subels = [['first',{'ref':lname},[]],
155 ['second',{'ref':rname},[]]]
156 self.addPos(subels, 'position', rname+'pos', rtr)
157 self.addRot(subels, 'rotation', rname+'rot', rrot)
158 self.addPos(subels, 'firstposition', lname+'pos', ltr)
159 self.addRot(subels, 'firstrotation', lname+'rot', lrot)
160 self.solids[2].append( ['subtraction',{'name':name}, subels])
161
162 def addIntersection(self, name, lname, ltr, lrot, rname, rtr, rrot):
163 subels = [['first',{'ref':lname},[]],
164 ['second',{'ref':rname},[]]]
165 self.addPos(subels, 'position', rname+'pos', rtr)
166 self.addRot(subels, 'rotation', rname+'rot', rrot)
167 self.addPos(subels, 'firstposition', lname+'pos', ltr)
168 self.addRot(subels, 'firstrotation', lname+'rot', lrot)
169 self.solids[2].append( ['intersection',{'name':name}, subels])
170
171 def addVolume(self, volume, solid, material, daughters):
172 subels = [['materialref',{'ref':material},[]],
173 ['solidref',{'ref':solid},[]]]
174 for child in daughters:
175 subsubels = [['volumeref',{'ref':child[0]},[]],
176 ['positionref',{'ref':child[1]},[]]]
177 if child[2]!='':
178 subsubels.append( ['rotationref',{'ref':child[2]},[]] )
179
180 subels.append( ['physvol',{}, subsubels])
181
182 used = 0
183 self.structure[2].append(['volume',{'name':volume}, subels, used])
184
185 def addAssembly(self, volume, daughters):
186 subels = []
187 for child in daughters:
188 subsubels = [['volumeref',{'ref':child[0]},[]],
189 ['positionref',{'ref':child[1]},[]]]
190 if child[2]!='':
191 subsubels.append( ['rotationref',{'ref':child[2]},[]] )
192
193 subels.append( ['physvol',{}, subsubels])
194
195 self.structure[2].append(['assembly',{'name':volume}, subels])
196
197 def addSetup(self, name, version, world):
198 self.document[2].append( ['setup',{'name':name, 'version':version},
199 [ ['world',{'ref':world},[]]] ] )
200
201 def writeFile(self):
202 file = open(self.gdmlfile,'w')
203 offset = ''
204
205 def writeElement(elem, offset):
206 offset = offset + ' '
207 file.write(offset+'<%s' %(elem[0]))
208 for attr in elem[1].keys():
209 file.write(' %s="%s"' %(attr,elem[1][attr]))
210 if elem[2].__len__()>0:
211 file.write('>\n')
212 for subel in elem[2]:
213 writeElement(subel, offset)
214
215 file.write(offset+'</%s>\n' %(elem[0]))
216 else:
217 file.write('/>\n')
218
219 file.write('<?xml version="1.0" encoding="UTF-8" ?>\n')
220 writeElement(self.document,'')
221
222
def addSubtraction(self, name, lname, ltr, lrot, rname, rtr, rrot)
Definition: writer.py:153
def addTrd(self, name, x1, x2, y1, y2, z)
Definition: writer.py:89
def addTrap(self, name, z, theta, phi, y1, x1, x2, alpha1, y2, x3, x4, alpha2)
Definition: writer.py:79
def addEltube(self, name, x, y, z)
Definition: writer.py:129
def addMaterial(self, name, a, z, rho)
Definition: writer.py:36
def addPara(self, name, x, y, z, alpha, theta, phi)
Definition: writer.py:75
def addIntersection(self, name, lname, ltr, lrot, rname, rtr, rrot)
Definition: writer.py:162
def addParaboloid(self, name, rlo, rhi, dz)
Definition: writer.py:58
def addMixture(self, name, rho, elems)
Definition: writer.py:40
def addSphere(self, name, rmin, rmax, startphi, deltaphi, starttheta, deltatheta)
Definition: writer.py:64
def addElement(self, symb, name, z, a)
Definition: writer.py:48
def addPolyhedra(self, name, startphi, deltaphi, numsides, zplanes)
Definition: writer.py:113
def addTorus(self, name, r, rmin, rmax, startphi, deltaphi)
Definition: writer.py:109
def addXtrusion(self, name, vertices, sections)
Definition: writer.py:121
def addTube(self, name, rmin, rmax, z, startphi, deltaphi)
Definition: writer.py:93
def addPos(self, subels, type, name, v)
Definition: writer.py:136
def addSetup(self, name, version, world)
Definition: writer.py:197
def addReflSolid(self, name, solid, dx, dy, dz, sx, sy, sz, rx, ry, rz)
Definition: writer.py:52
def addPosition(self, name, x, y, z)
Definition: writer.py:30
def addArb8(self, name, v1x, v1y, v2x, v2y, v3x, v3y, v4x, v4y, v5x, v5y, v6x, v6y, v7x, v7y, v8x, v8y, dz)
Definition: writer.py:61
def addRotation(self, name, x, y, z)
Definition: writer.py:33
def addVolume(self, volume, solid, material, daughters)
Definition: writer.py:171
def addRot(self, subels, type, name, v)
Definition: writer.py:140
def addAssembly(self, volume, daughters)
Definition: writer.py:185
def addPolycone(self, name, startphi, deltaphi, zplanes)
Definition: writer.py:102
def addHype(self, name, rmin, rmax, inst, outst, z)
Definition: writer.py:132
def writeFile(self)
Definition: writer.py:201
def addBox(self, name, dx, dy, dz)
Definition: writer.py:55
def addCutTube(self, name, rmin, rmax, z, startphi, deltaphi, lowX, lowY, lowZ, highX, highY, highZ)
Definition: writer.py:97
def addUnion(self, name, lname, ltr, lrot, rname, rtr, rrot)
Definition: writer.py:144
def addTwistedTrap(self, name, z, theta, phi, y1, x1, x2, alpha1, y2, x3, x4, alpha2, twist)
Definition: writer.py:84
def __init__(self, fname)
Definition: writer.py:18
def addCone(self, name, z, rmin1, rmin2, rmax1, rmax2, sphi, dphi)
Definition: writer.py:70