From 62114740b914eca1df5e4213582371fcdc9927fc Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Mon, 11 Jan 2016 15:13:48 +0000
Subject: [PATCH] FieldConfiguration: Add LargestAcceptableStep, change field
 printout

This parameter is needed to not have single steps attempting to reach 1km (the
default, see
https://geant4.web.cern.ch/geant4/UserDocumentation/UsersGuides/ForApplicationDeveloper/html/ch04s03.html
). This can happen for low energy curlers in the beam pipe vacuum.

Add this also to DDG4 setup and to documentation example for field
get default value in case it is not set
---
 DDG4/plugins/Geant4FieldTrackingSetup.cpp | 31 +++++++++++++++++------
 DDG4/python/DDG4.py                       |  2 ++
 doc/LaTex/DDG4Manual-Setup.tex            |  1 +
 3 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/DDG4/plugins/Geant4FieldTrackingSetup.cpp b/DDG4/plugins/Geant4FieldTrackingSetup.cpp
index b2ad82e30..98ccab460 100644
--- a/DDG4/plugins/Geant4FieldTrackingSetup.cpp
+++ b/DDG4/plugins/Geant4FieldTrackingSetup.cpp
@@ -57,6 +57,8 @@ namespace DD4hep {
       double      eps_min;
       /// G4PropagatorInField parameter: eps_min
       double      eps_max;
+      /// G4PropagatorInField parameter: LargestAcceptableStep
+      double      largest_step;
 
     public:
       /// Default constructor
@@ -187,6 +189,7 @@ Geant4FieldTrackingSetup::Geant4FieldTrackingSetup() : eq_typ(), stepper_typ() {
   delta_chord        = -1.0;
   delta_one_step     = -1.0;
   delta_intersection = -1.0;
+  largest_step       = -1.0;
 }
 
 /// Default destructor
@@ -218,6 +221,11 @@ int Geant4FieldTrackingSetup::execute(Geometry::LCDD& lcdd)   {
     propagator->SetMinimumEpsilonStep(eps_min);
   if ( eps_max >= 0e0 )
     propagator->SetMaximumEpsilonStep(eps_max);
+  if ( largest_step >= 0e0 ) {
+    propagator->SetLargestAcceptableStep(largest_step);
+  } else {
+    largest_step = propagator->GetLargestAcceptableStep();
+  }
   return 1;
 }
 
@@ -234,6 +242,7 @@ static long setup_fields(lcdd_t& lcdd, const DD4hep::Geometry::GeoHandler& /* cn
       if ( pm["delta_chord"] ) delta_chord = pm.toDouble("delta_chord");
       if ( pm["delta_one_step"] ) delta_one_step = pm.toDouble("delta_one_step");
       if ( pm["delta_intersection"] ) delta_intersection = pm.toDouble("delta_intersection");
+      if ( pm["largest_step"] ) largest_step = pm.toDouble("largest_step");
     }
     virtual ~XMLFieldTrackingSetup() {}
   } setup(vals);
@@ -252,15 +261,18 @@ Geant4FieldTrackingSetupAction::Geant4FieldTrackingSetupAction(Geant4Context* ct
   declareProperty("delta_intersection", delta_intersection = -1.0);
   declareProperty("eps_min",            eps_min = -1.0);
   declareProperty("eps_max",            eps_max = -1.0);
+  declareProperty("largest_step",       largest_step = -1.0);
 }
 
 /// Post-track action callback
 void Geant4FieldTrackingSetupAction::operator()()   {
   execute(context()->lcdd());
-  print("Geant4 magnetic field tracking configured. G4MagIntegratorStepper:%s G4Mag_EqRhs:%s "
-        "Epsilon:[min:%f mm max:%f mm] Delta:[chord:%f 1-step:%f intersect:%f]",
-        stepper_typ.c_str(),eq_typ.c_str(),eps_min, eps_max,
-        delta_chord,delta_one_step,delta_intersection);
+  printout( INFO, "FieldSetup", "Geant4 magnetic field tracking configured.");
+  printout( INFO, "FieldSetup", "G4MagIntegratorStepper:%s G4Mag_EqRhs:%s",
+	    stepper_typ.c_str(), eq_typ.c_str());
+  printout( INFO, "FieldSetup", "Epsilon:[min:%f mm max:%f mm]", eps_min, eps_max);
+  printout( INFO, "FieldSetup", "Delta:[chord:%f 1-step:%f intersect:%f] LargestStep %f mm",
+	    delta_chord, delta_one_step, delta_intersection, largest_step);
 }
 
 
@@ -276,15 +288,18 @@ Geant4FieldTrackingConstruction::Geant4FieldTrackingConstruction(Geant4Context*
   declareProperty("delta_intersection", delta_intersection = -1.0);
   declareProperty("eps_min",            eps_min = -1.0);
   declareProperty("eps_max",            eps_max = -1.0);
+  declareProperty("largest_step",       largest_step = -1.0);
 }
 
 /// Post-track action callback
 void Geant4FieldTrackingConstruction::operator()()   {
   execute(context()->lcdd());
-  print("Geant4 magnetic field tracking configured. G4MagIntegratorStepper:%s G4Mag_EqRhs:%s "
-        "Epsilon:[min:%f mm max:%f mm] Delta:[chord:%f 1-step:%f intersect:%f]",
-        stepper_typ.c_str(),eq_typ.c_str(),eps_min, eps_max,
-        delta_chord,delta_one_step,delta_intersection);
+  printout( INFO, "FieldSetup", "Geant4 magnetic field tracking configured.");
+  printout( INFO, "FieldSetup", "G4MagIntegratorStepper:%s G4Mag_EqRhs:%s",
+	    stepper_typ.c_str(), eq_typ.c_str());
+  printout( INFO, "FieldSetup", "Epsilon:[min:%f mm max:%f mm]", eps_min, eps_max);
+  printout( INFO, "FieldSetup", "Delta:[chord:%f 1-step:%f intersect:%f] LargestStep %f mm",
+	    delta_chord, delta_one_step, delta_intersection, largest_step);
 }
 
 DECLARE_GEANT4_SETUP(Geant4FieldSetup,setup_fields)
diff --git a/DDG4/python/DDG4.py b/DDG4/python/DDG4.py
index 94076704a..3e80c91f6 100644
--- a/DDG4/python/DDG4.py
+++ b/DDG4/python/DDG4.py
@@ -508,6 +508,7 @@ class Geant4:
     field.delta_chord        = 0.25*SystemOfUnits.mm
     field.delta_intersection = 1e-05*SystemOfUnits.mm
     field.delta_one_step     = 0.001*SystemOfUnits.mm
+    field.largest_step       = 10*SystemOfUnits.m
     if prt:
       print '+++++> ',field.name,'-> stepper  = ',field.stepper
       print '+++++> ',field.name,'-> equation = ',field.equation
@@ -517,6 +518,7 @@ class Geant4:
       print '+++++> ',field.name,'-> min_chord_step     = ',field.min_chord_step,'[mm]'
       print '+++++> ',field.name,'-> delta_one_step     = ',field.delta_one_step,'[mm]'
       print '+++++> ',field.name,'-> delta_intersection = ',field.delta_intersection,'[mm]'
+      print '+++++> ',field.name,'-> largest_step       = ',field.largest_step,'[mm]'
     return field
   
   def setupPhysics(self,name):
diff --git a/doc/LaTex/DDG4Manual-Setup.tex b/doc/LaTex/DDG4Manual-Setup.tex
index 60dc4ce23..b4ec99e5a 100644
--- a/doc/LaTex/DDG4Manual-Setup.tex
+++ b/doc/LaTex/DDG4Manual-Setup.tex
@@ -309,6 +309,7 @@ electromagnetic fields used in Geant4:
             delta_one_step="0.001*mm"
             eps_min="5e-05*mm"
             eps_max="0.001*mm"
+            largest_step = "10*m"
             stepper="HelixSimpleRunge"
             equation="Mag_UsualEqRhs">
       </attributes>
-- 
GitLab