From cb51b400c503fb634c26ddf9086eb9935566cdee Mon Sep 17 00:00:00 2001
From: Carl Gwilliam <gwilliam@hep.ph.liv.ac.uk>
Date: Tue, 7 Jun 2022 11:01:15 +0100
Subject: [PATCH] Merge

---
 ...serMC-MDC_PG_muon_fasernu_logE-101302.json |  2 +-
 .../Generation/scripts/faserMDC_foresee.py    | 20 ++++++-----
 .../scripts/faserMDC_particlegun.py           | 20 ++++++-----
 .../Reconstruction/scripts/faserMDC_reco.py   |  2 +-
 .../scripts/submit_faserMDC_reco.sh           |  2 +-
 .../Simulation/scripts/faserMDC_simulate.py   | 34 ++++++++++++-------
 .../scripts/submit_faserMDC_simulate.sh       | 30 ++++++++++++++--
 .../python/RadialPosSampler.py                |  1 -
 .../HEPMCReader/python/HepMCReaderConfig.py   | 30 +++++++++++-----
 .../test/G4FaserAlgConfigNew_Test.py          |  0
 10 files changed, 98 insertions(+), 43 deletions(-)
 mode change 100644 => 100755 Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py

diff --git a/Control/CalypsoExample/Generation/data/mdc/FaserMC-MDC_PG_muon_fasernu_logE-101302.json b/Control/CalypsoExample/Generation/data/mdc/FaserMC-MDC_PG_muon_fasernu_logE-101302.json
index 8ae524dc..76ba19e1 100644
--- a/Control/CalypsoExample/Generation/data/mdc/FaserMC-MDC_PG_muon_fasernu_logE-101302.json
+++ b/Control/CalypsoExample/Generation/data/mdc/FaserMC-MDC_PG_muon_fasernu_logE-101302.json
@@ -9,5 +9,5 @@
     "sampler": "log",
     "segment": 0,
     "short": "MDC_PG_muon_fasernu_logE",
-    "zpos": -5000.0
+    "zpos": -4000.0
 }
diff --git a/Control/CalypsoExample/Generation/scripts/faserMDC_foresee.py b/Control/CalypsoExample/Generation/scripts/faserMDC_foresee.py
index 607ce904..b48bbf86 100755
--- a/Control/CalypsoExample/Generation/scripts/faserMDC_foresee.py
+++ b/Control/CalypsoExample/Generation/scripts/faserMDC_foresee.py
@@ -113,6 +113,14 @@ if __name__ == '__main__':
     #if args.zpos:
     #    ConfigFlags.Sim.Gun["z"] = args.zpos
 
+    doShiftLOS = (ConfigFlags.Sim.Beam.xangle or ConfigFlags.Sim.Beam.yangle or
+                  ConfigFlags.Sim.Beam.xshift or ConfigFlags.Sim.Beam.yshift)
+
+    if doShiftLOS:
+        pgConfig = ConfigFlags.Sim.Gun
+        pgConfig["McEventKey"] = "BeamTruthEvent_ATLASCoord"
+        ConfigFlags.Sim.Gun = pgConfig
+
 #
 # MDC geometry configuration
 #
@@ -153,26 +161,22 @@ 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):
-
-        MCEventKey = "BeamTruthEventShifted"
+    if doShiftLOS:
         import McParticleEvent.Pythonizations
         from GeneratorUtils.ShiftLOSConfig import ShiftLOSCfg
-        cfg.merge(ShiftLOSCfg(ConfigFlags, OutputMCEventKey = MCEventKey,
+
+        cfg.merge(ShiftLOSCfg(ConfigFlags, 
                               xcross = ConfigFlags.Sim.Beam.xangle, 
                               ycross = ConfigFlags.Sim.Beam.yangle,
                               xshift = ConfigFlags.Sim.Beam.xshift,
                               yshift = ConfigFlags.Sim.Beam.yshift))
 
-    else:    
-        MCEventKey = "BeamTruthEvent"
     
 #
 # Add the G4FaserAlg
 #
     from G4FaserAlg.G4FaserAlgConfigNew import G4FaserAlgCfg
-    cfg.merge(G4FaserAlgCfg(ConfigFlags, InputTruthCollection = MCEventKey))
+    cfg.merge(G4FaserAlgCfg(ConfigFlags))
 #
 # Dump config
 #
diff --git a/Control/CalypsoExample/Generation/scripts/faserMDC_particlegun.py b/Control/CalypsoExample/Generation/scripts/faserMDC_particlegun.py
index c9f9154e..20970fdf 100755
--- a/Control/CalypsoExample/Generation/scripts/faserMDC_particlegun.py
+++ b/Control/CalypsoExample/Generation/scripts/faserMDC_particlegun.py
@@ -124,6 +124,14 @@ if __name__ == '__main__':
     # Pass this in one go to ConfigFlags
     ConfigFlags.Sim.Gun = sg_dict
 
+    doShiftLOS = (ConfigFlags.Sim.Beam.xangle or ConfigFlags.Sim.Beam.yangle or
+                  ConfigFlags.Sim.Beam.xshift or ConfigFlags.Sim.Beam.yshift)
+
+    if doShiftLOS:
+        pgConfig = ConfigFlags.Sim.Gun
+        pgConfig["McEventKey"] = "BeamTruthEvent_ATLASCoord"
+        ConfigFlags.Sim.Gun = pgConfig
+
 #
 # MDC geometry configuration
 #
@@ -164,26 +172,22 @@ 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):
-
-        MCEventKey = "BeamTruthEventShifted"
+    if doShiftLOS:
         import McParticleEvent.Pythonizations
         from GeneratorUtils.ShiftLOSConfig import ShiftLOSCfg
-        cfg.merge(ShiftLOSCfg(ConfigFlags, OutputMCEventKey = MCEventKey,
+
+        cfg.merge(ShiftLOSCfg(ConfigFlags, 
                               xcross = ConfigFlags.Sim.Beam.xangle, 
                               ycross = ConfigFlags.Sim.Beam.yangle,
                               xshift = ConfigFlags.Sim.Beam.xshift,
                               yshift = ConfigFlags.Sim.Beam.yshift))
 
-    else:    
-        MCEventKey = "BeamTruthEvent"
     
 #
 # Add the G4FaserAlg
 #
     from G4FaserAlg.G4FaserAlgConfigNew import G4FaserAlgCfg
-    cfg.merge(G4FaserAlgCfg(ConfigFlags, InputTruthCollection = MCEventKey))
+    cfg.merge(G4FaserAlgCfg(ConfigFlags))
 #
 # Dump config
 #
diff --git a/Control/CalypsoExample/Reconstruction/scripts/faserMDC_reco.py b/Control/CalypsoExample/Reconstruction/scripts/faserMDC_reco.py
index f9003eec..942340f1 100755
--- a/Control/CalypsoExample/Reconstruction/scripts/faserMDC_reco.py
+++ b/Control/CalypsoExample/Reconstruction/scripts/faserMDC_reco.py
@@ -191,7 +191,7 @@ itemList = [ "xAOD::EventInfo#*"
 #
 if args.isMC:
     # Add truth records here?
-    itemList.extend( ["McEventCollection#*"] )
+    itemList.extend( ["McEventCollection#*", "TrackerSimDataCollection#*"] )
 
 acc.merge(OutputStreamCfg(ConfigFlags, "xAOD", itemList))
 
diff --git a/Control/CalypsoExample/Reconstruction/scripts/submit_faserMDC_reco.sh b/Control/CalypsoExample/Reconstruction/scripts/submit_faserMDC_reco.sh
index 60c39ca3..b04d56aa 100755
--- a/Control/CalypsoExample/Reconstruction/scripts/submit_faserMDC_reco.sh
+++ b/Control/CalypsoExample/Reconstruction/scripts/submit_faserMDC_reco.sh
@@ -124,7 +124,7 @@ fi
 cd "$file_stem"
 #
 # Run job
-if [[ -z "$rtag" ]]; then
+if [[ -z "$tag" ]]; then
     faserMDC_reco.py "--nevents=$nevents" "$file_path"
 else
     faserMDC_reco.py "--nevents=$nevents" "--reco=$tag" "$file_path"
diff --git a/Control/CalypsoExample/Simulation/scripts/faserMDC_simulate.py b/Control/CalypsoExample/Simulation/scripts/faserMDC_simulate.py
index 323adda6..12de492f 100755
--- a/Control/CalypsoExample/Simulation/scripts/faserMDC_simulate.py
+++ b/Control/CalypsoExample/Simulation/scripts/faserMDC_simulate.py
@@ -127,6 +127,7 @@ if __name__ == '__main__':
 # Skip events
 #
     ConfigFlags.Exec.SkipEvents = args.skip
+    ConfigFlags.Exec.MaxEvents = args.nevents
 #
 # Output file name
 # 
@@ -153,6 +154,9 @@ if __name__ == '__main__':
     # import sys
     # ConfigFlags.fillFromArgs(sys.argv[1:])
 
+    doShiftLOS = (ConfigFlags.Sim.Beam.xangle or ConfigFlags.Sim.Beam.yangle or
+                  ConfigFlags.Sim.Beam.xshift or ConfigFlags.Sim.Beam.yshift)
+
 #
 # MDC geometry configuration
 #
@@ -178,7 +182,11 @@ if __name__ == '__main__':
     if 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))
@@ -189,6 +197,11 @@ if __name__ == '__main__':
         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"))
+
 #
 # Output file
 #
@@ -198,26 +211,22 @@ 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:
 
-        MCEventKey = "BeamTruthEventShifted"
         import McParticleEvent.Pythonizations
         from GeneratorUtils.ShiftLOSConfig import ShiftLOSCfg
-        cfg.merge(ShiftLOSCfg(ConfigFlags, OutputMCEventKey = MCEventKey,
+        cfg.merge(ShiftLOSCfg(ConfigFlags, 
                               xcross = ConfigFlags.Sim.Beam.xangle, 
                               ycross = ConfigFlags.Sim.Beam.yangle,
                               xshift = ConfigFlags.Sim.Beam.xshift,
                               yshift = ConfigFlags.Sim.Beam.yshift))
 
-    else:    
-        MCEventKey = "BeamTruthEvent"
 
 #
 # Add the G4FaserAlg
 #
     from G4FaserAlg.G4FaserAlgConfigNew import G4FaserAlgCfg
-    cfg.merge(G4FaserAlgCfg(ConfigFlags, InputTruthCollection = MCEventKey))
+    cfg.merge(G4FaserAlgCfg(ConfigFlags))
 #
 # Dump config
 #
@@ -235,10 +244,11 @@ if __name__ == '__main__':
 # Execute and finish
 #
 
-    if args.verbose:
-        cfg.foreach_component("*").OutputLevel = "DEBUG" 
-    else:
-        cfg.foreach_component("*").OutputLevel = "INFO"  
+    # This fails with ShiftLOSCfg...
+    #if args.verbose:
+    #    cfg.foreach_component("*").OutputLevel = "DEBUG" 
+    #else:
+    #    cfg.foreach_component("*").OutputLevel = "INFO"  
 
     sc = cfg.run(maxEvents=args.nevents)
 
diff --git a/Control/CalypsoExample/Simulation/scripts/submit_faserMDC_simulate.sh b/Control/CalypsoExample/Simulation/scripts/submit_faserMDC_simulate.sh
index e00adfd5..3c175d72 100755
--- a/Control/CalypsoExample/Simulation/scripts/submit_faserMDC_simulate.sh
+++ b/Control/CalypsoExample/Simulation/scripts/submit_faserMDC_simulate.sh
@@ -2,7 +2,10 @@
 # Used with a condor file to submit to vanilla universe
 #
 # Usage:
-# submit_faserMDC_simluate.sh input_file output_file [release_directory] [working_directory] [skip] [nevts]
+# submit_faserMDC_simluate.sh [--shift] input_file output_file [release_directory] [working_directory] [skip] [nevts]
+#
+# Options:
+# --shift - apply crossing angle (and FASER shift)
 # 
 # input_file - full file name (with path)
 # output_file - full output file name
@@ -22,6 +25,24 @@
 SECONDS=0
 #
 # Parse command-line options
+while [ -n "$1" ]
+do 
+  case "$1" in
+    -s | --shift) 
+	  echo "Applying crossing-angle shift" 
+	  xangle=1 
+	  shift;;  # This 'eats' the argument
+
+    -*) 
+	  echo "Unknown option $1"
+	  shift;;
+
+    *) break;;  # Not an option, don't shift
+  esac
+done
+
+#
+# Parse command-line arguments
 infile=${1}
 outfile=${2}
 release_directory=${3}
@@ -143,8 +164,11 @@ cd "${file_stem}"
 # Run job
 #if [[ -z "$tag" ]]; then
 #fi
-faserMDC_simulate.py  --skip "$skip_events" -n "$nevts" "$infile" "$outfile"
-
+if [[ -z "$xangle" ]]; then
+    faserMDC_simulate.py  --skip "$skip_events" -n "$nevts" "$infile" "$outfile"
+else
+    faserMDC_simulate.py  --yangle -0.000150 --yshift 12.0 --skip "$skip_events" -n "$nevts" "$infile" "$outfile"
+fi
 #
 # Print out ending time
 date
diff --git a/Generators/FaserParticleGun/python/RadialPosSampler.py b/Generators/FaserParticleGun/python/RadialPosSampler.py
index dd599bf8..7f1ed179 100644
--- a/Generators/FaserParticleGun/python/RadialPosSampler.py
+++ b/Generators/FaserParticleGun/python/RadialPosSampler.py
@@ -89,4 +89,3 @@ if __name__ == "__main__":
     plt.hist(np.sqrt(xarr**2 + yarr**2))    
     plt.tight_layout()
     plt.show()
-x
diff --git a/Generators/HEPMCReader/python/HepMCReaderConfig.py b/Generators/HEPMCReader/python/HepMCReaderConfig.py
index bb116c11..affee04e 100644
--- a/Generators/HEPMCReader/python/HepMCReaderConfig.py
+++ b/Generators/HEPMCReader/python/HepMCReaderConfig.py
@@ -2,7 +2,7 @@
 
 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
 
-import sys, tempfile
+import sys, tempfile, pathlib
 from AthenaConfiguration.MainServicesConfig import AthSequencer
 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
 from AthenaConfiguration.ComponentFactory import CompFactory
@@ -13,7 +13,8 @@ def get_file_skip_events(ConfigFlags):
     "Create a file with events from ConfigFlags.Exec.SkipEvents to ConfigFlags.Exec.SkipEvents + ConfigFlags.Exec.MaxEvents"
 
     usetemp = True
-    
+    #usetemp = False
+
     skip = ConfigFlags.Exec.SkipEvents
     fname = ConfigFlags.Input.Files[0]
     evtMax = ConfigFlags.Exec.MaxEvents
@@ -21,12 +22,22 @@ def get_file_skip_events(ConfigFlags):
     if skip == 0:
         return fname
 
+    print(f"get_file_skip_events skipping {skip} events with max {evtMax}")
+
     if usetemp:
         fout = tempfile.NamedTemporaryFile("w", delete = False)
         foutname = fout.name
     else:
-        foutname, fext = ".".join(fname.split('.')[:-1]), fname.split('.')[-1]
-        foutname = f"{foutname}-evts{skip}-{skip+evtMax}.{fext}"        
+        infile = pathlib.Path(fname)
+        # Put this in current working directory
+        if evtMax > 0:
+            end = skip+evtMax
+        else:
+            end = 'all'
+
+        foutname = f"{infile.stem}=evts{skip}-{end}.{infile.suffix}"
+        #foutname, fext = ".".join(fname.split('.')[:-1]), fname.split('.')[-1]
+        #foutname = f"{foutname}-evts{skip}-{skip+evtMax}{fext}"        
         fout = open(foutname, "w")
 
     fout.write("HepMC::Version 2.06.09\nHepMC::IO_GenEvent-START_EVENT_LISTING\n")
@@ -37,19 +48,22 @@ def get_file_skip_events(ConfigFlags):
             if l.startswith("E "):
                 ievt += 1
 
-            if ievt > skip + evtMax:
-                    break
+            if evtMax > 0 and ievt > skip + evtMax:
+                break
                 
             if ievt > skip:
+                #print(f"Writing event {ievt}")
                 fout.write(l)
-
+            # else:
+                # print(f"Skipping event {ievt}")
 
     fout.write("HepMC::IO_GenEvent-END_EVENT_LISTING\n")
     fout.close()
 
+    #print(f"Wrote to file {foutname}")
+
     return foutname
                 
-    
 def HepMCReaderCfg(ConfigFlags, **kwargs) :
     cfg = ComponentAccumulator(AthSequencer("AthBeginSeq", Sequential = True))
     from TruthIO.TruthIOConf import HepMCReadFromFile
diff --git a/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py b/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py
old mode 100644
new mode 100755
-- 
GitLab