ATLAS Offline Software
POOLtoHEPMC_Skeleton.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 # Job transform version of converting an EVNT file into a HEPMC file
3 
4 # For the exit code at the end
5 import sys
6 import re
7 
8 # For translating the run arguments into flags
9 from PyJobTransforms.CommonRunArgsToFlags import commonRunArgsToFlags
10 
11 # For using pre include, pre-exec, etc; should only rarely be needed
12 from PyJobTransforms.TransformUtils import processPreExec, processPreInclude, processPostExec, processPostInclude
13 
14 # Force no legacy job properties
15 from AthenaCommon import JobProperties
16 JobProperties.jobPropertiesDisallowed = True
17 
18 def fromRunArgs(runArgs):
19  # Start the logger and identify ourselves
20  from AthenaCommon.Logging import logging
21  log = logging.getLogger('POOLtoHEPMC')
22  log.info('*** Starting POOLtoHEPMC translation ***')
23 
24  # Print some job information
25  log.info('*** Transformation run arguments ***')
26  log.info(str(runArgs))
27 
28  # Set up the flags we need
29  log.info('*** Setting-up configuration flags ***')
30  from AthenaConfiguration.AllConfigFlags import initConfigFlags
31  flags = initConfigFlags()
32  commonRunArgsToFlags(runArgs, flags)
33 
34  # Set ProductionStep
35  from AthenaConfiguration.Enums import ProductionStep
36  flags.Common.ProductionStep = ProductionStep.Derivation
37 
38  # Set the input file and configure the input collection key
39  if hasattr(runArgs, 'inputEVNTFile'):
40  flags.Input.Files = runArgs.inputEVNTFile
41  McEventKey = 'GEN_EVENT'
42  elif hasattr(runArgs, 'inputHITSFile'):
43  flags.Input.Files = runArgs.inputHITSFile
44  McEventKey = 'TruthEvent'
45  elif hasattr(runArgs, 'inputRDOFile'):
46  flags.Input.Files = runArgs.inputRDOFile
47  McEventKey = 'TruthEvent'
48  else:
49  log.error('Input EVNT, HITS, or RDO file required for POOLtoHEPMC')
50  # Set the output file
51  if hasattr(runArgs, 'outputHEPMCFile'):
52  if ('.tar' in runArgs.outputHEPMCFile):
53  index = re.search(".tar",runArgs.outputHEPMCFile).span()[0]
54  if hasattr(runArgs, 'extension') and ('events' in runArgs.extension):
55  my_output_HepMCFile = runArgs.outputHEPMCFile[:index]+'.events'
56  else:
57  my_output_HepMCFile = runArgs.outputHEPMCFile[:index]+'.hepmc'
58  else:
59  log.error('Output should be a tar.gz file but it is '+runArgs.outputHEPMCFile)
60  else:
61  log.error('OutputHEPMCFile required for POOLtoHEPMC')
62 
63  hepMCFormat = 'hepmc2'
64  if hasattr(runArgs, 'hepmcFormat'):
65  hepMCFormat = runArgs.hepmcFormat
66 
67  # Setup perfmon flags from runargs
68  from PerfMonComps.PerfMonConfigHelpers import setPerfmonFlagsFromRunArgs
69  setPerfmonFlagsFromRunArgs(flags, runArgs)
70 
71  # Pre-include
72  processPreInclude(runArgs, flags)
73 
74  # Pre-exec
75  processPreExec(runArgs, flags)
76 
77  # To respect --athenaopts
78  flags.fillFromArgs()
79 
80  # Lock flags
81  flags.lock()
82 
83  # Do the configuration of the main services
84  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
85  cfg = MainServicesCfg(flags)
86 
87  # Set us up for reading a POOL file
88  from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
89  cfg.merge(PoolReadCfg(flags))
90 
91  # We need the component factory to build the job up
92  from AthenaConfiguration.ComponentFactory import CompFactory
93 
94  # Add FixHepMC to remove loops here
95  # This is a work-around for AGENE-2342, which needs a HepMC patch to fix
96  cfg.addEventAlgo(CompFactory.FixHepMC("FixHepMC"))
97 
98  # Use the WriteHepMC AlgTool from TruthIO to do the conversion
99  cfg.addEventAlgo( CompFactory.WriteHepMC( 'WriteHepMC',
100  OutputFile = my_output_HepMCFile,
101  Format = hepMCFormat,
102  McEventKey = McEventKey ) )
103  # Here one should set the output format
104  # Post-include
105  processPostInclude(runArgs, flags, cfg)
106 
107  # Post-exec
108  processPostExec(runArgs, flags, cfg)
109 
110  import time
111  tic = time.time()
112 
113  # Run the final accumulator
114  sc = cfg.run()
115 
116  # Compress the output file
117  log.info('Compressing HEPMC output (may take a moment)')
118  import tarfile
119  with tarfile.open(runArgs.outputHEPMCFile,'w:gz') as out_tar:
120  out_tar.add( my_output_HepMCFile )
121 
122  # And remove the uncompressed version
123  log.debug('Deleting original (uncompressed) file')
124  import os
125  os.remove( my_output_HepMCFile )
126 
127  # All done, now just report back
128  log.info("Ran POOLtoHEPMC in " + str(time.time()-tic) + " seconds")
129 
130  sys.exit(not sc.isSuccess())
131 
CxxUtils::span
span(T *ptr, std::size_t sz) -> span< T >
A couple needed deduction guides.
python.TransformUtils.processPreExec
def processPreExec(runArgs, flags)
Definition: Tools/PyJobTransforms/python/TransformUtils.py:41
python.TransformUtils.processPostExec
def processPostExec(runArgs, flags, cfg)
Definition: Tools/PyJobTransforms/python/TransformUtils.py:50
python.POOLtoHEPMC_Skeleton.fromRunArgs
def fromRunArgs(runArgs)
Definition: POOLtoHEPMC_Skeleton.py:18
python.TransformUtils.processPostInclude
def processPostInclude(runArgs, flags, cfg)
Definition: Tools/PyJobTransforms/python/TransformUtils.py:69
python.TransformUtils.processPreInclude
def processPreInclude(runArgs, flags)
Definition: Tools/PyJobTransforms/python/TransformUtils.py:62
python.PerfMonConfigHelpers.setPerfmonFlagsFromRunArgs
def setPerfmonFlagsFromRunArgs(flags, runArgs)
Definition: PerfMonConfigHelpers.py:3
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:312
python.CommonRunArgsToFlags.commonRunArgsToFlags
def commonRunArgsToFlags(runArgs, configFlags)
Definition: CommonRunArgsToFlags.py:12
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
str
Definition: BTagTrackIpAccessor.cxx:11
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:71