Newer
Older
Lb2Ksppi-Bender / Phys / B2KShh / job / download.py
@Elena Graverini Elena Graverini on 29 Jan 2016 2 KB changed location of data
import pickle

global jobs

def getlist(*job_numbers):
    for j in job_numbers:
        print j
    if not job_numbers or len(job_numbers) > 2:
        return
    if len(job_numbers) == 1:
        sel_jobs = [jobs(job_numbers[0])]
    else:
        sel_jobs = jobs.select(job_numbers[0], job_numbers[1])
    lfns = {}
    print 'Selecting jobs %s' % [j.id for j in sel_jobs]
    for j in sel_jobs:
        lfns[j.name] = []
        for sj in j.subjobs:
            if sj.status == 'completed' or sj.status == 'completing':
                outfiles = sj.outputfiles
                rootfile = [f for f in outfiles if '.root' in f.namePattern][0]
                lfn = rootfile.lfn
                lfns[j.name].append(lfn)
            else:
                print '\tWARNING: no outputs for subjob %s.%s' % (j.id, sj.id)
    #pickle.dump(lfns, open('B2KSph-MC.pkl', 'wb'))
    return lfns



dest1 = '/disk/data3/lhcb/elena/B2KShh/ntuples/jobs'
dest2 = '/disk/data3/lhcb/elena/Lb2Lcmunu/ntuples/jobs'
scripts = '/cvmfs/lhcb.cern.ch/lib/lhcb/LHCBDIRAC/LHCBDIRAC_v8r2p29/scripts'
import os

def download(mylist, dest):
    #os.system('SetupProject.sh LHCbDirac')
    #os.system('lhcb-proxy-init')
    for k in mylist.keys():
        os.system('mkdir -p %s/%s' % (dest, k))
        l = len(mylist[k])
        for i,rootfile in enumerate(mylist[k]):
            print '%s\t\t: %s of %s' % (k, i, l)
            file_id = rootfile.split('/')[-2]
            file_name = rootfile.split('/')[-1]
            if not os.path.isfile('%s/%s/%s.root' % (dest, k, file_id)):
                os.system('%s/dirac-dms-get-file %s' % (scripts, rootfile))
                os.system('mv %s %s/%s/%s.root' % (file_name, dest, k, file_id))


def deleteFromGrid(mylist):
    #os.system('SetupProject.sh LHCbDirac')
    #os.system('lhcb-proxy-init')
    for k in mylist.keys():
        l = len(mylist[k])
        for i,rootfile in enumerate(mylist[k]):
            print '%s\t\t: Deleting %s of %s' % (k, i, l)
            os.system('%s/dirac-dms-remove-files %s' % (scripts, rootfile))


def hadd(mylist):
    #os.system('SetupProject Bender v27r0p1')
    paths = ['%s/%s/' % (dest, k) for k in mylist.keys()]
    print 'Found folders:'
    for path in paths:
        target = dest + '/' + path.split('/')[-2] + '.root'
        print '\t %s' % path
        print '\t\t Target is: %s' % target
        files = os.listdir(path)
        paths = [path + f for f in files]
        command = 'hadd -f %s %s' % (target, ' '.join(paths))
        print command
        os.system(command)



if __name__ == '__main__':
    #mylist = getlist(265, 268)
    out = '/afs/cern.ch/work/e/egraveri/Analysis/ganga/Lb2Lcmunu'
    #pickle.dump(mylist, open(out + '/lfns.pkl', 'wb'))
    mylist = pickle.load(open(out + '/lfns.pkl', 'rb'))
    download(mylist, dest2)
    #hadd(mylist)