ATLAS Offline Software
Loading...
Searching...
No Matches
SimulationMetadata.py
Go to the documentation of this file.
1# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
3from AthenaCommon.Logging import logging
4from AthenaConfiguration.Enums import FlagEnum, ProductionStep
5from AthenaKernel.EventIdOverrideConfig import getMinMaxRunNumbers
6
7folderName = "/Simulation/Parameters"
8
9
11 """Collect simulation metadata parameters as a dictionary"""
12 simMDlog = logging.getLogger('Sim_Metadata')
13 params = {}
14
15 #add all flags to the metadata
16 #todo - only add certain ones?
17 #in future this should be a ConfigFlags method...?
18 for flag in sorted(flags._flagdict): #only sim
19 if flag.startswith("Sim."):
20 if "GenerationConfiguration" in flag:
21 # This flag is only temporarily defined in the SimConfigFlags module
22 continue
23 if "Twiss" in flag and not flags.Detector.GeometryForward:
24 # The various Twiss flags should only be written out when Forward Detector simulation is enabled
25 continue
26 if "UseShadowEvent" in flag and not flags.Sim.UseShadowEvent:
27 # This flag is added temporarily to allow a new approach to quasi-stable particle simulation to be tested.
28 continue
29 if "VertexTimeWidth" in flag and not flags.Sim.VertexTimeSmearing:
30 # This flag is only written to metadata when vertex time smearing is enabled
31 continue
32 if "RunOnGPU" in flag and not flags.Sim.ISF.Simulator.usesFastCaloSim():
33 # This flag is only written to metadata when FastCaloSim/FastCaloGAN is enabled
34 continue
35 if "FastCalo.ParamsInputFilename" in flag and not flags.Sim.ISF.Simulator.usesFastCaloSim():
36 # This flag is only written to metadata when FastCaloSim/FastCaloGAN is enabled
37 continue
38 if "SimplifiedGeoPath" in flag and not flags.Sim.SimplifiedGeoPath:
39 # This flag is only written to metadata in case the FastCaloSim simplified geometry path is set
40 continue
41 if "FastCalo.doPunchThrough" in flag and not flags.Sim.FastCalo.doPunchThrough:
42 # This flag is only written to metadata in case PunchThroughG4Tool is set
43 continue
44 if "UseG4Workers" in flag:
45 # This flag is still experimental, and should not be recorded in metadata yet
46 continue
47
48 key = flag.split(".")[-1] #use final part of flag as the key
49 value = flags._get(flag)
50 if isinstance(value, FlagEnum):
51 value = value.value
52 if not isinstance(value, str):
53 value = str(value)
54 params[key] = value
55 simMDlog.info('SimulationMetaData: setting "%s" to be %s', key, value)
56
57 params['G4Version'] = flags.Sim.G4Version
58 params['RunType'] = 'atlas'
59 params['beamType'] = flags.Beam.Type.value
60 params['SimLayout'] = flags.GeoModel.AtlasVersion
61 params['MagneticField'] = 'AtlasFieldSvc' # TODO hard-coded for now for consistency with old-style configuration.
62
63 #---------
64
65 from AthenaConfiguration.DetectorConfigFlags import getEnabledDetectors
66 simDets = ['Truth'] + getEnabledDetectors(flags)
67 simMDlog.info("Setting 'SimulatedDetectors' = %r", simDets)
68 params['SimulatedDetectors'] = repr(simDets)
69
70
71 params['hitFileMagicNumber'] = '0'
72
73 if flags.Sim.ISFRun:
74 params['Simulator'] = flags.Sim.ISF.Simulator.value
75 params['SimulationFlavour'] = flags.Sim.ISF.Simulator.value.replace('MT', '') # used by egamma
76 else:
77 # TODO hard-code for now, but set flag properly later
78 params['Simulator'] = 'AtlasG4'
79 params['SimulationFlavour'] = 'AtlasG4'
80
81
82 if flags.Common.isOverlay and flags.Overlay.DataOverlay:
83 params['IsDataOverlay'] = 'True'
84
85 return params
86
87
88def fillAtlasMetadata(flags, dbFiller):
89 """Fill ParameterDbFiller with simulation metadata (sqlite mode interface)"""
90 params = collectSimulationMetadata(flags)
91 for key, value in params.items():
92 dbFiller.addSimParam(key, value)
93
94
96 simMDlog = logging.getLogger('Sim_Metadata')
97 myRunNumber, myEndRunNumber = getMinMaxRunNumbers(flags)
98 simMDlog.debug('Metadata BeginRun = %s', str(myRunNumber))
99 simMDlog.debug('Metadata EndRun = %s', str(myEndRunNumber))
100
101 if flags.IOVDb.WriteParametersAsMetaData:
102 # Direct in-file metadata mode: bypass intermediate sqlite files
103 from IOVDbMetaDataTools.ParameterWriterConfig import writeParametersToMetaData
104 simMDlog.info('Writing simulation parameters directly to in-file metadata (bypassing SimParams.db)')
105 params = collectSimulationMetadata(flags)
106 return writeParametersToMetaData(flags, folderName, params, myRunNumber, myEndRunNumber)
107 else:
108 # Sqlite mode: write to SimParams.db intermediate file
109 from IOVDbMetaDataTools import ParameterDbFiller
110 simMDlog.info('Writing simulation parameters to intermediate sqlite file (SimParams.db)')
112 dbFiller.setBeginRun(myRunNumber)
113 dbFiller.setEndRun(myEndRunNumber)
114
115 fillAtlasMetadata(flags, dbFiller)
116
117 #-------------------------------------------------
118 # Make the MetaData Db
119 #-------------------------------------------------
120 dbFiller.genSimDb()
121
122 return writeSimulationParameters(flags)
123
124
126 """Read simulation parameters metadata"""
127 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
128 from IOVDbSvc.IOVDbSvcConfig import addFolders
129
130 # Direct in-file metadata mode: IOVDbMetaDataTool populates ConditionStore from file metadata
131 # Exception: In overlay mode, always use IOVDbSvc since input files may not have parameters in metadata
132 if flags.IOVDb.WriteParametersAsMetaData and not flags.Common.isOverlay:
133 return ComponentAccumulator()
134
135 # Sqlite mode: use IOVDbSvc to read and populate ConditionStore
136 if flags.Common.ProductionStep in [ProductionStep.Simulation, ProductionStep.FastChain]:
137 # Reading from intermediate sqlite file SimParams.db (during simulation job)
138 return addFolders(flags, folderName, detDb="SimParams.db", db="SIMPARAM", className="AthenaAttributeList")
139 else:
140 # Reading from input file metadata via IOVDbSvc
141 return addFolders(flags, folderName, className="AthenaAttributeList", tag="HEAD")
142
143
145 """Write digitization parameters metadata"""
146 from IOVDbSvc.IOVDbSvcConfig import IOVDbSvcCfg, addFolders
147 acc = IOVDbSvcCfg(flags, FoldersToMetaData=[folderName])
148 acc.merge(addFolders(flags, folderName, detDb="SimParams.db", db="SIMPARAM"))
149 return acc
fillAtlasMetadata(flags, dbFiller)