/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

geometry.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """
00003 Module to assist in writing geometry.xml content.
00004 """
00005 
00006 import reference
00007 
00008 class Logvol(reference.Referable):
00009     "A <logvol>"
00010     def __init__(self,name,material,shape,physvols=[]):
00011         reference.Referable.__init__(self,name)
00012         self.material = material
00013         self.shape = shape
00014         self.physvols = physvols
00015         return
00016 
00017     def xml(self,fo,pre):
00018         "Output self as XML to file object fo, prefacing each line with pre"
00019         material = ""
00020         if self.material:
00021             material = 'material="%s"' % self.material
00022         fo.write('%s<logvol name="%s" %s>\n'%(pre,self.name,material))
00023         if self.shape: self.shape.xml(fo,pre+'  ')
00024         for pv in self.physvols:
00025             if isinstance(pv, str):
00026                 fo.write('%s%s\n'%(pre,pv))
00027             else:
00028                 pv.xml(fo,pre+'  ')
00029         fo.write('%s</logvol>\n'%pre)
00030         return
00031 
00032 class Physvol:
00033     "A <physvol>"
00034     def __init__(self,name,logvol,pos=None,rot=None):
00035         self.name = name
00036         self.logvol = logvol
00037         self.pos = pos
00038         self.rot = rot
00039         return
00040 
00041     def xml(self,fo,pre):
00042 
00043         if self.logvol.__class__ == Logvol:
00044             lvname = self.logvol.full_path
00045         else:
00046             lvname = self.logvol
00047 
00048         fo.write('%s<physvol name="%s" logvol="%s">\n'%(pre,self.name,lvname))
00049         if self.pos: self.pos.xml(fo,pre+'  ')
00050         if self.rot: self.rot.xml(fo,pre+'  ')
00051         fo.write('%s</physvol>\n'%pre)
00052         return
00053 
00054 
00055 ### this isn't actually supported.
00056 # class Physvolref:
00057 #     "A <physvolref/>"
00058 #     def __init__(self,url):
00059 #         self.url = url
00060 #         return
00061 #
00062 #     def xml(self,fo,pre):
00063 #         fo.write('%s<physvolref href="%s"/>\n'%(pre,self.url))
00064 
00065 class Boolean:
00066     "A boolean shape, <subtraction>, etc"
00067     def __init__(self,type,name,primary,secondaries=[]):
00068         self.type = type
00069         self.name = name
00070         self.primary = primary
00071         self.secondaries = secondaries
00072         return
00073 
00074     def xml(self,fo,pre):
00075         "Output self as XML to file object fo, prefacing each line with pre"
00076         fo.write('%s<%s name="%s">\n'%(pre,self.type,self.name))
00077         self.primary.xml(fo,pre+'  ')
00078         for sec in self.secondaries:
00079             sec.xml(fo,pre+'  ')
00080         fo.write('%s</%s>\n'%(pre,self.type))
00081         return
00082 
00083 class BooleanSecondary:
00084     "A shape + position + rotation"
00085     def __init__(self,shape,pos,rot):
00086         self.shape = shape
00087         self.pos = pos
00088         self.rot = rot
00089         return
00090     def xml(self,fo,pre):
00091         "Output self as XML to file object fo, prefacing each line with pre"
00092         for thing in [self.shape, self.pos, self.rot]:
00093             thing.xml(fo,pre)
00094         return
00095 
00096 class Box:
00097     "A box shape"
00098     def __init__(self,name,sizeX=None,sizeY=None,sizeZ=None):
00099         self.name = name
00100         self.sizeX = sizeX
00101         self.sizeY = sizeY
00102         self.sizeZ = sizeZ
00103         return
00104     def xml(self,fo,pre):
00105         fo.write('%s<box name="%s"'%(pre,self.name))
00106         if self.sizeX: fo.write(' sizeX="%s"'%self.sizeX)
00107         if self.sizeY: fo.write(' sizeY="%s"'%self.sizeY)
00108         if self.sizeZ: fo.write(' sizeZ="%s"'%self.sizeZ)
00109         fo.write(' />\n')
00110         return
00111 
00112 class Tubs:
00113     "A cylindrical shape"
00114     def __init__(self,name,sizeZ,outerRad,innerRad=None,initPhi=None,dPhi=None):
00115         self.name=name
00116         self.sizeZ=sizeZ
00117         self.outerRad=outerRad
00118         self.innerRad=innerRad
00119         self.initPhi=initPhi
00120         self.dPhi=dPhi
00121         return
00122 
00123     def xml(self,fo,pre):
00124         fo.write('%s<tubs name="%s"'%(pre,self.name))
00125         fo.write(' sizeZ="%s"'%self.sizeZ)
00126         if self.outerRad: fo.write(' outerRadius="%s"'%self.outerRad)
00127         if self.innerRad: fo.write(' innerRadius="%s"'%self.innerRad)
00128         if self.initPhi:  fo.write(' startPhiAngle="%s"'%self.initPhi)
00129         if self.dPhi:     fo.write(' deltaPhiAngle="%s"'%self.dPhi)
00130         fo.write(' />\n')
00131         return;
00132 
00133 class zPlane:
00134     "A zplane element for PolyCone"
00135     def __init__(self, z, outerRadius, innerRadius=None):
00136         self.z=z
00137         self.outerRadius=outerRadius
00138         self.innerRadius=innerRadius
00139         return
00140 
00141     def xml(self, fo, pre):
00142         fo.write('%s<zplane'%pre+' ')
00143         fo.write('z="%s"\n'%self.z)
00144         fo.write('%souterRadius="%s"\n'%(pre+"        ", self.outerRadius))
00145         if self.innerRadius:
00146             fo.write('%sinnerRadius="%s"\n'%(pre+"        ", self.innerRadius))
00147         fo.write('%s        />\n'%pre)
00148         return
00149 
00150 class polyCone:
00151     "A PolyCone with N sections"
00152     def __init__(self, name, zPos, outerRad, 
00153                  innerRad=None, initPhi=None, dPhi=None):
00154         self.zP=[]
00155         self.name=name
00156         self.initPhi=initPhi
00157         self.dPhi=dPhi
00158         self.nSection=len(zPos)
00159         print "polyCone # of zPlanes: ", self.nSection
00160         for i in range(self.nSection):
00161             if innerRad:
00162                 self.zP.append(zPlane(zPos[i], outerRad[i], innerRad[i]))
00163             else: 
00164                 self.zP.append(zPlane(zPos[i], outerRad[i]))
00165         return
00166     """
00167     def xml(self, fo, pre):
00168         fo.write('%s<polycone name="%s"'%(pre,self.name))
00169         if self.initPhi:  
00170             fo.write('\n%sstartPhiAngle="%s"'%(pre+'    ', self.initPhi))
00171         if self.dPhi:  
00172             fo.write('\n%sdeltaPhiAngle="%s"'%(pre+'    ', self.dPhi))
00173         fo.write('>\n')
00174         for i in range(self.nSection):
00175             self.zP[i].xml(fo, pre+'  ')
00176         fo.write('%s</polycone>\n'%pre)
00177         return;
00178     """
00179     def xml(self, fo, pre):
00180         if self.name in ["oav","lso","iav","gds"]:
00181             # Hack to use union of cylinder plus polycone
00182             fo.write('%s<union name="%s">\n'%(pre,self.name))
00183             fo.write('%s<tubs name="%s_cyl"\n'%(pre+'  ',self.name))
00184             fo.write('%s      sizeZ="%s"\n'%(pre+'  ',self.zP[1].z))
00185             fo.write('%s      outerRadius="%s"\n'%(pre+'  ',self.zP[1].outerRadius))
00186             fo.write('%s      />\n'%(pre+'  '))
00187             fo.write('%s<polycone name="%s_polycone"'%(pre+'  ',self.name))
00188             if self.initPhi:  
00189                 fo.write('\n%sstartPhiAngle="%s"'%(pre+'      ', self.initPhi))
00190             if self.dPhi:  
00191                 fo.write('\n%sdeltaPhiAngle="%s"'%(pre+'      ', self.dPhi))
00192             fo.write('>\n')
00193             for i in range(2,self.nSection):
00194                 self.zP[i].xml(fo, pre+'    ')
00195             fo.write('%s</polycone>\n'%(pre+'  '))
00196             fo.write('%s<posXYZ z="-(%s)/2"/>\n'%(pre+'  ',self.zP[1].z))
00197             fo.write('%s</union>\n'%pre)
00198             return
00199         else:
00200             fo.write('%s<polycone name="%s"'%(pre,self.name))
00201             if self.initPhi:  
00202                 fo.write('\n%sstartPhiAngle="%s"'%(pre+'    ', self.initPhi))
00203             if self.dPhi:  
00204                 fo.write('\n%sdeltaPhiAngle="%s"'%(pre+'    ', self.dPhi))
00205             fo.write('>\n')
00206             for i in range(self.nSection):
00207                 self.zP[i].xml(fo, pre+'  ')
00208             fo.write('%s</polycone>\n'%pre)
00209             return
00210 
00211 
00212 class PosXYZ:
00213     'A cartesian placement'
00214     def __init__(self,X=None,Y=None,Z=None):
00215         self.X = X
00216         self.Y = Y
00217         self.Z = Z
00218         return
00219     def xml(self,fo,pre):
00220         fo.write('%s<posXYZ'%pre)
00221         if self.X: fo.write(' x="%s"'%self.X)
00222         if self.Y: fo.write(' y="%s"'%self.Y)
00223         if self.Z: fo.write(' z="%s"'%self.Z)
00224         fo.write(' />\n')
00225         return
00226 
00227 class RotXYZ:
00228     'A rotation about the axes'
00229     def __init__(self,rotX=None,rotY=None,rotZ=None):
00230         self.rotX = rotX
00231         self.rotY = rotY
00232         self.rotZ = rotZ
00233         return
00234     def xml(self,fo,pre):
00235         fo.write('%s<rotXYZ'%pre)
00236         if self.rotX: fo.write(' rotX="%s"'%self.rotX)
00237         if self.rotY: fo.write(' rotY="%s"'%self.rotY)
00238         if self.rotZ: fo.write(' rotZ="%s"'%self.rotZ)
00239         fo.write(' />\n')
00240         return
00241     
00242 if '__main__' == __name__:
00243     import sys
00244 
00245     # Logical volumes
00246     world = Logvol('lvWorld','Air',Box('world_box','10*m','10*m','10*m'))
00247     sd = Logvol('lvSubDetector','Water',Box('subdet_box','1*m','1*m','1*m'))
00248     ssd = Logvol('lvSubSubDetector','Iron',Box('subsubdet_box','10*cm','10*cm','10*cm'))
00249 
00250     # Catalogs
00251     from catalog import *
00252     from reference import *
00253     top = Catalog("Geometry", [world])
00254     sc = Catalog("Sub",[sd])
00255     ssc = Catalog("SubSub",[ssd])
00256     top.refs=[Reference("#Sub",sc),Reference("#SubSub",ssc)]
00257 
00258     # Physical volumes
00259     world.physvols = [Physvol('pvSubDet1',sd,PosXYZ(Z="5*m")),
00260                       Physvol('pvSubDet2',sd,PosXYZ(Z="-5*m"))]
00261     sd.physvols = [Physvol('pvSubSubDet',ssd,RotXYZ(rotZ="45*degree"))]
00262 
00263     # Dump
00264     for c in [top, sc, ssc]:
00265         c.xml(sys.stdout,'')
00266 
00267     pass
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:56:07 for XmlDetDesc by doxygen 1.7.4