ATLAS Offline Software
Loading...
Searching...
No Matches
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)
10from AthenaConfiguration.AllConfigFlags import initConfigFlags
11flags = initConfigFlags()
12flags.addFlag('Output.HepMCFileName','events.hepmc',help='Name of the output HepMC file; files with .tgz, .gz, or .tar.gz extensions will be compressed')
13flags.fillFromArgs()
14flags.lock()
15
16from AthenaConfiguration.MainServicesConfig import MainServicesCfg
17from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
18cfg = MainServicesCfg(flags)
19cfg.merge(PoolReadCfg(flags))
20
21McEventKey = 'GEN_EVENT'
22if '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
29from 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
33cfg.addEventAlgo(CompFactory.FixHepMC("FixHepMC"))
34# For events with loops, you may still need to add the option SetHasCycles=True
35
36# Get the name of the uncompressed events file that we will write
37events_filename = flags.Output.HepMCFileName.replace('.tgz','').replace('.tar','').replace('.gz','')
38
39# Use the WriteHepMC AlgTool from TruthIO to do the conversion
40cfg.addEventAlgo( CompFactory.WriteHepMC( 'WriteHepMC',
41 OutputFile = events_filename,
42 McEventKey = McEventKey ) )
43cfg.run(flags.Exec.MaxEvents)
44
45# Check based on the file name if we need to compress the output
46if '.tgz' in flags.Output.HepMCFileName or '.tar.gz' in flags.Output.HepMCFileName:
47 print('Compressing output into tar+gz format (this may take a moment)')
48 import tarfile
49 tar = tarfile.open(flags.Output.HepMCFileName,'w:gz')
50 tar.add(events_filename)
51 tar.close()
52 # Remove the original uncompressed file
53 import os
54 os.remove(events_filename)
55elif '.gz' in flags.Output.HepMCFileName:
56 print('Compressing output into gz format (this may take a moment)')
57 import gzip
58 import shutil
59 with open(events_filename,'rb') as in_file, gzip.open(flags.Output.HepMCFileName,'wb') as out_file:
60 shutil.copyfileobj(in_file,out_file)
61 # Remove the original uncompressed file
62 import os
63 os.remove(events_filename)
void print(char *figname, TCanvas *c1)
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:310