1 from __future__
import absolute_import
2 from __future__
import print_function
5 from array
import array
10 from six.moves
import range
22 """Sets the PubComm recommended style
24 Just a copy of <http://ghm.web.cern.ch/ghm/plots/MacroExample/tdrstyle.C>
25 @sa ModTDRStyle() to use this style with some additional customisation.
28 R.gStyle.SetCanvasBorderMode(0)
29 R.gStyle.SetCanvasColor(R.kWhite)
30 R.gStyle.SetCanvasDefH(600)
31 R.gStyle.SetCanvasDefW(600)
32 R.gStyle.SetCanvasDefX(0)
33 R.gStyle.SetCanvasDefY(0)
36 R.gStyle.SetPadBorderMode(0)
38 R.gStyle.SetPadColor(R.kWhite)
39 R.gStyle.SetPadGridX(
False)
40 R.gStyle.SetPadGridY(
False)
41 R.gStyle.SetGridColor(0)
42 R.gStyle.SetGridStyle(3)
43 R.gStyle.SetGridWidth(1)
46 R.gStyle.SetFrameBorderMode(0)
47 R.gStyle.SetFrameBorderSize(1)
48 R.gStyle.SetFrameFillColor(0)
49 R.gStyle.SetFrameFillStyle(0)
50 R.gStyle.SetFrameLineColor(1)
51 R.gStyle.SetFrameLineStyle(1)
52 R.gStyle.SetFrameLineWidth(1)
57 R.gStyle.SetHistLineColor(1)
58 R.gStyle.SetHistLineStyle(0)
59 R.gStyle.SetHistLineWidth(1)
63 R.gStyle.SetEndErrorSize(2)
67 R.gStyle.SetMarkerStyle(20)
71 R.gStyle.SetFitFormat(
'5.4g')
72 R.gStyle.SetFuncColor(2)
73 R.gStyle.SetFuncStyle(1)
74 R.gStyle.SetFuncWidth(1)
77 R.gStyle.SetOptDate(0)
82 R.gStyle.SetOptFile(0)
83 R.gStyle.SetOptStat(0)
85 R.gStyle.SetStatColor(R.kWhite)
86 R.gStyle.SetStatFont(42)
87 R.gStyle.SetStatFontSize(0.025)
88 R.gStyle.SetStatTextColor(1)
89 R.gStyle.SetStatFormat(
'6.4g')
90 R.gStyle.SetStatBorderSize(1)
91 R.gStyle.SetStatH(0.1)
92 R.gStyle.SetStatW(0.15)
98 R.gStyle.SetPadTopMargin(0.05)
99 R.gStyle.SetPadBottomMargin(0.13)
100 R.gStyle.SetPadLeftMargin(0.16)
101 R.gStyle.SetPadRightMargin(0.02)
104 R.gStyle.SetOptTitle(0)
105 R.gStyle.SetTitleFont(42)
106 R.gStyle.SetTitleColor(1)
107 R.gStyle.SetTitleTextColor(1)
108 R.gStyle.SetTitleFillColor(10)
109 R.gStyle.SetTitleFontSize(0.05)
118 R.gStyle.SetTitleColor(1,
'XYZ')
119 R.gStyle.SetTitleFont(42,
'XYZ')
120 R.gStyle.SetTitleSize(0.06,
'XYZ')
124 R.gStyle.SetTitleXOffset(0.9)
125 R.gStyle.SetTitleYOffset(1.25)
130 R.gStyle.SetLabelColor(1,
'XYZ')
131 R.gStyle.SetLabelFont(42,
'XYZ')
132 R.gStyle.SetLabelOffset(0.007,
'XYZ')
133 R.gStyle.SetLabelSize(0.05,
'XYZ')
137 R.gStyle.SetAxisColor(1,
'XYZ')
138 R.gStyle.SetStripDecimals(
True)
139 R.gStyle.SetTickLength(0.03,
'XYZ')
140 R.gStyle.SetNdivisions(510,
'XYZ')
141 R.gStyle.SetPadTickX(1)
142 R.gStyle.SetPadTickY(1)
145 R.gStyle.SetOptLogx(0)
146 R.gStyle.SetOptLogy(0)
147 R.gStyle.SetOptLogz(0)
150 R.gStyle.SetPaperSize(20., 20.)
163 R.gStyle.SetHatchesLineWidth(5)
164 R.gStyle.SetHatchesSpacing(0.05)
167 def ModTDRStyle(width=600, height=600, t=0.06, b=0.12, l=0.16, r=0.04):
168 """Modified version of the tdrStyle
171 width (int): Canvas width in pixels
172 height (int): Canvas height in pixels
173 t (float): Pad top margin [0-1]
174 b (float): Pad bottom margin [0-1]
175 l (float): Pad left margin [0-1]
176 r (float): Pad right margin [0-1]
181 R.gStyle.SetCanvasDefW(width)
182 R.gStyle.SetCanvasDefH(height)
187 def_w = float(R.gStyle.GetCanvasDefW())
188 def_h = float(R.gStyle.GetCanvasDefH())
190 scale_h = (def_w / def_h)
if (def_h > def_w)
else 1.
191 scale_w = (def_h / def_w)
if (def_w > def_h)
else 1.
193 def_min = def_h
if (def_h < def_w)
else def_w
195 R.gStyle.SetPadTopMargin(t * scale_h)
197 R.gStyle.SetPadBottomMargin(b * scale_h)
199 R.gStyle.SetPadLeftMargin(l * scale_w)
201 R.gStyle.SetPadRightMargin(r * scale_w)
207 R.gStyle.SetNdivisions(506,
'XYZ')
210 R.gStyle.SetMarkerColor(R.kBlack)
211 R.gStyle.SetMarkerSize(1.0)
213 R.gStyle.SetLabelOffset(0.007,
'YZ')
217 R.gStyle.SetLabelOffset(0.005 * (3. - 2. / scale_h),
'X')
226 title_px = title_size * def_min
228 R.gStyle.SetTitleSize(title_size,
'XYZ')
229 R.gStyle.SetLabelSize(label_size,
'XYZ')
231 R.gStyle.SetTitleXOffset(0.5 * scale_h *
232 (1.2 * (def_h * b * scale_h - 0.6 * title_px)) /
234 R.gStyle.SetTitleYOffset(0.5 * scale_w *
235 (1.2 * (def_w * l * scale_w - 0.6 * title_px)) /
239 R.gStyle.SetPadTickX(0)
240 R.gStyle.SetPadTickY(0)
241 R.gStyle.SetTickLength(0.02,
'XYZ')
243 R.gStyle.SetLegendBorderSize(0)
244 R.gStyle.SetLegendFont(42)
245 R.gStyle.SetLegendFillColor(0)
246 R.gStyle.SetFillColor(0)
254 'd', [0.0000, 0.1250, 0.2500, 0.3750, 0.5000, 0.6250, 0.7500, 0.8750, 1.0000])
256 'd', [0.2082, 0.0592, 0.0780, 0.0232, 0.1802, 0.5301, 0.8186, 0.9956, 0.9764])
258 'd', [0.1664, 0.3599, 0.5041, 0.6419, 0.7178, 0.7492, 0.7328, 0.7862, 0.9832])
260 'd', [0.5293, 0.8684, 0.8385, 0.7914, 0.6425, 0.4662, 0.3499, 0.1968, 0.0539])
261 R.TColor.CreateGradientColorTable(nRGBs, stops, red, green, blue, 255, 1)
267 'd', [0.0000, 0.1250, 0.2500, 0.3750, 0.5000, 0.6250, 0.7500, 0.8750, 1.0000])
269 'd', reversed([0./255., 9./255., 13./255., 17./255., 24./255., 32./255., 27./255., 25./255., 29./255.]))
271 'd', reversed([0./255., 0./255., 0./255., 2./255., 37./255., 74./255., 113./255., 160./255., 221./255.]))
273 'd', reversed([28./255., 42./255., 59./255., 78./255., 98./255., 129./255., 154./255., 184./255., 221./255.]))
274 R.TColor.CreateGradientColorTable(nRGBs, stops, red, green, blue, 255, 1)
278 R.TColor.CreateGradientColorTable(3,
279 array (
"d", [0.00, 0.50, 1.00]),
280 array (
"d", [1.00, 1.00, 0.00]),
281 array (
"d", [0.70, 1.00, 0.34]),
282 array (
"d", [0.00, 1.00, 0.82]),
287 adapt = R.gROOT.GetColor(color)
288 new_idx = R.gROOT.GetListOfColors().GetLast() + 1
290 new_idx, adapt.GetRed(), adapt.GetGreen(), adapt.GetBlue(),
'', alpha)
291 COL_STORE.append(trans)
292 trans.SetName(
'userColor%i' % new_idx)
297 for key, value
in six.iteritems(kwargs):
299 getattr(obj,
'Set' + key)()
300 elif isinstance(value, (list, tuple)):
301 getattr(obj,
'Set' + key)(*value)
303 getattr(obj,
'Set' + key)(value)
315 pad = R.TPad(
'pad',
'pad', 0., 0., 1., 1.)
323 upper = R.TPad(
'upper',
'upper', 0., 0., 1., 1.)
324 upper.SetBottomMargin(split_point + gap_high)
325 upper.SetFillStyle(4000)
327 lower = R.TPad(
'lower',
'lower', 0., 0., 1., 1.)
328 lower.SetTopMargin(1 - split_point + gap_low)
329 lower.SetFillStyle(4000)
332 result = [upper, lower]
336 upper2 = R.TPad(
'upper2',
'upper2', 0., 0., 1., 1.)
337 upper2.SetTopMargin(1 - upper_split_point)
338 upper2.SetBottomMargin(split_point + gap_high)
339 upper2.SetFillStyle(4000)
341 upper1 = R.TPad(
'upper1',
'upper1', 0., 0., 1., 1.)
342 upper1.SetBottomMargin(upper_split_point)
343 upper1.SetFillStyle(4000)
345 lower = R.TPad(
'lower',
'lower', 0., 0., 1., 1.)
346 lower.SetTopMargin(1 - split_point + gap_low)
347 lower.SetFillStyle(4000)
350 result = [upper1, lower, upper2]
354 """Create a set of TPads split vertically on the TCanvas
356 This is a generalisation of the two pad main/ratio split but for the case
357 of multiple ratio pads.
361 split_points (list[float]): Height of each ratio pad as a fraction of the
362 canvas height. Pads will be created from the bottom of the frame
363 upwards. The final, main pad will occupy however much space remains,
364 therefore the size of this list should be [number of pads] - 1.
365 gaps_low (list[float]): Gaps between ratio pad frames created on the
366 lower pad side at each boundary. Give a list of zeroes for no gap
367 between pad frames. Should be the same length as `split_points`.1
368 gaps_high (list[float]): Gaps between ratio pad frames created on the
369 upper pad side at each boundary. Give a list of zeroes for no gap
373 list[TPad]: List of TPads, indexed from top to bottom on the canvas.
376 for i
in range(len(split_points)+1):
377 pad = R.TPad(
'pad%i'%i,
'', 0., 0., 1., 1.)
379 pad.SetBottomMargin(sum(split_points[0:i])+gaps_high[i-1])
380 if i < len(split_points):
381 pad.SetTopMargin(1.-sum(split_points[0:i+1])+gaps_low[i])
382 pad.SetFillStyle(4000)
390 left = R.TPad(
'left',
'left', 0., 0., 1., 1.)
391 left.SetRightMargin(1 - split_point + gap_right)
392 left.SetFillStyle(4000)
394 right = R.TPad(
'right',
'right', 0., 0., 1., 1.)
395 right.SetLeftMargin(split_point + gap_left)
396 right.SetFillStyle(4000)
399 result = [left, right]
405 for i
in range(len(split_points)+1):
406 pad = R.TPad(
'pad%i'%i,
'', 0., 0., 1., 1.)
408 pad.SetLeftMargin(sum(split_points[0:i])+gaps_left[i-1])
409 if i < len(split_points):
410 pad.SetRightMargin(1.-sum(split_points[0:i+1])+gaps_right[i])
411 pad.SetFillStyle(4000)
421 if lower.GetXaxis().GetTitle() ==
'':
422 lower.GetXaxis().SetTitle(upper.GetXaxis().GetTitle())
423 upper.GetXaxis().SetTitle(
"")
424 upper.GetXaxis().SetLabelSize(0)
425 upper_h = 1. - pads[0].GetTopMargin() - pads[0].GetBottomMargin()
426 lower_h = 1. - pads[1].GetTopMargin() - pads[1].GetBottomMargin()
427 lower.GetYaxis().SetTickLength(R.gStyle.GetTickLength() * upper_h / lower_h)
430 lower.GetYaxis().SetTitle(y_title)
431 lower.GetYaxis().CenterTitle(y_centered)
433 lower.SetMinimum(y_min)
434 lower.SetMaximum(y_max)
438 width = xaxis.GetBinWidth(1)
439 w_label = (
"%"+fmt) % width
442 yaxis.SetTitle(
"Events / " + w_label)
444 xaxis.SetTitle(var +
" (" + units +
")")
445 yaxis.SetTitle(
"Events / " + w_label +
" " + units)
465 result = src.GetHistogram().Clone(
'tmp')
471 src.GetPoint(0, x, y)
474 for i
in range(1, src.GetN()):
475 src.GetPoint(i, x, y)
480 result.GetXaxis().SetLimits(min, max)
489 res.append(h.Clone(
'tmp%i'%i))
494 pad_obs = pad.GetListOfPrimitives()
499 if obj.InheritsFrom(R.TH1.Class()):
501 if obj.InheritsFrom(R.TMultiGraph.Class()):
502 return obj.GetHistogram()
503 if obj.InheritsFrom(R.TGraph.Class()):
504 return obj.GetHistogram()
505 if obj.InheritsFrom(R.THStack.Class()):
506 return obj.GetHistogram()
518 """Performs a series of tests on a TFile to ensure that it can be opened
522 filename: `str` The name of the TFile to check
525 `bool` True if the file can opened, is not a zombie, and if ROOT did
526 not need to try and recover the contents
528 fin = R.TFile(filename)
531 if fin
and not fin.IsOpen():
533 elif fin
and fin.IsOpen()
and fin.IsZombie():
536 elif fin
and fin.IsOpen()
and fin.TestBit(R.TFile.kRecovered):
546 chain = R.TChain(tree)
553 R.TH1.AddDirectory(
False)
555 res = R.gDirectory.Get(obj)
561 if len(re.findall(param +
'\.\d+\.\d+', filename)):
563 param +
'\.\d+\.\d+', filename)[0].replace(param +
'.',
'')
565 elif len(re.findall(param +
'\.\d+', filename)):
566 num1 = re.findall(param +
'\.\d+', filename)[0].replace(param +
'.',
'')
569 print(
"Error: parameter " + param +
" not found in filename")
582 tree.Draw(xvar +
':' + yvar, selection,
'goff')
583 gr = R.TGraph(tree.GetSelectedRows(), tree.GetV1(), tree.GetV2())
588 tree.Draw(xvar +
':' + yvar +
':' + zvar, selection,
'goff')
590 tree.GetSelectedRows(), tree.GetV1(), tree.GetV2(), tree.GetV3())
595 backup = R.TH1.AddDirectoryStatus()
596 R.TH1.AddDirectory(
False)
600 x_int = h_x.IntegralAndError(0, h_x.GetNbinsX() + 1, x_err)
601 for i
in range(1, h_x.GetNbinsX() + 1):
602 x_part_err = R.Double(0.)
603 x_part_int = h_x.IntegralAndError(i, h_x.GetNbinsX(
604 ) + 1, x_part_err)
if cut_is_greater_than
else h_x.IntegralAndError(0, i, x_part_err)
605 x_den.SetBinContent(i, x_int)
606 x_den.SetBinError(i, x_err)
607 x_num.SetBinContent(i, x_part_int)
608 x_num.SetBinError(i, x_part_err)
612 y_int = h_y.IntegralAndError(0, h_y.GetNbinsX() + 1, y_err)
613 for i
in range(1, h_y.GetNbinsX() + 1):
614 y_part_err = R.Double(0.)
615 y_part_int = h_y.IntegralAndError(i, h_y.GetNbinsX(
616 ) + 1, y_part_err)
if cut_is_greater_than
else h_y.IntegralAndError(0, i, y_part_err)
617 y_den.SetBinContent(i, y_int)
618 y_den.SetBinError(i, y_err)
619 y_num.SetBinContent(i, y_part_int)
620 y_num.SetBinError(i, y_part_err)
625 x_gr = R.TGraphAsymmErrors(x_num, x_den)
626 y_gr = R.TGraphAsymmErrors(y_num, y_den)
629 for i
in range(0, res.GetN()):
630 res.GetX()[i] = x_gr.GetY()[i]
631 res.GetEXlow()[i] = x_gr.GetEYlow()[i]
632 res.GetEXhigh()[i] = x_gr.GetEYhigh()[i]
634 R.TH1.AddDirectory(backup)
641 """Build an empty TH2 from the set of points in a TGraph2D
643 There is no unique way to define a TH2 binning given an arbitrary
644 TGraph2D, therefore this function supports multiple named methods:
646 - `BinEdgeAligned` simply takes the sets of x- and y- values in the
647 TGraph2D and uses these as the bin edge arrays in the TH2. The
648 implication of this is that when filling the bin contents interpolation
649 will be required when evaluating the TGraph2D at the bin centres.
650 - `BinCenterAligned` will try to have the TGraph2D points at the bin
651 centers, but this will only work completely correctly when the input
652 point spacing is regular. The algorithm first identifies the bin width
653 as the smallest interval between points on each axis. The start
654 position of the TH2 axis is then defined as the lowest value in the
655 TGraph2D minus half this width, and the axis continues with regular
656 bins until the graph maximum is passed.
659 graph (TGraph2D): Should have at least two unique x and y values,
660 otherwise we can't define any bins
661 method (str): The binning algorithm to use
662 force_x_width (bool): Override the derived x-axis bin width in the
664 force_y_width (bool): Override the derived y-axis bin width in the
668 RuntimeError: If the method name is not recognised
671 TH2F: The exact binning of the TH2F depends on the chosen method
676 for i
in range(graph.GetN()):
677 x_vals.add(graph.GetX()[i])
678 y_vals.add(graph.GetY()[i])
680 x_vals = sorted(x_vals)
681 y_vals = sorted(y_vals)
682 if method ==
'BinEdgeAligned':
683 h_proto = R.TH2F(
'prototype',
'',
684 len(x_vals) - 1, array(
'd', x_vals),
685 len(y_vals) - 1, array(
'd', y_vals))
686 elif method ==
'BinCenterAligned':
689 for i
in range(1, len(x_vals)):
690 x_widths.append(x_vals[i] - x_vals[i - 1])
691 for i
in range(1, len(y_vals)):
692 y_widths.append(y_vals[i] - y_vals[i - 1])
693 x_min = min(x_widths)
if force_x_width
is None else force_x_width
694 y_min = min(y_widths)
if force_y_width
is None else force_y_width
695 x_bins = int(((x_vals[-1] - (x_vals[0] - 0.5 * x_min)) / x_min) + 0.5)
696 y_bins = int(((y_vals[-1] - (y_vals[0] - 0.5 * y_min)) / y_min) + 0.5)
697 print(
'[TH2FromTGraph2D] x-axis binning: (%i, %g, %g)' % (x_bins, x_vals[0] - 0.5 * x_min, x_vals[0] - 0.5 * x_min + x_bins * x_min))
698 print(
'[TH2FromTGraph2D] y-axis binning: (%i, %g, %g)' % (y_bins, y_vals[0] - 0.5 * y_min, y_vals[0] - 0.5 * y_min + y_bins * y_min))
701 h_proto = R.TH2F(
'prototype',
'',
703 0] - 0.49999 * x_min, x_vals[0] - 0.50001 * x_min + x_bins * x_min,
704 y_bins, y_vals[0] - 0.49999 * y_min, y_vals[0] - 0.50001 * y_min + y_bins * y_min)
707 '[TH2FromTGraph2D] Method %s not supported' % method)
708 h_proto.SetDirectory(0)
713 errorBand = R.TGraphAsymmErrors()
716 for i
in range(LowerGraph.GetN()):
718 (float(LowerGraph.GetX()[i]), float(LowerGraph.GetY()[i])))
720 (float(UpperGraph.GetX()[i]), float(UpperGraph.GetY()[i])))
721 lower_list = sorted(set(lower_list))
722 upper_list = sorted(set(upper_list))
723 for i
in range(LowerGraph.GetN()):
724 errorBand.SetPoint(i, lower_list[i][0], lower_list[i][1])
725 errorBand.SetPointEYlow(i, lower_list[i][1] - lower_list[i][1])
726 errorBand.SetPointEYhigh(i, upper_list[i][1] - lower_list[i][1])
734 xvals.append(float(key))
735 yvals.append(js[key][label])
736 graph = R.TGraph(len(xvals), array(
'd', xvals), array(
'd', yvals))
742 with open(jsfile)
as jsonfile:
743 js = json.load(jsonfile)
749 if isinstance(label,(str,)):
750 for entry
in js[label]:
751 xvals.append(float(entry))
758 graph = R.TGraph(len(xvals), array(
'd', xvals), array(
'd', yvals))
769 with open(jsfile)
as jsonfile:
770 js = json.load(jsonfile)
779 xvals.append(float(key))
780 yvals.append(js[key][central])
781 yvals_lo.append(js[key][central] - js[key][lo])
782 yvals_hi.append(js[key][hi] - js[key][central])
783 graph = R.TGraphAsymmErrors(len(xvals), array(
'd', xvals), array(
'd', yvals), array(
784 'd', [0]), array(
'd', [0]), array(
'd', yvals_lo), array(
'd', yvals_hi))
792 with open(json_file)
as jsonfile:
793 data = json.load(jsonfile)
796 if 'exp0' in draw
or 'exp' in draw:
798 if 'exp1' in draw
or 'exp' in draw:
800 if 'exp2' in draw
or 'exp' in draw:
806 nfind = tree.Draw(y +
":" + x, cut +
"deltaNLL == 0")
809 gr0.SetPoint(0, -999, -999)
811 grc = R.gROOT.FindObject(
"Graph").Clone()
814 gr0.SetPoint(0, grc.GetXmax(), grc.GetYmax())
815 gr0.SetMarkerStyle(34)
816 gr0.SetMarkerSize(2.0)
820 def treeToHist2D(t, x, y, name, cut, xmin, xmax, ymin, ymax, xbins, ybins):
821 t.Draw(
"2*deltaNLL:%s:%s>>%s_prof(%d,%10g,%10g,%d,%10g,%10g)" %
822 (y, x, name, xbins, xmin, xmax, ybins, ymin, ymax), cut +
"deltaNLL != 0",
"PROF")
823 prof = R.gROOT.FindObject(name +
"_prof")
824 h2d = R.TH2D(name, name, xbins, xmin, xmax, ybins, ymin, ymax)
825 for ix
in range(1, xbins + 1):
826 for iy
in range(1, ybins + 1):
827 z = prof.GetBinContent(ix, iy)
828 if (z != z)
or (z > 4294967295):
830 h2d.SetBinContent(ix, iy, z)
831 h2d.GetXaxis().SetTitle(x)
832 h2d.GetYaxis().SetTitle(y)
838 def makeHist1D(name, xbins, graph, scaleXrange=1.0, absoluteXrange=None):
839 len_x = graph.GetX()[graph.GetN() - 1] - graph.GetX()[0]
840 binw_x = (len_x * 0.5 / (float(xbins) - 1.)) - 1E-5
842 hist = R.TH1F(name,
'', xbins, absoluteXrange[0], absoluteXrange[1])
845 name,
'', xbins, graph.GetX()[0], scaleXrange * (graph.GetX()[graph.GetN() - 1] + binw_x))
850 len_x = graph2d.GetXmax() - graph2d.GetXmin()
851 binw_x = (len_x * 0.5 / (float(xbins) - 1.)) - 1E-5
852 len_y = graph2d.GetYmax() - graph2d.GetYmin()
853 binw_y = (len_y * 0.5 / (float(ybins) - 1.)) - 1E-5
854 hist = R.TH2F(name,
'', xbins, graph2d.GetXmin() - binw_x, graph2d.GetXmax() +
855 binw_x, ybins, graph2d.GetYmin() - binw_y, graph2d.GetYmax() + binw_y)
862 xbins_new = [
None] * (len(xbins) + 1)
863 for i
in range(len(xbins) - 1):
865 xbins_new[i] = xbins[i] - ((xbins[i + 1] - xbins[i]) / 2) + 1E-5
867 xbins_new[i] = xbins[i] - ((xbins[i + 1] - xbins[i]) / 2)
868 xbins_new[len(xbins) - 1] = xbins[len(xbins) - 2] + \
869 ((xbins[len(xbins) - 2] - xbins[len(xbins) - 3]) / 2)
870 xbins_new[len(xbins)] = xbins[len(xbins) - 1] + \
871 ((xbins[len(xbins) - 1] - xbins[len(xbins) - 2]) / 2) - 1E-5
873 ybins_new = [
None] * (len(ybins) + 1)
874 for i
in range(len(ybins) - 1):
876 ybins_new[i] = ybins[i] - ((ybins[i + 1] - ybins[i]) / 2) + 1E-5
878 ybins_new[i] = ybins[i] - ((ybins[i + 1] - ybins[i]) / 2)
879 ybins_new[len(ybins) - 1] = ybins[len(ybins) - 2] + \
880 ((ybins[len(ybins) - 2] - ybins[len(ybins) - 3]) / 2)
881 ybins_new[len(ybins)] = ybins[len(ybins) - 1] + \
882 ((ybins[len(ybins) - 1] - ybins[len(ybins) - 2]) / 2) - 1E-5
883 hist = R.TH2F(name,
'', len(
884 xbins_new) - 1, array(
'd', xbins_new), len(ybins_new) - 1, array(
'd', ybins_new))
891 if graph1.GetN() != graph2.GetN():
893 for i
in range(graph1.GetN()):
894 xvals.append(graph1.GetX()[i])
896 yvals.append(2*abs(graph1.GetY()[i]-graph2.GetY()[i])/(graph1.GetY()[i]+graph2.GetY()[i]))
898 yvals.append(2*(graph1.GetY()[i]-graph2.GetY()[i])/(graph1.GetY()[i]+graph2.GetY()[i]))
899 diff_graph = R.TGraph(len(xvals),array(
'd',xvals),array(
'd',yvals))
906 for i
in range(num.GetN()):
907 res.GetY()[i] = res.GetY()[i]/den.Eval(res.GetX()[i])
908 if type(res)
is R.TGraphAsymmErrors:
909 for i
in range(num.GetN()):
910 res.GetEYhigh()[i] = res.GetEYhigh()[i]/den.Eval(res.GetX()[i])
911 res.GetEYlow()[i] = res.GetEYlow()[i]/den.Eval(res.GetX()[i])
917 """Make a new ratio TH1 from numerator and denominator TH1s with optional
921 num (TH1): Numerator histogram
922 den (TH1): Denominator histogram
923 num_err (bool): Propagate the error in the numerator TH1
924 den_err (bool): Propagate the error in the denominator TH1
927 TH1: A new TH1 containing the ratio
931 for i
in range(1, result.GetNbinsX()+1):
932 result.SetBinError(i, 0.)
933 den_fix = den.Clone()
935 for i
in range(1, den_fix.GetNbinsX()+1):
936 den_fix.SetBinError(i, 0.)
937 result.Divide(den_fix)
949 while i < graph.GetN() - 1:
950 if graph.GetX()[i + 1] == graph.GetX()[i]:
952 graph.RemovePoint(i + 1)
958 for i
in range(graph.GetN() - 1):
959 graph.GetY()[i] = graph.GetY()[i] + y_off
963 for i
in range(graph.GetN()):
964 if graph.GetY()[i] == val:
965 print(
'[RemoveGraphYAll] Removing point (%f, %f)' % (graph.GetX()[i], graph.GetY()[i]))
972 for i
in range(graph.GetN()):
973 diff = abs(graph.GetY()[i])
975 print(
'[RemoveSmallDelta] Removing point (%f, %f)' % (graph.GetX()[i], graph.GetY()[i]))
982 for i
in range(graph.GetN()):
983 if graph.GetY()[i] > val:
994 for i
in range(graph.GetN()):
995 if graph.GetY()[i] < min :
996 min = graph.GetY()[i]
998 for i
in range(graph.GetN()):
999 graph.SetPoint(i, graph.GetX()[i], graph.GetY()[i]-min)
1007 for i
in range(graph.GetN()):
1008 if graph.GetY()[i] < fit_y:
1010 fit_x = graph.GetX()[i]
1011 fit_y = graph.GetY()[i]
1012 if fit_i == 0
or fit_i == (graph.GetN() - 1):
1014 min_x = graph.GetX()[fit_i]
1015 min_y = graph.GetY()[fit_i]
1016 for i
in range(graph.GetN()):
1017 before = graph.GetY()[i]
1018 graph.GetY()[i] -= min_y
1019 after = graph.GetY()[i]
1020 print(
'Point %i, before=%f, after=%f' % (i, before, after))
1021 return (fit_x, fit_y)
1022 search_min = fit_i - 2
if fit_i >= 2
else fit_i - 1
1023 search_max = fit_i + 2
if fit_i + 2 < graph.GetN()
else fit_i + 1
1024 min_x = func.GetMinimumX(graph.GetX()[search_min], graph.GetX()[search_max])
1025 min_y = func.Eval(min_x)
1026 print(
'[ImproveMinimum] Fit minimum was (%f, %f)' % (fit_x, fit_y))
1027 print(
'[ImproveMinimum] Better minimum was (%f, %f)' % (min_x, min_y))
1029 for i
in range(graph.GetN()):
1030 before = graph.GetY()[i]
1031 graph.GetY()[i] -= min_y
1032 after = graph.GetY()[i]
1033 print(
'Point %i, before=%f, after=%f' % (i, before, after))
1034 graph.Set(graph.GetN() + 1)
1035 graph.SetPoint(graph.GetN() - 1, min_x, 0)
1037 return (min_x, min_y)
1044 for i
in range(graph.GetN() - 1):
1045 if (graph.GetY()[i] - yval) * (graph.GetY()[i + 1] - yval) < 0.:
1046 cross = func.GetX(yval, graph.GetX()[i], graph.GetX()[i + 1])
1047 if (graph.GetY()[i] - yval) > 0.
and current
is None:
1050 'hi': graph.GetX()[graph.GetN() - 1],
1054 if (graph.GetY()[i] - yval) < 0.
and current
is None:
1056 'lo': graph.GetX()[0],
1061 intervals.append(current)
1063 if (graph.GetY()[i] - yval) < 0.
and current
is not None:
1064 current[
'hi'] = cross
1065 current[
'valid_hi'] =
True
1066 intervals.append(current)
1071 crossings.append(cross)
1072 if current
is not None:
1073 intervals.append(current)
1074 if len(intervals) == 0:
1076 'lo': graph.GetX()[0],
1077 'hi': graph.GetX()[graph.GetN() - 1],
1081 intervals.append(current)
1090 for i
in range(gr.GetN()):
1091 if gr.GetY()[i] == 0.:
1092 fit_x = gr.GetX()[i]
1093 fit_y = gr.GetY()[i]
1097 for i
in range(gr.GetN()):
1098 if gr.GetY()[i] < min_y:
1099 min_y = gr.GetY()[i]
1100 min_x = gr.GetX()[i]
1102 print(
'[ReZeroTGraph] Fit minimum was (%f, %f)' % (fit_x, fit_y))
1103 print(
'[ReZeroTGraph] Better minimum was (%f, %f)' % (min_x, min_y))
1105 for i
in range(gr.GetN()):
1106 before = gr.GetY()[i]
1107 gr.GetY()[i] -= min_y
1108 after = gr.GetY()[i]
1115 for i
in range(gr.GetN()):
1116 if gr.GetY()[i] == 0.:
1118 if counter % n < (n - 1):
1119 remove_list.append(i)
1122 for i
in reversed(remove_list):
1128 for i
in range(gr.GetN()):
1129 if gr.GetY()[i] == 0.:
1131 if gr.GetX()[i] > xmin
and gr.GetX()[i] < xmax:
1132 remove_list.append(i)
1134 for i
in reversed(remove_list):
1144 spacing = (graph.GetX()[n - 1] - graph.GetX()[0]) / float(n - 2)
1147 for i
in range(graph.GetN()):
1148 if graph.GetY()[i] == 0.:
1149 bf = graph.GetX()[i]
1154 print(
'[RemoveNearMin] No minimum found!')
1156 for i
in range(graph.GetN()):
1159 if abs(graph.GetX()[i] - bf) < (val * spacing):
1160 print(
'[RemoveNearMin] Removing point (%f, %f) close to minimum at %f' % (graph.GetX()[i], graph.GetY()[i], bf))
1161 graph.RemovePoint(i)
1167 sortedGraph = R.TGraph()
1169 for i
in range(Graph.GetN()):
1170 graph_list.append((float(Graph.GetX()[i]), float(Graph.GetY()[i])))
1171 graph_list = sorted(set(graph_list))
1172 for i
in range(Graph.GetN()):
1173 sortedGraph.SetPoint(i, graph_list[i][0], graph_list[i][1])
1188 ymin = hobj.GetMinimum()
1189 hobj.SetMaximum((fix_y - fraction * ymin) / (1. - fraction))
1190 if R.gPad.GetLogy():
1192 print(
'Cannot adjust log-scale y-axis range if the minimum is zero!')
1194 maxval = (math.log10(fix_y) - fraction * math.log10(ymin)) / \
1196 maxval = math.pow(10, maxval)
1197 hobj.SetMaximum(maxval)
1200 def FixBothRanges(pad, fix_y_lo, frac_lo, fix_y_hi, frac_hi):
1201 """Adjusts y-axis range such that a lower and a higher value are located a
1202 fixed fraction of the frame height away from a new minimum and maximum
1205 This function is useful in conjunction with GetPadYMax which returns the
1206 maximum or minimum y value of all histograms and graphs drawn on the pad.
1208 In the example below, the minimum and maximum values found via this function
1209 are used as the `fix_y_lo` and `fix_y_hi` arguments, and the spacing fractions
1210 as 0.15 and 0.30 respectively.
1213 FixBothRanges(pad, GetPadYMin(pad), 0.15, GetPadYMax(pad), 0.30)
1216 ![](figures/FixBothRanges.png)
1219 pad (TPad): A TPad on which histograms and graphs have already been drawn
1220 fix_y_lo (float): The y value which will end up a fraction `frac_lo` above
1221 the new axis minimum.
1222 frac_lo (float): A fraction of the y-axis height
1223 fix_y_hi (float): The y value which will end up a fraction `frac_hi` below
1224 from the new axis maximum.
1225 frac_hi (float): A fraction of the y-axis height
1230 if R.gPad.GetLogy():
1232 print(
'Cannot adjust log-scale y-axis range if the minimum is zero!')
1234 ymin = math.log10(ymin)
1235 ymax = math.log10(ymax)
1240 (1. / (1. - (fh*fl/((1.-fl)*(1.-fh))))) *
1244 yminn = (ymin - fl*ymaxn) / (1. - fl)
1245 if R.gPad.GetLogy():
1246 yminn = math.pow(10, yminn)
1247 ymaxn = math.pow(10, ymaxn)
1248 hobj.SetMinimum(yminn)
1249 hobj.SetMaximum(ymaxn)
1253 pad_obs = pad.GetListOfPrimitives()
1259 if obj.InheritsFrom(R.TH1.Class()):
1261 for j
in range(1, hobj.GetNbinsX()+1):
1262 if (hobj.GetBinLowEdge(j) + hobj.GetBinWidth(j) < x_min
or
1263 hobj.GetBinLowEdge(j) > x_max):
1265 if (hobj.GetBinContent(j) + hobj.GetBinError(j) > h_max):
1266 h_max = hobj.GetBinContent(j) + hobj.GetBinError(j)
1267 if (hobj.GetBinContent(j) - hobj.GetBinError(j) < h_min)
and not do_min:
1270 h_min = hobj.GetBinContent(j) - hobj.GetBinError(j)
1271 elif obj.InheritsFrom(R.TGraphAsymmErrors.Class()):
1274 for k
in range(0, n):
1277 if x < x_min
or x > x_max:
1279 if (y + gobj.GetEYhigh()[k]) > h_max:
1280 h_max = y + gobj.GetEYhigh()[k]
1281 if (y - gobj.GetEYlow()[k]) < h_min:
1282 h_min = y - gobj.GetEYlow()[k]
1283 elif obj.InheritsFrom(R.TGraphErrors.Class()):
1286 for k
in range(0, n):
1289 if x < x_min
or x > x_max:
1291 if (y + gobj.GetEY()[k]) > h_max:
1292 h_max = y + gobj.GetEY()[k]
1293 if (y - gobj.GetEY()[k]) < h_min:
1294 h_min = y - gobj.GetEY()[k]
1295 elif obj.InheritsFrom(R.TGraph.Class()):
1298 for k
in range(0, n):
1301 if x < x_min
or x > x_max:
1307 return h_max
if do_min
is False else h_min
1311 pad_obs = pad.GetListOfPrimitives()
1324 R.gPad.GetFrame().Draw()
1336 f_x1 = (p_x1 - pad.GetLeftMargin()) / (1. - pad.GetLeftMargin() - pad.GetRightMargin())
1337 f_x2 = (p_x2 - pad.GetLeftMargin()) / (1. - pad.GetLeftMargin() - pad.GetRightMargin())
1338 f_y1 = (p_y1 - pad.GetBottomMargin()) / (1. - pad.GetTopMargin() - pad.GetBottomMargin())
1343 xmin = hobj.GetBinLowEdge(hobj.GetXaxis().GetFirst())
1344 xmax = hobj.GetBinLowEdge(hobj.GetXaxis().GetLast()+1)
1345 ymin = hobj.GetMinimum()
1346 ymax = hobj.GetMaximum()
1349 a_x1 = xmin + (xmax - xmin) * f_x1
1350 a_x2 = xmin + (xmax - xmin) * f_x2
1356 f_max_h = (a_max_h - ymin) / (ymax - ymin);
1357 if R.gPad.GetLogy()
and f_max_h > 0.:
1358 f_max_h = (math.log10(a_max_h) - math.log10(ymin)) / (math.log10(ymax) - math.log10(ymin))
1360 if f_y1 - f_max_h < frac:
1361 f_target = 1. - (f_y1 - frac)
1373 width = xaxis.GetBinWidth(1)
1374 w_label =
"%.1f" % width
1377 yaxis.SetTitle(
"Events / " + w_label)
1379 xaxis.SetTitle(var +
" (" + units +
")")
1380 yaxis.SetTitle(
"Events / " + w_label +
" " + units)
1383 def DrawCMSLogo(pad, cmsText, extraText, iPosX, relPosX, relPosY, relExtraDY, extraText2='', cmsTextSize=0.8):
1387 pad (TYPE): Description
1388 cmsText (TYPE): Description
1389 extraText (TYPE): Description
1390 iPosX (TYPE): Description
1391 relPosX (TYPE): Description
1392 relPosY (TYPE): Description
1393 relExtraDY (TYPE): Description
1394 extraText2 (str): Description
1395 cmsTextSize (float): Description
1403 writeExtraText = len(extraText) > 0
1404 writeExtraText2 = len(extraText2) > 0
1409 lumiTextOffset = 0.2
1414 extraOverCmsTextSize = 0.76
1422 if (iPosX / 10 == 0):
1428 if (iPosX / 10 == 1):
1430 if (iPosX / 10 == 2):
1432 if (iPosX / 10 == 3):
1435 align_ = 10 * alignX_ + alignY_
1437 l = pad.GetLeftMargin()
1438 t = pad.GetTopMargin()
1439 r = pad.GetRightMargin()
1440 b = pad.GetBottomMargin()
1444 latex.SetTextAngle(0)
1445 latex.SetTextColor(R.kBlack)
1447 extraTextSize = extraOverCmsTextSize * cmsTextSize
1448 pad_ratio = (float(pad.GetWh()) * pad.GetAbsHNDC()) / \
1449 (float(pad.GetWw()) * pad.GetAbsWNDC())
1450 if (pad_ratio < 1.):
1454 latex.SetTextFont(cmsTextFont)
1455 latex.SetTextAlign(11)
1456 latex.SetTextSize(cmsTextSize * t * (1./pad_ratio))
1457 latex.DrawLatex(l, 1 - t + lumiTextOffset * t, cmsText)
1461 posX_ = l + relPosX * (1 - l - r)
1462 elif (iPosX % 10 == 2):
1463 posX_ = l + 0.5 * (1 - l - r)
1464 elif (iPosX % 10 == 3):
1465 posX_ = 1 - r - relPosX * (1 - l - r)
1467 posY_ = 1 - t - relPosY * (1 - t - b)
1469 latex.SetTextFont(cmsTextFont)
1470 latex.SetTextSize(cmsTextSize * t * pad_ratio)
1471 latex.SetTextAlign(align_)
1472 latex.DrawLatex(posX_, posY_, cmsText)
1474 latex.SetTextFont(extraTextFont)
1475 latex.SetTextAlign(align_)
1476 latex.SetTextSize(extraTextSize * t * pad_ratio)
1478 posX_, posY_ - relExtraDY * cmsTextSize * t, extraText)
1481 posX_, posY_ - 1.8 * relExtraDY * cmsTextSize * t, extraText2)
1482 elif writeExtraText:
1484 posX_ = l + relPosX * (1 - l - r)
1485 posY_ = 1 - t + lumiTextOffset * t
1486 latex.SetTextFont(extraTextFont)
1487 latex.SetTextSize(extraTextSize * t * (1./pad_ratio))
1488 latex.SetTextAlign(align_)
1489 latex.DrawLatex(posX_, posY_, extraText)
1494 ho = horizontaloffset
1499 l = R.gPad.GetLeftMargin()
1500 t = R.gPad.GetTopMargin()
1501 b = R.gPad.GetBottomMargin()
1502 r = R.gPad.GetRightMargin()
1504 return R.TLegend(l + ho, 1 - t - o - h, l + ho + w, 1 - t - o,
'',
'NBNDC')
1506 c = l + 0.5 * (1 - l - r)
1507 return R.TLegend(c - 0.5 * w, 1 - t - o - h, c + 0.5 * w, 1 - t - o,
'',
'NBNDC')
1509 return R.TLegend(1 - r - ho - w, 1 - t - o - h, 1 - r - ho, 1 - t - o,
'',
'NBNDC')
1511 return R.TLegend(l + ho, b + o, l + ho + w, b + o + h,
'',
'NBNDC')
1513 c = l + 0.5 * (1 - l - r)
1514 return R.TLegend(c - 0.5 * w, b + o, c + 0.5 * w, b + o + h,
'',
'NBNDC')
1516 return R.TLegend(1 - r - ho - w, b + o, 1 - r - ho, b + o + h,
'',
'NBNDC')
1521 xmin = axis.GetXaxis().GetXmin()
1522 xmax = axis.GetXaxis().GetXmax()
1523 line.DrawLine(xmin, yval, xmax, yval)
1528 ymin = axis.GetYaxis().GetXmin()
1529 ymax = axis.GetYaxis().GetXmax()
1530 line.DrawLine(xval, ymin, xval, ymax)
1534 ymin = axis.GetYaxis().GetXmin()
1535 ymax = axis.GetYaxis().GetXmax()
1536 box.DrawBox(x1, ymin, x2, ymax)
1539 def DrawTitle(pad, text, align, textOffset=0.2,textSize=0.6):
1542 t = pad.GetTopMargin()
1543 l = pad.GetLeftMargin()
1544 r = pad.GetRightMargin()
1546 pad_ratio = (float(pad.GetWh()) * pad.GetAbsHNDC()) / \
1547 (float(pad.GetWw()) * pad.GetAbsWNDC())
1553 latex.SetTextAngle(0)
1554 latex.SetTextColor(R.kBlack)
1555 latex.SetTextFont(42)
1556 latex.SetTextSize(textSize * t * pad_ratio)
1558 y_off = 1 - t + textOffset * t
1560 latex.SetTextAlign(11)
1562 latex.DrawLatex(l, y_off, text)
1564 latex.SetTextAlign(21)
1566 latex.DrawLatex(l + (1 - l - r) * 0.5, y_off, text)
1568 latex.SetTextAlign(31)
1570 latex.DrawLatex(1 - r, y_off, text)
1584 return abs(a-b) <= max(abs_tol, rel_tol * max(abs(a),abs(b)))
1588 'obs' : {
'LineWidth' : 2},
1589 'exp0' : {
'LineWidth' : 2,
'LineColor' : R.kRed},
1590 'exp1' : {
'FillColor' : R.kGreen},
1591 'exp2' : {
'FillColor' : R.kYellow}
1593 if overwrite_style_dict
is not None:
1594 for key
in overwrite_style_dict:
1595 if key
in style_dict:
1596 style_dict[key].update(overwrite_style_dict[key])
1598 style_dict[key] = overwrite_style_dict[key]
1599 for key
in graph_dict:
1600 Set(graph_dict[key],**style_dict[key])
1602 def DrawLimitBand(pad, graph_dict, draw=['exp2', 'exp1', 'exp0', 'obs'], draw_legend=None,
1603 legend=None, legend_overwrite=None):
1605 'obs' : {
'Label' :
'Observed',
'LegendStyle' :
'LP',
'DrawStyle' :
'PLSAME'},
1606 'exp0' : {
'Label' :
'Expected',
'LegendStyle' :
'L',
'DrawStyle' :
'LSAME'},
1607 'exp1' : {
'Label' :
'#pm1#sigma Expected',
'LegendStyle' :
'F',
'DrawStyle' :
'3SAME'},
1608 'exp2' : {
'Label' :
'#pm2#sigma Expected',
'LegendStyle' :
'F',
'DrawStyle' :
'3SAME'}
1610 if legend_overwrite
is not None:
1611 for key
in legend_overwrite:
1612 if key
in legend_dict:
1613 legend_dict[key].update(legend_overwrite[key])
1615 legend_dict[key] = legend_overwrite[key]
1618 if key
in graph_dict:
1619 graph_dict[key].Draw(legend_dict[key][
'DrawStyle'])
1620 if legend
is not None:
1621 if draw_legend
is None:
1622 draw_legend = reversed(draw)
1623 for key
in draw_legend:
1624 if key
in graph_dict:
1625 legend.AddEntry(graph_dict[key],legend_dict[key][
'Label'],legend_dict[key][
'LegendStyle'])
1638 contoursList = [threshold]
1639 contours = array(
'd', contoursList)
1643 h2 =
frameTH2D(h2in, threshold, frameValue)
1645 h2.SetContour(1, contours)
1649 canv = R.TCanvas(
'tmp',
'tmp')
1651 h2.Draw(
'CONT Z LIST')
1654 conts = R.gROOT.GetListOfSpecials().FindObject(
'contours')
1657 if conts
is None or conts.GetSize() == 0:
1658 print(
'*** No Contours Were Extracted!')
1661 for i
in range(conts.GetSize()):
1662 contLevel = conts.At(i)
1663 print(
'>> Contour %d has %d Graphs' % (i, contLevel.GetSize()))
1664 for j
in range(contLevel.GetSize()):
1665 gr1 = contLevel.At(j)
1666 print(
'\t Graph %d has %d points' % (j, gr1.GetN()))
1667 if gr1.GetN() > minPoints:
1668 ret.Add(gr1.Clone())
1682 x_bins = [hist.GetXaxis().GetBinLowEdge(x)
1683 for x
in range(1, hist.GetNbinsX() + 2)]
1684 y_bins = [hist.GetYaxis().GetBinLowEdge(y)
1685 for y
in range(1, hist.GetNbinsY() + 2)]
1688 x_new = [0.] * (len(x_bins) + 4)
1689 y_new = [0.] * (len(y_bins) + 4)
1692 xw1 = x_bins[1] - x_bins[0]
1693 xw2 = x_bins[-1] - x_bins[-2]
1694 yw1 = y_bins[1] - y_bins[0]
1695 yw2 = y_bins[-1] - y_bins[-2]
1699 x_new[0] = x_bins[0] - 2 * xw1 * 0.02
1700 x_new[1] = x_bins[0] - 1 * xw1 * 0.02
1701 x_new[-1] = x_bins[-1] + 2 * xw2 * 0.02
1702 x_new[-2] = x_bins[-1] + 1 * xw2 * 0.02
1703 y_new[0] = y_bins[0] - 2 * yw1 * 0.02
1704 y_new[1] = y_bins[0] - 1 * yw1 * 0.02
1705 y_new[-1] = y_bins[-1] + 2 * yw2 * 0.02
1706 y_new[-2] = y_bins[-1] + 1 * yw2 * 0.02
1709 for i
in range(0, len(x_bins)):
1710 x_new[i + 2] = x_bins[i]
1711 for i
in range(0, len(y_bins)):
1712 y_new[i + 2] = y_bins[i]
1717 framed = R.TH2D(
'%s framed' % hist.GetName(),
'%s framed' % hist.GetTitle(), len(
1718 x_new) - 1, array(
'd', x_new), len(y_new) - 1, array(
'd', y_new))
1719 framed.SetDirectory(0)
1721 for x
in range(1, framed.GetNbinsX() + 1):
1722 for y
in range(1, framed.GetNbinsY() + 1):
1723 if x == 1
or x == framed.GetNbinsX()
or y == 1
or y == framed.GetNbinsY():
1725 framed.SetBinContent(x, y, frameValue)
1733 elif x == (len(x_new) - 2):
1737 elif y == (len(y_new) - 2):
1739 framed.SetBinContent(x, y, hist.GetBinContent(ux - 2, uy - 2))
1742 def fastFillTH2(hist2d, graph, initalValue=99999, interpolateMissing=False):
1743 for x
in range(1,hist2d.GetNbinsX()+1):
1744 for y
in range(1,hist2d.GetNbinsY()+1):
1745 hist2d.SetBinContent(x,y,initalValue)
1748 for i
in range(graph.GetN()):
1749 xbin = hist2d.GetXaxis().FindBin(graph.GetX()[i])
1750 ybin = hist2d.GetYaxis().FindBin(graph.GetY()[i])
1751 if isclose(hist2d.GetXaxis().GetBinCenter(xbin), graph.GetX()[i], rel_tol=1e-2)
and isclose(hist2d.GetYaxis().GetBinCenter(ybin), graph.GetY()[i], rel_tol=1e-2):
1752 hist2d.SetBinContent(xbin, ybin, graph.GetZ()[i])
1754 if interpolateMissing:
1755 for x
in range(1,hist2d.GetNbinsX()+1):
1756 for y
in range(1,hist2d.GetNbinsY()+1):
1757 if hist2d.GetBinContent(x,y) == initalValue:
1759 hist2d.SetBinContent(x, y, graph.Interpolate(hist2d.GetXaxis().GetBinCenter(x),hist2d.GetYaxis().GetBinCenter(y)))
1762 for x
in range(1, hist2d.GetNbinsX() + 1):
1763 for y
in range(1, hist2d.GetNbinsY() + 1):
1764 xc = hist2d.GetXaxis().GetBinCenter(x)
1765 yc = hist2d.GetYaxis().GetBinCenter(y)
1766 val = graph.Interpolate(xc, yc)
1767 hist2d.SetBinContent(x, y, val)
1770 for x
in range(1, hist2d.GetNbinsX() + 1):
1771 for y
in range(1, hist2d.GetNbinsY() + 1):
1772 xc = hist2d.GetXaxis().GetBinCenter(x)
1773 yc = hist2d.GetYaxis().GetBinCenter(y)
1774 val = graph.Interpolate(xc, yc)
1775 hist2d.SetBinContent(x, y, 1-val)
1783 histCopy = hist.Clone()
1786 hist_step1 = histCopy.Clone()
1788 hist_step2 = histCopy.Clone()
1791 nBinsX = histCopy.GetNbinsX()
1792 nBinsY = histCopy.GetNbinsY()
1796 xMax = histCopy.GetNbinsX() + 1
1797 yMax = histCopy.GetNbinsY() + 1
1799 for i
in range(1, nBinsX + 1):
1800 for j
in range(1, nBinsY + 1):
1802 if (i < xMin)
or (i > xMax)
or (j < yMin)
or (j > yMax):
1804 binContent = histCopy.GetBinContent(i, j)
1805 binContentNW = histCopy.GetBinContent(i + 1, j + 1)
1806 binContentSE = histCopy.GetBinContent(i - 1, j - 1)
1807 binContentNE = histCopy.GetBinContent(i + 1, j - 1)
1808 binContentSW = histCopy.GetBinContent(i - 1, j + 1)
1809 binContentUp = histCopy.GetBinContent(i, j + 1)
1810 binContentDown = histCopy.GetBinContent(i, j - 1)
1811 binContentLeft = histCopy.GetBinContent(i - 1, j)
1812 binContentRight = histCopy.GetBinContent(i + 1, j)
1814 if(binContentNW > 0):
1816 if(binContentSE > 0):
1818 if(binContentNE > 0):
1820 if(binContentSW > 0):
1822 if(binContentUp > 0):
1824 if(binContentDown > 0):
1826 if(binContentRight > 0):
1828 if(binContentLeft > 0):
1832 if(binContent == 0)
and (nFilled > 1):
1834 binContent = (binContentNW + binContentSE + binContentNE + binContentSW +
1835 binContentUp + binContentDown + binContentRight + binContentLeft) / nFilled
1836 hist_step1.SetBinContent(i, j, binContent)
1839 histCopy.Add(hist_step1)
1841 for i
in range(1, nBinsX):
1842 for j
in range(1, nBinsY):
1843 if(i < xMin)
or (i > xMax)
or (j < yMin)
or (j > yMax):
1845 binContent = histCopy.GetBinContent(i, j)
1847 binContentUp = histCopy.GetBinContent(i, j + 1)
1848 binContentDown = histCopy.GetBinContent(i, j - 1)
1849 binContentLeft = histCopy.GetBinContent(i - 1, j)
1850 binContentRight = histCopy.GetBinContent(i + 1, j)
1852 if(binContentUp > 0):
1854 if(binContentDown > 0):
1856 if(binContentRight > 0):
1858 if(binContentLeft > 0):
1860 if(binContent == 0)
and (nFilled > 0):
1863 binContentUp + binContentDown + binContentRight + binContentLeft) / nFilled
1864 hist_step2.SetBinContent(i, j, binContent)
1866 histCopy.Add(hist_step2)
1872 histName = hist.GetName()
1873 histName +=
"_rebin"
1881 histRebinned = R.TH2F(histName, histName, 2 * hist.GetNbinsX() - 1, hist.GetXaxis().GetXmin(),
1882 hist.GetXaxis().GetXmax(), 2 * hist.GetNbinsY() - 1, hist.GetYaxis().GetXmin(), hist.GetYaxis().GetXmax())
1885 for iX
in range(1, hist.GetNbinsX() + 1):
1886 for iY
in range(1, hist.GetNbinsY() + 1):
1887 binContent = hist.GetBinContent(iX, iY)
1888 histRebinned.SetBinContent(2 * iX - 1, 2 * iY - 1, binContent)
1889 histRebinned.SetMaximum(hist.GetMaximum())
1890 histRebinned.SetMinimum(hist.GetMinimum())
1895 return histRebinnedInterpolated
1899 higgsBand = R.TGraph2D()
1904 for mass
in range(masslow, masshigh, massstep):
1905 myfile = open(
"../../HiggsAnalysis/HiggsToTauTau/data/Higgs125/" +
1906 model +
"/higgs_" + str(mass) +
".dat",
'r')
1908 tanb = (line.split())[0]
1909 mh = float((line.split())[1])
1910 mH = float((line.split())[3])
1911 if higgstype ==
"h":
1912 higgsBand.SetPoint(n, mass, float(tanb), mh)
1913 elif higgstype ==
"H":
1914 higgsBand.SetPoint(n, mass, float(tanb), mH)
1921 borderLegend = legend.Clone()
1922 borderLegend.Clear()
1924 for i
in range(legend.GetNRows()):
1926 graph = entries[i].Clone()
1927 options[i][
"MarkerSize"] = graph.GetMarkerSize()*borderSize
1928 Set(graph,**options[i])
1929 borderLegend.AddEntry(graph,
" ", markerStyle)
1930 graphs.append(graph)
1932 borderLegend.AddEntry(
"",
" ",
"")
1933 borderLegend.SetFillStyle(0)
1934 borderLegend.SetFillColor(0)
1935 return (borderLegend,graphs)