-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprintsf.py
135 lines (125 loc) · 5.74 KB
/
printsf.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/env python
import os
import sys
import datetime
import ROOT as r
def printsf(funcname, xthreshs, ythreshs, sfs, errs, filename="", xvar="eta", yvar="pt", xvarabs=False, yvarabs=False, command=""):
"""
Function to print scale factors (or fake rate) from arrays of numbers
"""
# parse some options and process some stuff
yvarabsstr = ""
xvarabsstr = ""
if yvarabs: yvarabsstr = "fabs"
if xvarabs: xvarabsstr = "fabs"
# Get the boundary
xvarmaxthresh = xthreshs[-1] - abs(xthreshs[-1]) * 0.001
xvarminthresh = xthreshs[0] - abs(xthreshs[0]) * 0.001
yvarmaxthresh = ythreshs[-1] - abs(ythreshs[-1]) * 0.001
yvarminthresh = ythreshs[0] - abs(ythreshs[0]) * 0.001
# Form the function sring
funcstr = "// Auto generated from https://github.com/sgnoohc/rooutil/blob/master/printsf.py\n"
funcstr += "// Created on {}\n".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
funcstr += "// Created by the command:\n"
if len(command):
funcstr += "// > {}\n".format(command)
funcstr += "float {}(float {}_raw, float {}_raw, int isyst=0)\n".format(funcname, yvar, xvar)
funcstr += "{\n"
funcstr += " if (isyst != 1 && isyst != -1 && isyst != 0)\n"
funcstr += " printf(\"%s\",Form(\"WARNING - in function=%s, isyst=%d is not recommended!\\n\", __FUNCTION__, isyst));\n"
funcstr += " float {xvar} = std::min((float) {xvarmaxthresh}, std::max((float) {xvarminthresh}, {xvar}_raw)); // minimum values are just below the first bin upper edge\n".format(xvar=xvar, xvarmaxthresh=xvarmaxthresh, xvarminthresh=xvarminthresh)
funcstr += " float {yvar} = std::min((float) {yvarmaxthresh}, std::max((float) {yvarminthresh}, {yvar}_raw)); // minimum values are just below the first bin upper edge\n".format(yvar=yvar, yvarmaxthresh=yvarmaxthresh, yvarminthresh=yvarminthresh)
for i, xthresh in enumerate(xthreshs):
for j, ythresh in enumerate(ythreshs):
sf = sfs[i][j]
err = errs[i][j]
if i == len(xthreshs) - 1 and j == len(ythreshs):
funcstr += " return {} + isyst * {};\n".format(yvarabsstr, yvar, ythresh, xvarabsstr, xvar, xthresh, sf, err)
elif i == len(xthreshs) -1:
funcstr += " if ({}({}) < {}) return {} + isyst * {};\n".format(yvarabsstr, yvar, ythresh, sf, err)
#elif j == len(ythreshs) -1:
# funcstr += " if ({}({}) < {}) return {} + isyst * {};\n".format(yvarabsstr, yvar, ythresh, sf, err)
else:
funcstr += " if ({}({}) < {} && {}({}) < {}) return {} + isyst * {};\n".format(yvarabsstr, yvar, ythresh, xvarabsstr, xvar, xthresh, sf, err)
funcstr += " printf(\"WARNING in {}(): the given phase-space (%f, %f) did not fall under any range!\\n\", {}, {}); \n".format(funcname, yvar, xvar)
funcstr += " return 1;\n"
funcstr += "}\n"
# print or write to file
if len(filename) != 0:
f = open(filename, "w")
f.write(funcstr)
else:
print funcstr
def printsf_th2(funcname, th2, filename="", xvar="eta", yvar="pt", xvarabs=False, yvarabs=False, command=""):
"""
Function to print scale factors (or fake rate) from TH2
"""
sfs = []
errs = []
xthreshs = []
ythreshs = []
for i in xrange(1, th2.GetNbinsX() + 1): xthreshs.append(th2.GetXaxis().GetBinUpEdge(i))
for i in xrange(1, th2.GetNbinsY() + 1): ythreshs.append(th2.GetYaxis().GetBinUpEdge(i))
for i in xrange(1, th2.GetNbinsX() + 1):
sfs.append([])
errs.append([])
for j in xrange(1, th2.GetNbinsY() + 1):
sfs[i-1].append(th2.GetBinContent(i, j))
errs[i-1].append(th2.GetBinError(i, j))
printsf(funcname, xthreshs, ythreshs, sfs, errs, filename, xvar, yvar, xvarabs, yvarabs, command)
def printsf_tgraph1d(funcname, tgraph1d, filename="", xvar="eta", yvar="pt", xvarabs=False, yvarabs=False):
"""
Function to print scale factors (or fake rate) from 1D TGraph
WARNING: this only takes one side of the error
"""
npoints = tgraph1d.GetN()
xs = tgraph1d.GetX()
ys = tgraph1d.GetY()
xerrs = tgraph1d.GetEX()
yerrs = tgraph1d.GetEY()
xthreshs = [1000000]
ythreshs = []
sfs = [[]]
errs = [[]]
for index, x in enumerate(xs):
ythreshs.append(x + tgraph1d.GetErrorXhigh(index))
for index, y in enumerate(ys):
sfs[0].append(y)
errs[0].append(tgraph1d.GetErrorYhigh(index))
printsf(funcname, xthreshs, ythreshs, sfs, errs, filename, xvar, yvar, xvarabs, yvarabs)
if __name__ == "__main__":
default_func_name = "th2d_weight"
def help():
print "Usage:"
print ""
print " python {} FILENAME TH2NAME \"CONFIGSTRING\" [OUTPUTFUNCNAME={}] [OUTPUTFILENAME]".format(sys.argv[0], default_func_name)
print ""
print " CONFIGSTRING defines the xvar and yvar name and whether they are absolute or not"
print " NOTE: Make sure to escape string for CONFIGSTRING"
print ""
print " e.g. |xvar|:yvar"
print ""
sys.exit()
if len(sys.argv) < 3:
help()
filename = sys.argv[1]
histname = sys.argv[2]
configstring = sys.argv[3]
xvar = configstring.split(":")[0]
yvar = configstring.split(":")[1]
xvarabs = True if len(xvar.replace("|", "")) != len(xvar) else False
yvarabs = True if len(yvar.replace("|", "")) != len(yvar) else False
xvar = xvar.replace("|", "")
yvar = yvar.replace("|", "")
try:
funcname = sys.argv[4]
except:
funcname = default_func_name
try:
oname = sys.argv[5]
except:
oname = ""
f = r.TFile(filename)
h = f.Get(histname)
printsf_th2(funcname, h, oname, xvar, yvar, xvarabs, yvarabs, " ".join(sys.argv))
#eof