Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:30:44

0001 #! /usr/bin/env python
0002 # reads trackingMaterialGroups.xml (take it from SimTracker/TrackerMaterialAnalysis/data) 
0003 # and parameters.xml (output from SimTracker/TrackerMaterialAnalysis/test/trackingMaterialAnalyser.py)
0004 # and produces trackerRecoMaterial.xml (suitable for Geometry/TrackerRecoData/data)
0005 
0006 import sys
0007 import xml.dom
0008 from xml.dom import minidom
0009 import material
0010 from domtools import DOMIterator, dom_strip
0011 
0012 # Look for a child node like <Parameter name="name" value="value"/> .
0013 # Return a tuple (child, value) is such node is found.
0014 def findParameter(element, parameter):
0015   for child in element.getElementsByTagName("Parameter"):
0016     if child.hasAttribute("name") and child.getAttribute("name") == parameter:
0017       return (child, child.getAttribute("value"))
0018   return (None, "")
0019 
0020 
0021 def update():
0022   # parse trackingMaterialGroups.xml
0023   document = minidom.parse("trackingMaterialGroups.xml")
0024   # remove text
0025   dom_strip(document)
0026 
0027   # parse parameters.xml into a map
0028   data = minidom.parse("parameters.xml")
0029   material = dict()
0030   for group in data.getElementsByTagName("Group"):
0031     name = group.getAttribute("name")
0032     radlen = findParameter(group, "TrackerRadLength")[1]
0033     dedx   = findParameter(group, "TrackerXi")[1]
0034     material[name] = (radlen, dedx)
0035 
0036   for group in document.getElementsByTagName("SpecPar"):
0037     (parameter, name) = findParameter(group, "TrackingMaterialGroup")
0038     parameter.parentNode.removeChild(parameter)
0039     parameter.unlink()
0040     parameter = document.createElement("Parameter")
0041     parameter.setAttribute("name",  "TrackerRadLength")
0042     parameter.setAttribute("value", material[name][0])
0043     group.appendChild(parameter)
0044     parameter = document.createElement("Parameter")
0045     parameter.setAttribute("name",  "TrackerXi")
0046     parameter.setAttribute("value", material[name][1])
0047     group.appendChild(parameter)
0048 
0049   # write the updated XML
0050   out = open("trackerRecoMaterial.xml", "w")
0051   out.write(document.toprettyxml("  ", "\n", "utf-8"))
0052   out.close()
0053 
0054 
0055 if __name__ == "__main__":
0056   update()