-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotLimMQRun3HV.py
199 lines (185 loc) · 7.35 KB
/
plotLimMQRun3HV.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import math
import numpy as np
import ROOT as r
from array import array
import glob
import copy
inputLimits = []
inDir = "limitsLooseMuonHV"
tag ="v1"
r.gStyle.SetPaintTextFormat("0.3f")
colours = {2:r.kBlack,3:r.kBlack,4:r.kBlack,5:r.kRed,10:r.kBlue, 15:r.kMagenta, 20:r.kOrange+1}
# outputFile = r.TFile("limitsHVScale_{0}.root".format(tag),"RECREATE")
michaelInput = False
def makeGraphFromHisto(hist,coords,quant):
xCoord = []
yCoord = []
zCoord = []
for x,y,zOrig in zip(coords[:,0],coords[:,1],coords[:,2]):
z = hist.GetBinContent(hist.GetXaxis().FindBin(x),hist.GetYaxis().FindBin(y))
# if zOrig != 0 and (z/zOrig > 10 or z/zOrig < 0.1):
# z = zOrig
xCoord.append(x)
yCoord.append(y)
zCoord.append(z)
# Remove nan z-values
np_zCoord = np.array(zCoord)
xCoord = np.array(xCoord)[~np.isnan(np_zCoord)]
yCoord = np.array(yCoord)[~np.isnan(np_zCoord)]
zCoord = np_zCoord[~np.isnan(np_zCoord)]
# Convert from numpy to array
xCoord = array('d',xCoord)
yCoord = array('d',yCoord)
zCoord = array('d',zCoord)
tempGraph = r.TGraph2D(quant+'tempGraph',"",len(zCoord),xCoord,yCoord,zCoord)
return tempGraph
def matthewSmooth(temp):
lastBinsY = {}
firstBinsY = {}
for xBin in range(1,temp.GetNbinsX()+1):
lastBinsY[xBin] = 0
lastBinContent = 0
firstBinsY[xBin] = temp.GetNbinsY()+1
firstBinContent = 0
notFound = True
allZero = True
#Last bins
for yBin in range(1,temp.GetNbinsY()+1)[::-1]:
if temp.GetBinContent(xBin,yBin) == 0:
if allZero:
continue
if notFound:
if temp.GetBinContent(xBin-1,yBin) == 0:
lastBinContent = temp.GetBinContent(xBin,yBin+1)
elif temp.GetBinContent(xBin,yBin+1) == 0:
lastBinContent = temp.GetBinContent(xBin-1,yBin)
else:
lastBinContent = 0.5*(temp.GetBinContent(xBin,yBin+1)+temp.GetBinContent(xBin-1,yBin))
lastBinsY[xBin] = yBin
notFound = False
temp.SetBinContent(xBin,yBin,lastBinContent)
else:
allZero = False
if allZero:
lastBinsY[xBin] = temp.GetNbinsY()+1
#lastBinsX[yBin] = xBin
notFound = True
allZero = True
for yBin in range(1,temp.GetNbinsY()+1):
if temp.GetBinContent(xBin,yBin) == 0:
if allZero: continue
if notFound:
if temp.GetBinContent(xBin+1,yBin) == 0:
firstBinContent = temp.GetBinContent(xBin,yBin-1)
elif temp.GetBinContent(xBin,yBin-1) == 0:
lastBinContent = temp.GetBinContent(xBin+1,yBin)
else:
firstBinContent = 0.5*(temp.GetBinContent(xBin,yBin-1)+temp.GetBinContent(xBin+1,yBin))
firstBinsY[xBin] = yBin
notFound = False
temp.SetBinContent(xBin,yBin,firstBinContent)
else:
allZero = False
if allZero:
firstBinsY[xBin] = 0
temp.Smooth(1,'k5a')
for xBin in range(1,temp.GetNbinsX()+1):
for yBin in range(1,lastBinsY[xBin]):
temp.SetBinContent(xBin,yBin,0)
temp.SetBinError(xBin,yBin,0)
for yBin in range(firstBinsY[xBin]+1,temp.GetNbinsY()+1):
temp.SetBinContent(xBin,yBin,0)
temp.SetBinError(xBin,yBin,0)
return temp
def isRootFileOk(fileName):
import ROOT as r
r.gStyle.SetOptStat(0)
r.gROOT.SetBatch(True)
isOK = False
if os.path.exists(os.path.abspath(fileName)):
# r.gErrorIgnoreLevel=r.kError
r.gErrorIgnoreLevel=r.kSysError
tf = r.TFile(os.path.abspath(fileName),"READ")
if not (tf.IsZombie() or tf.TestBit(r.TFile.kRecovered)): isOK = True
tf.Close()
return isOK
def getContour(graph,level=1.0):
graph.GetHistogram().SetContour(1,array('d',[1.0]))
graph.Draw("cont list")
contourList = []
contLevel = graph.GetContourList(level)
if contLevel and contLevel.GetSize()>0:
# print (graph.GetName())
# print (contLevel.GetSize())
for i in range(0,contLevel.GetSize()):
contourTemp = contLevel[i].Clone()
if type(contourTemp) != int: contourTemp.SetName(graph.GetName()+"_r1Contour_{0}".format(i))
else: continue
contourTemp.SetLineWidth(3)
contourList.append(contourTemp)
contourList = sorted(contourList,key = lambda contour: contour.GetN())[::-1]
return contourList
else:
return None
from collections import defaultdict
inputArrays = defaultdict(list)
# inputArrays = {"Signif":[],"M2":[],"M1":[],"C":[],"P1":[],"P2":[]}#"Obs":[]}
inFiles = glob.glob(inDir+"/higgsCombine*mH120.root")
for inFile in inFiles:
if not isRootFileOk(inFile): continue
tFile = r.TFile(inFile)
inFileForMC = inFile.replace("Obs","")
portal = inFileForMC.split("_")[2]
mass = float((inFileForMC.split("_")[3]))
lifetime = float((inFileForMC.split("_")[4]))
xo = inFileForMC.split("_")[5]
xl = inFileForMC.split("_")[6].split(".Asymp")[0]
tTree = tFile.Get("limit")
tTree.GetEntry(2)
if not tTree:continue
if "Asymp" in inFile:
inputArrays[portal,xo,xl].append([mass,lifetime/1000.,float(tTree.limit)/100.])
for (portal,xo,xl) in inputArrays:
masses = sorted(list(set([x[0] for x in inputArrays[portal,xo,xl]])))
lifetimes = sorted(list(set([x[1] for x in inputArrays[portal,xo,xl]])))
outHist = r.TH2D("portal_{0}_xo_{1}_xl_{2}".format(portal,xo,xl),";mass [GeV];ctau [m]",len(masses),0,len(masses),len(lifetimes),0,len(lifetimes))
oC = r.TCanvas()
oC.SetLogz()
for iM in range(len(masses)):
outHist.GetXaxis().SetBinLabel(iM+1,str(float(masses[iM])))
for iY in range(len(lifetimes)):
outHist.GetYaxis().SetBinLabel(iY+1,str(float(lifetimes[iY])))
output1D = defaultdict(list)
for mass,lifetime,limit in inputArrays[portal,xo,xl]:
iM = masses.index(mass)
iL = lifetimes.index(lifetime)
outHist.Fill(iM+0.5,iL+0.5,limit)
output1D[mass].append([lifetime,limit])
outHist.SetMaximum(1)
outHist.SetMinimum(0.001)
outHist.Draw("colztext")
oC.SaveAs("outputHVLooseMuon/"+outHist.GetName()+".pdf")
dummyHist = r.TH1D("dummy",";c#tau [m]; 95% CL UL on BR(H #rightarrow ss)",100,0.1,30)
dummyHist.SetMaximum(100)
dummyHist.SetMinimum(1E-4)
oC.SetLogy()
oC.SetLogx()
dummyHist.Draw()
graphs = []
leg = r.TLegend(0.6,0.7,0.89,0.89)
leg.SetBorderSize(0)
for mass in masses:
output1D[mass] = sorted(output1D[mass])
# output1D[mass].append([output1D[mass][-1][0]*10,output1D[mass][-1][1]*10])
xVals = array('d',[x[0] for x in output1D[mass]])
yVals = array('d',[x[1] for x in output1D[mass]])
oG = r.TGraph(len(xVals),xVals,yVals)
oG.SetName("limits_{0}".format(mass))
oG.SetLineColor(colours[int(mass)])
oG.Draw("same")
leg.AddEntry(oG,"m_{{s}} = {0} GeV".format(mass))
# oG.Write()
graphs.append(oG)
leg.Draw()
oC.SaveAs("outputHVLooseMuon/limsUpdatedBackground_portal_{0}_xo_{1}_xl_{2}.pdf".format(portal,xo,xl))