@@ -71,8 +71,6 @@ def __init__(self):
7171 self .enableGun = False
7272 self .enableG4GPS = False
7373 self .enableG4Gun = False
74- self ._g4gun = None
75- self ._g4gps = None
7674 self .vertexSigma = [0.0 , 0.0 , 0.0 , 0.0 ]
7775 self .vertexOffset = [0.0 , 0.0 , 0.0 , 0.0 ]
7876 self .enableDetailedShowerMode = False
@@ -295,64 +293,14 @@ def getDetectorLists(self, detectorDescription):
295293
296294# ==================================================================================
297295
298- def run (self ):
299- """setup the geometry and dd4hep and geant4 and do what was asked to be done"""
300- import ROOT
301- ROOT .PyConfig .IgnoreCommandLineOptions = True
302-
296+ def __setupActions (self , kernel ):
303297 import DDG4
304- import dd4hep
305-
306- self .printLevel = getOutputLevel (self .printLevel )
307-
308- kernel = DDG4 .Kernel ()
309- dd4hep .setPrintLevel (self .printLevel )
310-
311- for compactFile in self .compactFile :
312- kernel .loadGeometry (str ("file:" + os .path .abspath (compactFile )))
313- detectorDescription = kernel .detectorDescription ()
314-
315- DDG4 .importConstants (detectorDescription )
316-
317- # ----------------------------------------------------------------------------------
318-
319- # simple = DDG4.Geant4( kernel, tracker='Geant4TrackerAction',calo='Geant4CalorimeterAction')
320- # geant4 = DDG4.Geant4( kernel, tracker='Geant4TrackerCombineAction',calo='Geant4ScintillatorCalorimeterAction')
321- geant4 = DDG4 .Geant4 (kernel , tracker = self .action .tracker , calo = self .action .calo )
322-
323- geant4 .printDetectors ()
324-
325- if self .runType == "vis" :
326- uiaction = geant4 .setupUI (typ = "tcsh" , vis = True , macro = self .macroFile )
327- elif self .runType == "qt" :
328- uiaction = geant4 .setupUI (typ = "qt" , vis = True , macro = self .macroFile )
329- elif self .runType == "run" :
330- uiaction = geant4 .setupUI (typ = "tcsh" , vis = False , macro = self .macroFile , ui = False )
331- elif self .runType == "shell" :
332- uiaction = geant4 .setupUI (typ = "tcsh" , vis = False , macro = None , ui = True )
333- elif self .runType == "batch" :
334- uiaction = geant4 .setupUI (typ = "tcsh" , vis = False , macro = None , ui = False )
335- else :
336- logger .error ("unknown runType" )
337- exit (1 )
338298
339- # User Configuration for the Geant4Phases
340- uiaction .ConfigureCommands = self .ui ._commandsConfigure
341- uiaction .InitializeCommands = self .ui ._commandsInitialize
342- uiaction .PostRunCommands = self .ui ._commandsPostRun
343- uiaction .PreRunCommands = self .ui ._commandsPreRun
344- uiaction .TerminateCommands = self .ui ._commandsTerminate
299+ # Configure default run action
300+ run = DDG4 .RunAction (kernel , 'Geant4TestRunAction/RunInit' )
301+ kernel .registerGlobalAction (run )
302+ kernel .runAction ().add (run )
345303
346- kernel .NumEvents = self .numberOfEvents
347-
348- # -----------------------------------------------------------------------------------
349- # setup the magnetic field:
350- self .__setMagneticFieldOptions (geant4 )
351-
352- # configure geometry creation
353- self .geometry .constructGeometry (kernel , geant4 , self .output .geometry )
354-
355- # ----------------------------------------------------------------------------------
356304 # Configure run, event, track, step, and stack actions, if present
357305 for action_list , DDG4_Action , kernel_Action in \
358306 [(self .action .run , DDG4 .RunAction , kernel .runAction ),
@@ -366,17 +314,10 @@ def run(self):
366314 setattr (action , parameter , value )
367315 kernel_Action ().add (action )
368316
369- # ----------------------------------------------------------------------------------
370- # Configure Run actions
371- run1 = DDG4 .RunAction (kernel , 'Geant4TestRunAction/RunInit' )
372- kernel .registerGlobalAction (run1 )
373- kernel .runAction ().add (run1 )
374-
375- # Configure the random seed, do it before the I/O because we might change the seed!
376- self .random .initialize (DDG4 , kernel , self .output .random )
317+ return 1
377318
378- # Configure the output file format and plugin
379- self . outputConfig . initialize ( dd4hepsimulation = self , geant4 = geant4 )
319+ def __setupGeneratorActions ( self , kernel , geant4 ):
320+ import DDG4
380321
381322 actionList = []
382323
@@ -391,19 +332,19 @@ def run(self):
391332
392333 if self .enableG4Gun :
393334 # GPS Create something
394- self . _g4gun = DDG4 .GeneratorAction (kernel , "Geant4GeneratorWrapper/Gun" )
395- self . _g4gun .Uses = 'G4ParticleGun'
396- self . _g4gun .Mask = 2
335+ g4gun = DDG4 .GeneratorAction (kernel , "Geant4GeneratorWrapper/Gun" )
336+ g4gun .Uses = 'G4ParticleGun'
337+ g4gun .Mask = 2
397338 logger .info ("++++ Adding Geant4 Particle Gun ++++" )
398- actionList .append (self . _g4gun )
339+ actionList .append (g4gun )
399340
400341 if self .enableG4GPS :
401342 # GPS Create something
402- self . _g4gps = DDG4 .GeneratorAction (kernel , "Geant4GeneratorWrapper/GPS" )
403- self . _g4gps .Uses = 'G4GeneralParticleSource'
404- self . _g4gps .Mask = 3
343+ g4gps = DDG4 .GeneratorAction (kernel , "Geant4GeneratorWrapper/GPS" )
344+ g4gps .Uses = 'G4GeneralParticleSource'
345+ g4gps .Mask = 3
405346 logger .info ("++++ Adding Geant4 General Particle Source ++++" )
406- actionList .append (self . _g4gps )
347+ actionList .append (g4gps )
407348
408349 start = 4
409350 for index , plugin in enumerate (self .inputConfig .userInputPlugin , start = start ):
@@ -452,8 +393,6 @@ def run(self):
452393 self ._buildInputStage (geant4 , actionList , output_level = self .output .inputStage ,
453394 have_mctruth = self ._enablePrimaryHandler ())
454395
455- # ================================================================================================
456-
457396 # And handle the simulation particles.
458397 part = DDG4 .GeneratorAction (kernel , "Geant4ParticleHandler/ParticleHandler" )
459398 kernel .generatorAction ().adopt (part )
@@ -472,31 +411,134 @@ def run(self):
472411
473412 self .part .setupUserParticleHandler (part , kernel , DDG4 )
474413
475- # =================================================================================
414+ return 1
415+
416+ def __setupSensitives (geant4 , detectorDescription , sim ):
417+ kernel = geant4 .kernel ()
476418
477419 # Setup global filters for use in sensitive detectors
478420 try :
479- self .filter .setupFilters (kernel )
421+ sim .filter .setupFilters (kernel )
480422 except RuntimeError as e :
481423 logger .error ("%s" , e )
482424 exit (1 )
483425
484- # =================================================================================
485426 # get lists of trackers and calorimeters in detectorDescription
486-
487- trk , cal , unk = self .getDetectorLists (detectorDescription )
427+ trk , cal , unk = sim .getDetectorLists (detectorDescription )
488428 for detectors , function , defFilter , defAction , abort in \
489- [(trk , geant4 .setupTracker , self .filter .tracker , self .action .tracker , False ),
490- (cal , geant4 .setupCalorimeter , self .filter .calo , self .action .calo , False ),
429+ [(trk , geant4 .setupTracker , sim .filter .tracker , sim .action .tracker , False ),
430+ (cal , geant4 .setupCalorimeter , sim .filter .calo , sim .action .calo , False ),
491431 (unk , geant4 .setupDetector , None , "No Default" , True ),
492432 ]:
493433 try :
494- self .__setupSensitiveDetectors (detectors , function , defFilter , defAction , abort )
434+ sim .__setupSensitiveDetectors (detectors , function , defFilter , defAction , abort )
495435 except Exception as e :
496436 logger .error ("Failed setting up sensitive detector %s" , e )
497437 raise
498438
499- # =================================================================================
439+ return 1
440+
441+ def __setupWorker (geant4 , sim ):
442+ logger .debug ("Setting up worker" )
443+ kernel = geant4 .kernel ()
444+ logger .debug ("Setting up actions" )
445+ sim .__setupActions (kernel )
446+ logger .debug ("Setting up generator actions" )
447+ sim .__setupGeneratorActions (kernel , geant4 )
448+ return 1
449+
450+ def __setupMaster (geant4 ):
451+ logger .debug ("Setting up master" )
452+ kernel = geant4 .master ()
453+ return 1
454+
455+ def run (self ):
456+ """setup the geometry and dd4hep and geant4 and do what was asked to be done"""
457+ import ROOT
458+ ROOT .PyConfig .IgnoreCommandLineOptions = True
459+
460+ import DDG4
461+ import dd4hep
462+
463+ self .printLevel = getOutputLevel (self .printLevel )
464+
465+ kernel = DDG4 .Kernel ()
466+ dd4hep .setPrintLevel (self .printLevel )
467+
468+ for compactFile in self .compactFile :
469+ kernel .loadGeometry (str ("file:" + os .path .abspath (compactFile )))
470+ detectorDescription = kernel .detectorDescription ()
471+
472+ DDG4 .importConstants (detectorDescription )
473+
474+ # ----------------------------------------------------------------------------------
475+
476+ geant4 = DDG4 .Geant4 (kernel , tracker = self .action .tracker , calo = self .action .calo )
477+
478+ geant4 .printDetectors ()
479+
480+ if self .runType == "vis" :
481+ uiaction = geant4 .setupUI (typ = "tcsh" , vis = True , macro = self .macroFile )
482+ elif self .runType == "qt" :
483+ uiaction = geant4 .setupUI (typ = "qt" , vis = True , macro = self .macroFile )
484+ elif self .runType == "run" :
485+ uiaction = geant4 .setupUI (typ = "tcsh" , vis = False , macro = self .macroFile , ui = False )
486+ elif self .runType == "shell" :
487+ uiaction = geant4 .setupUI (typ = "tcsh" , vis = False , macro = None , ui = True )
488+ elif self .runType == "batch" :
489+ uiaction = geant4 .setupUI (typ = "tcsh" , vis = False , macro = None , ui = False )
490+ else :
491+ logger .error ("unknown runType" )
492+ exit (1 )
493+
494+ # User Configuration for the Geant4Phases
495+ uiaction .ConfigureCommands = self .ui ._commandsConfigure
496+ uiaction .InitializeCommands = self .ui ._commandsInitialize
497+ uiaction .PostRunCommands = self .ui ._commandsPostRun
498+ uiaction .PreRunCommands = self .ui ._commandsPreRun
499+ uiaction .TerminateCommands = self .ui ._commandsTerminate
500+
501+ kernel .NumEvents = self .numberOfEvents
502+
503+ if self .numberOfThreads > 1 :
504+ logger .info ("Multi-threaded with %d threads" , self .numberOfThreads )
505+ kernel .RunManagerType = "G4MTRunManager"
506+ kernel .NumberOfThreads = self .numberOfThreads
507+ geant4 .addUserInitialization (
508+ worker = DD4hepSimulation .__setupWorker , worker_args = (geant4 , self ),
509+ master = DD4hepSimulation .__setupMaster , master_args = (geant4 ,))
510+ else :
511+ kernel .RunManagerType = "G4RunManager"
512+ kernel .NumberOfThreads = 1
513+ geant4 .addUserInitialization (
514+ worker = DD4hepSimulation .__setupWorker , worker_args = (geant4 , self ))
515+
516+ # -----------------------------------------------------------------------------------
517+
518+ logger .info ("# Configure G4 geometry setup" )
519+ seq , act = geant4 .addDetectorConstruction ("Geant4DetectorGeometryConstruction/ConstructGeo" )
520+
521+ logger .info ("# Configure G4 sensitive detectors: python setup callback" )
522+ seq , act = geant4 .addDetectorConstruction (
523+ "Geant4PythonDetectorConstruction/SetupSD" ,
524+ sensitives = DD4hepSimulation .__setupSensitives ,
525+ sensitives_args = (geant4 , detectorDescription , self ))
526+ logger .info ("# Configure G4 sensitive detectors: atach'em to the sensitive volumes" )
527+ seq , act = geant4 .addDetectorConstruction ("Geant4DetectorSensitivesConstruction/ConstructSD" )
528+
529+ # setup the magnetic field:
530+ logger .info ("Setting magnetic field" )
531+ self .__setMagneticFieldOptions (geant4 )
532+
533+ # Configure the random seed, do it before the I/O because we might change the seed!
534+ logger .info ("Initializing random" )
535+ self .random .initialize (DDG4 , kernel , self .output .random )
536+
537+ # Configure the output file format and plugin
538+ logger .info ("Initializing output" )
539+ self .outputConfig .initialize (dd4hepsimulation = self , geant4 = geant4 )
540+
541+ # =================================================================================
500542 # Now build the physics list:
501543 _phys = self .physics .setupPhysics (kernel , name = self .physicsList )
502544
@@ -507,19 +549,9 @@ def run(self):
507549
508550 dd4hep .setPrintLevel (self .printLevel )
509551
510- kernel .configure ()
511- kernel .initialize ()
512-
513- # GPS
514- if self ._g4gun is not None :
515- self ._g4gun .generator ()
516- if self ._g4gps is not None :
517- self ._g4gps .generator ()
518-
519552 startUpTime , _sysTime , _cuTime , _csTime , _elapsedTime = os .times ()
520553
521- kernel .run ()
522- kernel .terminate ()
554+ geant4 .run ()
523555
524556 totalTimeUser , totalTimeSys , _cuTime , _csTime , _elapsedTime = os .times ()
525557 if self .printLevel <= 3 :
0 commit comments