00001
00002
00003 import PyCintex
00004 gbl = PyCintex.makeNamespace("")
00005
00006 import xmldetdesc
00007 xmldetdesc.config()
00008
00009 from Gaudi.Configuration import *
00010 conf = ApplicationMgr()
00011
00012 from GaudiPython.GaudiAlgs import GaudiAlgo
00013 from GaudiPython import *
00014 class MyAlg(GaudiAlgo):
00015 def __init__(self,name):
00016 GaudiAlgo.__init__(self,name)
00017 print "Making MyAlg",name
00018 self.depth = 0
00019
00020 def tab(self):
00021 return " "*self.depth
00022 def out(self,*stuff):
00023
00024 return
00025
00026 def dump_all(self):
00027 from GaudiPython import AppMgr
00028 app = AppMgr()
00029 det = app.detsvc()
00030 lar = det['/dd/Structure/Sites/la-rock']
00031 self.dump_de(lar,None)
00032 return
00033
00034 def initialize(self):
00035 status = GaudiAlgo.initialize(self)
00036 print "Init MyAlg",self.name
00037 if status.isFailure(): return status
00038
00039 app = AppMgr()
00040 self.det = app.detsvc()
00041
00042 import time
00043 for n in range(0,10):
00044 start = time.time()
00045 self.do_one()
00046 stop = time.time()
00047 print "Run %d all AD PMTs in %f seconds"%(n,stop-start)
00048
00049 return SUCCESS
00050
00051 def do_one(self):
00052
00053 touchable_history_template = [
00054 "/dd/Geometry/PMT/lvPmtHemiVacuum#pvPmtHemiCathode",
00055 "/dd/Geometry/PMT/lvPmtHemi#pvPmtHemiVacuum",
00056 "/dd/Geometry/AD/lvOIL#pvAdPmtArray#pvAdPmtArrayRotated#pvAdPmtRingInCyl:%(ring)d#pvAdPmtInRing:%(col)d#pvAdPmtUnit",
00057 "/dd/Geometry/AD/lvSST#pvOIL",
00058 "/dd/Geometry/AD/lvADE#pvSST",
00059 "/dd/Geometry/Pool/lvNearPoolIWS#pvNearADE%(nad)d",
00060 "/dd/Geometry/Pool/lvNearPoolCurtain#pvNearPoolIWS",
00061 "/dd/Geometry/Pool/lvNearPoolOWS#pvNearPoolCurtain",
00062 "/dd/Geometry/Pool/lvNearPoolLiner#pvNearPoolOWS",
00063 "/dd/Geometry/Pool/lvNearPoolDead#pvNearPoolLiner",
00064 "/dd/Geometry/Sites/lvNearHallBot#pvNearPoolDead",
00065 "/dd/Geometry/Sites/lvNearSiteRock#pvNearHallBot",
00066 "/dd/Structure/Sites/%(site)s-rock"
00067 ]
00068
00069
00070 for site in ['la','db']:
00071 for nad in range(0,2):
00072 nad += 1
00073 for ring in range(0,8):
00074 ring += 1
00075 for col in range(0,24):
00076 col += 1
00077
00078 params = { 'ring':ring,'col':col,'nad':nad,'site':site }
00079
00080 th = " ".join(touchable_history_template)%params
00081 touchableHistory = th.split(" ")
00082
00083 self.fix_th(touchableHistory)
00084 de = self.th2de(touchableHistory)
00085 if not de:
00086 print "Failed, remaining TH is:\n%s"%"\n".join(touchableHistory)
00087 continue
00088 continue
00089 continue
00090 continue
00091 return
00092
00093 def fix_th(self,th):
00094 "Expand multiple pv chains"
00095
00096 th.reverse()
00097
00098 index = 0
00099 for entry in th:
00100 index += 1
00101 names = entry.split("#")
00102 if len(names) <= 2: continue
00103 th.pop(index-1)
00104 th.insert(index-1,"%s#%s"%(names[0],names[1]))
00105
00106 lv = self.det[names[0]]
00107 pv = lv[names[1]]
00108 lv = pv.lvolume()
00109
00110 for pvname in names[2:]:
00111 pv = lv[pvname]
00112 th.insert(index,"%s#%s"%(lv.name(),pv.name()))
00113 index += 1
00114 lv = pv.lvolume()
00115 continue
00116 continue
00117 return
00118
00119 def th2de(self,th):
00120 "Return the DetectorElement matching the TouchableHistory"
00121
00122 if not "/dd/Structure" in th[0]:
00123 print "TouchableHistory not grounded in a structure:",th[0]
00124
00125 return None
00126 from GaudiPython import AppMgr
00127 de = self.det[th.pop(0)]
00128 return self.find_de(de,th)
00129
00130 def support_path(self,gi):
00131 "Returns a list of (lv,pv) tuples that is the support path"
00132 if not gi.hasSupport(): return []
00133 support_gi = gi.supportIGeometryInfo()
00134 rpath = gi.supportPath()
00135 lv = support_gi.lvolume()
00136
00137 sp = []
00138 for depth in range(0,rpath.size()):
00139 pv = lv.pvolumes()[rpath[depth]]
00140 sp.append((lv,pv))
00141 self.out("sp: %d %s#%s"%(depth,lv.name(),pv.name()))
00142 lv = pv.lvolume()
00143 continue
00144 return sp
00145
00146 def support_check(self,sp,th):
00147 "Return index of history the support path sp spans, o.w. -1"
00148
00149 index = 0
00150 for lv,pv in sp:
00151 self.out("lv#pv=%s#%s th[%d]=%s"%(lv.name(),pv.name(),index,th[index]))
00152 names = th[index].split("#")
00153 if not lv.name() == names[0]: return -1
00154 if not pv.name() == names[1]: return -1
00155 index += 1
00156 continue
00157 self.out("passed with index %d"%index)
00158 return index
00159
00160 def find_child_de(self,de,th):
00161 """
00162 Find the immediate child of de that has a support consistent
00163 with upper parts of th and remove those parts from th.
00164
00165 """
00166 self.out("Checking children of %s(%s)"%(de.name(),de.geometry().lvolumeName()))
00167 self.depth += 1
00168 children = de.childIDetectorElements()
00169 for ichild in range(0,children.size()):
00170 child = children[ichild]
00171 self.out("child %s(%s)"%(child.name(),child.geometry().lvolumeName()))
00172 sp = self.support_path(child.geometry())
00173 index = self.support_check(sp,th)
00174
00175 if index < 0: continue
00176
00177 del th[:index]
00178
00179 self.out("remaining th:\n%s"%"\n".join(th))
00180 self.depth -= 1
00181 return child
00182
00183 self.depth -= 1
00184 return None
00185
00186
00187 def find_de(self,de,th):
00188
00189 if not th: return de
00190
00191 names = th[0].split("#")
00192 lvname = names.pop(0)
00193
00194 de_lvname = de.geometry().lvolumeName()
00195
00196 self.out('de="%s"("%s") th="%s"[%s]'%(de.name(),de_lvname,lvname,",".join(names)))
00197
00198 if lvname != de_lvname:
00199 print 'Given mismatched DE and TH top "%s" != "%s"'%(lvname,de_lvname)
00200 return None
00201
00202
00203 child_de = self.find_child_de(de,th)
00204 if child_de: return self.find_de(child_de,th)
00205
00206
00207
00208 th.pop(0)
00209 return de
00210
00211
00212
00213 def dump_de(self,de,parent_de):
00214 gi = de.geometry()
00215 self.out(de.name(),gi.lvolumeName())
00216
00217 self.depth += 1
00218 count = 0
00219 for lv,pv in self.support_path(gi):
00220 self.out("%d:%s#%s"%(count,lv.name(),pv.name()))
00221 count += 1
00222 self.depth -= 1
00223
00224 self.depth += 2
00225 children = de.childIDetectorElements()
00226 for ichild in range(0,children.size()):
00227 child = children[ichild]
00228 self.dump_de(child,de)
00229 self.depth -= 2
00230
00231 return
00232
00233 def execute(self):
00234 print "execute"
00235 return SUCCESS
00236
00237 pass
00238
00239
00240
00241 from GaudiPython import AppMgr
00242 app = AppMgr()
00243 app.EvtSel = "NONE"
00244
00245
00246 myalg = MyAlg("MyAlg")
00247 print 'myalg="%s"'%str(myalg)
00248 app.setAlgorithms( [ myalg ] )
00249 print ' running'
00250 app.run(1)
00251