ATLAS Offline Software
BeamEffectsAlgConfig.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 
5 """Define methods to configure beam effects with the ComponentAccumulator"""
6 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
7 from AthenaConfiguration.ComponentFactory import CompFactory
8 from AthenaConfiguration.Enums import BeamType, ProductionStep
9 
10 # possible components from BeamEffectsConf
11 # todo names required to copy function name? what are names used for?
12 # todo add default construction options to make these potentiall useful
13 # todo verify and add suggestions made in todo
14 
15 
16 # GenEventManipulators
17 def ValidityCheckerCfg(flags, name="GenEventValidityChecker", **kwargs):
18  """Return a validity checker tool"""
19  acc = ComponentAccumulator()
20  acc.setPrivateTools(CompFactory.Simulation.GenEventValidityChecker(name, **kwargs))
21  return acc
22 
23 
24 def GenEventRotatorCfg(flags, name="GenEventRotator", **kwargs):
25  """Return a event rotator tool"""
26  acc = ComponentAccumulator()
27  acc.setPrivateTools(CompFactory.Simulation.GenEventRotator(name, **kwargs))
28  return acc
29 
30 
31 def GenEventBeamEffectBoosterCfg(flags, name="GenEventBeamEffectBooster", **kwargs):
32  """Return a lorentz booster tool"""
33  # todo needs random seed, more?
34  acc = ComponentAccumulator()
35  acc.setPrivateTools(CompFactory.Simulation.GenEventBeamEffectBooster(name, **kwargs))
36  return acc
37 
38 
39 def GenEventVertexPositionerCfg(flags, name="GenEventVertexPositioner", **kwargs):
40  """Return a vertex positioner tool"""
41  # todo needs input file(s?)
42 
43  acc = ComponentAccumulator()
44 
45  from SimulationConfig.SimEnums import VertexSource
46  if flags.Sim.VertexSource is VertexSource.MatchingBkg:
47  kwargs.setdefault("VertexShifters", [acc.popToolsAndMerge(MatchingBkgVertexPositionerCfg(flags))])
48  elif flags.Sim.VertexSource is VertexSource.CondDB:
49  kwargs.setdefault("VertexShifters", [acc.popToolsAndMerge(VertexBeamCondPositionerCfg(flags))])
50  elif flags.Sim.VertexSource is VertexSource.LongBeamspotVertexPositioner:
51  kwargs.setdefault("VertexShifters", [acc.popToolsAndMerge(LongBeamspotVertexPositionerCfg(flags))])
52 
53  acc.setPrivateTools(CompFactory.Simulation.GenEventVertexPositioner(name, **kwargs))
54  return acc
55 
56 
57 # LorentzVectorGenerators
58 def VertexBeamCondPositionerCfg(flags, name="VertexBeamCondPositioner", **kwargs):
59  """Return a conditional (? todo) vertex positioner tool"""
60  from RngComps.RngCompsConfig import AthRNGSvcCfg
61 
62  acc = ComponentAccumulator()
63 
64  kwargs.setdefault("RandomSvc", acc.getPrimaryAndMerge(AthRNGSvcCfg(flags)).name)
65  kwargs.setdefault("SimpleTimeSmearing", flags.Sim.VertexTimeSmearing)
66  kwargs.setdefault("TimeWidth", flags.Sim.VertexTimeWidth)
67 
68  from BeamSpotConditions.BeamSpotConditionsConfig import BeamSpotCondAlgCfg
69  acc.merge(BeamSpotCondAlgCfg(flags))
70 
71  acc.setPrivateTools(CompFactory.Simulation.VertexBeamCondPositioner(name, **kwargs))
72  return acc
73 
74 
75 def MatchingBkgVertexPositionerCfg(flags, name="MatchingBkgVertexPositioner", **kwargs):
76  """Return a vertex positioner tool that reads a matching vertex from the background input file."""
77  acc = ComponentAccumulator()
78 
79  if flags.Overlay.DataOverlay:
80  kwargs.setdefault("PrimaryVertexContainerName", f"{flags.Overlay.BkgPrefix}PrimaryVertices")
81 
82  from SGComps.SGInputLoaderConfig import SGInputLoaderCfg
83  acc.merge(SGInputLoaderCfg(flags, [f'xAOD::VertexContainer#{kwargs["PrimaryVertexContainerName"]}']))
84 
85  acc.setPrivateTools(CompFactory.Simulation.MatchingBkgVertexPositioner(name, **kwargs))
86  return acc
87 
88 
89 def CrabKissingVertexPositionerCfg(flags, name="CrabKissingVertexPositioner", **kwargs):
90  """Return a Crab-Kissing vertex positioner tool"""
91  # todo needs BunchLength, RandomSvc, BunchShape
92  acc = ComponentAccumulator()
93  acc.setPrivateTools(CompFactory.Simulation.CrabKissingVertexPositioner(name, **kwargs))
94  return acc
95 
96 
97 def LongBeamspotVertexPositionerCfg(flags, name="LongBeamspotVertexPositioner", **kwargs):
98  """Return a long beamspot vertex positioner tool"""
99  # todo needs LParameter and RandomSvc
100  acc = ComponentAccumulator()
101  kwargs.setdefault("SimpleTimeSmearing", flags.Sim.VertexTimeSmearing)
102  acc.setPrivateTools(CompFactory.Simulation.LongBeamspotVertexPositioner(name, **kwargs))
103  return acc
104 
105 
106 def BeamEffectsAlgCfg(flags, name="BeamEffectsAlg", **kwargs):
107  """Return an accumulator and algorithm for beam effects, wihout output"""
108  acc = ComponentAccumulator()
109 
110  from SimulationConfig.SimEnums import LArParameterization
111  if flags.Sim.LArParameterization is LArParameterization.FastCaloSim:
112  kwargs.setdefault("ISFRun", True)
113  else:
114  kwargs.setdefault("ISFRun", flags.Sim.ISFRun)
115 
116  # Set default properties
117  if flags.Sim.DoFullChain and flags.Digitization.PileUp:
118  kwargs.setdefault("InputMcEventCollection", "OriginalEvent_SG+GEN_EVENT")
119  else:
120  kwargs.setdefault("InputMcEventCollection", "GEN_EVENT")
121 
122  if flags.Sim.DoFullChain and flags.Common.isOverlay and not flags.Overlay.DataOverlay:
123  kwargs.setdefault('OutputMcEventCollection', f"{flags.Overlay.SigPrefix}TruthEvent")
124  else:
125  kwargs.setdefault('OutputMcEventCollection', 'BeamTruthEvent')
126 
127  # Set (todo) the appropriate manipulator tools
128  manipulators = []
129  manipulators.append(acc.popToolsAndMerge(ValidityCheckerCfg(flags)))
130  from SimulationConfig.SimEnums import VertexSource
131  if not flags.Sim.VertexSource == VertexSource.AsGenerated:
132  # Vertex manipulation required
133  from SimulationConfig.SimEnums import CavernBackground
134  if flags.Beam.Type not in [BeamType.Cosmics, BeamType.TestBeam] and flags.Sim.CavernBackground is not CavernBackground.Read:
135  manipulators.append(acc.popToolsAndMerge(GenEventVertexPositionerCfg(flags)))
136  # manipulators.append(acc.popToolsAndMerge(GenEventBeamEffectBoosterCfg(flags))) # todo segmentation violation
137  # manipulators.append(acc.popToolsAndMerge(CrabKissingVertexPositionerCfg(flags))) # todo Callback registration failed
138  kwargs.setdefault("GenEventManipulators", manipulators)
139 
140  acc.addEventAlgo(CompFactory.Simulation.BeamEffectsAlg(name, **kwargs), primary=True)
141  return acc
142 
143 
144 def BeamEffectsAlgOutputCfg(flags, **kwargs):
145  """Return an accumulator and algorithm for beam effects, with output"""
146  acc = BeamEffectsAlgCfg(flags, **kwargs)
147  # Set to write HITS pool file
148  alg = acc.getPrimary()
149  ItemList = ["McEventCollection#" + alg.OutputMcEventCollection]
150  from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg
151  acc.merge(OutputStreamCfg(flags, "HITS", ItemList=ItemList, disableEventTag=True))
152  return acc
153 
154 
155 def BeamSpotFixerAlgCfg(flags, name="BeamSpotFixerAlg", **kwargs):
156  from BeamSpotConditions.BeamSpotConditionsConfig import BeamSpotCondAlgCfg
157  acc = BeamSpotCondAlgCfg(flags)
158 
159  kwargs.setdefault("InputKey", "Input_EventInfo")
160  if flags.Common.ProductionStep == ProductionStep.PileUpPresampling:
161  kwargs.setdefault("OutputKey", flags.Overlay.BkgPrefix + "EventInfo")
162  else:
163  kwargs.setdefault("OutputKey", "EventInfo")
164 
165  acc.addEventAlgo(CompFactory.Simulation.BeamSpotFixerAlg(name, **kwargs))
166  return acc
167 
168 
169 def ZeroLifetimePositionerCfg(flags, name="ZeroLifetimePositioner", **kwargs):
170  result = ComponentAccumulator()
171  kwargs.setdefault('ApplyPatch', True)
172  kwargs.setdefault('RemovePatch', True)
173  result.addService(CompFactory.Simulation.ZeroLifetimePositioner(name, **kwargs), primary = True)
174  return result
175 
176 
177 def BeamSpotReweightingAlgCfg(flags, name="BeamSpotReweightingAlg", **kwargs):
178  from BeamSpotConditions.BeamSpotConditionsConfig import BeamSpotCondAlgCfg
179  acc = BeamSpotCondAlgCfg(flags)
180 
181  kwargs.setdefault("Input_beam_sigma_z", flags.Digitization.InputBeamSigmaZ)
182 
183  acc.addEventAlgo(CompFactory.Simulation.BeamSpotReweightingAlg(name, **kwargs))
184 
185  # Ignore dependencies
186  from AthenaConfiguration.MainServicesConfig import OutputUsageIgnoreCfg
187  acc.merge(OutputUsageIgnoreCfg(flags, name))
188 
189  return acc
190 
191 
192 if __name__ == "__main__":
193  from AthenaCommon.Logging import log
194  from AthenaCommon.Constants import DEBUG
195  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
196  from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
197 
198  from AthenaConfiguration.TestDefaults import defaultGeometryTags
199  from AthenaConfiguration.AllConfigFlags import initConfigFlags
200  flags = initConfigFlags()
201 
202  # Set up logging
203  log.setLevel(DEBUG)
204 
205  import os
206  inputDir = os.environ.get("ATLAS_REFERENCE_DATA",
207  "/cvmfs/atlas-nightlies.cern.ch/repo/data/data-art")
208  # Provide input
209  flags.Input.Files = [
210  inputDir +
211  "/SimCoreTests/e_E50_eta34_49.EVNT.pool.root"
212  ]
213 
214  # Specify output
215  flags.Output.HITSFileName = "myHITS.pool.root"
216 
217  # set the source of vertex positioning
218  from SimulationConfig.SimEnums import VertexSource
219  # flags.Sim.VertexSource = VertexSource.VertexOverrideFile
220  flags.Sim.VertexSource = VertexSource.CondDB
221  # flags.Sim.VertexSource = VertexSource.LongBeamspotVertexPositioner"
222 
223  # included to stop segmentation error - TODO see why it's failing
224  flags.Input.isMC = True
225  flags.GeoModel.AtlasVersion = defaultGeometryTags.RUN2
226  flags.IOVDb.GlobalTag = "OFLCOND-MC16-SDR-14" # conditions tag for conddb (which one to use - old one for simulation)
227  flags.Input.RunNumbers = [284500] # run test job with and without run number and 222510
228 
229  # Finalize
230  flags.lock()
231 
232  # Initialize a new component accumulator
233  cfg = MainServicesCfg(flags) # use this syntax for storegate
234  # Add configuration to read EVNT pool file
235  cfg.merge(PoolReadCfg(flags))
236 
237  # Make use of our defiend function
238  cfg.merge(BeamEffectsAlgCfg(flags))
239 
240  cfg.getService("StoreGateSvc").Dump = True
241  cfg.printConfig(withDetails=True)
242  flags.dump()
243 
244  # Store in a pickle file
245  with open("BeamEffectsAlg.pkl", "wb") as f:
246  cfg.store(f)
247 
248  # Run it in athena
249  cfg.run(maxEvents=20)
BeamEffectsAlgConfig.ValidityCheckerCfg
def ValidityCheckerCfg(flags, name="GenEventValidityChecker", **kwargs)
Definition: BeamEffectsAlgConfig.py:17
python.JetAnalysisCommon.ComponentAccumulator
ComponentAccumulator
Definition: JetAnalysisCommon.py:302
BeamEffectsAlgConfig.LongBeamspotVertexPositionerCfg
def LongBeamspotVertexPositionerCfg(flags, name="LongBeamspotVertexPositioner", **kwargs)
Definition: BeamEffectsAlgConfig.py:97
python.OutputStreamConfig.OutputStreamCfg
def OutputStreamCfg(flags, streamName, ItemList=[], MetadataItemList=[], disableEventTag=False, trigNavThinningSvc=None, takeItemsFromInput=False, extendProvenanceRecord=True, AcceptAlgs=[], HelperTools=[])
Definition: OutputStreamConfig.py:12
python.BeamSpotConditionsConfig.BeamSpotCondAlgCfg
def BeamSpotCondAlgCfg(flags, name="BeamSpotCondAlg", **kwargs)
Definition: BeamSpotConditionsConfig.py:7
python.MainServicesConfig.OutputUsageIgnoreCfg
def OutputUsageIgnoreCfg(flags, algorithm)
Definition: MainServicesConfig.py:57
BeamEffectsAlgConfig.BeamEffectsAlgCfg
def BeamEffectsAlgCfg(flags, name="BeamEffectsAlg", **kwargs)
Definition: BeamEffectsAlgConfig.py:106
SGInputLoaderConfig.SGInputLoaderCfg
def SGInputLoaderCfg(flags, Load=None, **kwargs)
Definition: SGInputLoaderConfig.py:7
BeamEffectsAlgConfig.GenEventVertexPositionerCfg
def GenEventVertexPositionerCfg(flags, name="GenEventVertexPositioner", **kwargs)
Definition: BeamEffectsAlgConfig.py:39
BeamEffectsAlgConfig.CrabKissingVertexPositionerCfg
def CrabKissingVertexPositionerCfg(flags, name="CrabKissingVertexPositioner", **kwargs)
Definition: BeamEffectsAlgConfig.py:89
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:260
Constants
some useful constants -------------------------------------------------—
BeamEffectsAlgConfig.BeamSpotFixerAlgCfg
def BeamSpotFixerAlgCfg(flags, name="BeamSpotFixerAlg", **kwargs)
Definition: BeamEffectsAlgConfig.py:155
BeamEffectsAlgConfig.VertexBeamCondPositionerCfg
def VertexBeamCondPositionerCfg(flags, name="VertexBeamCondPositioner", **kwargs)
Definition: BeamEffectsAlgConfig.py:58
Trk::open
@ open
Definition: BinningType.h:40
BeamEffectsAlgConfig.BeamEffectsAlgOutputCfg
def BeamEffectsAlgOutputCfg(flags, **kwargs)
Definition: BeamEffectsAlgConfig.py:144
BeamEffectsAlgConfig.BeamSpotReweightingAlgCfg
def BeamSpotReweightingAlgCfg(flags, name="BeamSpotReweightingAlg", **kwargs)
Definition: BeamEffectsAlgConfig.py:177
BeamEffectsAlgConfig.GenEventBeamEffectBoosterCfg
def GenEventBeamEffectBoosterCfg(flags, name="GenEventBeamEffectBooster", **kwargs)
Definition: BeamEffectsAlgConfig.py:31
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
BeamEffectsAlgConfig.GenEventRotatorCfg
def GenEventRotatorCfg(flags, name="GenEventRotator", **kwargs)
Definition: BeamEffectsAlgConfig.py:24
BeamEffectsAlgConfig.MatchingBkgVertexPositionerCfg
def MatchingBkgVertexPositionerCfg(flags, name="MatchingBkgVertexPositioner", **kwargs)
Definition: BeamEffectsAlgConfig.py:75
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:69
BeamEffectsAlgConfig.ZeroLifetimePositionerCfg
def ZeroLifetimePositionerCfg(flags, name="ZeroLifetimePositioner", **kwargs)
Definition: BeamEffectsAlgConfig.py:169
RngCompsConfig.AthRNGSvcCfg
def AthRNGSvcCfg(flags, name="AthRNGSvc")
Definition: RngCompsConfig.py:51