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