ATLAS Offline Software
trigbs_pickEvents.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
4 
5 import eformat
6 import sys
7 import libpyevent_storage as EventStorage
8 import getopt
9 
10 def usage():
11  print(" usage: %s [options] <filein> <fileout> <events1> [event2] ..." % sys.argv[0])
12  print(" Writes out selected events from a bytestream file")
13  print(" Events are specified as individual event number (counting from 0)")
14  print(" or as ranges of event numbers (e.g. 3-10).")
15  print(" Multiple ranges can be specified")
16  print()
17  print(" options: ")
18  print(" -1 - select on l1 id instead of event number")
19  print(" -g - select on global id instead of event number")
20  print(" -v - verbose")
21  print(" -h - help")
22  sys.exit(1)
23 
24 try:
25  opts, args = getopt.getopt(sys.argv[1:], "1ghv")
26  if len(args)<3:
27  raise getopt.GetoptError("not enough arguments provided")
28 except getopt.GetoptError as err:
29  print(err)
30  usage()
31  sys.exit(2)
32 
33 useL1ID=False
34 useGlobalID=False
35 verbose=False
36 for opt,arg in opts:
37  if opt == '-1':
38  useL1ID=True
39  if opt == '-g':
40  useGlobalID=True
41  if opt == '-v':
42  verbose=True
43 
44 input_file = args[0]
45 output_file = args[1]
46 eventNums=args[2:]
47 
48 if verbose:
49  print("Opening file: %s" % ( input_file))
50 
51 input = eformat.istream([input_file])
52 dr=EventStorage.pickDataReader(input_file)
53 output = eformat.ostream(core_name=output_file,
54  run_number=dr.runNumber(),
55  trigger_type=dr.triggerType(),
56  detector_mask=dr.detectorMask(),
57  beam_type=dr.beamType(),
58  beam_energy=dr.beamEnergy())
59 
60 ranges=[]
61 for range in eventNums:
62  vals=range.split('-')
63  if len(vals)==1:
64  vals.append(vals[0])
65  if vals[0]=='':
66  vals[0]=0
67  if vals[1]=='':
68  vals[1]=1<<32
69  ranges.append( (int(vals[0]),int(vals[1])) )
70 
71 cnt=0
72 numSaved=0
73 for event in input:
74  if verbose:
75  print("Reading event %d with Lvl1 Id = %ld and global Id = %ld"% (cnt,event.lvl1_id(),event.global_id()))
76  num=cnt
77  if useL1ID:
78  num=event.lvl1_id()
79  if useGlobalID:
80  num=event.global_id()
81  for range in ranges:
82  if num>=range[0] and num<=range[1]:
83  print(" - Saving event %d with Lvl1 Id = %ld and global Id = %ld"% (cnt,event.lvl1_id(),event.global_id()))
84  output.write(event)
85  numSaved+=1
86  break
87  cnt+=1
88 
89 print(numSaved,'Events saved to',output.last_filename())
trigbs_pickEvents.usage
def usage()
Definition: trigbs_pickEvents.py:10
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70