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