beamline/compact/scripts/vis_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 #
12 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
13 logger = logging.getLogger(__name__)
14 """
15 
16  dd4hep simulation example setup using the python configuration
17 
18  @author M.Frank
19  @version 1.0
20 
21 """
22 
23 
24 def run():
25  os.environ['G4UI_USE_TCSH'] = "1"
26  kernel = DDG4.Kernel()
27  description = kernel.detectorDescription()
28  install_dir = os.environ['DD4hepINSTALL']
29  #kernel.loadGeometry(str("file:" + install_dir + "/DDDetectors/compact/SiD.xml"))
30  kernel.loadGeometry(str("file:" + "eic_beamline_example.xml"))
31  DDG4.importConstants(description)
32 
33  geant4 = DDG4.Geant4(kernel, tracker='Geant4TrackerCombineAction')
34  geant4.printDetectors()
35 
36  logger.info("# Configure UI")
37  geant4.setupUI('tcsh',vis=True,macro='macro/vis.mac')
38 
39  logger.info("# Configure G4 magnetic field tracking")
40  geant4.setupTrackingField()
41 
42  logger.info("# Setup random generator")
43  rndm = DDG4.Action(kernel, 'Geant4Random/Random')
44  rndm.Seed = 987654321
45  rndm.initialize()
46  # rndm.showStatus()
47 
48  logger.info("# Configure Run actions")
49  run1 = DDG4.RunAction(kernel, 'Geant4TestRunAction/RunInit')
50  run1.Property_int = 12345
51  run1.Property_double = -5e15 * keV
52  run1.Property_string = 'Startrun: Hello_2'
53  logger.info("%s %s %s", run1.Property_string, str(run1.Property_double), str(run1.Property_int))
54  run1.enableUI()
55  kernel.registerGlobalAction(run1)
56  kernel.runAction().adopt(run1)
57 
58  logger.info("# Configure Event actions")
59  prt = DDG4.EventAction(kernel, 'Geant4ParticlePrint/ParticlePrint')
60  prt.OutputLevel = Output.INFO
61  prt.OutputType = 3 # Print both: table and tree
62  kernel.eventAction().adopt(prt)
63 
64  logger.info("""
65  Configure I/O
66  """)
67  # evt_lcio = geant4.setupLCIOOutput('LcioOutput','CLICSiD_'+time.strftime('%Y-%m-%d_%H-%M'))
68  # evt_lcio.OutputLevel = Output.ERROR
69 
70  geant4.setupROOTOutput('RootOutput', 'derp_' + time.strftime('%Y-%m-%d_%H-%M'))
71 
72  gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorActionInit/GenerationInit")
73  kernel.generatorAction().adopt(gen)
74 
75  gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorWrapper/GPS")
76  gen.Uses = 'G4GeneralParticleSource'
77  gen.OutputLevel = Output.INFO
78  gen.Mask = 1
79  gen.enableUI()
80  kernel.generatorAction().adopt(gen)
81 
82  #gen = DDG4.GeneratorAction(kernel, "Geant4IsotropeGenerator/IsotropPi+")
83  #gen.Mask = 1
84  #gen.Particle = 'e-'
85  #gen.Energy = 5 * GeV
86  #gen.Multiplicity = 2
87  #gen.Distribution = 'cos(theta)'
88 
89  #geant4.setupGun('electron','e-',3.0*GeV)
90 
91 
92  #logger.info("# Install vertex smearing for this interaction")
93  #gen = DDG4.GeneratorAction(kernel, "Geant4InteractionVertexSmear/SmearPi+")
94  #gen.Mask = 1
95  #gen.Offset = (20 * mm, 10 * mm, 10 * mm, 0 * ns)
96  #gen.Sigma = (4 * mm, 1 * mm, 1 * mm, 0 * ns)
97  #kernel.generatorAction().adopt(gen)
98 
99  #logger.info("# Second particle generator: e-")
100  #gen = DDG4.GeneratorAction(kernel, "Geant4IsotropeGenerator/IsotropE-")
101  #gen.Mask = 2
102  #gen.Particle = 'e-'
103  #gen.Energy = 25 * GeV
104  #gen.Multiplicity = 3
105  #gen.Distribution = 'uniform'
106  #kernel.generatorAction().adopt(gen)
107  #logger.info(" Install vertex smearing for this interaction")
108  #gen = DDG4.GeneratorAction(kernel, "Geant4InteractionVertexSmear/SmearE-")
109  #gen.Mask = 2
110  #gen.Offset = (-20 * mm, -10 * mm, -10 * mm, 0 * ns)
111  #gen.Sigma = (12 * mm, 8 * mm, 8 * mm, 0 * ns)
112  #kernel.generatorAction().adopt(gen)
113  # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114 
115  #logger.info("# Merge all existing interaction records")
116  #gen = DDG4.GeneratorAction(kernel, "Geant4InteractionMerger/InteractionMerger")
117  #gen.OutputLevel = 4 # generator_output_level
118  #gen.enableUI()
119  #kernel.generatorAction().adopt(gen)
120 
121  #logger.info("# Finally generate Geant4 primaries")
122  #gen = DDG4.GeneratorAction(kernel, "Geant4PrimaryHandler/PrimaryHandler")
123  #gen.OutputLevel = 4 # generator_output_level
124  #gen.enableUI()
125  #kernel.generatorAction().adopt(gen)
126 
127  #logger.info("# ....and handle the simulation particles.")
128  #part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
129  #kernel.generatorAction().adopt(part)
130 
140 
141  #logger.info("# Setup global filters fur use in sensitive detectors")
142 
154 
155  #logger.info("# First the PID detectors")
156  #seq, act = geant4.setupDetector('ForwardRICH','PhotoMultiplierSDAction')
157  #act.adopt(f2)
158 
159  #seq, act = geant4.setupTracker('SiVertexBarrel')
160  #seq.adopt(f1)
161 
163 
164  #seq, act = geant4.setupTracker('SiVertexEndcap')
165  #seq.adopt(f1)
166 
167 
168  #seq, act = geant4.setupTracker('SiTrackerBarrel')
169  #seq, act = geant4.setupTracker('SiTrackerEndcap')
170  #seq, act = geant4.setupTracker('SiTrackerForward')
171  #logger.info("# Now setup the calorimeters")
172  #seq, act = geant4.setupCalorimeter('EcalBarrel')
173  #seq, act = geant4.setupCalorimeter('EcalEndcap')
174  #seq, act = geant4.setupCalorimeter('HcalBarrel')
175  #seq, act = geant4.setupCalorimeter('HcalEndcap')
176  #seq, act = geant4.setupCalorimeter('HcalPlug')
177  #seq, act = geant4.setupCalorimeter('MuonBarrel')
178  #seq, act = geant4.setupCalorimeter('MuonEndcap')
179  #seq, act = geant4.setupCalorimeter('LumiCal')
180  #seq, act = geant4.setupCalorimeter('BeamCal')
181 
182  logger.info("# Now build the physics list:")
183  phys = geant4.setupPhysics('QGSP_BERT')
184  geant4.addPhysics(str('Geant4PhysicsList/Myphysics'))
185 
186  #ph = DDG4.PhysicsList(kernel, 'Geant4OpticalPhotonPhysics/OpticalPhotonPhys')
187  #ph.VerboseLevel = 2
188  #ph.enableUI()
189  #phys.adopt(ph)
190 
191  #ph = DDG4.PhysicsList(kernel, 'Geant4CerenkovPhysics/CerenkovPhys')
192  #ph.MaxNumPhotonsPerStep = 10
193  #ph.MaxBetaChangePerStep = 10.0
194  #ph.TrackSecondariesFirst = True
195  #ph.VerboseLevel = 2
196  #ph.enableUI()
197  #phys.adopt(ph)
198 
199 
200 
203 
204  # Add global range cut
205  rg = geant4.addPhysics('Geant4DefaultRangeCut/GlobalRangeCut')
206  rg.RangeCut = 0.7 * mm
207 
208  phys.dump()
209 
210  #ui_action = dd4hep.sim.createAction(kernel, "Geant4UIManager/UI")
211  #ui_action.HaveVIS = True
212  #ui_action.HaveUI = True
213  #ui_action.SessionType = qt
214  #ui_action.SetupUI = macro
215  #kernel.registerGlobalAction(ui_action)
216 
217  #ui = geant4.setupUI("qt",vis=True,macro="vis.mac")
218  #ui.enableUI()
219 
220  kernel.configure()
221  kernel.initialize()
222 
223  # DDG4.setPrintLevel(Output.DEBUG)
224  kernel.run()
225  kernel.terminate()
226 
227 
228 if __name__ == "__main__":
229  run()
def Action(kernel, nam, shared=False)
Definition: DDG4.py:181
def RunAction(kernel, nam, shared=False)
Definition: DDG4.py:193
Kernel
Definition: DDG4.py:67
def GeneratorAction(kernel, nam, shared=False)
Definition: DDG4.py:201
def importConstants(description, namespace=None, debug=False)
Definition: DDG4.py:79
def EventAction(kernel, nam, shared=False)
Definition: DDG4.py:197