plotter.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/usr/bin/env python3
  2. import math
  3. import matplotlib as mpl
  4. # mpl.rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']})
  5. # mpl.rc('font', **{'family': 'serif', 'serif': ['Palatino']})
  6. mpl.rc('text', usetex=True)
  7. mpl.rc('figure', dpi=200)
  8. mpl.rc('savefig', dpi=200)
  9. def add_decorations(axes, luminosity, energy):
  10. cms_prelim = r'{\raggedright{}\textsf{\textbf{CMS}}\\ \emph{Preliminary}}'
  11. axes.text(0.01, 0.98, cms_prelim,
  12. horizontalalignment='left',
  13. verticalalignment='top',
  14. transform=axes.transAxes)
  15. lumi = ""
  16. energy_str = ""
  17. if luminosity is not None:
  18. lumi = r'${} \mathrm{{fb}}^{{-1}}$'.format(luminosity)
  19. if energy is not None:
  20. energy_str = r'({} TeV)'.format(energy)
  21. axes.text(1, 1, ' '.join([lumi, energy_str]),
  22. horizontalalignment='right',
  23. verticalalignment='bottom',
  24. transform=axes.transAxes)
  25. def to_bin_list(th1, include_errors=False):
  26. bins = []
  27. for i in range(th1.GetNbinsX()):
  28. center = th1.GetBinCenter(i + 1)
  29. width = th1.GetBinWidth(i + 1)
  30. content = th1.GetBinContent(i + 1)
  31. if include_errors:
  32. error = th1.GetBinError(i + 1)
  33. bins.append((center-width/2, center+width/2, (content, error)))
  34. else:
  35. bins.append((center-width/2, center+width/2, content))
  36. return bins
  37. def histogram(th1, include_errors=False):
  38. edges = []
  39. values = []
  40. bin_list = to_bin_list(th1, include_errors)
  41. for (l_edge, _, val) in bin_list:
  42. edges.append(l_edge)
  43. values.append(val)
  44. edges.append(bin_list[-1][1])
  45. return values, edges
  46. def histogram2d(th2, include_errors=False):
  47. """ converts TH2 object to something amenable to
  48. plotting w/ matplotlab's pcolormesh
  49. """
  50. import numpy as np
  51. nbins_x = th2.GetNbinsX()
  52. nbins_y = th2.GetNbinsY()
  53. xs = np.zeros((nbins_y, nbins_x), np.float64)
  54. ys = np.zeros((nbins_y, nbins_x), np.float64)
  55. zs = np.zeros((nbins_y, nbins_x), np.float64)
  56. for i in range(nbins_x):
  57. for j in range(nbins_y):
  58. xs[j][i] = th2.GetXaxis().GetBinLowEdge(i+1)
  59. ys[j][i] = th2.GetYaxis().GetBinLowEdge(j+1)
  60. zs[j][i] = th2.GetBinContent(i+1, j+1)
  61. # just_xs = np.array([th2.GetXaxes().GetBinLowEdge(i) for i in range(1,nbins_x)] +
  62. # [th2.GetXaxes().GetBinHighEdge(nbins_x-1)])
  63. # just_ys = np.array([th2.GetYaxes().GetBinLowEdge(i) for i in range(1,nbins_y)] +
  64. # [th2.GetYaxes().GetBinHighEdge(nbins_y-1)])
  65. return xs, ys, zs
  66. class StackHist:
  67. def __init__(self, title=""):
  68. self.title = title
  69. self.xlabel = ""
  70. self.ylabel = ""
  71. self.xlim = (None, None)
  72. self.ylim = (None, None)
  73. self.logx = False
  74. self.logy = False
  75. self.backgrounds = []
  76. self.signal = None
  77. self.signal_stack = True
  78. self.data = None
  79. def add_mc_background(self, th1, label, lumi=None, plot_color=''):
  80. self.backgrounds.append((label, lumi, to_bin_list(th1), plot_color))
  81. def set_mc_signal(self, th1, label, lumi=None, stack=True, scale=1, plot_color=''):
  82. self.signal = (label, lumi, to_bin_list(th1), plot_color)
  83. self.signal_stack = stack
  84. self.signal_scale = scale
  85. def set_data(self, th1, lumi=None, plot_color=''):
  86. self.data = ('data', lumi, to_bin_list(th1), plot_color)
  87. self.luminosity = lumi
  88. def _verify_binning_match(self):
  89. bins_count = [len(bins) for _, _, bins, _ in self.backgrounds]
  90. if self.signal is not None:
  91. bins_count.append(len(self.signal[2]))
  92. if self.data is not None:
  93. bins_count.append(len(self.data[2]))
  94. n_bins = bins_count[0]
  95. if any(bin_count != n_bins for bin_count in bins_count):
  96. raise ValueError("all histograms must have the same number of bins")
  97. self.n_bins = n_bins
  98. def save(self, filename, **kwargs):
  99. import matplotlib.pyplot as plt
  100. plt.ioff()
  101. fig = plt.figure()
  102. ax = fig.gca()
  103. self.do_draw(ax, **kwargs)
  104. fig.savefig("figures/"+filename, transparent=True)
  105. plt.close(fig)
  106. plt.ion()
  107. def do_draw(self, axes):
  108. self.axeses = [axes]
  109. self._verify_binning_match()
  110. bottoms = [0]*self.n_bins
  111. if self.logx:
  112. axes.set_xscale('log')
  113. if self.logy:
  114. axes.set_yscale('log')
  115. def draw_bar(label, lumi, bins, plot_color, scale=1, stack=True, **kwargs):
  116. if stack:
  117. lefts = []
  118. widths = []
  119. heights = []
  120. for left, right, content in bins:
  121. lefts.append(left)
  122. widths.append(right-left)
  123. if lumi is not None:
  124. content *= self.luminosity/lumi
  125. content *= scale
  126. heights.append(content)
  127. axes.bar(lefts, heights, widths, bottoms, label=label, color=plot_color, **kwargs)
  128. for i, (_, _, content) in enumerate(bins):
  129. if lumi is not None:
  130. content *= self.luminosity/lumi
  131. content *= scale
  132. bottoms[i] += content
  133. else:
  134. xs = [bins[0][0] - (bins[0][1]-bins[0][0])/2]
  135. ys = [0]
  136. for left, right, content in bins:
  137. width2 = (right-left)/2
  138. if lumi is not None:
  139. content *= self.luminosity/lumi
  140. content *= scale
  141. xs.append(left-width2)
  142. ys.append(content)
  143. xs.append(right-width2)
  144. ys.append(content)
  145. xs.append(bins[-1][0] + (bins[-1][1]-bins[-1][0])/2)
  146. ys.append(0)
  147. axes.plot(xs, ys, label=label, color=plot_color, **kwargs)
  148. if self.signal is not None and self.signal_stack:
  149. label, lumi, bins, plot_color = self.signal
  150. if self.signal_scale != 1:
  151. label = r"{}$\times{:d}$".format(label, self.signal_scale)
  152. draw_bar(label, lumi, bins, plot_color, scale=self.signal_scale, hatch='/')
  153. for background in self.backgrounds:
  154. draw_bar(*background)
  155. if self.signal is not None and not self.signal_stack:
  156. # draw_bar(*self.signal, stack=False, color='k')
  157. label, lumi, bins, plot_color = self.signal
  158. if self.signal_scale != 1:
  159. label = r"{}$\times{:d}$".format(label, self.signal_scale)
  160. draw_bar(label, lumi, bins, plot_color, scale=self.signal_scale, stack=False)
  161. axes.set_title(self.title)
  162. axes.set_xlabel(self.xlabel)
  163. axes.set_ylabel(self.ylabel)
  164. axes.set_xlim(*self.xlim)
  165. # axes.set_ylim(*self.ylim)
  166. if self.logy:
  167. axes.set_ylim(None, math.exp(math.log(max(bottoms))*1.4))
  168. else:
  169. axes.set_ylim(None, max(bottoms)*1.2)
  170. axes.legend(frameon=True, ncol=2)
  171. add_decorations(axes, self.luminosity, self.energy)
  172. def draw(self, axes, save=False, filename=None, **kwargs):
  173. self.do_draw(axes, **kwargs)
  174. if save:
  175. if filename is None:
  176. filename = "".join(c for c in self.title if c.isalnum() or c in (' ._+-'))+".png"
  177. self.save(filename, **kwargs)
  178. class StackHistWithSignificance(StackHist):
  179. def __init__(self, *args, **kwargs):
  180. super().__init__(*args, **kwargs)
  181. def do_draw(self, axes, bin_significance=True, low_cut_significance=False, high_cut_significance=False):
  182. bottom_box, _, top_box = axes.get_position().splity(0.28, 0.30)
  183. axes.set_position(top_box)
  184. super().do_draw(axes)
  185. axes.set_xticks([])
  186. rhs_color = '#cc6600'
  187. bottom = axes.get_figure().add_axes(bottom_box)
  188. bottom_rhs = bottom.twinx()
  189. bgs = [0]*self.n_bins
  190. for (_, _, bins, _) in self.backgrounds:
  191. for i, (left, right, value) in enumerate(bins):
  192. bgs[i] += value
  193. sigs = [0]*self.n_bins
  194. if bin_significance:
  195. xs = []
  196. for i, (left, right, value) in enumerate(self.signal[2]):
  197. sigs[i] += value
  198. xs.append(left)
  199. xs, ys = zip(*[(x, sig/(sig+bg)) for x, sig, bg in zip(xs, sigs, bgs) if (sig+bg)>0])
  200. bottom.plot(xs, ys, '.k')
  201. if high_cut_significance:
  202. # s/(s+b) for events passing a minimum cut requirement
  203. min_bg = [sum(bgs[i:]) for i in range(self.n_bins)]
  204. min_sig = [sum(sigs[i:]) for i in range(self.n_bins)]
  205. min_xs, min_ys = zip(*[(x, sig/math.sqrt(sig+bg)) for x, sig, bg in zip(xs, min_sig, min_bg)
  206. if (sig+bg) > 0])
  207. bottom_rhs.plot(min_xs, min_ys, '->', color=rhs_color)
  208. if low_cut_significance:
  209. # s/(s+b) for events passing a maximum cut requirement
  210. max_bg = [sum(bgs[:i]) for i in range(self.n_bins)]
  211. max_sig = [sum(sigs[:i]) for i in range(self.n_bins)]
  212. max_xs, max_ys = zip(*[(x, sig/math.sqrt(sig+bg)) for x, sig, bg in zip(xs, max_sig, max_bg)
  213. if (sig+bg) > 0])
  214. bottom_rhs.plot(max_xs, max_ys, '-<', color=rhs_color)
  215. bottom.set_ylabel(r'$S/(S+B)$')
  216. bottom.set_xlim(axes.get_xlim())
  217. bottom.set_ylim((0, 1.1))
  218. if low_cut_significance or high_cut_significance:
  219. bottom_rhs.set_ylabel(r'$S/\sqrt{S+B}$')
  220. bottom_rhs.yaxis.label.set_color(rhs_color)
  221. bottom_rhs.tick_params(axis='y', colors=rhs_color, size=4, width=1.5)
  222. # bottom.grid()
  223. if __name__ == '__main__':
  224. import matplotlib.pyplot as plt
  225. from utils import ResultSet
  226. rs_TTZ = ResultSet("TTZ", "../data/TTZToLLNuNu_treeProducerSusyMultilepton_tree.root")
  227. rs_TTW = ResultSet("TTW", "../data/TTWToLNu_treeProducerSusyMultilepton_tree.root")
  228. rs_TTH = ResultSet("TTH", "../data/TTHnobb_mWCutfix_ext1_treeProducerSusyMultilepton_tree.root")
  229. rs_TTTT = ResultSet("TTTT", "../data/TTTT_ext_treeProducerSusyMultilepton_tree.root")
  230. sh = StackHist('B-Jet Multiplicity')
  231. sh.add_mc_background(rs_TTZ.b_jet_count, 'TTZ', lumi=40)
  232. sh.add_mc_background(rs_TTW.b_jet_count, 'TTW', lumi=40)
  233. sh.add_mc_background(rs_TTH.b_jet_count, 'TTH', lumi=40)
  234. sh.set_mc_signal(rs_TTTT.b_jet_count, 'TTTT', lumi=40, scale=10)
  235. sh.luminosity = 40
  236. sh.energy = 13
  237. sh.xlabel = 'B-Jet Count'
  238. sh.ylabel = r'\# Events'
  239. sh.xlim = (-.5, 9.5)
  240. sh.signal_stack = False
  241. fig = plt.figure()
  242. sh.draw(fig.gca())
  243. plt.show()
  244. # sh.add_data(rs_TTZ.b_jet_count, 'TTZ')