trackers/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=False,macro='macro/gps.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  #gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorActionInit/GenerationInit")
62  #kernel.generatorAction().adopt(gen)
63  #gen.enableUI()
64 
65  #gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorWrapper/GPS")
66  #gen.Uses = 'G4GeneralParticleSource'
67  #gen.OutputLevel = Output.WARNING
68 
71 
72  #gen = DDG4.GeneratorAction(kernel, "Geant4IsotropeGenerator/IsotropPi+")
73  #gen.Mask = 1
74  #gen.Particle = 'e-'
75  #gen.Energy = 5 * GeV
76  #gen.Multiplicity = 2
77  #gen.Distribution = 'cos(theta)'
78 
79  #logger.info("# Install vertex smearing for this interaction")
80  #gen = DDG4.GeneratorAction(kernel, "Geant4InteractionVertexSmear/SmearPi+")
81  #gen.Mask = 1
82  #gen.Offset = (20 * mm, 10 * mm, 10 * mm, 0 * ns)
83  #gen.Sigma = (4 * mm, 1 * mm, 1 * mm, 0 * ns)
84  #kernel.generatorAction().adopt(gen)
85 
86  #logger.info("# Second particle generator: e-")
87  #gen = DDG4.GeneratorAction(kernel, "Geant4IsotropeGenerator/IsotropE-")
88  #gen.Mask = 2
89  #gen.Particle = 'e-'
90  #gen.Energy = 25 * GeV
91  #gen.Multiplicity = 3
92  #gen.Distribution = 'uniform'
93  #kernel.generatorAction().adopt(gen)
94  #logger.info(" Install vertex smearing for this interaction")
95  #gen = DDG4.GeneratorAction(kernel, "Geant4InteractionVertexSmear/SmearE-")
96  #gen.Mask = 2
97  #gen.Offset = (-20 * mm, -10 * mm, -10 * mm, 0 * ns)
98  #gen.Sigma = (12 * mm, 8 * mm, 8 * mm, 0 * ns)
99  #kernel.generatorAction().adopt(gen)
100  # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101 
102  #logger.info("# Merge all existing interaction records")
103  #gen = DDG4.GeneratorAction(kernel, "Geant4InteractionMerger/InteractionMerger")
104  #gen.OutputLevel = 0 # generator_output_level
105  #gen.enableUI()
106  #kernel.generatorAction().adopt(gen)
107 
108 
113 
114  #logger.info("# ....and handle the simulation particles.")
115  #part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
116  #kernel.generatorAction().adopt(part)
117 
127 
128  logger.info("# Setup global filters fur use in sensitive detectors")
129  #f1 = DDG4.Filter(kernel, 'GeantinoRejectFilter/GeantinoRejector')
130  f2 = DDG4.Filter(kernel, 'ParticleRejectFilter/OpticalPhotonRejector')
131  f2.particle = 'opticalphoton'
132  f3 = DDG4.Filter(kernel, 'ParticleSelectFilter/OpticalPhotonSelector')
133  f3.particle = 'opticalphoton'
134  #f4 = DDG4.Filter(kernel, 'EnergyDepositMinimumCut')
135  #f4.Cut = 10 * MeV
136  #f4.enableUI()
137  #kernel.registerGlobalFilter(f1)
138  kernel.registerGlobalFilter(f2)
139  kernel.registerGlobalFilter(f3)
140  #kernel.registerGlobalFilter(f4)
141 
142  logger.info("# First the PID detectors")
143  seq, act = geant4.setupDetector('ForwardRICH','PhotoMultiplierSDAction')
144  act.adopt(f2)
145 
146  #seq, act = geant4.setupTracker('SiVertexBarrel')
147  #seq.adopt(f1)
148 
150 
151  #seq, act = geant4.setupTracker('SiVertexEndcap')
152  #seq.adopt(f1)
153 
154 
155  #seq, act = geant4.setupTracker('SiTrackerBarrel')
156  #seq, act = geant4.setupTracker('SiTrackerEndcap')
157  #seq, act = geant4.setupTracker('SiTrackerForward')
158  #logger.info("# Now setup the calorimeters")
159  #seq, act = geant4.setupCalorimeter('EcalBarrel')
160  #seq, act = geant4.setupCalorimeter('EcalEndcap')
161  #seq, act = geant4.setupCalorimeter('HcalBarrel')
162  #seq, act = geant4.setupCalorimeter('HcalEndcap')
163  #seq, act = geant4.setupCalorimeter('HcalPlug')
164  #seq, act = geant4.setupCalorimeter('MuonBarrel')
165  #seq, act = geant4.setupCalorimeter('MuonEndcap')
166  #seq, act = geant4.setupCalorimeter('LumiCal')
167  #seq, act = geant4.setupCalorimeter('BeamCal')
168 
169  logger.info("# Now build the physics list:")
170  phys = geant4.setupPhysics('QGSP_BERT')
171  geant4.addPhysics(str('Geant4PhysicsList/Myphysics'))
172 
173  ph = DDG4.PhysicsList(kernel, 'Geant4OpticalPhotonPhysics/OpticalPhotonPhys')
174  ph.VerboseLevel = 0
175  ph.enableUI()
176  phys.adopt(ph)
177 
178  ph = DDG4.PhysicsList(kernel, 'Geant4CerenkovPhysics/CerenkovPhys')
179  ph.MaxNumPhotonsPerStep = 10
180  ph.MaxBetaChangePerStep = 10.0
181  ph.TrackSecondariesFirst = True
182  ph.VerboseLevel = 0
183  ph.enableUI()
184  phys.adopt(ph)
185 
186 
189 
190  # Add global range cut
191  rg = geant4.addPhysics('Geant4DefaultRangeCut/GlobalRangeCut')
192  rg.RangeCut = 0.7 * mm
193 
194  #phys.dump()
195 
196  #ui_action = dd4hep.sim.createAction(kernel, "Geant4UIManager/UI")
197  #ui_action.HaveVIS = True
198  #ui_action.HaveUI = True
199  #ui_action.SessionType = qt
200  #ui_action.SetupUI = macro
201  #kernel.registerGlobalAction(ui_action)
202 
203  #ui = geant4.setupUI("qt",vis=True,macro="vis.mac")
204  #ui.enableUI()
205 
206  kernel.configure()
207  kernel.initialize()
208 
209  # DDG4.setPrintLevel(Output.DEBUG)
210  kernel.run()
211  kernel.terminate()
212 
213 
214 if __name__ == "__main__":
215  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