6 from os.path
import isfile
7 from optparse
import OptionParser
8 from random
import randint
11 usage =
"usage: %prog [options] input1 [input2...]"
13 parser = OptionParser(usage=usage, version=
"%prog v0.0.1 $Id: LArG4FSStartPointFilter.py 711210 2015-11-27 15:56:00Z jchapman $")
15 parser.add_option(
"-p",
"--particle", dest=
"part", type=
"choice", action=
"append", choices=[
"11",
"22",
"2112"], help=
"particle to be filtered out (default - no filter)")
16 parser.add_option(
"-t",
"--truncate", dest=
"numevents", type=int, help=
"Truncate the number of events (default - all)")
17 parser.add_option(
"-l",
"--outevents", dest=
"outevents", type=int, help=
"Truncate the number of output (default - all)")
18 parser.add_option(
"-o",
"--output", dest=
"outfile", help=
"Name of output file")
20 parser.set_defaults(part=[],outfile=
"genevents.ascii",numevents=0,outevents=0,draw=
False,execute=
False)
22 (options, args) = parser.parse_args()
27 print (
"ERROR: No input! Aborting")
35 for infilename
in args :
37 print (
"Opening file",infilename)
40 linever = infile.readline().rstrip(
"\n")
42 linever = infile.readline().rstrip(
"\n")
44 if (globallinever ==
"") :
45 globallinever = linever
46 linehead = infile.readline().rstrip(
"\n")
48 if (globallinehead ==
"") :
49 globallinehead = linehead
51 if ((
not linever.startswith(
"HepMC::Version"))
or (
"START_EVENT_LISTING" not in linehead)
or (linever != globallinever)) :
52 print (
"ERROR: Wrong input:",infilename,
"omitting", file=sys.stderr)
56 line = infile.readline()
57 while ((line !=
"")
and (
not line.startswith(
"HepMC::" ))) :
58 line1 = infile.readline()
59 line2 = infile.readline()
60 line3 = infile.readline()
61 line4 = infile.readline()
63 stpoint = [line,line1,line2,line3]
64 if "Ascii" in globallinehead:
65 if (line[0] !=
"E")
or (line1[0] !=
"U")
or (line2[0] !=
"P")
or (line3[0] !=
"V")
or (line4[0] !=
"P"):
66 print (
"ERROR:",infilename,
"is not a valid file!")
71 if (line[0] !=
"E")
or (line1[0] !=
"U")
or (line2[0] !=
"V")
or (line3[0] !=
"P") :
75 print (
"ERROR:",infilename,
"is not a valid file!")
87 stpointsloc.append(stpoint)
91 line = infile.readline()
94 stpoints += stpointsloc
95 if (
"tfile" in dir()) :
99 if len(stpoints) == 0 :
100 print (
"ERROR: no events found is not a valid file!")
104 if isfile(options.outfile) :
105 print (
"WARNING: File",options.outfile,
"already exists.")
106 outdata =
open(options.outfile,
"w")
107 outdata.write(globallinever)
109 outdata.write(globallinehead)
112 stpsize = len(stpoints)
113 if (options.numevents > stpsize) :
114 print (
"WARNING: requested number of events is bigger then provided in input files")
115 options.numevents = 0
117 if (options.numevents == 0) :
118 options.numevents = stpsize
121 while i < options.numevents :
123 print (
"INFO: We've run out of starting point.\nIt's okay if you didn't specify -t option, but may mean that you do not have enough otherwise.")
125 rand = randint(0,stpsize-1)
126 stpoint = stpoints.pop(rand)
130 if "Ascii" in globallinehead:
132 if (options.part == [])
or (pid_filt
in options.part) :
133 parsed = stpoint[0].
split()
135 stpoint[0] =
" ".
join(parsed)
137 parsed = stpoint[2].
split()
143 if len(options.outfile) > 0 :
144 for iline
in stpoint:
145 outdata.write(iline.rstrip(
"\n")+
"\n")
148 outdata.write(globallinehead.replace(
"START",
"END"))
150 print (
"INFO: Written", i, options.numevents,
"starting points")
152 if (options.outevents > i) :
153 print (
"WARNING: requested number of events is bigger then provided in input files")
154 options.outevents = 0
156 if (options.outevents == 0) :
157 options.outevents = i
160 exec = __file__.replace(
"LArG4FSStartPointFilterLegacy.py",
"LArG4FSStartPointFilterBody.py")
162 os.system(
'athena -c "options={:s}" {:s}'.
format(
str(options),exec))