Advertisement
reeps

baluev5.1.2

Apr 12th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.69 KB | None | 0 0
  1. #!/usr/bin/python
  2. import ROOT, Geant4, math, sys
  3. from Geant4 import cm, cm3, mm, MeV, g, mole, m, deg
  4. from Geant4 import G4Element, G4Material, G4Box
  5. from Geant4 import G4LogicalVolume, G4ThreeVector
  6. import g4py.EMSTDpl
  7. import g4py.NISTmaterials
  8. import g4py.ezgeom
  9. import g4py.ParticleGun
  10. g4py.EMSTDpl.Construct() # electron/gamma standard EM physics list
  11. g4py.NISTmaterials.Construct()
  12. g4py.ezgeom.Construct()
  13. # fill the world with air
  14. g4py.ezgeom.ResizeWorld(2.*m, 2.*m, 4.*m)
  15. g4py.ezgeom.SetWorldMaterial(Geant4.G4Material.GetMaterial('G4_AIR'))
  16.  
  17.  
  18.  
  19. au = G4Material.GetMaterial("G4_Au")
  20. Pb = G4Material.GetMaterial("G4_Pb")
  21.  
  22. maxEnGun = 10.0
  23.  
  24. # Create a detector
  25. class ClrSD(Geant4.G4VSensitiveDetector):
  26.     "My sensitive detector"
  27.     def __init__(self):
  28.         Geant4.G4VSensitiveDetector.__init__(self, "ClrSD")
  29.         self.eventEnergy = 0.0
  30.         self.vec = (0., 0., 0.)
  31.         #self.hist2 = hist2
  32.     def ProcessHits(self, step, rohist):
  33. #       print "hist"
  34.         energy = (step.GetTotalEnergyDeposit())/MeV
  35.         self.eventEnergy += energy
  36.         pos = step.GetPostStepPoint().GetPosition()
  37.         #theta = step.GetPostStepPoint().GetPosition().getTheta()
  38.     cofVec = 1. / 1*cm
  39.         self.vec = ( cofVec*(self.vec[0] + pos.getX() * energy),
  40.                 cofVec*(self.vec[1] + pos.getY() * energy),
  41.                 cofVec*(self.vec[2] + pos.getZ() * energy))
  42.         #theta = math.atan(pos.getRho() / pos.getZ())
  43.         #hist2.Fill(theta, energy, 1)
  44.  
  45.  
  46. class ClrEventAction(Geant4.G4UserEventAction):
  47.     def __init__(self, sd, ad, hist, hist2, histab, hist2ab):
  48.         Geant4.G4UserEventAction.__init__(self)
  49.         self.sd = sd
  50.         self.ad = ad
  51.         self.hist = hist
  52.         self.hist2 = hist2
  53.         self.histab = histab
  54.         self.hist2ab = hist2ab
  55.  
  56.         self.count = 0
  57.         self.count_high_energy = 0
  58.  
  59.         self.countab = 0
  60.         self.count_high_energyab = 0
  61.  
  62.     def BeginOfEventAction(self, event):
  63.         self.count += 1
  64.         #print '==== start of event #', self.count
  65.         self.sd.eventEnergy = 0.
  66.         self.sd.vec = (0., 0., 0.)
  67.  
  68.         self.ad.eventEnergy = 0.
  69.         self.ad.vec = (0., 0., 0.)
  70.  
  71.  
  72.     def EndOfEventAction(self, event):
  73.         if self.sd.eventEnergy :
  74.             cofVecsd = 1. / (self.sd.eventEnergy )
  75.             self.hist.Fill(self.sd.eventEnergy)
  76.  
  77.             self.sd.vec = ( cofVecsd * self.sd.vec[0] , cofVecsd * self.sd.vec[1] , cofVecsd * self.sd.vec[2] )
  78.             self.hist2.Fill(cofVecsd * self.sd.vec[0], cofVecsd * self.sd.vec[1])
  79.             print cofVecsd * self.sd.vec[0], cofVecsd * self.sd.vec[1]
  80.             print '== element ', 1 , ' end of event #', self.count, ', energy=', self.sd.eventEnergy
  81.             # if self.sd.eventEnergy > 0.01 * MeV:
  82.             #     self.count_high_energy += 1
  83.  
  84.         if self.ad.eventEnergy:
  85.         cofVecad = 1. / (self.ad.eventEnergy * cm )
  86.             self.histab.Fill(self.ad.eventEnergy)
  87.  
  88.             self.ad.vec = (cofVecad * self.ad.vec[0] , cofVecad * self.ad.vec[1] , cofVecad * self.ad.vec[2] )
  89.             self.hist2ab.Fill(cofVecad * self.ad.vec[0], cofVecad * self.ad.vec[1])
  90.             print '== element ', 2 , ' end of event #', self.countab, ', energy=', self.ad.eventEnergy
  91.             # if self.sd.eventEnergy > 1 * MeV:
  92.             #     self.count_high_energy += 1
  93.  
  94.  
  95. calorimeter = g4py.ezgeom.G4EzVolume('Calorimeter')
  96. calorimeter.CreateBoxVolume(au, 10.*cm, 10.*cm, 1.*mm)
  97. calorimeter.PlaceIt(Geant4.G4ThreeVector(0.0, 0.0, 0.5*m-0.5*mm))
  98.  
  99. absorber = g4py.ezgeom.G4EzVolume('Absorber')
  100. absorber.CreateBoxVolume(Pb, 1.*m, 1.*m, 1.*m)
  101. absorber.PlaceIt(Geant4.G4ThreeVector(0.0, 0.0, -1*mm))
  102.  
  103.  
  104. print "1"
  105. histSlr = ROOT.TH1F("histSlr", "Energy", 100, 0., maxEnGun*1.1) # name, description, nbins, low, high
  106. hist2Slr =ROOT.TH2F("hist2Slr", "histogram title", 100, -15., 15., 100, -15., 15.);
  107.  
  108. histAbs = ROOT.TH1F("histAbs", "Energy2", 100, 0., maxEnGun*1.1) # name, description, nbins, low, high
  109. hist2Abs =ROOT.TH2F("hist2Abs", "histogram title2", 100, -15., 15., 100, -15., 15.);
  110.  
  111. detector = ClrSD()
  112. detectorAbs = ClrSD()
  113.  
  114. calorimeter.SetSensitiveDetector(detector)
  115. absorber.SetSensitiveDetector(detectorAbs)
  116.  
  117. uaction = ClrEventAction(detector, detectorAbs, histSlr, hist2Slr, histAbs, hist2Abs )
  118. # absuaction = ClrEventAction(detectorAbs, histAbs, hist2Abs, 2)
  119.  
  120. Geant4.gRunManager.Initialize()
  121.  
  122. # ???
  123. Geant4.gRunManager.SetUserAction(uaction)
  124. # Geant4.gRunManager.SetUserAction(absuaction)
  125.  
  126. Geant4.gApplyUICommand("/control/execute vis.mac") # enable drawing
  127. Geant4.gApplyUICommand("/vis/disable") # disable drawing
  128.  
  129.  
  130. print "2"
  131. pg = g4py.ParticleGun.Construct()
  132. print "3"
  133. pg.SetParticleByName("e-") # particle type:
  134. pg.SetParticlePosition(Geant4.G4ThreeVector(0.0*m, 0.0*m+1*mm, 1.5*m)) # starting point
  135. pg.SetParticleEnergy(maxEnGun*MeV) # energy
  136. for i in range(10): #1000
  137.     phi = ROOT.gRandom.Uniform(0.0, 2 * math.pi)
  138.     theta = ROOT.gRandom.Gaus(0.0, 5.0*math.pi/180.0) #  5
  139.     vx = math.sin(theta) * math.cos(phi)
  140.     vy = math.sin(theta) * math.sin(phi)
  141.     vz = -math.cos(theta)
  142.     if i == 10:
  143.         Geant4.gApplyUICommand("/vis/disable") # disable drawing
  144.     pg.SetParticleMomentumDirection(Geant4.G4ThreeVector(vx, vy, vz))
  145.     Geant4.gRunManager.BeamOn(1) # fire one particle
  146. print "4"
  147. # print 'High energy:', uaction.get_count_high_energy(),
  148. # print 'ratio:', uaction.get_ratio()
  149.  
  150. c = ROOT.TCanvas('lll', 'nnn')
  151. c.Divide(2, 2)
  152. c.cd(1)
  153. histSlr.Draw()
  154. c.cd(2)
  155. hist2Slr.Draw("lego")
  156. c.cd(3)
  157. histAbs.Draw()
  158. c.cd(4)
  159. hist2Abs.Draw("lego2")
  160.  
  161. # c2 = ROOT.TCanvas('lll2', 'nnn2')
  162. # c2.Divide(1, 2)
  163. # c2.cd(1)
  164. # histAbs.Draw()
  165.  
  166. # c2.cd(2)
  167. # hist2Abs.Draw("lego")
  168.  
  169.  
  170.  
  171. sys.stdin.readline()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement