-
Notifications
You must be signed in to change notification settings - Fork 1
/
mergeLHE.py
45 lines (40 loc) · 1.6 KB
/
mergeLHE.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import glob
from optparse import OptionParser
from datetime import datetime
# TODO: add the capabality to choose between
# globbing all lhe files
# inputting specific files by hand
# add the capability to name output file
help_text = """python mergeLHE.py -s /path/to/save/dir"""
parser = OptionParser(usage=help_text)
parser.add_option("-s", "--save_loc", type="string", dest="save_loc", default=".", help="Sets the location of the output file.")
(options, args) = parser.parse_args()
#filenames = glob.glob('*.events')
filenames = glob.glob('*.lhe')
startTime = datetime.now()
save_location = options.save_loc
with open(save_location+'/outfile.lhe', 'w') as outfile:
numEvents = 0
outfile.write("""<LesHouchesEvents version="1.0">\n""")
with open(filenames[0]) as toGetHeader:
writeHeader = False
for line in toGetHeader:
if "<!--" in line: writeHeader = True
if writeHeader: outfile.write(line)
if "</init>" in line:
break
for fname in filenames:
writeLine = False
stopWrite = False
with open(fname) as infile:
for line in infile:
if "<event>" in line:
numEvents += 1
writeLine = True
if "</LesHouchesEvents>" in line:
stopWrite = True
if writeLine and not stopWrite:
outfile.write(line)
outfile.write("</LesHouchesEvents>")
print "There were {0} events processed in {1} files.".format(numEvents, len(filenames))
print(datetime.now()-startTime)