def DrawObjectROOT(particle, pt, ptype): import math phiAxis = pt * 2. * math.pi / 224. # Ellypse axis etaAxis = pt * 9. / 224. y = particle.phi() try: x = particle.eta() except: x = 0 ellipse = TEllipse(x, y, etaAxis, phiAxis) if 'mltop' in ptype: ellipse.SetLineColor(10000 + len(ptype)) ellipse.SetLineWidth(2) # ellipse.SetFillColorAlpha(20000+len(ptype), 0.0) #Este provoca solapamiento, mejor no usar ellipse.SetFillStyle(0) # Mejor usar este (circunferencias) else: # ellipse.SetLineWidth(2) #Empty ellipse.SetLineWidth(0) # ellipse.SetLineColor(10000+len(ptype)) #Empty ellipse.SetFillColor(20000 + len(ptype)) # ellipse.SetFillStyle(0) #Empty ellipse.SetFillStyle(1001) return ellipse
def addEllipse(self, x0, y0, r1, r2, color, fillcolor=False, thickness=0.): ellipse = TEllipse(x0, y0, r1, r2) ellipse.SetLineColor(color) if fillcolor: ellipse.SetFillColor(fillcolor) else: ellipse.SetFillStyle(0) if thickness: ellipse.SetLineWidth(thickness) self.ellipses.append(ellipse)
def covarianceMatrixEllipse(covarianceMatrix, probability=0.683): """Plot ellipse with a given cl from a 2x2 covariance matrix. TODO: better to create a class to store all the information and fastly plot within the class? """ k = np.sqrt(-2 * np.log(1 - probability)) secondDerivativeMatrix = np.linalg.inv(covarianceMatrix) eigenvals, eigenvecs = np.linalg.eig(secondDerivativeMatrix) lambd = np.sqrt(eigenvals) angle = np.rad2deg(np.arctan(eigenvecs[1, 0] / eigenvecs[0, 0])) ellipse = TEllipse(0, 0, k / lambd[0], k / lambd[1], 0., 360., angle) ellipse.SetFillStyle(0) ellipse.SetLineColor(2) ellipse.SetLineWidth(4) scale = max(k / lambd[0], k / lambd[1]) return ellipse, scale
def DrawObjectROOT(particle, pt, ptype) : phiAxis = pt *2.* math.pi / 224. # Ellypse axis etaAxis = pt *9. / 224. y = particle.Phi() try : x = particle.Eta() except: x = 0 ellipse = TEllipse(x,y,etaAxis, phiAxis) if 'mltop' in ptype : ellipse.SetLineColor(10000+len(ptype)) ellipse.SetLineWidth(2) ellipse.SetFillColorAlpha(20000+len(ptype), 0.0) else : ellipse.SetLineWidth(0) ellipse.SetFillColor(20000+len(ptype)) ellipse.SetFillStyle(1001) return ellipse
class CovarianceMatrix2DContourPlot(object): """2D confidence interval contour plot. This is a base class to plot, given a 2D fit result, the contour region (that is an ellipse in the gaussian likelihood approximation) relative to a given confidence level, on the two dimensional space parameters. TODO: extend the class to plot more than one covariance_matrix! TODO: raise exceptions in the init code instead of printing some erros message! TODO: would a name like PlotFitResult be nicer? """ def __init__(self, name, fit_parameters, covariance_matrix, args = dict(sigma = 1)): """Constructor with the fit output. Provide the constructor with the following objects: - name: identificative name for ROOT objects - fit_parameters: numpy array (shape 1,2) containing the two paramters of the fit - covariance_matrix: numpy array (shape 2,2) containing the covariance matrix of the fit - args: dictionary to specify the type of plot you want: It can be: - dict(sigma = 1) - dict(probability = 0.68) """ self.name = name self.fit_parameters = fit_parameters self.covariance_matrix = covariance_matrix self.args = args if len(self.args) != 1: print "args length different than 1" return if 'sigma' in self.args: self.sigma = self.args['sigma'] self.ellipse_dimension = self.sigma elif 'probability' in self.args: self.probability = self.args['probability'] if self.probability > 1 or self.probability < 0: print "probability must be between 0 and 1!" return self.ellipse_dimension = np.sqrt(-2 * np.log(1 - self.probability)) else: print "neither 'sigma' nor 'probability' in args" return def DefineEllipse(self): self.second_derivative_matrix = np.linalg.inv(self.covariance_matrix) self.eigenvals, self.eigenvecs = np.linalg.eig(self.second_derivative_matrix) self.lambd = np.sqrt(self.eigenvals) self.angle = np.rad2deg(np.arctan(self.eigenvecs[1,0]/self.eigenvecs[0,0])) self.ellipse = TEllipse(self.fit_parameters[0, 0], self.fit_parameters[0, 1], self.ellipse_dimension/self.lambd[0], self.ellipse_dimension/self.lambd[1], 0., 360., self.angle) self.ellipse.SetFillStyle(0) self.ellipse.SetLineColor(2) self.ellipse.SetLineWidth(4) self.graph_axis_range = max(self.ellipse_dimension/self.lambd[0], self.ellipse_dimension/self.lambd[1]) def PrepareDraw(self, title = '', legend_title = '', xtitle = '', ytitle = ''): self.title = title self.legend_title = legend_title self.xtitle = xtitle self.ytitle = ytitle self.x_axis = np.array([-self.graph_axis_range * 2.1, self.graph_axis_range * 2.1, self.fit_parameters[0, 0], self.fit_parameters[0, 0], self.fit_parameters[0, 0], self.fit_parameters[0, 0]]) self.y_axis = np.array([self.fit_parameters[0, 1], self.fit_parameters[0, 1], self.fit_parameters[0, 1], -self.graph_axis_range * 2.1, self.fit_parameters[0, 1], self.graph_axis_range * 2.1]) self.graph_axis = TGraph(6, self.x_axis, self.y_axis) self.graph_axis.SetNameTitle("axis_" + self.name, self.title) self.graph_axis.GetXaxis().SetTitle(xtitle) self.graph_axis.GetYaxis().SetTitle(ytitle) self.graph_axis.GetXaxis().SetRangeUser(self.fit_parameters[0, 0] - self.graph_axis_range * 2., self.fit_parameters[0, 0] + self.graph_axis_range * 2.) self.graph_axis.GetYaxis().SetRangeUser(self.fit_parameters[0, 1] - self.graph_axis_range * 2., self.fit_parameters[0, 1] + self.graph_axis_range * 2.) self.ellipse.SetFillStyle(3001) self.ellipse.SetFillColor(2) self.ellipse.SetLineColor(2) self.ellipse.SetLineWidth(4) self.legend = TLegend(0.7407705, 0.6983945, 0.9911717, 0.928899) key, value = self.args.items()[0] if key == 'sigma': aux_legend_string = self.legend_title + " " + str(value) + ' #sigma' else: aux_legend_string = self.legend_title + " C.L. " + str(int(100 * value)) + '%' self.legend.AddEntry(self.ellipse, aux_legend_string, "f") def Draw(self): TopMassStyle() self.canvas = TCanvas("canvas_" + self.name, self.title, 800, 600) self.canvas.cd() self.canvas.SetGrid() self.graph_axis.Draw("AP") self.ellipse.Draw("same") self.legend.Draw("same")
def MakePlots(self): if not hasattr(self, "Numerator") or not hasattr(self, "Denominator"): print "Either Numerator or Denominator has not been defined. Not finding hot spots..." return circles = [] for spots in self._hotSpotsList: circle = TEllipse(float(spots[0]), float(spots[1]), 0.06) circle.SetLineColor(2) circle.SetLineWidth(1) circle.SetFillStyle(0) circles.append(circle) LumiText = str.format('{0:.1f}', self._luminosityInInvFb) + " fb^{-1} (13 TeV)" pasLumiLatex = TLatex() pasLumiLatex.SetNDC() pasLumiLatex.SetTextAngle(0) pasLumiLatex.SetTextFont(42) pasLumiLatex.SetTextAlign(32) pasLumiLatex.SetTextSize(0.04) pasCMSLatex = TLatex() pasCMSLatex.SetNDC() pasCMSLatex.SetTextAngle(0) pasCMSLatex.SetTextFont(62) pasCMSLatex.SetTextAlign(12) pasCMSLatex.SetTextSize(0.04) pasPrelimLatex = TLatex() pasPrelimLatex.SetNDC() pasPrelimLatex.SetTextAngle(0) pasPrelimLatex.SetTextFont(62) pasPrelimLatex.SetTextAlign(12) pasPrelimLatex.SetTextSize(0.04) self.Denominator["histogram"].Draw('colz') pasLumiLatex.DrawLatex(0.96, 0.93, LumiText) pasCMSLatex.DrawLatex(0.12, 0.925, "CMS Preliminary") self._canvas.SaveAs('fiducialMapCalc_beforeVeto_' + self.Denominator["sample"] + '.pdf') self.Numerator["histogram"].Draw('colz') pasLumiLatex.DrawLatex(0.96, 0.93, LumiText) pasCMSLatex.DrawLatex(0.12, 0.925, "CMS Preliminary") self._canvas.SaveAs('fiducialMapCalc_afterVeto_' + self.Numerator["sample"] + '.pdf') self._canvas.SetLogz(False) self.Numerator["histogram"].Divide(self.Denominator["histogram"]) self.Numerator["histogram"].Draw('colz') if 'SingleEle' in self.Numerator["sample"]: self.Numerator["histogram"].GetZaxis().SetRangeUser(0, 0.5) elif 'SingleMu' in self.Numerator["sample"]: self.Numerator["histogram"].GetZaxis().SetRangeUser(0, 0.05) self.Numerator["histogram"].GetZaxis().SetLabelSize(0.025) for circle in circles: circle.Draw("same") pasLumiLatex.DrawLatex(0.96, 0.93, LumiText) pasCMSLatex.DrawLatex(0.12, 0.925, "CMS Preliminary") self._canvas.SaveAs('fiducialMapCalc_efficiency_' + self.Numerator["sample"] + '.pdf') for xbin in range(1, self.Numerator["histogram"].GetXaxis().GetNbins()): for ybin in range( 1, self.Numerator["histogram"].GetYaxis().GetNbins()): thisInefficiency = self.Numerator["histogram"].GetBinContent( xbin, ybin) if thisInefficiency == 0: continue valueInSigma = (thisInefficiency - self._meanInefficiency ) / self._stdDevInefficiency if valueInSigma < 0: valueInSigma = 0 self.Numerator["histogram"].SetBinContent( xbin, ybin, valueInSigma) self._canvas.SetLogz(False) self.Numerator["histogram"].GetZaxis().SetLabelSize(0.04) self.Numerator["histogram"].Draw('colz') if 'SingleEle' in self.Numerator["sample"]: if '2017' in self.Numerator["sample"]: self.Numerator["histogram"].GetZaxis().SetRangeUser(0, 7) self.Numerator["histogram"].GetZaxis().SetRangeUser(0, 12) elif 'SingleMu' in self.Numerator["sample"]: self.Numerator["histogram"].GetZaxis().SetRangeUser(0, 23) self.Numerator["histogram"].GetZaxis().SetLabelSize(0.025) for circle in circles: circle.Draw("same") pasLumiLatex.DrawLatex(0.96, 0.93, LumiText) pasCMSLatex.DrawLatex(0.12, 0.925, "CMS Preliminary") self._canvas.SaveAs('fiducialMapCalc_efficiencyInSigma_' + self.Numerator["sample"] + '.pdf')
class CovarianceMatrix2DContourPlot(object): """ TODO: extend the class to plot more than one covariance_matrix! TODO: raise exceptions in the init code instead of printing some erros message! """ def __init__(self, name, mean, covariance_matrix, args=dict(sigma=1)): """ """ self.name = name self.mean = mean self.covariance_matrix = covariance_matrix self.args = args if len(self.args) != 1: print "args length different than 1" return if 'sigma' in self.args: self.sigma = self.args['sigma'] self.k = self.sigma elif 'probability' in self.args: self.probability = self.args['probability'] if self.probability > 1 or self.probability < 0: print "probability must be between 0 and 1!" return self.k = np.sqrt(-2 * np.log(1 - self.probability)) else: print "neither 'sigma' nor 'probability' in args" return self.second_derivative_matrix = np.linalg.inv(self.covariance_matrix) self.eigenvals, self.eigenvecs = np.linalg.eig( self.second_derivative_matrix) self.lambd = np.sqrt(self.eigenvals) self.angle = np.rad2deg( np.arctan(self.eigenvecs[1, 0] / self.eigenvecs[0, 0])) self.ellipse = TEllipse(self.mean[0], self.mean[1], self.k / self.lambd[0], self.k / self.lambd[1], 0., 360., self.angle) self.ellipse.SetFillStyle(0) self.ellipse.SetLineColor(2) self.ellipse.SetLineWidth(4) self.scale = max(self.k / self.lambd[0], self.k / self.lambd[1]) def Draw(self): self.canvas = TCanvas("canvas_" + self.name, self.name, 800, 600) self.x_axis = np.array( [-self.scale * 2.1, self.scale * 2.1, 0., 0., 0., 0.]) self.y_axis = np.array( [0., 0., 0., -self.scale * 2.1, 0., self.scale * 2.1]) self.axis = TGraph(6, x, y) self.axis.SetNameTitle("axis_" + self.name, self.name) self.ellipse.SetFillStyle(3001) self.ellipse.SetFillColor(2) self.ellipse.SetLineColor(2) self.ellipse.SetLineWidth(4) self.legend = TLegend(0.7407705, 0.6983945, 0.9911717, 0.928899) self.legend.AddEntry(self.ellipse, "FCC-ee 1 #sigma ", "f") self.canvas.cd() self.canvas.SetGrid() self.axis.Draw("AP") self.ellipse.Draw("same") self.legend.Draw("same") def LoadExternalPoints(self): external_points = TGraph() external_points.SetNameTitle() with open(fileName) as f: for i, line in enumerate(f): data = line.split() external_points.SetPoint(i, data[0], data[1]) external_points.GetXaxis().SetLimits(-6, 6) external_points.GetYaxis().SetRangeUser(-6, 10) external_points.GetXaxis().SetTitle("#Delta g_{L} / g_{L} (%)") external_points.GetYaxis().SetTitle("#Delta g_{R} / g_{R} (%)") external_points.SetTitle( "Expected relative precision on the Zt_{L}t_{L} and Zt_{R}t_{R} couplings at FCC-ee" ) external_points.SetMarkerSize(0.4) external_points.SetMarkerStyle(20) self.legend.AddEntry(external_points, "4DCHM f #leq 1.5 TeV", "p")