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