#!/usr/bin/python import os,sys,getopt opts, args = getopt.getopt(sys.argv[1:],"Mm") MC = 0 for opt,arg in opts: if opt in ("-M","-m"): MC = 1 loc = '/cache/halld/offline_monitoring/RunPeriod-2017-01/ver33/tree_tof_eff' loc1 = '' if MC: loc = '/cache/halld/workshops/workfest2018/recon/ver01/bggen/root' loc1 = '/cache/halld/workshops/workfest2018/recon/ver01/bggen_v2/root' if not MC: RunDirs = [] RUNS = [] for d in os.listdir(loc): if d.startswith('03'): run = d[0:len(d)] R = int(run) RunDirs.append(d) RUNS.append(R) RUNS.sort() RunDirs.sort() locations = './localdir' FileAvailable = [] RunsAvailable = [] for f in os.listdir(locations): if f.endswith('root'): FileAvailable.append(f); rstr = f[16:21] RunsAvailable.append(int(rstr)) for k in RUNS: # check first of already existing found = 0 for n in RunsAvailable: if n == k: found = 1 if not found: files = [] dd = loc+'/0'+str(k) runfiles = [] for f in os.listdir(dd): if f.endswith('root'): runfiles.append(f) runfiles.sort() of = 'localdir/tree_tof_eff_run0'+str(k)+'.root' cmd = 'hadd '+of LEN = len(runfiles) for n in range(0,LEN): cmd += ' '+dd+'/'+runfiles[n] print 'do run ',k, LEN #print cmd os.system(cmd) print 'Done with ',k else: rfiles = [] RUNS = [] for d in os.listdir(loc): if d.startswith('tree_tof_eff'): run = d[19:25] R = int(run) rfiles.append(d) RUNS.append(R) a = len(rfiles) for d in os.listdir(loc1): if d.startswith('tree_tof_eff'): run = d[19:25] R = int(run) rfiles.append(d) RUNS.append(R) of = 'localdir/tree_tof_eff_bggen_run0'+str(RUNS[0])+'.root' cmd = 'hadd '+of LEN = len(rfiles) for n in range(0,a): cmd += ' '+loc+'/'+rfiles[n] for n in range(a,len(rfiles)): cmd += ' '+loc1+'/'+rfiles[n] print 'do run ',RUNS[0], LEN print cmd os.system(cmd) print 'Done with ',RUNS[0]