-
Notifications
You must be signed in to change notification settings - Fork 4
/
rescaleDatacards.py
95 lines (83 loc) · 2.99 KB
/
rescaleDatacards.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
import os
from ROOT import *
#WARNING: changed value to 2.29. Was 2.32 presiously
oldLumi = 2.29
#oldLumi = 2.32
newLumi = 5.0
dirName = "5fb"
#channel = "Znn"
channel = "Zll"
if channel == "Zvv":
oldFileNames = [
"vhbb_TH_Znn_13TeVTightLowPt_Wbb.root",
"vhbb_TH_Znn_13TeVTightHighPt_QCD.root",
"vhbb_TH_Znn_13TeVTightHighPt_Signal.root",
"vhbb_TH_Znn_13TeVTightHighPt_TTbarTight.root",
"vhbb_TH_Znn_13TeVTightHighPt_WLight.root",
"vhbb_TH_Znn_13TeVTightHighPt_Wbb.root",
"vhbb_TH_Znn_13TeVTightHighPt_ZLight.root",
"vhbb_TH_Znn_13TeVTightHighPt_Zbb.root",
"vhbb_TH_Znn_13TeVTightLowPt_QCD.root",
"vhbb_TH_Znn_13TeVTightLowPt_Signal.root",
"vhbb_TH_Znn_13TeVTightLowPt_TTbarTight.root",
"vhbb_TH_Znn_13TeVTightLowPt_WLight.root",
"vhbb_TH_Znn_13TeVTightLowPt_Wbb.root",
"vhbb_TH_Znn_13TeVTightLowPt_ZLight.root",
"vhbb_TH_Znn_13TeVTightLowPt_Zbb.root"
]
elif channel == "Zll":
oldFileNames = [
"vhbb_TH_First_dc_highpt.root",
"vhbb_TH_First_dc_lowpt.root",
]
def scaleDC(oldFileName, newFileName, scale):
print "DC - Opening: ",oldFileName+" . Writing:",newFileName
fOld = open(oldFileName)
fNew = open(newFileName, 'w')
for line in fOld.readlines():
if 'rate' == line[:4] or 'observation' == line[:11]:
newLine = ""
for word in line.split(" "):
try:
num = eval(word)
except:
num = 0.0
if num>0:
word = str(num*scale)
newLine = newLine + word + " "
line = newLine
fNew.write(line)
fOld.close()
fNew.close()
def scaleHistos(oldFileName, newFileName, scale):
print "Opening: ",oldFileName+" . Writing:",newFileName
fileOld = TFile(oldFileName)
fileOld.ls()
if channel == "Zvv": Dir = fileOld.Get(channel+"_13TeV")
elif channel == "Zll": Dir = fileOld.Get("Vpt1")
assert(type(Dir)==TDirectoryFile)
fileNew = TFile(newFileName,"recreate")
if channel == "Zvv": newDir = fileNew.mkdir(channel+"_13TeV")
elif channel == "Zll": newDir = fileNew.mkdir("Vpt1")
newDir.cd()
for i in Dir.GetListOfKeys():
obj = i.ReadObj()
obj = obj.Clone(obj.GetName())
obj.Scale(scale)
if obj.GetName()=="TTCMS_vhbb_bTagHFWeightHFStats1Up":
print obj.GetName(), obj.GetMaximum()
obj.Write()
fileNew.Close()
return
scale = newLumi/oldLumi
oldFileName = oldFileNames[0]
try:
os.mkdir(oldFileName.split("vhbb_")[0]+dirName)
except:
pass
for oldFileName in oldFileNames:
newFileName = oldFileName.replace("vhbb_",dirName+"/vhbb_")
oldFileNameDC = (oldFileName.replace("vhbb_TH","vhbb_DC_TH")).replace(".root",".txt")
newFileNameDC = (newFileName.replace("vhbb_TH","vhbb_DC_TH")).replace(".root",".txt")
scaleHistos(oldFileName, newFileName, scale)
scaleDC(oldFileNameDC, newFileNameDC, scale)