Advertisement
reeps

Task4mine

Apr 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 KB | None | 0 0
  1. import Geant4
  2. import ROOT
  3. import sys
  4. from math import pi, cos, sin
  5. from Geant4 import cm, mm, MeV
  6. import g4py.EMSTDpl
  7. import g4py.NISTmaterials
  8. import g4py.ezgeom
  9. import g4py.ParticleGun
  10.  
  11. n = int(sys.argv[1])
  12. print n, ' iterations'
  13. offset = 0.001
  14. energy = 6.0
  15. absorbedGamma = 0
  16. #classes for registering of detector responce
  17. class MySD(Geant4.G4VSensitiveDetector):
  18.     "My sensitive detector"
  19.     def __init__(self):
  20.         Geant4.G4VSensitiveDetector.__init__(self, "MySD")
  21.         self.eventEnergy = 0.0
  22.         self.eventX = 0.0
  23.         self.eventY = 0.0
  24.         self.eventZ = 0.0
  25.     def ProcessHits(self, step, rohist):
  26.         energy = step.GetTotalEnergyDeposit() / MeV
  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.     def BeginOfEventAction(self, event):
  39.         self.count += 1
  40.         print '==== start of event #', self.count
  41.         self.sd1.eventEnergy = 0
  42.         self.sd2.eventEnergy = 0
  43.     def EndOfEventAction (self, event):
  44.         self.hist1.Fill(self.sd1.eventEnergy)
  45.         self.hist2.Fill(self.sd2.eventEnergy)
  46.         self.hist3.Fill(self.sd1.eventEnergy / (self.sd1.eventEnergy + self.sd2.eventEnergy))
  47.         global absorbedGamma
  48.         if (self.sd2.eventEnergy >= 0.99 * energy):
  49.             absorbedGamma += 1
  50.        
  51.         print '==== end of event # ', self.count, ', energy1, energy2 = ', self.sd1.eventEnergy, self.sd2.eventEnergy
  52.        
  53.  
  54. #draw
  55. art = ROOT.TCanvas("art")
  56. art.Clear()
  57. art.Divide(2,2)
  58. art.cd(1)
  59.  
  60. #initialization
  61. g4py.EMSTDpl.Construct()
  62. g4py.NISTmaterials.Construct()
  63. g4py.ezgeom.Construct()
  64. #create outer space
  65. air = Geant4.G4Material.GetMaterial("G4_AIR")
  66. g4py.ezgeom.SetWorldMaterial(air)
  67. g4py.ezgeom.ResizeWorld(50.0 * cm, 50.0 * cm, 70.0 * cm)
  68. #detector
  69. Ge = Geant4.G4Material.GetMaterial("G4_Ge")
  70. target1 = g4py.ezgeom.G4EzVolume("Ge Detector")
  71. target1.CreateBoxVolume(Ge, 5.0 * cm, 5.0 * cm, 5.0 * cm)
  72. target1.PlaceIt(Geant4.G4ThreeVector(0.0 * cm, 0.0 * cm, 7.5 * cm))
  73. #absorber
  74. Pb = Geant4.G4Material.GetMaterial("G4_Pb")
  75. target2 = g4py.ezgeom.G4EzVolume("Pb Absorber")
  76. target2.CreateBoxVolume(Pb, 20.0 * cm, 20.0 * cm, 20.0 * cm)
  77. target2.PlaceIt(Geant4.G4ThreeVector(0.0 * cm, 0.0 * cm, 20.0 * cm))
  78. #create sensitive detectors
  79. detector = MySD()
  80. target1.SetSensitiveDetector(detector)
  81.  
  82. absorber = MySD()
  83. target2.SetSensitiveDetector(absorber)
  84. #histagrammsi
  85. hist1 = ROOT.TH1D("first hist", "Emitted energy in detector", 200, -0.1, 6.1)
  86. hist2 = ROOT.TH1D("second hist", "Emitted energy in absorber", 200, -0.1, 6.1)
  87. hist3 = ROOT.TH1D("third hist", "Ratio", 200, -0.1, 1.1)
  88. #action
  89. uaction = MyEventAction(detector, absorber, hist1, hist2, hist3)
  90. Geant4.gRunManager.Initialize()
  91. Geant4.gRunManager.SetUserAction(uaction)
  92.  
  93. Geant4.gApplyUICommand("/control/execute visinit.mac")
  94. Geant4.gApplyUICommand("/vis/enable true")
  95. #create particle gun
  96. pg = g4py.ParticleGun.Construct()
  97. pg.SetParticleByName("gamma")
  98. pg.SetParticlePosition(Geant4.G4ThreeVector(0.0 * cm, 0.0 * cm, 0.0 * cm))
  99. pg.SetParticleEnergy(energy * MeV)
  100. #cycle shoots
  101. for i in range(0,n):
  102.     if (i < 10):
  103.         raw_input()
  104.     if (i == 10):
  105.         Geant4.gApplyUICommand("/vis/enable false")
  106.     phi = ROOT.gRandom.Uniform(0.0, 2.0 * pi)
  107.     theta = ROOT.gRandom.Gaus(0.0, offset * pi / 180)
  108.     vx = sin(theta) * cos(phi)
  109.     vy = sin(theta) * sin(phi)
  110.     vz = cos(theta)
  111.     pg.SetParticleMomentumDirection(Geant4.G4ThreeVector(vx, vy, vz))
  112.     Geant4.gRunManager.BeamOn(1)
  113.  
  114.  
  115.  
  116.  
  117. print 'absorbed photons part: ', 1.0 * absorbedGamma / n
  118.  
  119. hist1.Draw()
  120. art.cd(2)
  121. raw_input()
  122. hist2.Draw()
  123. art.cd(3)
  124. raw_input()
  125. hist3.Draw()
  126. raw_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement