From e7fdf8081cc9209613ff8a557b540fd7ccce8d89 Mon Sep 17 00:00:00 2001
From: Markus Frank <markus.frank@cern.ch>
Date: Thu, 19 Dec 2013 12:13:20 +0000
Subject: [PATCH] Fix problems for xml setup of DDG4

---
 DDG4/examples/physics.xml                 | 37 ++++++++++-------------
 DDG4/examples/sequences.xml               | 22 +++++++++++++-
 DDG4/include/DDG4/Geant4MonteCarloTruth.h | 27 +++++++++++++++++
 DDG4/plugins/Geant4Factories.cpp          |  4 +++
 DDG4/plugins/Geant4SDActions.cpp          |  6 ++--
 DDG4/plugins/Geant4XMLSetup.cpp           | 10 ++++++
 DDG4/src/Geant4Kernel.cpp                 | 12 +++++---
 DDG4/src/Geant4MonteCarloTruth.cpp        | 27 +++++++++++++++++
 8 files changed, 116 insertions(+), 29 deletions(-)

diff --git a/DDG4/examples/physics.xml b/DDG4/examples/physics.xml
index 0aa910f4d..3c9a20618 100644
--- a/DDG4/examples/physics.xml
+++ b/DDG4/examples/physics.xml
@@ -10,31 +10,26 @@
   -->
 
   <sequences>
-    <sequence name="Geant4PhysicsListSequence/PhysicsList"/>
-  </sequences>
+    <sequence name="Geant4PhysicsListSequence/PhysicsList" decays="true" extends="FTFP_BERT"/>
 
-  <physicslist name="Geant4PhysicsList/MyPhysics.0">
-    <extends name="QGSP_BERT"/>
-    <particles>
-      <construct name="G4Geantino"/>
-      <construct name="G4ChargedGeantino"/>
-      <construct name="G4Electron"/>
-      <construct name="G4Gamma"/>
-      <construct name="G4BosonConstructor"/>
-      <construct name="G4LeptonConstructor"/>
-      <construct name="G4MesonConstructor"/>
-      <construct name="G4BaryonConstructor"/>
-    </particles>
-  </physicslist>
 <!--
-  <sequences>
-  // No transportation nor decays if you are already extending an existing physics list!
-    <sequence name="Geant4PhysicsListSequence/PhysicsList" transportation="true" decays="true" extends="QGSP_BERT"/>
-  </sequences>
+    <physicslist name="Geant4PhysicsList/MyPhysics.0" decays="true">
+      <extends name="FTFP_BERT"/>
+      <particles>
+	<construct name="G4BosonConstructor"/>
+	<construct name="G4LeptonConstructor"/>
+      </particles>
+      <processes>
+	<particle name="e[+-]" cut="0.001*mm">
+	  <process name="G4eMultipleScattering" ordAtRestDoIt="-1" ordAlongSteptDoIt="1" ordPostStepDoIt="1"/>
+	  <process name="G4eIonisation"         ordAtRestDoIt="-1" ordAlongSteptDoIt="2" ordPostStepDoIt="2"/>
+	</particle>
+      </processes>
+    </physicslist>
 -->
-  <!--
-
+  </sequences>
 
+  <!--
   <physicslist name="Geant4PhysicsList/MyPhysics.0">
 
 
diff --git a/DDG4/examples/sequences.xml b/DDG4/examples/sequences.xml
index 909624ab8..363520c5a 100644
--- a/DDG4/examples/sequences.xml
+++ b/DDG4/examples/sequences.xml
@@ -3,6 +3,17 @@
 
 
 
+  <!-- Kernel parameters  
+  --   UI="UI" is default, 
+  --
+  --   Note:
+  --   NumEvents is ignored in interactive sessions, but  used
+  --   in batch mode without ui nor vis.
+  --
+  --   To run batch: set UI=""
+  -->
+  <kernel UI="UI" NumEvents="1"/>  
+
   <!-- List of globally known actions. These actions will be registered with the Geant4Kernel 
   --   object and may be retrieved by there using the action name.
   --
@@ -21,6 +32,15 @@
 		  Property_double="5e15"
 		  Property_string="Hello_2"/>
      </action>
+
+    <!-- UI handler initializing vis and ui. NAME IS FIXED/COMMON KNOWLEDGE -->
+    <action name="Geant4UIManager/UI">
+      <properties HaveVIS="1"
+		  HaveUI="1"
+		  SessionType="csh"/>
+     </action>
+    <!-- void and empty for the time being.... NAME IS FIXED/COMMON KNOWLEDGE -->
+    <action name="Geant4DummyTruthHandler/MonteCarloTruthHandler"/>
   </actions>
 
   <!-- List of known phases.
@@ -92,7 +112,7 @@
     </sequence>
     <sequence name="Geant4GeneratorActionSequence/GeneratorAction">
       <action name="Geant4ParticleGun/Gun">
-        <properties energy="500*GeV"
+        <properties energy="500*MeV"
   	  	    particle="'e-'"
 		    multiplicity="1"/>
       </action>
diff --git a/DDG4/include/DDG4/Geant4MonteCarloTruth.h b/DDG4/include/DDG4/Geant4MonteCarloTruth.h
index cce5b1296..bd1d90f2e 100644
--- a/DDG4/include/DDG4/Geant4MonteCarloTruth.h
+++ b/DDG4/include/DDG4/Geant4MonteCarloTruth.h
@@ -10,6 +10,7 @@
 #define DD4HEP_DDG4_GEANT4MONTECARLOTRUTH_H
 
 // Framework include files
+#include "DDG4/Geant4Action.h"
 
 // C/C++ include files
 
@@ -51,6 +52,32 @@ namespace DD4hep {
       /// Store a track produced in a step to be kept for later MC truth analysis
       virtual void mark(const G4Step* step, bool created_hit) = 0;
     };
+
+    /** @class Geant4DummyTruthHandler Geant4DummyTruthHandler.h DDG4/Geant4DummyTruthHandler.h
+     *
+     * Void implementation doing nothing at all.
+     *
+     * @author  M.Frank
+     * @version 1.0
+     */
+    class Geant4DummyTruthHandler : 
+    public Geant4Action, public Geant4MonteCarloTruth  
+      {
+    public:
+      /// Standard constructor
+      Geant4DummyTruthHandler(Geant4Context* ctxt,const std::string& nam);
+      /// Default destructor
+      virtual ~Geant4DummyTruthHandler();
+      /// Mark a Geant4 track to be kept for later MC truth analysis
+      virtual void mark(const G4Track* track);
+      /// Store a track, with a flag
+      virtual void mark(const G4Track* track, bool created_hit);
+      /// Mark a Geant4 track of the step to be kept for later MC truth analysis
+      virtual void mark(const G4Step* step);
+      /// Store a track produced in a step to be kept for later MC truth analysis
+      virtual void mark(const G4Step* step, bool created_hit);
+    };
+
   }    // End namespace Simulation
 }      // End namespace DD4hep
 
diff --git a/DDG4/plugins/Geant4Factories.cpp b/DDG4/plugins/Geant4Factories.cpp
index 624eb7acb..f64104bfd 100644
--- a/DDG4/plugins/Geant4Factories.cpp
+++ b/DDG4/plugins/Geant4Factories.cpp
@@ -37,8 +37,12 @@ DECLARE_GEANT4ACTION(Geant4SensDetActionSequence)
 #include "DDG4/Geant4UIManager.h"
 DECLARE_GEANT4ACTION(Geant4UIManager)
 
+#include "DDG4/Geant4MonteCarloTruth.h"
+DECLARE_GEANT4ACTION(Geant4DummyTruthHandler)
+
 #include "DDG4/Geant4TrackPersistency.h"
 DECLARE_GEANT4ACTION(Geant4TrackPersistency)
+
 #include "DDG4/Geant4MonteCarloRecordManager.h"
 DECLARE_GEANT4ACTION(Geant4MonteCarloRecordManager)
 
diff --git a/DDG4/plugins/Geant4SDActions.cpp b/DDG4/plugins/Geant4SDActions.cpp
index d529b5d8e..02929b0a0 100644
--- a/DDG4/plugins/Geant4SDActions.cpp
+++ b/DDG4/plugins/Geant4SDActions.cpp
@@ -132,7 +132,7 @@ namespace DD4hep {
 	double new_len = mean_length(h.preMom(),h.postMom())/hit_len;
 	direction *= new_len/hit_len;
       }
-      printout(INFO,"SimpleTracker","%s> Add hit with deposit:%f  Pos:%f %f %f",
+      printout(DEBUG,"SimpleTracker","%s> Add hit with deposit:%f  Pos:%f %f %f",
 	       c_name(),step->GetTotalEnergyDeposit(),position.X(),position.Y(),position.Z());
       Hit* hit = new Hit(h.track->GetTrackID(),
 			 h.track->GetDefinition()->GetPDGEncoding(),
@@ -171,11 +171,11 @@ namespace DD4hep {
 	hit = new Hit(pos) ;
 	hit->cellID = volumeID( step ) ;
 	coll->add(hit) ;
-	printout(INFO,"SimpleTracker","%s> CREATE hit with deposit:%f  Pos:%f %f %f",
+	printout(DEBUG,"SimpleTracker","%s> CREATE hit with deposit:%f  Pos:%f %f %f",
 	       c_name(),contrib.deposit,pos.X(),pos.Y(),pos.Z());
       }
       else  {
-	printout(INFO,"SimpleTracker","%s> UPDATE hit with deposit:%f  Pos:%f %f %f",
+	printout(DEBUG,"SimpleTracker","%s> UPDATE hit with deposit:%f  Pos:%f %f %f",
 	       c_name(),contrib.deposit,pos.X(),pos.Y(),pos.Z());
       }
       hit->truth.push_back(contrib);
diff --git a/DDG4/plugins/Geant4XMLSetup.cpp b/DDG4/plugins/Geant4XMLSetup.cpp
index 4b612987b..fa182a966 100644
--- a/DDG4/plugins/Geant4XMLSetup.cpp
+++ b/DDG4/plugins/Geant4XMLSetup.cpp
@@ -407,10 +407,20 @@ namespace DD4hep  {
     kernel.physicsList().adopt(handle);
   }
 
+  template <> void Converter<Kernel>::operator()(xml_h e) const {
+    Kernel& kernel = Kernel::access(lcdd);
+    xml_comp_t k(e);
+    if ( k.hasAttr(_Unicode(NumEvents)) )  
+      kernel.property("NumEvents").str(k.attr<string>(_Unicode(NumEvents)));
+    if ( k.hasAttr(_Unicode(UI)) )  
+      kernel.property("UI").str(k.attr<string>(_Unicode(UI)));
+  }
+
   template <> void Converter<XMLSetup>::operator()(xml_h seq)  const  {
     xml_elt_t compact(seq);
     // First execute the basic setup from the plugins module
     ROOT::Reflex::PluginService::Create<TNamed*>("geant4_xml_setup",&lcdd,&seq);
+    xml_coll_t(compact,_Unicode(kernel)).for_each(Converter<Kernel>(lcdd,param));
     // Now deal with the new stuff.....
     xml_coll_t(compact,_Unicode(actions) ).for_each(_Unicode(action),Converter<Action>(lcdd,param));
     xml_coll_t(compact,_Unicode(filters) ).for_each(_Unicode(filter),Converter<Filter>(lcdd,param));
diff --git a/DDG4/src/Geant4Kernel.cpp b/DDG4/src/Geant4Kernel.cpp
index 3cab6f7e7..cd3d71dbb 100644
--- a/DDG4/src/Geant4Kernel.cpp
+++ b/DDG4/src/Geant4Kernel.cpp
@@ -98,8 +98,10 @@ Geant4Kernel::~Geant4Kernel() {
   for_each(m_globalFilters.begin(), m_globalFilters.end(), releaseObjects(m_globalFilters));
   for_each(m_globalActions.begin(), m_globalActions.end(), releaseObjects(m_globalActions));
   deletePtr  (m_runManager);
-  deletePtr  (m_mcTruthMgr);
-  releasePtr (m_mcRecordMgr);
+  m_mcTruthMgr = 0;
+  m_mcRecordMgr = 0;  // These are already released by the global actions....
+  //deletePtr  (m_mcTruthMgr);
+  //releasePtr (m_mcRecordMgr);
   releasePtr (m_physicsList);
   releasePtr (m_stackingAction);
   releasePtr (m_steppingAction);
@@ -181,8 +183,10 @@ void Geant4Kernel::terminate() {
   for_each(m_globalFilters.begin(), m_globalFilters.end(), releaseObjects(m_globalFilters));
   for_each(m_globalActions.begin(), m_globalActions.end(), releaseObjects(m_globalActions));
   deletePtr  (m_runManager);
-  deletePtr  (m_mcTruthMgr);
-  releasePtr (m_mcRecordMgr);
+  m_mcTruthMgr = 0;
+  m_mcRecordMgr = 0;  // These are already released by the global actions....
+  //deletePtr  (m_mcTruthMgr);
+  //releasePtr (m_mcRecordMgr);
   releasePtr (m_physicsList);
   releasePtr (m_stackingAction);
   releasePtr (m_steppingAction);
diff --git a/DDG4/src/Geant4MonteCarloTruth.cpp b/DDG4/src/Geant4MonteCarloTruth.cpp
index b597e40c3..2dad969fd 100644
--- a/DDG4/src/Geant4MonteCarloTruth.cpp
+++ b/DDG4/src/Geant4MonteCarloTruth.cpp
@@ -22,3 +22,30 @@ Geant4MonteCarloTruth::Geant4MonteCarloTruth()   {
 Geant4MonteCarloTruth::~Geant4MonteCarloTruth() {
   InstanceCount::decrement(this);
 }
+
+/// Standard constructor
+Geant4DummyTruthHandler::Geant4DummyTruthHandler(Geant4Context* ctxt,const std::string& nam) : Geant4Action(ctxt,nam), Geant4MonteCarloTruth()
+{
+}
+
+/// Default destructor
+Geant4DummyTruthHandler::~Geant4DummyTruthHandler()
+{
+}
+
+/// Mark a Geant4 track to be kept for later MC truth analysis
+void Geant4DummyTruthHandler::mark(const G4Track* )  {
+}
+
+/// Store a track, with a flag
+void Geant4DummyTruthHandler::mark(const G4Track* , bool ) {
+}
+
+/// Mark a Geant4 track of the step to be kept for later MC truth analysis
+void Geant4DummyTruthHandler::mark(const G4Step* ) {
+}
+
+/// Store a track produced in a step to be kept for later MC truth analysis
+void Geant4DummyTruthHandler::mark(const G4Step* , bool ) {
+}
+
-- 
GitLab