ATLAS Offline Software
Loading...
Searching...
No Matches
GENtoEVGEN_Skeleton.py
Go to the documentation of this file.
1# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2#
3"""Functionality core of the Gen_tf transform"""
4
5# force no legacy job properties
6from AthenaCommon import JobProperties
7JobProperties.jobPropertiesDisallowed = True
8
9# Get logger
10from AthenaCommon.Logging import logging
11evgenLog = logging.getLogger("Gen_tf")
12
13# Common
14from AthenaCommon.SystemOfUnits import GeV
15from GeneratorConfig.Sequences import EvgenSequence
16from PyUtils.Helpers import release_metadata
17
18# Functions for pre/post-include/exec
19from PyJobTransforms.TransformUtils import processPreExec, processPreInclude, processPostExec, processPostInclude
20
21# Other imports that are needed
22import sys, os, re
23
24# Function that reads the jO and returns an instance of Sample(EvgenCAConfig)
25def setupSample(runArgs, flags):
26 # Only permit one jobConfig argument for evgen
27 if len(runArgs.jobConfig) != 1:
28 raise RuntimeError("You must supply one and only one jobConfig file argument")
29
30 evgenLog.info("Using JOBOPTSEARCHPATH (as seen in skeleton) = {}".format(os.environ["JOBOPTSEARCHPATH"]))
31
32 FIRST_DIR = (os.environ["JOBOPTSEARCHPATH"]).split(":")[0]
33
34 # Find jO file
35 jofiles = [f for f in os.listdir(FIRST_DIR) if (f.startswith("mc") and f.endswith(".py"))]
36 if len(jofiles) !=1:
37 raise RuntimeError("You must supply one and only one jobOption file in DSID directory")
38 jofile = jofiles[0]
39
40 # Perform consistency checks on the jO
41 from GeneratorConfig.GenConfigHelpers import checkJOConsistency, checkNEventsPerJob, checkKeywords, checkCategories
42 officialJO = checkJOConsistency(jofile)
43
44 # Import the jO as a module
45 # We cannot do import BLAH directly since
46 # 1. the filenames are not python compatible (mc.GEN_blah.py)
47 # 2. the filenames are different for every jO
48 import importlib.util
49 spec = importlib.util.spec_from_file_location(
50 name="sample",
51 location=os.path.join(FIRST_DIR,jofile),
52 )
53 jo = importlib.util.module_from_spec(spec)
54 spec.loader.exec_module(jo)
55 evgenLog.info("including file %s", jofile)
56
57 # Create instance of Sample(EvgenCAConfig)
58 sample = jo.Sample(flags)
59
60 # Set up the sample properties
61 sample.setupFlags(flags)
62
63 # Set the random number seed
64 # Need to use logic in EvgenJobTransforms.Generate_dsid_ranseed
65
66 # Get DSID
67 dsid = os.path.basename(runArgs.jobConfig[0])
68 if dsid.startswith("Test"):
69 dsid = dsid.split("Test")[-1]
70
71 # Update the global flags
72 if dsid.isdigit():
73 flags.Generator.DSID = int(dsid)
74
75 # Set nEventsPerJob
76 if not sample.nEventsPerJob:
77 evgenLog.info("#############################################################")
78 evgenLog.info(" !!!! no sample.nEventsPerJob set !!! The default 10000 used. !!! ")
79 evgenLog.info("#############################################################")
80 else:
81 checkNEventsPerJob(sample)
82 evgenLog.info(" nEventsPerJob = " + str(sample.nEventsPerJob))
83 flags.Generator.nEventsPerJob = sample.nEventsPerJob
84
85 # Check if sample attributes have been properly set
86 for var, value in vars(sample).items():
87 if not value:
88 raise RuntimeError("self.{} should be set in Sample(EvgenConfig)".format(var))
89 else:
90 evgenLog.info("MetaData: {} = {}".format(var, value))
91
92 # Keywords check
93 if hasattr(sample, "keywords"):
94 checkKeywords(sample, evgenLog, officialJO)
95
96 # L1, L2 categories check
97 if hasattr(sample, "categories"):
98 checkCategories(sample, evgenLog, officialJO)
99
100 return sample
101
102
103# Function to check black-listed releases
104def checkBlackList(cache, generatorName, checkType) :
105 isError = None
106 fileName = "BlackList_caches.txt" if checkType == "black" else "PurpleList_generators.txt"
107 with open(f"/cvmfs/atlas.cern.ch/repo/sw/Generators/MC16JobOptions/common/{fileName}") as bfile:
108 for line in bfile.readlines():
109 if not line.strip():
110 continue
111 # Bad caches
112 badCache=line.split(',')[1].strip()
113 # Bad generators
114 badGens=line.split(',')[2].strip()
115
116 used_gens = ','.join(generatorName)
117 # Match Generator and release cache
118 if cache==badCache and re.search(badGens,used_gens) is not None:
119 if badGens=="": badGens="all generators"
120 isError=f"{cache} is {checkType}-listed for {badGens}"
121 return isError
122 return isError
123
124
125# Main function
126def fromRunArgs(runArgs):
127 # print release information
128 d = release_metadata()
129 evgenLog.info("using release [%(project name)s-%(release)s] [%(platform)s] [%(nightly name)s/%(nightly release)s] -- built on [%(date)s]", d)
130 athenaRel = d["release"]
131
132 evgenLog.info("****************** STARTING EVENT GENERATION *****************")
133
134 evgenLog.info("**** Transformation run arguments")
135 evgenLog.info(runArgs)
136
137 evgenLog.info("**** Setting-up configuration flags")
138
139 from AthenaConfiguration.AllConfigFlags import initConfigFlags
140 flags = initConfigFlags()
141
142 from AthenaConfiguration.Enums import ProductionStep
143 flags.Common.ProductionStep = ProductionStep.Generation
144
145 # Convert run arguments to global athena flags
146 from PyJobTransforms.CommonRunArgsToFlags import commonRunArgsToFlags
147 commonRunArgsToFlags(runArgs, flags)
148
149 # Convert generator-specific run arguments to global athena flags
150 from GeneratorConfig.GeneratorConfigFlags import generatorRunArgsToFlags
151 generatorRunArgsToFlags(runArgs, flags)
152
153 # convert arguments to flags
154 flags.fillFromArgs()
155
156 # Create an instance of the Sample(EvgenCAConfig) and update global flags accordingly
157 sample = setupSample(runArgs, flags)
158
159 # Setup the main flags
160 flags.Exec.FirstEvent = runArgs.firstEvent
161 # Max events should be not set, job stopping is handled by CountHepMC
162 # using the RequestedOutput property
163 flags.Exec.MaxEvents = -1
164
165 if hasattr(runArgs, "inputEVNT_PreFile"):
166 flags.Input.Files = runArgs.inputEVNT_PreFile
167 else:
168 flags.Input.Files = []
169 flags.Input.RunNumbers = [flags.Generator.DSID]
170 flags.Input.TimeStamps = [0]
171
172 flags.Output.EVNTFileName = runArgs.outputEVNTFile
173
174 flags.Beam.Energy = runArgs.ecmEnergy / 2 * GeV
175
176 flags.PerfMon.doFastMonMT = True
177 flags.PerfMon.doFullMonMT = True
178
179 # Process pre-include
180 processPreInclude(runArgs, flags)
181
182 # Process pre-exec
183 processPreExec(runArgs, flags)
184
185 # Lock flags
186 flags.lock()
187
188 evgenLog.info("**** Configuration flags")
189 if runArgs.VERBOSE:
190 flags.dump()
191 else:
192 flags.dump("Generator.*")
193
194 # Print various stuff
195 evgenLog.info(".transform = Gen_tf")
196 evgenLog.info(".platform = " + str(os.environ["BINARY_TAG"]))
197
198 # Announce start of job configuration
199 evgenLog.info("**** Configuring event generation")
200
201 # Main object
202 from AthenaConfiguration.MainServicesConfig import MainEvgenServicesCfg
203 cfg = MainEvgenServicesCfg(flags, withSequences=True)
204
205 # Input file handling (if needed)
206 if flags.Input.Files:
207 from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
208 cfg.merge(PoolReadCfg(flags))
209
210 # EventInfoCnvAlg
211 from xAODEventInfoCnv.xAODEventInfoCnvConfig import EventInfoCnvAlgCfg
212 cfg.merge(EventInfoCnvAlgCfg(flags, disableBeamSpot=True, xAODKey="TMPEvtInfo"),
213 sequenceName=EvgenSequence.Generator.value)
214
215 # Set up the process
216 cfg.merge(sample.setupProcess(flags))
217
218 # Sort the list of generator names into standard form
219 from GeneratorConfig.GenConfigHelpers import gen_sortkey
220 from GeneratorConfig.Versioning import generatorsGetInitialVersionedDictionary, generatorsVersionedStringList
221 generators = sorted(cfg.getService("GeneratorInfoSvc").Generators, key=gen_sortkey)
222 gendict = generatorsGetInitialVersionedDictionary(generators)
223 generatorsWithVersion = generatorsVersionedStringList(gendict)
224
225 # Check if the setup requires steering
226 from GeneratorConfig.GenConfigHelpers import gen_require_steering
227 if gen_require_steering(generators):
228 if hasattr(runArgs, "outputEVNTFile") and not hasattr(runArgs, "outputEVNT_PreFile"):
229 raise RuntimeError("'EvtGen' found in job options name, please set '--steering=afterburn'")
230
231 # Check black-list and purple-list
232 blError = checkBlackList(athenaRel, generators, "black")
233 plError = checkBlackList(athenaRel, generators, "purple")
234 if blError is not None:
235 raise RuntimeError(blError)
236 if plError is not None:
237 evgenLog.warning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
238 evgenLog.warning(f"!!! WARNING {plError} !!!")
239 evgenLog.warning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
240
241 # Fix non-standard event features
242 if not flags.Input.Files:
243 from EvgenProdTools.EvgenProdToolsConfig import FixHepMCCfg
244 from GeneratorConfig.GenConfigHelpers import gens_purgenoendvtx
245 generatorsList = generators.copy()
246 if "Pythia8" in generatorsList:
247 pythia8Alg = cfg.getEventAlgo("Pythia8_i")
248 if pythia8Alg.Beam1 != "PROTON" or pythia8Alg.Beam2 != "PROTON":
249 # generator name is still "Pythia8", even when colliding nuclei
250 generatorsList.append("Pythia8-Angantyr")
251 cfg.merge(FixHepMCCfg(flags,
252 PurgeUnstableWithoutEndVtx=gens_purgenoendvtx(generatorsList)))
253
254
255 from GeneratorConfig.GenConfigHelpers import gens_testhepmc
256 if gens_testhepmc(generators):
257 from EvgenProdTools.EvgenProdToolsConfig import TestHepMCCfg
258 cfg.merge(TestHepMCCfg(flags))
259
260 # Copy the event weight from HepMC to the Athena EventInfo class
261 from EvgenProdTools.EvgenProdToolsConfig import CopyEventWeightCfg
262 cfg.merge(CopyEventWeightCfg(flags))
263
264 from EvgenProdTools.EvgenProdToolsConfig import FillFilterValuesCfg
265 cfg.merge(FillFilterValuesCfg(flags))
266
267 # Configure the event counting (AFTER all filters)
268 from EvgenProdTools.EvgenProdToolsConfig import CountHepMCCfg
269 cfg.merge(CountHepMCCfg(flags,
270 RequestedOutput=sample.nEventsPerJob if runArgs.maxEvents == -1
271 else runArgs.maxEvents))
272 evgenLog.info("Requested output events = %d", cfg.getEventAlgo("CountHepMC").RequestedOutput)
273
274 # Print out the contents of the first 5 events (after filtering)
275 if hasattr(runArgs, "printEvts") and runArgs.printEvts > 0:
276 from TruthIO.TruthIOConfig import PrintMCCfg
277 cfg.merge(PrintMCCfg(flags,
278 LastEvent=runArgs.printEvts))
279
280 # PerfMon
281 from PerfMonComps.PerfMonCompsConfig import PerfMonMTSvcCfg
282 cfg.merge(PerfMonMTSvcCfg(flags), sequenceName=EvgenSequence.Post.value)
283
284 # Estimate time needed for Simulation
285 from EvgenProdTools.EvgenProdToolsConfig import SimTimeEstimateCfg
286 cfg.merge(SimTimeEstimateCfg(flags))
287
288 # TODO: Rivet
289
290 # Extra metadata
291 from EventInfoMgt.TagInfoMgrConfig import TagInfoMgrCfg
292 from GeneratorConfig.GenConfigHelpers import gen_lhef
293 metadata = {
294 "project_name": "IS_SIMULATION",
295 f"AtlasRelease_{runArgs.trfSubstepName}": flags.Input.Release or "n/a",
296 "beam_energy": str(int(flags.Beam.Energy)),
297 "beam_type": flags.Beam.Type.value,
298 "generators": '+'.join(generatorsWithVersion),
299 "tune": cfg.getService("GeneratorInfoSvc").Tune,
300 "hepmc_version": f"HepMC{os.environ['HEPMCVER']}",
301 "keywords": ", ".join(sample.keywords).lower(),
302 "lhefGenerator": '+'.join(filter(gen_lhef, generators)),
303 "mc_channel_number": str(flags.Generator.DSID),
304 }
305 if hasattr(sample, "process"): metadata.update({"evgenProcess": sample.process})
306 if hasattr(sample, "specialConfig"): metadata.update({"specialConfiguration": sample.specialConfig})
307 if hasattr(sample, "hardPDF"): metadata.update({"hardPDF": sample.hardPDF})
308 if hasattr(sample, "softPDF"): metadata.update({"softPDF": sample.softPDF})
309 if hasattr(sample, "randomSeed"): metadata.update({"randomSeed": str(runArgs.randomSeed)})
310 cfg.merge(TagInfoMgrCfg(flags, tagValuePairs=metadata))
311
312 # Print metadata in the log
313 evgenLog.info(f"HepMC version {os.environ["HEPMCVER"]}")
314 evgenLog.info(f"MetaData: generatorTune = {cfg.getService("GeneratorInfoSvc").Tune}")
315 evgenLog.info("MetaData: generatorName = {}".format(generatorsWithVersion))
316
317 # Configure output stream
318 from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg
319 cfg.merge(OutputStreamCfg(flags, "EVNT", ["McEventCollection#*"]))
320
321 # Add in-file MetaData
322 from xAODMetaDataCnv.InfileMetaDataConfig import SetupMetaDataForStreamCfg
323 cfg.merge(SetupMetaDataForStreamCfg(flags, "EVNT"))
324
325 # Post-include
326 processPostInclude(runArgs, flags, cfg)
327
328 # Post-exec
329 processPostExec(runArgs, flags, cfg)
330
331 # Write AMI tag into in-file MetaData
332 from PyUtils.AMITagHelperConfig import AMITagCfg
333 cfg.merge(AMITagCfg(flags, runArgs))
334
335 # Print ComponentAccumulator components
336 cfg.printConfig(prefix="Gen_tf", printSequenceTreeOnly=not runArgs.VERBOSE)
337
338 # Run final ComponentAccumulator
339 sys.exit(not cfg.run().isSuccess())
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
checkBlackList(cache, generatorName, checkType)