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 
7 # For translating the run arguments into flags
8 from PyJobTransforms.CommonRunArgsToFlags import commonRunArgsToFlags
9 
10 # For using pre include, pre-exec, etc; should only rarely be needed
11 from PyJobTransforms.TransformUtils import processPreExec, processPreInclude, processPostExec, processPostInclude
12 
13 # Force no legacy job properties
14 from AthenaCommon import JobProperties
15 JobProperties.jobPropertiesDisallowed = True
16 
17 def fromRunArgs(runArgs):
18  # Start the logger and identify ourselves
19  from AthenaCommon.Logging import logging
20  log = logging.getLogger('POOLtoHEPMC')
21  log.info('*** Starting POOLtoHEPMC translation ***')
22 
23  # Print some job information
24  log.info('*** Transformation run arguments ***')
25  log.info(str(runArgs))
26 
27  # Set up the flags we need
28  log.info('*** Setting-up configuration flags ***')
29  from AthenaConfiguration.AllConfigFlags import initConfigFlags
30  flags = initConfigFlags()
31  commonRunArgsToFlags(runArgs, flags)
32 
33  # Set ProductionStep
34  from AthenaConfiguration.Enums import ProductionStep
35  flags.Common.ProductionStep = ProductionStep.Derivation
36 
37  # Set the input file and configure the input collection key
38  if hasattr(runArgs, 'inputEVNTFile'):
39  flags.Input.Files = runArgs.inputEVNTFile
40  McEventKey = 'GEN_EVENT'
41  elif hasattr(runArgs, 'inputHITSFile'):
42  flags.Input.Files = runArgs.inputHITSFile
43  McEventKey = 'TruthEvent'
44  elif hasattr(runArgs, 'inputRDOFile'):
45  flags.Input.Files = runArgs.inputRDOFile
46  McEventKey = 'TruthEvent'
47  else:
48  log.error('Input EVNT, HITS, or RDO file required for POOLtoHEPMC')
49 
50  # Set the output file
51  if hasattr(runArgs, 'outputHEPMCFile'):
52  if '.tar' in runArgs.outputHEPMCFile or '.tgz' in runArgs.outputHEPMCFile or '.gz' in runArgs.outputHEPMCFile:
53  log.info('Output will be compressed')
54  if '.tar' in runArgs.outputHEPMCFile:
55  index = runArgs.outputHEPMCFile.find('.tar')
56  elif '.tgz' in runArgs.outputHEPMCFile:
57  index = runArgs.outputHEPMCFile.find('.tgz')
58  elif '.gz' in runArgs.outputHEPMCFile:
59  index = runArgs.outputHEPMCFile.find('.gz')
60  if hasattr(runArgs, 'extension') and 'events' in runArgs.extension:
61  my_output_HepMCFile = runArgs.outputHEPMCFile[:index]+'.events'
62  else:
63  my_output_HepMCFile = runArgs.outputHEPMCFile[:index]+'.hepmc'
64  else:
65  log.info('Output will not be compressed')
66  my_output_HepMCFile = runArgs.outputHEPMCFile
67  else:
68  log.error('OutputHEPMCFile required for POOLtoHEPMC')
69  raise RuntimeError('OutputHEPMCFile required for POOLtoHEPMC')
70 
71  hepMCFormat = 'hepmc2'
72  if hasattr(runArgs, 'hepmcFormat'):
73  hepMCFormat = runArgs.hepmcFormat
74 
75  hepMCUnits = 'GEVMM'
76  if hasattr(runArgs, 'hepmcUnits'):
77  hepMCUnits = runArgs.hepmcUnits
78 
79  # Setup perfmon flags from runargs
80  from PerfMonComps.PerfMonConfigHelpers import setPerfmonFlagsFromRunArgs
81  setPerfmonFlagsFromRunArgs(flags, runArgs)
82 
83  # Pre-include
84  processPreInclude(runArgs, flags)
85 
86  # Pre-exec
87  processPreExec(runArgs, flags)
88 
89  # To respect --athenaopts
90  flags.fillFromArgs()
91 
92  # Lock flags
93  flags.lock()
94 
95  # Do the configuration of the main services
96  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
97  cfg = MainServicesCfg(flags)
98 
99  # Set us up for reading a POOL file
100  from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
101  cfg.merge(PoolReadCfg(flags))
102 
103  # We need the component factory to build the job up
104  from AthenaConfiguration.ComponentFactory import CompFactory
105 
106  # Add FixHepMC to remove loops here
107  # This is a work-around for AGENE-2342, which needs a HepMC patch to fix
108  cfg.addEventAlgo(CompFactory.FixHepMC("FixHepMC"))
109 
110  # Use the WriteHepMC AlgTool from TruthIO to do the conversion
111  cfg.addEventAlgo( CompFactory.WriteHepMC( 'WriteHepMC',
112  OutputFile = my_output_HepMCFile,
113  Format = hepMCFormat,
114  Units = hepMCUnits,
115  McEventKey = McEventKey ) )
116  # Here one should set the output format
117  # Post-include
118  processPostInclude(runArgs, flags, cfg)
119 
120  # Post-exec
121  processPostExec(runArgs, flags, cfg)
122 
123  import time
124  tic = time.time()
125 
126  # Run the final accumulator
127  sc = cfg.run()
128 
129  # Check based on the file name if we need to compress the output
130  if '.tgz' in runArgs.outputHEPMCFile or '.tar.gz' in runArgs.outputHEPMCFile:
131  log.info('Compressing output into tar+gz format (this may take a moment)')
132  import tarfile
133  with tarfile.open(runArgs.outputHEPMCFile,'w:gz') as out_tar:
134  out_tar.add(my_output_HepMCFile)
135  # Remove the original uncompressed file
136  log.debug(f'Deleting original (uncompressed) file {my_output_HepMCFile}')
137  import os
138  os.remove(my_output_HepMCFile)
139  elif '.gz' in runArgs.outputHEPMCFile:
140  log.info('Compressing output into gz format (this may take a moment)')
141  import gzip
142  import shutil
143  with open(my_output_HepMCFile,'rb') as in_file, gzip.open(runArgs.outputHEPMCFile,'wb') as out_file:
144  shutil.copyfileobj(in_file,out_file)
145  # Remove the original uncompressed file
146  log.debug(f'Deleting original (uncompressed) file {my_output_HepMCFile}')
147  import os
148  os.remove(my_output_HepMCFile)
149 
150  # All done, now just report back
151  log.info("Ran POOLtoHEPMC in " + str(time.time()-tic) + " seconds")
152 
153  sys.exit(not sc.isSuccess())
154 
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:17
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:310
python.CommonRunArgsToFlags.commonRunArgsToFlags
def commonRunArgsToFlags(runArgs, configFlags)
Definition: CommonRunArgsToFlags.py:12
Trk::open
@ open
Definition: BinningType.h:40
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
str
Definition: BTagTrackIpAccessor.cxx:11
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:71