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