Example #1
0
def ReadBJESHistograms(dirName):
    if not dirName.endswith("/"):
        dirName = dirName + "/"

    # Run over each file (one per jet definition)
    # Skip files that don't start with b-JES and contain GSC
    histos = {}
    files = sorted(glob.glob(dirName+"b-JES*GSC.root"))
    for aFileName in files:
        # Determine the jet definition
        jetDef = ""
        for aFileDef,aJetDef in jetDefDict.iteritems():
            if aFileDef in aFileName:
                jetDef = aJetDef
                break
        if jetDef == "":
            print "Failed to determine jet definition for file:",aFileName
            return None
        histos[jetDef] = {}

        # Read in the histograms of interest from the file
        inFile = TFile(aFileName,"READ")
        for aName,aSystName in SystematicNameDictionary.iteritems():
            systematicName = aSystName + "_" + jetDef
            histo = inFile.Get(aName)
            if histo is None:
                print "Failed to get histogram:",systematicName
                return None

            histo2D = ProviderHistoHelpers.ConvertTGraphToProviderHisto(histo,systematicName)
            histo2D.SetDirectory(0)
            
            # It's currently response, set to difference from 1
            for ptBin in range(1,histo2D.GetNbinsX()+1):
                for etaBin in range(1,histo2D.GetNbinsY()+1):
                    histo2D.SetBinContent(ptBin,etaBin,1-histo2D.GetBinContent(ptBin,etaBin))
            
            histos[jetDef][aSystName] = histo2D

        # Done reading, close the file
        inFile.Close()

        # Make one quadrature sum histo of the desired components for now
        # The new provider design should make this irrelevant for multijet components too, but for now...
        for aDef,aHistoSet in histos.iteritems():
            # We want one histo of herwigpp and distorted_geometry (leave out FTFP_BERT)
            histos[aDef]['bJES'] = ProviderHistoHelpers.QuadratureSumHisto('bJES',[histos[aDef]['bJES_Generator'],histos[aDef]['bJES_Geometry']])
            histos[aDef]['bJES'].SetDirectory(0)
    
    return histos
Example #2
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 #3
0
def ReadCrossCalValidityHistograms(dirName):
    if not dirName.endswith("/"):
        dirName = dirName + "/"

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

        # Read in the histograms of interest from the file
        inFile = TFile(aFileName, "READ")
        for histName in inFile.GetKeyNames():
            if 'valid' not in histName:
                continue
            dictkey = histName.replace("valid_", "")
            systematicName = "valid_" + SystematicNameDictionary[
                dictkey] + "_" + jetDef
            histo = inFile.Get(histName)
            if histo is None:
                print "Failed to get histogram:", systematicName
                return None
            histo.SetName(systematicName + "_old")

            # Histo has different range, extend it
            histoNew = ProviderHistoHelpers.ExtendPtRangeOfHisto(
                histo, "valid_" + systematicName)
            histoSym = ProviderHistoHelpers.SymmetrizeHistoInEta(
                histoNew, "valid_" + systematicName)
            histoSym.SetDirectory(0)
            histos[jetDef][SystematicNameDictionary[dictkey]] = histoSym

        # Done reading, close the file
        inFile.Close()

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

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

        # Read in the histogram from the file
        inFile = TFile(aFileName, "READ")
        for aName, aSystName in SystematicNameDictionary.iteritems():
            systematicName = aSystName + "_" + jetDef
            histo = inFile.Get(aName)
            if histo is None:
                print "Failed to get histogram:", systematicName
                return None

            histoSym = ProviderHistoHelpers.SymmetrizeHistoInEta(
                histo, systematicName)
            histoSym.SetDirectory(0)
            histos[jetDef][aSystName] = histoSym

            # Also add a blank nonclosure histo for Pythia8
            # Also add a blank Closeby histo for both AFII and Pythia8
            etaBins = [
                min(ProviderHistoHelpers.GetDefaultEtaBins()), 0,
                max(ProviderHistoHelpers.GetDefaultEtaBins())
            ]
            histos[jetDef][
                "RelativeNonClosure_Pythia8"] = ProviderHistoHelpers.MakeProviderHisto(
                    "RelativeNonClosure_Pythia8",
                    ptBins=[15, 1000, 2500],
                    etaBins=etaBins)
            histos[jetDef]["RelativeNonClosure_Pythia8"].SetDirectory(0)
            histos[jetDef][
                "CloseBy_Pythia8"] = ProviderHistoHelpers.MakeProviderHisto(
                    "CloseBy_Pythia8",
                    ptBins=[15, 1000, 2500],
                    etaBins=etaBins)
            histos[jetDef]["CloseBy_Pythia8"].SetDirectory(0)
            histos[jetDef][
                "CloseBy_AFII"] = ProviderHistoHelpers.MakeProviderHisto(
                    "CloseBy_AFII", ptBins=[15, 1000, 2500], etaBins=etaBins)
            histos[jetDef]["CloseBy_AFII"].SetDirectory(0)

        # Done reading, close the file
        inFile.Close()

    return histos
Example #5
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 #6
0
def ReadEtaIntercalibrationHistograms(dirName):
    if not dirName.endswith("/"):
        dirName = dirName + "/"

    # Run over each file (one per jet definition)
    histos = {}
    files = sorted(glob.glob(dirName + "*.root"))
    for aFileName in files:
        # Determine the jet definition
        jetDef = ""
        for aFileDef, aJetDef in jetDefDict.iteritems():
            if aFileDef in aFileName:
                jetDef = aJetDef
                break
        if jetDef == "":
            print "Failed to determine jet definition for file:", aFileName
            return None
        if jetDef not in histos.keys():
            histos[jetDef] = {}

        # Read in the histograms of interest from the file
        inFile = TFile(aFileName, "READ")
        for aName in SystematicNames.keys():
            getsystematicName = aName + "_" + jetDef.replace("Topo", "")
            systematicName = SystematicNames[aName] + "_" + jetDef

            histo = inFile.Get(getsystematicName)
            if not histo:
                print "Failed to get histogram:", getsystematicName
                continue
            histo.SetName(systematicName + "_old")

            # Histo has different range, extend it
            histoNew = ProviderHistoHelpers.ExtendPtRangeOfHisto(
                histo, systematicName)
            histoNew.SetDirectory(0)
            histos[jetDef][SystematicNames[aName]] = histoNew

        # Done reading, close the file
        inFile.Close()

    return histos
Example #7
0
def ReadFlavourHistograms(dirName):
    if not dirName.endswith("/"):
        dirName = dirName + "/"
    
    # Run over each file (one per jet definition for LQ/Gluon and one per jet definition for HvP)
    histos = {}
    files = sorted(glob.glob(dirName+"*.root"))
    for aFileName in files:
        # Determine the jet definition
        jetDef = ""
        for aFileDef,aJetDef in jetDefDict.iteritems():
            if aFileDef in aFileName:
                jetDef = aJetDef
                break
        if jetDef == "":
            print "Failed to determine jet definition for file:",aFileName
            return None
        if jetDef not in histos:
            histos[jetDef] = {}

        # Read in the histograms from the file
        # Note that we want all the histograms, but with this scheme not everything in the dict is in each file
        # So instead loop over all histos in the file
        inFile = TFile(aFileName,"READ")
        for histName in inFile.GetKeyNames():
            systematicName = SystematicNameDictionary[histName]+"_"+jetDef
            histo = inFile.Get(histName)
            if histo is None:
                print "Failed to get histogram:",systematicName
                return None

            histoSym = ProviderHistoHelpers.SymmetrizeHistoInEta(histo,systematicName)
            histoSym.SetDirectory(0)
            histos[jetDef][SystematicNameDictionary[histName]] = histoSym
        # Done reading, close the file
        inFile.Close()

    return histos
Example #8
0
def ReadCrossCalHistograms(dirName,
                           scaleCrossCalVal=1.0,
                           freezeCrossCalAll=False,
                           freezeCrossCalBarrel=False,
                           freezeCrossCalCentral=False):
    if not dirName.endswith("/"):
        dirName = dirName + "/"

    # Run over each file (one per jet definition)
    histos = {}
    files = sorted(glob.glob(dirName + "*.root"))
    for aFileName in files:
        # Determine the jet definition
        jetDef = ""
        for aFileDef, aJetDef in jetDefDict.iteritems():
            if aFileDef in aFileName:
                jetDef = aJetDef
                break
            if jetDef == "":
                print "Failed to determine jet definition for file:", aFileName
                return None

        histos[jetDef] = {}

        # Read in the histograms of interest from the file
        inFile = TFile(aFileName, "READ")

        for histName in inFile.GetKeyNames():

            # Skip validity histograms.
            if 'valid' in histName:
                continue

            systematicName = SystematicNameDictionary[histName] + "_" + jetDef
            histo = inFile.Get(histName)
            if histo is None:
                print "Failed to get histogram:", systematicName
                return None
            histo.SetName(systematicName + "_old")

            if freezeCrossCalAll or freezeCrossCalBarrel or freezeCrossCalCentral:
                # Loop over pT bins.
                for xbin in range(histo.GetNbinsX() + 2):
                    # Loop out in eta.
                    freezeval = 0
                    for ybin in range(histo.GetNbinsY() + 2):
                        #print "Bin at pT ",histo.GetXaxis().GetBinCenter(xbin)," and [ylow,y,yhigh] = ", binYLow, histo.GetYaxis().GetBinCenter(ybin), binYHigh
                        # Store bin contents as we go out: last one is one we want as frozen value.
                        eta = abs(histo.GetYaxis().GetBinCenter(ybin))
                        if eta < 0.8:
                            freezeval = histo.GetBinContent(xbin, ybin)
                        else:
                            if (eta < 2.5 and
                                    freezeCrossCalBarrel) or freezeCrossCalAll:
                                histo.SetBinContent(
                                    xbin, ybin, freezeval * scaleCrossCalVal)
                            else:
                                histo.SetBinContent(xbin, ybin, 0)

            # Histo has different range, extend it
            histoNew = ProviderHistoHelpers.ExtendPtRangeOfHisto(
                histo, systematicName)
            histoSym = ProviderHistoHelpers.SymmetrizeHistoInEta(
                histoNew, systematicName)
            histoSym.SetDirectory(0)

            # If this is the 50 vs 25 ns histogram, we need to separate it by eta region.
            if '50nsVs25ns' in histName:
                CentralHist = histo.Clone()
                CentralHist.SetName("Central_holder")
                ForwardHist = histo.Clone()
                ForwardHist.SetName("Forward_holder")
                # Loop over pT bins.
                for xbin in range(histo.GetNbinsX() + 2):
                    # Loop out in eta.
                    freezeval = 0
                    for ybin in range(histo.GetNbinsY() + 2):
                        #print "Bin at pT ",histo.GetXaxis().GetBinCenter(xbin)," and [ylow,y,yhigh] = ", binYLow, histo.GetYaxis().GetBinCenter(ybin), binYHigh
                        # Store bin contents as we go out: last one is one we want as frozen value.
                        eta = abs(histo.GetYaxis().GetBinCenter(ybin))
                        if eta < 0.8:
                            freezeval = histo.GetBinContent(xbin, ybin)
                            ForwardHist.SetBinContent(xbin, ybin, 0.0)
                            CentralHist.SetBinContent(xbin, ybin, freezeval)
                        else:
                            ForwardHist.SetBinContent(xbin, ybin, freezeval)
                            CentralHist.SetBinContent(xbin, ybin, 0.0)

                # Now do symmetrization for these hists and write to file.
                NewCentral = ProviderHistoHelpers.ExtendPtRangeOfHisto(
                    CentralHist, systematicName + "holder")
                CentralSym = ProviderHistoHelpers.SymmetrizeHistoInEta(
                    NewCentral,
                    SystematicNameDictionary[histName] + "_Central_" + jetDef)
                CentralSym.SetDirectory(0)
                histos[jetDef][SystematicNameDictionary[histName] +
                               "_Central"] = CentralSym
                NewForward = ProviderHistoHelpers.ExtendPtRangeOfHisto(
                    ForwardHist, systematicName + "holder")
                ForwardSym = ProviderHistoHelpers.SymmetrizeHistoInEta(
                    NewForward,
                    SystematicNameDictionary[histName] + "_Forward_" + jetDef)
                ForwardSym.SetDirectory(0)
                histos[jetDef][SystematicNameDictionary[histName] +
                               "_Forward"] = ForwardSym

            # If this is the early data histogram, we need to deactivate it in the central region for 25 ns data.
            if 'EarlyData' in histName:
                ForwardHist = histo.Clone()
                ForwardHist.SetName("EarlyData_Forward_holder")
                # Loop over pT bins.
                for xbin in range(histo.GetNbinsX() + 2):
                    # Loop out in eta.
                    for ybin in range(histo.GetNbinsY() + 2):
                        #print "Bin at pT ",histo.GetXaxis().GetBinCenter(xbin)," and [ylow,y,yhigh] = ", binYLow, histo.GetYaxis().GetBinCenter(ybin), binYHigh
                        # Store bin contents as we go out: last one is one we want as frozen value.
                        eta = abs(histo.GetYaxis().GetBinCenter(ybin))
                        if eta < 0.8:
                            ForwardHist.SetBinContent(xbin, ybin, 0.0)

                # Now do symmetrization and write to file.
                NewForward = ProviderHistoHelpers.ExtendPtRangeOfHisto(
                    ForwardHist, systematicName + "holder")
                ForwardSym = ProviderHistoHelpers.SymmetrizeHistoInEta(
                    NewForward,
                    SystematicNameDictionary[histName] + "_Forward_" + jetDef)
                ForwardSym.SetDirectory(0)
                histos[jetDef][SystematicNameDictionary[histName] +
                               "_Forward"] = ForwardSym
                histos[jetDef][SystematicNameDictionary[histName]] = histoSym

            else:
                histos[jetDef][SystematicNameDictionary[histName]] = histoSym

        # Done reading, close the file
        inFile.Close()

    return histos
Example #9
0
def ReadNonClosureHistograms(dirName, freezepT=False):
    if dirName.endswith(".root"):
        files = [dirName]

    elif not dirName.endswith("/"):
        dirName = dirName + "/"
        files = sorted(glob.glob(dirName + "*.root"))

    # Run over each file (one per jet definition)
    histos = {}
    for aFileName in files:
        # Determine the jet definition
        jetDef = ""
        for aFileDef, aJetDef in jetDefDict.iteritems():
            if aFileDef in aFileName:
                jetDef = aJetDef
                break
        if jetDef == "":
            print "Failed to determine jet definition for file:", aFileName
            return None
        histos[jetDef] = {}

        # Read in the histogram from the file
        inFile = TFile(aFileName, "READ")
        for aName, aSystName in SystematicNameDictionary.iteritems():
            systematicName = aSystName + "_" + jetDef
            histo = inFile.Get(aName)
            if histo is None:
                print "Failed to get histogram:", systematicName
                return None

            if freezepT:
                # Loop over eta bins.
                for ybin in range(histo.GetNbinsY() + 2):
                    # Loop out in pT.
                    freezeval = 0
                    for xbin in range(histo.GetNbinsX() + 2):
                        inbin = histo.GetBinContent(xbin, ybin)
                        #print "Bin at pT ",histo.GetXaxis().GetBinCenter(xbin)," and [ylow,y,yhigh] = ", binYLow, histo.GetYaxis().GetBinCenter(ybin), binYHigh
                        # Store bin contents as we go out: last one is one we want as frozen value.
                        if inbin > -0.5:
                            freezeval = inbin
                        else:
                            histo.SetBinContent(xbin, ybin, freezeval)

            histoSym = ProviderHistoHelpers.SymmetrizeHistoInEta(
                histo, systematicName)
            histoSym.SetDirectory(0)
            histos[jetDef][aSystName] = histoSym

            # Also add a blank nonclosure histo for Pythia8
            # Also add a blank Closeby histo for both AFII and Pythia8
            etaBins = [
                min(ProviderHistoHelpers.GetDefaultEtaBins()), 0,
                max(ProviderHistoHelpers.GetDefaultEtaBins())
            ]
            histos[jetDef][
                "RelativeNonClosure_MC15"] = ProviderHistoHelpers.MakeProviderHisto(
                    "RelativeNonClosure_MC15",
                    ptBins=[15, 1000, 2500],
                    etaBins=etaBins)
            histos[jetDef]["RelativeNonClosure_MC15"].SetDirectory(0)
            histos[jetDef][
                "CloseBy_Pythia8"] = ProviderHistoHelpers.MakeProviderHisto(
                    "CloseBy_MC15", ptBins=[15, 1000, 2500], etaBins=etaBins)
            histos[jetDef]["CloseBy_Pythia8"].SetDirectory(0)
            histos[jetDef][
                "CloseBy_AFII"] = ProviderHistoHelpers.MakeProviderHisto(
                    "CloseBy_AFII", ptBins=[15, 1000, 2500], etaBins=etaBins)
            histos[jetDef]["CloseBy_AFII"].SetDirectory(0)

        # Done reading, close the file
        inFile.Close()

    return histos
Example #10
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