def _plot(self): # container where all canvas related objects will be saved class Canvas: pass canvases = [] ''' loop over plots and draw them: 1. all background channels stacked (MC channels + QCD) 2. background error band (MC + QCD) 3. data with errors 4. ratio of data over background ''' # loop over plots for plot_name, channels in self.loader.plots.items(): # create container for current objects obj = Canvas() # extact MC combined mc_combo = channels.get("mc") # extract Data data = channels.get("data") if "none" == self._ratio: obj.legend = ROOT.TLegend(.65, .50, .88, .88) else: obj.legend = ROOT.TLegend(.7, .50, .88, .88) obj.legend.SetMargin(0.12); obj.legend.SetTextSize(0.03); obj.legend.SetFillColor(10); obj.legend.SetBorderSize(0); # background combined obj.bg_combo = None for channel_type in ["mc", "qcd"]: if channel_type in channels: hist = channels[channel_type].hist if obj.bg_combo: obj.bg_combo.Add(hist) else: obj.bg_combo = hist.Clone() obj.bg_combo.SetDirectory(0) if obj.bg_combo: # apply uncertainty style MCChannelTemplate.channel_styles["mc"].apply(obj.bg_combo) # stack all backgrounds obj.bg_stack = None bg_order = ["qcd"] + (mc_combo.allowed_inputs if mc_combo else []) bg_channels = set(channels.keys()) & set(bg_order) # Add channels in order: QCD + channel_type["mc"] for channel_type in bg_order: if channel_type in bg_channels: hist = channels[channel_type].hist if not obj.bg_stack: obj.bg_stack = ROOT.THStack() clone = hist.Clone() clone.SetLineWidth(0) obj.bg_stack.Add(hist) # Add channels in order: QCD + channel_type["mc"] for channel_type in reversed(bg_order): if channel_type in bg_channels: hist = channels[channel_type].hist obj.legend.AddEntry(hist, self.channel_names.get(channel_type, "unknown"), "fe") # Adjust y-Maximum to be drawn max_y = 1.3 * max( (h.GetBinContent(h.GetMaximumBin()) + h.GetBinError(h.GetMaximumBin())) if h else 0 for h in [obj.bg_combo if obj.bg_combo else None, data.hist if data else None] + [ch.hist for name, ch in channels.items() if (name.startswith("zprime") or name.startswith("rsgluon"))] if h) # take care of ratio if self._ratio and self._ratio != "none": try: # make sure specified channels are available ratio = [] for term in self._ratio: if "bg" == term: if not obj.bg_combo: raise KeyError("background is not loaded") ratio.append({ "title": "BKGD", "hist": obj.bg_combo }) elif term in channels: ratio.append({ "title": self.channel_names.get(term, "unknown"), "hist": channels[term].hist }) else: raise KeyError("unsupported channel {0!r}".format( term)) obj.canvas_obj = ComparisonCanvas() obj.canvas = obj.canvas_obj.canvas obj.canvas.cd(2) obj.ratio = compare.ratio(ratio[0]["hist"], ratio[1]["hist"], title = "#frac{" + ratio[0]["title"] + "}{" + ratio[1]["title"] + "}") obj.ratio.GetXaxis().SetTitle("") obj.ratio.Draw("9 e") except KeyError as error: print("ratio error: {0}".format(error)) obj.canvas = ROOT.TCanvas() obj.canvas.SetWindowSize(640, 560) pad = obj.canvas.cd(1) pad.SetRightMargin(5) pad.SetBottomMargin(0.15) elif data and obj.bg_combo and self._ratio != "none": # Prepare comparison canvas: top pad plots, bottom - ratio obj.canvas_obj = ComparisonCanvas() obj.canvas = obj.canvas_obj.canvas obj.canvas.cd(2) obj.ratio = compare.data_mins_bg_over_bg(data.hist, obj.bg_combo) ''' obj.ratio = compare.ratio(channels["zprime_m1000_w10"].hist, obj.bg_combo, title = "#frac{Z' 1 TeV}{BKGD}") ''' obj.ratio.GetXaxis().SetTitle("") obj.ratio.Draw("9 e") else: obj.canvas = ROOT.TCanvas() obj.canvas.SetWindowSize(640, 560) pad = obj.canvas.cd(1) pad.SetRightMargin(5) pad.SetBottomMargin(0.15) obj.canvas.SetName("canvas_" + plot_name.replace("/", "_")) pad = obj.canvas.cd(1) if self._logy: pad.SetLogy() # use data or background to draw axes obj.axis_hist = None if data: obj.axis_hist = data.hist.Clone() elif obj.bg_combo: obj.axis_hist = obj.bg_combo.Clone() else: for name, channel in channels.items(): if name.startswith("zprime") or name.startswith("rsgluon"): obj.axis_hist = channel.hist.Clone() break obj.axis_hist.Reset() obj.axis_hist.SetLineWidth(1) for axis in ROOT.TH1.GetXaxis, ROOT.TH1.GetYaxis: axis(obj.axis_hist).SetLabelSize(0.04) axis(obj.axis_hist).SetAxisColor(ROOT.kBlack) if "chi2" in plot_name and False: obj.axis_hist.GetXaxis().SetRangeUser(0, 21) obj.axis_hist.Draw("9") # Draw plots if obj.bg_stack: obj.bg_stack.Draw("9 hist same") if obj.bg_combo: obj.legend.AddEntry(obj.bg_combo, "Uncertainty", "fe") obj.bg_combo.Draw("9 e2 same") if data: obj.legend.AddEntry(data.hist, "CMS Data 2011", "lpe") data.hist.Draw("9 same") obj.axis_hist.SetMaximum(max_y) obj.axis_hist.Draw("9 same") if "none" == self._ratio: obj.labels = [ root.label.CMSLabel(text_size=0.033), root.label.LuminosityLabel(InputTemplate.luminosity(), text_size=0.033) if data else root.label.CMSSimulationLabel(text_size=0.033)] else: obj.labels = [ root.label.CMSLabel(), root.label.LuminosityLabel(InputTemplate.luminosity()) if data else root.label.CMSSimulationLabel()] if self._label: obj.labels.append(root.label.ChannelLabel(self._label)) # draw signals for channel_type, channel in channels.items(): if (channel_type.startswith("zprime") or channel_type.startswith("rsgluon")): obj.legend.AddEntry(channel.hist, self.channel_names.get(channel_type, "unknown signal"), "l") channel.hist.Draw("9 hist same") print("{0} S: {s:.1f} B: {b:.1f} S/B: {r:.3f}".format( channel_type, s=channel.hist.Integral(), b=obj.bg_combo.Integral(), r=channel.hist.Integral() / obj.bg_combo.Integral())) # Draw Labels and Legend for label in obj.labels: label.draw() obj.legend.Draw("9") obj.canvas.Update() canvases.append(obj) return canvases
def tsdb_ratio(h, url, url_ago, name, key, method, symbol, threshold, floatingthreshold, floatingvalue, counter, attempt, groups, alert): filename = "/tmp/%s_ratio_url_%s.%s" % (h.uuid, name, key) filename_ago = "/tmp/%s_ratio_url_ago_%s.%s" % (h.uuid, name, key) data = pycurl_data(filename, quote(url, ':/=&()?,>.')) data_ago = pycurl_data(filename_ago, quote(url_ago, ':/=&()?,>.')) metric = "%s.%s" % (name, key) if data == 0 and data_ago == 0: dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S") symbols = { '>': 'gt', '>=': 'ge', '<': 'lt', '<=': 'le', '=': 'eq', '!=': 'ne', '<>': 'ne' } comparator = operator.__dict__[symbols[symbol]] with open(filename, "rb") as f: bfb_val = tsdb_ratio_data(f.readline(), method) with open(filename_ago, "rb") as f: bfb_val_ago = tsdb_ratio_data(f.readline(), method) rv = 0 if bfb_val and bfb_val_ago: val = ratio(bfb_val_ago, bfb_val) if comparator(abs(val), threshold): rv = 1 if rv == 0: floatingvalue = 0 alert = 0 if counter > 0 and alert == 0: counter = 0 content = "状态:ok %s 主机:%s metric:%s 阀值百分比:%s 方法:环比 %s 结果百分比:|%s|" % ( dt, h.ip, metric, bytes2human(threshold), symbol, bytes2human(val)) print content alarm(content, groups) item_update(h, name, key, val, counter, floatingvalue, alert) else: item_update(h, name, key, val, counter, floatingvalue, alert) elif rv == 1: if floatingthreshold == 0: floatingvalue = 0 content = "状态:critical %s 主机:%s metric:%s 结果百分比:|%s| 方法:环比 %s 阀值百分比:%s" % ( dt, h.ip, metric, bytes2human(val), symbol, bytes2human(threshold)) counter += 1 if attempt == 0 and alert == 0: alarm(content, groups) elif counter < attempt and alert == 0: alarm(content, groups) item_update(h, name, key, val, counter, floatingvalue, alert) else: if counter == 0: floatingvalue = val + floatingthreshold content = "状态:critical %s 主机:%s metric:%s 结果百分比:|%s| 方法:环比 %s 阀值百分比:%s" % ( dt, h.ip, metric, bytes2human(val), symbol, bytes2human(threshold)) print content counter += 1 if attempt == 0 and alert == 0: alarm(content, groups) elif counter < attempt and alert == 0: alarm(content, groups) item_update(h, name, key, val, counter, floatingvalue, alert) elif counter > 0: fv = 0 if comparator(val, floatingvalue): fv = 1 if fv == 0: while not comparator(val, floatingvalue): floatingvalue -= floatingthreshold print "floatingvalue:%s" % (floatingvalue) floatingvalue += floatingthreshold item_update(h, name, key, val, counter, floatingvalue, alert) elif fv == 1: content = "状态:critical %s 主机:%s metric:%s 结果百分比:|%s| 方法:环比 %s 浮动值百分比:%s" % ( dt, h.ip, metric, bytes2human(val), symbol, bytes2human(floatingvalue)) print content if attempt == 0 and alert == 0: alarm(content, groups) elif counter < attempt and alert == 0: alarm(content, groups) floatingvalue = val + floatingthreshold counter += 1 item_update(h, name, key, val, counter, floatingvalue, alert)
def tsdb_ratio(h, url, url_ago, name, key, method, symbol, threshold, floatingthreshold, floatingvalue, counter, attempt, groups, alert): filename = "/tmp/%s_ratio_url_%s.%s" % (h.uuid, name, key) filename_ago = "/tmp/%s_ratio_url_ago_%s.%s" % (h.uuid, name, key) data = pycurl_data(filename, quote(url, ':/=&()?,>.')) data_ago = pycurl_data(filename_ago, quote(url_ago, ':/=&()?,>.')) metric = "%s.%s" % (name, key) if data == 0 and data_ago == 0: dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S") symbols = { '>' : 'gt', '>=' : 'ge', '<' : 'lt', '<=' : 'le', '=' : 'eq', '!=' : 'ne', '<>' : 'ne' } comparator = operator.__dict__[symbols[symbol]] with open(filename, "rb") as f: bfb_val = tsdb_ratio_data(f.readline(), method) with open(filename_ago, "rb") as f: bfb_val_ago = tsdb_ratio_data(f.readline(), method) rv = 0 if bfb_val and bfb_val_ago: val = ratio(bfb_val_ago, bfb_val) if comparator(abs(val), threshold): rv = 1 if rv == 0: floatingvalue = 0 alert = 0 if counter > 0 and alert == 0: counter = 0 content = "状态:ok %s 主机:%s metric:%s 阀值百分比:%s 方法:环比 %s 结果百分比:|%s|" % (dt, h.ip, metric, bytes2human(threshold), symbol, bytes2human(val)) print content alarm(content, groups) item_update(h, name, key, val, counter, floatingvalue, alert) else: item_update(h, name, key, val, counter, floatingvalue, alert) elif rv == 1: if floatingthreshold == 0: floatingvalue = 0 content = "状态:critical %s 主机:%s metric:%s 结果百分比:|%s| 方法:环比 %s 阀值百分比:%s" % (dt, h.ip, metric, bytes2human(val), symbol, bytes2human(threshold)) counter += 1 if attempt == 0 and alert== 0: alarm(content, groups) elif counter < attempt and alert == 0: alarm(content, groups) item_update(h, name, key, val, counter, floatingvalue, alert) else: if counter == 0: floatingvalue = val + floatingthreshold content = "状态:critical %s 主机:%s metric:%s 结果百分比:|%s| 方法:环比 %s 阀值百分比:%s" % (dt, h.ip, metric, bytes2human(val), symbol, bytes2human(threshold)) print content counter += 1 if attempt == 0 and alert == 0: alarm(content, groups) elif counter < attempt and alert == 0: alarm(content, groups) item_update(h, name, key, val, counter, floatingvalue, alert) elif counter > 0: fv = 0 if comparator(val, floatingvalue): fv = 1 if fv == 0: while not comparator(val, floatingvalue): floatingvalue -= floatingthreshold print "floatingvalue:%s" % (floatingvalue) floatingvalue += floatingthreshold item_update(h, name, key, val, counter, floatingvalue, alert) elif fv == 1: content = "状态:critical %s 主机:%s metric:%s 结果百分比:|%s| 方法:环比 %s 浮动值百分比:%s" % (dt, h.ip, metric, bytes2human(val), symbol, bytes2human(floatingvalue)) print content if attempt == 0 and alert == 0: alarm(content, groups) elif counter < attempt and alert == 0: alarm(content, groups) floatingvalue = val + floatingthreshold counter += 1 item_update(h, name, key, val, counter, floatingvalue, alert)