EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
simpleanalysis.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file simpleanalysis.py
1 #!/usr/bin/env python
2 #SBATCH -J EICSTUDY # job name
3 #SBATCH -o logs/simple-%A_%a.out
4 #SBATCH -e logs/simple-%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 12:00: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 ./simpleanalysis.py --input <DIRECTORY CONTAINING FILES> --name <OUTPUT DIRECTORY>
22 #
23 #
24 
25 import subprocess
26 import math
27 import os
28 import sys
29 import shutil
30 import glob
31 import re
32 import ast
33 
34 import argparse
35 
36 global tclparams, pythiaparams
37 tclparams = {}
38 pythiaparams = {}
39 
40 
41 parser = argparse.ArgumentParser()
42 
43 parser.add_argument("-n", "--name", type=str,
44  help="name for this study set (e.g. bfield)")
45 parser.add_argument("-i", "--input", type=str,
46  help="input directory of Delphes ROOT files")
47 parser.add_argument("-m", "--modules", type=str,
48  help="analysis module list")
49 parser.add_argument("-f", "--force", default=False, action='store_true',
50  help="force-overwrite existing output")
51 
52 global args
53 args = parser.parse_args()
54 
55 # Create the task superdirectory
56 
57 if not os.path.exists(args.name):
58  try:
59  os.makedirs(args.name)
60  except OSError:
61  print("%s already exists... continuing..." % (args.name))
62 
63 
64 SLURM_ARRAY_TASK_ID="0"
65 
66 try:
67  SLURM_ARRAY_TASK_ID=os.environ["SLURM_ARRAY_TASK_ID"]
68 except:
69  print("Please set the SLURM_ARRAY_TASK_ID environment variable to a number (e.g. 0) before running this script.")
70  sys.exit()
71  pass
72 
73 print("Task ID requested: %d" % (int(SLURM_ARRAY_TASK_ID)))
74 
75 
76 
77 # Load all the ROOT files to Process
78 
79 root_files = glob.glob(args.input + "/*/*.root")
80 root_files = sorted(root_files)
81 
82 if len(root_files) == 0:
83  print("No appropriate ROOT files exist in the input directory")
84  sys.exit()
85 else:
86  print("Input directory contains %d files for processing" % (len(root_files)))
87 
88 
89 #print(root_files)
90 
91 
92 # Use the task ID to load the necessary file from the array
93 fileNumber = int(SLURM_ARRAY_TASK_ID)
94 
95 # Search out the file number in this list
96 root_file = None
97 for test_file in root_files:
98  if test_file.find('%d/out.root' % fileNumber) != -1:
99  root_file = test_file
100  break
101 
102 if root_file == None:
103  print("Unable to find an input file consistent with the task ID = %d" %
104  (int(SLURM_ARRAY_TASK_ID)))
105  sys.exit()
106 
107 # Get the full path to the input file
108 root_file = os.path.abspath(root_file)
109 print("Processing %s" % (root_file))
110 
111 
112 # Execute the study
113 
114 taskdir="%s/%d" % (args.name, fileNumber)
115 
116 if os.path.exists(taskdir) and not args.force:
117  print("Skipping this task directory --- it already exists. Cleanup before overwriting!")
118  print(taskdir)
119 else:
120  if not os.path.exists(taskdir):
121  os.makedirs(taskdir)
122 
123  # Copy or Link needed files to working directory
124  subprocess.call("cp -a mva_taggers %s/" % (taskdir), shell=True);
125  # Execute the study
126  subprocess.call('cd {0[taskdir]}; SimpleAnalysis.exe --input_dir {0[root_file]} --output_file out.root --module_sequence "{0[modules]}"'.format({'taskdir': taskdir, 'root_file': root_file, 'modules': args.modules}), shell=True)