| 1 | #!/usr/bin/python
|
|---|
| 2 | # ==================================================================
|
|---|
| 3 | # python script for Geant4Py test
|
|---|
| 4 | #
|
|---|
| 5 | # gtest06
|
|---|
| 6 | # - test for constructing/visualizing boolean geoemtries
|
|---|
| 7 | # ==================================================================
|
|---|
| 8 | from Geant4 import *
|
|---|
| 9 | import g4py.ExN01pl, g4py.ParticleGun
|
|---|
| 10 |
|
|---|
| 11 | # ==================================================================
|
|---|
| 12 | # user actions in python
|
|---|
| 13 | # ==================================================================
|
|---|
| 14 | class MyDetectorConstruction(G4VUserDetectorConstruction):
|
|---|
| 15 | "My Detector Construction"
|
|---|
| 16 |
|
|---|
| 17 | def __init__(self):
|
|---|
| 18 | G4VUserDetectorConstruction.__init__(self)
|
|---|
| 19 | self.air= gNistManager.FindOrBuildMaterial("G4_AIR")
|
|---|
| 20 | self.lv_object= None
|
|---|
| 21 | self.world= self.ConstructWorld()
|
|---|
| 22 |
|
|---|
| 23 | self.va_red= G4VisAttributes(G4Color(1.,0.,0.))
|
|---|
| 24 | self.va_cyan= G4VisAttributes(G4Color(0.,1.,1.))
|
|---|
| 25 | self.va_green= G4VisAttributes(G4Color(0.,1.,0.))
|
|---|
| 26 | self.va_blue= G4VisAttributes(G4Color(0.,0.,1.))
|
|---|
| 27 | self.va_magenta= G4VisAttributes(G4Color(1.,0.,1.))
|
|---|
| 28 |
|
|---|
| 29 | self.sld_box= G4Box("box",20.*cm, 20.*cm, 20.*cm);
|
|---|
| 30 | self.sld_cyl= G4Tubs("cylinder",0., 10.*cm, 30.*cm, 0., twopi)
|
|---|
| 31 |
|
|---|
| 32 | # -----------------------------------------------------------------
|
|---|
| 33 | def ConstructWorld(self):
|
|---|
| 34 | # Python has automatic garbage collection system.
|
|---|
| 35 | # Geometry objects must be defined as GLOBAL not to be deleted.
|
|---|
| 36 | global sld_world, lv_world, pv_world, va_world
|
|---|
| 37 |
|
|---|
| 38 | sld_world= G4Box("world", 1.*m, 1.*m, 1.*m)
|
|---|
| 39 | lv_world= G4LogicalVolume(sld_world, self.air, "world")
|
|---|
| 40 | pv_world= G4PVPlacement(G4Transform3D(), lv_world, "world",
|
|---|
| 41 | None, False, 0)
|
|---|
| 42 |
|
|---|
| 43 | va_world= G4VisAttributes()
|
|---|
| 44 | va_world.SetVisibility(False)
|
|---|
| 45 | lv_world.SetVisAttributes(va_world)
|
|---|
| 46 |
|
|---|
| 47 | # solid object (dummy)
|
|---|
| 48 | global sld_sld, lv_sld, pv_sld
|
|---|
| 49 | sld_sld= G4Box("dummy", 10.*cm, 10.*cm, 10.*cm)
|
|---|
| 50 | self.lv_object= lv_sld= G4LogicalVolume(sld_sld, self.air, "dummy")
|
|---|
| 51 | pv_sld= G4PVPlacement(None, G4ThreeVector(), "dummy", lv_sld,
|
|---|
| 52 | pv_world, False, 0)
|
|---|
| 53 |
|
|---|
| 54 | return pv_world
|
|---|
| 55 |
|
|---|
| 56 | # -----------------------------------------------------------------
|
|---|
| 57 | def ConstructUnion(self):
|
|---|
| 58 | global sld_union
|
|---|
| 59 | sld_union= G4UnionSolid("box+cylinder", self.sld_box, self.sld_cyl);
|
|---|
| 60 |
|
|---|
| 61 | self.lv_object.SetSolid(sld_union)
|
|---|
| 62 | self.lv_object.SetVisAttributes(self.va_blue)
|
|---|
| 63 | gRunManager.GeometryHasBeenModified()
|
|---|
| 64 |
|
|---|
| 65 | # -----------------------------------------------------------------
|
|---|
| 66 | def ConstructIntersection(self):
|
|---|
| 67 | offset= G4ThreeVector(20.*cm, 20.*cm, 0.)
|
|---|
| 68 | global sld_intersection
|
|---|
| 69 | sld_intersection= G4IntersectionSolid("box*cylinder",
|
|---|
| 70 | self.sld_box, self.sld_cyl,
|
|---|
| 71 | None, offset)
|
|---|
| 72 |
|
|---|
| 73 | self.lv_object.SetSolid(sld_intersection)
|
|---|
| 74 | self.lv_object.SetVisAttributes(self.va_magenta)
|
|---|
| 75 | gRunManager.GeometryHasBeenModified()
|
|---|
| 76 |
|
|---|
| 77 | # -----------------------------------------------------------------
|
|---|
| 78 | def ConstructSubtraction(self):
|
|---|
| 79 | global sld_subtraction
|
|---|
| 80 | sld_subtraction= G4SubtractionSolid("box-cylinder",
|
|---|
| 81 | self.sld_box, self.sld_cyl)
|
|---|
| 82 |
|
|---|
| 83 | self.lv_object.SetSolid(sld_subtraction)
|
|---|
| 84 | self.lv_object.SetVisAttributes(self.va_red)
|
|---|
| 85 | gRunManager.GeometryHasBeenModified()
|
|---|
| 86 |
|
|---|
| 87 | # -----------------------------------------------------------------
|
|---|
| 88 | def Construct(self): # return the world volume
|
|---|
| 89 | return self.world
|
|---|
| 90 |
|
|---|
| 91 | # ==================================================================
|
|---|
| 92 | # main
|
|---|
| 93 | # ==================================================================
|
|---|
| 94 | # set geometry
|
|---|
| 95 | myDC= MyDetectorConstruction()
|
|---|
| 96 | gRunManager.SetUserInitialization(myDC)
|
|---|
| 97 |
|
|---|
| 98 | # minimal physics list
|
|---|
| 99 | g4py.ExN01pl.Construct()
|
|---|
| 100 |
|
|---|
| 101 | # set primary generator action
|
|---|
| 102 | g4py.ParticleGun.Construct()
|
|---|
| 103 |
|
|---|
| 104 | # initialize
|
|---|
| 105 | gRunManager.Initialize()
|
|---|
| 106 |
|
|---|
| 107 | # visualization
|
|---|
| 108 | gApplyUICommand("/vis/open RayTracer")
|
|---|
| 109 | gApplyUICommand("/vis/rayTracer/headAngle 40.")
|
|---|
| 110 | gApplyUICommand("/vis/rayTracer/eyePosition 100 100 150 cm")
|
|---|
| 111 |
|
|---|
| 112 | # create a vrml file for each solid type
|
|---|
| 113 | f_list= (
|
|---|
| 114 | ("union", myDC.ConstructUnion),
|
|---|
| 115 | ("intersection", myDC.ConstructIntersection),
|
|---|
| 116 | ("subtraction", myDC.ConstructSubtraction)
|
|---|
| 117 | )
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | for s,f in f_list:
|
|---|
| 121 | f.__call__()
|
|---|
| 122 | fname= "%s.jpg" % (s)
|
|---|
| 123 | cmdstr= "/vis/rayTracer/trace " + fname
|
|---|
| 124 | gApplyUICommand(cmdstr)
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|