Advertisement
reeps

geant4koryak

Apr 12th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. import Geant4
  2. import ROOT
  3. import sys
  4. import math
  5. import ROOT
  6. import Geant4
  7. from Geant4 import cm, mm, MeV
  8. import g4py.EMSTDpl
  9. import g4py.NISTmaterials
  10. import g4py.ezgeom
  11. import g4py.ParticleGun
  12.  
  13.  
  14. class MySD(Geant4.G4VSensitiveDetector):
  15.     "My sensitive detector"
  16.     def __init__(self):
  17.         Geant4.G4VSensitiveDetector.__init__(self, "MySD")
  18.         self.eventEnergy = 0.0
  19.         self.eventX = 0.0      
  20.         self.eventY = 0.0
  21.         self.eventZ = 0.0
  22.         self.psum = Geant4.G4ThreeVector()
  23.     def ProcessHits(self, step, rohist):
  24.         energy = step.GetTotalEnergyDeposit()/MeV
  25.         point = step.GetPreStepPoint().GetPosition()
  26.         self.psum += energy * point
  27.         self.eventEnergy += energy
  28.  
  29. class MyEventAction(Geant4.G4UserEventAction):
  30.     def __init__(self, sd1, sd2, hist1, hist2, hist3):
  31.         Geant4.G4UserEventAction.__init__(self)    
  32.         self.sd1 = sd1
  33.         self.hist1 = hist1
  34.         self.sd2 = sd2
  35.         self.hist2 = hist2
  36.         self.hist3 = hist3
  37.         self.count = 0
  38.  
  39.     def BeginOfEventAction(self, event):
  40.  
  41.         self.count += 1
  42.         #print "==== start of event #", self.count
  43.         self.sd1.eventEnergy = 0
  44.         self.sd2.eventEnergy = 0
  45.         self.sd1.psum = Geant4.G4ThreeVector()
  46.     def EndOfEventAction(self, event):
  47.         self.hist1.Fill(self.sd1.eventEnergy)
  48.         self.hist2.Fill(self.sd2.eventEnergy)
  49.         #print "==== end of event #", self.count
  50.         if self.sd1.eventEnergy > 0. :
  51.                     sumP = (1. / self.sd1.eventEnergy) * self.sd1.psum
  52.                     self.hist3.Fill(sumP.x/cm,sumP.y/cm)
  53.                     #print "HIT!"
  54.                     #print "X="+str(sumP.x)
  55.                     #print "Y="+str(sumP.y)
  56.  
  57. c= ROOT.TCanvas("c")
  58. c.Clear()
  59. c.Divide(2,2)
  60. c.cd(1)
  61. # electron/gamma standard EM physics list
  62. g4py.EMSTDpl.Construct()
  63. g4py.NISTmaterials.Construct()
  64. #print Geant4.gMaterialTable
  65.  
  66. g4py.ezgeom.Construct()
  67. # fill the world with air
  68. air = Geant4.G4Material.GetMaterial("G4_AIR")
  69. g4py.ezgeom.SetWorldMaterial(air)
  70. g4py.ezgeom.ResizeWorld(500.0*cm,500.0*cm,500.0*cm)
  71.  
  72. Au = Geant4.G4Material.GetMaterial("G4_Au")
  73. target1 = g4py.ezgeom.G4EzVolume("Plast")
  74. target1.CreateBoxVolume(Au, 10*cm, 10*cm, 1.0*mm)
  75. target1.PlaceIt(Geant4.G4ThreeVector(0.0*cm, 0.0*cm, 50*cm+0.5*mm))
  76.  
  77. Pb = Geant4.G4Material.GetMaterial("G4_Pb")
  78. target2 = g4py.ezgeom.G4EzVolume("Cub")
  79. target2.CreateBoxVolume(Pb, 100.0*cm, 100.0*cm, 100.0*cm)
  80. target2.PlaceIt(Geant4.G4ThreeVector(0.0, 0.0, 0.0))
  81.  
  82.  
  83.  
  84. detector1 = MySD()
  85. target1.SetSensitiveDetector(detector1)
  86.  
  87. detector2 = MySD()
  88. target2.SetSensitiveDetector(detector2)
  89.  
  90.  
  91. hist1 = ROOT.TH1D("hist1", "EnergyPlast", 1000, 0, 20)
  92. hist2 = ROOT.TH1D("hist2", "EnergyCub", 1000, 0, 20)
  93. hist3 = ROOT.TH2F("hist3", "average X Y", 100, -10. , 10., 100 , -10. , 10.)
  94.  
  95. uaction = MyEventAction(detector1,detector2, hist1,hist2,hist3)
  96. Geant4.gRunManager.Initialize()
  97. Geant4.gRunManager.SetUserAction(uaction)
  98.  
  99. Geant4.gApplyUICommand("/control/execute vic.mac")
  100. Geant4.gApplyUICommand("/vis/enable true")
  101.  
  102.  
  103. pg = g4py.ParticleGun.Construct()
  104. pg.SetParticleByName("e-")
  105. #pg.SetParticlePosition( Geant4.G4ThreeVector(0.0*cm,50*cm+1*mm+100*cm,0.0*cm) )
  106. pg.SetParticlePosition( Geant4.G4ThreeVector(0.0*cm,0.0*cm,50*cm+1*mm+100*cm) )
  107.  
  108. pg.SetParticleEnergy(10.0*MeV)
  109.  
  110.  
  111. for i in range(0,10000):
  112.     if(i==10):
  113.         Geant4.gApplyUICommand("/vis/enable false")
  114.     phi   = ROOT.gRandom.Uniform(0.0, 2*math.pi)
  115.     theta = ROOT.gRandom.Gaus(0.0, 5.0*math.pi/180)
  116.     vx = math.sin(theta)*math.cos(phi)
  117.     vy = math.sin(theta)*math.sin(phi)
  118.     vz = math.cos(theta)
  119.     pg.SetParticleMomentumDirection( Geant4.G4ThreeVector(vx, vy, -1*vz) )
  120.     Geant4.gRunManager.BeamOn(1)
  121.     if(i<=10):
  122.         raw_input('--> ')
  123.  
  124. hist1.Draw()
  125. c.cd(2)
  126. hist2.Draw()
  127. c.cd(3)
  128. hist3.Draw("box")
  129.  
  130. print "=============="
  131. print "=============="
  132. print "=============="
  133. print "=============="
  134. print "=============="
  135. raw_input('--> ')
  136. raw_input('--> ')
  137. raw_input('--> ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement