| 1 | # $Id: myUtils.py,v 1.6 2004/12/08 15:37:14 daquinog Exp $
|
|---|
| 2 | # -------------------------------------------------------------------
|
|---|
| 3 | # GEANT4 tag $Name: $
|
|---|
| 4 | # -------------------------------------------------------------------
|
|---|
| 5 | #
|
|---|
| 6 | import CLHEP
|
|---|
| 7 | import G4Kernel
|
|---|
| 8 |
|
|---|
| 9 | import string
|
|---|
| 10 | import os
|
|---|
| 11 | import time
|
|---|
| 12 | import shelve
|
|---|
| 13 |
|
|---|
| 14 | G4analysisUse = os.environ.has_key("G4ANALYSIS_USE")
|
|---|
| 15 | if G4analysisUse:
|
|---|
| 16 | import myPI
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 | def createParallelSampler(impGeo, impScorer):
|
|---|
| 20 | parallelSampler = G4Kernel. \
|
|---|
| 21 | G4ParallelGeometrySampler(\
|
|---|
| 22 | impGeo.getWorldVolume(), "neutron")
|
|---|
| 23 | parallelSampler.PrepareScoring(impScorer)
|
|---|
| 24 | if impGeo.base > 1:
|
|---|
| 25 | istore = impGeo.getImportanceStore()
|
|---|
| 26 | parallelSampler.PrepareImportanceSampling(istore,None)
|
|---|
| 27 |
|
|---|
| 28 | parallelSampler.Configure()
|
|---|
| 29 | return parallelSampler
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | def printImpTable(impScorer, istore = ""):
|
|---|
| 34 | iScMap = impScorer.GetMapGeometryCellCellScorer()
|
|---|
| 35 | table = ""
|
|---|
| 36 | if istore:
|
|---|
| 37 | table = G4Kernel.G4ScoreTable(istore)
|
|---|
| 38 | else:
|
|---|
| 39 | table = G4Kernel.G4ScoreTable()
|
|---|
| 40 | table.Print(iScMap)
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | def saveResults(tApp, path, shelveName, impScorer, impGeo):
|
|---|
| 45 | myShelve = shelve.open(path + "/" + shelveName)
|
|---|
| 46 | printImpTable(impScorer, impGeo.getImportanceStore())
|
|---|
| 47 | table = G4Kernel.G4ScoreTable()
|
|---|
| 48 | table.Print(tApp.cellScorerStore.GetMapGeometryCellCellScorer())
|
|---|
| 49 | if G4analysisUse:
|
|---|
| 50 | storePath = path + "/" + myShelve["xmlStoreName"]
|
|---|
| 51 | persistantStore = myPI.tf.create(storePath,"xml",0,1)
|
|---|
| 52 | coppyTrees(tApp.tree, persistantStore)
|
|---|
| 53 | persistantStore.commit()
|
|---|
| 54 | persistantStore.close()
|
|---|
| 55 | print "wrote to store: ", storePath
|
|---|
| 56 |
|
|---|
| 57 | tApp.fillShelve(myShelve)
|
|---|
| 58 | myShelve.close()
|
|---|
| 59 | print "wrote shelve: ", shelveName
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | def coppyTrees(srcTree, cpTree):
|
|---|
| 63 | mntPoint = "cpTreeMountPoint"
|
|---|
| 64 | srcTree.mkdir("/" + mntPoint)
|
|---|
| 65 | srcTree.mount("/" + mntPoint, cpTree, "/")
|
|---|
| 66 | objectNames = srcTree.listObjectNames()
|
|---|
| 67 | objectTypes = srcTree.listObjectTypes()
|
|---|
| 68 | for i in range(len(objectNames)):
|
|---|
| 69 | name = objectNames[i]
|
|---|
| 70 | type = objectTypes[i]
|
|---|
| 71 | if string.find(name, mntPoint) < 0:
|
|---|
| 72 | srcTree.cp (name,"/" + mntPoint +"/")
|
|---|
| 73 |
|
|---|
| 74 | min = 6000
|
|---|
| 75 | hour = 60 * min
|
|---|
| 76 | day = 24 * hour
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | def getTotalTime(totalTime):
|
|---|
| 80 | days = totalTime / day
|
|---|
| 81 | dayRest = totalTime - days*day
|
|---|
| 82 | hours = dayRest / hour
|
|---|
| 83 | minrest = dayRest - hours*hour
|
|---|
| 84 | minutes = minrest / min
|
|---|
| 85 |
|
|---|
| 86 | tString = ""
|
|---|
| 87 | if days > 0:
|
|---|
| 88 | tString += "%(days)dd" % vars()
|
|---|
| 89 | if hours > 0:
|
|---|
| 90 | tString += "%(hours)dh" % vars()
|
|---|
| 91 | if minutes > 0:
|
|---|
| 92 | tString += "%(minutes)dm" % vars()
|
|---|
| 93 |
|
|---|
| 94 | return tString
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | def getStoreName():
|
|---|
| 98 |
|
|---|
| 99 | Y, M, D, h, m, s, wd, jd, ds = time.localtime()
|
|---|
| 100 | hostName = os.environ["HOST"]
|
|---|
| 101 |
|
|---|
| 102 | storeName = "tiara-" + \
|
|---|
| 103 | "%(Y)d" % vars() + "_" + \
|
|---|
| 104 | "%(M)d" % vars() + "_" + \
|
|---|
| 105 | "%(D)d" % vars() + "_" + \
|
|---|
| 106 | "%(h)d" % vars() + "_" + \
|
|---|
| 107 | "%(m)d" % vars() + "_" + \
|
|---|
| 108 | "%(s)d" % vars() + "_" + \
|
|---|
| 109 | hostName
|
|---|
| 110 |
|
|---|
| 111 | return storeName
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | def getConfigurationInfo(impGeo, experiment, physicsList, totalTime,
|
|---|
| 115 | comment):
|
|---|
| 116 | configInfo = {}
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | configInfo["energy"] = experiment.energy
|
|---|
| 120 |
|
|---|
| 121 | configInfo["shieldMaterial"] = experiment.shieldMaterial
|
|---|
| 122 |
|
|---|
| 123 | width = experiment.shieldWidth / CLHEP.cm
|
|---|
| 124 | s_width = "%(width)d" % vars()
|
|---|
| 125 | configInfo["shieldWidth"] = s_width
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | base = impGeo.base
|
|---|
| 129 | simp = "no"
|
|---|
| 130 | if base > 1:
|
|---|
| 131 | simp = "ImpBase_%(base)f" % vars()
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 | configInfo["biasing"] = simp
|
|---|
| 135 |
|
|---|
| 136 | configInfo["physListName"] = physicsList.getName()
|
|---|
| 137 |
|
|---|
| 138 | particles = ""
|
|---|
| 139 |
|
|---|
| 140 | configInfo["minEnergyCut"] = experiment.particleCut
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | stime = "no"
|
|---|
| 144 | if totalTime > 0:
|
|---|
| 145 | stime = getTotalTime(totalTime)
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 | configInfo["timeLimit"] = stime
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | configInfo["impGeoName"] = impGeo.nameExt
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 | configInfo["hostName"] = os.environ["HOST"]
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 | configInfo["comment"] = comment
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | return configInfo
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | def setConfigInfo(tree, confInfo, af):
|
|---|
| 167 | hf = af.createHistogramFactory(tree)
|
|---|
| 168 | h = hf.createHistogram1D("configInfo","configInfo",1,0,1)
|
|---|
| 169 | anno = h.annotation()
|
|---|
| 170 | for k in confInfo:
|
|---|
| 171 | anno.addItem(k, confInfo[k])
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 | def getConfigInfoFromTree(tree):
|
|---|
| 175 | configInfo = {}
|
|---|
| 176 | h = tree.findH1D("configInfo")
|
|---|
| 177 | anno = h.annotation ()
|
|---|
| 178 | for i in range(8, anno.size()):
|
|---|
| 179 | configInfo[anno.key(i)] = anno.value(i)
|
|---|
| 180 |
|
|---|
| 181 | return configInfo
|
|---|
| 182 |
|
|---|
| 183 | if G4analysisUse:
|
|---|
| 184 | def addToXML(mergedXMLStore, xmlStore):
|
|---|
| 185 | objNames = xmlStore.listObjectNames()
|
|---|
| 186 | objTypes = xmlStore.listObjectTypes()
|
|---|
| 187 | hf = myPI.af.createHistogramFactory(mergedXMLStore)
|
|---|
| 188 | for i in range(len(objNames)):
|
|---|
| 189 | oName = objNames[i]
|
|---|
| 190 | oType = objTypes[i]
|
|---|
| 191 | if oType == "IHistogram1D":
|
|---|
| 192 | h1 = mergedXMLStore.findH1D(oName)
|
|---|
| 193 | h2 = xmlStore.findH1D(oName)
|
|---|
| 194 | hf.add(oName,h1,h2)
|
|---|
| 195 |
|
|---|
| 196 | if G4analysisUse:
|
|---|
| 197 | def comparableShelves(she, mergedXMLStore):
|
|---|
| 198 | comp = 1
|
|---|
| 199 | if mergedShelve["energy"] != she["energy"]:
|
|---|
| 200 | comp = 0
|
|---|
| 201 | if mergedShelve["shieldWidth"] != she["shieldWidth"]:
|
|---|
| 202 | comp = 0
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 | def setUpMergedShelve(she, mergedShelve, mergedXMLname, shelveNameToBeAdded):
|
|---|
| 206 | mergedShelve["xmlStoreName"] = mergedXMLname
|
|---|
| 207 | mergedShelve["energy"] = she["energy"]
|
|---|
| 208 | mergedShelve["shieldWidth"] = she["shieldWidth"]
|
|---|
| 209 | mergedShelve["mergedFiles"] = shelveNameToBeAdded
|
|---|
| 210 | mergedShelve["runTime"] = she["runTime"]
|
|---|
| 211 | mergedShelve["generatorTally"] = she["generatorTally"]
|
|---|
| 212 | mergedShelve["sourceDetectorTally"] = she["sourceDetectorTally"]
|
|---|
| 213 | mergedShelve["detector_00Tally"] = she["detector_00Tally"]
|
|---|
| 214 | mergedShelve["detector_20Tally"] = she["detector_20Tally"]
|
|---|
| 215 | mergedShelve["detector_40Tally"] = she["detector_40Tally"]
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 | def addToShelve(mergedShelve, she, shelveNameToBeAdded):
|
|---|
| 219 | mergedShelve["mergedFiles"] += shelveNameToBeAdded + " "
|
|---|
| 220 | mergedShelve["runTime"] += she["runTime"]
|
|---|
| 221 | mergedShelve["generatorTally"].addMeasures(she["generatorTally"])
|
|---|
| 222 | mergedShelve["sourceDetectorTally"].addMeasures(she["sourceDetectorTally"])
|
|---|
| 223 | mergedShelve["detector_00Tally"].addMeasures(she["detector_00Tally"])
|
|---|
| 224 | mergedShelve["detector_20Tally"].addMeasures(she["detector_20Tally"])
|
|---|
| 225 | mergedShelve["detector_40Tally"].addMeasures(she["detector_40Tally"])
|
|---|
| 226 |
|
|---|
| 227 | if G4analysisUse:
|
|---|
| 228 | def mergeData(mergedName, shelveList):
|
|---|
| 229 | mergedShelveName = mergedName + ".shelve"
|
|---|
| 230 | print "creating merged shelve: ", mergedShelveName
|
|---|
| 231 | mergedShelve = shelve.open(mergedShelveName)
|
|---|
| 232 | mergedXMLname = mergedName + ".xml"
|
|---|
| 233 | print "creating merged XML store: ", mergedXMLname
|
|---|
| 234 | mergedXMLStore = myPI.tf.create(mergedXMLname, "xml", 0, 1)
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | for i in range(len(shelveList)):
|
|---|
| 238 | shelveName = shelveList[i]
|
|---|
| 239 | print "adding data from: ", shelveName
|
|---|
| 240 | she = shelve.open(shelveName,"r")
|
|---|
| 241 | xmlStoreName = she["xmlStoreName"]
|
|---|
| 242 | print "opening: ", xmlStoreName
|
|---|
| 243 | xmlStore = myPI.tf.create(xmlStoreName,"xml",1,0)
|
|---|
| 244 | if i == 0:
|
|---|
| 245 | if mergedXMLStore.listObjectNames() == ():
|
|---|
| 246 | setUpMergedShelve(she, mergedShelve, mergedXMLname, shelveName)
|
|---|
| 247 | coppyTrees(xmlStore, mergedXMLStore)
|
|---|
| 248 | else:
|
|---|
| 249 | if comparableShelves(she, mergedXMLStore):
|
|---|
| 250 | addToShelve(mergedShelve, she, shelveName)
|
|---|
| 251 | addToXML(mergedXMLStore, xmlStore)
|
|---|
| 252 | else:
|
|---|
| 253 | addToShelve(mergedShelve, she, shelveName)
|
|---|
| 254 | addToXML(mergedXMLStore, xmlStore)
|
|---|
| 255 |
|
|---|
| 256 | mergedShelve.close()
|
|---|
| 257 | mergedXMLStore.commit()
|
|---|
| 258 | mergedXMLStore.close()
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 | def rmPath(she):
|
|---|
| 262 | xmlFile = she["xmlStoreName"]
|
|---|
| 263 | n = string.rfind(xmlFile,"/")
|
|---|
| 264 | if n > -1:
|
|---|
| 265 | xmlFile = xmlFile[n+1:]
|
|---|
| 266 | she["xmlStoreName"] = xmlFile
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|