Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fixGDML.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file fixGDML.py
1 #!/usr/bin/env python
2 
3 import xml.etree.ElementTree
4 from xml.dom import minidom
5 import sys
6 import re
7 
8 # parse the GDML file
9 root = xml.etree.ElementTree.parse(sys.argv[1]).getroot()
10 
11 # look for all physvol elements
12 for pv in root.iter('physvol'):
13  # get and parse name of position
14  posname = pv.find('positionref').attrib['ref']
15 
16  posinfo = re.split(r'(\d+)in', posname.strip())
17  pvname = posinfo[0] + posinfo[1]
18 
19  # add name to physvol
20  pv.attrib['name'] = pvname
21 
22 # add sensitive volume node to all Sensors
23 for lv in root.iter('volume'):
24  if not 'Sensor' in lv.attrib['name']:
25  continue
26 
27  element = xml.etree.ElementTree.SubElement(lv, 'auxiliary')
28  element.set('auxtype', 'SensDet')
29  element.set('auxvalue', lv.attrib['name'])
30 
31 # reparse the xml to a string
32 rough_string = xml.etree.ElementTree.tostring(root, 'utf-8')
33 reparsed_string = minidom.parseString(rough_string).toprettyxml(indent = ' ', newl = '\n', encoding = 'utf-8')
34 
35 # remove empty linea
36 final_string = '\n'.join(line for line in reparsed_string.split('\n') if line.strip())
37 
38 # write to file
39 fout = open(sys.argv[2], 'w')
40 fout.write(final_string)
41 fout.close()