def main():



    parser = OptionParser()
    #parser.add_option("-s", "--sample",   action="store", type="string", dest="sample", help="sample name" )
    #parser.add_option("-l", "--listSamples",   action="store", type="string", dest="list", help="listAllSamples" )
    (options, args) = parser.parse_args()
    
    anaVersion = getVariant()
    print " Current active skim: ", anaVersion
    anaDef = getAnaDefinition("sam")
    directlyFromRootfile = False
    if len(args) != 1 or (not args[0].endswith(".root") and args[0] not in anaDef):
            print "Usage: printTTree.py sampleName"
            print " - or -"
            print "Usage: printTTree.py rootfile"
            print "Avaliable samples:"
            for t in anaDef:
                print " ", t
            sys.exit(0)

    if args[0].endswith(".root"):
        print "Will print structure of given file"
        directlyFromRootfile = True

    if not directlyFromRootfile:
        sample= args[0]
        treeFilesAndNormalizations = getTreeFilesAndNormalizations(maxFilesMC=1, maxFilesData=1,
                    quiet = True, samplesToProcess=[sample,])

        if not treeFilesAndNormalizations[sample]["files"]:
            print "No files found for sample", sample, "- exiting"
            sys.exit(0)

        filename = treeFilesAndNormalizations[sample]["files"][0]
    else:
        filename = args[0]

    
    rootfile = ROOT.TFile.Open(filename, "read")

    todo = [(rootfile, 0), ]
    for t in todo:
        indent = t[1]
        print " "*indent, t[0].GetName()
        lst = t[0].GetListOfKeys()
        for l in lst:
            #print l.GetName()
            #continue

            #print "Going through", l.GetName(), l.ClassName()
            current = l.ReadObj()
            #current = rootfile.Get(l.GetName())
            if not current: continue
            if "TDirectory" in current.ClassName():
                todo.append( (current, indent+2) )
            else:
                print " "*(indent+2), current.GetName(), current.GetTitle(), "/"+current.ClassName()+"/"
                #print " "*(indent+2), current.GetTitle(), "/"+current.ClassName()+"/"

            if current.ClassName() == "TTree":
                branches =  current.GetListOfBranches()
                branchesNames = []
                for b in branches:
                    branchesNames.append(b.GetName())

                for b in sorted(branchesNames):
                    print " "*(indent+4), b
Exemple #2
0
def main():

    parser = OptionParser()
    #parser.add_option("-s", "--sample",   action="store", type="string", dest="sample", help="sample name" )
    #parser.add_option("-l", "--listSamples",   action="store", type="string", dest="list", help="listAllSamples" )
    (options, args) = parser.parse_args()

    anaVersion = getVariant()
    print " Current active skim: ", anaVersion
    anaDef = getAnaDefinition("sam")
    directlyFromRootfile = False
    if len(args) != 1 or (not args[0].endswith(".root")
                          and args[0] not in anaDef):
        print "Usage: printTTree.py sampleName"
        print " - or -"
        print "Usage: printTTree.py rootfile"
        print "Avaliable samples:"
        for t in anaDef:
            print " ", t
        sys.exit(0)

    if args[0].endswith(".root"):
        print "Will print structure of given file"
        directlyFromRootfile = True

    if not directlyFromRootfile:
        sample = args[0]
        treeFilesAndNormalizations = getTreeFilesAndNormalizations(
            maxFilesMC=1,
            maxFilesData=1,
            quiet=True,
            samplesToProcess=[
                sample,
            ])

        if not treeFilesAndNormalizations[sample]["files"]:
            print "No files found for sample", sample, "- exiting"
            sys.exit(0)

        filename = treeFilesAndNormalizations[sample]["files"][0]
    else:
        filename = args[0]

    rootfile = ROOT.TFile.Open(filename, "read")

    todo = [
        (rootfile, 0),
    ]
    for t in todo:
        indent = t[1]
        print " " * indent, t[0].GetName()
        lst = t[0].GetListOfKeys()
        for l in lst:
            #print l.GetName()
            #continue

            #print "Going through", l.GetName(), l.ClassName()
            current = l.ReadObj()
            #current = rootfile.Get(l.GetName())
            if not current: continue
            if "TDirectory" in current.ClassName():
                todo.append((current, indent + 2))
            else:
                print " " * (indent + 2), current.GetName(), current.GetTitle(
                ), "/" + current.ClassName() + "/"
                #print " "*(indent+2), current.GetTitle(), "/"+current.ClassName()+"/"

            if current.ClassName() == "TTree":
                branches = current.GetListOfBranches()
                branchesNames = []
                for b in branches:
                    branchesNames.append(b.GetName())

                for b in sorted(branchesNames):
                    print " " * (indent + 4), b
def main():
    filesToMerge = 10
    parser = OptionParser()
    parser.add_option("-f", "--force",   action="store_true",  dest="force", help="run even if target dir is present" )
    (options, args) = parser.parse_args()

    anaDef = getAnaDefinition("sam")
    if len(args) != 1 or args[0] not in anaDef:
        print "Usage: printTTree.py sampleName"
        print "Avaliable samples:"
        for t in anaDef:
            print " ", t
        sys.exit(1)

    sample= args[0]
    treeFilesAndNormalizations = getTreeFilesAndNormalizations(maxFilesMC=None, maxFilesData=None,
                quiet = True, samplesToProcess=[sample,], usePickle=False, donotvalidate=True)

    if not treeFilesAndNormalizations[sample]["files"]:
        print "No files found for sample", sample, "- exiting"
        sys.exit(1)

    indirName = os.path.dirname(anaDef[sample]["pathSE"])
    if not indirName.startswith("srm://"):
        print "Dont know how to process: ", indirName

    odirName = indirName+"_merged/"

    odirExists = subprocess.call(["lcg-ls", odirName],  stdout=subprocess.PIPE, stderr=subprocess.PIPE)==0
    if odirExists and not options.force:
        print "output directory seems to allready exist", odirName
        sys.exit(1)

    if not odirExists:
        odirCreated = subprocess.call(["srmmkdir", odirName],  stdout=subprocess.PIPE, stderr=subprocess.PIPE)==0
        if not odirCreated:
            print "cannot create output directory", odirName
            sys.exit(1)

    aTodo = []
    todos = []
    for f in treeFilesAndNormalizations[sample]["files"]:
        aTodo.append(f)
        if len(aTodo)==filesToMerge:
            todos.append(aTodo)
            aTodo = []

    if aTodo:
        todos.append(aTodo)

    cnt = 0
    for t in todos:
        cnt += 1
        print "Doing", cnt, len(todos),"...",
        goodFiles = validateRootFiles(t, quiet=True)["fileList"]
        # be extra careful here - oname will be removed!!
        onamebase = "trees_"+str(cnt)+"_1_TMF.root"
        onameForCopy = odirName+"/"+onamebase
        command = ["lcg-ls", onameForCopy]
        if runQuiet(command)==0:
            print "Looks like file is allready present, skipping"
            continue

        oname = "/tmp/"+onamebase
        command = ["hadd", oname]
        command.extend(goodFiles)
        print "Hadd...",
        ret=runQuiet(command)
        if ret!=0:
            print "Problem with hadd, ofile", cnt
            runQuiet(["rm", oname])
            continue


        command = ["lcg-cp", "file:"+oname, onameForCopy]
        print "lcg-cp...",
        ret=runQuiet(command)
        if ret!=0:
            print "Problem with lcg-cp, ofile", cnt
            runQuiet(["rm", oname])
            runQuiet(["srmrm", onameForCopy])
            continue

        print "rm local file...",
        runQuiet(["rm", oname])
        print "done"