Advertisement
reeps

Igor's Geant4

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