15 import sys, os, argparse, subprocess, fnmatch
24 eventStrings = eventList.split(
',')
26 for e
in eventStrings:
30 raise argparse.ArgumentTypeError(
"Can't convert %s to a valid event number" % e)
34 if not any(x
in formatString
for x
in [
'RAW',
'ESD',
'AOD']):
43 runEvents.append((run,e))
48 suffix +=
'.pool.root'
50 outputFile =
"r%08d_" % run
52 outputFile +=
"multiple_events."
54 outputFile +=
"e%012d." % events[0]
58 inputFilesString +=
" %s" % f
59 cmd =
"acmd.py filter-files -o %s -s '%s'%s" % (outputFile,
str(runEvents), inputFilesString)
62 if os.path.isfile(outputFile):
63 print (
"Output file %s already exists - please remove it or choose a different name for the output file (-o/--outputfile)" % outputFile)
66 print (
"Will run the following command to start the extraction")
68 extraction = subprocess.Popen(cmd, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
69 lines_iterator = iter(extraction.stdout.readline, b
"")
70 for line
in lines_iterator:
71 print((line.rstrip()))
74 size = os.path.getsize(outputFile)
76 print (
"Size of output file only %d bytes, deleting it as most likely empty" % size)
79 print (
"Done. Output saved to %s (%.1f kB)" % (outputFile, size/1024.))
81 print (
"NB! You requested %d events, please check the log to see that all were found" % len(events))
85 parser = argparse.ArgumentParser(description=
'Extract events specified by run, lumi block and event number from ATLAS event files. Written by C. Ohm - feedback is very welcome!')
86 parser.add_argument(
'-r',
'--run', required=
True, type=int, help=
'Run number')
87 parser.add_argument(
'-lb',
'--lumiblock', required=
True, type=int, help=
'Luminosity block number')
88 parser.add_argument(
'-e',
'--events', type=str, required=
True, help=
'Event number(s), separated by commas if several (e.g. 12345 or 123,456,789)')
89 parser.add_argument(
'-p',
'--projecttag', type=str, nargs=
'?', help=
'Project tag, defaults to "data15_13TeV"', default=
'data15_13TeV')
90 parser.add_argument(
'-s',
'--stream', type=str, nargs=
'?', help=
'Stream name, defaults to "physics_Main"', default=
'physics_Main')
91 parser.add_argument(
'-f',
'--fileformat', type=validFileFormat, help=
'File format: (D)RAW(_XYZ), (D)ESD(_XYZ), (D)AOD(_XYZ)', default=
'AOD')
92 parser.add_argument(
'-m',
'--matchingstring', type=str, nargs=
'?', default=
'', help=
'String for matching the dataset to look in, useful when there are several processings available, or both merged and unmerged datasets, e.g. "*merge.AOD*f620*" will do what you think')
93 parser.add_argument(
'-o',
'--outputfile', type=str, nargs=
'?', help=
'Name for the output file (appropriate suffix based on input format will be appended automatically)')
94 parser.add_argument(
'-v',
'--verbose', action=
'store_true', default=
False, help=
'Verbose mode, prints out eos paths and commands, file names, etc')
95 parser.add_argument(
'-i',
'--inputfile', nargs=
'+', type=str, default=
'', help=
'Local input file(s), to bypass search in EOS disk buffer')
97 args = parser.parse_args()
103 if args.inputfile !=
"":
104 print (
"Will look in specified input file(s)")
105 extractEvents(args.run, eventList, args.inputfile, args.fileformat)
108 if args.fileformat
is False:
109 print (
"Unknown argument for -f/--fileformat - please provide a valid string describing the file format, i.e. one containing 'RAW', 'ESD' or 'AOD'")
112 print (
"Will try to extract event(s) %s in LB %d of run %d (project tag: %s, stream: %s, format: %s, matching string: \"%s\")" % (args.events, args.lumiblock, args.run, args.projecttag, args.stream, args.fileformat, args.matchingstring))
114 filePath =
"root://eosatlas.cern.ch//eos/atlas/atlastier0/rucio/%s/%s/%08d/" % (args.projecttag, args.stream, args.run)
116 cmd = eospath+
" ls "+filePath
118 print (
"Will run the following command to get a list of datasets matching the provided info")
120 env = os.environ.copy()
121 datasetListing = subprocess.Popen(cmd, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
124 print (
"Datasets before requiring match with pattern (%s):" % args.matchingstring)
125 for line
in datasetListing.stdout.readlines():
127 print (
" %s" % line.rstrip())
129 if "LOGARC.tar" in line:
132 if (
".%s." % args.fileformat)
in line
or (args.fileformat ==
"RAW" and ".RAW" in line):
133 datasetNames.append(line.rstrip())
136 datasetNames = [ds
for ds
in datasetNames
if ".LOGARC" not in ds]
137 if args.matchingstring !=
'':
139 print (
"Removing datasets that don't match %s" % (args.matchingstring))
140 datasetNames = [ds
for ds
in datasetNames
if fnmatch.fnmatch(ds,
"*"+args.matchingstring+
"*")]
142 if len(datasetNames) > 1:
143 print (
"More than one dataset matching the provided info")
144 for ds
in datasetNames:
146 print (
"Please provide tighter constraints, e.g. by using the -m/--matchingstring option")
149 if len(datasetNames) == 0:
150 print (
"No dataset matching the provided info - please provide looser constraints and or use the -v/--verbose switch to see more info about what datasets are available in EOS")
153 print (
"Will use the following dataset found in EOS: %s" % datasetNames[0])
155 cmd = eospath+
" ls "+filePath+datasetNames[0]+
'/'
157 print (
"Will run the following command to get a list of files in the selected dataset")
160 fileListing = subprocess.Popen(cmd, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
162 for line
in fileListing.stdout.readlines():
164 substrings = line.split(
'lb')
166 if line.count(
'lb') == 2:
167 lbs =
range(
int(substrings[1][0:4]),
int(substrings[2][0:4])+1)
169 lbs.append(
int(substrings[1][0:4]))
170 if args.lumiblock
in lbs:
171 fileNames.append(filePath+datasetNames[0]+
"/"+line.rstrip())
173 print (
"Will look for the sought event(s) in the following %d file(s):" % len(fileNames))
174 for file
in fileNames:
178 if len(fileNames) == 0:
179 print (
"No files available in %s - will exit" % (filePath+datasetNames[0]+
'/'))
183 extractEvents(args.run, eventList, fileNames, args.fileformat)
185 if __name__ ==
'__main__':