-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_skim.py
executable file
·92 lines (70 loc) · 3.03 KB
/
run_skim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
#SBATCH -J EICSKIM # job name
#SBATCH -o logs/eicskim-%A_%a.out
#SBATCH -e logs/eicskim-%A_%a.out
#SBATCH -n 1
#SBATCH -c 1
#SBATCH --mem=6G
#SBATCH -p htc # queue (partition) -- batch, parallel, etc. parallel-medium
#SBATCH -t 00:05:00 # run time (hh:mm:ss)
#SBATCH -D . # Directory where executable will be run
#SBATCH [email protected]
#SBATCH --mail-type=fail # email me when the job finishes
# This script was written originally for use on a SLURM-based batch system,
# like the one available at SMU ("ManeFrame II"). It can be run on the command-line
# alone; if the script doesn't detect the requested SLURM environment variables,
# it will ask you to specify them. For instance, to run the first variation in a
# study,
#
# SLURM_ARRAY_TASK_ID=0 ./run_skim.py -i <INPUT DIRECTORY> -o <OUTPUT DIRECTORY> -f out.root -c "Jet.Flavor==4"
#
# will skim the <INPUT DIRECTORY>/0/out.root file into <OUTPUT DIRECTORY>/0/out.root file, using the cut
# Jet.Flavor==4. Any event containing such a jet will be retained in the skim.
import subprocess
import math
import os
import sys
import shutil
import glob
import re
import ast
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", type=str,
help="directory holding all input ROOT files for skimming")
parser.add_argument("-o", "--output", type=str,
help="directory holding all input ROOT files for skimming")
parser.add_argument("-r", "--rootfile", type=str,
help="Name of the ROOT file in each subdirectory of the input directory")
parser.add_argument("-c", "--cuts", type=str,
help="ROOT selection string-style cuts")
parser.add_argument("-f", "--force", default=False, action='store_true',
help="force-overwrite existing output")
args = parser.parse_args()
# Create the task superdirectory
if not os.path.exists(args.output):
try:
os.makedirs(args.output)
except OSError:
print("%s already exists... continuing..." % (args.output))
SLURM_ARRAY_TASK_ID="0"
try:
SLURM_ARRAY_TASK_ID=os.environ["SLURM_ARRAY_TASK_ID"]
except:
print("Please set the SLURM_ARRAY_TASK_ID environment variable to a number (e.g. 0) before running this script.")
sys.exit()
pass
print("Task ID requested: %d" % (int(SLURM_ARRAY_TASK_ID)))
value_index = int(SLURM_ARRAY_TASK_ID)
# Execute the skim
taskdir="%s/%d" % (args.output, value_index)
inputfile="%s/%d/%s" % (args.input, value_index, args.rootfile)
outputfile="%s/%d/%s" % (args.output, value_index, args.rootfile)
if os.path.exists(taskdir) and not args.force:
print("Skipping this task directory --- it already exists. Cleanup before overwriting!")
print(taskdir)
else:
if not os.path.exists(taskdir):
os.makedirs(taskdir)
# Execute the study
subprocess.call("root -q -l -b ./DelphesSkim.C'+(\"{0[input]}\",\"{0[output]}\",\"{0[cuts]}\")'".format({'input': inputfile, 'output': outputfile, 'cuts': args.cuts}), shell=True)