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
35# Get the name of the uncompressed events file that we will write
36events_filename = flags.Output.HepMCFileName.replace('.tgz','').replace('.tar','').replace('.gz','')
37
38# Use the WriteHepMC AlgTool from TruthIO to do the conversion
39cfg.addEventAlgo( CompFactory.WriteHepMC( 'WriteHepMC',
40 OutputFile = events_filename,
41 McEventKey = McEventKey ) )
42cfg.run(flags.Exec.MaxEvents)
43
44# Check based on the file name if we need to compress the output
45if '.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)
54elif '.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)
void print(char *figname, TCanvas *c1)
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:310