Esempio n. 1
0
 def __init__(self, name, info):
     self.plotTable = PrettyTable(
         ["Plot Group", "Weighted Events", "Error"])
     self.output = open("%s.txt" % name, "w")
     self.info = info
     self.hists = [None, None, None, None]
     self.callTime = ""
     self.command = ""
Esempio n. 2
0
 def __init__(self, name, info, path='.'):
     self.plotTable = PrettyTable(
         ["Plot Group", "Weighted Events", "Error"])
     self.output = open("%s/%s_info.log" % (path, name), "w")
     self.info = info
     self.hists = [GenericHist()] * 4
     self.callTime = ""
     self.command = ""
     self.name = name
Esempio n. 3
0
def writeMCLogInfo(hist_info, selection, branch_name, luminosity, cut_string,
                   latex):
    mc_info = PrettyTable(
        ["Plot Group", "Weighted Events", "Error", "Stat Error", "Raw Events"])
    weighted_events = 0
    total_background = 0
    background_err = 0
    total_err2 = 0
    signal = 0
    signal_err = 0
    for plot_set, entry in hist_info.iteritems():
        wevents = round(entry["weighted_events"],
                        3 if entry["weighted_events"] < 1 else 2)
        mc_info.add_row([
            plot_set, wevents,
            round(entry["error"], 2),
            round(entry["stat error"], 2),
            int(round(entry["raw_events"]))
        ])
        weighted_events += entry["weighted_events"]
        total_err2 += entry["error"]**2
        if "wz" not in plot_set:
            total_background += entry["weighted_events"]
            background_err += entry["error"] * entry["error"]
        else:
            signal += entry["weighted_events"]
            signal_err += entry["error"]
    total_err = math.sqrt(total_err2)
    likelihood = 0 if weighted_events <= 0 else \
        signal/math.sqrt(weighted_events)
    likelihood_err = 0 if signal <= 0 or weighted_events <= 0 else \
        likelihood*math.sqrt((signal_err/signal)**2 + \
            (0.5*total_err/weighted_events)**2)
    sigbkgd = 0 if weighted_events <= 0 else signal / weighted_events
    sigbkgd_err = 0 if signal <= 0 or weighted_events <= 0 else \
        sigbkgd*math.sqrt((signal_err/signal)**2 + (total_err/weighted_events)**2)
    if weighted_events == 0:
        raise RuntimeError("Empty histogram produced for variable " +
                           branch_name)
    with open("temp.txt", "a") as mc_file:
        mc_file.write("\n" + (mc_info.get_string() if not latex else mc_info.
                              get_latex_string()) + "\n")
        mc_file.write(
            "\nTotal sum of Monte Carlo: %0.2f +/- %0.2f" %
            (round(weighted_events, 2),
             round(
                 math.sqrt(
                     sum([x["error"] * x["error"]
                          for x in hist_info.values()])), 2)))
        mc_file.write(
            "\nTotal sum of background Monte Carlo: %0.2f +/- %0.2f" %
            (round(total_background, 2), round(math.sqrt(background_err), 2)))
        mc_file.write("\nRatio S/(S+B): %0.2f +/- %0.2f" %
                      (round(sigbkgd, 2), round(sigbkgd_err, 2)))
        mc_file.write("\nRatio S/sqrt(S+B): %0.2f +/- %0.2f" %
                      (round(likelihood, 2), round(likelihood_err, 2)))
Esempio n. 4
0
def makeLogFile(channels, hist_info, args):
    with open("temp.txt", "w") as log_file:
        meta_info = '-'*80 + '\n' + \
            'Script called at %s\n' % datetime.datetime.now() + \
            'The command was: %s\n' % ' '.join(sys.argv) + \
            '-'*80 + '\n'
        log_file.write(meta_info)
        log_file.write("Selection: %s" % args.selection)
        log_file.write("\nLuminosity: %0.2f fb^{-1}\n" % (args.luminosity))
        log_file.write('-' * 80 + '\n')
    columns = ["Process"] + ["\\" + c for c in channels] + ["Total Yield"]
    yield_table = PrettyTable(columns)
    yield_info = OrderedDict()

    formatted_names = {
        "wz-powheg": "WZ (POWHEG)",
        "wz-mgmlm": "WZ (MG MLM)",
        "QCD-WZjj": "QCD-WZjj",
        "EW-WZjj": "EW-WZjj",
        "wzjj-ewk": "WZjj EWK",
        "wzjj-ewk_filled": "WZjj EWK",
        "wzjj-vbfnlo": "WZjj EWK (VBFNLO)",
        "nonprompt": "Nonprompt",
        "top-ewk": "t+V/VVV",
        "zg": "Z$\gamma$",
        "vv-powheg": "VV (POWHEG)",
        "vv": "VV",
        "wz": "WZ (MG5\_aMC)",
        "wz-powheg": "WZ (POWHEG)",
        "predyield": "Pred. Background",
        "data_2016": "Data",
        "data": "Data",
        "data_2016H": "Data (2016H)",
    }

    sigfigs = 3
    for name, entry in hist_info.iteritems():
        if "aqgc" in name:
            continue
        for i, chan in enumerate(channels):
            # Channels should be ordered the same way as passed to the histogram
            # This bin 0 is the underflow, bin 1 is total, and bin 2
            # is the first bin with channel content (mmm/emm/eem/eee by default)
            if name == "data":
                yield_info[chan] = "%i" % entry[chan][0]
            else:
                yield_info[chan] = getFormattedYieldAndError(
                    entry[chan][0], entry[chan][1], sigfigs)
        if name == "data":
            yield_info["Total Yield"] = "%i" % entry["total"][0]
        else:
            yield_info["Total Yield"] = getFormattedYieldAndError(
                entry["total"][0], entry["total"][1], sigfigs)
        yield_table.add_row([formatted_names[name]] + yield_info.values())
    with open("temp.txt", "a") as log_file:
        log_file.write(yield_table.get_latex_string())
def makeLogFile(channels, hist_stack, data_hist, signal_stack):
    with open("temp.txt", "w") as log_file:
        meta_info = '-'*80 + '\n' + \
            'Script called at %s\n' % datetime.datetime.now() + \
            'The command was: %s\n' % ' '.join(sys.argv) + \
            '-'*80 + '\n'
        log_file.write(meta_info)
        log_file.write("Selection: %s" % args.selection)
        log_file.write("\nLuminosity: %0.2f fb^{-1}\n" % (args.luminosity))
        log_file.write('-' * 80 + '\n')
    columns = ["Process"] + ["\\" + c for c in channels] + ["Total Yield"]
    yield_table = PrettyTable(columns)
    yield_info = OrderedDict()
    hists = hist_stack.GetHists()
    hists.sort(key=lambda x: x.Integral(), reverse=True)

    formatted_names = {
        "wz-powheg": "WZ (POWHEG)",
        "wz-mgmlm": "WZ (MG MLM)",
        "wz3lnu-mgmlm-0j": "WZ+0j (MG MLM)",
        "wz3lnu-mgmlm-1j": "WZ+1j (MG MLM)",
        "wz3lnu-mgmlm-2j": "WZ+2j (MG MLM)",
        "wz3lnu-mgmlm-3j": "WZ+3j (MG MLM)",
        "chargedHiggsWZ-m300": "m_{H^{\pm}} = 300",
        "chargedHiggsWZ-m400": "m_{H^{\pm}} = 400",
        "chargedHiggsWZ-m500": "m_{H^{\pm}} = 500",
        "chargedHiggsWZ-m600": "m_{H^{\pm}} = 600",
        "chargedHiggsWZ-m700": "m_{H^{\pm}} = 700",
        "chargedHiggsWZ-m800": "m_{H^{\pm}} = 800",
        "chargedHiggsWZ-m900": "m_{H^{\pm}} = 900",
        "chargedHiggsWZ-m1000": "m_{H^{\pm}} = 1000",
        "chargedHiggsWZ-m1500": "m_{H^{\pm}} = 1500",
        "chargedHiggsWZ-m2000": "m_{H^{\pm}} = 2000",
        "wzjj-ewk": "WZjj EWK",
        "wzjj-vbfnlo": "WZjj EWK (VBFNLO)",
        "nonprompt": "Nonprompt",
        "top-ewk": "t+V/VVV",
        "zg": "Z$\gamma$",
        "vv-powheg": "VV (POWHEG)",
        "vv": "VV (MG5\_aMC)",
        "wz": "WZ (MG5\_aMC)",
        "wz-powheg": "WZ (POWHEG)",
        "predyield": "Pred. Background" if signal_stack else "Predicted Yield",
        "data_2016": "Data",
        "data_2016H": "Data (2016H)",
    }

    signal_names = []
    if signal_stack:
        hists += signal_stack.GetHists()
        signal_names = [h.GetName() for h in signal_stack.GetHists()]
    signal_names.append("predyield")
    hist_allbackground = ROOT.TH1D("predyield", "all background",
                                   1 + len(channels), 0, 1 + len(channels))
    hists.append(hist_allbackground)

    if data_hist:
        hists.Add(data_hist)

    sigfigs = 0 if not data_hist else 1
    for hist in hists:
        if hist.GetName() not in signal_names and "data" not in hist.GetName():
            hist_allbackground.Add(hist)
        for i, chan in enumerate(channels):
            # Channels should be ordered the same way as passed to the histogram
            # Thus bin 0 is the underflow, bin 1 is total, and bin 2
            # is the first bin with channel content (mmm/emm/eem/eee by default)
            yield_info[chan] = getFormattedYieldAndError(hist, i + 2, sigfigs)
        yield_info["Total Yield"] = getFormattedYieldAndError(hist, 1, sigfigs)
        yield_table.add_row(
            [formatted_names[hist.GetName().replace("_standalone", "")]] +
            yield_info.values())
    with open("temp.txt", "a") as log_file:
        log_file.write(yield_table.get_latex_string())
Esempio n. 6
0
class LogFile:
    def __init__(self, name, info):
        self.plotTable = PrettyTable(
            ["Plot Group", "Weighted Events", "Error"])
        self.output = open("%s.txt" % name, "w")
        self.info = info
        self.hists = [None, None, None, None]
        self.callTime = ""
        self.command = ""

    def getBackground(self):
        return self.hists[BKG]

    def getSignal(self):
        return self.hists[SIGNAL]

    def getTotalMC(self):
        return self.hists[TOTAL]

    def getData(self):
        return self.hists[DATA]

    def addMC(self, groupHists, drawOrder):
        for groupName in [i for i in drawOrder[::-1] if i in groupHists]:
            hist = groupHists[groupName]
            wEvents, error = self.getIntErr(hist)
            self.plotTable.add_row(sl((groupName, wEvents, error)))
            self.addHist(hist, BKG)
        self.addHist(self.getBackground(), TOTAL)

    def addHist(self, hist, dest):
        if not self.hists[dest]:
            self.hists[dest] = hist.Clone()
        else:
            self.hists[dest].Add(hist)

    def addSignal(self, signal, groupName):
        self.addHist(signal, SIGNAL)
        wEvents, error = self.getIntErr(signal)
        self.plotTable.add_row(sl((groupName, wEvents, error)))
        self.addHist(signal, TOTAL)

    def addMetaInfo(self, callTime, command):
        self.callTime = callTime
        self.command = command

    def getIntErr(self, hist):
        error = array.array('d', [0])
        wEvents = hist.IntegralAndError(0, hist.GetNbinsX(), error)
        return (wEvents, error[0])

    def writeOut(self, isLatex=False):
        self.output.write('-' * 80 + '\n')
        self.output.write("Script called at %s \n" % self.callTime)
        self.output.write("The command was: %s \n" % self.command)
        self.output.write('-' * 80 + '\n')
        self.output.write("Selection: %s/%s\n" %
                          (self.info.getAnalysis(), self.info.getSelection()))
        self.output.write("Luminosity: %0.2f fb^{-1}\n" %
                          (self.info.getLumi() / 1000))
        #output.write("\nPlotting branch: %s\n" % branch_name)

        if isLatex:
            self.output.write('\n' + self.plotTable.get_latex_string() +
                              '\n' * 2)
        else:
            self.output.write('\n' + self.plotTable.get_string() + '\n' * 2)

        self.output.write("Total sum of Monte Carlo: %0.2f +/- %0.2f \n" %
                          sl(self.getIntErr(self.getTotalMC())))
        if self.getSignal():
            self.output.write(
                "Total sum of background Monte Carlo: %0.2f +/- %0.2f \n" %
                sl(self.getIntErr(self.getBackground())))
            self.output.write("Ratio S/(S+B): %0.2f +/- %0.2f \n" %
                              sl(self.getSigBkgRatio()))
            self.output.write("Ratio S/sqrt(S+B): %0.2f +/- %0.2f \n" %
                              sl(self.getLikelihood()))
        if self.getData():
            self.output.write("Number of events in data %d \n" %
                              self.getData.Integral())

        self.output.close()

    def getSigBkgRatio(self):
        sig, sigErr = self.getIntErr(self.getSignal())
        tot, totErr = self.getIntErr(self.getTotalMC())
        sigbkgd = sig / tot
        sigbkgdErr = sigbkgd * math.sqrt((sigErr / sig)**2 + (totErr / tot)**2)
        return (sigbkgd, sigbkgdErr)

    def getLikelihood(self):
        sig, sigErr = self.getIntErr(self.getSignal())
        tot, totErr = self.getIntErr(self.getTotalMC())
        likelihood = sig / math.sqrt(tot)
        likelihoodErr = likelihood * math.sqrt((sigErr / sig)**2 +
                                               (0.5 * totErr / tot)**2)
        return (likelihood, likelihoodErr)
Esempio n. 7
0
class LogFile:
    def __init__(self, name, info, path='.'):
        self.plotTable = PrettyTable(
            ["Plot Group", "Weighted Events", "Error"])
        self.output = open("%s/%s_info.log" % (path, name), "w")
        self.info = info
        self.hists = [GenericHist()] * 4
        self.callTime = ""
        self.command = ""
        self.name = name

    def getBackground(self):
        return self.hists[BKG]

    def getSignal(self):
        return self.hists[SIGNAL]

    def getTotalMC(self):
        return self.hists[TOTAL]

    def getData(self):
        return self.hists[DATA]

    def addMC(self, drawOrder):
        for name, hist in drawOrder:
            wEvents, error = self.getIntErr(hist)
            self.plotTable.add_row(sl((name, wEvents, error)))
            self.hists[BKG] += hist

        self.hists[TOTAL] += self.getBackground()

    def addSignal(self, signal, groupName):
        self.hists[SIGNAL] += signal
        wEvents, error = self.getIntErr(signal)
        self.plotTable.add_row(sl((groupName, wEvents, error)))
        self.hists[TOTAL] += signal

    def addMetaInfo(self, callTime, command):
        self.callTime = callTime
        self.command = command

    def getIntErr(self, hist):
        wEvents = sum(hist.hist)
        error = math.sqrt(sum(hist.histErr2))
        return (wEvents, error)

    def writeOut(self, isLatex=False):
        self.output.write('-' * 80 + '\n')
        self.output.write("Script called at %s \n" % self.callTime)
        self.output.write("The command was: %s \n" % self.command)
        self.output.write("The name of this Histogram is: %s \n" % self.name)
        self.output.write('-' * 80 + '\n')
        self.output.write("Selection: %s/%s\n" %
                          (self.info.getAnalysis(), self.info.getSelection()))
        self.output.write("Luminosity: %0.2f fb^{-1}\n" %
                          (self.info.getLumi() / 1000))
        #output.write("\nPlotting branch: %s\n" % branch_name)

        if isLatex:
            self.output.write('\n' + self.plotTable.get_latex_string() +
                              '\n' * 2)
        else:
            self.output.write('\n' + self.plotTable.get_string() + '\n' * 2)

        if not self.getTotalMC().empty():
            self.output.write("Total sum of Monte Carlo: %0.2f +/- %0.2f \n" %
                              sl(self.getIntErr(self.getTotalMC())))
        if not self.getSignal().empty():
            self.output.write(
                "Total sum of background Monte Carlo: %0.2f +/- %0.2f \n" %
                sl(self.getIntErr(self.getBackground())))
            self.output.write("Ratio S/(S+B): %0.2f +/- %0.2f \n" %
                              sl(self.getSigBkgRatio()))
            self.output.write("Ratio S/sqrt(S+B): %0.2f +/- %0.2f \n" %
                              sl(self.getLikelihood()))
        if not self.getData().empty():
            self.output.write("Number of events in data %d \n" %
                              self.getIntErr(self.getData())[0])

        self.output.close()

    def getSigBkgRatio(self):
        sig, sigErr = self.getIntErr(self.getSignal())
        tot, totErr = self.getIntErr(self.getTotalMC())
        sigbkgd = sig / tot
        sigbkgdErr = sigbkgd * math.sqrt((sigErr / sig)**2 + (totErr / tot)**2)
        return (sigbkgd, sigbkgdErr)

    def getLikelihood(self):
        sig, sigErr = self.getIntErr(self.getSignal())
        tot, totErr = self.getIntErr(self.getTotalMC())
        likelihood = sig / math.sqrt(tot)
        likelihoodErr = likelihood * math.sqrt((sigErr / sig)**2 +
                                               (0.5 * totErr / tot)**2)
        return (likelihood, likelihoodErr)
Esempio n. 8
0
def writeMCLogInfo(hist_info, selection, branch_name, luminosity, cut_string):
    meta_info = (
        "-" * 80
        + "\n"
        + "Script called at %s\n" % datetime.datetime.now()
        + "The command was: %s\n" % " ".join(sys.argv)
        + "-" * 80
        + "\n"
    )
    mc_info = PrettyTable(["Plot Group", "Weighted Events", "Error", "Stat Error", "Raw Events"])
    weighted_events = 0
    total_background = 0
    background_err = 0
    total_err2 = 0
    sigbkgd = 0
    sigbkgd_err = 0
    signal = 0
    signal_err = 0
    likelihood = 0
    likelihood_err = 0
    for plot_set, entry in hist_info.iteritems():
        mc_info.add_row(
            [
                plot_set,
                round(entry["weighted_events"], 2),
                round(entry["error"], 2),
                round(entry["stat error"], 2),
                entry["raw_events"],
            ]
        )
        weighted_events += entry["weighted_events"]
        total_err2 += entry["error"] ** 2
        if "wz" not in plot_set:
            total_background += entry["weighted_events"]
            background_err += entry["error"] * entry["error"]
        else:
            signal += entry["weighted_events"]
            signal_err += entry["error"]
    total_err = math.sqrt(total_err2)
    likelihood = signal / math.sqrt(weighted_events)
    likelihood_err = likelihood * math.sqrt((signal_err / signal) ** 2 + (0.5 * total_err / weighted_events) ** 2)
    sigbkgd = signal / weighted_events
    sigbkgd_err = sigbkgd * math.sqrt((signal_err / signal) ** 2 + (total_err / weighted_events) ** 2)
    with open("temp.txt", "w") as mc_file:
        mc_file.write(meta_info)
        mc_file.write("Selection: %s" % selection)
        mc_file.write("\nAdditional cut: %s" % ("None" if cut_string == "" else cut_string))
        mc_file.write("\nLuminosity: %0.2f fb^{-1}" % (luminosity))
        mc_file.write("\nPlotting branch: %s\n" % branch_name)
        mc_file.write(mc_info.get_string())
        mc_file.write(
            "\nTotal sum of Monte Carlo: %0.2f +/- %0.2f"
            % (
                round(weighted_events, 2),
                round(math.sqrt(sum([x["error"] * x["error"] for x in hist_info.values()])), 2),
            )
        )
        mc_file.write(
            "\nTotal sum of background Monte Carlo: %0.2f +/- %0.2f"
            % (round(total_background, 2), round(math.sqrt(background_err), 2))
        )
        mc_file.write("\nRatio S/(S+B): %0.2f +/- %0.2f" % (round(sigbkgd, 2), round(sigbkgd_err, 2)))
        mc_file.write("\nRatio S/sqrt(S+B): %0.2f +/- %0.2f" % (round(likelihood, 2), round(likelihood_err, 2)))