ATLAS Offline Software
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 
4 import sys
5 import os
6 from os.path import isfile
7 from optparse import OptionParser
8 from random import randint
9 from math import sqrt
10 from pyHepMC3 import HepMC3 as HepMC
11 
12 usage = "usage: %prog [options] input1 [input2...]"
13 
14 parser = OptionParser(usage=usage, version="%prog v0.0.1 $Id: LArG4FSStartPointFilter.py 711210 2015-11-27 15:56:00Z jchapman $")
15 
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")
20 
21 parser.set_defaults(part=[],outfile="genevents.ascii",numevents=0,outevents=0)
22 
23 (options, args) = parser.parse_args()
24 
25 print (options, args)
26 
27 if len(args) == 0 :
28  print ("ERROR: No input! Aborting")
29  sys.exit(1)
30 
31 stpoints = []
32 #reading input
33 for 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 
53 if 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
59 if isfile(options.outfile) :
60  print ("WARNING: File",options.outfile,"already exists.")
61 
62 outdata = HepMC.WriterAscii(options.outfile)
63 if outdata.failed():
64  print("Bad output. Exit.\n")
65  sys.exit(1)
66 
67 stpsize = len(stpoints)
68 if (options.numevents > stpsize) :
69  print ("WARNING: requested number of events is bigger then provided in input files")
70  options.numevents = 0
71 
72 if (options.numevents == 0) :
73  options.numevents = stpsize #all events
74 
75 i = 0
76 while 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 
92 outdata.close()
93 print ("INFO: Written", i, options.numevents, "starting points")
94 
95 if (options.outevents > i) :
96  print ("WARNING: requested number of events is bigger then provided in input files")
97  options.outevents = 0
98 
99 if (options.outevents == 0) :
100  options.outevents = i #all events
101 
102 #starting the filter
103 exec = __file__.replace("LArG4FSStartPointFilter.py","LArG4FSStartPointFilterBody.py")
104 #print('athena -c "options={:s}" {:s}'.format(str(options),exec))
105 os.system('athena -c "options={:s}" {:s}'.format(str(options),exec))
106 
vtune_athena.format
format
Definition: vtune_athena.py:14
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
str
Definition: BTagTrackIpAccessor.cxx:11