forked from tongbaojia/MakePlot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_truthdist.py
213 lines (185 loc) · 6.84 KB
/
plot_truthdist.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import ROOT, rootlogon
import argparse
import copy
import glob
import helpers
import os
import sys
import time
import config as CONF
from array import array
import numpy as np
#end of import for now
ROOT.gROOT.SetBatch(True)
ROOT.gROOT.LoadMacro("AtlasStyle.C")
ROOT.SetAtlasStyle()
#set global variables
#set output directory
outputdir = CONF.outplotpath
mass_lst = [1500, 2000, 3000]
# mass_lst = [700, 800, 900, 1000, 1100, 1200, 1400, 1500, 1600, 1800, 2000, 2250, 2500, 2750, 3000]
# mass_lst = mass_lst[3:-1]
def main():
ops = options()
# create output file
output = ROOT.TFile.Open(CONF.outplotpath + "sig_truth.root", "recreate")
#print output
plt_lst = ["mHH_l", "hCandDr",
"leadHCand_Mass_s", "leadHCand_Pt_m", "leadHCand_trk0_Pt", "leadHCand_trk1_Pt",
"sublHCand_Mass_s", "sublHCand_Pt_m", "sublHCand_trk0_Pt", "sublHCand_trk1_Pt"]
# select the cuts
scut_lst = [path + data for path in ["FourTag_Signal/", "ThreeTag_Signal/", "TwoTag_split_Signal/", "OneTag_Signal/"]
for data in plt_lst]
dcut_lst = [path + data for path in ["NoTag_Signal/"]*4
for data in plt_lst]
#for data in ["h0b0_dR"]]
"""
for data in ["nh0_pt", "nh1_pt", "nb0_pt", "nb1_pt", "nb2_pt",
"nb3_pt", "h0h1_dR", "h0b0_dR", "h1b2_dR",
"b0b1_dR", "b2b3_dR"]]
"""
# # Draw the efficiency plot relative to the all normalization
DrawSignalTruth(output, dcut_lst, scut_lst, inputdir=ops.inputdir, outputname="comp_")
output.Close()
def DrawSignalTruth(outputroot, dcut_lst, scut_lst, inputdir="", outputname="", normalization=0):
### the first argument is the input directory
### the second argument is the output prefix name
### the third argument is relative to what normalization: 0 for total number of events
### 1 for signal mass region
# setup basic plot parameters
lowmass = -50
highmass = 3150
# load input MC file
maxbincontent = .4 # approx
minbincontent = -0.01
for i, (dcut, scut) in enumerate(zip(dcut_lst, scut_lst)):
canv = ROOT.TCanvas(inputdir + "_" + outputname + str(normalization), "Efficiency", 800, 800)
xleg, yleg = 0.55, 0.7
legend = ROOT.TLegend(xleg, yleg, xleg+0.3, yleg+0.2)
sfiles = ["/signal_G_hh_c10_M%i/hist-MiniNTuple.root" % mass for mass in mass_lst]
sinfos = ["RSG " + str(m) + " GeV" for m in mass_lst]
dfiles = ["/data_test/hist-MiniNTuple.root"]
dinfos = ["0Tag data"]
allmc = []
for j,(f,info) in enumerate(zip(dfiles + sfiles, dinfos + sinfos)):
cut = dcut if "data" in f else scut
#here could be changed to have more options
input_mc = ROOT.TFile.Open(CONF.inputpath + inputdir + f)
if not input_mc:
print CONF.inputpath + inputdir + f
try:
temp_mc = input_mc.Get(cut).Clone()
#temp_mc = temp_mc.ProjectionY()
except:
print CONF.inputpath + inputdir + f
print cut
raise
# rebin the hist for pt
# set other options
temp_mc.GetYaxis().SetTitle("Normalized")
temp_mc.GetXaxis().SetNdivisions(510)
temp_mc.GetXaxis().SetLabelSize(0.03)
temp_mc.GetYaxis().SetNdivisions(505)
if cut.split("_")[-1] == "pt":
temp_mc.Rebin(10)
temp_mc.GetXaxis().SetTitle( temp_mc.GetXaxis().GetTitle() + " [GeV]")
elif "deta" in cut.lower():
temp_mc.Rebin(30)
maxbincontent = .6 / 1.5
# draw line at the cut value
cutval = 1.7
line = ROOT.TLine(1.7, 0, 1.7, 0.4)
line.SetLineWidth(3)
line.SetLineStyle(9)
line.Draw()
else:
temp_mc.GetXaxis().SetTitle( "#DeltaR between H, child b")
dR_type = cut.split("/")[1][0:4]
if dR_type != "h0h1":
temp_mc.GetXaxis().SetRangeUser(0, 3)
maxbincontent = 1.0/1.5
else:
maxbincontent = .5
if dR_type == "h1b2" or dR_type == "h0b0":
dR1_line = ROOT.TLine(1, 0, 1, maxbincontent)
dR04_line = ROOT.TLine(.4, 0, .4, maxbincontent)
dR1_line.Draw("")
dR04_line.Draw("")
elif dR_type == "b0b1" or dR_type == "b2b3":
dR1_line = ROOT.TLine(2, 0, 2, maxbincontent)
dR04_line = ROOT.TLine(.8, 0, .8, maxbincontent)
dR1_line.Draw("")
dR04_line.Draw("")
if temp_mc.GetNbinsX() == 76:
temp_mc.Rebin(4)
elif temp_mc.GetNbinsX() > 20:
temp_mc.Rebin(temp_mc.GetNbinsX() / 20)
if "data" in f:
temp_mc.Sumw2(True)
outputroot.cd()
temp_mc.Scale(1/temp_mc.Integral())
temp_mc.SetName("RSG_" + info + "_" + cut.replace("/", "_"))
temp_mc.Write()
temp2_mc = outputroot.Get(temp_mc.GetName())
if temp_mc.Integral() < 1e-4:
continue
truth_mc = helpers.TH1toTAsym(temp2_mc, efficiency=False)
truth_mc.SetLineColor(CONF.clr_lst[j])
truth_mc.SetMarkerStyle(20 + j)
truth_mc.SetMarkerColor(CONF.clr_lst[j])
truth_mc.SetMarkerSize(1)
truth_mc.SetMaximum(maxbincontent * 1.5)
truth_mc.SetMinimum(minbincontent)
truth_mc.GetXaxis().SetTitle(cut.split("/")[1].replace("_", " "))
canv.cd()
#reset data style
if "data" in f:
truth_mc.SetLineColor(1)
truth_mc.SetMarkerColor(1)
if j==0:
truth_mc.Draw("APC")
else:
truth_mc.Draw("PC")
allmc.append(truth_mc)
legend.AddEntry(truth_mc, info, "apl")
input_mc.Close()
legend.SetBorderSize(0)
legend.SetMargin(0.3)
legend.SetTextSize(0.04)
legend.Draw()
# draw watermarks
xatlas, yatlas = 0.35, 0.87
atlas = ROOT.TLatex(xatlas, yatlas, "ATLAS Internal")
hh4b = ROOT.TLatex(xatlas, yatlas-0.06, "RSG c=1.0")
lumi = ROOT.TLatex(xatlas, yatlas-0.12, "MC #sqrt{s} = 13 TeV")
watermarks = [atlas, hh4b, lumi]
for wm in watermarks:
wm.SetTextAlign(22)
wm.SetTextSize(0.04)
wm.SetTextFont(42)
wm.SetNDC()
wm.Draw()
# finish up
outputroot.cd()
helpers.checkpath(CONF.inputpath + inputdir + "/Plot/Truth/")
#print CONF.inputpath + inputdir + "/Plot/Truth/"
canv.SaveAs(CONF.inputpath + inputdir + "/Plot/Truth/" + canv.GetName() + "_" + cut.replace("/", "_") + ".pdf")
canv.Close()
def ratioerror(a, b):
if a > 0:
return a / b * ROOT.TMath.Sqrt(1.0/a + 1.0/b)
else:
return 0
def options():
parser = argparse.ArgumentParser()
parser.add_argument("--plotter")
parser.add_argument("--inputdir", default=CONF.workdir)
return parser.parse_args()
def fatal(message):
sys.exit("Error in %s: %s" % (__file__, message))
def warn(message):
print
print "Warning in %s: %s" % (__file__, message)
print
if __name__ == "__main__":
main()