Example #1
0
def ReadHighPtHistograms(dirName):

    if os.path.isdir(dirName):
        fileList = sorted(glob.glob(dirName + "*.root"))
        if len(fileList) > 1:
            print "Nope!!!"
            return {}
        else:
            file = TFile(fileList[0], "READ")
    else:
        file = TFile(dirName, "READ")

    # Run over the file
#    emFileList = sorted(glob.glob(dirName+"EM*/*.root"))
#    lcFileList = sorted(glob.glob(dirName+"LC*/*.root"))
#    if len(emFileList) != 1:
#        print "Found a number of EM root files not equal to 1 in dir:",dirName
#        return None
#    if len(lcFileList) != 1:
#        print "Found a number of LC root files not equal to 1 in dir:",dirName
#        return None
#    emFile = TFile(emFileList[0],"READ")
#    lcFile = TFile(lcFileList[0],"READ")

    histos = {}
    for aJetDef in jetDefList:
        histos[aJetDef] = {}

        #        rootFile = None
        #        if "EMJES" in aJetDef:
        #            rootFile = emFile
        #        elif "LCJES" in aJetDef:
        #            rootFile = lcFile
        #        else:
        #            print "Unexpected jet def:",aJetDef
        #            return None

        rootFile = file

        for histName in rootFile.GetKeyNames():
            if aJetDef not in histName: continue
            histo = rootFile.Get(histName)
            if histo is None:
                print "Failed to get histogram:", histName
                return None
            histo.SetName(histName + "_1D")

            histos[aJetDef][re.sub(
                "_%s" % (aJetDef), "", histName
            )] = ProviderHistoHelpers.ConvertPtHistoToProviderHisto(
                histo, histName)
            histos[aJetDef][re.sub("_%s" % (aJetDef), "",
                                   histName)].SetDirectory(0)

    lcFile.Close()
    emFile.Close()

    return histos
Example #2
0
def ReadHighPtHistograms(dirName):
    if not dirName.endswith("/"):
        dirName = dirName + "/"

    # Run over the two files
    emFileList = sorted(glob.glob(dirName + "EM*/*.root"))
    lcFileList = sorted(glob.glob(dirName + "LC*/*.root"))
    if len(emFileList) != 1:
        print "Found a number of EM root files not equal to 1 in dir:", dirName
        return None
    if len(lcFileList) != 1:
        print "Found a number of LC root files not equal to 1 in dir:", dirName
        return None
    emFile = TFile(emFileList[0], "READ")
    lcFile = TFile(lcFileList[0], "READ")

    histos = {}
    for aJetDef in jetDefList:
        histos[aJetDef] = {}

        rootFile = None
        if "EMJES" in aJetDef:
            rootFile = emFile
        elif "LCJES" in aJetDef:
            rootFile = lcFile
        else:
            print "Unexpected jet def:", aJetDef
            return None

        for histName in rootFile.GetKeyNames():
            if aJetDef not in histName: continue
            histo = rootFile.Get(histName)
            if histo is None:
                print "Failed to get histogram:", histName
                return None
            histo.SetName(histName + "_1D")

            histos[aJetDef][re.sub(
                "_%s" % (aJetDef), "", histName
            )] = ProviderHistoHelpers.ConvertPtHistoToProviderHisto(
                histo, histName)
            histos[aJetDef][re.sub("_%s" % (aJetDef), "",
                                   histName)].SetDirectory(0)

    lcFile.Close()
    emFile.Close()

    return histos
Example #3
0
def ReadInSituHistograms(dirName):
    if not dirName.endswith("/"):
        dirName = dirName + "/"

    # Run over each subdirectory (one per jet definition)
    histos = {}
    subDirs = sorted(glob.glob(dirName + "*"))
    for aSubDirName in subDirs:
        # Determine the jet definition
        jetDef = ""
        for aDirDef, aJetDef in jetDefDict.iteritems():
            if aDirDef in aSubDirName:
                jetDef = aJetDef
                break
        if jetDef == "":
            print "Failed to determine jet definition for directory:", aSubDirName
            return None
        histos[jetDef] = {}

        # Loop over the systematic files in the subdirectory
        systFiles = sorted(glob.glob(aSubDirName + "/SystError_*.txt"))
        for aSystFile in systFiles:
            # Figure out which component this is
            systematicNameHandle = re.sub(aSubDirName + "/SystError_", "",
                                          aSystFile)
            systematicNameHandle = re.sub(".txt", "", systematicNameHandle)
            systematicName = SystematicNameDictionary[systematicNameHandle]
            #print "Making histogram %s --> %s"%(systematicNameHandle,systematicName)

            # Open the file
            systematicFile = open(aSystFile, "r")

            # Read the lines of the file into arrays
            lowBinEdges = []
            systValues = []
            for line in systematicFile.readlines():
                line = line.strip("\r\n")
                lowEdge = float(line.split()[0])
                systVal = float(line.split()[2].strip())

                lowBinEdges.append(lowEdge)
                systValues.append(systVal)

            # Done reading, close the file
            systematicFile.close()

            # Make the last bin go up to 2500
            lowBinEdges.append(2500.)
            systValues.append(systValues[-1])

            # Turn the lists into arrays and build the 1D histogram
            lowBinEdgesArray = array('d', lowBinEdges)
            systValuesArray = array('d', systValues)
            histoName = systematicName + "_" + jetDef
            histo1D = TH1D(histoName + "_1D", histoName + "_1D",
                           len(lowBinEdges) - 1, lowBinEdgesArray)

            # Fill it from the file values
            for iBin in xrange(1, histo1D.GetNbinsX() + 1):
                histo1D.SetBinContent(iBin, systValues[iBin - 1])

            # Convert to a 2D provider-stlye histo
            histo = ProviderHistoHelpers.ConvertPtHistoToProviderHisto(
                histo1D, histoName)
            histo.SetDirectory(0)
            histos[jetDef][systematicName] = histo

        # EM has 10 stat parameters, LC has 11
        # So for EM algorithms, make an empty histo for stat 11
        if "EM" in jetDef:
            systematicName = SystematicNameDictionary['mjbstat11']
            histos[jetDef][
                systematicName] = ProviderHistoHelpers.MakeProviderHisto(
                    systematicName + "_" + jetDef)
            histos[jetDef][systematicName].SetDirectory(0)

    # Done, return dictionary of histos
    return histos