ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RWTH3b/python/splitLHE.py
Revision: 1.2
Committed: Fri May 25 11:53:19 2012 UTC (12 years, 11 months ago) by htholen
Content type: text/x-python
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +2 -2 lines
Error occurred while calculating annotation data.
Log Message:
Added nice Usage string

File Contents

# Content
1 import re,getopt,sys
2 opts, args = getopt.getopt(sys.argv[1:], '',['inputFile=','steps=','outputFile=',"maxEvents="])
3 inputFile=None
4 steps=-1
5 outputFile=None
6 maxEvents=-1
7 for opt,arg in opts:
8 #print opt , " : " , arg
9 if opt in ("--inputFile"):
10 inputFile=arg
11 if opt in ("--steps"):
12 steps=int(arg)
13 if opt in ("--outputFile"):
14 outputFile=arg
15 if opt in ("--maxEvents"):
16 maxEvents=int(arg)
17 if inputFile == None or steps==-1:
18 sys.exit("Usage: python splitLHE.py --inputFile=<file> --outputFile=<file without postfix> --steps=<num> --maxEvents=<num>")
19 if outputFile==None:
20 outputFile=re.match('(.*)\.lhe',str(inputFile)).group(1)+"_"
21 def events(filename):
22 eventblock = False
23 content = ""
24 for line in open(filename):
25 if "<event>" in line:
26 eventblock = True
27 content = ""
28 elif "</event>" in line:
29 eventblock = False
30 content += line
31 yield content
32 if eventblock:
33 content += line
34 raise StopIteration
35 def getHeader(filename):
36 content=""
37 for line in open(filename):
38 #print line
39 if "<event>" in line:
40 return content
41 else:
42 content += line
43
44 header = getHeader(inputFile)
45 #
46 content=''
47 numfile=0
48 for i, event in enumerate(events(inputFile)):
49 if i == int(maxEvents):
50 break
51 if i != 0 and i%steps == 0:
52 print "writing file ",outputFile+str(numfile)+".lhe ..."
53 outfile=open(outputFile+str(numfile)+".lhe","w")
54 outfile.write(header)
55 outfile.write(content)
56 outfile.write("</LesHouchesEvents>")
57 outfile.close()
58 content=''
59 numfile+=1
60 print "done."
61 content+=event
62 print "finished, created ",numfile+1," files, each contains ",steps," events"