CMS 3D CMS Logo

Functions | Variables

insertMaterial Namespace Reference

Functions

def createCompositeMaterial
def dokument
def getAttributes
def getMaterial
def getMaterialSection
def getNodes
def getSection
def main
def prettierprint
def printMaterials
def readFractions
def readXML

Variables

float maxDist = 0.0001

Function Documentation

def insertMaterial::createCompositeMaterial (   doc,
  rootNode,
  name,
  density,
  fractions,
  method = "mixture by weight",
  symbol = " " 
)

Definition at line 139 of file insertMaterial.py.

00140                                                                                                         :
00141     newMaterial = doc.createElement("CompositeMaterial")
00142     newMaterial.setAttribute("name",name)
00143     newMaterial.setAttribute("density",density)
00144     newMaterial.setAttribute("method",method)
00145     newMaterial.setAttribute("symbol",symbol)    
00146 
00147     for fracMaterialName in fractions:
00148         fraction = doc.createElement("MaterialFraction")
00149         fraction.setAttribute("fraction",str(fractions[fracMaterialName]))
00150         newMaterial.appendChild(fraction)
00151         fracMaterial = doc.createElement("rMaterial")
00152         fracMaterial.setAttribute("name",fracMaterialName)
00153         fraction.appendChild(fracMaterial)
00154 
00155     exMaterials = getNodes(rootNode,"CompositeMaterial")
00156     if name in exMaterials:
00157         rootNode.replaceChild(newMaterial,exMaterials[name])
00158     else:
00159         rootNode.appendChild(newMaterial)
00160 
#main
def insertMaterial::dokument (   domina)

Definition at line 103 of file insertMaterial.py.

00104                     :
00105     for node in domina.childNodes:
00106         print "NodeName:", node.nodeName,
00107         if node.nodeType == node.ELEMENT_NODE:
00108             print "Typ ELEMENT_NODE"
00109             print getAttributes(node)
00110         elif node.nodeType == node.TEXT_NODE:
00111             print "Typ TEXT_NODE, Content: ", node.nodeValue.strip()
00112         elif node.nodeType == node.COMMENT_NODE:
00113             print "Typ COMMENT_NODE, "
00114         #dokument(node)
00115 
00116 #prints all CompositeMaterials beneeth [rootNode]
#(not used but interessting for debug)
def insertMaterial::getAttributes (   node)

Definition at line 94 of file insertMaterial.py.

00095                        :
00096     result = {};
00097     for i in range(0,node.attributes.length):
00098 #        print "  "+node.attributes.item(i).name+" = "+node.attributes.item(i).nodeValue
00099         result[node.attributes.item(i).name] = node.attributes.item(i).nodeValue    
00100     return result
00101 
00102 #print information on all subnodes of [domina]
#(not used but interessting for debug)
def insertMaterial::getMaterial (   material,
  fileName 
)

Definition at line 43 of file insertMaterial.py.

00044                                    :
00045     materialMap ={}
00046     file = open(fileName,"r")
00047     for line in file:
00048         line = line.strip("\n")
00049         content = line.split()
00050         if len(content) == 2:
00051             materialMap[content[0]] = content[1] 
00052     if material in materialMap:
00053         result = materialMap[material]+":"+material
00054     else:
00055         result =  "materials:"+material
00056     return result
00057 
#parse XML-File
def insertMaterial::getMaterialSection (   rootNode)

Definition at line 131 of file insertMaterial.py.

00132                                 :
00133     dddef = getSection(rootNode,'DDDefinition')
00134     matSec = getSection(dddef,'MaterialSection')
00135     return matSec
00136 
00137 #creates a CompositeMaterial with [name] [method] [density] and [symbol] beneeth [rootNode]. 
00138 #fractions is a map of material Names containing the fractions
#NOTE: if an material of that name allready exists it will be overridden. 
def insertMaterial::getNodes (   rootNode,
  name 
)

Definition at line 75 of file insertMaterial.py.

00076                             :
00077     result = {}
00078     for node in rootNode.childNodes:
00079         if node.nodeName == name and node.nodeType == node.ELEMENT_NODE:
00080             for i in range(0,node.attributes.length):
00081                 if node.attributes.item(i).name == "name":
00082                     result[node.attributes.item(i).nodeValue] = node
00083     return result
#returns a pretty printed string of [dom] withot whitespace-only-lines
def insertMaterial::getSection (   rootNode,
  name 
)

Definition at line 65 of file insertMaterial.py.

Referenced by JetCorrectorParametersCollection::getSections(), and JetCorrectorParameters::JetCorrectorParameters().

00066                               :
00067     result = None
00068     for node in rootNode.childNodes:
00069         if node.nodeName == name:
00070             result = node
00071     if result == None:
00072         raise StandardError, "Could not find: \""+name+"\" in childnodes of the rootNode!"
00073     return result          
00074 
#returns a map of [name] nodes by their names. stating from rootNode
def insertMaterial::main ( )

Definition at line 161 of file insertMaterial.py.

00162           :
00163     optParser = optparse.OptionParser()
00164     optParser.add_option("-t", "--titles", dest="titlesFile",
00165                   help="the .titles file to parse (as generated by mixture)", metavar="TITLES")
00166     optParser.add_option("-x", "--xml", dest="xmlFile",
00167                   help="the .xml file to parse (must be DDD complient)", metavar="XML")
00168     optParser.add_option("-o", "--output", dest="output",
00169                   help="the file to write the new materials into default: materialOutput.xml", metavar="XMLOUT")
00170     optParser.add_option("-m", "--materialMap", dest="materialMap",
00171                   help="file containing map of materials not defined in materials.xml. default: material.map", metavar="MMAP")
00172 
00173     (options, args) = optParser.parse_args()
00174 
00175     if options.titlesFile == None:
00176         raise StandardError, "no .titles File given!"
00177     if options.xmlFile == None:
00178         raise StandardError, "no .xml File given!"
00179     if options.output == None:
00180         options.output = "materialOutput.xml"
00181     if options.materialMap == None:
00182         options.materialMap = "material.map"
00183     theOptions = options
00184 
00185     materials = readFractions(options.titlesFile, options.materialMap)
00186     
00187     dom = readXML(options.xmlFile)
00188     matSec = getMaterialSection(dom)
00189 
00190  #   print "before:"
00191  #   printMaterials(matSec)
00192 
00193     for material in materials:
00194         createCompositeMaterial(dom,matSec,material,str(materials[material][0])+"*g/cm3",materials[material][1])
00195 
00196 #    print "after:"
00197 #    printMaterials(matSec)
00198     outFile = open(options.output,"w")
00199     outFile.write(prettierprint(dom))
00200     outFile.close()
00201     
00202 main()
00203 
def insertMaterial::prettierprint (   dom)

Definition at line 84 of file insertMaterial.py.

00085                       :
00086     result = ""
00087     output = dom.toprettyxml()
00088     for line in output.splitlines():
00089         if not line.strip(" \t") == "":
00090             result+= line+"\n"
00091     return result
00092 
00093 #gets a map of attributes and their values of [node] 
#(not used but interessting for debug)
def insertMaterial::printMaterials (   rootNode)

Definition at line 117 of file insertMaterial.py.

00118                             :
00119     matNodes = getNodes(rootNode,"CompositeMaterial")
00120     for name in matNodes:
00121         print "  "+name+" (dens = "+getAttributes(matNodes[name])["density"]+")"
00122         for fractionNode in matNodes[name].childNodes:
00123             if fractionNode.nodeName == "MaterialFraction":
00124                 fractionString = getAttributes(fractionNode)["fraction"]
00125                 for materialNode in fractionNode.childNodes:
00126                     if materialNode.nodeName == "rMaterial":
00127                         fractionString += "\tof "+getAttributes(materialNode)["name"].split(":")[1]
00128                         fractionString += "\tfrom "+getAttributes(materialNode)["name"].split(":")[0]
00129                 print "   |-- "+fractionString
00130     
#returns the Material Section doe of a DDD Material xmlfile
def insertMaterial::readFractions (   fileName,
  materialMap 
)

Definition at line 10 of file insertMaterial.py.

00011                                         :
00012     result = {}
00013     file = open(fileName,"r")
00014     name = None
00015     dens = None
00016     fractions = {}
00017     for line in file:
00018         line = line.strip("\n")
00019         if len(line.split("\""))==3:
00020             contentName = line.split("\"")[1]
00021             content = line.split("\"")[2].split()
00022             if len(content) == 2:
00023                 if not name == None:
00024                     result[name] = dens,fractions
00025                 name = contentName
00026                 dens = content[1]
00027                 fractions = {}
00028             elif len(content) == 1:
00029     #            print "  "+contentName+" "+str(float(content[0][1:])*0.01)
00030                 fractions[getMaterial(contentName,materialMap)] = float(content[0][1:])*0.01
00031     
00032     if not name == None:
00033         result[name] = dens,fractions
00034                         
00035     for material in result:
00036         sum = 0
00037         for fraction in result[material][1]:
00038             sum+= result[material][1][fraction]
00039         if math.fabs(sum - 1.0) > maxDist:
00040             raise StandardError, "Material Fractions do not add up to 100%: "+ material+" "+str(sum)
00041     return result
00042         
#get a source:material from the [material] only
def insertMaterial::readXML (   fileName)

Variable Documentation

float insertMaterial::maxDist = 0.0001

Definition at line 7 of file insertMaterial.py.

Referenced by Conv4HitsReco::IsNotValidForVtxPosition().