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