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