import glob, sys, os, string, io, ROOT, math, commands, time pandapath=os.environ.get('PANDAPATH') home=os.environ.get('HOME') sys.path.append(pandapath+'/macro/tpc/FOPI/python/argparse-1.2.1') sys.path.append(pandapath+'/macro/tpc/FOPI/mberger') import argparse from makeRunlist import make_runlist parser=argparse.ArgumentParser(description='send analyseFOPIReco_cosmics2 to batchfarm') parser.add_argument('path', help='the path to where the runfolders are',type=str) parser.add_argument('pattern', help='pattern in the filenames',type=str) parser.add_argument('runs', help='the run numbers',type=str) parser.add_argument('--merge', help='merge all histo file together (default=%(default)s)',type=str,default='') parser.add_argument('--mergeOnly', help='submit only the anamerger file (default=%(default)s)',action='store_true') parser.add_argument('--dontSub', help='so not sumbit to batch farm (default=%(default)s)',action='store_true') parser.add_argument('--inter', help='interactive not batch farm (default=%(default)s)',action='store_true') parser.add_argument('--fopi', help='enable them full fopi reconstruction (default=%(default)s)',action='store_true') parser.add_argument('--verbose', help='be more verbose (default=%(default)s)',action='store_true') parser.add_argument('--detonly', help='use oly detector with ID',type=int,default=-1) parser.add_argument('--tpcID', help='The detector ID of the TCP (default=%(default)s)',default=22,type=int) parser.add_argument('--skipExist', help='skip existing hist files',action='store_true') parser.add_argument('--runfolderadd',help='string added to the run folders',type=str,default='') #parser.add_argument('--parts', help='the files exist as parts',action='store_true') args=parser.parse_args() ROOT.gROOT.ProcessLine(".x rootlogon.C") ROOT.gROOT.ProcessLine('gStyle->SetPalette(1)') ROOT.gROOT.ProcessLine('gROOT->SetStyle("Plain")') runList=make_runlist(args.runs) if args.verbose: print 'the runlist:',runList mergename='{0}/{1}'.format(args.path,args.merge) if args.fopi: submitScript='batchAnaPion.sh' else: submitScript='batchAnaCosmics.sh' addfiles='' ids='' if args.verbose: print "using submit script",submitScript print "checking for pattern: *{0}*.reco.root".format(args.pattern) for run in runList: if args.verbose: print "checking for files in: {0}/run_{1}{2}/".format(args.path,run,args.runfolderadd) files=glob.glob('{0}/run_{1}{3}/*{2}*.reco.root'.format(args.path,run,args.pattern,args.runfolderadd)) files.sort() histfiles=glob.glob('{0}/run_{1}/*{2}*.hist.root'.format(args.path,run,args.pattern)) if args.verbose: print 'the files:' print files for f in files: words=f.split('_') histfile=f.replace('.reco.','.hist.') addfiles+=' '+histfile if histfile in histfiles and args.skipExist: print "skipping",histfile,"because it is already done" continue os.environ['anafilename']=f os.environ['histfilename']=histfile.split('/')[-1] os.environ['runnnumber']=str(run) os.environ['gas']=words[5] os.environ['field']=words[4] os.environ['gain']=words[6] os.environ['det']=str(args.detonly) os.environ['tpcID']=str(args.tpcID) err='' if 'stdErr' in words: err+='_stdErr' if 'planeErr' in words: err+='_planeErr' if 'scaled' in words: err+='_scaled' os.environ['error']=err if args.verbose: print "found error types:",err corr='' if 'recl' in words: corr+='_recl' if 'clcorr' in words: corr+='_clcorr' os.environ['correction']=corr if args.verbose: print "found correction types:",corr name=f.split('/')[-1] print 'submitting file',f if not args.mergeOnly and not args.inter: os.system("python {0}/FOPI/BatchTools/check_disk_io.py".format(pandapath)) qsub='sbatch -J {0} -o {2}/sgeout/{0} -e {2}/sgeout/{0} --time=120 --mem-per-cpu=2000m {3}/FOPI/BatchTools/{1}'.format(name,submitScript,home,pandapath) status, jobid = commands.getstatusoutput(qsub) print 'submitted:',name, jobid ids+=':'+jobid.split()[-1] time.sleep(0.5) if args.inter: os.system('chmod a+x {1}/FOPI/BatchTools/{0}'.format(submitScript,pandapath)) os.system('{1}/FOPI/BatchTools/{0}'.format(submitScript,pandapath)) if args.merge!='': mergefilename=pandapath+'/anamerger_'+args.pattern mergefile=open(mergefilename,'w') mergefile.write('#!/bin/bash\n') mergefile.write('pwd \n') mergefile.write('echo $PANDAPATH \n') mergefile.write('cd $PANDAPATH\npwd\n. ./setenv\n') mergefile.write('hadd -f {0} {1}\n'.format(mergename,addfiles)) mergefile.write('if [ -f {0} ];then'.format(mergename)) mergefile.write(' echo all merged\n') mergefile.write('fi') mergefile.close() os.system('chmod a+x {0}'.format(mergefilename)) print 'submitting merger' print 'dependent on:',ids if not args.dontSub and not args.inter: if args.mergeOnly: qsub='sbatch -J {1} -o {2}/sgeout/merge_{1} -e {2}/sgeout/merge_{1} --time=20 --mem-per-cpu=1000m {0}'.format(mergefilename,args.merge,home) print qsub status, jobid = commands.getstatusoutput(qsub) print 'merger:',jobid else: qsub='sbatch -J {1} -o {2}/sgeout/merge_{1} -e {2}/sgeout/merge_{1} --time=20 --mem-per-cpu=1000m -d afterok{3} {0}'.format(mergefilename,args.merge,home,ids) status, jobid = commands.getstatusoutput(qsub) print 'merger:',jobid if args.inter: os.system('{0}'.format(mergefilename))