Exemplo n.º 1
0
estimate.initCache(setup.defaultCacheDir())

# Accounting for
leptonPtCutVar = "LeptonTightInvIsoNoSieie0_pt"
leptonEtaCutVar = "abs(LeptonTightInvIsoNoSieie0_eta+LeptonTightInvIsoNoSieie0_deltaEtaSC)"
QCDTF_updates_2J = copy.deepcopy(QCDTF_updates)

key = (data_sample.name, "AR", args.variable, "_".join(map(str, args.binning)),
       data_sample.weightString, data_sample.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist = dirDB.get(key).Clone("data")
else:
    dataHist = data_sample.get1DHistoFromDraw(args.variable,
                                              binning=args.binning,
                                              selectionString=selection)
    dirDB.add(key, dataHist.Clone("data"), overwrite=True)

dataHist_SB = dataHist.Clone("data_SB")
dataHist_SB.Scale(0)

genCat = [None]
if args.photonCat:
    hists = {}
    genCat = [
        "noChgIsoNoSieiephotoncat0", "noChgIsoNoSieiephotoncat1",
        "noChgIsoNoSieiephotoncat2", "noChgIsoNoSieiephotoncat3",
        "noChgIsoNoSieiephotoncat4"
    ]
    catSettings = {
        "noChgIsoNoSieiephotoncat0": {
            "texName": "gen #gamma",
Exemplo n.º 2
0
data_sample_4p.setSelectionString([
    filterCutData, "reweightHEM>0",
    cutInterpreter.cutString(args.mode), selection
])
data_sample_4p.setWeightString("weight")

key = (data_sample_4p.name, "4p", args.variable,
       "_".join(map(str, args.binning)), data_sample_4p.weightString,
       data_sample_4p.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist_4p = dirDB.get(key).Clone("4p")
else:
    dataHist_4p = data_sample_4p.get1DHistoFromDraw(args.variable,
                                                    binning=args.binning)
    dirDB.add(key, dataHist_4p.Clone("4p"), overwrite=True)

key = (data_sample_3.name, "3", args.variable, "_".join(map(str,
                                                            args.binning)),
       data_sample_3.weightString, data_sample_3.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist_3 = dirDB.get(key).Clone("3")
else:
    dataHist_3 = data_sample_3.get1DHistoFromDraw(args.variable,
                                                  binning=args.binning)
    dirDB.add(key, dataHist_3.Clone("3"), overwrite=True)

key = (data_sample_2.name, "2", args.variable, "_".join(map(str,
                                                            args.binning)),
       data_sample_2.weightString, data_sample_2.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
Exemplo n.º 3
0
class DataObservation():
    def __init__(self, name, process, cacheDir=None):
        self.name = name
        self.process = process
        self.initCache(cacheDir)

    def initCache(self, cacheDir="dataObs"):
        if cacheDir:
            self.cacheDir = os.path.join(cache_directory, cacheDir)
            try:
                os.makedirs(cacheDir)
            except:
                pass

            cacheDirName = os.path.join(cacheDir, self.name)
            self.cache = MergingDirDB(cacheDirName)
            if not self.cache: raise

        else:
            self.cache = None

    def uniqueKey(self, region, channel, setup):
        ## this is used in MCBasedEstimate
        if hasattr(setup, "blinding"):
            return str(region), channel, json.dumps(
                setup.sys, sort_keys=True), json.dumps(
                    setup.parameters,
                    sort_keys=True), json.dumps(setup.lumi,
                                                sort_keys=True), setup.blinding
        else:
            return str(region), channel, json.dumps(
                setup.sys, sort_keys=True), json.dumps(
                    setup.parameters,
                    sort_keys=True), json.dumps(setup.lumi, sort_keys=True)

    # alias for cachedObservation to make it easier to call the same function as for the mc"s
    def cachedEstimate(self,
                       region,
                       channel,
                       setup,
                       signalAddon=None,
                       save=True,
                       overwrite=False,
                       checkOnly=False):
        return self.cachedObservation(region,
                                      channel,
                                      setup,
                                      overwrite=overwrite,
                                      checkOnly=checkOnly)

    def cachedObservation(self,
                          region,
                          channel,
                          setup,
                          save=True,
                          overwrite=False,
                          checkOnly=False):
        key = self.uniqueKey(region, channel, setup)
        if (self.cache and self.cache.contains(key)) and not overwrite:
            res = self.cache.get(key)
            logger.debug("Loading cached %s result for %r : %r" %
                         (self.name, key, res))
        elif self.cache and not checkOnly:
            res = self.observation(region, channel, setup, overwrite)
            _res = self.cache.add(key, res, overwrite=True)
            logger.debug("Adding cached %s result for %r" % (self.name, key))
        elif not checkOnly:
            res = self.observation(region, channel, setup, overwrite)
        else:
            res = u_float(-1, 0)
        return res if res >= 0 or checkOnly else u_float(0, 0)

    def writeToCache(self,
                     region,
                     channel,
                     setup,
                     value,
                     signalAddon=None,
                     save=True,
                     overwrite=False,
                     checkOnly=False):
        key = self.uniqueKey(region, channel, setup)
        if (self.cache and self.cache.contains(key)) and not overwrite:
            res = self.cache.get(key)
            if res.val != value.val:
                print "Warning, caches estimate not equal to input value: have %s, got %s" % (
                    res, value)
            logger.debug("Loading cached %s result for %r : %r" %
                         (self.name, key, res))
        elif self.cache and not checkOnly:
            _res = self.cache.add(key, value, overwrite=True)
            res = value
            logger.debug("Adding cached %s result for %r" % (self.name, key))
        else:
            res = u_float(-1, 0)
        return res if res >= 0 or checkOnly else u_float(0, 0)

    def observation(self, region, channel, setup, overwrite):

        if setup.nJet == "3p":
            setup4p = setup.sysClone(parameters={"nJet": (4, -1)})
            setup3 = setup.sysClone(parameters={"nJet": (3, 3)})
            return sum([
                self.cachedEstimate(region, channel, s, overwrite=overwrite)
                for s in [setup3, setup4p]
            ])

        if channel == "all":
            return sum([
                self.cachedEstimate(region, c, setup, overwrite=overwrite)
                for c in lepChannels
            ])

        elif channel == "SFtight":
            return sum([
                self.cachedEstimate(region, c, setup, overwrite=overwrite)
                for c in dilepChannels
            ])

        else:
            preSelection = setup.preselection("Data", channel=channel)
            #            cut = "&&".join([region.cutString(setup.sys['selectionModifier']), preSelection['cut']])
            cut = "&&".join([region.cutString(), preSelection['cut']])

            logger.debug("Using cut %s" % cut)

            weight = preSelection['weightStr']
            if hasattr(setup, "blinding") and setup.blinding:
                weight += "*" + setup.blinding

            return u_float(**self.process.getYieldFromDraw(
                selectionString=cut, weightString=weight))
Exemplo n.º 4
0
    filterCutData, "reweightHEM>0",
    cutInterpreter.cutString(high + "-" + args.mode),
    copy.deepcopy(dataselection)
])
data_high.setWeightString("weight")

key = (mc_high.name, "high", args.variable, "_".join(map(str, bins)),
       mc_high.weightString, mc_high.selectionString)
if dirDB.contains(key) and not args.overwrite:
    mcHist_high = dirDB.get(key).Clone("high")
else:
    mcHist_high = mc_high.get1DHistoFromDraw(args.variable,
                                             binning=bins,
                                             binningIsExplicit=True,
                                             addOverFlowBin="upper")
    dirDB.add(key, mcHist_high.Clone("high"), overwrite=True)

key = (mc_low.name, "low", args.variable, "_".join(map(str, bins)),
       mc_low.weightString, mc_low.selectionString)
if dirDB.contains(key) and not args.overwrite:
    mcHist_low = dirDB.get(key).Clone("low")
else:
    mcHist_low = mc_low.get1DHistoFromDraw(args.variable,
                                           binning=bins,
                                           binningIsExplicit=True,
                                           addOverFlowBin="upper")
    dirDB.add(key, mcHist_low.Clone("low"), overwrite=True)

key = (data_high.name, "high", args.variable, "_".join(map(str, bins)),
       data_high.weightString, data_high.selectionString)
if dirDB.contains(key) and not args.overwrite:
Exemplo n.º 5
0
])
dataE.setWeightString("weight")

dataF.setSelectionString([
    filterCutData, "reweightHEM>0",
    cutInterpreter.cutString(args.mode), selection
])
dataF.setWeightString("weight")

key = (dataB.name, "B", args.variable, "_".join(map(str, args.binning)),
       dataB.weightString, dataB.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHistB = dirDB.get(key).Clone("B")
else:
    dataHistB = dataB.get1DHistoFromDraw(args.variable, binning=args.binning)
    dirDB.add(key, dataHistB.Clone("B"), overwrite=True)

key = (dataC.name, "C", args.variable, "_".join(map(str, args.binning)),
       dataC.weightString, dataC.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHistC = dirDB.get(key).Clone("C")
else:
    dataHistC = dataC.get1DHistoFromDraw(args.variable, binning=args.binning)
    dirDB.add(key, dataHistC.Clone("C"), overwrite=True)

key = (dataD.name, "D", args.variable, "_".join(map(str, args.binning)),
       dataD.weightString, dataD.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHistD = dirDB.get(key).Clone("D")
else:
    dataHistD = dataD.get1DHistoFromDraw(args.variable, binning=args.binning)
Exemplo n.º 6
0
    ws, misIDSF_val[2016].val)
ws17 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2017))" % (
    ws, misIDSF_val[2017].val)
ws18 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2018))" % (
    ws, misIDSF_val[2018].val)
weightStringAR = ws + ws16 + ws17 + ws18

key = (data_sample.name, "AR", args.variable, "_".join(map(str, args.binning)),
       data_sample.weightString, data_sample.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist = dirDB.get(key).Clone("data")
else:
    dataHist = data_sample.get1DHistoFromDraw(args.variable,
                                              binning=args.binning,
                                              selectionString=selection)
    dirDB.add(key, dataHist.Clone("data"), overwrite=True)

for s in mc:
    s.setWeightString(weightStringAR + "*" + sampleWeight)
    key = (s.name, "AR", args.variable, "_".join(map(str, args.binning)),
           s.weightString, s.selectionString, selection)
    if dirDB.contains(key) and not args.overwrite:
        s.hist = copy.deepcopy(dirDB.get(key).Clone(s.name))
    else:
        s.hist = s.get1DHistoFromDraw(args.variable,
                                      binning=args.binning,
                                      selectionString=selection)
        dirDB.add(key, s.hist.Clone(s.name), overwrite=True)

    s.hist.style = styles.lineStyle(ROOT.kRed, width=2, errors=True)
    s.hist.legendText = "MC (all)"
selection = setup.selection( "MC", channel="all", **setup.defaultParameters() )["prefix"]
selection = cutInterpreter.cutString( selection )
selection += "&&triggered==1"
print selection
if args.addCut:
    selection += "&&" + cutInterpreter.cutString( args.addCut )
print( "Using selection string: %s"%selection )


key = (data2018.name, "18", args.variable, "_".join(map(str,args.binning)), data2018.weightString, data2018.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist2018 = dirDB.get(key).Clone("edataAR18")
else:
    dataHist2018 = data2018.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection )
    dirDB.add(key, dataHist2018.Clone("edataAR18"), overwrite=True)

key = (data2017.name, "17", args.variable, "_".join(map(str,args.binning)), data2017.weightString, data2017.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist2017 = dirDB.get(key).Clone("edataAR17")
else:
    dataHist2017 = data2017.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection )
    dirDB.add(key, dataHist2017.Clone("edataAR17"), overwrite=True)

key = (data2016.name, "16", args.variable, "_".join(map(str,args.binning)), data2016.weightString, data2016.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist2016 = dirDB.get(key).Clone("mudataAR16")
else:
    dataHist2016 = data2016.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection )
    dirDB.add(key, dataHist2016.Clone("mudataAR17"), overwrite=True)
Exemplo n.º 8
0
lumiString = "(35.92*(year==2016)+41.53*(year==2017)+59.74*(year==2018))"
ws   = "(%s*weight*reweightHEM*reweightTrigger*reweightL1Prefire*reweightPU*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightBTag_SF)"%lumiString
ws16 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2016))" %(ws, misIDSF_val[2016].val)
ws17 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2017))" %(ws, misIDSF_val[2017].val)
ws18 = "+(%s*(PhotonNoChgIsoNoSieie0_photonCatMagic==2)*(%f-1)*(year==2018))" %(ws, misIDSF_val[2018].val)
if not addSF:
    weightStringAR  = ws
else:
    weightStringAR = ws + ws16 + ws17 + ws18

key = (data_sample.name, "AR", args.variable, "_".join(map(str,args.binning)), data_sample.weightString, data_sample.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist = dirDB.get(key).Clone("dataAR")
else:
    dataHist = data_sample.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection, addOverFlowBin="upper" )
    dirDB.add(key, dataHist.Clone("dataAR"), overwrite=True)

for s in mc:
    s.setWeightString( weightStringAR + "*" + sampleWeight )
    key = (s.name, "AR", args.variable, "_".join(map(str,args.binning)), s.weightString, s.selectionString, selection)
    if dirDB.contains(key) and not args.overwrite:
        s.hist = copy.deepcopy(dirDB.get(key).Clone(s.name+"AR"))
    else:
        s.hist = s.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection, addOverFlowBin="upper" )
        dirDB.add(key, s.hist.Clone("%s_AR"%s.name), overwrite=True)

    if addSF:
            if "DY" in s.name:
                s.hist.Scale(DYSF_val[args.year].val)
            elif "ZG" in s.name:# and njets < 4:
                s.hist.Scale(ZGSF_val[args.year].val)
Exemplo n.º 9
0
invVariable = replaceVariable[
    args.variable] if args.variable in replaceVariable.keys(
    ) else args.variable

# histos
key = (data_sample.name, "Binv", args.variable,
       "_".join(map(str, args.binning)), data_sample.weightString,
       data_sample.selectionString, preSelection)
if dirDB.contains(key) and not args.overwrite:
    dataHist_SB = dirDB.get(key)
else:
    dataHist_SB = data_sample.get1DHistoFromDraw(invVariable,
                                                 binning=args.binning,
                                                 selectionString=preSelection,
                                                 addOverFlowBin="upper")
    dirDB.add(key, dataHist_SB, overwrite=True)

key = (data_sample.name, "AR", args.variable, "_".join(map(str, args.binning)),
       data_sample.weightString, data_sample.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist = dirDB.get(key)
else:
    dataHist = data_sample.get1DHistoFromDraw(args.variable,
                                              binning=args.binning,
                                              selectionString=selection,
                                              addOverFlowBin="upper")
    dirDB.add(key, dataHist, overwrite=True)

dataHist_SB.style = styles.errorStyle(ROOT.kBlack)
dataHist_SB.legendText = "Observed (%s)" % args.mode.replace("mu", "#mu")
dataHist.style = styles.errorStyle(ROOT.kBlack)
Exemplo n.º 10
0
selection = setup.selection( "MC", channel="all", **setup.defaultParameters() )["prefix"]
selection = cutInterpreter.cutString( selection )
selection += "&&pTStitching==1&&triggered==1"
print selection
if args.addCut:
    selection += "&&" + cutInterpreter.cutString( args.addCut )
print( "Using selection string: %s"%selection )


key = (mc18.name, "18", args.variable, "_".join(map(str,args.binning)), mc18.weightString, mc18.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    mcHist18 = dirDB.get(key).Clone("18")
else:
    mcHist18 = mc18.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection )
    dirDB.add(key, mcHist18.Clone("18"), overwrite=True)

key = (mc17.name, "17", args.variable, "_".join(map(str,args.binning)), mc17.weightString, mc17.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    mcHist17 = dirDB.get(key).Clone("17")
else:
    mcHist17 = mc17.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection )
    dirDB.add(key, mcHist17.Clone("17"), overwrite=True)

key = (mc16.name, "16", args.variable, "_".join(map(str,args.binning)), mc16.weightString, mc16.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    mcHist16 = dirDB.get(key).Clone("16")
else:
    mcHist16 = mc16.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection )
    dirDB.add(key, mcHist16.Clone("16"), overwrite=True)
Exemplo n.º 11
0
def plotRegions(sorted=True):
    # get region histograms
    if args.year == "combined":
        hists_tmp = Results.getRegionHistos(postFit=args.postFit,
                                            plotBins=plotBins,
                                            nuisances=plotNuisances,
                                            addStatOnlyHistos=True,
                                            bkgSubstracted=args.bkgSubstracted,
                                            labelFormater=labelFormater)
        for i, dir in enumerate(Results.channels):
            if i == 0:
                hists = {
                    key: hist.Clone(str(i) + dir + key)
                    for key, hist in hists_tmp[dir].iteritems()
                    if not args.plotNuisances or key not in plotNuisances
                }  #copy.deepcopy(hists_tmp)
                if args.plotNuisances:
                    for n in plotNuisances:
                        hists.update({
                            n: {
                                "up":
                                hists_tmp[dir][n]["up"].Clone(
                                    str(i) + dir + n + "up"),
                                "down":
                                copy.deepcopy(hists_tmp[dir][n]["down"].Clone(
                                    str(i) + dir + n + "down"))
                            }
                        })
                        hists[n]["up"].legendText = hists_tmp[dir][n][
                            "up"].legendText
                        hists[n]["up"].style = hists_tmp[dir][n]["up"].style
                        hists[n]["down"].legendText = hists_tmp[dir][n][
                            "down"].legendText
                        hists[n]["down"].style = hists_tmp[dir][n][
                            "down"].style
            else:
                for key, hist in hists_tmp[dir].iteritems():
                    if args.plotNuisances and key in plotNuisances:
                        hists[key]["up"].Add(
                            hist["up"].Clone(str(i) + dir + key + "up"))
                        hists[key]["down"].Add(
                            hist["down"].Clone(str(i) + dir + key + "down"))
                    else:
                        hists[key].Add(hist.Clone(str(i) + dir + key))

    else:
        hists = Results.getRegionHistos(postFit=args.postFit,
                                        plotBins=plotBins,
                                        nuisances=plotNuisances,
                                        addStatOnlyHistos=True,
                                        bkgSubstracted=args.bkgSubstracted,
                                        labelFormater=labelFormater)["Bin0"]

    if args.plotNuisances and args.plotNuisances[0] == "total":

        hists["totalUnc"] = Results.sumNuisanceHistos(hists,
                                                      addStatUnc=True,
                                                      postFit=args.postFit)
        for i_n, ni in enumerate(plotNuisances):
            del hists[ni]

        error = []
        for i in range(hists["totalUnc"]["up"].GetNbinsX()):
            print i + 1, hists["totalUnc"]["relUp"].GetBinContent(
                i + 1) - hists["total"].GetBinError(i + 1) > 0, abs(
                    hists["totalUnc"]["relUp"].GetBinContent(i + 1) -
                    hists["total"].GetBinError(i + 1)
                ), abs(hists["totalUnc"]["relUp"].GetBinContent(i + 1) -
                       hists["total"].GetBinError(i + 1)
                       ) / hists["total"].GetBinError(i + 1)
            error.append(
                abs(hists["totalUnc"]["relUp"].GetBinContent(i + 1) -
                    hists["total"].GetBinError(i + 1)) /
                hists["total"].GetBinError(i + 1))
        global max
        print "Max difference: %f, Mean difference: %f" % (max(error),
                                                           np.mean(error))

    differential = False
    ch = "all"
    xLabel = ""
    if args.substituteCard: subCard = args.substituteCard.split("_")
    if args.bkgSubstracted and args.substituteCard:
        hists = replaceHistoBinning(hists)
        differential = True
        ch = subCard[-1]

    minMax = 0.29  #0.19 if args.postFit else 0.9
    if args.bkgSubstracted:
        minMax = 0.29  #0.19 if args.postFit else 0.9
        ratioCenter = None
        boxes, ratio_boxes = getErrorBoxes(copy.copy(hists["signal"]),
                                           minMax,
                                           lineColor=ROOT.kOrange - 2,
                                           fillColor=ROOT.kOrange - 2,
                                           hashcode=1001,
                                           ratioCenter=ratioCenter)
        boxes_stat, ratio_boxes_stat = getErrorBoxes(copy.copy(
            hists["signal_stat"]),
                                                     minMax,
                                                     lineColor=ROOT.kAzure - 3,
                                                     fillColor=ROOT.kAzure - 3,
                                                     hashcode=1001,
                                                     ratioCenter=ratioCenter)
    else:
        boxes, ratio_boxes = getUncertaintyBoxes(
            copy.copy(hists["total"]),
            minMax,
            lineColor=ROOT.kGray + 3,
            fillColor=ROOT.kGray + 3,
            hashcode=formatSettings(nBins)["hashcode"])
        if args.postFit:
            boxes_stat, ratio_boxes_stat = getUncertaintyBoxes(
                copy.copy(hists["total_stat"]),
                minMax,
                lineColor=ROOT.kAzure - 3,
                fillColor=ROOT.kAzure - 3,
                hashcode=1001)

    hists["data"].style = styles.errorStyle(ROOT.kBlack)
    hists[
        "data"].legendText = "data" if not args.bkgSubstracted else "bkg-sub. data (#color[61]{stat}, #color[92]{total} error, %s)" % (
            ch.replace("mu", "#mu") if ch != "all" else "e+#mu")
    hists["data"].legendOption = "p" if args.bkgSubstracted else "p"

    if args.bkgSubstracted:
        hists["signal"].style = styles.lineStyle(ROOT.kOrange + 7,
                                                 width=2,
                                                 errors=False)
        hists[
            "signal"].legendText = "tt#gamma SM prediction"  # (detector level)"

    else:
        for h_key, h in hists.iteritems():
            if "total" in h_key or h_key not in processes: continue
            hists[h_key].legendText = default_processes[h_key]["texName"]
            hists[h_key].style = styles.fillStyle(
                default_processes[h_key]["color"], errors=False)
            hists[h_key].LabelsOption("v", "X")

    # some settings and things like e.g. uncertainty boxes
    drawObjects_ = drawObjects(
        nBins=nBins,
        isData=(not args.expected),
        lumi_scale=lumi_scale,
        postFit=args.postFit,
        cardfile=args.substituteCard if args.substituteCard else args.cardfile,
        preliminary=args.preliminary)
    drawObjects_ += boxes
    if args.postFit:
        drawObjects_ += boxes_stat
#    if args.bkgSubstracted: drawObjects_ += boxes_stat
    drawObjects_ += drawDivisions(crLabel,
                                  misIDPOI=("misIDPOI" in args.cardfile))
    drawObjects_ += drawPTDivisions(crLabel, ptLabels)

    histModifications = []
    if not differential:
        histModifications += [
            lambda h: h.GetYaxis().SetTitleSize(
                formatSettings(nBins)["textsize"])
        ]
        histModifications += [
            lambda h: h.GetYaxis().SetLabelSize(
                formatSettings(nBins)["ylabelsize"])
        ]
        histModifications += [
            lambda h: h.GetYaxis().SetTitleOffset(
                formatSettings(nBins)["textoffset"])
        ]
        histModifications += [
            setPTBinLabels(ptLabels,
                           crName,
                           fac=formatSettings(nBins)["offsetfactor"] *
                           hists["total"].GetMaximum())
        ]

    ratioHistModifications = []
    if not differential:
        ratioHistModifications += [
            lambda h: h.GetYaxis().SetTitleSize(
                formatSettings(nBins)["textsize"])
        ]
        ratioHistModifications += [
            lambda h: h.GetYaxis().SetLabelSize(
                formatSettings(nBins)["ylabelsize"])
        ]
        ratioHistModifications += [
            lambda h: h.GetYaxis().SetTitleOffset(
                formatSettings(nBins)["textoffset"])
        ]
        ratioHistModifications += [
            lambda h: h.GetXaxis().SetTitleSize(
                formatSettings(nBins)["textsize"])
        ]
        ratioHistModifications += [
            lambda h: h.GetXaxis().SetLabelSize(
                formatSettings(nBins)["xlabelsize"])
        ]
        ratioHistModifications += [
            lambda h: h.GetXaxis().SetLabelOffset(0.035)
        ]

    addon = []
    if args.bkgSubstracted: addon += ["bkgSub"]
    if args.substituteCard:
        addon += ["rebinned"] + [cr for cr in subCard
                                 ]  #if cr not in args.cardfile.split("_") ]
    if args.plotNuisances: addon += args.plotNuisances

    # plot name
    if args.plotRegions and args.plotChannels:
        plotName = "_".join(
            ["regions"] + addon + args.plotRegions +
            [ch for ch in args.plotChannels if not "tight" in ch])
    elif args.plotRegions:
        plotName = "_".join(["regions"] + addon + args.plotRegions)
    elif args.plotChannels:
        plotName = "_".join(
            ["regions"] + addon +
            [ch for ch in args.plotChannels if not "tight" in ch])
    else:
        plotName = "_".join(["regions"] + addon)

    if args.cacheHistogram:
        from Analysis.Tools.MergingDirDB import MergingDirDB
        from TTGammaEFT.Tools.user import cache_directory
        cache_dir = os.path.join(cache_directory, "unfolding", str(args.year),
                                 "bkgSubstracted",
                                 "expected" if args.expected else "observed")
        dirDB = MergingDirDB(cache_dir)
        if not dirDB: raise
        addon = []
        if args.plotRegions: addon += args.plotRegions
        if args.plotChannels: addon += args.plotChannels
        systematics = [
            sys for sys in Results.getPulls(postFit=True).keys()
            if not "prop" in sys
        ]
        # data histogram
        name = ["bkgSubtracted", args.substituteCard, args.cardfile, "data"]
        print "_".join(name + addon)
        dirDB.add("_".join(name + addon), hists["data"], overwrite=True)
        # bkg substracted total histogram (signal) with total error
        name = ["bkgSubtracted", args.substituteCard, args.cardfile, "signal"]
        print "_".join(name + addon)
        dirDB.add("_".join(name + addon), hists["signal"], overwrite=True)
        # bkg substracted total histogram (signal) with stat error
        name = [
            "bkgSubtracted", args.substituteCard, args.cardfile, "signal_stat"
        ]
        print "_".join(name + addon)
        dirDB.add("_".join(name + addon), hists["signal_stat"], overwrite=True)

    # get histo list
    plots, ratioHistos = Results.getRegionHistoList(
        hists,
        processes=processes,
        noData=False,
        sorted=sorted and not args.bkgSubstracted,
        bkgSubstracted=args.bkgSubstracted,
        directory="dc_2016" if args.year == "combined" else "Bin0")
    if args.plotRegionPlot:

        plotting.draw(
            Plot.fromHisto(
                plotName,
                plots,
                texX="" if not differential else xLabel,
                texY="Observed - Background"
                if args.bkgSubstracted else "Number of Events",
            ),
            logX=False,
            logY=True,
            sorting=False,
            plot_directory=plotDirectory,
            legend=[(0.2, 0.86 if args.bkgSubstracted else
                     formatSettings(nBins)["legylower"], 0.9, 0.9),
                    formatSettings(nBins)["legcolumns"]]
            if not differential else (0.15, 0.80, 0.9, 0.9),
            widths={
                "x_width": formatSettings(nBins)["padwidth"],
                "y_width": formatSettings(nBins)["padheight"],
                "y_ratio_width": formatSettings(nBins)["padratio"]
            } if not differential else {},
            yRange=(0.7, hists["total"].GetMaximum() *
                    formatSettings(nBins)["heightFactor"])
            if not differential else "auto",
            ratio={
                "yRange": (1 - minMax, 1 + minMax),
                "texY": "Data/Pred." if args.bkgSubstracted else "Data/MC",
                "histos": ratioHistos,
                "drawObjects": ratio_boxes +
                ratio_boxes_stat if args.postFit else ratio_boxes,
                "histModifications": ratioHistModifications
            },
            drawObjects=drawObjects_ if not differential else
            drawObjectsDiff(lumi_scale) + boxes + boxes_stat,
            histModifications=histModifications,
            copyIndexPHP=True,
            extensions=["png", "pdf", "root"] if args.bkgSubstracted else
            ["png"],  # pdfs are quite large for sorted histograms (disco plot)
            redrawHistos=args.bkgSubstracted,
        )

    del hists
Exemplo n.º 12
0
                    matrix.Fill(reco_variable_overflow[year],
                                fiducial_variable_val, gen_weight_val)

            if r.event.is_fiducial:  # note: there is no reco_reweight_val because we construct the gen spectrum
                # counter
                counter_fid += 1
                yield_fid += gen_weight_val

                fiducial_spectrum.Fill(fiducial_variable_val, gen_weight_val)

        logger.info(
            "total: %6.2f (%6.2f) fiducial: %6.2f (%6.2f) fiducial+reco %6.2f (%6.2f) reco-total: %6.2f (%6.2f)",
            yield_tot, counter_tot, yield_fid, counter_fid, yield_fid_reco,
            counter_fid_reco, yield_reco, counter_reco)

    dirDB.add(loop_key, (matrix, fiducial_spectrum, reco_spectrum),
              overwrite=True)

    # Unfolding matrix
    plot_matrix = Plot2D.fromHisto("unfolding_matrix", [[matrix]],
                                   texY=tex_gen,
                                   texX=tex_reco +
                                   " +%i*(year-2016)" % max_reco_val)
    plotting.draw2D(
        plot_matrix,
        plot_directory=plot_directory_,
        logX=False,
        logY=False,
        logZ=True,
        drawObjects=drawObjects(),
        widths={'x_width': 500 * len(args.years)},
        copyIndexPHP=True,
Exemplo n.º 13
0
        ISR_unc.append(ISR_scale_rel)
        FSR_unc.append(FSR_scale_rel)
        Scale_unc.append(scale_rel)

        # Store results
        key = uniqueKey(args.selectEstimator, region, c, setup) + tuple(
            str(args.year))
        scaleKeys = []
        scaleKeys.append(tuple(list(key) + ["DownDown"]))
        scaleKeys.append(tuple(list(key) + ["DownNom"]))
        scaleKeys.append(tuple(list(key) + ["NomDown"]))
        scaleKeys.append(tuple(list(key) + ["NomUp"]))
        scaleKeys.append(tuple(list(key) + ["UpNom"]))
        scaleKeys.append(tuple(list(key) + ["UpUp"]))

        PDF_cache.add(key, delta_sigma_rel, overwrite=True)
        scale_cache.add(key, scale_rel, overwrite=True)
        PS_cache.add(key, PS_scale_rel, overwrite=True)
        ISR_cache.add(key, ISR_scale_rel, overwrite=True)
        FSR_cache.add(key, FSR_scale_rel, overwrite=True)
        for i_s, var in enumerate(scales):
            print i_s, var, scaleKeys[i_s]
            scale_cache.add(scaleKeys[i_s], var, overwrite=True)

logger.info("Min. PDF uncertainty: %.3f" % min(PDF_unc))
logger.info("Max. PDF uncertainty: %.3f" % max(PDF_unc))
logger.info("Av. PDF uncertainty: %.3f" %
            (sum(PDF_unc) / len(PDF_unc) if PDF_unc else 1.))
logger.info("Min. PS uncertainty: %.3f" % min(PS_unc))
logger.info("Max. PS uncertainty: %.3f" % max(PS_unc))
logger.info("Av. PS uncertainty: %.3f" %
Exemplo n.º 14
0
selection = setup.selection( "MC", channel="all", **setup.defaultParameters() )["prefix"]
selection = cutInterpreter.cutString( selection )
selection += "&&triggered==1"
print selection
if args.addCut:
    selection += "&&" + cutInterpreter.cutString( args.addCut )
print( "Using selection string: %s"%selection )


key = (data_sample_e.name, "AR", args.variable, "_".join(map(str,args.binning)), data_sample_e.weightString, data_sample_e.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist_e = dirDB.get(key).Clone("edataAR")
else:
    dataHist_e = data_sample_e.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection )
    dirDB.add(key, dataHist_e.Clone("edataAR"), overwrite=True)

key = (data_sample_mu.name, "AR", args.variable, "_".join(map(str,args.binning)), data_sample_mu.weightString, data_sample_mu.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist_mu = dirDB.get(key).Clone("mudataAR")
else:
    dataHist_mu = data_sample_mu.get1DHistoFromDraw( args.variable, binning=args.binning, selectionString=selection )
    dirDB.add(key, dataHist_mu.Clone("mudataAR"), overwrite=True)

dataHist_mu.style         = styles.errorStyle( ROOT.kBlack )
dataHist_mu.legendText    = "data (#mu)"

dataHist_e.style         = styles.errorStyle( ROOT.kRed )
dataHist_e.legendText    = "data (e)"

histos     = [[dataHist_mu], [dataHist_e]]
Exemplo n.º 15
0
    reconstruction = Plot(
        name="reconstruction",
        texX="p^{gen}_{T}(#gamma) / p^{reco}_{T}(#gamma)",
        attribute=lambda event, sample: getattr(event, args.genPtVariable) /
        event.PhotonGood0_pt if selection(event, sample) else -999,
        binning=reconstructionBinning,
        read_variables=read_variables,
    )

    plotting.fill([unfold2D, reconstruction], read_variables=read_variables)

    matrix = unfold2D.histos[0][0]
    resHisto = reconstruction.histos[0][0]

    dirDB.add(resMatrix, matrix, overwrite=True)
    dirDB.add(resRes, resHisto, overwrite=True)

matrix.Scale(lumi_scale)
resHisto.Scale(lumi_scale)

histos = {}
histos["gen"] = matrix.ProjectionX("gen")
histos["efficiency"] = ROOT.TH1D("efficiency", "efficiency", nGen, xminGen,
                                 xmaxGen)
histos["purity"] = ROOT.TH1D("purity", "purity", nReco, xminReco, xmaxReco)
histos["recoMC"] = matrix.ProjectionY("reco")
histos["reconstruction"] = resHisto
if args.noData: histos["reco"] = histos["recoMC"]
else: histos["reco"] = dataHisto
Exemplo n.º 16
0
                                fiducial_variable_val, gen_weight_val)

            if r.event.is_fiducial:  # note: there is no reco_reweight_val because we construct the gen spectrum
                # counter
                counter_fid += 1
                yield_fid += gen_weight_val

                fiducial_spectrum.Fill(fiducial_variable_val, gen_weight_val)

        logger.info(
            "total: %6.2f (%6.2f) fiducial: %6.2f (%6.2f) fiducial+reco %6.2f (%6.2f) reco-total: %6.2f (%6.2f)",
            yield_tot, counter_tot, yield_fid, counter_fid, yield_fid_reco,
            counter_fid_reco, yield_reco, counter_reco)

    dirDB.add(loop_key, (matrix, fiducial_spectrum, reco_spectrum, yield_fid,
                         yield_fid_reco, yield_reco),
              overwrite=True)

    # Unfolding matrix
    plot_matrix = Plot2D.fromHisto("unfolding_matrix", [[matrix]],
                                   texY=settings.tex_gen,
                                   texX=settings.tex_reco +
                                   " +%i*(year-2016)" % settings.max_reco_val)
    plotting.draw2D(
        plot_matrix,
        plot_directory=plot_directory_,
        logX=False,
        logY=False,
        logZ=True,
        drawObjects=drawObjects(),
        widths={'x_width': 500 * len(settings.years)},
Exemplo n.º 17
0
class SystematicEstimator:
    __metaclass__ = abc.ABCMeta

    def __init__(self, name, cacheDir=None):
        logger.info("Initializing Systematic Estimator for %s"%name)
        self.name = name
        self.initCache(cacheDir)
        self.processCut = None

        if   "_gen"    in name: self.processCut = "cat0"   #"photoncat0"
        elif "_misID"  in name: self.processCut = "cat2"   #"photoncat2"
        elif "_had"    in name: self.processCut = "cat134" #"photoncat134"
        elif "_prompt" in name: self.processCut = "cat02"  #"photoncat02"
        elif "_np"     in name: self.processCut = "cat134" #"photoncat134"
        elif "_hp"     in name: self.processCut = "cat1"   #"photoncat1"
        elif "_fake"   in name: self.processCut = "cat3"   #"photoncat3"
        elif "_PU"     in name: self.processCut = "cat4"   #"photoncat4"

    def initCache(self, cacheDir="systematics"):
        logger.info("Initializing cache for %s in directory %s"%(self.name, cacheDir))
        if cacheDir:
            self.cacheDir = os.path.join(cache_directory, cacheDir)
            try:    os.makedirs(cacheDir)
            except: pass

            cacheDirName       = os.path.join(cacheDir, self.name)

            self.cache = MergingDirDB(cacheDirName)
            if not self.cache:
                raise Exeption("Cache not initiated!")

            if self.name.count("DD"):
                helperCacheDirName = os.path.join(cacheDir, self.name+"_helper")
                self.helperCache = MergingDirDB(helperCacheDirName)
                if not self.helperCache: raise
                histoHelperCacheDirName = os.path.join(cacheDir, self.name+"_histo")
                self.histoHelperCache = MergingDirDB(histoHelperCacheDirName)
                if not self.histoHelperCache: raise
                tfCacheDirName = os.path.join(cacheDir, self.name+"_tf")
                self.tfCache = MergingDirDB(tfCacheDirName)
                if not self.tfCache: raise
            elif self.name.count("had"):
                helperCacheDirName = os.path.join(cacheDir, "had_helper")
                self.helperCache = MergingDirDB(helperCacheDirName)
                if not self.helperCache: raise
            else:
                self.helperCache=None
                self.tfCache=None

        else:
            self.cache=None
            self.helperCache=None
            self.tfCache=None

    # For the datadriven subclasses which often need the same getYieldFromDraw we write those yields to a cache
    def yieldFromCache(self, setup, process, c, selectionString, weightString, overwrite=False):
        s = (process, c, selectionString, weightString)
        if self.helperCache and self.helperCache.contains(s) and not overwrite:
            return self.helperCache.get(s)
        else:
            yieldFromDraw = u_float(**setup.processes[process].getYieldFromDraw(selectionString, weightString))
            if self.helperCache: self.helperCache.add(s, yieldFromDraw, overwrite=True)
            return yieldFromDraw

    # For the datadriven subclasses which often need the same mT histos we write those yields to a cache
    def histoFromCache(self, var, binning, setup, process, c, selectionString, weightString, overwrite=False):
        s = (var, "_".join(map(str,binning)), process, c, selectionString, weightString)
        if self.histoHelperCache and self.histoHelperCache.contains(s) and not overwrite:
            return self.histoHelperCache.get(s).Clone(process+c+var)
        else:
            histo = setup.processes[process].get1DHistoFromDraw( var, binning=binning, selectionString=selectionString, weightString=weightString, addOverFlowBin="upper" )
            if self.histoHelperCache: self.histoHelperCache.add(s, histo.Clone(process+c+var), overwrite=True)
            return histo.Clone(process+c+var)

    def uniqueKey(self, region, channel, setup, signalAddon=None, qcdUpdates={}):
        sysForKey = setup.sys.copy()
        sysForKey["reweight"] = "TEMP"
        reweightKey = '["' + '", "'.join(sorted([i for i in setup.sys['reweight']])) + '"]' # little hack to preserve order of list when being dumped into json
        key = region, channel, json.dumps(sysForKey, sort_keys=True).replace('"TEMP"',reweightKey), json.dumps(setup.parameters, sort_keys=True), json.dumps(setup.lumi, sort_keys=True)
        if qcdUpdates:  key += tuple(json.dumps(qcdUpdates, sort_keys=True))
        if signalAddon: key += tuple(signalAddon)
        return key

    def replace(self, i, r):
        try:
          if i.count("reweight"): return i.replace(r[0], r[1])
          else:                   return i
        except:                   return i

    def cachedEstimate(self, region, channel, setup, signalAddon=None, save=True, overwrite=False, checkOnly=False):
        key =  self.uniqueKey(region, channel, setup, signalAddon=signalAddon)
        if (self.cache and self.cache.contains(key)) and not overwrite:
            res = self.cache.get(key)
            logger.debug( "Loading cached %s result for %r : %r"%(self.name, key, res) )
        elif self.cache and not checkOnly:
            logger.debug( "Calculating %s result for %r"%(self.name, key) )
            res = self._estimate( region, channel, setup, signalAddon=signalAddon, overwrite=overwrite )
            _res = self.cache.add( key, res, overwrite=True )
            logger.debug( "Adding cached %s result for %r : %r" %(self.name, key, res) )
        elif not checkOnly:
            res = self._estimate( region, channel, setup, signalAddon=signalAddon, overwrite=overwrite)
        else:
            res = u_float(-1,0)
        return res if res >= 0 or checkOnly else u_float(0,0)

    def writeToCache(self, region, channel, setup, value, signalAddon=None, save=True, overwrite=False, checkOnly=False):
        key =  self.uniqueKey(region, channel, setup, signalAddon=signalAddon)
        if (self.cache and self.cache.contains(key)) and not overwrite:
            res = self.cache.get(key)
#            if res.val != value.val: print "Warning, caches estimate not equal to input value: have %s, got %s"%(res, value)
#            logger.debug( "Loading cached %s result for %r : %r"%(self.name, key, res) )
        elif self.cache and not checkOnly:
            _res = self.cache.add( key, value, overwrite=True )
            res = value
            logger.debug( "Adding cached %s result for %r : %r" %(self.name, key, res) )
        else:
            res = u_float(-1,0)
        return res if res >= 0 or checkOnly else u_float(0,0)

    def cachedTransferFactor(self, channel, setup, qcdUpdates=None, save=True, overwrite=False, checkOnly=False):
        key =  self.uniqueKey("region", channel, setup, qcdUpdates=qcdUpdates)
        if (self.tfCache and self.tfCache.contains(key)) and not overwrite:
            res = self.tfCache.get(key)
            logger.debug( "Loading cached %s result for %r : %r"%(self.name, key, res) )
        elif self.tfCache and not checkOnly:
            logger.debug( "Calculating %s result for %r"%(self.name, key) )
#            res = self._dataDrivenTransferFactor( channel, setup, qcdUpdates=qcdUpdates, overwrite=overwrite )
            res = self._fittedTransferFactor( channel, setup, qcdUpdates=qcdUpdates, overwrite=overwrite )
            _res = self.tfCache.add( key, res, overwrite=True )
            logger.debug( "Adding cached transfer factor for %r : %r" %(key, res) )
        elif not checkOnly:
#            res = self._dataDrivenTransferFactor( channel, setup, qcdUpdates=qcdUpdates, overwrite=overwrite )
            res = self._fittedTransferFactor( channel, setup, qcdUpdates=qcdUpdates, overwrite=overwrite )
        else:
            res = u_float(-1,0)
        return res if res > 0 or checkOnly else u_float(0,0)

    def cachedQCDMCTransferFactor(self, channel, setup, qcdUpdates=None, save=True, overwrite=False, checkOnly=False):
        key =  self.uniqueKey("regionQCDMC", channel, setup, qcdUpdates=qcdUpdates)
        if (self.tfCache and self.tfCache.contains(key)) and not overwrite:
            res = self.tfCache.get(key)
            logger.debug( "Loading cached %s result for %r : %r"%(self.name, key, res) )
        elif self.tfCache and not checkOnly:
            logger.debug( "Calculating %s result for %r"%(self.name, key) )
#            res = self._dataDrivenTransferFactor( channel, setup, qcdUpdates=qcdUpdates, overwrite=overwrite )
            res = self._transferFactor( channel, setup, qcdUpdates=qcdUpdates, overwrite=overwrite )
            _res = self.tfCache.add( key, res, overwrite=True )
            logger.debug( "Adding cached transfer factor for %r : %r" %(key, res) )
        elif not checkOnly:
#            res = self._dataDrivenTransferFactor( channel, setup, qcdUpdates=qcdUpdates, overwrite=overwrite )
            res = self._transferFactor( channel, setup, qcdUpdates=qcdUpdates, overwrite=overwrite )
        else:
            res = u_float(-1,0)
        return res if res > 0 or checkOnly else u_float(0,0)

    def cachedFakeFactor(self, region, channel, setup, overwrite=False, checkOnly=False):
        key =  self.uniqueKey(region, channel, setup)
        if (self.helperCache and self.helperCache.contains(key)) and not overwrite:
            res = self.helperCache.get(key)
            logger.debug( "Loading cached %s result for %r : %r"%(self.name, key, res) )
        elif self.helperCache and not checkOnly:
            logger.debug( "Calculating %s result for %r"%(self.name, key) )
            res = self._dataDrivenFakeCorrectionFactor( region, channel, setup, overwrite=overwrite )
            _res = self.helperCache.add( key, res, overwrite=True )
            logger.debug( "Adding cached transfer factor for %r : %r" %(key, res) )
        elif not checkOnly:
            res = self._dataDrivenFakeCorrectionFactor( region, channel, setup, overwrite=overwrite )
        else:
            res = u_float(-1,0)
        return res if res > 0 or checkOnly else u_float(0,0)

    @abc.abstractmethod
    def _estimate(self, region, channel, setup, signalAddon=None, overwrite=False):
        """Estimate yield in "region" using setup"""
        return

    def _transferFactor(self, channel, setup, overwrite=False):
        """Estimate transfer factor for QCD in "region" using setup"""
        return

    def _dataDrivenTransferFactor(self, channel, setup, qcdUpdates=None, overwrite=False):
        """Estimate transfer factor for QCD in "region" using setup"""
        return

    def _fittedTransferFactor(self, channel, setup, qcdUpdates=None, overwrite=False):
        """Estimate transfer factor for QCD in "region" using setup"""
        return

    def _dataDrivenFakeCorrectionFactor(self, region, channel, setup, overwrite=False):
        """Estimate fake factor for hadronic fakes in "region" using setup"""
        return

    def TransferFactorStatistic(self, region, channel, setup):
        ref  = self.cachedTransferFactor(channel, setup)
        up   = u_float(ref.val + ref.sigma)
        down = u_float(ref.val - ref.sigma)
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def TuneSystematic(self, region, channel, setup):
        up   = self.cachedEstimate(region, channel, setup, signalAddon="TuneUp")
        down = self.cachedEstimate(region, channel, setup, signalAddon="TuneDown")
        ref  = self.cachedEstimate(region, channel, setup)
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def topPtSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightTopPt"]}))
        return abs((up-ref)/ref) if ref > 0 else up

    def ErdOnSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup, signalAddon="erdOn")
        return abs((up-ref)/ref) if ref > 0 else up

    def QCDbasedSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup, signalAddon="QCDbased")
        return abs((up-ref)/ref) if ref > 0 else up

    def GluonMoveSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup, signalAddon="GluonMove")
        return abs((up-ref)/ref) if ref > 0 else up

    def PUSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightPUUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightPUDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def EERSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"eResUp"}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"eResDown"}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def EESSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"eScaleUp"}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"eScaleDown"}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

#    def MERSystematic(self, region, channel, setup):
#        ref  = self.cachedEstimate(region, channel, setup)
#        up   = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"muTotalUp"}))
#        down = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"muTotalDown"}))
#        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def JERSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"jerUp"}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"jerDown"}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def JECSystematic(self, region, channel, setup, jes="Total"):
        ref  = self.cachedEstimate(region, channel, setup)
        if ref == 0: return u_float(0,0)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"jes%sUp"%jes}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"jes%sDown"%jes}))
        unc  = abs(0.5*(up-down)/ref)
        if unc.val == 0:
            uncUp    = abs((ref-up)/ref)
            uncDown  = abs((ref-down)/ref)
            unc      = uncUp if uncUp.val >= uncDown.val else uncDown
            if unc.val == 0: return u_float(0,0)
        return unc

    def unclusteredSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"unclustEnUp"}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"selectionModifier":"unclustEnDown"}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def L1PrefireSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightL1PrefireUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightL1PrefireDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)
    
    def btaggingSFbSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightBTag_SF_b_Up"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightBTag_SF_b_Down"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def btaggingSFlSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightBTag_SF_l_Up"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightBTag_SF_l_Down"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def leptonSFSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def leptonSFStatSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFStatUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFStatDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def leptonSFSystSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFSystUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFSystDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def leptonTrackingSFSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightLeptonTrackingTightSFUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightLeptonTrackingTightSFDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def photonSFSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightPhotonSFUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightPhotonSFDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def photonSFAltSigSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightPhotonSFAltSigUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightPhotonSFAltSigDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def photonElectronVetoSFSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightPhotonElectronVetoSFUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightPhotonElectronVetoSFDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def triggerSystematic(self, region, channel, setup):
        ref  = self.cachedEstimate(region, channel, setup)
        up   = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightTriggerUp"]}))
        down = self.cachedEstimate(region, channel, setup.sysClone({"reweight":["reweightTriggerDown"]}))
        return abs(0.5*(up-down)/ref) if ref > 0 else u_float(0,0)

    def getBkgSysJobs(self, region, channel, setup):
        l = [
            (region, channel, setup.sysClone({"reweight":["reweightTopPt"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightPUUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightPUDown"]}), None),

            (region, channel, setup.sysClone({"selectionModifier":"eScaleUp"}), None),
            (region, channel, setup.sysClone({"selectionModifier":"eScaleDown"}), None),

            (region, channel, setup.sysClone({"selectionModifier":"eResUp"}), None),
            (region, channel, setup.sysClone({"selectionModifier":"eResDown"}), None),

#            (region, channel, setup.sysClone({"selectionModifier":"muTotalUp"}), None),
#            (region, channel, setup.sysClone({"selectionModifier":"muTotalDown"}), None),

            (region, channel, setup.sysClone({"selectionModifier":"jerUp"}), None),
            (region, channel, setup.sysClone({"selectionModifier":"jerDown"}), None),

#            (region, channel, setup.sysClone({"selectionModifier":"jesTotalUp"}), None),
#            (region, channel, setup.sysClone({"selectionModifier":"jesTotalDown"}), None),

#            (region, channel, setup.sysClone({"selectionModifier":"unclustEnUp"}), None),
#            (region, channel, setup.sysClone({"selectionModifier":"unclustEnDown"}), None),

            (region, channel, setup.sysClone({"reweight":["reweightL1PrefireUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightL1PrefireDown"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightBTag_SF_b_Up"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightBTag_SF_b_Down"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightBTag_SF_l_Up"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightBTag_SF_l_Down"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightLeptonTrackingTightSFUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightLeptonTrackingTightSFDown"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightPhotonSFUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightPhotonSFDown"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightPhotonSFAltSigUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightPhotonSFAltSigDown"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightPhotonElectronVetoSFUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightPhotonElectronVetoSFDown"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightTriggerUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightTriggerDown"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFStatUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFStatDown"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFSystUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFSystDown"]}), None),

            (region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFUp"]}), None),
            (region, channel, setup.sysClone({"reweight":["reweightLeptonTightSFDown"]}), None),
        ]


        # JEC Tags, (standard is "Total")
        jesTags = ['FlavorQCD', 'RelativeBal', 'HF', 'BBEC1', 'EC2', 'Absolute', 'Absolute_%i'%setup.year, 'HF_%i'%setup.year, 'EC2_%i'%setup.year, 'RelativeSample_%i'%setup.year, 'BBEC1_%i'%setup.year]
        for jes in jesTags:
            l += [
                   (region, channel, setup.sysClone({"selectionModifier":"jes%sUp"%jes}), None),
                   (region, channel, setup.sysClone({"selectionModifier":"jes%sDown"%jes}), None),
                 ]


        return l

    def getSigSysJobs(self, region, channel, setup):
        # in case there is a difference, enter it here (originally for fastSim)
        l  = self.getBkgSysJobs(region = region, channel = channel, setup = setup)
        l += [
            (region, channel, setup, "TuneUp"),
            (region, channel, setup, "TuneDown"),
            (region, channel, setup, "erdOn"),
            (region, channel, setup, "QCDbased"),
            (region, channel, setup, "GluonMove"),
        ]
        return l

    def getTexName(self, channel, rootTex=True):
        try:
          name = self.texName
        except:
          try:
            name = self.process[channel].texName
          except:
            try:
              texNames = [self.process[c].texName for c in allChannels]                # If all, only take texName if it is the same for all lepChannels
              if texNames.count(texNames[0]) == len(texNames):
                name = texNames[0]
              else:
                name = self.name
            except:
              name = self.name
        if not rootTex: name = "$" + name.replace("#","\\") + "$" # Make it tex format
        return name
Exemplo n.º 18
0
            addSel = cutInterpreter.cutString( addSel )
            print addSel
            for iso, invIso in replaceSelection.iteritems():
                addSel = addSel.replace(iso,invIso)
            print preSelection
            print addSel
            preSelection += "&&" + addSel
else:
    raise Exception("Region not implemented")

key = (data_sample.name, "mTinv", "_".join(map(str,binning)), data_sample.weightString, data_sample.selectionString, preSelection)
if dirDB.contains(key) and not args.overwrite:
    dataHist_SB = dirDB.get(key).Clone("mTinv")
else:
    dataHist_SB = data_sample.get1DHistoFromDraw( mTinv, binning=binning, selectionString=preSelection, addOverFlowBin="upper" )
    dirDB.add(key, dataHist_SB.Clone("mTinv"), overwrite=True)

key = (data_sample.name, "mT", "_".join(map(str,binning)), data_sample.weightString, data_sample.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist = dirDB.get(key).Clone("mT")
else:
    dataHist = data_sample.get1DHistoFromDraw( "mT",    binning=binning, selectionString=selection,    addOverFlowBin="upper" )
    dirDB.add(key, dataHist.Clone("mT"), overwrite=True)

qcdHist     = dataHist_SB.Clone("QCD")

dataHist_SB.style      = styles.errorStyle( ROOT.kBlack )
dataHist_SB.legendText = "Observed (%s)"%args.mode.replace("mu","#mu")
dataHist.style         = styles.errorStyle( ROOT.kBlack )
dataHist.legendText    = "Observed (%s)"%args.mode.replace("mu","#mu")
qcdHist.style          = styles.fillStyle( color.QCD )
Exemplo n.º 19
0
# Plot ranges

polString = "[0]*x**2+[1]*x**3+[2]*x**4+[3]*x**5+[4]*x**6+[5]*x**7+[6]*x**8"
# get TGraph from results data list
xhist = toGraph( "f", "f", results )
func  = ROOT.TF1("func",polString,args.binning[1], args.binning[2] ) 
func.SetNpx(10000)
xhist.Fit(func,"NO")
x68max = func.GetX( 0.989, args.binning[1], args.binning[2] )
x95max = func.GetX( 3.84, args.binning[1], args.binning[2] )
xmax   = func.GetX( args.yRange[1], args.binning[1], args.binning[2] )

sConfig = "_".join( [args.sample, args.selection, args.pdf] )
res = { 95:x95max, 68:x68max }
limitCache.add( sConfig, res, overwrite=True )

xhist.SetLineWidth(0)

func.SetFillColor(ROOT.kWhite)
func.SetFillStyle(1001)
func.SetLineWidth(3)
func.SetLineColor(ROOT.kBlack)

print '68', x68max
print '95', x95max

ROOT.gStyle.SetPadLeftMargin(0.14)
ROOT.gStyle.SetPadRightMargin(0.1)
ROOT.gStyle.SetPadTopMargin(0.11)
Exemplo n.º 20
0
             if event.PhotonNoChgIsoNoSieie0_photonCatMagic == 2 and event.year
             == 2017 else 1.) * (misIDSF_val[2018].val
                                 if event.PhotonNoChgIsoNoSieie0_photonCatMagic
                                 == 2 and event.year == 2018 else 1.)

for s in mc:
    s.setWeightString(weightStringAR)
    key = (s.name, "AR", args.variable, "_".join(map(str, args.binning)),
           s.weightString, s.selectionString, selection)
    if dirDB.contains(key) and not args.overwrite:
        s.hist = copy.deepcopy(dirDB.get(key).Clone(s.name + "AR"))
    else:
        s.hist = s.get1DHistoFromDraw(
            args.variable, binning=args.binning,
            selectionString=selection)  #, addOverFlowBin="upper" )
        dirDB.add(key, s.hist.Clone("%s_AR" % s.name))

    s.style = styles.lineStyle(s.color,
                               width=2,
                               dotted=False,
                               dashed=False,
                               errors=True)
    s.hist.style = styles.lineStyle(s.color,
                                    width=2,
                                    dotted=False,
                                    dashed=False,
                                    errors=True)
    s.hist.legendText = s.texName

histos = [[s.hist] for s in mc]
Exemplo n.º 21
0
selection = cutInterpreter.cutString(selection)
selection += "&&pTStitching==1&&triggered==1"
print selection
if args.addCut:
    selection += "&&" + cutInterpreter.cutString(args.addCut)
print("Using selection string: %s" % selection)

key = (mc_e.name, "AR", args.variable, "_".join(map(str, args.binning)),
       mc_e.weightString, mc_e.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    mcHist_e = dirDB.get(key.Clone("e"))
else:
    mcHist_e = mc_e.get1DHistoFromDraw(args.variable,
                                       binning=args.binning,
                                       selectionString=selection)
    dirDB.add(key, mcHist_e.Clone("e"), overwrite=True)

key = (mc_mu.name, "AR", args.variable, "_".join(map(str, args.binning)),
       mc_mu.weightString, mc_mu.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    mcHist_mu = dirDB.get(key.Clone("mu"))
else:
    mcHist_mu = mc_mu.get1DHistoFromDraw(args.variable,
                                         binning=args.binning,
                                         selectionString=selection)
    dirDB.add(key, mcHist_mu.Clone("mu"), overwrite=True)

mcHist_mu.style = styles.lineStyle(ROOT.kBlack, width=2, errors=True)
mcHist_mu.legendText = "MC (#mu)"

mcHist_e.style = styles.lineStyle(ROOT.kRed, width=2, errors=True)
Exemplo n.º 22
0
            preSelection = preSelection.replace(iso, invIso)
            addSel = addSel.replace(iso, invIso)
        preSelection += "&&" + addSel
else:
    raise Exception("Region not implemented")

key = (data_sample.name, "mTinv", "_".join(map(str, binning)),
       data_sample.weightString, data_sample.selectionString, preSelection)
if dirDB.contains(key) and not args.overwrite:
    dataHist_SB = dirDB.get(key)
else:
    dataHist_SB = data_sample.get1DHistoFromDraw(mTinv,
                                                 binning=binning,
                                                 selectionString=preSelection,
                                                 addOverFlowBin="upper")
    dirDB.add(key, dataHist_SB)

key = (data_sample.name, "mT", "_".join(map(str, binning)),
       data_sample.weightString, data_sample.selectionString, selection)
if dirDB.contains(key) and not args.overwrite:
    dataHist = dirDB.get(key)
else:
    dataHist = data_sample.get1DHistoFromDraw("mT",
                                              binning=binning,
                                              selectionString=selection,
                                              addOverFlowBin="upper")
    dirDB.add(key, dataHist)

qcdHist = dataHist_SB.Clone("QCD")

dataHist_SB.style = styles.errorStyle(ROOT.kBlack)