pid/compact/scripts/sim_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 from __future__ import absolute_import, unicode_literals
4 import os
5 import time
6 import logging
7 import DDG4
8 from DDG4 import OutputLevel as Output
9 from g4units import keV, GeV, mm, ns, MeV
10 
11 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARNING)
12 logger = logging.getLogger(__name__)
13 
14 def run():
15 
16  os.environ['G4UI_USE_TCSH'] = "1"
17  kernel = DDG4.Kernel()
18  description = kernel.detectorDescription()
19  kernel.loadGeometry(str("file:" + "GenericRICH_example.xml"))
20  DDG4.importConstants(description)
21 
22  geant4 = DDG4.Geant4(kernel)#, tracker='Geant4TrackerCombineAction')
23  geant4.printDetectors()
24 
25  logger.info("""
26  Configure I/O
27  """)
28 
29  rootoutput = geant4.setupROOTOutput('RootOutput', 'derp_' + time.strftime('%Y-%m-%d_%H-%M'), mc_truth=True)
30  rootoutput.HandleMCTruth = True
31 
32  logger.info("# Configure UI")
33  geant4.setupUI('tcsh',vis=True,macro='macro/run.mac')
34 
35  logger.info("# Configure G4 magnetic field tracking")
36  geant4.setupTrackingField()
37 
38  logger.info("# Setup random generator")
39  rndm = DDG4.Action(kernel, 'Geant4Random/Random')
40  rndm.Seed = 987654321
41  rndm.initialize()
42  # rndm.showStatus()
43 
44  logger.info("# Configure Run actions")
45  #run1 = DDG4.RunAction(kernel, 'Geant4TestRunAction/RunInit')
46  #run1.Property_int = 12345
47  #run1.Property_double = -5e15 * keV
48  #run1.Property_string = 'Startrun: Hello_2'
49  #logger.info("%s %s %s", run1.Property_string, str(run1.Property_double), str(run1.Property_int))
50  #run1.enableUI()
51  #kernel.registerGlobalAction(run1)
52  #kernel.runAction().adopt(run1)
53 
54  #logger.info("# Configure Event actions")
55  #prt = DDG4.EventAction(kernel, 'Geant4ParticlePrint/ParticlePrint')
56  #prt.OutputLevel = Output.WARNING
57  #prt.OutputType = 0 # Print both: table and tree
58  #kernel.eventAction().adopt(prt)
59 
60  geant4.setupGun('electron','e-',3.0*GeV)
61 
62  #gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorActionInit/GenerationInit")
63  #kernel.generatorAction().adopt(gen)
64  #gen.enableUI()
65 
66  #gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorWrapper/GPS")
67  #gen.Uses = 'G4GeneralParticleSource'
68  #gen.OutputLevel = Output.WARNING
69 
72 
73  #gen = DDG4.GeneratorAction(kernel, "Geant4IsotropeGenerator/IsotropPi+")
74  #gen.Mask = 1
75  #gen.Particle = 'e-'
76  #gen.Energy = 5 * GeV
77  #gen.Multiplicity = 2
78  #gen.Distribution = 'cos(theta)'
79 
80  #logger.info("# Install vertex smearing for this interaction")
81  #gen = DDG4.GeneratorAction(kernel, "Geant4InteractionVertexSmear/SmearPi+")
82  #gen.Mask = 1
83  #gen.Offset = (20 * mm, 10 * mm, 10 * mm, 0 * ns)
84  #gen.Sigma = (4 * mm, 1 * mm, 1 * mm, 0 * ns)
85  #kernel.generatorAction().adopt(gen)
86 
87  #logger.info("# Second particle generator: e-")
88  #gen = DDG4.GeneratorAction(kernel, "Geant4IsotropeGenerator/IsotropE-")
89  #gen.Mask = 2
90  #gen.Particle = 'e-'
91  #gen.Energy = 25 * GeV
92  #gen.Multiplicity = 3
93  #gen.Distribution = 'uniform'
94  #kernel.generatorAction().adopt(gen)
95  #logger.info(" Install vertex smearing for this interaction")
96  #gen = DDG4.GeneratorAction(kernel, "Geant4InteractionVertexSmear/SmearE-")
97  #gen.Mask = 2
98  #gen.Offset = (-20 * mm, -10 * mm, -10 * mm, 0 * ns)
99  #gen.Sigma = (12 * mm, 8 * mm, 8 * mm, 0 * ns)
100  #kernel.generatorAction().adopt(gen)
101  # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102 
103  #logger.info("# Merge all existing interaction records")
104  #gen = DDG4.GeneratorAction(kernel, "Geant4InteractionMerger/InteractionMerger")
105  #gen.OutputLevel = 0 # generator_output_level
106  #gen.enableUI()
107  #kernel.generatorAction().adopt(gen)
108 
109 
114 
115  #logger.info("# ....and handle the simulation particles.")
116  #part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
117  #kernel.generatorAction().adopt(part)
118 
128 
129  logger.info("# Setup global filters fur use in sensitive detectors")
130  #f1 = DDG4.Filter(kernel, 'GeantinoRejectFilter/GeantinoRejector')
131  f2 = DDG4.Filter(kernel, 'ParticleRejectFilter/OpticalPhotonRejector')
132  f2.particle = 'opticalphoton'
133  f3 = DDG4.Filter(kernel, 'ParticleSelectFilter/OpticalPhotonSelector')
134  f3.particle = 'opticalphoton'
135  #f4 = DDG4.Filter(kernel, 'EnergyDepositMinimumCut')
136  #f4.Cut = 10 * MeV
137  #f4.enableUI()
138  #kernel.registerGlobalFilter(f1)
139  kernel.registerGlobalFilter(f2)
140  kernel.registerGlobalFilter(f3)
141  #kernel.registerGlobalFilter(f4)
142 
143  logger.info("# First the PID detectors")
144  seq, act = geant4.setupDetector('ForwardRICH','PhotoMultiplierSDAction')
145  act.adopt(f2)
146 
147  #seq, act = geant4.setupTracker('SiVertexBarrel')
148  #seq.adopt(f1)
149 
151 
152  #seq, act = geant4.setupTracker('SiVertexEndcap')
153  #seq.adopt(f1)
154 
155 
156  #seq, act = geant4.setupTracker('SiTrackerBarrel')
157  #seq, act = geant4.setupTracker('SiTrackerEndcap')
158  #seq, act = geant4.setupTracker('SiTrackerForward')
159  #logger.info("# Now setup the calorimeters")
160  #seq, act = geant4.setupCalorimeter('EcalBarrel')
161  #seq, act = geant4.setupCalorimeter('EcalEndcap')
162  #seq, act = geant4.setupCalorimeter('HcalBarrel')
163  #seq, act = geant4.setupCalorimeter('HcalEndcap')
164  #seq, act = geant4.setupCalorimeter('HcalPlug')
165  #seq, act = geant4.setupCalorimeter('MuonBarrel')
166  #seq, act = geant4.setupCalorimeter('MuonEndcap')
167  #seq, act = geant4.setupCalorimeter('LumiCal')
168  #seq, act = geant4.setupCalorimeter('BeamCal')
169 
170  logger.info("# Now build the physics list:")
171  phys = geant4.setupPhysics('QGSP_BERT')
172  geant4.addPhysics(str('Geant4PhysicsList/Myphysics'))
173 
174  ph = DDG4.PhysicsList(kernel, 'Geant4OpticalPhotonPhysics/OpticalPhotonPhys')
175  ph.VerboseLevel = 0
176  ph.enableUI()
177  phys.adopt(ph)
178 
179  ph = DDG4.PhysicsList(kernel, 'Geant4CerenkovPhysics/CerenkovPhys')
180  ph.MaxNumPhotonsPerStep = 10
181  ph.MaxBetaChangePerStep = 10.0
182  ph.TrackSecondariesFirst = True
183  ph.VerboseLevel = 0
184  ph.enableUI()
185  phys.adopt(ph)
186 
187 
190 
191  # Add global range cut
192  rg = geant4.addPhysics('Geant4DefaultRangeCut/GlobalRangeCut')
193  rg.RangeCut = 0.7 * mm
194 
195  #phys.dump()
196 
197  #ui_action = dd4hep.sim.createAction(kernel, "Geant4UIManager/UI")
198  #ui_action.HaveVIS = True
199  #ui_action.HaveUI = True
200  #ui_action.SessionType = qt
201  #ui_action.SetupUI = macro
202  #kernel.registerGlobalAction(ui_action)
203 
204  #ui = geant4.setupUI("qt",vis=True,macro="vis.mac")
205  #ui.enableUI()
206 
207  kernel.configure()
208  kernel.initialize()
209 
210  # DDG4.setPrintLevel(Output.DEBUG)
211  kernel.run()
212  kernel.terminate()
213 
214 
215 if __name__ == "__main__":
216  run()
def Action(kernel, nam, shared=False)
Definition: DDG4.py:181
def Filter(kernel, nam, shared=False)
Definition: DDG4.py:185
Kernel
Definition: DDG4.py:67
def PhysicsList(kernel, nam)
Definition: DDG4.py:221
def importConstants(description, namespace=None, debug=False)
Definition: DDG4.py:79