-
Notifications
You must be signed in to change notification settings - Fork 17
/
extractVol.py
174 lines (158 loc) · 5.41 KB
/
extractVol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sys, os
from lxml import etree
volList = []
solidList = []
positionList = []
rotationList = []
def processSolid(sname) :
# Passed olid name volume, booleans
global solidList, solids
solid = oldSolids.find(f"*[@name='{sname}']")
print('Adding : '+sname)
print(solid.attrib)
if sname not in solidList : solidList.append(sname)
if solid.tag == 'subtraction' or solid.tag == 'union' or \
solid.tag == 'intersection' :
print('Found Boolean')
first = solid.find('first')
print('first : '+str(first.attrib))
fname = first.attrib.get('ref')
processSolid(fname)
second = solid.find('second')
print('second : '+str(second.attrib))
sname = second.attrib.get('ref')
processSolid(sname)
def processPhysVol(volasm):
for pv in volasm.findall('physvol') :
volref = pv.find('volumeref')
pname = volref.attrib.get('ref')
print('physvol : '+pname)
processVolAsm(pname)
posref = pv.find('positionref')
if posref is not None :
posname = posref.attrib.get('ref')
print('Position ref : '+posname)
if posname not in positionList :
positionList.append(posname)
rotref = pv.find('rotationref')
if rotref is not None :
rotname = rotref.attrib.get('ref')
print('Rotation ref : '+rotname)
if rotname not in rotationList : rotationList.append(rotname)
def processVol(vol) :
global volList, solidList, oldSolids
print(vol)
print(vol.attrib)
# Need to process physvols first
processPhysVol(vol)
vname = vol.attrib.get('name')
print('volume : ' + vname)
if vname not in volList : volList.append(vname)
solid = vol.find('solidref')
sname = solid.attrib.get('ref')
processSolid(sname)
material = vol.find('materialref')
if material is not None :
#print('material : '+str(material.attrib))
print('material : ' + material.attrib.get('ref'))
def processAssembly(assem) :
aname = assem.attrib.get('name')
print('Process Assembly ; '+aname)
processPhysVol(assem)
if aname not in volList : volList.append(aname)
def processVolAsm(vaname) :
volasm = structure.find(f"*[@name='{vaname}']")
if volasm.tag == 'volume' :
processVol(volasm)
elif volasm.tag == 'assembly' :
processAssembly(volasm)
else :
print('Not Volume or Assembly : '+volasm.tag)
def exportElement(dirPath, elemName, elem) :
import os
global gdml, docString
etree.ElementTree(elem).write(os.path.join(dirPath,elemName))
docString += '<!ENTITY '+elemName+' SYSTEM "'+elemName+'">\n'
gdml.append(etree.Entity(elemName))
def checkDirectory(path) :
if not os.path.exists(path):
print('Creating Directory : '+path)
os.mkdir(path)
if len(sys.argv)<5:
print ("Usage: sys.argv[0] <parms> <Volume> <in_file> <Out_directory> <materials>")
print("/n For parms the following are or'ed together")
print(" For future")
sys.exit(1)
parms = int(sys.argv[1])
gdml = etree.Element('gdml')
volume = sys.argv[2]
iName = sys.argv[3]
oName = sys.argv[4]
print('\nExtracting Volume : '+volume+' from : '+iName+' to '+oName)
tree = etree.parse(iName)
root = tree.getroot()
structure = tree.find('structure')
oldSolids = tree.find('solids')
#print(etree.fromstring(structure))
# Following works
#vol = structure.find('volume[@name="World"]')
# Test if Volume
vol = structure.find(f"volume[@name='{volume}']")
if vol is not None :
processVol(vol)
else :
# Test if Assembly
vol = structure.find(f"assembly[@name='{volume}']")
if vol is not None :
processAssembly(vol)
else :
print(volume+' : Not found as Volume or Assembly')
exit(0)
newDefine = etree.Element('define')
materials = tree.find('materials')
newSolids = etree.Element('solids')
newStructure = etree.Element('structure')
oldDefine = tree.find('define')
oldSolids = tree.find('solids')
oldVols = tree.find('structure')
for posName in positionList :
p = oldDefine.find(f"position[@name='{posName}']")
newDefine.append(p)
for rotName in rotationList :
p = oldDefine.find(f"rotation[@name='{rotName}']")
newDefine.append(p)
for solidName in solidList :
print('Solid : '+solidName)
s = oldSolids.find(f"*[@name='{solidName}']")
#print(s.attrib)
newSolids.append(s)
for vaName in volList :
v = oldVols.find(f"*[@name='{vaName}']")
newStructure.append(v)
print('Vol List')
print(volList)
print('Solid List')
print(solidList)
print('Position List')
print(positionList)
print('Rotation List')
print(rotationList)
setup = etree.Element('setup', {'name':'Default', 'version':'1.0'})
etree.SubElement(setup,'world', { 'ref' : volList[-1]})
print("Write GDML structure to Directory")
NS = 'http://www.w3.org/2001/XMLSchema-instance'
location_attribute = '{%s}noNameSpaceSchemaLocation' % NS
gdml = etree.Element('gdml',attrib={location_attribute: \
'http://service-spi.web.cern.ch/service-spi/app/releases/GDML/schema/gdml.xsd'})
docString = '\n<!DOCTYPE gdml [\n'
checkDirectory(oName)
#exportElement(oName, 'constants',constants)
exportElement(oName, volume+'_define',newDefine)
exportElement(oName, volume+'_materials',materials)
exportElement(oName, volume+'_solids',newSolids)
exportElement(oName, volume+'_structure',newStructure)
exportElement(oName, volume+'_setup',setup)
docString += ']>\n'
#indent(gdml)
etree.ElementTree(gdml).write(os.path.join(oName,volume+'.gdml'), \
doctype=docString.encode('UTF-8'))