ATLAS Offline Software
Loading...
Searching...
No Matches
GENtoEVGEN_Skeleton.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 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 GeneratorConfig.Sequences import EvgenSequence
15from PyUtils.Helpers import release_metadata
16
17# Functions for pre/post-include/exec
18from PyJobTransforms.TransformUtils import (
19 processPreExec,
20 processPreInclude,
21 processPostExec,
22 processPostInclude
23)
24
25# Helper functions
26from EvgenJobTransforms.EvgenHelpers import (
27 _count_lhe_events,
28 _validate_sample_properties,
29 _handle_input_files,
30 _is_txt_only_run
31)
32
33# Other imports that are needed
34import sys, os, re
35
36# Function that reads the jO and returns an instance of Sample(EvgenCAConfig)
37def setupSample(flags):
38 # Only permit one jobConfig argument for evgen
39 job_config = flags.Generator.jobConfig
40 if isinstance(job_config, str):
41 job_config = [job_config]
42 if len(job_config) != 1:
43 raise RuntimeError("You must supply one and only one jobConfig file argument")
44
45 evgenLog.info("Using JOBOPTSEARCHPATH (as seen in skeleton) = {}".format(os.environ["JOBOPTSEARCHPATH"]))
46
47 FIRST_DIR = (os.environ["JOBOPTSEARCHPATH"]).split(":")[0]
48
49 # Find jO file
50 jofiles = [f for f in os.listdir(FIRST_DIR) if (f.startswith("mc") and f.endswith(".py"))]
51 if len(jofiles) !=1:
52 raise RuntimeError("You must supply one and only one jobOption file in DSID directory")
53 jofile = jofiles[0]
54
55 # Perform consistency checks on the jO
56 from GeneratorConfig.GenConfigHelpers import (
57 checkNaming,
58 checkNEventsPerJob,
59 checkKeywords,
60 checkCategories
61 )
62 checkNaming(jofile)
63
64 # Import the jO as a module
65 # We cannot do import BLAH directly since
66 # 1. the filenames are not python compatible (mc.GEN_blah.py)
67 # 2. the filenames are different for every jO
68 import importlib.util
69 spec = importlib.util.spec_from_file_location(
70 name="sample",
71 location=os.path.join(FIRST_DIR,jofile),
72 )
73 jo = importlib.util.module_from_spec(spec)
74
75 spec.loader.exec_module(jo)
76 evgenLog.info(f"including file {jofile}")
77
78 # Create instance of Sample(EvgenCAConfig)
79 sample = jo.Sample(flags)
80
81 # Set up the sample properties
82 sample.setupFlags(flags)
83
84 # Set the random number seed
85 # Need to use logic in EvgenJobTransforms.Generate_dsid_ranseed
86
87 # Get DSID
88 dsid = os.path.basename(job_config[0])
89 if dsid.startswith("Test"):
90 dsid = dsid.split("Test")[-1]
91
92 # Update the global flags
93 if dsid.isdigit():
94 flags.Generator.DSID = int(dsid)
95
96 # Set nEventsPerJob
97 if not sample.nEventsPerJob:
98 evgenLog.info("#############################################################")
99 evgenLog.info(" !!!! no sample.nEventsPerJob set !!!")
100 evgenLog.info("#############################################################")
101 # We don't need to set the global flag because its default is 10000
102 else:
103 checkNEventsPerJob(sample)
104 evgenLog.info(" nEventsPerJob = " + str(sample.nEventsPerJob))
105 flags.Generator.nEventsPerJob = sample.nEventsPerJob
106
107 # Validate all required/conditional sample metadata with explicit rules.
108 _validate_sample_properties(sample)
109
110 # Propagate optional sample values to global flags.
111 flags.Generator.inputFilesPerJob = sample.inputFilesPerJob
112 flags.Generator.MEgenerator = sample.MEgenerator or ""
113
114 # Print sample metadata in the log.
115 for var, value in vars(sample).items():
116 evgenLog.info("MetaData: {} = {}".format(var, value))
117
118 # Keywords check
119 if hasattr(sample, "keywords"):
120 checkKeywords(sample, evgenLog)
121
122 # L1, L2 categories check
123 if hasattr(sample, "categories"):
124 checkCategories(sample, evgenLog)
125
126 return sample
127
128
129# Function to check black-listed releases
130def checkBlackList(cache, generatorName, checkType) :
131 isError = None
132 fileName = "BlackList_caches.txt" if checkType == "black" else "PurpleList_generators.txt"
133 with open(f"/cvmfs/atlas.cern.ch/repo/sw/Generators/MC16JobOptions/common/{fileName}") as bfile:
134 for line in bfile.readlines():
135 if not line.strip():
136 continue
137 # Bad caches
138 badCache=line.split(',')[1].strip()
139 # Bad generators
140 badGens=line.split(',')[2].strip()
141
142 used_gens = ','.join(generatorName)
143 # Match Generator and release cache
144 if cache==badCache and re.search(badGens,used_gens) is not None:
145 if badGens=="": badGens="all generators"
146 isError=f"{cache} is {checkType}-listed for {badGens}"
147 return isError
148 return isError
149
150
151# Main function
152def fromRunArgs(runArgs):
153 # print release information
154 d = release_metadata()
155 evgenLog.info("using release [%(project name)s-%(release)s] [%(platform)s] [%(nightly name)s/%(nightly release)s] -- built on [%(date)s]", d)
156 athenaRel = d["release"]
157
158 evgenLog.info("****************** STARTING EVENT GENERATION *****************")
159
160 evgenLog.info("**** Transformation run arguments")
161 evgenLog.info(runArgs)
162
163 evgenLog.info("**** Setting-up configuration flags")
164
165 from AthenaConfiguration.AllConfigFlags import initConfigFlags
166 flags = initConfigFlags()
167
168 from AthenaConfiguration.Enums import ProductionStep
169 flags.Common.ProductionStep = ProductionStep.Generation
170
171 # Convert run arguments to global athena flags
172 from PyJobTransforms.CommonRunArgsToFlags import commonRunArgsToFlags
173 commonRunArgsToFlags(runArgs, flags)
174
175 # Convert generator-specific run arguments to global athena flags
176 from GeneratorConfig.GeneratorConfigFlags import generatorRunArgsToFlags
177 generatorRunArgsToFlags(runArgs, flags)
178
179 # convert arguments to flags
180 flags.fillFromArgs()
181
182 # Determine maximum number of events to generate
183 requested_max_events = flags.Exec.MaxEvents
184
185 # Create an instance of the Sample(EvgenCAConfig) and update global flags accordingly
186 sample = setupSample(flags)
187
188 # Determine output file name and type.
189 output_pool_file = (
190 flags.Output.EVNTFileName
191 or getattr(runArgs, "outputEVNTFile", None)
192 or getattr(runArgs, "outputEVNT_PreFile", None)
193 )
194 flags.Output.EVNTFileName = output_pool_file or ""
195 output_txt_file = (
196 flags.Output.TXTFileName
197 or getattr(runArgs, "outputTXTFile", None)
198 )
199 flags.Output.TXTFileName = output_txt_file or ""
200
201 # If no EVNT output is specified, we check if it's a TXT-only run (i.e. standalone LHE output production).
202 # In that case, we don't require an EVNT output file.
203 txt_only_mode = _is_txt_only_run(flags)
204 if not output_pool_file and not (flags.Generator.outputYODAFile or txt_only_mode):
205 raise RuntimeError("No output evgen EVNT or EVNT_Pre file provided.")
206
207 # Setup the main flags
208 flags.Exec.FirstEvent = flags.Generator.firstEvent
209
210 # If no inputEVNT_PreFile was provided clear transform placeholder input files
211 # and set RunNumber/TimeStamp based on DSID.
212 if hasattr(runArgs, "inputEVNT_PreFile") and runArgs.inputEVNT_PreFile:
213 flags.Input.Files = runArgs.inputEVNT_PreFile
214 else:
215 flags.Input.Files = []
216
217 if not flags.Input.Files:
218 flags.Input.Files = []
219 flags.Input.RunNumbers = [flags.Generator.DSID]
220 flags.Input.TimeStamps = [0]
221
222 flags.PerfMon.doFastMonMT = True
223 flags.PerfMon.doFullMonMT = True
224
225 # Process pre-include
226 processPreInclude(runArgs, flags)
227
228 # Process pre-exec
229 processPreExec(runArgs, flags)
230
231 # Lock flags
232 flags.lock()
233
234 evgenLog.info("**** Configuration flags")
235 if runArgs.VERBOSE:
236 flags.dump()
237 else:
238 flags.dump("Generator.*")
239
240 # Print various stuff
241 evgenLog.info(".transform = Gen_tf")
242 evgenLog.info(".platform = " + str(os.environ["BINARY_TAG"]))
243
244 # Announce start of job configuration
245 evgenLog.info("**** Configuring event generation")
246
247 # Main object
248 from AthenaConfiguration.MainServicesConfig import MainEvgenServicesCfg
249 cfg = MainEvgenServicesCfg(flags, withSequences=True)
250
251 # Input file handling (if needed)
252 if flags.Input.Files and not txt_only_mode:
253 from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
254 cfg.merge(PoolReadCfg(flags))
255
256 # EventInfoCnvAlg
257 from xAODEventInfoCnv.xAODEventInfoCnvConfig import EventInfoCnvAlgCfg
258 cfg.merge(EventInfoCnvAlgCfg(flags, disableBeamSpot=True, xAODKey="TMPEvtInfo"),
259 sequenceName=EvgenSequence.Generator.value)
260
261 # Set up the process
262 cfg.merge(sample.setupProcess(flags))
263
264 # Sort the list of generator names into standard form
265 from GeneratorConfig.GenConfigHelpers import gen_sortkey
266 from GeneratorConfig.Versioning import generatorsGetInitialVersionedDictionary, generatorsVersionedStringList
267 generators = sorted(cfg.getService("GeneratorInfoSvc").Generators, key=gen_sortkey)
268 gendict = generatorsGetInitialVersionedDictionary(generators)
269 generatorsWithVersion = generatorsVersionedStringList(gendict)
270
271 # Check if the setup requires steering
272 from GeneratorConfig.GenConfigHelpers import gen_require_steering
273 if gen_require_steering(generators):
274 if hasattr(runArgs, "outputEVNTFile") and not hasattr(runArgs, "outputEVNT_PreFile"):
275 raise RuntimeError("'EvtGen' found in job options name, please set '--steering=afterburn'")
276
277 # LHE input handling
278 nEventsLHE = None
279 if flags.Generator.inputFilesPerJob > 0:
280 if not flags.Generator.inputGeneratorFile:
281 raise RuntimeError(f"Sample sets inputFilesPerJob = {flags.Generator.inputFilesPerJob} but Gen_tf run without inputGeneratorFile")
282 else:
283 nEventsLHE = _handle_input_files(generators, flags)
284
285 # Check black-list and purple-list
286 blError = checkBlackList(athenaRel, generators, "black")
287 plError = checkBlackList(athenaRel, generators, "purple")
288 if blError is not None:
289 raise RuntimeError(blError)
290 if plError is not None:
291 evgenLog.warning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
292 evgenLog.warning(f"!!! WARNING {plError} !!!")
293 evgenLog.warning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
294
295 # Fix non-standard event features
296 if not txt_only_mode and not flags.Input.Files:
297 from EvgenProdTools.EvgenProdToolsConfig import FixHepMCCfg
298 from GeneratorConfig.GenConfigHelpers import gens_purgenoendvtx
299 generatorsList = generators.copy()
300 if "Pythia8" in generatorsList:
301 pythia8Alg = cfg.getEventAlgo("Pythia8_i")
302 if pythia8Alg.Beam1 != "PROTON" or pythia8Alg.Beam2 != "PROTON":
303 # generator name is still "Pythia8", even when colliding nuclei
304 generatorsList.append("Pythia8-Angantyr")
305 cfg.merge(FixHepMCCfg(flags,
306 PurgeUnstableWithoutEndVtx=gens_purgenoendvtx(generatorsList)))
307
308 # Sanity check the event record (not appropriate for all generators)
309 from GeneratorConfig.GenConfigHelpers import gens_testhepmc
310 if not txt_only_mode and gens_testhepmc(generators):
311 from EvgenProdTools.EvgenProdToolsConfig import TestHepMCCfg
312 cfg.merge(TestHepMCCfg(flags))
313
314 # Copying event-level HepMC decorations is EVNT-oriented and not needed for
315 # standalone LHE output production.
316 if not txt_only_mode:
317 from EvgenProdTools.EvgenProdToolsConfig import CopyEventWeightCfg
318 cfg.merge(CopyEventWeightCfg(flags))
319
320 from EvgenProdTools.EvgenProdToolsConfig import FillFilterValuesCfg
321 cfg.merge(FillFilterValuesCfg(flags))
322
323 # Configure the event counting (AFTER all filters)
324 from EvgenProdTools.EvgenProdToolsConfig import CountHepMCCfg
325 requested_output = (
326 1 if txt_only_mode else
327 (sample.nEventsPerJob if requested_max_events == -1 else requested_max_events)
328 )
329 count_kwargs = {"RequestedOutput": requested_output}
330 if txt_only_mode:
331 # In TXT-only mode there is no GEN_EVENT in StoreGate. Disabling
332 # HepMC/EventInfo corrections avoids dereferencing missing event data.
333 count_kwargs["CorrectHepMC"] = False
334 count_kwargs["CorrectEventID"] = False
335 count_kwargs["CorrectRunNumber"] = False
336 count_kwargs["CopyRunNumber"] = False
337 count_kwargs["InputEventInfo"] = ""
338 count_kwargs["OutputEventInfo"] = ""
339 count_kwargs["mcEventWeightsKey"] = ""
340 cfg.merge(CountHepMCCfg(flags, **count_kwargs))
341 evgenLog.info(f"Requested output events = {cfg.getEventAlgo('CountHepMC').RequestedOutput}")
342
343 # Print out the contents of the first 5 events (after filtering)
344 if not txt_only_mode and flags.Generator.printEvts > 0:
345 from TruthIO.TruthIOConfig import PrintMCCfg
346 cfg.merge(PrintMCCfg(flags,
347 LastEvent=flags.Generator.printEvts))
348
349 # PerfMon
350 from PerfMonComps.PerfMonCompsConfig import PerfMonMTSvcCfg
351 cfg.merge(PerfMonMTSvcCfg(flags), sequenceName=EvgenSequence.Post.value)
352
353 # Estimate time needed for Simulation
354 if not txt_only_mode:
355 from EvgenProdTools.EvgenProdToolsConfig import SimTimeEstimateCfg
356 cfg.merge(SimTimeEstimateCfg(flags))
357
358 # TODO: Rivet
359
360 # Extra metadata
361 from EventInfoMgt.TagInfoMgrConfig import TagInfoMgrCfg
362 from GeneratorConfig.GenConfigHelpers import gen_lhef
363 metadata = {
364 "project_name": "IS_SIMULATION",
365 f"AtlasRelease_{runArgs.trfSubstepName}": flags.Input.Release or "n/a",
366 "beam_energy": str(int(flags.Beam.Energy)),
367 "beam_type": flags.Beam.Type.value,
368 "generators": '+'.join(generatorsWithVersion),
369 "tune": cfg.getService("GeneratorInfoSvc").Tune,
370 "hepmc_version": f"HepMC{os.environ['HEPMCVER']}",
371 "keywords": ", ".join(sample.keywords).lower(),
372 "lhefGenerator": '+'.join(filter(gen_lhef, generators)),
373 "mc_channel_number": str(flags.Generator.DSID),
374 }
375 if hasattr(sample, "process"): metadata.update({"evgenProcess": sample.process})
376 if hasattr(sample, "specialConfig"): metadata.update({"specialConfiguration": sample.specialConfig})
377 if hasattr(sample, "hardPDF"): metadata.update({"hardPDF": sample.hardPDF})
378 if hasattr(sample, "softPDF"): metadata.update({"softPDF": sample.softPDF})
379 if hasattr(sample, "randomSeed"): metadata.update({"randomSeed": str(flags.Random.SeedOffset)})
380 cfg.merge(TagInfoMgrCfg(flags, tagValuePairs=metadata))
381
382 # Print metadata in the log
383 evgenLog.info(f"HepMC version {os.environ['HEPMCVER']}")
384 evgenLog.info(f"MetaData: generatorTune = {cfg.getService('GeneratorInfoSvc').Tune}")
385 evgenLog.info("MetaData: generatorName = {}".format(generatorsWithVersion))
386 if nEventsLHE is not None:
387 print(f"MetaData: Number of input LHE events = {nEventsLHE}")
388 elif txt_only_mode:
389 produced_lhe = None
390 for candidate in (flags.Output.TXTFileName, "events.lhe"):
391 if candidate and os.path.exists(candidate):
392 produced_lhe = candidate
393 break
394 if produced_lhe:
395 nEventsTXT = _count_lhe_events(produced_lhe)
396 print(f"MetaData: Number of produced LHE events = {nEventsTXT}")
397
398 if output_pool_file:
399 # Configure output stream
400 from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg
401 cfg.merge(OutputStreamCfg(flags, "EVNT", ["McEventCollection#*"]))
402
403 # Add in-file MetaData
404 from xAODMetaDataCnv.InfileMetaDataConfig import SetupMetaDataForStreamCfg
405 cfg.merge(SetupMetaDataForStreamCfg(flags, "EVNT"))
406
407 # Post-include
408 processPostInclude(runArgs, flags, cfg)
409
410 # Post-exec
411 processPostExec(runArgs, flags, cfg)
412
413 # Write AMI tag into in-file MetaData
414 from PyUtils.AMITagHelperConfig import AMITagCfg
415 cfg.merge(AMITagCfg(flags, runArgs))
416
417 # Print ComponentAccumulator components
418 cfg.printConfig(prefix="Gen_tf", printSequenceTreeOnly=not runArgs.VERBOSE)
419
420 # Run final ComponentAccumulator
421 sys.exit(not cfg.run().isSuccess())
void print(char *figname, TCanvas *c1)
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179
checkBlackList(cache, generatorName, checkType)