ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
plot_aggregates.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import argparse
4 import functools
5 import os
6 import ROOT
7 import numpy
8 
9 @functools.total_ordering
10 class Result(object):
11  def __init__(self, line):
12  parts = line.split()
13  self.mglu = int(parts[0])
14  self.mlsp = int(parts[1])
15  self.lumi = float(parts[2])
16  self.met = float(parts[3])
17  self.njets = int(parts[4])
18  self.nbm = int(parts[5])
19  self.tkveto = (parts[6] == "True")
20  self.limit = float(parts[7])
21  self.significance = float(parts[8])
22 
23  def __str__(self):
24  return "Result(mglu="+str(self.mglu)+", mlsp="+str(self.mlsp)+", lumi="+str(self.lumi)+", tkveto="+str(self.tkveto)+", nbm="+str(self.nbm)+", njets="+str(self.njets)+", met="+str(self.met)+", limit="+str(self.limit)+", significance="+str(self.significance)+")"
25 
26  def __lt__(self, other):
27  return (self.mglu, self.mlsp, self.lumi, self.tkveto, self.nbm, self.njets, self.met, self.limit, self.significance) < (other.mglu, other.mlsp, other.lumi, other.tkveto, other.nbm, other.njets, other.met, other.limit, other.significance)
28 
29 def GroupResults(results):
30  results.sort()
31  out = list()
32  while len(results) > 0:
33  plot_match = results[0]
34  plot_matches = [r for r in results if r.mglu == plot_match.mglu and r.mlsp == plot_match.mlsp and r.tkveto == plot_match.tkveto and r.nbm == plot_match.nbm]
35 
36  plot = list()
37  while len(plot_matches) > 0:
38  line_match = plot_matches[0]
39  line_matches = [p for p in plot_matches if p.njets == line_match.njets]
40 
41  plot.append(line_matches)
42  for l in line_matches:
43  plot_matches.remove(l)
44  results.remove(l)
45 
46  out.append(plot)
47  return out
48 
49 def GetResults(dir_name):
50  results = list()
51  files = [f for f in os.listdir(dir_name)]
52  for f in files:
53  with open(os.path.join(dir_name, f), "r") as the_file:
54  for line in the_file:
55  results.extend([Result(line)])
56 
57  results = GroupResults(results)
58  return results
59 
60 def GetMax(graphs):
61  return max(max([g.GetY()[i] for i in xrange(0, g.GetN())]) for g in graphs)
62 
63 def GetMin(graphs):
64  return min(min([g.GetY()[i] for i in xrange(0, g.GetN())]) for g in graphs)
65 
66 def GetMaxSignif(results):
67  best_result = None
68  for met_list in results:
69  for result in met_list:
70  if best_result == None or result.significance > best_result.significance:
71  best_result = result
72  return best_result
73 
74 def GetMinLimit(results):
75  best_result = None
76  for met_list in results:
77  for result in met_list:
78  if best_result == None or result.limit < best_result.limit:
79  best_result = result
80  return best_result
81 
82 def GetColor(index):
83  if index == 0:
84  return ROOT.kRed
85  elif index == 1:
86  return ROOT.kOrange
87  elif index == 2:
88  return ROOT.kGreen
89  elif index == 3:
90  return ROOT.kCyan
91  elif index == 4:
92  return ROOT.kBlue
93  elif index == 5:
94  return ROOT.kBlack
95 
96 def GetTitle(results, is_signif):
97  title = "T1tttt("+str(results[0][0].mglu)+","+str(results[0][0].mlsp)+"), L="+str(results[0][0].lumi)+" fb^{-1}, "
98  if not results[0][0].tkveto:
99  title += "No "
100  title += "Tk Veto, N_{b}#geq "+str(results[0][0].nbm)+": "
101 
102  if is_signif:
103  title += "[signif=%(signif)0.3f]"%{"signif": GetMaxSignif(results).significance}
104  else:
105  title += "[limit=%(limit)0.3f]"%{"limit": GetMinLimit(results).limit}
106 
107  title += ";E_{T}^{miss} [GeV];"+("Significance" if is_signif else "X-Sec Limit/Nominal")
108  return title
109 
110 def Format(graphs, results, is_signif):
111  title = GetTitle(results, is_signif)
112 
113  for i in xrange(0, len(graphs)):
114  graphs[i].SetLineColor(GetColor(i))
115  graphs[i].SetLineWidth(4)
116  graphs[i].SetLineStyle(1)
117  graphs[i].SetMarkerSize(0)
118  graphs[i].SetMarkerColor(0)
119  graphs[i].SetFillStyle(0)
120  graphs[i].SetFillColor(0)
121  graphs[i].SetTitle(title)
122  graphs[i].GetHistogram().SetTitleSize(0.05, "xyz")
123  graphs[i].GetHistogram().SetLabelSize(0.04, "xyz")
124  graphs[i].SetMinimum(0.)
125  graphs[i].SetMaximum(3.)
126 
127 def GetFileName(ex):
128  name = "t1tttt_"+str(ex.mglu)+"_"+str(ex.mlsp)+"_lumi_"+str(ex.lumi)+"_tkveto_"
129  if ex.tkveto:
130  name += "true"
131  else:
132  name += "false"
133  name += "_nbm_"+str(ex.nbm)
134  return name
135 
136 def GetMarker(results, is_signif):
137  best_result = GetMaxSignif(results) if is_signif else GetMinLimit(results)
138  x = best_result.met
139  y = best_result.significance if is_signif else best_result.limit
140  marker = ROOT.TMarker(x,y,20)
141  marker.SetMarkerSize(3)
142  for i in xrange(0, len(results)):
143  for j in xrange(0, len(results[i])):
144  if best_result == results[i][j]:
145  marker.SetMarkerColor(GetColor(i))
146  return marker
147 
148 def MakePlot(results, out_dir):
149  sig_graphs = list()
150  lim_graphs = list()
151 
152  for line in results:
153  met = numpy.asarray([point.met for point in line])
154  sig = numpy.asarray([point.significance for point in line])
155  lim = numpy.asarray([point.limit for point in line])
156  sig_graphs.extend([ROOT.TGraph(len(line), met, sig)])
157  lim_graphs.extend([ROOT.TGraph(len(line), met, lim)])
158 
159  Format(sig_graphs, results, True)
160  Format(lim_graphs, results, False)
161 
162  canvas = ROOT.TCanvas()
163  canvas.SetFillStyle(4000)
164  canvas.SetMargin(0.10, 0.20, 0.12, 0.12)
165  canvas.SetGrid(0,1)
166 
167  legend = ROOT.TLegend(0.80, 0.12, 1.0, 0.88)
168  legend.SetFillStyle(0)
169  legend.SetBorderSize(0)
170  for i in xrange(0, len(sig_graphs)):
171  legend.AddEntry(sig_graphs[i], "N_{jets}#geq "+str(results[i][0].njets), "l")
172 
173  sig_marker = GetMarker(results, True)
174  lim_marker = GetMarker(results, False)
175 
176  file_base = GetFileName(results[0][0])
177 
178  opt = "a l"
179  for g in sig_graphs:
180  g.Draw(opt)
181  opt = "l same"
182  legend.Draw("same")
183  sig_marker.Draw("same")
184  canvas.SetLogy(False)
185  canvas.Print(os.path.join(out_dir, file_base+"_significance.pdf"))
186 
187  opt = "a l"
188  for g in lim_graphs:
189  g.Draw(opt)
190  opt = "l same"
191  legend.Draw("same")
192  lim_marker.Draw("same")
193  canvas.Print(os.path.join(out_dir, file_base+"_limit_lin.pdf"))
194  canvas.SetLogy(True)
195  canvas.Print(os.path.join(out_dir, file_base+"_limit_log.pdf"))
196 
197 if __name__ == "__main__":
198  parser = argparse.ArgumentParser(description="Takes a precomputed set of results with different aggregate binning and makes comparison plots")
199  parser.add_argument("-i", "--input", required=True, help="Directory from which to read precomputed results")
200  parser.add_argument("-o", "--output", required=True, help="Directoy in which to save plots")
201  args = parser.parse_args()
202 
203  results = GetResults(args.input)
204  for plot in results:
205  MakePlot(plot, args.output)
def GetTitle(results, is_signif)
def Format(graphs, results, is_signif)
def __lt__(self, other)
def __init__(self, line)
def GetMaxSignif(results)
def GetMax(graphs)
def GetMinLimit(results)
def GetColor(index)
def GetMin(graphs)
def GetResults(dir_name)
def MakePlot(results, out_dir)
def GroupResults(results)
def GetMarker(results, is_signif)