From 6a36c53a19e4bb5c906d63d3cba636db5697f37a Mon Sep 17 00:00:00 2001
From: Carl Gwilliam <gwilliam@hep.ph.liv.ac.uk>
Date: Fri, 3 Jun 2022 10:27:33 +0100
Subject: [PATCH] Fix LOS dealing with ATLAS vs FASER coords

---
 .../GeneratorUtils/python/ShiftLOSConfig.py   |  5 ---
 .../test/G4FaserAlgConfigNew_Test.py          | 40 ++++++++++++++-----
 2 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/Generators/GeneratorUtils/python/ShiftLOSConfig.py b/Generators/GeneratorUtils/python/ShiftLOSConfig.py
index b9c3ec2a..ffc5c79b 100644
--- a/Generators/GeneratorUtils/python/ShiftLOSConfig.py
+++ b/Generators/GeneratorUtils/python/ShiftLOSConfig.py
@@ -7,8 +7,6 @@ from AthenaConfiguration.MainServicesConfig import AthSequencer
 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator, ConfigurationError
 from AthenaConfiguration.ComponentFactory import CompFactory
 
-from SGComps.AddressRemappingConfig import InputOverwriteCfg
-
 from GeneratorUtils.ShiftLOS import ShiftLOS
 
 
@@ -17,9 +15,6 @@ def ShiftLOSCfg(ConfigFlags, **kwargs) :
 
     cfg = ComponentAccumulator()
 
-    # Rename old truth collection to add ATLAS coord to can still use BeamTruthEvent for the one in FASER Coords
-    cfg.merge(InputOverwriteCfg("McEventCollection", "BeamTruthEvent", "McEventCollection", "BeamTruthEvent_ATLASCoord"))
-
     shift = ShiftLOS(name = kwargs.setdefault("name", "ShiftLOS"))
     shift.InputMCEventKey =  kwargs.setdefault("InputMCEventKey", "BeamTruthEvent_ATLASCoord")
     shift.OutputMCEventKey =  kwargs.setdefault("OutputMCEventKey", "BeamTruthEvent")    
diff --git a/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py b/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py
index 6b99fe63..f56b7bf7 100644
--- a/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py
+++ b/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py
@@ -58,11 +58,21 @@ if __name__ == '__main__':
     import sys
     ConfigFlags.fillFromArgs(sys.argv[1:])
 
-#     from math import atan
-#     from AthenaCommon.SystemOfUnits import GeV, TeV, cm, m
-#     from AthenaCommon.PhysicalConstants import pi
-#     import ParticleGun as PG
-#     ConfigFlags.Sim.Gun = {"Generator" : "SingleParticle", "pid" : 11, "energy" : PG.LogSampler(10*GeV, 1*TeV), "theta" :  PG.GaussianSampler(0, atan((10*cm)/(7*m)), oneside = True), "phi" : [0, 2*pi], "mass" : 0.511, "radius" : -10*cm, "randomSeed" : 12345}
+    doShiftLOS = (ConfigFlags.Sim.Beam.xangle or ConfigFlags.Sim.Beam.yangle or
+                  ConfigFlags.Sim.Beam.xshift or ConfigFlags.Sim.Beam.yshift)
+
+    from math import atan
+    from AthenaCommon.SystemOfUnits import GeV, TeV, cm, m
+    from AthenaCommon.PhysicalConstants import pi
+    import ParticleGun as PG
+    ConfigFlags.Sim.Gun = {"Generator" : "SingleParticle", "pid" : 11, "energy" : PG.LogSampler(10*GeV, 1*TeV), "theta" :
+                           PG.GaussianSampler(0, atan((10*cm)/(7*m)), oneside = True), "phi" : [0, 2*pi], "mass" : 0.511, "radius" : -10*cm, "randomSeed" : 12345}
+
+    if doShiftLOS:
+        pgConfig = ConfigFlags.Sim.Gun
+        pgConfig["McEventKey"] = "BeamTruthEvent_ATLASCoord"
+        ConfigFlags.Sim.Gun = pgConfig
+
 
 #
 # By being a little clever, we can steer the geometry setup from the command line using GeoModel.FaserVersion
@@ -97,12 +107,16 @@ if __name__ == '__main__':
         print("Input.Files = ",ConfigFlags.Input.Files)
 
 #
-# If so, and only one file that ends in .events read as HepMC
+# If so, and only one file that ends in .events or .hepmc read as HepMC
 #
         if len(ConfigFlags.Input.Files) == 1 and (ConfigFlags.Input.Files[0].endswith(".events") or ConfigFlags.Input.Files[0].endswith(".hepmc")):
 
             from HEPMCReader.HepMCReaderConfig import HepMCReaderCfg
-            cfg.merge(HepMCReaderCfg(ConfigFlags))
+
+            if doShiftLOS:
+                cfg.merge(HepMCReaderCfg(ConfigFlags, McEventKey = "BeamTruthEvent_ATLASCoord"))
+            else:
+                cfg.merge(HepMCReaderCfg(ConfigFlags))
 
             from McEventSelector.McEventSelectorConfig import McEventSelectorCfg
             cfg.merge(McEventSelectorCfg(ConfigFlags))
@@ -113,6 +127,12 @@ if __name__ == '__main__':
         else:
             from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
             cfg.merge(PoolReadCfg(ConfigFlags))
+
+            if doShiftLOS:
+                from SGComps.AddressRemappingConfig import InputOverwriteCfg
+                # Rename old truth collection to add ATLAS coord to can still use BeamTruthEvent for the one in FASER Coords
+                cfg.merge(InputOverwriteCfg("McEventCollection", "BeamTruthEvent", "McEventCollection", "BeamTruthEvent_ATLASCoord"))
+
 #
 # If not, configure the particle gun as requested, or using defaults
 #
@@ -122,6 +142,7 @@ if __name__ == '__main__':
 #
         from FaserParticleGun.FaserParticleGunConfig import FaserParticleGunCfg
         cfg.merge(FaserParticleGunCfg(ConfigFlags))
+        
         from McEventSelector.McEventSelectorConfig import McEventSelectorCfg
         cfg.merge(McEventSelectorCfg(ConfigFlags))
 
@@ -135,8 +156,7 @@ if __name__ == '__main__':
 # Shift LOS
 #
 
-    if (ConfigFlags.Sim.Beam.xangle or ConfigFlags.Sim.Beam.yangle or
-        ConfigFlags.Sim.Beam.xshift or ConfigFlags.Sim.Beam.yshift):
+    if doShiftLOS:
 
         import McParticleEvent.Pythonizations
         from GeneratorUtils.ShiftLOSConfig import ShiftLOSCfg
@@ -150,7 +170,7 @@ if __name__ == '__main__':
     from G4FaserAlg.G4FaserAlgConfigNew import G4FaserAlgCfg
     cfg.merge(G4FaserAlgCfg(ConfigFlags))
 
-    ###cfg.getEventAlgo("OutputStreamHITS").ItemList += ["McEventCollection#BeamTruthEvent_ATLASCoord"]    
+    #cfg.getEventAlgo("OutputStreamHITS").ItemList += ["McEventCollection#BeamTruthEvent_ATLASCoord"]    
 #
 # Dump config
 #
-- 
GitLab