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