From 4c69de3db4eca7797c2438efc9b65326d53bf003 Mon Sep 17 00:00:00 2001
From: Thomas Strebler <thomas.strebler@cern.ch>
Date: Mon, 25 Sep 2023 12:55:31 +0200
Subject: [PATCH] Make time width configurable for VertexBeamCondPositioner

Make time width configurable for VertexBeamCondPositioner
---
 .../python/BeamEffectsAlgConfig.py            |  1 +
 .../src/VertexBeamCondPositioner.cxx          | 26 +++----------------
 .../src/VertexBeamCondPositioner.h            |  6 +++--
 .../SimulationConfig/python/SimConfigFlags.py | 11 +++++++-
 .../python/SimulationMetadata.py              |  3 +++
 Tools/PROCTools/data/q447_AOD_digest.ref      | 10 +++----
 Tools/WorkflowTestRunner/python/References.py |  2 +-
 7 files changed, 28 insertions(+), 31 deletions(-)

diff --git a/Simulation/BeamEffects/python/BeamEffectsAlgConfig.py b/Simulation/BeamEffects/python/BeamEffectsAlgConfig.py
index 27463fc266c..bff12c5281f 100755
--- a/Simulation/BeamEffects/python/BeamEffectsAlgConfig.py
+++ b/Simulation/BeamEffects/python/BeamEffectsAlgConfig.py
@@ -64,6 +64,7 @@ def VertexBeamCondPositionerCfg(flags, name="VertexBeamCondPositioner", **kwargs
 
     kwargs.setdefault("RandomSvc", acc.getPrimaryAndMerge(AthRNGSvcCfg(flags)).name)
     kwargs.setdefault("SimpleTimeSmearing", flags.Sim.VertexTimeSmearing)
+    kwargs.setdefault("TimeWidth", flags.Sim.VertexTimeWidth)
 
     from BeamSpotConditions.BeamSpotConditionsConfig import BeamSpotCondAlgCfg
     acc.merge(BeamSpotCondAlgCfg(flags))
diff --git a/Simulation/BeamEffects/src/VertexBeamCondPositioner.cxx b/Simulation/BeamEffects/src/VertexBeamCondPositioner.cxx
index 6d68dff227d..6b2bc7bc34e 100644
--- a/Simulation/BeamEffects/src/VertexBeamCondPositioner.cxx
+++ b/Simulation/BeamEffects/src/VertexBeamCondPositioner.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
 */
 
 // class header include
@@ -104,33 +104,15 @@ namespace Simulation
                     << "\tWidth is (" << beamSpotHandle->beamSigma(0)
                     << ", " << beamSpotHandle->beamSigma(1) << ", "
                     << beamSpotHandle->beamSigma(2) << ")" << std::endl
+		    << "\tTime smearing is " << (m_timeSmearing ? "on" : "off") << " with width " << m_timeWidth << std::endl
                     << "\tTilts are " << beamSpotHandle->beamTilt(0) << " and " << beamSpotHandle->beamTilt(1) << std::endl
                     << "\tVertex Position before transform: " << *vertexSmearing);
 
     // update with the tilt
     *vertexSmearing = transform * HepGeom::Point3D<double>(*vertexSmearing);
 
-    // See if we were asked to do time smearing as well
-    if (m_timeSmearing){
-      /* This is ballpark code courtesy of Brian Amadio.  He provided some functions based on beam parameters.
-         He provided a little trick for pulling out the beam bunch width as well.  Hard coding the crossing angle
-         parameter for the time being, as the beam spot service doesn't really provide that yet.  */
-      double bunch_length_z = (std::sqrt(2)*beamSpotHandle->beamSigma(2))/0.9; // 0.9 is the crossing angle reduction factor
-      //    double tLimit = 2.*(bunch_length_z+bunch_length_z)/Gaudi::Units::c_light;
-      //    TF1 func = TF1("func","[0]*exp((-([3]-299792458*x)^2*[2]^2-([3]+299792458*x)^2*[1]^2)/(2*[1]^2*[2]^2))",-1*tLimit,tLimit);
-      //    func.SetParameter(0,Gaudi::Units::c_light/(M_PI*bunch_length_z*bunch_length_z));
-      //    func.SetParameter(1,bunch_length_z);
-      //    func.SetParameter(2,bunch_length_z);
-      //    func.SetParameter(3,vertexSmearing->z());
-      //    double time_offset = func.GetRandom();
-
-      // Time should be set in units of distance, which is a little funny
-      double time_offset = CLHEP::RandGaussZiggurat::shoot(
-          randomEngine, vertexSmearing->z()/Gaudi::Units::c_light,
-          bunch_length_z/Gaudi::Units::c_light );
-
-      vertexSmearing->setT( vertexSmearing->t() + time_offset*Gaudi::Units::c_light );
-    }
+    float vertexT = m_timeSmearing ? CLHEP::RandGaussZiggurat::shoot(randomEngine)*m_timeWidth : 0.;
+    vertexSmearing->setT(vertexT);
 
     // and return it
     return vertexSmearing;
diff --git a/Simulation/BeamEffects/src/VertexBeamCondPositioner.h b/Simulation/BeamEffects/src/VertexBeamCondPositioner.h
index ff507722efa..fe8a5744a8c 100644
--- a/Simulation/BeamEffects/src/VertexBeamCondPositioner.h
+++ b/Simulation/BeamEffects/src/VertexBeamCondPositioner.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
 */
 
 #ifndef BEAMEFFECTS_VERTEXBEAMCONPOSITIONER_H
@@ -54,7 +54,9 @@ namespace Simulation {
       ATHRNG::RNGWrapper*           m_randomEngine ATLAS_THREAD_SAFE{};  //!< Slot-local RNG
 
       Gaudi::Property<std::string>  m_randomEngineName{this, "RandomStream", "VERTEX"};         //!< Name of the random number stream
-      Gaudi::Property<bool>           m_timeSmearing{this, "SimpleTimeSmearing", false};             //!< Do time smearing
+      Gaudi::Property<bool> m_timeSmearing{this, "SimpleTimeSmearing", false}; //!< Do time smearing
+      Gaudi::Property<float> m_timeWidth{this, "TimeWidth", 0.}; //!< Width of time for smearing
+
 
   };
 
diff --git a/Simulation/SimulationConfig/python/SimConfigFlags.py b/Simulation/SimulationConfig/python/SimConfigFlags.py
index 704e269d754..aa1353f919a 100644
--- a/Simulation/SimulationConfig/python/SimConfigFlags.py
+++ b/Simulation/SimulationConfig/python/SimConfigFlags.py
@@ -5,7 +5,7 @@ from AthenaConfiguration.AutoConfigFlags import GetFileMD
 from AthenaConfiguration.Enums import LHCPeriod
 from SimulationConfig.SimEnums import BeamPipeSimMode, CalibrationRun, CavernBackground, \
     LArParameterization, SimulationFlavour, TruthStrategy, VertexSource
-from AthenaCommon.SystemOfUnits import m
+from AthenaCommon.SystemOfUnits import m, ns
 
 #todo? add in the explanatory text from previous implementation
 
@@ -132,6 +132,15 @@ def createSimConfigFlags():
     scf.addFlag("Sim.VertexTimeSmearing", lambda prevFlags:
                 prevFlags.GeoModel.Run >= LHCPeriod.Run4)
 
+    def _checkVertexTimeWidth(prevFlags):
+        default = 0.175*ns
+        vertexTimeWidth  = default
+        if prevFlags.Input.Files:
+            vertexTimeWidth = GetFileMD(prevFlags.Input.Files).get("VertexTimeWidth", default)
+        return vertexTimeWidth
+
+    scf.addFlag("Sim.VertexTimeWidth", _checkVertexTimeWidth)
+
     # G4UserActions
     scf.addFlag("Sim.NRRThreshold", False)
     scf.addFlag("Sim.NRRWeight", False)
diff --git a/Simulation/SimulationConfig/python/SimulationMetadata.py b/Simulation/SimulationConfig/python/SimulationMetadata.py
index 52455898c5c..09242ae9b8e 100644
--- a/Simulation/SimulationConfig/python/SimulationMetadata.py
+++ b/Simulation/SimulationConfig/python/SimulationMetadata.py
@@ -23,6 +23,9 @@ def fillAtlasMetadata(flags, dbFiller):
             if "UseShadowEvent" in flag and not flags.Sim.UseShadowEvent:
                 # This flag is added temporarily to allow a new approach to quasi-stable particle simulation to be tested.
                 continue
+            if "VertexTimeWidth" in flag and not flags.Sim.VertexTimeSmearing:
+                # This flag is only written to metadata when vertex time smearing is enabled
+                continue
             key = flag.split(".")[-1] #use final part of flag as the key
             value = flags._get(flag)
             if isinstance(value, FlagEnum):
diff --git a/Tools/PROCTools/data/q447_AOD_digest.ref b/Tools/PROCTools/data/q447_AOD_digest.ref
index faef3900923..9a2d739fb2f 100644
--- a/Tools/PROCTools/data/q447_AOD_digest.ref
+++ b/Tools/PROCTools/data/q447_AOD_digest.ref
@@ -1,6 +1,6 @@
          run       event       nTopo   nIdTracks  nTauTracks       nTaus      nMuons       nElec   nTrueElec   nFakeElec       nPhot   nTruePhot   nFakePhot
-      242000     3120001         249           8           2           1           1           0           0           0           0           0           0
-      242000     3120002         153           6           0           0           1           0           0           0           0           0           0
-      242000     3120003         356          36           7           2           2           1           0           1           0           0           0
-      242000     3120004         170          10           5           1           0           0           0           0           0           0           0
-      242000     3120005         409          40           7           2           3           1           0           1           3           1           2
+      242000     3120001         199           6           2           1           1           0           0           0           0           0           0
+      242000     3120002         184           6           0           0           1           0           0           0           0           0           0
+      242000     3120003         380          35           3           1           2           0           0           0           0           0           0
+      242000     3120004         182          11           4           1           0           0           0           0           0           0           0
+      242000     3120005         394          36           7           2           2           1           0           1           3           2           1
diff --git a/Tools/WorkflowTestRunner/python/References.py b/Tools/WorkflowTestRunner/python/References.py
index f42cb575aa9..4310a5ab7de 100644
--- a/Tools/WorkflowTestRunner/python/References.py
+++ b/Tools/WorkflowTestRunner/python/References.py
@@ -11,7 +11,7 @@
 # Format is "test" : "version"
 references_map = {
     # Simulation
-    "s3761": "v4",
+    "s3761": "v5",
     "s3779": "v2",
     "s4005": "v3",
     "s4006": "v3",
-- 
GitLab