From 0f20ee91e2f340e1520fb8712a49872cda9a8e87 Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Thu, 17 Mar 2016 13:07:52 +0000
Subject: [PATCH] DDSim: Fix issue with G4GPS/G4Gun

Cannot use PrimaryHandler together with either of these (particles are created twice)
Thanks to S.Lukic for pointing out the problem

Move consistency checks to separate function for better readability
---
 DDSim/DD4hepSimulation.py | 49 ++++++++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 9 deletions(-)

diff --git a/DDSim/DD4hepSimulation.py b/DDSim/DD4hepSimulation.py
index 98bf31efe..fa230344c 100644
--- a/DDSim/DD4hepSimulation.py
+++ b/DDSim/DD4hepSimulation.py
@@ -212,14 +212,7 @@ class DD4hepSimulation(object):
     self.vertexOffset = parsed.vertexOffset
     self.vertexSigma = parsed.vertexSigma
 
-    if not self.compactFile:
-      self._errorMessages.append("ERROR: No geometry compact file provided")
-
-    if self.runType == "batch":
-      if not self.numberOfEvents:
-        self._errorMessages.append("ERROR: Batch mode requested, but did not set number of events")
-      if not self.inputFiles and not self.enableGun:
-        self._errorMessages.append("ERROR: Batch mode requested, but did not set inputFile(s) or gun")
+    self._consistencyChecks()
 
     #self.__treatUnknownArgs( parsed, unknown )
     self.__parseAllHelper( parsed )
@@ -381,7 +374,8 @@ class DD4hepSimulation(object):
       self.__applyBoostOrSmear(kernel, actionList, index)
 
     if actionList:
-      simple.buildInputStage( actionList , output_level=self.output.inputStage )
+      simple.buildInputStage( actionList , output_level=self.output.inputStage,
+                              have_mctruth = self._enablePrimaryHandler() )
 
     #================================================================================================
 
@@ -662,6 +656,43 @@ SIM = DD4hepSimulation()
         steeringFileBase += "\n"
     print steeringFileBase
 
+
+  def _consistencyChecks( self ):
+    """Check if the requested setup makes sense, or if there is something preventing it from working correctly
+
+    Appends error messages to self._errorMessages
+
+    :returns: None
+    """
+
+    if not self.compactFile:
+      self._errorMessages.append("ERROR: No geometry compact file provided")
+
+    if self.runType == "batch":
+      if not self.numberOfEvents:
+        self._errorMessages.append("ERROR: Batch mode requested, but did not set number of events")
+      if not self.inputFiles and not self.enableGun:
+        self._errorMessages.append("ERROR: Batch mode requested, but did not set inputFile(s) or gun")
+
+    if self.inputFiles and ( self.enableG4Gun or self.enableG4GPS ):
+      self._errorMessages.append("ERROR: Cannot use both inputFiles and Geant4Gun or GeneralParticleSource")
+
+    if self.enableGun and ( self.enableG4Gun or self.enableG4GPS ):
+      self._errorMessages.append("ERROR: Cannot use both DD4hepGun and Geant4 Gun or GeneralParticleSource")
+
+
+  def _enablePrimaryHandler( self ):
+    """ the geant4 Gun or GeneralParticleSource cannot be used together with the PrimaryHandler.
+        Particles would be simulated multiple times
+
+    :returns: True or False
+    """
+    enablePrimaryHandler = not (self.enableG4Gun or self.enableG4GPS)
+    if enablePrimaryHandler:
+      print "Enabling the PrimaryHandler"
+    else:
+      print "Disabling the PrimaryHandler"
+
 ################################################################################
 ### MODULE FUNCTIONS GO HERE
 ################################################################################
-- 
GitLab