ATLAS Offline Software
G4AtlasUserActionConfig.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 from AthenaCommon.SystemOfUnits import MeV
4 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
5 from AthenaConfiguration.ComponentFactory import CompFactory
6 from AthenaConfiguration.Enums import BeamType, LHCPeriod
7 from CaloG4Sim.CaloG4SimConfig import CalibrationDefaultProcessingToolCfg
8 from SimulationConfig.SimEnums import CalibrationRun, CavernBackground, SimulationFlavour
9 from G4CosmicFilter.G4CosmicFilterConfig import CosmicFilterToolCfg, StoppedParticleFilterToolCfg
10 from G4UserActions.G4UserActionsConfig import (
11  AthenaStackingActionToolCfg, AthenaTrackingActionToolCfg,
12  G4SimTimerToolCfg, G4TrackCounterToolCfg, HitWrapperToolCfg,
13  LooperKillerToolCfg, StoppedParticleActionToolCfg
14 )
15 from ISF_Geant4CommonTools.ISF_Geant4CommonToolsConfig import EntryLayerToolCfg, EntryLayerToolMTCfg
16 from ISF_Services.ISF_ServicesCoreConfig import ATLFAST_GeoIDSvcCfg, GeoIDSvcCfg
17 from ISF_Services.ISF_ServicesConfig import (
18  ATLFAST_ParticleBrokerSvcCfg, ParticleBrokerSvcCfg, TruthServiceCfg
19 )
20 from MCTruthBase.MCTruthBaseConfig import MCTruthSteppingActionToolCfg
21 
22 
24  """ flags.Sim.OptionalUserActionList = ['G4UserActions.G4UserActionsConfig.FixG4CreatorProcessToolCfg']"""
25  result = ComponentAccumulator()
26  optionalUserActions = []
27  for userActionString in flags.Sim.OptionalUserActionList:
28  optionalUserActions += [result.popToolsAndMerge(getOptionalUACfg(flags, userActionString))]
29  result.setPrivateTools(optionalUserActions)
30  return result
31 
32 
33 def getOptionalUACfg(flags, userActionString):
34  """Execute a function to configure and optional UserAction"""
35  parts = userActionString.split('.')
36  if len(parts) < 2:
37  raise ValueError('OptionalUserAction strings should be of the form Package.Module.Function or Package.Function if defined in __init__.py')
38  function = parts[-1]
39  module = '.'.join(parts[:-1])
40  from importlib import import_module
41  loaded_module = import_module(module)
42  function_def = getattr(loaded_module, function)
43  return function_def(flags)
44 
45 
46 # Pulled in from ISF G4 to avoid circular dependence
47 def FullG4TrackProcessorUserActionToolCfg(flags, name="FullG4TrackProcessorUserActionTool", **kwargs):
48  result = ComponentAccumulator()
49  if flags.Sim.ISF.Simulator in [SimulationFlavour.FullG4MT, SimulationFlavour.FullG4MT_QS]:
50  tool = result.popToolsAndMerge(EntryLayerToolMTCfg(flags))
51  else:
52  tool = result.popToolsAndMerge(EntryLayerToolCfg(flags))
53  kwargs.setdefault("EntryLayerTool", result.addPublicTool(tool))
54  kwargs.setdefault("GeoIDSvc", result.getPrimaryAndMerge(GeoIDSvcCfg(flags)).name)
55  if flags.Detector.GeometryCavern:
56  kwargs.setdefault("TruthVolumeLevel", 2)
57  kwargs.setdefault("IsITkGeometry", flags.GeoModel.Run >= LHCPeriod.Run4)
58  result.setPrivateTools(CompFactory.G4UA.iGeant4.TrackProcessorUserActionFullG4Tool(name, **kwargs))
59  return result
60 
61 
62 def PhysicsValidationUserActionToolCfg(flags, name="ISFG4PhysicsValidationUserActionTool", **kwargs):
63  result = ComponentAccumulator()
64  result.setPrivateTools(CompFactory.G4UA.iGeant4.PhysicsValidationUserActionTool(name, **kwargs))
65  return result
66 
67 
68 def MCTruthUserActionToolCfg(flags, name="ISFMCTruthUserActionTool", **kwargs):
69  result = ComponentAccumulator()
70  kwargs.setdefault("TruthRecordSvc", result.getPrimaryAndMerge(TruthServiceCfg(flags)).name)
71  result.setPrivateTools(CompFactory.G4UA.iGeant4.MCTruthUserActionTool(name, **kwargs))
72  return result
73 
74 
75 def TrackProcessorUserActionToolCfg(flags, name="ISFG4TrackProcessorUserActionTool", **kwargs):
76  result = ComponentAccumulator()
77  if "ParticleBroker" not in kwargs:
78  kwargs.setdefault("ParticleBroker", result.getPrimaryAndMerge(ParticleBrokerSvcCfg(flags)).name)
79  kwargs.setdefault("GeoIDSvc", result.getPrimaryAndMerge(GeoIDSvcCfg(flags)).name)
80  result.setPrivateTools(CompFactory.G4UA.iGeant4.TrackProcessorUserActionPassBackTool(name, **kwargs))
81  return result
82 
83 
84 def PassBackG4TrackProcessorUserActionToolCfg(flags, name="PassBackG4TrackProcessorUserActionTool", **kwargs):
85  if flags.Sim.ISF.Simulator in [SimulationFlavour.PassBackG4MT]:
86  kwargs.setdefault("ParticleBroker", "")
87  return TrackProcessorUserActionToolCfg(flags, name, **kwargs)
88 
89 
90 def ATLFAST_G4TrackProcessorUserActionToolCfg(flags, name="ATLFAST_G4TrackProcessorUserActionTool", **kwargs):
91  result = ComponentAccumulator()
92  if flags.Sim.ISF.Simulator in [SimulationFlavour.PassBackG4MT, SimulationFlavour.ATLFASTIIMT, SimulationFlavour.ATLFAST3MT, SimulationFlavour.ATLFAST3MT_QS]:
93  kwargs.setdefault("ParticleBroker", "")
94  elif flags.Sim.ISF.Simulator in [SimulationFlavour.ATLFASTIIF_G4MS, SimulationFlavour.ATLFAST3F_G4MS]:
95  kwargs.setdefault("ParticleBroker", result.getPrimaryAndMerge(ATLFAST_ParticleBrokerSvcCfg(flags)).name)
96  kwargs.setdefault("GeoIDSvc", result.getPrimaryAndMerge(ATLFAST_GeoIDSvcCfg(flags)).name)
97  kwargs.setdefault("PassBackEkinThreshold", 0.05*MeV)
98  kwargs.setdefault("KillBoundaryParticlesBelowThreshold", True)
99  tool = result.popToolsAndMerge(PassBackG4TrackProcessorUserActionToolCfg(flags, name, **kwargs))
100  result.setPrivateTools(tool)
101  return result
102 
103 
104 # New function for all user action types
105 def getDefaultActions(flags):
106  result = ComponentAccumulator()
107 
108  actions = []
109 
110  # System stacking action
111  actions += [result.popToolsAndMerge(AthenaStackingActionToolCfg(flags))]
112 
113  # Some truth handling actions (and timing)
114  if not flags.Sim.ISFRun:
115  actions += [result.popToolsAndMerge(AthenaTrackingActionToolCfg(flags)),
116  result.popToolsAndMerge(MCTruthSteppingActionToolCfg(flags)),
117  result.popToolsAndMerge(G4SimTimerToolCfg(flags))]
118  # Track counter
119  actions += [result.popToolsAndMerge(G4TrackCounterToolCfg(flags))]
120 
121  # Cosmic Perigee action
122  if flags.Beam.Type is BeamType.Cosmics and flags.Sim.CavernBackground is CavernBackground.Off:
123  actions += [CompFactory.G4UA.CosmicPerigeeActionTool()]
124  # Cosmic filter
125  if flags.Beam.Type is BeamType.Cosmics and not flags.Sim.ISFRun:
126  actions += [result.popToolsAndMerge(CosmicFilterToolCfg(flags))]
127  if flags.Sim.StoppedParticleFile:
128  actions += [result.popToolsAndMerge(StoppedParticleFilterToolCfg(flags)),
129  result.popToolsAndMerge(StoppedParticleActionToolCfg(flags))]
130  # Hit wrapper action
131  if flags.Sim.CavernBackground is CavernBackground.Read:
132  actions += [result.popToolsAndMerge(HitWrapperToolCfg(flags))]
133  # Photon killer
134  if flags.Sim.PhysicsList == "QGSP_BERT_HP":
135  actions += [CompFactory.G4UA.PhotonKillerTool()]
136  # Calo calibration default processing
137  if flags.Sim.CalibrationRun in [CalibrationRun.LArTile, CalibrationRun.LArTileZDC]:
138  actions += [result.popToolsAndMerge(CalibrationDefaultProcessingToolCfg(flags))]
139  if 'G4UserActions.G4UserActionsConfig.MonopoleLooperKillerToolCfg' not in flags.Sim.OptionalUserActionList:
140  actions += [result.popToolsAndMerge(LooperKillerToolCfg(flags))]
141 
142  result.setPrivateTools(actions)
143  return result
144 
145 
146 def UserActionSvcCfg(flags, name="G4UA::UserActionSvc", **kwargs):
147  """
148  Get the standard UA svc configurable with all default actions added.
149  This function is normally called by the configured factory, not users.
150  """
151  result = ComponentAccumulator()
152 
153  generalActions = ( result.popToolsAndMerge(getDefaultActions(flags)) +
154  result.popToolsAndMerge(OptionalUserActionCfg(flags)) )
155 
156  # New user action tools
157  kwargs.setdefault("UserActionTools", generalActions)
158 
159  # placeholder for more advanced config, if needed
160  result.addService(CompFactory.G4UA.UserActionSvc(name, **kwargs), primary = True)
161 
162  return result
163 
164 
165 def CTBUserActionSvcCfg(flags, name="G4UA::CTBUserActionSvc", **kwargs):
166  result = ComponentAccumulator()
167  # FIXME migrate an alternative to this
168  generalActions = ( result.popToolsAndMerge(getDefaultActions(flags)) +
169  result.popToolsAndMerge(OptionalUserActionCfg(flags)) )
170  # This comment carried over from old style:
171  # FIXME: ADS these actions are not yet migrated to Hive
172  #if simFlags.SimLayout.get_Value()=="tb_LArH6_2004":
173  # eoe=["LArHitsH6EventAction"]+eoe
174  # eoe+=["LArGeoH62004EventAction"]
175  # if simFlags.LArTB_H6Step.statusOn:
176  # if simFlags.LArTB_H6Step.get_Value():
177  # stepping+=["LArGeoH62004SteppingAction"]
178  # event+=["RadLenNtuple"]
179  # eoe+=["RadLenNtuple"]
180  # stepping+=["RadLenNtuple"]
181 
182  # New user action tools
183  kwargs.setdefault("UserActionTools", generalActions)
184 
185  # placeholder for more advanced config, if needed
186  result.addService(CompFactory.G4UA.UserActionSvc(name, **kwargs), primary = True)
187  return result
188 
189 
190 def ISFUserActionSvcCfg(flags, name="G4UA::ISFUserActionSvc", **kwargs):
191  result = ComponentAccumulator()
192  TrackProcessorUserAction = kwargs.pop("TrackProcessorUserAction",[])
193 
194  PhysicsValidationUserAction = []
195  if flags.Sim.ISF.ValidationMode:
196  PhysicsValidationUserAction = [result.popPrivateTools(PhysicsValidationUserActionToolCfg(flags))]
197 
198  MCTruthUserAction = kwargs.pop("MCTruthUserAction",
199  [result.popToolsAndMerge(MCTruthUserActionToolCfg(flags))])
200 
201  generalActions = ( TrackProcessorUserAction + MCTruthUserAction +
202  result.popToolsAndMerge(getDefaultActions(flags)) +
203  result.popToolsAndMerge(OptionalUserActionCfg(flags)) +
204  PhysicsValidationUserAction )
205 
206  # New user action tools
207  kwargs.setdefault("UserActionTools", generalActions)
208  result.addService(CompFactory.G4UA.UserActionSvc(name, **kwargs), primary = True)
209  return result
210 
211 
212 def ISFFullUserActionSvcCfg(flags, name="G4UA::ISFFullUserActionSvc", **kwargs):
213  # this configuration needs ISFMCTruthUserAction
214  # and FullG4TrackProcessorUserAction
216  kwargs.setdefault("TrackProcessorUserAction", [tpAcc.popPrivateTools()])
217  result = ISFUserActionSvcCfg(flags, name, **kwargs)
218  result.merge(tpAcc)
219  return result
220 
221 
222 def ISFPassBackUserActionSvcCfg(flags, name="G4UA::ISFPassBackUserActionSvc", **kwargs):
223  # this configuration needs ISFMCTruthUserAction and
224  # PassBackG4TrackProcessorUserAction
225  tpAcc = ComponentAccumulator()
226  kwargs.setdefault("TrackProcessorUserAction",
227  [tpAcc.popToolsAndMerge(PassBackG4TrackProcessorUserActionToolCfg(flags))])
228  result = ISFUserActionSvcCfg(flags, name, **kwargs)
229  result.merge(tpAcc)
230  return result
231 
232 
233 def ISF_ATLFAST_UserActionSvcCfg(flags, name="G4UA::ISF_ATLFAST_UserActionSvc", **kwargs):
234  # this configuration needs ISFMCTruthUserAction and
235  # ATLFAST_G4TrackProcessorUserAction
236  tpAcc = ComponentAccumulator()
237  kwargs.setdefault("TrackProcessorUserAction",
238  [tpAcc.popToolsAndMerge(ATLFAST_G4TrackProcessorUserActionToolCfg(flags))])
239  result = ISFUserActionSvcCfg(flags, name, **kwargs)
240  result.merge(tpAcc)
241  return result
G4AtlasUserActionConfig.ATLFAST_G4TrackProcessorUserActionToolCfg
def ATLFAST_G4TrackProcessorUserActionToolCfg(flags, name="ATLFAST_G4TrackProcessorUserActionTool", **kwargs)
Definition: G4AtlasUserActionConfig.py:90
G4CosmicFilterConfig.CosmicFilterToolCfg
def CosmicFilterToolCfg(flags, name="G4UA::G4CosmicFilterTool", **kwargs)
Definition: G4CosmicFilterConfig.py:27
SystemOfUnits
G4CosmicFilterConfig.StoppedParticleFilterToolCfg
def StoppedParticleFilterToolCfg(flags, name="G4UA::StoppedParticleFilterTool", **kwargs)
Definition: G4CosmicFilterConfig.py:61
python.G4UserActionsConfig.AthenaStackingActionToolCfg
def AthenaStackingActionToolCfg(flags, name='G4UA::AthenaStackingActionTool', **kwargs)
Definition: G4UserActionsConfig.py:13
G4AtlasUserActionConfig.getOptionalUACfg
def getOptionalUACfg(flags, userActionString)
Definition: G4AtlasUserActionConfig.py:33
python.JetAnalysisCommon.ComponentAccumulator
ComponentAccumulator
Definition: JetAnalysisCommon.py:302
G4AtlasUserActionConfig.ISFFullUserActionSvcCfg
def ISFFullUserActionSvcCfg(flags, name="G4UA::ISFFullUserActionSvc", **kwargs)
Definition: G4AtlasUserActionConfig.py:212
python.G4UserActionsConfig.StoppedParticleActionToolCfg
def StoppedParticleActionToolCfg(flags, name="G4UA::StoppedParticleActionTool", **kwargs)
Definition: G4UserActionsConfig.py:78
ISF_ServicesConfig.ATLFAST_ParticleBrokerSvcCfg
def ATLFAST_ParticleBrokerSvcCfg(flags, name="ISF_ATLFAST_ParticleBrokerSvc", **kwargs)
Definition: ISF_ServicesConfig.py:70
G4AtlasUserActionConfig.OptionalUserActionCfg
def OptionalUserActionCfg(flags)
Definition: G4AtlasUserActionConfig.py:23
CaloG4SimConfig.CalibrationDefaultProcessingToolCfg
def CalibrationDefaultProcessingToolCfg(flags, name="G4UA::CaloG4::CalibrationDefaultProcessing", **kwargs)
Definition: CaloG4SimConfig.py:10
MCTruthBaseConfig.MCTruthSteppingActionToolCfg
def MCTruthSteppingActionToolCfg(flags, name='G4UA::MCTruthSteppingActionTool', **kwargs)
Definition: MCTruthBaseConfig.py:37
ISF_ServicesCoreConfig.ATLFAST_GeoIDSvcCfg
def ATLFAST_GeoIDSvcCfg(flags, name="ISF_ATLFAST_GeoIDSvc", **kwargs)
Definition: ISF_ServicesCoreConfig.py:39
G4AtlasUserActionConfig.MCTruthUserActionToolCfg
def MCTruthUserActionToolCfg(flags, name="ISFMCTruthUserActionTool", **kwargs)
Definition: G4AtlasUserActionConfig.py:68
G4AtlasUserActionConfig.UserActionSvcCfg
def UserActionSvcCfg(flags, name="G4UA::UserActionSvc", **kwargs)
Definition: G4AtlasUserActionConfig.py:146
python.G4UserActionsConfig.AthenaTrackingActionToolCfg
def AthenaTrackingActionToolCfg(flags, name='G4UA::AthenaTrackingActionTool', **kwargs)
Definition: G4UserActionsConfig.py:39
ISF_Geant4CommonToolsConfig.EntryLayerToolMTCfg
def EntryLayerToolMTCfg(flags, name="ISF_EntryLayerToolMT", **kwargs)
Definition: ISF_Geant4CommonToolsConfig.py:35
G4AtlasUserActionConfig.PhysicsValidationUserActionToolCfg
def PhysicsValidationUserActionToolCfg(flags, name="ISFG4PhysicsValidationUserActionTool", **kwargs)
Definition: G4AtlasUserActionConfig.py:62
ISF_ServicesConfig.ParticleBrokerSvcCfg
def ParticleBrokerSvcCfg(flags, name="ISF_ParticleBrokerSvc", **kwargs)
Definition: ISF_ServicesConfig.py:60
python.G4UserActionsConfig.HitWrapperToolCfg
def HitWrapperToolCfg(flags, name="G4UA::HitWrapperTool", **kwargs)
Definition: G4UserActionsConfig.py:96
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
G4AtlasUserActionConfig.ISFPassBackUserActionSvcCfg
def ISFPassBackUserActionSvcCfg(flags, name="G4UA::ISFPassBackUserActionSvc", **kwargs)
Definition: G4AtlasUserActionConfig.py:222
G4AtlasUserActionConfig.ISF_ATLFAST_UserActionSvcCfg
def ISF_ATLFAST_UserActionSvcCfg(flags, name="G4UA::ISF_ATLFAST_UserActionSvc", **kwargs)
Definition: G4AtlasUserActionConfig.py:233
ISF_ServicesConfig.TruthServiceCfg
def TruthServiceCfg(flags, **kwargs)
Definition: ISF_ServicesConfig.py:80
python.G4UserActionsConfig.G4TrackCounterToolCfg
def G4TrackCounterToolCfg(flags, name='G4UA::G4TrackCounterTool', **kwargs)
Definition: G4UserActionsConfig.py:72
G4AtlasUserActionConfig.FullG4TrackProcessorUserActionToolCfg
def FullG4TrackProcessorUserActionToolCfg(flags, name="FullG4TrackProcessorUserActionTool", **kwargs)
Definition: G4AtlasUserActionConfig.py:47
G4AtlasUserActionConfig.CTBUserActionSvcCfg
def CTBUserActionSvcCfg(flags, name="G4UA::CTBUserActionSvc", **kwargs)
Definition: G4AtlasUserActionConfig.py:165
G4AtlasUserActionConfig.getDefaultActions
def getDefaultActions(flags)
Definition: G4AtlasUserActionConfig.py:105
G4AtlasUserActionConfig.TrackProcessorUserActionToolCfg
def TrackProcessorUserActionToolCfg(flags, name="ISFG4TrackProcessorUserActionTool", **kwargs)
Definition: G4AtlasUserActionConfig.py:75
ISF_ServicesCoreConfig.GeoIDSvcCfg
def GeoIDSvcCfg(flags, name="ISF_GeoIDSvc", **kwargs)
Definition: ISF_ServicesCoreConfig.py:22
G4AtlasUserActionConfig.PassBackG4TrackProcessorUserActionToolCfg
def PassBackG4TrackProcessorUserActionToolCfg(flags, name="PassBackG4TrackProcessorUserActionTool", **kwargs)
Definition: G4AtlasUserActionConfig.py:84
python.G4UserActionsConfig.LooperKillerToolCfg
def LooperKillerToolCfg(flags, name='G4UA::LooperKillerTool', **kwargs)
Definition: G4UserActionsConfig.py:52
G4AtlasUserActionConfig.ISFUserActionSvcCfg
def ISFUserActionSvcCfg(flags, name="G4UA::ISFUserActionSvc", **kwargs)
Definition: G4AtlasUserActionConfig.py:190
python.G4UserActionsConfig.G4SimTimerToolCfg
def G4SimTimerToolCfg(flags, name='G4UA::G4SimTimerTool', **kwargs)
Definition: G4UserActionsConfig.py:66