Geant4_10
test.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # ==================================================================
3 # python script for Geant4Py test
4 #
5 # gtest01
6 # - check basic control flow
7 # ==================================================================
8 from Geant4 import *
9 import gtest01
10 import random
11 #import thread
12 
13 # ==================================================================
14 # user actions in python
15 # ==================================================================
16 class MyPrimaryGeneratorAction(G4VUserPrimaryGeneratorAction):
17  "My Primary Generator Action"
18 
19  def __init__(self):
20  G4VUserPrimaryGeneratorAction.__init__(self)
21  self.particleGun= G4ParticleGun(1)
22 
23  def GeneratePrimaries(self, event):
24  #dx= random.gauss(0., 0.1)
25  dx=0.
26  self.particleGun.SetParticleMomentumDirection(G4ThreeVector(dx, 0., 1.))
27  self.particleGun.GeneratePrimaryVertex(event)
28 
29 # ------------------------------------------------------------------
31  "My Run Action"
32 
33  def BeginOfRunAction(self, run):
34  print "*** #event to be processed (BRA)=",
35  run.GetNumberOfEventToBeProcessed()
36 
37  def EndOfRunAction(self, run):
38  print "*** run end run(ERA)=", run.GetRunID()
39 
40 # ------------------------------------------------------------------
42  "My Event Action"
43 
44  #def BeginOfEventAction(self, event):
45  #print "*** current event (BEA)=", event.GetEventID()
46  # pass
47 
48  #def EndOfEventAction(self, event):
49  # print "*** current event (EEA)=", event.GetEventID()
50 
51 # ------------------------------------------------------------------
52 class MySteppingAction(G4UserSteppingAction):
53  "My Stepping Action"
54 
55  def UserSteppingAction(self, step):
56  #print "*** dE/dx in current step=", step.GetTotalEnergyDeposit()
57  track= step.GetTrack()
58  touchable= track.GetTouchable()
59  pv= touchable.GetVolume()
60  #print pv.GetCopyNo()
61  #print touchable.GetReplicaNumber(0)
62 
63 # ------------------------------------------------------------------
65  "My Magnetic Field"
66 
67  def GetFieldValue(self, pos, time):
68  bfield= G4ThreeVector()
69  bfield.x= 0.
70  bfield.y= 5.*tesla
71  bfield.z= 0.
72  return bfield
73 
74 # ==================================================================
75 # main
76 # ==================================================================
77 qMaterials= gtest01.QMaterials()
78 qMaterials.Construct()
79 
80 qDC= gtest01.QDetectorConstruction()
81 gRunManager.SetUserInitialization(qDC)
82 
83 qPL= gtest01.QPhysicsList()
84 gRunManager.SetUserInitialization(qPL)
85 
86 # set user actions...
87 #qPGA= gtest01.QPrimaryGeneratorAction()
89 gRunManager.SetUserAction(myPGA)
90 
91 #myRA= MyRunAction()
92 #gRunManager.SetUserAction(myRA)
93 
94 myEA= MyEventAction()
95 gRunManager.SetUserAction(myEA)
96 
97 mySA= MySteppingAction()
98 gRunManager.SetUserAction(mySA)
99 
100 # set particle gun
101 #ApplyUICommand("/control/execute gun.mac")
102 #pg= qPGA.GetParticleGun()
103 pg= myPGA.particleGun
104 pg.SetParticleByName("e-")
105 pg.SetParticleEnergy(200.*MeV)
106 pg.SetParticlePosition(G4ThreeVector(0.,0.,-14.9)*cm)
107 
108 # magnetic field
109 fieldMgr= gTransportationManager.GetFieldManager()
110 myField= G4UniformMagField(G4ThreeVector(0.,10.*tesla,0.))
111 #myField= MyField()
112 fieldMgr.SetDetectorField(myField)
113 fieldMgr.CreateChordFinder(myField)
114 
115 gRunManager.Initialize()
116 
117 # visualization
118 gControlExecute("vis.mac")
119 
120 # beamOn
121 gRunManager.BeamOn(10)
122 #thread.start_new_thread(gRunManager.BeamOn, (100000))
123 
def BeginOfRunAction
Definition: test.py:32
def UserSteppingAction
Definition: test.py:42
def EndOfRunAction
Definition: test.py:26
def GetFieldValue
Definition: test.py:66
gControlExecute
Definition: __init__.py:167