pid/compact/scripts/SiD_Markus.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 global geant4
12 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
13 logger = logging.getLogger(__name__)
14 #
15 """
16 
17  dd4hep simulation example setup using the python configuration
18 
19  @author M.Frank
20  @version 1.0
21 
22 """
23 
24 
26  k = DDG4.Kernel()
27  kernel = k.worker()
28  logger.info('PYTHON: +++ Creating Geant4 worker thread ....')
29 
30  # Configure Run actions
31  run1 = DDG4.RunAction(kernel, 'Geant4TestRunAction/RunInit')
32  run1.Property_int = 12345
33  run1.Property_double = -5e15 * keV
34  run1.Property_string = 'Startrun: Hello_2'
35  logger.info("%s %f %d", run1.Property_string, run1.Property_double, run1.Property_int)
36  run1.enableUI()
37  kernel.registerGlobalAction(run1)
38  kernel.runAction().adopt(run1)
39 
40  # Configure Event actions
41  prt = DDG4.EventAction(kernel, 'Geant4ParticlePrint/ParticlePrint')
42  prt.OutputLevel = Output.DEBUG
43  prt.OutputType = 3 # Print both: table and tree
44  kernel.eventAction().adopt(prt)
45 
46  # Configure Event actions
47  prt = DDG4.EventAction(kernel, 'Geant4SurfaceTest/SurfaceTest')
48  prt.OutputLevel = Output.INFO
49  kernel.eventAction().adopt(prt)
50 
51  # Configure I/O
52  evt_lcio = geant4.setupLCIOOutput('LcioOutput', 'CLICSiD_' + time.strftime('%Y-%m-%d_%H-%M'))
53  evt_lcio.OutputLevel = Output.DEBUG
54 
55  # evt_root = geant4.setupROOTOutput('RootOutput','CLICSiD_'+time.strftime('%Y-%m-%d_%H-%M'))
56  # generator_output_level = Output.INFO
57 
58  gen = DDG4.GeneratorAction(kernel, "Geant4GeneratorActionInit/GenerationInit")
59  kernel.generatorAction().adopt(gen)
60 
61  # VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
62  """
63  Generation of isotrope tracks of a given multiplicity with overlay:
64  """
65  # First particle generator: pi+
66  gen = DDG4.GeneratorAction(kernel, "Geant4IsotropeGenerator/IsotropPi+")
67  gen.Particle = 'pi+'
68  gen.Energy = 100 * GeV
69  gen.Multiplicity = 2
70  gen.Mask = 1
71  gen.OutputLevel = Output.DEBUG
72  gen.PhiMin = 0
73  gen.PhiMax = 0
74  gen.ThetaMin = 1.61
75  gen.ThetaMax = 1.61
76 
77  kernel.generatorAction().adopt(gen)
78  # Install vertex smearing for this interaction
79  gen = DDG4.GeneratorAction(kernel, "Geant4InteractionVertexSmear/SmearPi+")
80  gen.Mask = 1
81  gen.Offset = (20 * mm, 10 * mm, 10 * mm, 0 * ns)
82  gen.Sigma = (4 * mm, 1 * mm, 1 * mm, 0 * ns)
83  kernel.generatorAction().adopt(gen)
84  """
85  # Second particle generator: e-
86  gen = DDG4.GeneratorAction(kernel,"Geant4IsotropeGenerator/IsotropE-");
87  gen.Particle = 'e-'
88  gen.Energy = 25 * GeV
89  gen.Multiplicity = 3
90  gen.Mask = 2
91  gen.OutputLevel = Output.DEBUG
92  kernel.generatorAction().adopt(gen)
93  # Install vertex smearing for this interaction
94  gen = DDG4.GeneratorAction(kernel,"Geant4InteractionVertexSmear/SmearE-");
95  gen.Mask = 2
96  gen.Offset = (-20*mm, -10*mm, -10*mm, 0*ns)
97  gen.Sigma = (12*mm, 8*mm, 8*mm, 0*ns)
98  kernel.generatorAction().adopt(gen)
99  #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100  """
101  # Merge all existing interaction records
102  gen = DDG4.GeneratorAction(kernel, "Geant4InteractionMerger/InteractionMerger")
103  gen.OutputLevel = 4 # generator_output_level
104  gen.enableUI()
105  kernel.generatorAction().adopt(gen)
106 
107  # Finally generate Geant4 primaries
108  gen = DDG4.GeneratorAction(kernel, "Geant4PrimaryHandler/PrimaryHandler")
109  gen.OutputLevel = Output.DEBUG # generator_output_level
110  gen.enableUI()
111  kernel.generatorAction().adopt(gen)
112 
113  # And handle the simulation particles.
114  part = DDG4.GeneratorAction(kernel, "Geant4ParticleHandler/ParticleHandler")
115  kernel.generatorAction().adopt(part)
116  # part.SaveProcesses = ['conv','Decay']
117  part.SaveProcesses = ['Decay']
118  part.MinimalKineticEnergy = 100 * MeV
119  part.OutputLevel = Output.DEBUG # generator_output_level
120  part.enableUI()
121  user = DDG4.Action(kernel, "Geant4TCUserParticleHandler/UserParticleHandler")
122  user.TrackingVolume_Zmax = DDG4.EcalEndcap_zmin
123  user.TrackingVolume_Rmax = DDG4.EcalBarrel_rmin
124  user.enableUI()
125  part.adopt(user)
126  logger.info('PYTHON: +++ Geant4 worker thread configured successfully....')
127  return 1
128 
129 
131  logger.info('PYTHON: +++ Setting up master thread.....')
132  return 1
133 
134 
136  global geant4
137  # First the tracking detectors
138  seq, act = geant4.setupTracker('SiVertexBarrel')
139  act.OutputLevel = Output.ERROR
140  act.CollectSingleDeposits = False
141  seq, act = geant4.setupTracker('SiVertexEndcap')
142  act.OutputLevel = Output.ERROR
143  act.CollectSingleDeposits = False
144  logger.info('PYTHON: +++ Setting up Geant4 sensitive detectors for worker thread.....')
145  return 1
146 
147 
148 def dummy_sd():
149  logger.info('PYTHON: +++ Setting up DUMMY Geant4 sensitive detectors for worker thread.....')
150  return 1
151 
152 
154  logger.info('PYTHON: +++ Setting up DUMMY Geant4 geometry for worker thread.....')
155  return 1
156 
157 
158 def run():
159  global geant4
160  kernel = DDG4.Kernel()
161  description = kernel.detectorDescription()
162  install_dir = os.environ['DD4hepINSTALL']
163  kernel.loadGeometry(str("file:" + install_dir + "/DDDetectors/compact/SiD_Markus.xml"))
164  DDG4.importConstants(description)
165  DDG4.Core.setPrintLevel(Output.DEBUG)
166  DDG4.Core.setPrintFormat(str("%-32s %6s %s"))
167 
168  kernel.NumberOfThreads = 1
169  geant4 = DDG4.Geant4(kernel, tracker='Geant4TrackerWeightedAction')
170  geant4.printDetectors()
171  # Configure UI
172  geant4.setupCshUI()
173 
174  # Geant4 user initialization action
175  geant4.addUserInitialization(worker=setupWorker, master=setupMaster)
176 
177  # Configure G4 geometry setup
178  seq, act = geant4.addDetectorConstruction("Geant4DetectorGeometryConstruction/ConstructGeo")
179 
180  # Configure G4 magnetic field tracking
181  self.setupTrackingFieldMT() # noqa: F821
182 
183  seq, act = geant4.addDetectorConstruction("Geant4PythonDetectorConstruction/DummyDet",
184  geometry=dummy_geom,
185  sensitives=dummy_sd)
186  # Configure G4 sensitive detectors
187  seq, act = geant4.addDetectorConstruction("Geant4PythonDetectorConstruction/SetupSD",
188  sensitives=setupSensitives)
189 
190  # Configure G4 sensitive detectors
191  seq, act = geant4.addDetectorConstruction("Geant4DetectorSensitivesConstruction/ConstructSD",
192  allow_threads=True)
193 
194  # Setup random generator
195  rndm = DDG4.Action(kernel, 'Geant4Random/Random')
196  rndm.Seed = 987654321
197  rndm.initialize()
198 
199  # Setup global filters fur use in sensntive detectors
200  f1 = DDG4.Filter(kernel, 'GeantinoRejectFilter/GeantinoRejector')
201  kernel.registerGlobalFilter(f1)
202 
203  # seq,act = geant4.setupTracker('SiTrackerBarrel')
204  # seq,act = geant4.setupTracker('SiTrackerEndcap')
205  # seq,act = geant4.setupTracker('SiTrackerForward')
206  # Now the calorimeters
207  # seq,act = geant4.setupCalorimeter('EcalBarrel')
208  # seq,act = geant4.setupCalorimeter('EcalEndcap')
209  # seq,act = geant4.setupCalorimeter('HcalBarrel')
210  # seq,act = geant4.setupCalorimeter('HcalEndcap')
211  # seq,act = geant4.setupCalorimeter('HcalPlug')
212  # seq,act = geant4.setupCalorimeter('MuonBarrel')
213  # seq,act = geant4.setupCalorimeter('MuonEndcap')
214  # seq,act = geant4.setupCalorimeter('LumiCal')
215  # seq,act = geant4.setupCalorimeter('BeamCal')
216 
217  # Now build the physics list:
218  seq = geant4.setupPhysics('QGSP_BERT')
219  phys = DDG4.PhysicsList(geant4.master(), 'Geant4PhysicsList/MyPhysics')
220  part = DDG4.Action(geant4.master(), 'Geant4ExtraParticles/extraparts')
221  part.pdgfile = 'checkout/DDG4/examples/particle.tbl'
222  phys.adoptPhysicsConstructor(part.get())
223  seq.add(phys)
224 
225  geant4.run()
226 
227  # kernel.configure()
228  # kernel.initialize()
229 
230  # DDG4.setPrintLevel(Output.DEBUG)
231  # kernel.run()
232  # kernel.terminate()
233  return 1
234 
235 
236 if __name__ == "__main__":
237  import sys
238  logger.info('Arguments: %s', str(sys.argv))
239  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