ATLAS Offline Software
Loading...
Searching...
No Matches
LArG4FSStartPointFilter.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3
4import sys
5import os
6from os.path import isfile
7from optparse import OptionParser
8from random import randint
9from math import sqrt
10from pyHepMC3 import HepMC3 as HepMC
11
12usage = "usage: %prog [options] input1 [input2...]"
13
14parser = OptionParser(usage=usage, version="%prog v0.0.1 $Id: LArG4FSStartPointFilter.py 711210 2015-11-27 15:56:00Z jchapman $")
15
16parser.add_option("-p","--particle", dest="part", type="choice", action="append", choices=["11","22","2112"], help="particle to be filtered out (default - no filter)")
17parser.add_option("-t","--truncate", dest="numevents", type=int, help="Truncate the number of events (default - all)")
18parser.add_option("-l","--outevents", dest="outevents", type=int, help="Truncate the number of output (default - all)")
19parser.add_option("-o","--output", dest="outfile", help="Name of output file")
20
21parser.set_defaults(part=[],outfile="genevents.ascii",numevents=0,outevents=0)
22
23(options, args) = parser.parse_args()
24
25print (options, args)
26
27if len(args) == 0 :
28 print ("ERROR: No input! Aborting")
29 sys.exit(1)
30
31stpoints = []
32#reading input
33for infilename in args :
34 #opening file
35 infile = HepMC.ReaderAscii(infilename)
36
37 if infile.failed():
38 print("Wrong input. Exit.\n")
39 sys.exit(1)
40
41 while not infile.failed():
42 evt = HepMC.GenEvent()
43 infile.read_event(evt)
44
45 if infile.failed():
46 print("End of file reached.\n")
47 break
48
49 stpoints.append(evt)
50
51 infile.close()
52
53if len(stpoints) == 0 :
54 print ("ERROR: no events found is not a valid file!")
55 sys.exit(1)
56
57
58#creating an output stream
59if isfile(options.outfile) :
60 print ("WARNING: File",options.outfile,"already exists.")
61
62outdata = HepMC.WriterAscii(options.outfile)
63if outdata.failed():
64 print("Bad output. Exit.\n")
65 sys.exit(1)
66
67stpsize = len(stpoints)
68if (options.numevents > stpsize) :
69 print ("WARNING: requested number of events is bigger then provided in input files")
70 options.numevents = 0
71
72if (options.numevents == 0) :
73 options.numevents = stpsize #all events
74
75i = 0
76while i < options.numevents :
77 if (stpsize == 0) :
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.")
79 break
80 rand = randint(0,stpsize-1)
81 stpoint = stpoints.pop(rand) #take a random event
82 stpsize-=1
83
84 particles = stpoint.particles()
85 pid_filt = str(particles[-1].pdg_id())
86
87 if (options.part == []) or (pid_filt in options.part) : #check PID
88 stpoint.set_event_number(i)
89 outdata.write_event(stpoint)
90 i += 1
91
92outdata.close()
93print ("INFO: Written", i, options.numevents, "starting points")
94
95if (options.outevents > i) :
96 print ("WARNING: requested number of events is bigger then provided in input files")
97 options.outevents = 0
98
99if (options.outevents == 0) :
100 options.outevents = i #all events
101
102#starting the filter
103exec = __file__.replace("LArG4FSStartPointFilter.py","LArG4FSStartPointFilterBody.py")
104#print('athena -c "options={:s}" {:s}'.format(str(options),exec))
105os.system('athena -c "options={:s}" {:s}'.format(str(options),exec))
106
void print(char *figname, TCanvas *c1)