def histVariables2D(self, vX, vY, plot_name, sample, cat): # get number of bins and binrange from config file binsX = binning.getNbins(vX) binsY = binning.getNbins(vY) rangeX = binning.getBinrange(vX) rangeY = binning.getBinrange(vY) # check if bin_range was found if not rangeX: maxValue = max(self.samples[sample].cut_data[cat][vX].values) minValue = min(self.samples[sample].cut_data[cat][vX].values) config_string = "variables[\""+vX+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue) with open("new_variable_configs.txt", "a") as f: f.write(config_string) rangeX = [minValue, maxValue] if not rangeY: maxValue = max(self.samples[sample].cut_data[cat][vY].values) minValue = min(self.samples[sample].cut_data[cat][vY].values) config_string = "variables[\""+vY+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue) with open("new_variable_configs.txt", "a") as f: f.write(config_string) rangeY = [minValue, maxValue] # fill hist weights = self.samples[sample].cut_data[cat]["weight"].values valuesX = self.samples[sample].cut_data[cat][vX].values valuesY = self.samples[sample].cut_data[cat][vY].values hist = setup.setupHistogram2D( valuesX = valuesX, valuesY = valuesY, weights = weights, binsX = binsX, binsY = binsY, rangeX = rangeX, rangeY = rangeY, titleX = vX, titleY = vY) canvas = setup.drawHistOnCanvas2D( hist = hist, canvasName = vX+"_vs_"+vY, catLabel = JTcut.getJTlabel(cat), sampleName = sample) # add lumi and category to plot setup.printLumi(canvas, lumi = self.options["lumiScale"], twoDim = True) # save canvas setup.saveCanvas(canvas, plot_name)
def histVariable(self, variable, plot_name, cat): # get number of bins and binrange from config filea bins = binning.getNbins(variable) bin_range = binning.getBinrange(variable) # check if bin_range was found if not bin_range: maxValue = -999 minValue = 999 for key in self.samples: maxValue = max( maxValue, max(self.samples[key].cut_data[cat][variable].values)) minValue = min( minValue, min(self.samples[key].cut_data[cat][variable].values)) config_string = "variables[\"" + variable + "\"]\t\t\t= Variable(bin_range = [{},{}])\n".format( minValue, maxValue) with open("new_variable_configs.txt", "a") as f: f.write(config_string) bin_range = [minValue, maxValue] bkgHists = [] bkgLabels = [] weightIntegral = 0 # loop over bachgrounds and fill hists for sampleName in self.ordered_stack: sample = self.samples[sampleName] # get weights weights = sample.cut_data[cat]["weight"].values weightIntegral += sum(weights) # setup histogram hist = setup.setupHistogram( values=sample.cut_data[cat][variable].values, weights=weights, nbins=bins, bin_range=bin_range, color=sample.plotColor, xtitle=cat + "_" + sample.sampleName + "_" + variable, ytitle=setup.GetyTitle(), filled=True) bkgHists.append(hist) bkgLabels.append(sample.sampleName) sigHists = [] sigLabels = [] sigScales = [] # if not background was added, the weight integral is equal to 0 if weightIntegral == 0: self.options["scaleSignal"] = 1 # loop over signals and fill hists for key in self.samples: sample = self.samples[key] if not sample.isSignal: continue # get weights weights = sample.cut_data[cat]["weight"].values # determine scale factor if self.options["scaleSignal"] == -1: scaleFactor = weightIntegral / (sum(weights) + 1e-9) else: scaleFactor = float(self.options["scaleSignal"]) # setup histogram hist = setup.setupHistogram( values=sample.cut_data[cat][variable].values, weights=weights, nbins=bins, bin_range=bin_range, color=sample.plotColor, xtitle=cat + "_" + sample.sampleName + "_" + variable, ytitle=setup.GetyTitle(), filled=False) hist.Scale(scaleFactor) sigHists.append(hist) sigLabels.append(sample.sampleName) sigScales.append(scaleFactor) # init canvas canvas = setup.drawHistsOnCanvas(sigHists, bkgHists, self.options, canvasName=cat + "_" + variable) # setup legend legend = setup.getLegend() # add signal entries for iSig in range(len(sigHists)): legend.AddEntry( sigHists[iSig], sigLabels[iSig] + " x {:4.0f}".format(sigScales[iSig]), "L") # add background entries for iBkg in range(len(bkgHists)): legend.AddEntry(bkgHists[iBkg], bkgLabels[iBkg], "F") # draw loegend legend.Draw("same") # add lumi and category to plot setup.printLumi(canvas, lumi=self.options["lumiScale"], ratio=self.options["ratio"]) setup.printCategoryLabel(canvas, JTcut.getJTlabel(cat), ratio=self.options["ratio"]) # save canvas setup.saveCanvas(canvas, plot_name)
def histVariable1D(self, sample, name, vX, vY, plot_name, cat): # get number of bins and binrange from config file binsX = binning.getNbins(vX) binsY = binning.getNbins(vY) rangeX = binning.getBinrange(vX) rangeY = binning.getBinrange(vY) # check if bin_range was found if not rangeX: maxValue = max(sample.cut_data[cat][vX].values) minValue = min(sample.cut_data[cat][vX].values) config_string = "variables[\""+vX+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue) with open("new_variable_configs.txt", "a") as f: f.write(config_string) rangeX = [minValue, maxValue] if not rangeY: maxValue = max(sample.cut_data[cat][vY].values) minValue = min(sample.cut_data[cat][vY].values) config_string = "variables[\""+vY+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue) with open("new_variable_configs.txt", "a") as f: f.write(config_string) rangeY = [minValue, maxValue] # get averages bins = int((binsX + binsY)/2.) bin_range = [min(rangeX[0],rangeY[0]), max(rangeX[1],rangeY[1])] weights = sample.cut_data[cat]["weight"].values valuesX = sample.cut_data[cat][vX].values valuesY = sample.cut_data[cat][vY].values hX = setup.setupHistogram( values = valuesX, weights = weights, nbins = bins, bin_range = bin_range, color = ROOT.kBlack, xtitle = cat+"_"+sample.sampleName+"_"+vX, ytitle = setup.GetyTitle(self.options["lumiScale"]), filled = False) hY = setup.setupHistogram( values = valuesY, weights = weights, nbins = bins, bin_range = bin_range, color = ROOT.kRed, xtitle = cat+"_"+sample.sampleName+"_"+vY, ytitle = setup.GetyTitle(self.options["lumiScale"]), filled = False) # init canvas canvas = setup.drawHistsOnCanvas( hX, hY, self.options, canvasName = "[{}] {}".format(sample.sampleName, name)) # setup legend legend = setup.getLegend() legend.AddEntry( hX, self.options["xName"], "L") labelY = self.options["yName"] # add KS score to label if activated if self.options["KSscore"]: KSscore = setup.calculateKSscore(hX, hY) labelY="#splitline{"+labelY+"}{KSscore = %.3f}"%(KSscore) legend.AddEntry( hY, labelY, "L") # draw loegend legend.Draw("same") # add lumi and category to plot setup.printLumi(canvas, lumi = self.options["lumiScale"], ratio = self.options["ratio"]) setup.printCategoryLabel(canvas, JTcut.getJTlabel(cat), ratio = self.options["ratio"]) if self.options["privateWork"]: setup.printPrivateWork(canvas, ratio = self.options["ratio"]) # save canvas setup.saveCanvas(canvas, plot_name)
def histVariable2D(self, name, vX, vY, plot_name, cat): # get number of bins and binrange from config file binsX = binning.getNbins(vX) binsY = binning.getNbins(vY) rangeX = binning.getBinrange(vX) rangeY = binning.getBinrange(vY) # check if bin_range was found if not rangeX: maxValue = max([max(self.samples[sample].cut_data[cat][vX].values) for sample in self.samples]) minValue = min([min(self.samples[sample].cut_data[cat][vX].values) for sample in self.samples]) config_string = "variables[\""+vX+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue) with open("new_variable_configs.txt", "a") as f: f.write(config_string) rangeX = [minValue, maxValue] if not rangeY: maxValue = max([max(self.samples[sample].cut_data[cat][vY].values) for sample in self.samples]) minValue = min([min(self.samples[sample].cut_data[cat][vY].values) for sample in self.samples]) config_string = "variables[\""+vY+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue) with open("new_variable_configs.txt", "a") as f: f.write(config_string) rangeY = [minValue, maxValue] # init value lists weights = np.array([]) valuesX = np.array([]) valuesY = np.array([]) for sample in self.samples: weights = np.append(weights, self.samples[sample].cut_data[cat]["weight"].values) valuesX = np.append(valuesX, self.samples[sample].cut_data[cat][vX].values) valuesY = np.append(valuesY, self.samples[sample].cut_data[cat][vY].values) hist = setup.setupHistogram2D( valuesX = valuesX, valuesY = valuesY, weights = weights, binsX = binsX, binsY = binsY, rangeX = rangeX, rangeY = rangeY, titleX = vX, titleY = vY) canvas = setup.drawHistOnCanvas2D( hist = hist, canvasName = name, catLabel = JTcut.getJTlabel(cat), sampleName = name) # add lumi setup.printLumi(canvas, lumi = self.options["lumiScale"], twoDim = True) if self.options["privateWork"]: setup.printPrivateWork(canvas, ratio = self.options["ratio"], twoDim = True) if self.options["getCorr"]: correlation = hist.GetCorrelationFactor() setup.printCorrelation(canvas, correlation) # save canvas setup.saveCanvas(canvas, plot_name) # plot distributions in 1D if activated if self.options["plot1D"]: # get averages bins = int((binsX + binsY)/2.) bin_range = [min(rangeX[0],rangeY[0]), max(rangeX[1],rangeY[1])] hX = setup.setupHistogram( values = valuesX, weights = weights, nbins = bins, bin_range = bin_range, color = ROOT.kBlack, xtitle = vX+"1D", ytitle = setup.GetyTitle(self.options["lumiScale"]), filled = False) if self.options["lumiScale"] == 0.: hXInt = hX.Integral() hX.Scale(1./hXInt) hY = setup.setupHistogram( values = valuesY, weights = weights, nbins = bins, bin_range = bin_range, color = ROOT.kRed, xtitle = vY+"1D", ytitle = setup.GetyTitle(self.options["lumiScale"]), filled = False) if self.options["lumiScale"] == 0.: hYInt = hY.Integral() hY.Scale(1./hYInt) # init canvas canvas = setup.drawHistsOnCanvas( hX, hY, self.options, canvasName = name) # setup legend legend = setup.getLegend() legend.AddEntry( hX, self.options["xName"], "L") labelY = self.options["yName"] # add KS score to label if activated if self.options["KSscore"]: KSscore = setup.calculateKSscore(hX, hY) labelY="#splitline{"+labelY+"}{KSscore = %.3f}"%(KSscore) legend.AddEntry( hY, labelY, "L") # draw loegend legend.Draw("same") # add lumi and category to plot setup.printLumi(canvas, lumi = self.options["lumiScale"], ratio = self.options["ratio"]) setup.printCategoryLabel(canvas, JTcut.getJTlabel(cat), ratio = self.options["ratio"]) if self.options["privateWork"]: setup.printPrivateWork(canvas, ratio = self.options["ratio"]) # save canvas setup.saveCanvas(canvas, plot_name.replace(".pdf","_1D.pdf"))
def histVariable(self, variable, plot_name, cat): histInfo = {} # get number of bins and binrange from config file bins = binning.getNbins(variable) bin_range = binning.getBinrange(variable) # check if bin_range was found if not bin_range: maxValue = max([max(self.samples[sample].cut_data[cat][variable].values) for sample in self.samples]) minValue = min([min(self.samples[sample].cut_data[cat][variable].values) for sample in self.samples]) config_string = "variables[\""+variable+"\"]\t\t\t= Variable(bin_range = [{},{}])\n".format(minValue, maxValue) with open("new_variable_configs.txt", "a") as f: f.write(config_string) bin_range = [minValue, maxValue] histInfo["nbins"] = bins histInfo["range"] = bin_range bkgHists = [] bkgLabels = [] weightIntegral = 0 # loop over backgrounds and fill hists for sampleName in self.ordered_stack: sample = self.samples[sampleName] # get weights weights = sample.cut_data[cat]["weight"].values # get values values = sample.cut_data[cat][variable].values #weights = [weights[i] for i in range(len(weights)) if not np.isnan(values[i])] #values = [values[i] for i in range(len(values)) if not np.isnan(values[i])] weightIntegral += sum(weights) # setup histogram hist = setup.setupHistogram( values = values, weights = weights, nbins = bins, bin_range = bin_range, color = sample.plotColor, xtitle = cat+"_"+sample.sampleName+"_"+variable, ytitle = setup.GetyTitle(self.options["lumiScale"]), filled = sample.filled) bkgHists.append(hist) bkgLabels.append(sample.sampleName) sigHists = [] sigLabels = [] sigScales = [] # if not background was added, the weight integral is equal to 0 if weightIntegral == 0: self.options["scaleSignal"] = 0 histInfo["bkgYield"] = weightIntegral # scale stack to one if lumiScale is set to zero if self.options["lumiScale"] == 0: for hist in bkgHists: hist.Scale(1./weightIntegral) weightIntegral = 1. # loop over signals and fill hists for key in self.samples: sample = self.samples[key] if not sample.isSignal: continue # get weights weights = sample.cut_data[cat]["weight"].values # determine scale factor if self.options["scaleSignal"] == -1: scaleFactor = weightIntegral/(sum(weights)+1e-9) elif self.options["scaleSignal"] == 0: scaleFactor = (1./(sum(weights)+1e-9)) else: scaleFactor = float(self.options["scaleSignal"]) # setup histogram hist = setup.setupHistogram( values = sample.cut_data[cat][variable].values, weights = weights, nbins = bins, bin_range = bin_range, color = sample.plotColor, xtitle = cat+"_"+sample.sampleName+"_"+variable, ytitle = setup.GetyTitle(), filled = sample.filled) hist.Scale(scaleFactor) sigHists.append(hist) sigLabels.append(sample.sampleName) sigScales.append(scaleFactor) # init canvas canvas = setup.drawHistsOnCanvas( sigHists, bkgHists, self.options, canvasName = variable) # setup legend legend = setup.getLegend() # add signal entriesa for iSig in range(len(sigHists)): labelstring = sigLabels[iSig] if not self.options["lumiScale"] == 0.: labelstring = sigLabels[iSig]+" x {:4.0f}".format(sigScales[iSig]) # add KS score to label if activated if self.options["KSscore"]: KSscore = setup.calculateKSscore(bkgHists[0],sigHists[iSig]) labelstring="#splitline{"+labelstring+"}{KSscore = %.3f}"%(KSscore) histInfo["KSScore"] = KSscore legend.AddEntry(sigHists[iSig], labelstring, "L") # add background entries for iBkg in range(len(bkgHists)): legend.AddEntry(bkgHists[iBkg], bkgLabels[iBkg], "F") # draw loegend legend.Draw("same") # add lumi and category to plot setup.printLumi(canvas, lumi = self.options["lumiScale"], ratio = self.options["ratio"]) setup.printCategoryLabel(canvas, JTcut.getJTlabel(cat), ratio = self.options["ratio"]) if self.options["privateWork"]: setup.printPrivateWork(canvas, ratio = self.options["ratio"]) # save canvas setup.saveCanvas(canvas, plot_name) return histInfo