Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
plotUtil.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file plotUtil.py
1 from ROOT import *
2 
3 TickSize = 0.03
4 AxisTitleSize = 0.05
5 AxisLabelSize = 0.04
6 LeftMargin = 0.15
7 RightMargin = 0.08
8 TopMargin = 0.08
9 BottomMargin = 0.13
10 
11 black_hex = '#1B1A17'
12 red_hex = '#9A031E'
13 blue_hex = '#0B60B0'
14 green_hex = '#186F65'
15 orange_hex = '#e99960'
16 purple_hex = '#7F167F'
17 pink_hex = '#FFC0CB'
18 yellow_hex = '#ffcc66'
19 cyan_hex = '#7FE9DE'
20 
21 def colorset(i):
22  if i == 1:
23  return [black_hex]
24  elif i == 2:
25  return [blue_hex, red_hex]
26  elif i == 3:
27  return [blue_hex, red_hex, green_hex]
28  elif i == 4:
29  return [blue_hex, red_hex, green_hex, orange_hex]
30  elif i == 5:
31  return [blue_hex, red_hex, green_hex, orange_hex, purple_hex]
32  else:
33  print ("Attempt to use more than 5 colors")
34  return [blue_hex, red_hex, green_hex, orange_hex, purple_hex, pink_hex, yellow_hex, cyan_hex]
35 
36 
37 def Draw_1Dhist(hist, IsData, norm1, logy, ymaxscale, XaxisName, Ytitle_unit, outname):
38  hist.Sumw2()
39  binwidth = hist.GetXaxis().GetBinWidth(1)
40  c = TCanvas('c', 'c', 800, 700)
41  if norm1:
42  hist.Scale(1. / hist.Integral(-1, -1))
43  if logy:
44  c.SetLogy()
45  c.cd()
46  gPad.SetRightMargin(RightMargin)
47  gPad.SetTopMargin(TopMargin)
48  gPad.SetLeftMargin(LeftMargin)
49  gPad.SetBottomMargin(BottomMargin)
50  if norm1:
51  if Ytitle_unit == '':
52  hist.GetYaxis().SetTitle(
53  'Normalized entries / ({:g})'.format(binwidth))
54  else:
55  hist.GetYaxis().SetTitle(
56  'Normalized entries / ({:g} {unit})'.format(binwidth, unit=Ytitle_unit))
57  else:
58  if Ytitle_unit == '':
59  hist.GetYaxis().SetTitle('Entries / ({:g})'.format(binwidth))
60  else:
61  hist.GetYaxis().SetTitle(
62  'Entries / ({:g} {unit})'.format(binwidth, unit=Ytitle_unit))
63 
64  # hist.GetXaxis().SetRangeUser(hist.GetBinLowEdge(1)-binwidth, hist.GetBinLowEdge(hist.GetNbinsX())+2*binwidth)
65  if logy:
66  hist.GetYaxis().SetRangeUser(hist.GetMinimum(0)*0.5, (hist.GetMaximum()) * ymaxscale)
67  else:
68  hist.GetYaxis().SetRangeUser(0., (hist.GetMaximum()) * ymaxscale)
69  hist.GetXaxis().SetTitle(XaxisName)
70  hist.GetXaxis().SetTickSize(TickSize)
71  hist.GetXaxis().SetTitleSize(AxisTitleSize)
72  hist.GetXaxis().SetLabelSize(AxisLabelSize)
73  hist.GetYaxis().SetTickSize(TickSize)
74  hist.GetYaxis().SetTitleSize(AxisTitleSize)
75  hist.GetYaxis().SetLabelSize(AxisLabelSize)
76  hist.GetXaxis().SetTitleOffset(1.1)
77  hist.GetYaxis().SetTitleOffset(1.35)
78  hist.SetLineColor(1)
79  hist.SetLineWidth(2)
80  hist.Draw('hist')
81  leg = TLegend((1-RightMargin)-0.45, (1-TopMargin)-0.13,
82  (1-RightMargin)-0.1, (1-TopMargin)-0.03)
83  leg.SetTextSize(0.04)
84  leg.SetFillStyle(0)
85  if IsData:
86  leg.AddEntry("", "#it{#bf{sPHENIX}} Work-in-progress", "")
87  leg.AddEntry("", "Au+Au #sqrt{s_{NN}}=200 GeV", "")
88  else:
89  leg.AddEntry("", "#it{#bf{sPHENIX}} Simulation", "")
90  leg.AddEntry("", "Au+Au #sqrt{s_{NN}}=200 GeV", "")
91  leg.Draw()
92  c.RedrawAxis()
93  c.Draw()
94  c.SaveAs(outname+'.pdf')
95  c.SaveAs(outname+'.png')
96  if(c):
97  c.Close()
98  gSystem.ProcessEvents()
99  del c
100  c = 0
101 
102 
103 def Draw_1DhistsComp(lhist, norm1, logx, logy, ymaxscale, XaxisName, Ytitle_unit, outname):
104  color = ['#1B1A17', '#035397', '#9B0000']
105  legtext = ['1st+2nd layers', '2nd+3rd layers', '1st+3rd layers']
106  ymax = -1
107  ymin = 10e10
108  for h in lhist:
109  h.Sumw2()
110  if h.GetMaximum() > ymax:
111  ymax = h.GetMaximum()
112  if h.GetMinimum(0) < ymin:
113  ymin = h.GetMinimum(0)
114  if norm1:
115  h.Scale(1. / h.Integral(-1, -1))
116  ymax = h.GetMaximum()
117  ymin = h.GetMinimum(0)
118 
119  binwidth_bin1 = lhist[0].GetXaxis().GetBinWidth(1)
120  binwidth_bin2 = lhist[0].GetXaxis().GetBinWidth(2)
121  printbinwidth = True
122  if binwidth_bin1 != binwidth_bin2:
123  printbinwidth = False
124 
125  c = TCanvas('c', 'c', 800, 700)
126  if logx:
127  c.SetLogx()
128  if logy:
129  c.SetLogy()
130  c.cd()
131  gPad.SetRightMargin(RightMargin)
132  gPad.SetTopMargin(TopMargin)
133  gPad.SetLeftMargin(LeftMargin)
134  gPad.SetBottomMargin(BottomMargin)
135  if printbinwidth:
136  if norm1:
137  if Ytitle_unit == '':
138  lhist[0].GetYaxis().SetTitle('Normalized entries / ({:g})'.format(binwidth_bin1))
139  else:
140  lhist[0].GetYaxis().SetTitle('Normalized entries / ({:g} {unit})'.format(binwidth_bin1, unit=Ytitle_unit))
141  else:
142  if Ytitle_unit == '':
143  lhist[0].GetYaxis().SetTitle('Entries / ({:g})'.format(binwidth_bin1))
144  else:
145  lhist[0].GetYaxis().SetTitle('Entries / ({:g} {unit})'.format(binwidth_bin1, unit=Ytitle_unit))
146  else:
147  if norm1:
148  if Ytitle_unit == '':
149  lhist[0].GetYaxis().SetTitle('Normalized entries')
150  else:
151  lhist[0].GetYaxis().SetTitle('Normalized entries {unit})'.format(unit=Ytitle_unit))
152  else:
153  if Ytitle_unit == '':
154  lhist[0].GetYaxis().SetTitle('Entries')
155  else:
156  lhist[0].GetYaxis().SetTitle('Entries {unit}'.format(unit=Ytitle_unit))
157  # lhist[0].GetXaxis().SetRangeUser(lhist[0].GetBinLowEdge(1)-binwidth, lhist[0].GetBinLowEdge(lhist[0].GetNbinsX())+2*binwidth)
158  if logy:
159  lhist[0].GetYaxis().SetRangeUser(ymin * 0.05, ymax * 100)
160  else:
161  lhist[0].GetYaxis().SetRangeUser(0., ymax * ymaxscale)
162  lhist[0].GetXaxis().SetTitle(XaxisName)
163  lhist[0].GetXaxis().SetTickSize(TickSize)
164  lhist[0].GetXaxis().SetTitleSize(AxisTitleSize)
165  lhist[0].GetXaxis().SetLabelSize(AxisLabelSize)
166  lhist[0].GetYaxis().SetTickSize(TickSize)
167  lhist[0].GetYaxis().SetTitleSize(AxisTitleSize)
168  lhist[0].GetYaxis().SetLabelSize(AxisLabelSize)
169  lhist[0].GetXaxis().SetTitleOffset(1.1)
170  lhist[0].GetYaxis().SetTitleOffset(1.4)
171  for i, h in enumerate(lhist):
172  if i == 0:
173  h.SetLineColor(TColor.GetColor(color[i]))
174  h.SetLineWidth(2)
175  h.Draw('hist')
176  else:
177  h.SetLineColor(TColor.GetColor(color[i]))
178  h.SetLineWidth(2)
179  h.Draw('histsame')
180 
181  leg = TLegend((1-RightMargin)-0.45, (1-TopMargin)-0.15,
182  (1-RightMargin)-0.1, (1-TopMargin)-0.03)
183  leg.SetTextSize(0.045)
184  leg.SetFillStyle(0)
185  leg.AddEntry("", "#it{#bf{sPHENIX}} Simulation", "")
186  leg.AddEntry("", "Au+Au #sqrt{s_{NN}}=200 GeV", "")
187  leg.Draw()
188 
189  leg1 = TLegend(LeftMargin+0.04, (1-TopMargin)-0.21,
190  LeftMargin+0.34, (1-TopMargin)-0.03)
191  leg1.SetTextSize(0.035)
192  leg1.SetFillStyle(0)
193  for i, h in enumerate(lhist):
194  leg1.AddEntry(h, legtext[i], "l")
195  leg1.Draw()
196  c.RedrawAxis()
197  c.Draw()
198  c.SaveAs(outname+'.pdf')
199  c.SaveAs(outname+'.png')
200  if(c):
201  c.Close()
202  gSystem.ProcessEvents()
203  del c
204  c = 0
205 
206 
207 def Draw_2Dhist(hist, IsData, logz, norm1, rmargin, XaxisName, YaxisName, drawopt, outname):
208  c = TCanvas('c', 'c', 800, 700)
209  if logz:
210  c.SetLogz()
211  c.cd()
212  gPad.SetRightMargin(rmargin)
213  gPad.SetTopMargin(TopMargin)
214  gPad.SetLeftMargin(LeftMargin)
215  gPad.SetBottomMargin(BottomMargin)
216  if norm1:
217  hist.Scale(1. / hist.Integral(-1, -1, -1, -1))
218  hist.GetXaxis().SetTitle(XaxisName)
219  hist.GetYaxis().SetTitle(YaxisName)
220  hist.GetXaxis().SetTickSize(TickSize)
221  hist.GetYaxis().SetTickSize(TickSize)
222  hist.GetXaxis().SetTitleSize(AxisTitleSize)
223  hist.GetYaxis().SetTitleSize(AxisTitleSize)
224  hist.GetXaxis().SetLabelSize(AxisLabelSize)
225  hist.GetYaxis().SetLabelSize(AxisLabelSize)
226  hist.GetXaxis().SetTitleOffset(1.1)
227  hist.GetYaxis().SetTitleOffset(1.3)
228  hist.GetZaxis().SetLabelSize(AxisLabelSize)
229  hist.SetContour(1000)
230  hist.Draw(drawopt)
231 
232  # leg = TLegend(LeftMargin, 1-TopMargin*1.1, LeftMargin+0.01, 0.98)
233  # leg.SetFillStyle(0)
234  # if IsData:
235  # leg.AddEntry("", "#it{#bf{sPHENIX}} Work-in-progress", "")
236  # leg.AddEntry("", "Au+Au #sqrt{s_{NN}}=200 GeV", "")
237  # else:
238  # leg.AddEntry("", "#it{#bf{sPHENIX}} Simulation", "")
239  # leg.AddEntry("", "Au+Au #sqrt{s_{NN}}=200 GeV", "")
240  # leg.Draw()
241  c.RedrawAxis()
242  c.Draw()
243  c.SaveAs(outname+'.pdf')
244  c.SaveAs(outname+'.png')
245  if(c):
246  c.Close()
247  gSystem.ProcessEvents()
248  del c
249  c = 0
250 
251 def plot_Stack(totalhist, list_hist, color, list_legtext, logy, ymaxscale, XaxisName, Ytitle_unit, plotname):
252  # color = ['#073b4c','#118ab2','#06d6a0','#ffd166','#ef476f']
253  binwidth = totalhist.GetXaxis().GetBinWidth(1)
254  hs = THStack('hs','hs');
255  for i, hist in enumerate(list_hist):
256  hist.SetLineColor(1)
257  hist.SetLineWidth(1)
258  hist.SetFillColor(TColor.GetColor(color[i]))
259  hs.Add(hist)
260 
261  # print(totalhist.GetMaximum(), hs.GetMaximum())
262 
263  c = TCanvas('c', 'c', 800, 700)
264  if logy:
265  c.SetLogy()
266  c.cd()
267  hs.Draw()
268  hs.GetXaxis().SetTitle(XaxisName)
269  if Ytitle_unit == '':
270  hist.GetYaxis().SetTitle('Entries / ({:g})'.format(binwidth))
271  else:
272  hist.GetYaxis().SetTitle('Entries / ({:g} {unit})'.format(binwidth, unit=Ytitle_unit))
273  hs.GetXaxis().SetTitleSize(AxisTitleSize)
274  hs.GetYaxis().SetTitleSize(AxisTitleSize)
275  hs.GetXaxis().SetTickSize(TickSize)
276  hs.GetYaxis().SetTickSize(TickSize)
277  hs.GetXaxis().SetLabelSize(AxisLabelSize)
278  hs.GetYaxis().SetLabelSize(AxisLabelSize)
279  hs.GetYaxis().SetTitleOffset(1.3)
280  hs.SetMaximum(totalhist.GetMaximum() * ymaxscale)
281  hs.SetMinimum(0.1)
282  totalhist.SetLineWidth(3)
283  totalhist.SetLineColor(1)
284  totalhist.Draw('histsame')
285  c.Update()
286  leg = TLegend((1-RightMargin)-0.45, (1-TopMargin)-0.15,
287  (1-RightMargin)-0.1, (1-TopMargin)-0.03)
288  leg.SetTextSize(0.045)
289  leg.SetFillStyle(0)
290  leg.AddEntry('', '#it{#bf{sPHENIX}} Simulation', '')
291  leg.AddEntry('', 'Au+Au #sqrt{s_{NN}}=200 GeV', '')
292  leg.Draw()
293  leg1 = TLegend(LeftMargin+0.05, (1-TopMargin)-0.22,
294  LeftMargin+0.3, (1-TopMargin)-0.01)
295  # leg1.SetNColumns(3)
296  leg1.SetTextSize(0.03)
297  leg1.SetFillStyle(0)
298  for i, text in enumerate(list_legtext):
299  leg1.AddEntry(list_hist[i], text, 'f')
300  leg1.Draw()
301  c.Update()
302  c.SaveAs(plotname+'.png')
303  c.SaveAs(plotname+'.pdf')
304  if(c):
305  c.Close()
306  gSystem.ProcessEvents()
307  del c
308  c = 0
309 
310 
311 def GetHistogram(filename, histname):
312  f = TFile(filename, 'r')
313  hm = f.Get(histname)
314  hm.SetDirectory(0)
315  f.Close()
316  return hm
317 
318 
319 def str_pttop(s):
320  return str(s).replace('.', 'p')
321