generate_kamland_mc.py 2.78 KB
Newer Older
maming's avatar
maming committed
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
93
94
95
#=====================================================================================
#  Author: Aobo Li
#  Contact: liaobo77@gmail.com
#  
#  Last Modified: Sep. 5, 2021
#  
#  * Batch processing script for KamNet
#  * Converting the 
#=====================================================================================
#!/usr/bin/python
import json
import time
import datetime
import sys
import argparse
import os
import re
import string
import signal
import subprocess
import shutil
import numpy as np
from settings import OUT_DIR, INPUT_DIR, MACRO_DIR, TIME, PROCESSOR, ISOTOPE

def main(argv):
   refresh = False

   if refresh:
      '''
      If refresh is true, this will delete all files in OUT_DIR, then start re-generating files.
      '''
      print("Delete all files in 5 second!")
      time.sleep(5)

   # Setting the output directory if it does not exist.
   if not os.path.exists(OUT_DIR):
      os.mkdir(OUT_DIR)
   else: 
      if refresh:
         shutil.rmtree(OUT_DIR)
         os.mkdir(OUT_DIR)

   if not os.path.exists(MACRO_DIR):
      os.mkdir(MACRO_DIR)
   else:
      if refresh:
         shutil.rmtree(MACRO_DIR)
         os.mkdir(MACRO_DIR)

   inputfiles = []
   # Reading out isotopes from the input directory, and add their addresses into a list
   for SIG in ISOTOPE:
      inputfiles += [(ifile) for ifile in os.listdir(INPUT_DIR) if SIG in ifile and ".root" in ifile]

   for rootfile in inputfiles:
      # Loading the template to run on the Boston University SCC cluster batch queue
      # In order to run on your cluster batch system, please modify this .sh template and its inputs
      macrotemplate = string.Template(open('process_kamland.sh', 'r').read())
      with cd(MACRO_DIR):
         outputstring = str(OUT_DIR)
         timestring = str(TIME)
         inputstring = str(INPUT_DIR + rootfile)
         macrostring = macrotemplate.substitute(TIME=timestring, INPUT=inputstring, OUTPUT=outputstring, PROCESSOR=PROCESSOR, PROCESSING_UNIT=-1)
         macrofilename = 'shell_%s.sh' % (str(rootfile))
         macro = open(macrofilename,'w')
         macro.write(macrostring)
         macro.close()
         # print(os.path.join(MACRO_DIR, macrofilename))
         try:
            # os.system("source " + os.path.join(MACRO_DIR, macrofilename))
            command = ['qsub', macrofilename]
            process = subprocess.call(command)
         except Exception as error:
            return 0
   return 1


class cd:
   '''
   Context manager for changing the current working directory
   '''
   def __init__(self, newPath):
      self.newPath = newPath

   def __enter__(self):
      self.savedPath = os.getcwd()
      os.chdir(self.newPath)

   def __exit__(self, etype, value, traceback):
      os.chdir(self.savedPath)

if __name__=="__main__":

   print(sys.exit(main(sys.argv[1:])))