File indexing completed on 2024-10-03 05:27:06
0001
0002
0003 import optparse
0004 import configparser as ConfigParser
0005
0006 from os.path import expandvars
0007
0008 class Constituent:
0009 def __init__(self, line, predefinedMaterials):
0010 if len(line.split('"')) < 5 or len(line.split('"')[4].split()) < 3:
0011 raise Exception("not a well formed Constituent: "+constString)
0012 self.theDescription = line.split('"')[1]
0013 self.theName = line.split('"')[3]
0014 self.theCount = float(line.split('"')[4].split()[1])
0015 self.theVolume = float(line.split('"')[4].split()[0]) * self.theCount
0016 self.theType = line.split('"')[4].split()[2]
0017 self.theDensity = float(predefinedMaterials[self.theName][2])
0018 self.theX0 = float(predefinedMaterials[self.theName][3]) * self.theDensity
0019 self.theL0 = float(predefinedMaterials[self.theName][4]) * self.theDensity
0020 self.theMass = self.theVolume * self.theDensity
0021
0022 def __str__(self):
0023 return "Name: "+self.theName+" desc: "+self.theDescription+" mass "+self.theMass+" type "+self.theType+" count "+self.theCount
0024
0025 class Material:
0026 def __init__(self, matString, comment):
0027 if matString == "" or not matString[0] == "#":
0028 raise Exception("not a valid material Definition: "+matString)
0029 line = matString[1:]
0030
0031 if len( line.split('"') ) < 5 or len( line.split('"')[4].split() ) < 2:
0032 raise Exception("not a well formed Material Definition: "+matString)
0033 self.theDescription = line.split('"')[1]
0034 self.theName = line.split('"')[3]
0035 self.theMcVolume = float(line.split('"')[4].split()[0])
0036 self.theComment = comment
0037 self.theConstituents = []
0038
0039 def getRealValues(self):
0040 result = {}
0041 result["Volume"] = 0
0042 result["X0"] = 0
0043 result["L0"] = 0
0044
0045 totMass = self.getMass()
0046 invX0 = 0
0047 invL0 = 0
0048 for con in self.theConstituents:
0049 pWeight = con.theVolume * con.theDensity / totMass
0050 invX0 += pWeight / con.theX0
0051 invL0 += pWeight / con.theL0
0052 result["Volume"] += con.theVolume
0053 result["Density"] = self.getMass() / result["Volume"]
0054 result["X0"] = 1 / ( invX0 * result["Density"] )
0055 result["L0"] = 1 / ( invL0 * result["Density"] )
0056
0057 return result
0058
0059 def getSimValues(self):
0060 result = self.getRealValues()
0061 fraction = self.theMcVolume / result["Volume"]
0062 result["Volume"] = self.theMcVolume
0063 result["Density"] = self.getMass() / self.theMcVolume
0064 result["X0"] *= fraction
0065 result["L0"] *= fraction
0066
0067 return result
0068
0069 def getMass(self):
0070 result = 0
0071 for con in self.theConstituents:
0072 result += con.theMass * con.theCount
0073 return result
0074
0075 def addConstituent(self, constString, predefinedMaterials):
0076 if constString == "" or not constString[0] == "*":
0077 raise Exception("not a valid Constituent: "+constString)
0078 line = constString[1:]
0079 self.theConstituents.append( Constituent(line, predefinedMaterials) )
0080
0081 number = int( line.split('"')[0].split()[0] )
0082 if not len(self.theConstituents) == number:
0083 raise Exception("Constituent Number mismatch for "+str(len(self.theConstituents))+" in: "+line)
0084
0085 def __str__(self):
0086 result = "[ "+self.theName+" Description: "+self.theDescription+" MC-Volume:"+str(self.theMcVolume)+"\n"
0087 result += "Comments:\n"+self.theComment
0088 return result
0089
0090
0091 def parseInFile(inFile, predefinedMaterials, config):
0092 comment = ""
0093 materials = []
0094 i = 0
0095 for line in inFile.split("\n"):
0096 i=i+1
0097 if i < int(config["parsing"]["intro"]):
0098 continue
0099 if not line == "" and line[0] == "#":
0100 material = Material( line , comment )
0101 if not material.theName == "END":
0102 materials.append( material )
0103 comment = ""
0104
0105
0106 elif not line == "" and line[0] == "*":
0107
0108 materials[-1].addConstituent(line, predefinedMaterials)
0109 else:
0110 ignore = False
0111 for char in config["parsing"]["ignorelines"]:
0112 if len(line.strip()) == line.strip().count(char) and not len(line) == 0:
0113 ignore = True
0114 if not ignore:
0115 comment += line+"\n"
0116 return materials
0117
0118 def parseComment(comment):
0119 result = ""
0120 if comment.find("<twiki>") >= 0:
0121 result = comment.split("twiki>")[1][:-2]
0122 else:
0123 result += "<verbatim>\n"
0124 result += comment
0125 result += "</verbatim>\n"
0126 return result
0127
0128 def getTwiki(materials,config):
0129 result = config["twiki"]["pagename"]+"\n"
0130 result += "%TOC%\n\n"
0131 for mat in materials:
0132 result += config["twiki"]["heading"]+mat.theName+"\n"
0133 result += "short Description: *"+mat.theDescription+"* <br>\n"
0134 result += "Mass: %.3fg\n" % mat.getMass()
0135 result += config["twiki"]["tableformat"]+'\n'
0136 result += "| ** | *Volume* | *Density* | *X<sub>0</sub>* | *λ<sub>0</sub>* |\n"
0137 result += "| ** | *cms<sup>3</sup>* | *g cm<sup>-3</sup>* | *cm* | *cm* |\n"
0138 result += "| Real | %(Volume).3f | %(Density).3f | %(X0).3f | %(L0).3f |\n" % mat.getRealValues()
0139 result += "| Simulation | %(Volume).3f | %(Density).3f | %(X0).3f | %(L0).3f |\n" % mat.getSimValues()
0140 result += "\n"+config["twiki"]["subheading"]+"Comments\n"
0141 result += parseComment(mat.theComment)
0142 result += "\n---+++!!Material\n"
0143 result += config["twiki"]["tableformat"]+'\n'
0144 result += " | *Description* | *Material Name* | *Volume* | *Mass* | *Count* | *Type* | *X<sub>0</sub>* | *λ<sub>0</sub>* |\n"
0145 result += ' | ** | ** | *cm<sup>3</sup>* | *g* | ** | ** | *g cm<sup>-2</sup>* | *g cm<sup>-2</sup>* |\n'
0146 for const in mat.theConstituents:
0147 result += " | =%s= | =%s= | %.2e | %.2e | %.2f | %s | %.2f | %.2f |\n" % ( const.theDescription , const.theName , const.theVolume , const.theMass , const.theCount , const.theType , const.theX0 , const.theL0 )
0148
0149 result += "\n"
0150
0151 return result
0152
0153
0154 def readMaterialFile(fileName):
0155 file = open(fileName,"r")
0156 result = {}
0157 for line in file:
0158 if not line == "\n":
0159 name = line.split('"')[1]
0160 content = line.split('"')[2].split()
0161 result[name] = content
0162 return result
0163
0164
0165 def readSection(config,section):
0166 result = {}
0167 for option in config.options(section):
0168 result[option] = config.get(section,option)
0169 return result
0170
0171
0172 def readConfig(fileName):
0173 config = ConfigParser.ConfigParser()
0174 config.read(fileName)
0175 result = {}
0176
0177 result["parsing"] = readSection(config,"parsing")
0178 result["twiki"] = readSection(config,"twiki")
0179
0180 return result
0181
0182
0183
0184 def main():
0185 optParser = optparse.OptionParser()
0186 optParser.add_option("-i", "--input", dest="inFile",
0187 help="the .in material description that is the input", metavar="INPUT")
0188 optParser.add_option("-o", "--output", dest="output",
0189 help="the file to put the twiki formated output in (default: <.in-Filename>.twiki)", metavar="TWIKIOUT")
0190 optParser.add_option("-c", "--config", dest="config",
0191 help="configuration to use (default twikiConfig.ini)", metavar="CONFIG")
0192
0193 (options, args) = optParser.parse_args()
0194
0195 if options.inFile == None:
0196 raise Exception("no .in File given!")
0197 if options.output == None:
0198 options.output = options.inFile.replace(".in",".twiki")
0199 if options.config == None:
0200 options.config = "twikiConfig.ini"
0201
0202 config = readConfig(options.config)
0203
0204 predefinedMaterials = {}
0205 predefinedMaterials.update( readMaterialFile(
0206 expandvars(
0207 "$CMSSW_BASE/src/Geometry/TrackerCommonData/data/Materials/pure_materials.input"
0208 )
0209 ))
0210
0211 predefinedMaterials.update( readMaterialFile(
0212 expandvars(
0213 "$CMSSW_BASE/src/Geometry/TrackerCommonData/data/Materials/mixed_materials.input"
0214 )
0215 ))
0216
0217 inFile = open(options.inFile,"r")
0218 inFileContent = inFile.read()
0219 inFile.close()
0220
0221 materials = parseInFile(inFileContent, predefinedMaterials, config)
0222 twikiString = getTwiki(materials, config)
0223
0224 outFile = open(options.output,"w")
0225 outFile.write(twikiString)
0226 outFile.close()
0227
0228 main()