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