コード例 #1
0
    def lheVarHistsForProcess(self, group, processName, chan, varName):
        weighthist_name = self.weightHistName(chan, processName)
        weightHist = group.FindObject(weighthist_name)
        if not weightHist:
            raise ValueError("Failed to find %s. Skipping" % weighthist_name)
        var = self.theoryVariations[processName][varName]
        if not self.isUnrolledFit:
            hists, name = HistTools.getLHEWeightHists(weightHist,
                                                      var['entries'], "",
                                                      varName)
        else:
            hists, name = HistTools.getTransformed3DLHEHists(
                weightHist, HistTools.makeUnrolledHist,
                [self.unrolledBinsX, self.unrolledBinsY], var['entries'], "",
                varName)

        # In this case, should be central, varCentral, varUp, varDown
        if len(var['entries']) == 4:
            for i in range(hists[0].GetNbinsX() + 1):
                central_ratio = hists[0].GetBinContent(i) / hists[
                    1].GetBinContent(i) if hists[1].GetBinContent(i) > 0 else 1
                hists[2].SetBinContent(
                    i, hists[2].GetBinContent(i) * central_ratio)
                hists[3].SetBinContent(
                    i, hists[3].GetBinContent(i) * central_ratio)
            hists = [hists[2], hists[3]]

        hists[0] = HistTools.rebinHist(hists[0], name, self.rebin)
        hists[1] = HistTools.rebinHist(hists[1], name.replace("Up", "Down"),
                                       self.rebin)
        return hists
コード例 #2
0
 def scaleHistsForProcess(self, group, processName, chan, expandedTheory, append=""):
     weightHist = group.FindObject(self.weightHistName(chan, processName, append))
     if not weightHist:
         raise ValueError("Failed to find %s. Skipping" % self.weightHistName(chan, processName, append))
     if 'scale' not in self.theoryVariations[processName]:
         return []
     scaleVars = self.theoryVariations[processName]['scale']
     
     scaleHists = HistTools.getScaleHists(weightHist, processName, self.rebin, 
             entries=scaleVars['entries'], 
             exclude=scaleVars['exclude'], 
             central=scaleVars['central']) if not self.isUnrolledFit else \
         HistTools.getTransformed3DScaleHists(weightHist, HistTools.makeUnrolledHist,
                 [self.unrolledBinsX, self.unrolledBinsY], processName,
             entries=scaleVars['entries'], 
             exclude=scaleVars['exclude'])
     if expandedTheory:
         name = processName if not self.correlateScaleUnc else ""
         expandedScaleHists = HistTools.getExpandedScaleHists(weightHist, name, self.rebin, 
                 entries=scaleVars['entries'], 
                 pairs=scaleVars['groups'], 
             ) if not self.isUnrolledFit else \
             HistTools.getTransformed3DExpandedScaleHists(weightHist, 
                     HistTools.makeUnrolledHist,
                 [self.unrolledBinsX, self.unrolledBinsY], name,
                 entries=scaleVars['entries'], 
                 pairs=scaleVars['groups'], 
             )
             
         scaleHists.extend(expandedScaleHists)
     return scaleHists
コード例 #3
0
    def GetMousePosition(self,position,physical=False):
	positionFraction = float(position[0]-self.GetPosition()[0])/float(self.GetSize()[0])
	#positionPhysical = (self.xmax-self.xmin)*(positionFraction-self.fractionBinStart)/(self.fractionBinEnd-self.fractionBinStart) + self.xmin
	positionPhysical = -1.
	if self.isLogX:
	    positionPhysical = HistTools.convertLogFractionToPhysical(positionFraction,self.fractionBinStart,self.fractionBinEnd,self.xmin,self.xmax)
	else:
	    positionPhysical = HistTools.convertLinearFractionToPhysical(positionFraction,self.fractionBinStart,self.fractionBinEnd,self.xmin,self.xmax)
        if self.doCutAtBinEdge:
	    positionFractionTmp = positionFraction
	    positionFraction = self.getLowBinEdge(positionFractionTmp,True)
	    positionPhysical = self.getLowBinEdge(positionFractionTmp)
	if physical: return positionPhysical
	return positionFraction
コード例 #4
0
def set_DrawDatum(xarray,
                  key,
                  nbins=100,
                  weights=[],
                  x_range=[-1, -1],
                  xbins=[],
                  color="black",
                  linestyle="-"):
    """
    Make DrawDatum object for 1D histogram

    Parameters
    ----------
    xarray : numpy array
        array of values that you want to make a histogram

    key : string
        name or description of the histogram, used in the label in legend

    nbins : int
        number of bins for the histogram

    weights : numpy array
        weight parameter for histogram

    x_range : list
        range of x axis 

    xbins : numpy array
        if you want to give your own xbins, set it.

    color : string
        name of color for drawing 
 
    linestyle : string
        line style marker

    Returns
    -------
    datum : DrawDatum object


    """
    datum = DrawDatum()
    datum.title = key
    datum.val, bins, datum.w2s = HT.make_1D_hist(xarray,
                                                 nbins=nbins,
                                                 weights=weights,
                                                 x_range=x_range,
                                                 xbins=xbins)
    datum.xbinedges = bins
    datum.xbins = 0.5 * (bins[1:] + bins[:-1])
    datum.color = color
    datum.linestyle = linestyle
    if len(weights) == 0:
        datum.xmean = float(sum(xarray)) / len(xarray)
    else:
        datum.xmean = sum(xarray * weights) / sum(weights)
    return datum
コード例 #5
0
    def loadHistsForProcess(self,
                            processName,
                            scaleNorm=1,
                            expandedTheory=True):
        plotsToRead = self.listOfHistsByProcess(processName,
                                                nameReplace=("unrolled", "2D"))

        group = HistTools.makeCompositeHists(
            self.inputFile,
            processName, {
                proc: self.crossSectionMap[proc]
                for proc in self.processes[processName]
            },
            self.lumi,
            plotsToRead,
            rebin=self.rebin,
            overflow=False)

        if self.isUnrolledFit:
            for hist in group:
                if not "TH2" in hist.ClassName():
                    continue
                hist = HistTools.makeUnrolledHist(hist, self.unrolledBinsX,
                                                  self.unrolledBinsY)
                histName = hist.GetName()
                group.Add(hist)

        fitVariable = self.getFitVariable(processName)
        #TODO:Make optional
        processedHists = []
        for chan in self.channels:
            histName = "_".join([fitVariable, chan])
            hist = group.FindObject(histName)
            if not hist:
                logging.warning("Failed to produce hist %s for process %s" %
                                (histName, processName))
                continue
            if self.removeZeros and "data" not in processName.lower():
                HistTools.removeZeros(hist)
            if self.addOverflow:
                HistTools.addOverflow(hist)
            processedHists.append(histName)
            self.yields[chan].update({
                processName:
                round(hist.Integral(), 3) if hist.Integral() > 0 else 0.0001
            })

            if chan == self.channels[0]:
                self.yields["all"][processName] = self.yields[chan][
                    processName]
            else:
                self.yields["all"][processName] += self.yields[chan][
                    processName]

            if processName in self.perbinVariations:
                for varName, var, corr in self.perbinVariations[processName]:
                    map(
                        lambda x: group.Add(x),
                        self.perBinVariationHists(hist, varName, var, corr,
                                                  chan == self.channels[0]))

            scaleHists = []
            if processName in self.theoryVariations:
                theoryVars = self.theoryVariations[processName]
                try:
                    scaleHists.extend(
                        self.scaleHistsForProcess(group, processName, chan,
                                                  expandedTheory))
                    if 'theoryBasedVars' in theoryVars['scale']:
                        for theoryBasedVar in theoryVars['scale'][
                                'theoryBasedVars']:
                            scaleHists.extend(
                                self.scaleHistsForProcess(
                                    group, processName, chan, expandedTheory,
                                    theoryBasedVar))
                except ValueError as e:
                    logging.warning(e)
                    continue

                pdfHists = []
                pdfVars = filter(lambda x: 'pdf' in x, theoryVars.keys())
                weightHist = group.FindObject(
                    self.weightHistName(chan, processName))
                for var in pdfVars:
                    pdfVar = theoryVars[var]
                    pdfType = "MC"
                    if "hessian" in pdfVar['combine']:
                        pdfType = "Hessian" if "assym" not in pdfVar[
                            'combine'] else "AssymHessian"

                    pdfFunction = "get%sPDFVarHists" % pdfType
                    args = [
                        weightHist, pdfVar['entries'], processName, self.rebin,
                        pdfVar['central'], pdfVar['name']
                    ]
                    if self.isUnrolledFit:
                        pdfFunction = pdfFunction.replace(
                            "get", "getTransformed3D")
                        args = args[0:1] + [
                            HistTools.makeUnrolledHist,
                            [self.unrolledBinsX, self.unrolledBinsY]
                        ] + args[1:]
                    updatePdfs = getattr(HistTools, pdfFunction)(*args)
                    pdfHists += updatePdfs

                    if expandedTheory and pdfVar['name'] == 'NNPDF31':
                        args.pop(len(args) - 1)
                        pdfFunction = HistTools.getAllSymHessianHists if not self.isUnrolledFit else HistTools.getTransformed3DAllSymHessianHists
                        allPdfHists = pdfFunction(*args)
                        pdfHists.extend(allPdfHists)

                        if not self.isUnrolledFit:
                            cenHist, _ = HistTools.getLHEWeightHists(
                                weightHist, pdfVar['entries'][:1], processName,
                                "pdf", self.rebin)
                        else:
                            cenHist = HistTools.getAllTransformed3DHists(
                                weightHist, HistTools.makeUnrolledHist,
                                [self.unrolledBinsX, self.unrolledBinsY],
                                processName, pdfVar['entries'][:1])
                        if len(cenHist) and cenHist[0]:
                            pdfHists.append(cenHist[0].Clone())
                group.extend(scaleHists + pdfHists)

                if chan == self.channels[0]:
                    theoryVarLabels = []
                    for h in group:
                        fitvar = self.getFitVariable(processName)
                        theoryVarLabels.extend(
                            re.findall("_".join([fitvar, "(.*pdf.*)", chan]),
                                       h.GetName()))
                        theoryVarLabels.extend(
                            re.findall(
                                "_".join([fitvar, "(.*QCDscale.*)", chan]),
                                h.GetName()))
                    self.variations[processName].extend(set(theoryVarLabels))

        if self.addOverflow:
            map(HistTools.addOverflow,
                filter(lambda x: (x.GetName() not in processedHists), group))
        if self.removeZeros and "data" not in group.GetName().lower():
            map(HistTools.removeZeros,
                filter(lambda x: (x.GetName() not in processedHists), group))
        #TODO: You may want to combine channels before removing zeros
        if self.channelsToCombine.keys():
            self.combineChannels(group, processName)

        self.histData[processName] = group
コード例 #6
0
    def Configure(self):
    	nbins = 0
	xmin = -1 
	xmax = -1
	isLogX = False
        if self.name=="nbjets":
	    nbins = 4
	    xmin = -.5
	    xmax = 3.5
	    isLogX = False
        elif self.name=="njets":
	    nbins = 10
	    xmin = -.5
	    xmax = 9.5
	    isLogX = False
        elif self.name=="pt":
	    nbins = 30
	    xmin = 20
	    xmax = 1000
	    isLogX = True
        elif self.name=="phi":
	    nbins = 16
	    xmin = -math.pi
	    xmax = math.pi
	    isLogX = False
        elif self.name=="eta":
	    nbins = 15
	    xmin = -2.5
	    xmax = 2.5
	    isLogX = False
        elif self.name=="charge":
	    nbins = 7
	    xmin = -3.5
	    xmax = 3.5
	    isLogX = False
        elif self.name=="masselel":
	    nbins = 20
	    xmin = 1.1876
	    xmax = 201.1876
	    isLogX = False
        elif self.name=="masselmu":
	    nbins = 20
	    xmin = 1.1876
	    xmax = 201.1876
	    isLogX = False
        elif self.name=="massmumu":
	    nbins = 20
	    xmin = 1.1876
	    xmax = 201.1876
	    isLogX = False
        elif self.name=="masssfos":
	    nbins = 20
	    xmin = 1.1876
	    xmax = 201.1876
	    isLogX = False
        elif self.name=="met":
	    nbins = 30
	    xmin = 20
	    xmax = 1000
	    isLogX = True
        elif self.name=="metphi":
	    nbins = 16
	    xmin = -math.pi
	    xmax = math.pi
	    isLogX = False
        elif self.name=="metsumet":
	    nbins = 30
	    xmin = 100.
	    xmax = 10000.
	    isLogX = True
        elif self.name=="nsfos":
	    nbins = 3
	    xmin = -.5
	    xmax = 2.5
	    isLogX = False
        elif self.name=="nmuons":
	    nbins = 4
	    xmin = -.5
	    xmax = 3.5
	    isLogX = False
        elif self.name=="mt":
	    nbins = 30
	    xmin = 20
	    xmax = 1000
	    isLogX = True
        elif self.name=="deltaphi":
	    nbins = 16
	    xmin = -math.pi
	    xmax = math.pi
	    isLogX = False
	else:
	    print "Error! Couldn't configure feature with name",self.name
	    exit(2)
	
	self.xmin = xmin
	self.xmax = xmax
	self.nbins = nbins
	self.isLogX = isLogX
	if self.isLogX: 
	    self.binsPhysical = HistTools.getLogBins(nbins,xmin,xmax)
	    self.binsFraction = HistTools.getLogBins(nbins,self.fractionBinStart,self.fractionBinEnd)
	else:
	    self.binsPhysical = HistTools.getLinearBins(nbins,xmin,xmax)
	    self.binsFraction = HistTools.getLinearBins(nbins,self.fractionBinStart,self.fractionBinEnd)
コード例 #7
0
    def loadHistsForProcess(self, processName, scaleNorm=1, expandedTheory=True):
        plotsToRead = self.listOfHistsByProcess(processName)

        group = HistTools.makeCompositeHists(self.inputFile, processName, 
                    {proc : self.crossSectionMap[proc] for proc in self.processes[processName]}, 
                    self.lumi, plotsToRead, rebin=self.rebin, overflow=False)

        fitVariable = self.getFitVariable(processName)
        #TODO:Make optional
        processedHists = []
        for chan in self.channels:
            histName = "_".join([fitVariable, chan]) if chan != "all" else fitVariable
            hist = group.FindObject(histName)
            if not hist:
                raise RuntimeError("Failed to produce hist %s for process %s" % (histName, processName))
            #TODO: Make optional
            if "data" not in processName.lower():
                HistTools.removeZeros(hist)
            HistTools.addOverflow(hist)
            processedHists.append(histName)
            self.yields[chan].update({processName : round(hist.Integral(), 3) if hist.Integral() > 0 else 0.0001})

            if chan == self.channels[0]:
                self.yields["all"][processName] = self.yields[chan][processName]
            else:
                self.yields["all"][processName] += self.yields[chan][processName]

            if processName in self.theoryVariations:
                weightHist = group.FindObject(self.weightHistName(chan, processName))
                if not weightHist:
                    logging.warning("Failed to find %s. Skipping" % self.weightHistName(chan, processName))
                    continue
                theoryVars = self.theoryVariations[processName]
                scaleHists = HistTools.getScaleHists(weightHist, processName, self.rebin, 
                    entries=theoryVars['scale']['entries'], 
                    central=(theoryVars['scale']['central'] if 'scale' in theoryVars else -1))
                if expandedTheory:
                    expandedScaleHists = HistTools.getExpandedScaleHists(weightHist, processName, self.rebin, 
                        entries=theoryVars['scale']['entries'], 
                        central=(theoryVars['scale']['central'] if 'scale' in theoryVars else -1),
                        )
                    scaleHists.extend(expandedScaleHists)

                pdfHists = []
                pdfVars = filter(lambda x: 'pdf' in x, theoryVars.keys())
                for var in pdfVars: 
                    pdfVar = theoryVars[var]
                    
                    pdfType = "SymmMC"
                    if "hessian" in pdfVar['combine']:
                        pdfType = "Hessian" if "assym" not in pdfVar['combine'] else "AssymHessian"

                    pdfFunction = "get%sPDFVariationHists" % pdfType
                    pdfHists += getattr(HistTools, pdfFunction)(weightHist, pdfVar['entries'], processName, 
                            self.rebin, central=pdfVar['central'],
                            pdfName=pdfVar['name'])
                    if expandedTheory and "hessian" in pdfVar['combine']:
                        allPdfHists = HistTools.getAllSymmetricHessianVariationHists(weightHist, pdfVar['entries'], processName, 
                            self.rebin, central=pdfVar['central'])
                        pdfHists.extend(allPdfHists)
                        cenHist, _ = HistTools.getLHEWeightHists(weightHist, pdfVar['entries'][:1], processName, "pdf", self.rebin)
                        if len(cenHist) and cenHist[0]:
                            pdfHists.append(cenHist[0].Clone())
                group.extend(scaleHists+pdfHists)

                if chan == self.channels[0]:
                    theoryVarLabels = []
                    for h in group:
                        theoryVarLabels.extend(re.findall("_".join([self.fitVariable, "(.*)", chan]), h.GetName()))
                    self.variations[processName].extend(set(theoryVarLabels))

        #TODO: Make optional
        map(HistTools.addOverflow, filter(lambda x: (x.GetName() not in processedHists), group))
        if "data" not in group.GetName().lower():
            map(HistTools.removeZeros, filter(lambda x: (x.GetName() not in processedHists), group))
        #TODO: You may want to combine channels before removing zeros
        self.combineChannels(group, processName)

        self.histData[processName] = group
コード例 #8
0
def set_DrawDatum2D(xarray,
                    yarray,
                    key,
                    nxbins,
                    nybins,
                    weights=[],
                    x_range=[-1, -1],
                    y_range=[-1, -1]):
    """
    Make DrawDatum object for 2D histogram

    Parameters
    ----------
    xarray : numpy array
        array of values for xaxis that you want to make a histogram

    yarray : numpy array
        array of values for yaxis that you want to make a histogram

    key : string
        name or description of the histogram, used in the label in legend

    nxbins : int
        number of bins for xaxis for the histogram

    nybins : int
        number of bins for yaxis for the histogram

    weights : numpy array
        weight parameter for histogram

    x_range : list 
        range of x axis 

    y_range : list
        range of y axis 

    Returns
    -------
    datum : DrawDatum object

    """
    datum = DrawDatum()
    datum.title = key
    datum.xmeshgrid, datum.ymeshgrid, datum.val, datum.w2s, xbins, ybins = HT.make_2D_hist(
        xarray,
        yarray,
        nxbins,
        nybins,
        weights=weights,
        x_range=x_range,
        y_range=y_range)

    datum.xbinedges = datum.xmeshgrid[:, 0]
    datum.xbins = 0.5 * (datum.xbinedges[1:] + datum.xbinedges[:-1])
    datum.ybinedges = datum.ymeshgrid[0]
    datum.ybins = 0.5 * (datum.ybinedges[1:] + datum.ybinedges[:-1])
    if len(weights) == 0:
        datum.xmean = float(sum(xarray)) / len(xarray)
    else:
        datum.xmean = sum(xarray * weights) / sum(weights)
    return datum