-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelp_table.py
117 lines (106 loc) · 4.68 KB
/
help_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import argparse, copy, os, sys, glob, math
from array import array
import ROOT
import helpers
import config as CONF
ROOT.gROOT.SetBatch()
#singal region table
def TableExample(masterdic, summarydic):
texoutpath = CONF.inputpath + "b77" + "/" + "Plot/Tables/"
if not os.path.exists(texoutpath):
os.makedirs(texoutpath)
outFile = open( texoutpath + "SR_summary.tex", "w")
tableList = []
add_table_head(tableList, cut_lst, title="Sample")
raw_lst = ["qcd", "ttbar", "totalbkg"]
for raw in raw_lst:
#get the corresponding region
outstr = ""
outstr += raw
#print masterdic, systag
for c in cut_lst:
totalsyst = add_syst(summarydic[c][raw])[0]
valuetuple = (masterdic[c][raw + "_est"]["int"], totalsyst * masterdic[c][raw + "_est"]["int"])
outstr += add_entry(valuetuple)
#finish the current entry
outstr+="\\\\"
tableList.append(outstr)
#add data:
tableList.append("\\hline")
outstr = ""
outstr += "Data"
for c in cut_lst:
outstr += add_entry((masterdic[c]["data"]["int"], masterdic[c]["data"]["int_err"]))
outstr+="\\\\"
tableList.append(outstr)
#finish the table
add_table_tail(tableList, cut_lst)
#return the table
for line in tableList:
print line
outFile.write(line+" \n")
outFile.close()
def add_entry(valuetuple, doerr=True, percent=False):
'''add the entry: tuple or value,
option of filling the error and as a percentage'''
temstr = ""
temstr += " & "
if isinstance(valuetuple, tuple):
if valuetuple[0] == 0:
temstr += " - "
return temstr
else:
temstr += str(helpers.round_sig(valuetuple[0] * (100 if percent else 1), 3))
if doerr:
temstr += " $\\pm$ "
temstr += str(helpers.round_sig(valuetuple[1] * (100 if percent else 1), 3)) #cause sqrt(a^2 + b^2) is sigma*a/sqrt(a^2 + b^2)
else:
temstr += str(helpers.round_sig(valuetuple * (100 if percent else 1), 3))
return temstr
def add_table_head(tableList, column_lst, title="", special_raw=None):
'''add the table header: the string, the columns and the title'''
title_lst = [title.replace("_", " ")]
for t in column_lst:
title_lst.append(t.replace("_", " "))
tableList.append("\\begin{footnotesize}")
tableList.append("\\begin{tabular}{c" + "{0}".format("|c" * len(column_lst)) + "}")
if special_raw is not None:
special_lst = [" "]
for s in special_raw:
special_lst.append("{" + s.replace("_", " ") + "}")
tableList.append(" & \multicolumn{2}{c}".join(special_lst) + " \\\\")
tableList.append(" & ".join(title_lst) + " \\\\")
tableList.append("\\hline\\hline")
#tableList.append("{0}\\\\".format("& " * len(column_lst)))
def add_table_tail(tableList, column_lst):
'''add the table ending line: the string, the columns'''
#tableList.append("{0}\\\\".format("& " * len(column_lst)))
tableList.append("\\hline\\hline")
tableList.append("\\end{tabular}")
tableList.append("\\end{footnotesize}")
tableList.append("\\newline")
#these still need development
def add_table_head_formal(tableList, column_lst, title="", special_raw=None):
'''add the table header: the string, the columns and the title'''
title_lst = [title.replace("_", " ")]
for t in column_lst:
title_lst.append(t.replace("_", " "))
tableList.append("\\begin{tabular}{lS[table-format=4.0,table-number-alignment=right,round-mode=places,round-precision=0]@{\,$\pm$}S[round-mode=figures,round-precision=1,table-format=3.0]")
tableList.append("S[table-format=3.0,table-number-alignment=right,round-mode=places,round-precision=0]@{\,$\pm$}S[round-mode=figures,round-precision=1,table-format=2.0]")
tableList.append("S[table-format=2.0,table-number-alignment=right,round-mode=places,round-precision=0]@{\,$\pm$}S[round-mode=figures,round-precision=1,table-format=1.0]}")
if special_raw is not None:
special_lst = [" "]
for s in special_raw:
special_lst.append("{" + s.replace("_", " ") + "}")
tableList.append(" & \multicolumn{2}{c}".join(special_lst) + " \\\\")
tableList.append(" & ".join(title_lst) + " \\\\")
tableList.append("\\hline\\hline")
#tableList.append("{0}\\\\".format("& " * len(column_lst)))
def add_table_tail_formal(tableList, column_lst):
'''add the table ending line: the string, the columns'''
#tableList.append("{0}\\\\".format("& " * len(column_lst)))
tableList.append("\\bottomrule")
tableList.append("\\end{tabular}")
tableList.append("\\newline")
if __name__ == '__main__':
main()