Ejemplo n.º 1
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
Ejemplo n.º 2
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