6 from os.path
import isfile
7 from optparse
import OptionParser
8 from random
import randint
10 from pyHepMC3
import HepMC3
as HepMC
12 usage =
"usage: %prog [options] input1 [input2...]"
14 parser = OptionParser(usage=usage, version=
"%prog v0.0.1 $Id: LArG4FSStartPointFilter.py 711210 2015-11-27 15:56:00Z jchapman $")
16 parser.add_option(
"-p",
"--particle", dest=
"part", type=
"choice", action=
"append", choices=[
"11",
"22",
"2112"], help=
"particle to be filtered out (default - no filter)")
17 parser.add_option(
"-t",
"--truncate", dest=
"numevents", type=int, help=
"Truncate the number of events (default - all)")
18 parser.add_option(
"-l",
"--outevents", dest=
"outevents", type=int, help=
"Truncate the number of output (default - all)")
19 parser.add_option(
"-o",
"--output", dest=
"outfile", help=
"Name of output file")
21 parser.set_defaults(part=[],outfile=
"genevents.ascii",numevents=0,outevents=0)
23 (options, args) = parser.parse_args()
28 print (
"ERROR: No input! Aborting")
33 for infilename
in args :
35 infile = HepMC.ReaderAscii(infilename)
38 print(
"Wrong input. Exit.\n")
41 while not infile.failed():
42 evt = HepMC.GenEvent()
43 infile.read_event(evt)
46 print(
"End of file reached.\n")
53 if len(stpoints) == 0 :
54 print (
"ERROR: no events found is not a valid file!")
59 if isfile(options.outfile) :
60 print (
"WARNING: File",options.outfile,
"already exists.")
62 outdata = HepMC.WriterAscii(options.outfile)
64 print(
"Bad output. Exit.\n")
67 stpsize = len(stpoints)
68 if (options.numevents > stpsize) :
69 print (
"WARNING: requested number of events is bigger then provided in input files")
72 if (options.numevents == 0) :
73 options.numevents = stpsize
76 while i < options.numevents :
78 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.")
80 rand = randint(0,stpsize-1)
81 stpoint = stpoints.pop(rand)
84 particles = stpoint.particles()
85 pid_filt =
str(particles[-1].pdg_id())
87 if (options.part == [])
or (pid_filt
in options.part) :
88 stpoint.set_event_number(i)
89 outdata.write_event(stpoint)
93 print (
"INFO: Written", i, options.numevents,
"starting points")
95 if (options.outevents > i) :
96 print (
"WARNING: requested number of events is bigger then provided in input files")
99 if (options.outevents == 0) :
100 options.outevents = i
103 exec = __file__.replace(
"LArG4FSStartPointFilter.py",
"LArG4FSStartPointFilterBody.py")
105 os.system(
'athena -c "options={:s}" {:s}'.
format(
str(options),exec))