def get_legend_name(key):
    if "Template" in key:
        for tag in legend_tags:
            if tag in key:
                return settings.get_pretty_name(tag)
    else:
        return settings.get_pretty_name(key)
    def write_uncert_tabular(self):
        res = self.result
        table = [
            r"\begin{tabular}{l c c} \\",
            r"\hline",
            r"\hline",
            r"Source & \multicolumn{2}{c}{Uncertainty (\%)} \\",
            r"& $\Rvis$ & $\sigma_{\ttgam}$ \\",
            r"\hline",
            r"Statistical & %.1f & %.1f \\" % (
                getattr(res, "R_fid_err_stat") / getattr(res, "R_fid") * 100.,
                getattr(res, "xsec_err_stat") / getattr(res, "xsec") * 100.,
            ),
            r"\hline",
            r"Systematic & %.1f & %.1f \\" % (
                getattr(res, "R_fid_err_sys") / getattr(res, "R_fid") * 100.,
                getattr(res, "xsec_err_sys") / getattr(res, "xsec") * 100.,
            ),
            r"\hline",
            r"Individual contributions: & & \\"
        ]
#        # sort by largest contribution
#        for sys in sorted(summed_uncerts,
#            key=lambda s: -getattr(res, s + "_" + self.xsec_calc + "_R")
#        ):
        for sys in summed_uncerts:
            table.append(
                "\;\;\;"
                + settings.get_pretty_name(sys)
                + r" & %.1f & %.1f \\" % (
                    getattr(res, sys+"_"+self.xsec_calc+"_R_fid") * 100.,
                    getattr(res, sys+"_"+self.xsec_calc+"_xsec") * 100.,
                ),
            )
        table += (
#            r"\;\;\;top-quark mass  & ---  & --- \\",
            r"\hline",
            r"\textbf{Total} & %.1f & %.1f \\" % (
(
    (getattr(res, "R_fid_err_stat") / getattr(res, "R_fid") * 100.)**2
  + (getattr(res, "R_fid_err_sys") / getattr(res, "R_fid") * 100.)**2
)**.5,
(
    (getattr(res, "xsec_err_stat") / getattr(res, "xsec") * 100.)**2
  + (getattr(res, "xsec_err_sys") / getattr(res, "xsec") * 100.)**2
)**.5,
            ),
            r"\hline",
            r"\hline",
            r"\end{tabular}",
        )
        with open(self.plot_output_dir + "sys_tabular.tex", "w") as f:
            f.writelines(map(lambda l: l + "\n", table))
        for n, l in enumerate(table):
            setattr(self.result, "line%02d"%n, l)
 def make_header(self):
     self.table_lines += (
         r"\begin{tabular}{l | "
             + len(self.cont.head_line)*"r "
             + "}",
         17*" "
             + " & "
             + " & ".join(itertools.imap(
                 lambda s: "%17s" % settings.get_pretty_name(s + "_tex"),
                 self.cont.head_line
             ))
             + r" \\",
         r"\hline",
         r"\hline",
     )
 def combine_cutflow_histo(self, grp):
     """Adds histos with single bins to a combined cutflow histogram."""
     grp = list(grp)
     bin_labels = dict(
         (
             w.histo.GetMaximumBin(),
             settings.get_pretty_name(w.histo.GetXaxis().GetTitle())
         ) for w in grp
     )
     lumi = grp[0].lumi
     wrp = gen.op.sum(grp)
     wrp.lumi = lumi
     for bin, label in bin_labels.iteritems():
         wrp.histo.GetXaxis().SetBinLabel(bin, label)
     return wrp
 def write_method_comparison_tabular(self):
     res = self.results
     l = len(res)
     for r in res:
         r.calc = r.name.split("_")[-1]
     table = [
         r"\begin{tabular}{l " + l*"c " + r"} \\",
         r"\hline",
         r"\hline",
         "Method " + (l*"& %s" + r" \\") % tuple(
             settings.get_pretty_name(r.calc)
             for r in res
         ),
         (r"$\Nsig / \epsilon_\gamma$ " + l*" & %.1f" + r" \\") % tuple(
             getattr(r, "n_sig_ttgam") / r.eff_gamma
             for r in res
         ),
         r"\hline",
         r"\hline",
         r"Source & \multicolumn{" + str(l) + r"}{c}{Uncertainty} \\",
         r"\hline",
         (r"Internal (e.g. fit) " + l*" & %.1f" + r" \\") % tuple(
             getattr(r, "n_sig_ttgam_err") / r.eff_gamma
             for r in res
             ),
         r"\hline",
         (r"Systematic" + l*" & %.1f" + r" \\") % tuple(
             getattr(r, "n_sig_ttgam_err_sys") / r.eff_gamma
             for r in res
             ),
         r"\hline",
         r"Contributions: & & \\"
     ]
     for sys in sorted(summed_uncerts,
         key=lambda s: -getattr(res[0], s + "_" + res[0].calc + "_n_sig_ttgam")
     ):
         table.append(
             "\;\;\;"
             + settings.get_pretty_name(sys)
             + (l*" & %.1f" + r" \\") % tuple(
                 getattr(r, sys+"_"+r.calc+"_n_sig_ttgam") * getattr(r, "n_sig_ttgam")  / r.eff_gamma
                 for r in res
                 ),
         )
     table += (
         r"\hline",
         (r"\textbf{Total uncertainty}" + l*" & %.1f" + r" \\") % tuple(
                 (
                     getattr(r, "n_sig_ttgam_err")**2
                     + getattr(r, "n_sig_ttgam_err_sys")**2
                 )**.5  / r.eff_gamma
                 for r in res
             ),
         r"\hline",
         r"\hline",
         r"\end{tabular}",
         )
     with open(self.plot_output_dir + "method_tabular.tex", "w") as f:
         f.writelines(map(lambda l: l + "\n", table))
     for n, l in enumerate(table):
         setattr(self.result, "line%02d"%n, l)