5 from array
import array
9 NotWantedChars = [
"{",
"}",
"[",
"]",
"#",
"^",
")",
"(",
"'",
" ",
"-"]
10 ReplaceChars = [
".",
","]
11 for C
in NotWantedChars:
12 Out = Out.replace(C,
"")
13 for C
in ReplaceChars:
14 Out = Out.replace(C,
"_")
38 if bins > 0
and not log_binning:
39 self.
__width = (bmax-bmin)/ bins
40 self.
__TH1 = ROOT.TH1D(name,
"Diagnostic histogram", bins, bmin, bmax)
43 log_start = math.log(bmin)
44 log_end = math.log(bmax)
45 log_step = (log_end -log_start) / bins
46 binning =
array(
"f", [math.exp(log_start +n*log_step)
for n
in range(bins+1)])
47 self.
__TH1 = ROOT.TH1D(name,
"Diagnostic histogram", bins, binning)
51 def name(self):
return self.__name
52 def TH1(self):
return self.__TH1
53 def fill(self, value, weight=1.):
60 try: self.
__error[rng] += weight**2
61 except: self.
__error[rng] = weight**2
67 if not self.
__TH1:
return
70 self.
TH1().SetBinContent(self.
TH1().GetNbinsX(),self.
TH1().GetBinContent(self.
TH1().GetNbinsX()+1) + self.
TH1().GetBinContent(self.
TH1().GetNbinsX()))
71 self.
TH1().SetBinError(self.
TH1().GetNbinsX(), math.sqrt( (self.
TH1().GetBinError(self.
TH1().GetNbinsX()+1) + self.
TH1().GetBinError(self.
TH1().GetNbinsX()))**2))
73 self.
TH1().SetBinContent(1,self.
TH1().GetBinContent(0) + self.
TH1().GetBinContent(1))
74 self.
TH1().SetBinError(1, math.sqrt( (self.
TH1().GetBinError(0) + self.
TH1().GetBinError(1))**2))
80 except:
return float(
'nan')
83 except:
return float(
'nan')
95 self.
__TH1.SetBinError(bin, math.sqrt(self.
__error[rng]))
98 def __init__(self, size=18, status="Internal", lumi=1., sqrts="13", normalizedToUnity=False):
114 def DrawTLatex(self, x, y, text, size=18, font=43, align=11, ndc=True, color=-1):
116 tex.SetTextAlign(align)
117 tex.SetTextSize(size)
118 tex.SetTextFont(font)
120 if color > -1: tex.SetTextColor(color)
122 tex.DrawLatex(x, y, text)
138 lumiToPrint =
"%.0f" % (self.
__Lumi * 1e6)
141 lumiToPrint =
"%.0f" % (self.
__Lumi * 1e3)
144 lumiToPrint =
"%.1f" % (self.
__Lumi)
153 print "WARNING: There already exists a legend. Will delete the old one"
155 self.
__Legend = ROOT.TLegend(x1, y1, x2, y2)
167 if isinstance(Items, list):
175 self.
__Legend.AddEntry(Item, Item.GetTitle(), Style)
177 self.
__Legend.AddEntry(Item.get(), Item.GetTitle(), Style)
183 print "No legend has been defined yet"
186 Col =
int((N - N % NperCol) / NperCol + (N % NperCol > 0))
195 if isQuad: height = width
199 print "INFO Already found a canvas named: " + cname
201 self.
__Canvas = ROOT.TCanvas(cname, cname, width, height)
205 if not self.
__Canvas:
print "WARNING: No Canvas has been created yet. Please call CreateXPadCanvas() before"
216 self.
__Pad1.SetBottomMargin(0.03)
217 self.
__Pad1.SetTopMargin(0.09)
219 self.
__Pad2.SetTopMargin(0.01)
220 self.
__Pad2.SetBottomMargin(0.35)
231 hist.GetXaxis().SetTitleSize(labelscalefact * hist.GetXaxis().GetTitleSize())
232 hist.GetYaxis().SetTitleSize(labelscalefact * hist.GetYaxis().GetTitleSize())
233 hist.GetXaxis().SetLabelSize(labelscalefact * hist.GetXaxis().GetLabelSize())
234 hist.GetYaxis().SetLabelSize(labelscalefact * hist.GetYaxis().GetLabelSize())
235 hist.GetXaxis().SetTitleOffset(1. / labelscalefact * hist.GetXaxis().GetTitleOffset())
236 hist.GetYaxis().SetTitleOffset(1. / labelscalefact * hist.GetYaxis().GetTitleOffset())
244 hist.GetXaxis().SetTitleSize(labelscalefact * hist.GetXaxis().GetTitleSize())
245 hist.GetYaxis().SetTitleSize(labelscalefact * hist.GetYaxis().GetTitleSize())
246 hist.GetXaxis().SetLabelSize(labelscalefact * hist.GetXaxis().GetLabelSize())
247 hist.GetYaxis().SetLabelSize(labelscalefact * hist.GetYaxis().GetLabelSize())
248 hist.GetXaxis().SetTitleOffset(2.2 / labelscalefact * hist.GetXaxis().GetTitleOffset())
249 hist.GetYaxis().SetTitleOffset(1.05 / labelscalefact * hist.GetYaxis().GetTitleOffset())
251 def drawStyling(self, Template, ymin, ymax, TopPad=True, RemoveLabel=False):
252 self.
__Styling = Template.Clone(
"Style" + Template.GetName())
256 if RemoveLabel ==
True: self.
__Styling.GetXaxis().SetLabelOffset(10.)
261 plot_range_x = (histo.GetXaxis().GetBinUpEdge(histo.GetNbinsX()) - histo.GetXaxis().GetBinLowEdge(1)) / (
263 plot_range_y = (histo.GetMaximum() - histo.GetMinimum())
265 left_margin = self.
GetCanvas().GetLeftMargin()
267 if on_top
and self.
__Pad1:
268 plot_range_y /= (1 - self.
__Pad1.GetTopMargin() - self.
__Pad1.GetBottomMargin())
269 bottom_margin = self.
__Pad1.GetBottomMargin()
272 plot_range_y /= (1 - self.
GetCanvas().GetTopMargin() - self.
GetCanvas().GetBottomMargin())
273 bottom_margin = self.
GetCanvas().GetBottomMargin()
274 elif not on_top
and self.
__Pad2:
275 plot_range_y /= (1 - self.
__Pad2.GetTopMargin() - self.
__Pad2.GetBottomMargin())
276 bottom_margin = self.
__Pad2.GetBottomMargin()
278 for i
in range(1, histo.GetNbinsX() + 1):
280 left_margin + ((histo.GetBinCenter(i) - histo.GetXaxis().GetBinLowEdge(1)) / plot_range_x),
281 bottom_margin + (histo.GetBinContent(i) + off_set_y - histo.GetMinimum()) / plot_range_y,
282 "%.3f" % (histo.GetBinContent(i)),
285 color=histo.GetLineColor(),
289 if Path.find(
"/") != -1:
290 dir_name = Path[ : Path.rfind(
"/") ]
291 os.system(
"mkdir -p %s"%(dir_name))
292 if "png" in outputfiletypes:
294 if not "pdf" in outputfiletypes:
295 outputfiletypes.append(
"pdf")
296 a, b = outputfiletypes.index(
'pdf'), outputfiletypes.index(
'png')
297 outputfiletypes[b], outputfiletypes[a] = outputfiletypes[a], outputfiletypes[b]
301 for otype
in outputfiletypes:
303 self.
__Canvas.SaveAs(
"%s.%s" % (Path, otype))
306 os.system(
"convert -density 300 %s.pdf %s.png" % (Path, Path))