Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-27 03:18:08

0001 #! /usr/bin/env python
0002 
0003 import sys
0004 import xml.dom
0005 from xml.dom import minidom
0006 import material
0007 from domtools import DOMIterator, dom_strip
0008 
0009 def usage():
0010   print("""Usage:
0011     split.py NAME [DIRECTION CUT [CUT ...]] 
0012 
0013 Read a list of detectors from standard input, splits them into subgrouos at the CUTs position along the given DIRECTION, named after NAME, DIRECTION and relevant CUT. 
0014 The groups are appended to the trackingMaterialGroups.xml file - if not present an empty one is created beforehand.
0015 """)
0016 
0017 def split():
0018   if (len(sys.argv) < 2):
0019     usage()
0020     sys.exit(1)
0021 
0022   basename = sys.argv[1]
0023   if (len(sys.argv) in (2,3)):
0024     dir_name  = 'none'
0025     cuts = []
0026   else:
0027     dir_name = sys.argv[2].lower()
0028     if not dir_name in material.Element.directions:
0029       usage()
0030       sys.exit(1)
0031     cuts = [float(x) for x in sys.argv[3:]]
0032   direction = material.Element.directions[dir_name]
0033   dir_label = material.Element.dir_labels[dir_name]
0034   
0035   elements = material.parse(sys.stdin)
0036   if len(elements) == 0:
0037     sys.exit(1)
0038 
0039   groups = material.split_along(direction, elements, cuts)
0040   groups = material.remove_copy_number(elements, groups)
0041 
0042   try:
0043     # parse trackingMaterialGroups.xml
0044     document = minidom.parse("trackingMaterialGroups.xml")
0045     # remove text
0046     dom_strip(document)
0047     section = document.getElementsByTagName("SpecParSection")[0]
0048   except:
0049     # invalid document or no document to parse, create a new one
0050     document = minidom.getDOMImplementation().createDocument(xml.dom.XML_NAMESPACE, "DDDefinition", None)
0051     document.documentElement.setAttribute("xmlns", "http://www.cern.ch/cms/DDL")
0052     document.documentElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
0053     document.documentElement.setAttribute("xsi:schemaLocation", "http://www.cern.ch/cms/DDL ../../../DetectorDescription/Schema/DDLSchema.xsd")
0054     section = document.createElement("SpecParSection")
0055     section.setAttribute("label", "spec-pars2.xml")
0056     section.appendChild(document.createTextNode(""))
0057     document.documentElement.appendChild(section)
0058 
0059   for (index, group) in enumerate(groups):
0060     if len(group) == 0:
0061     # empty group
0062       continue
0063 
0064     if len(groups) == 1:
0065       # layer with no subgroups, use simple name
0066       group_name = basename
0067     else:
0068       # layer with subgroups, build sensible names
0069       if index == 0:
0070         group_name = "%s_%s%d"% (basename, dir_label, 0)
0071       else:
0072         group_name = "%s_%s%d"% (basename, dir_label, int(round(cuts[index-1])))
0073     
0074     specpar = document.createElement("SpecPar")
0075     specpar.setAttribute("name", group_name)
0076     specpar.setAttribute("eval", "true")
0077     for filter in group:
0078       selector = document.createElement("PartSelector")
0079       selector.setAttribute("path", filter.full_name())
0080       specpar.appendChild(selector)
0081     groupname = document.createElement("Parameter")
0082     groupname.setAttribute("name", "TrackingMaterialGroup")
0083     groupname.setAttribute("value", group_name)
0084     specpar.appendChild(groupname)
0085     section.appendChild(specpar)
0086     section.appendChild(document.createTextNode(""))
0087 
0088   # write the updated XML
0089   out = open("trackingMaterialGroups.xml", "w")
0090   out.write(document.toprettyxml("  ", "\n", "utf-8"))
0091   out.close()
0092 
0093 
0094 if __name__ == "__main__":
0095   split()