Advertisement
reeps

NaI Geant4

Apr 12th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.40 KB | None | 0 0
  1. import ROOT
  2. from ROOT import TCanvas
  3. import Geant4
  4. from Geant4 import cm, mm, MeV, deg, rad
  5. import g4py.EMSTDpl
  6. import g4py.NISTmaterials
  7. import g4py.ezgeom
  8. import g4py.ParticleGun
  9. from Geant4 import G4ThreeVector
  10.  
  11. import math
  12. from math import sin, cos, atan2
  13. import sys
  14.  
  15. ############################################################
  16. #                        CONSTANTS                         #
  17. ############################################################
  18. PI = 3.1415926
  19. OUTPUT_DATA = True
  20. # Number of particles to emit:
  21. PARTICLE_COUNT = 10000
  22. # Number of emitted particles which will be slowly displayed
  23. REVIEWED_CASES = 0
  24. # Number of emitted particles which will be displayed
  25. SHOWN_CASES = 0
  26. # Histogram parameters
  27. ENERGY_BIN_COUNT = 100
  28. ANGLE_BIN_COUNT  = 18
  29. ENERGY_BIAS = 20 * MeV
  30. WORLD_SIZE = 200.0 * cm
  31.  
  32. ############################################################
  33. #                 DETECTOR CLASS DEFINITION                #
  34. ############################################################
  35. hits = 0
  36.  
  37. # electron/gamma standard EM physics list
  38. g4py.EMSTDpl.Construct()
  39.  
  40. class MySD(Geant4.G4VSensitiveDetector):
  41.     "My sensitive detector"
  42.     def __init__(self):
  43.         Geant4.G4VSensitiveDetector.__init__(self, "MySD")
  44.         self.eventEnergy = 0.0
  45.     def ProcessHits(self, step, rohist):
  46.         energy = step.GetTotalEnergyDeposit()/MeV
  47.         self.eventEnergy += energy
  48.  
  49.         point = step.GetPreStepPoint().GetPosition()
  50.         self.enX += energy * point.getX() / cm
  51.         self.enY += energy * point.getY() / cm
  52.         self.enZ += energy * point.getZ() / cm
  53.  
  54. class MyEventAction(Geant4.G4UserEventAction):
  55.     def __init__(self, sd, hist, hist2):
  56.         Geant4.G4UserEventAction.__init__(self)
  57.         self.sd    = sd
  58.         self.hist  = hist
  59.         self.hist2 = hist2
  60.         self.count = 0
  61.  
  62.         self.hist2.GetXaxis().SetTitle("Energy (MeV)")
  63.         self.hist2.GetYaxis().SetTitle("Angle (degrees)")
  64.     def BeginOfEventAction(self, event):
  65.         self.sd.enX = 0
  66.         self.sd.enY = 0
  67.         self.sd.enZ = 0
  68.  
  69.         self.count += 1
  70.         if OUTPUT_DATA == True:
  71.             print '==== start of event #', self.count
  72.         self.sd.eventEnergy = 0
  73.     def EndOfEventAction(self, event):
  74.         if self.sd.eventEnergy > ENERGY_BIAS:
  75.             global hits
  76.             hits += 1
  77.         self.hist.Fill(self.sd.eventEnergy)
  78.         self.theta = atan2((self.sd.enX**2 + self.sd.enY**2)**0.5, self.sd.enZ)
  79.         if self.sd.negative == True and self.theta < PI/2.0:
  80.             self.theta = PI - self.theta
  81.         self.hist2.Fill(self.sd.eventEnergy, self.theta * 180.0/PI)
  82.         if OUTPUT_DATA == True:
  83.             print '==== end of event #', self.count, ', energy=', self.sd.eventEnergy
  84.  
  85. ############################################################
  86. #                  INITIALIZING WORLD                      #
  87. ############################################################
  88.  
  89. g4py.ezgeom.ResizeWorld(WORLD_SIZE, WORLD_SIZE, WORLD_SIZE)
  90.  
  91. g4py.NISTmaterials.Construct()
  92. g4py.ezgeom.Construct()
  93.  
  94. # fill the world with air
  95. air = Geant4.G4Material.GetMaterial("G4_AIR")
  96. g4py.ezgeom.SetWorldMaterial(air)
  97.  
  98. # Sodium Iodide is the official name for NaI
  99. NaI = Geant4.G4Material.GetMaterial("G4_SODIUM_IODIDE")
  100. target = g4py.ezgeom.G4EzVolume("Sodium Iodide Sphere")
  101.  
  102. target.CreateShpereVolume(NaI, 25.0*cm, 85.0*cm, 0.0*deg, 360.0*deg, 18.0*deg, 144.0*deg)
  103.  
  104. target.PlaceIt(G4ThreeVector(0.0, 0.0, 0.0))
  105.  
  106. ############################################################
  107. #                INITIALIZING DETECTOR                     #
  108. ############################################################
  109.  
  110. detector = MySD()
  111. target.SetSensitiveDetector(detector)
  112.  
  113. ############################################################
  114. #               INITIALIZING HISTOGRAMS                    #
  115. ############################################################
  116.  
  117. canvas = TCanvas("SomeName", "SomeTitle", 800, 600)
  118. canvas.Divide(2,1)
  119.  
  120. hist  = ROOT.TH1D("hist", "Energy", ENERGY_BIN_COUNT, 0, 800.0)
  121. hist2 = ROOT.TH2D("hist2", "Energy2", ENERGY_BIN_COUNT, 0, 800.0, ANGLE_BIN_COUNT, 0, 180.05)
  122. uaction = MyEventAction(detector, hist, hist2)
  123. Geant4.gRunManager.Initialize()
  124. Geant4.gRunManager.SetUserAction(uaction)
  125.  
  126. Geant4.gApplyUICommand('/control/execute visinit.mac')
  127. Geant4.gApplyUICommand('/vis/enable true')
  128.  
  129. pg = g4py.ParticleGun.Construct()
  130.  
  131. ############################################################
  132. #                     OTHER STUFF                          #
  133. ############################################################
  134.  
  135. pg.SetParticleByName("gamma")
  136. pg.SetParticlePosition( Geant4.G4ThreeVector(0.0, 0.0, 0.0) )
  137. pg.SetParticleEnergy(500.0*MeV)
  138. for i in range(0,PARTICLE_COUNT):
  139.     phi = ROOT.gRandom.Uniform(0.0, 2*PI)
  140.     theta = math.acos(ROOT.gRandom.Uniform(-1.0, 1.0))
  141.  
  142.     if (theta >= PI/2.0):
  143.         uaction.sd.negative = True
  144.     else:
  145.         uaction.sd.negative = False
  146.  
  147.     vx = sin(theta)*cos(phi)
  148.     vy = sin(theta)*sin(phi)
  149.     vz = cos(theta)
  150.     pg.SetParticleMomentumDirection( Geant4.G4ThreeVector(vx, vy, vz) )
  151.     Geant4.gRunManager.BeamOn(1)
  152.     if i < REVIEWED_CASES:
  153.         sys.stdin.readline()
  154.     if i == SHOWN_CASES:
  155.         Geant4.gApplyUICommand('/vis/enable false')
  156.  
  157. canvas.cd(1)
  158. hist.Draw()
  159. canvas.cd(2)
  160. hist2.Draw("surf")
  161. canvas.Update()
  162. print 'Efficiency: ', (float(hits) / PARTICLE_COUNT) * 100.0, '%'
  163. sys.stdin.readline()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement