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