ATLAS Offline Software
POOLtoHEPMC.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 # Simple script for converting an EVNT, HITS, or RDO file into a HEPMC file
5 
6 # Example execution:
7 # POOLtoHEPMC.py --filesInput=EVNT.34993204._006761.pool.root.1 Output.HepMCFileName=output.hepmc
8 
9 # Options: input and output file, and compression (tgz)
10 from AthenaConfiguration.AllConfigFlags import initConfigFlags
11 flags = initConfigFlags()
12 flags.addFlag('Output.HepMCFileName','events.hepmc',help='Name of the output HepMC file; files with .tgz, .gz, or .tar.gz extensions will be compressed')
13 flags.fillFromArgs()
14 flags.lock()
15 
16 from AthenaConfiguration.MainServicesConfig import MainServicesCfg
17 from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
18 cfg = MainServicesCfg(flags)
19 cfg.merge(PoolReadCfg(flags))
20 
21 McEventKey = 'GEN_EVENT'
22 if 'McEventCollection#GEN_EVENT' not in flags.Input.TypedCollections:
23  if 'McEventCollection#TruthEvent' in flags.Input.TypedCollections:
24  McEventKey = 'TruthEvent'
25  else:
26  print('Truth collection not found in input file. Might be a problem.')
27 
28 # We need the component factory to build the job up
29 from AthenaConfiguration.ComponentFactory import CompFactory
30 
31 # Add FixHepMC to remove loops here
32 # This is a work-around for AGENE-2342, which needs a HepMC patch to fix
33 cfg.addEventAlgo(CompFactory.FixHepMC("FixHepMC"))
34 
35 # Get the name of the uncompressed events file that we will write
36 events_filename = flags.Output.HepMCFileName.replace('.tgz','').replace('.tar','').replace('.gz','')
37 
38 # Use the WriteHepMC AlgTool from TruthIO to do the conversion
39 cfg.addEventAlgo( CompFactory.WriteHepMC( 'WriteHepMC',
40  OutputFile = events_filename,
41  McEventKey = McEventKey ) )
42 cfg.run(flags.Exec.MaxEvents)
43 
44 # Check based on the file name if we need to compress the output
45 if '.tgz' in flags.Output.HepMCFileName or '.tar.gz' in flags.Output.HepMCFileName:
46  print('Compressing output into tar+gz format (this may take a moment)')
47  import tarfile
48  tar = tarfile.open(flags.Output.HepMCFileName,'w:gz')
49  tar.add(events_filename)
50  tar.close()
51  # Remove the original uncompressed file
52  import os
53  os.remove(events_filename)
54 elif '.gz' in flags.Output.HepMCFileName:
55  print('Compressing output into gz format (this may take a moment)')
56  import gzip
57  import shutil
58  with open(events_filename,'rb') as in_file, gzip.open(flags.Output.HepMCFileName,'wb') as out_file:
59  shutil.copyfileobj(in_file,out_file)
60  # Remove the original uncompressed file
61  import os
62  os.remove(events_filename)
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:310
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:310
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:26
Trk::open
@ open
Definition: BinningType.h:40
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
python.PoolReadConfig.PoolReadCfg
def PoolReadCfg(flags)
Definition: PoolReadConfig.py:71