diff --git a/Generators/GeneratorUtils/python/ShiftLOS.py b/Generators/GeneratorUtils/python/ShiftLOS.py index da6eff7e28db61f16960863890249d3903e495b3..d7e8f21d0c8fbe67346a9e435239e9858515510b 100644 --- a/Generators/GeneratorUtils/python/ShiftLOS.py +++ b/Generators/GeneratorUtils/python/ShiftLOS.py @@ -11,7 +11,7 @@ except ImportError: from AthenaPython.PyAthena import HepMC as HepMC class ShiftLOS(PyAthena.Alg): - def __init__(self, name="ShiftLOS", InputMCEventKey="BeamTruthEvent", OutputMCEventKey="BeamTruthEventShifted", xcross = 0.0, ycross = 0.0, simple = False): + def __init__(self, name="ShiftLOS", InputMCEventKey="BeamTruthEvent", OutputMCEventKey="BeamTruthEventShifted", xcross = 0, ycross = 0): super(ShiftLOS,self).__init__(name=name) self.InputMCEventKey = InputMCEventKey self.OutputMCEventKey = OutputMCEventKey @@ -38,13 +38,10 @@ class ShiftLOS(PyAthena.Alg): # Shift x or y by appropriate crossing angle if self.xcross: x += dz * self.xcross - self.msg.info(f"Shifting x by {self.xcross} over {dz}: {pos.x()} -> {x} ") + self.msg.debug(f"Shifting x by {self.xcross} over {dz}: {pos.x()} -> {x} ") elif self.ycross: y += dz * self.ycross - self.msg.info(f"Shifting y by {self.ycross} over {dz}: {pos.y()} -> {y} ") - else: - self.msg.error(f"Must suply crossing angle in either x or y (but not both)") - return StatusCode.Failure + self.msg.debug(f"Shifting y by {self.ycross} over {dz}: {pos.y()} -> {y} ") v.set_position(HepMC.FourVector(x, y, z, pos.t())) @@ -72,10 +69,8 @@ class ShiftLOS(PyAthena.Alg): pxsum += tmp.x() - mom.x() pysum += tmp.y() - mom.y() - p.print() # Convert back to HepMC p.set_momentum(HepMC.FourVector(tmp.px(), tmp.py(), tmp.pz(), tmp.e())) - p.print() self.msg.debug(f"Change in total px = {pxsum:.1f} MeV ({pxsum/pxsum_orig * 100: .3f} %), change in total py = {pysum:.1f} MeV ({pysum/pysum_orig * 100: .3f} %)") @@ -84,6 +79,11 @@ class ShiftLOS(PyAthena.Alg): def execute(self): self.msg.debug(f"Exectuing {self.getName()}") + print (self.xcross, self.ycross) + + if not self.xcross and not self.ycross: + return StatusCode.Success + self.msg.debug(f"Reading {self.InputMCEventKey}") inevt = self.evtStore[self.InputMCEventKey][0] diff --git a/Generators/GeneratorUtils/python/ShiftLOSConfig.py b/Generators/GeneratorUtils/python/ShiftLOSConfig.py new file mode 100644 index 0000000000000000000000000000000000000000..9683df1f8b3a2b4ee7f831533415315b2972cfda --- /dev/null +++ b/Generators/GeneratorUtils/python/ShiftLOSConfig.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration + +# import sys +from AthenaConfiguration.MainServicesConfig import AthSequencer +from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator +from AthenaConfiguration.ComponentFactory import CompFactory + +from GeneratorUtils.ShiftLOS import ShiftLOS + + +def ShiftLOSCfg(ConfigFlags, **kwargs) : + import McParticleEvent.Pythonizations + + cfg = ComponentAccumulator() + shift = ShiftLOS(name = kwargs.setdefault("name", "ShiftLOS")) + shift.InputMCEventKey = kwargs.setdefault("InputMCEventKey", "BeamTruthEvent") + shift.OutputMCEventKey = kwargs.setdefault("OutputMCEventKey", "BeamTruthEventShifted") + shift.xcross = kwargs.setdefault("xcross", 0) + shift.ycross = kwargs.setdefault("ycross", 0) + cfg.addEventAlgo(shift, sequenceName = "AthBeginSeq", primary = True) # to run *before* G4 + + return cfg diff --git a/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py b/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py index d6e254c86c1c9f8207512c52414847663fd39a11..418429635d803df570dd1298065af5ba944db1de 100644 --- a/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py +++ b/Simulation/G4Faser/G4FaserAlg/test/G4FaserAlgConfigNew_Test.py @@ -12,7 +12,7 @@ if __name__ == '__main__': # Set up logging and config behaviour # from AthenaCommon.Logging import log - from AthenaCommon.Constants import DEBUG + from AthenaCommon.Constants import DEBUG, VERBOSE from AthenaCommon.Configurable import Configurable log.setLevel(DEBUG) Configurable.configurableRun3Behavior = 1 @@ -43,6 +43,8 @@ if __name__ == '__main__': ConfigFlags.Sim.ReleaseGeoModel = False ConfigFlags.Sim.IncludeParentsInG4Event = True # Controls whether BeamTruthEvent is written to output HITS file ConfigFlags.addFlag("Sim.Gun",{"Generator" : "SingleParticle"}) # Property bag for particle gun keyword:argument pairs + ConfigFlags.addFlag("Sim.Beam.xangle", 0) # Potential beam crossing angles + ConfigFlags.addFlag("Sim.Beam.yangle", 0) ConfigFlags.GeoModel.FaserVersion = "FASERNU-02" # Geometry set-up ConfigFlags.IOVDb.GlobalTag = "OFLCOND-FASER-02" # Conditions set-up @@ -53,6 +55,12 @@ 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} + #ConfigFlags.Sim.Gun = {"Generator" : "SingleParticle", "pid" : 11, "energy" : PG.LogSampler(10*GeV, 1*TeV), "theta" : 0, "phi" : [0, 2*pi], "mass" : 0.511, "radius" : 10*cm, "randomSeed" : 12345} # # By being a little clever, we can steer the geometry setup from the command line using GeoModel.FaserVersion # @@ -86,7 +94,8 @@ if __name__ == '__main__': # # If so, and only one file that ends in .events read as HepMC # - if len(ConfigFlags.Input.Files) == 1 and ConfigFlags.Input.Files[0].endswith(".events"): + 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)) @@ -110,16 +119,31 @@ if __name__ == '__main__': cfg.merge(FaserParticleGunCfg(ConfigFlags)) from McEventSelector.McEventSelectorConfig import McEventSelectorCfg cfg.merge(McEventSelectorCfg(ConfigFlags)) + # # Output file # from AthenaPoolCnvSvc.PoolWriteConfig import PoolWriteCfg cfg.merge(PoolWriteCfg(ConfigFlags)) + +# +# Shift LOS +# + + if ConfigFlags.Sim.Beam.xangle or ConfigFlags.Sim.Beam.yangle: + MCEventKey = "BeamTruthEventShifted" + import McParticleEvent.Pythonizations + from GeneratorUtils.ShiftLOSConfig import ShiftLOSCfg + cfg.merge(ShiftLOSCfg(ConfigFlags, OutputMCEventKey = MCEventKey, + xcross = ConfigFlags.Sim.Beam.xangle, ycross = ConfigFlags.Sim.Beam.yangle)) + else: + MCEventKey = "BeamTruthEvent" + # # Add the G4FaserAlg # from G4FaserAlg.G4FaserAlgConfigNew import G4FaserAlgCfg - cfg.merge(G4FaserAlgCfg(ConfigFlags)) + cfg.merge(G4FaserAlgCfg(ConfigFlags, InputTruthCollection = MCEventKey)) # # Dump config # @@ -136,6 +160,8 @@ if __name__ == '__main__': # # Execute and finish # + #cfg.foreach_component("*").OutputLevel = VERBOSE + sc = cfg.run() b = time.time()