Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
runCondor_plotTracklet.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file runCondor_plotTracklet.py
1 from optparse import OptionParser
2 import time
3 import os
4 import pwd
5 import sys
6 import re
7 
8 def dir_empty(dir_path):
9  return not any((True for _ in os.scandir(dir_path)))
10 
11 # Ref: https://www.geeksforgeeks.org/how-to-search-and-replace-text-in-a-file-in-python/
12 def replacetext(filename, search_text, replace_text):
13  with open(filename, 'r+') as f:
14  file = f.read()
15  file = re.sub(search_text, replace_text, file)
16  f.seek(0)
17  f.write(file)
18  f.truncate()
19  # return 'Text {} replaced by {}'.format(search_text, replace_text)
20 
21 
22 if __name__ == '__main__':
23  parser = OptionParser(usage="usage: %prog ver [options -n]")
24  parser.add_option("-d", "--isdata", dest="isdata", action="store_true", default=False, help="Is data")
25  # parser.add_option("-p", "--dirprefix", dest="dirprefix", default='ana398_zvtx-20cm_dummyAlignParams', help="Directory prefix")
26  parser.add_option("-f", "--filedesc", dest="filedesc", default='HIJING_ana398_xvtx-0p04cm_yvtx0p24cm_zvtx-20cm_dummyAlignParams', help="File description")
27  # parser.add_option("-s", "--nset", dest="nset", default=400, help="Number of sets")
28  # parser.add_option("-r", "--drcut", dest="drcut", default=0.5, help="Delta R cut for tracklets")
29  parser.add_option("-j", "--nJob", dest="nJob", default=400, help="nJob")
30  parser.add_option("-r", "--drcut", dest="drcut", default=0.5, help="Delta R cut for tracklets")
31  parser.add_option("-s", "--submitcondor", dest="submitcondor", action="store_true", default=False, help="Submit condor jobs")
32 
33  (opt, args) = parser.parse_args()
34  print('opt: {}'.format(opt))
35 
36  isdata = opt.isdata
37  # dirprefix = opt.dirprefix
38  filedesc = opt.filedesc
39  # nset = int(opt.nset)
40  # drcut = float(opt.drcut)
41  nJob = int(opt.nJob)
42  submitcondor = opt.submitcondor
43  drcut = float(opt.drcut)
44  username = pwd.getpwuid(os.getuid())[0]
45 
46  # subdir = 'data' if isdata else 'sim'
47  finaloutfiledir = '/sphenix/user/{}/TrackletAna/analysis_INTT/plot/hists/{}/{}'.format(username, filedesc, 'dRcut'+str(drcut).replace('.', 'p'))
48  os.makedirs(finaloutfiledir, exist_ok=True)
49 
50  os.makedirs('./log_plottracklet/', exist_ok=True)
51  if not dir_empty('./log_plottracklet/'):
52  os.system('rm ./log_plottracklet/*')
53 
54  condorFileName = "submitCondor_plottracklet_{}.job".format('data' if isdata else 'sim')
55  condorFile = open("{}".format(condorFileName), "w")
56  condorFile.write("Universe = vanilla\n")
57  condorFile.write("InitialDir = /sphenix/user/{}/TrackletAna/analysis_INTT\n".format(username))
58  condorFile.write("Executable = $(InitialDir)/condor_plotTracklet.sh\n")
59  condorFile.write("PeriodicHold = (NumJobStarts>=1 && JobStatus == 1)\n")
60  condorFile.write("request_memory = 4GB\n")
61  condorFile.write("Priority = 20\n")
62  condorFile.write("job_lease_duration = 3600\n")
63  condorFile.write("isdata = {}\n".format(1 if isdata else 0))
64  condorFile.write("infilename = /sphenix/user/{}/TrackletAna/minitree/INTT/TrackletMinitree_{}/{}/minitree_$(Process).root\n".format(username, filedesc, 'dRcut'+str(drcut).replace('.', 'p')))
65  condorFile.write("outfilename = {}/hists_$(Process).root\n".format(finaloutfiledir))
66  condorFile.write("Output = $(Initialdir)/condor/log_plottracklet/condorlog_$(Process).out\n")
67  condorFile.write("Error = $(Initialdir)/condor/log_plottracklet/condorlog_$(Process).err\n")
68  condorFile.write("Log = $(Initialdir)/condor/log_plottracklet/condorlog_$(Process).log\n")
69  condorFile.write("Arguments = \"$(isdata) $(infilename) $(outfilename)\"\n")
70  condorFile.write("Queue {}\n".format(nJob))
71  condorFile.close() # Close the file before submitting the job
72 
73  if submitcondor:
74  cmd = 'condor_submit ' + condorFileName
75  os.system(cmd)
76 
77  # for i in range(nset):
78  # print('Set {}'.format(i))
79  # newfile = 'submitCondor_plotTracklet_set{}.job'.format(i)
80  # os.system('cp submitCondor_plotTracklet.job {}'.format(newfile))
81  # replacetext(newfile, 'ISDATA', '{}'.format(isdata))
82  # inputminitree = '/sphenix/user/mjpeters/analysis/dNdEta_Run2023/macros/dataTracklet_{}.root'.format(i) if isdata else '/sphenix/user/hjheng/TrackletAna/minitree/INTT/TrackletMinitree_{}/{}/TrackletAna_minitree_Set{}_dRcut{}.root'.format(dirprefix, subdir, i, str(drcut).replace('.', 'p'))
83  # outhistname = '/sphenix/user/hjheng/TrackletAna/analysis_INTT/plot/hists/{}/Hists_RecoTracklets_Set{}.root'.format(dirprefix, i)
84  # replacetext(newfile, 'INFILENAME', '{}'.format(inputminitree))
85  # replacetext(newfile, 'OUTFILENAME', '{}'.format(outhistname))
86  # replacetext(newfile, 'SET', '{}'.format(i))
87  # cmd = 'condor_submit ' + newfile
88  # os.system(cmd)