def plot_data_vs_refold(args, regularisation_settings, tau): ''' Plot the differences between the unfolded and refolded distributions TODO Include also with best tau - redo unfolding with best tau then come here ''' from ROOT import gStyle variable = regularisation_settings.variable channel = regularisation_settings.channel plot_outpath = regularisation_settings.outpath.replace('tables/', 'plots/')+'tauscan/taus/' make_folder_if_not_exists(plot_outpath) # tau as string name for output tau = str(tau).replace('.', 'p') outfile = plot_outpath+'data_vs_refold_'+channel+'_'+variable+'_tau_'+tau+'.pdf' if args.run_measured_as_data: outfile = plot_outpath+'measured_vs_refold_'+channel+'_'+variable+'_tau_'+tau+'.pdf' if args.run_smeared_measured_as_data: outfile = plot_outpath+'smeared_vs_refold_'+channel+'_'+variable+'_tau_'+tau+'.pdf' if args.unfolded_binning: outfile = outfile.replace('.pdf', '_unf_binning.pdf') c = TCanvas('c1','c1',1000,800) gStyle.SetOptStat(0) p1 = TPad("pad1", "p1",0.0,0.2,1.0,1.0,21) p1.SetFillColor(0); p1.Draw() p2 = TPad("pad2", "p2",0.0,0.0,1.0,0.2,22) p2.SetFillColor(0); p2.Draw() p1.cd() regularisation_settings.h_data.SetTitle("Data vs Refolded Data;;NEvents") regularisation_settings.h_data.Draw() regularisation_settings.h_refolded.SetLineColor(2) regularisation_settings.h_refolded.Draw("same") leg1 = TLegend(0.7, 0.8, 0.9, 0.9) leg1.SetLineColor(0) leg1.SetFillColor(0) leg1.AddEntry(regularisation_settings.h_data, "Data") leg1.AddEntry(regularisation_settings.h_refolded, "Refolded Data") leg1.Draw() p2.cd() h_ratio = regularisation_settings.h_data.Clone() h_ratio.Divide(regularisation_settings.h_refolded) h_ratio.SetTitle(";"+variable+";") h_ratio.SetLineColor(1); h_ratio.Draw() c.SaveAs(outfile) c.Delete() print "Written plots to {outfile}".format(outfile = outfile) return
def _calibrate_spectrum(self, h, title, proc, func='pol1', plot=True): """ Calibrates a DeltaE/E versus x spectrum based on the profile. """ import os from UserCode.HGCalMaskResolutionAna.RootTools import buildMedianProfile prof = buildMedianProfile(h) prof.Fit(func) calibGr = prof.GetListOfFunctions().At(0).Clone(h.GetName() + title + '_calib') if plot: from ROOT import TCanvas, TLatex c = TCanvas('c', 'c', 500, 500) c.SetTopMargin(0.05) c.SetBottomMargin(0.1) c.SetLeftMargin(0.12) c.SetRightMargin(0.1) h.Draw('colz') prof.Draw('e2p') tex = TLatex() tex.SetTextFont(42) tex.SetTextSize(0.04) tex.SetNDC() tex.DrawLatex(0.12, 0.96, '#bf{CMS} #it{simulation preliminary}') tex.DrawLatex(0.15, 0.88, title) tex.SetTextAlign(31) tex.DrawLatex(0.97, 0.96, proc) c.SaveAs( os.path.join(self.outpath, h.GetName() + '_' + title + '.png')) prof.Delete() c.Delete() return calibGr
class Plotter(object): populations = { "World" : 7.774797e9, "China" : 1.439323776e9, "US" : 331.002651e6, "Ukraine" : 43.733762e6, "Italy" : 60.461826e6, "France" : 65.273511e6, "India" : 1.380004385e9, "Spain" : 46.750411e6 } day = 3600. * 24. s = 1. fill_redish = 46 fill_sandy = 43 fill_greenish = 30 # names = ("confirmed", "deaths", "recovered") def __init__( self, country, draw_seq = ("confirmed", "recovered", "deaths"), derivative = 0, to_fit = True, fit_start = "03/22/20", fit_end = "03/28/20", fit_hist = "confirmed", draw_fit = True, floating_average = -1 ): super(Plotter, self).__init__() self.country = country self.data = { "confirmed" : self.get_data("confirmed"), "deaths" : self.get_data("deaths"), "recovered" : self.get_data("recovered") } self.sequence = draw_seq self.derivative = derivative self.to_fit = to_fit self.fit_start = fit_start self.fit_end = fit_end self.fit_hist = fit_hist self.draw_fit = draw_fit self.has_fit = False self.make_def() if to_fit: self.fit() if floating_average != -1: self.do_floating_average(floating_average) def fit(self): self.func = fit_hist(self.hists[self.fit_hist], self.fit_start, self.fit_end) self.has_fit = True def make_hist(self, x, y, name, color): gr = TH1D(name, name, len(x), x[0], x[-1]) for x_i, y_i, i in zip(x, y, range(1, len(x)+1)): if y_i == 0.: gr.SetBinContent(i, 0. + 1e-20) # gr.SetBinError(i, 0.) else: gr.SetBinContent(i, y_i + 1e-20) gr.SetBinError(i, y_i*0.05) # gr.SetBinError(i, sqrt(y_i)) # gr.Sumw2() gr.GetXaxis().SetTimeDisplay(1) gr.SetLineColor(color) gr.SetMarkerStyle(21) gr.SetMarkerSize(0.5) gr.SetFillStyle(3002) gr.SetFillColor(color) gr.GetXaxis().SetNdivisions(16) gr.GetXaxis().SetLabelSize(0.02) return gr def hist_from_data(self, data_type, color): x, y = self.data[data_type] for i in range(self.derivative): y = diferentiate(y) return self.make_hist(x, y, data_type, color) def zoom_axis(self, begin, end): begin = date_to_ut(begin) end = date_to_ut(end) for key in self.hists: self.hists[key].GetXaxis().SetRangeUser(begin, end) # self.draw() def draw(self, log = False): self.setup_graphics() if log: self.c.SetLogy() for i in range(0, len(self.sequence)): if i == 0: self.hists[self.sequence[0]].Draw("MIN0 HIST E1") else: self.hists[self.sequence[i]].Draw("HIST E1 SAME") self.legend.Draw() if self.has_fit and self.draw_fit: self.func.Draw("same") doubling, oneday = print_slope(self.func) newpad = TPad("newpad","a transparent pad",0.15, 0.65, 0.4, 0.55); self.text = TPaveText(0.1, 0.1, 0.9, 0.9) self.text.AddText("Doubles every {} days".format(doubling)) self.text.AddText("Multiplies by {} every day".format(oneday)) newpad.SetFillStyle(4000); newpad.Draw() newpad.cd() self.text.Draw() # input() def setup_graphics(self): gStyle.SetOptStat(0) gStyle.SetOptTitle(0) gROOT.ForceStyle() self.c = TCanvas("c", "c", 1920, 1080) def make_hist(self, x, y, name, color): gr = TH1D(name, name, len(x), x[0], x[-1]) for x_i, y_i, i in zip(x, y, range(1, len(x)+1)): if y_i == 0.: gr.SetBinContent(i, 0.) else: gr.SetBinContent(i, y_i + 1e-30) gr.SetBinError(i, 0.05 * y_i) # gr.Sumw2() gr.GetXaxis().SetTimeDisplay(1) gr.SetLineColor(color) gr.SetMarkerStyle(21) gr.SetMarkerSize(0.5) gr.SetFillStyle(3002) gr.SetFillColor(color) gr.GetXaxis().SetNdivisions(16) gr.GetXaxis().SetLabelSize(0.02) return gr def make_def(self): self.hists = { "confirmed" : self.hist_from_data("confirmed", self.fill_sandy), "deaths" : self.hist_from_data("deaths", self.fill_redish), "recovered" : self.hist_from_data("recovered", self.fill_greenish) } self.make_legend() def make_legend(self): l = TLegend(0.15, 0.85, 0.4, 0.7) if self.country != "wo China": l.SetHeader(self.country, "c") else: l.SetHeader("World (without Mainland China)", "c") for name in self.hists: l.AddEntry(self.hists[name], name) self.legend = l def get_data(self, data_type): path = "./COVID-19/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_{}_global.csv".format(data_type) my_data = genfromtxt(path, delimiter=',', dtype=str) time_series_number = [] for line in my_data: if line[1] == "Country/Region": time_series_date = line elif self.country == "wo China" and line[1] != "China": time_series_number.append(line) if line[1] == self.country: time_series_number.append(line) # if line[0] == "Hubei": # time_series_number = line cut = 4 time_series_date = time_series_date[cut:] time_series_number = [time_series_number[i][cut:] for i in range(0, len(time_series_number))] time_series_number = zip(*time_series_number) y = array([list(map(float, i)) for i in time_series_number]) y = array(list(map(sum, y))) x = array([date_to_ut(i) for i in time_series_date]) # print x # x = x[(len(x) - len(y)):] return x, y def clear(self): for key in self.hists: self.hists[key].Delete() try: self.func.Delete() except: pass self.c.Delete() def do_floating_average(self, length): for key in self.hists: nBins = self.hists[key].GetNbinsX() temp_length = length for bin_i in range(1, nBins + 1): if bin_i < length: temp_length = bin_i average_value = 0 total_error = 0 for float_i in range(bin_i - temp_length + 1, bin_i + 1): average_value += self.hists[key].GetBinContent(float_i) total_error += self.hists[key].GetBinError(float_i)**2 average_value /= temp_length total_error = sqrt(total_error) / temp_length self.hists[key].SetBinContent(bin_i, average_value) self.hists[key].SetBinError(bin_i, total_error) temp_length = length return
def makePlot(trees, cut, variable, combination, name, title=None, datasetName="Data"): from ROOT import TCanvas, TPad, TLegend, kBlue, kRed, TLatex from src.ratios import RatioGraph from src.histos import getHisto from math import sqrt from random import random if title is None: title = name cut = cut.replace("inv", "p4.M()") if "iso" in variable: cut = cut.replace("id1 < 0.15", "id1 < 10") cut = cut.replace("id2 < 0.15", "id2 < 10") print cut baseColor = kBlue if "jzb" in name: baseColor = kRed rmue = 1.21 trigger = {"EE": 0.970, "EMu": 0.942, "MuMu": 0.964} histos = { "MuMu": getHisto(trees["MuMu"], cut, variable), "EE": getHisto(trees["EE"], cut, variable), "EMu": getHisto(trees["EMu"], cut, variable), "EMu_MuLead": getHisto(trees["EMu"], "(%s) * (pt2 > pt1)" % cut, variable), } rmueSys = 0 predictionSrc = "EMu" if combination == "Both": sameFlavour = histos["MuMu"].Clone("sameFlavourSum") sameFlavour.Add(histos["EE"]) #~ nllPredictionScale = 0.5* sqrt(trigger["EE"]*trigger["MuMu"])*1./trigger["EMu"] *(rmue+1./(rmue)) #~ rmueSys = sqrt(sum(i**2 for i in [0.5*(1-(1./rmue**2))*0.1 + (1./trigger["EMu"] + 0.5*(1./trigger["EE"]+1./trigger["MuMu"]))*0.05])) nllPredictionScale = 1.02 rmueSys = 0.07 #0.5*(1-1./rmue**2)*0.1 elif combination in ["EE", "MuMu"]: rmueFactor = rmue if combination == "EE": rmueFactor = 1. / rmue nllPredictionScale = 0.5 * sqrt( trigger["EE"] * trigger["MuMu"]) * 1. / trigger["EMu"] * rmueFactor rmueSys = sqrt(0.1**2 + (0.67 / 1.025)**2) * nllPredictionScale sameFlavour = histos[combination].Clone("sameFlavourSum") elif combination == "MuMuSF": predictionSrc = "MuMu" rmueFactor = 1. / rmue nllPredictionScale = trigger["EE"] * 1. / trigger["MuMu"] * (rmueFactor **2) rmueSys = (2 * rmue) * 0.1 sameFlavour = histos["EE"].Clone("sameFlavourSum") else: raise StandardError, "unknown combination' %s'" % combination prediction = histos[predictionSrc].Clone("ofPrediction") predictionMuLead = histos["EMu_MuLead"].Clone("ofPrediction_MuLeading") prediction.Scale(nllPredictionScale) predictionMuLead.Scale(nllPredictionScale) rooTex = { "Both": "ee + #mu#mu", "EE": "ee", "MuMu": "#mu#mu", "MuMuSF": "ee", "EMu_Prediction": "OF-Prediciton", "MuMu_Prediction": "SF(#mu#mu)-Prediciton" } canv = TCanvas("canv", "", 800, 800) canv.Draw() pad = TPad("main_%x" % (1e16 * random()), "main", 0.01, 0.25, 0.99, 0.99) pad.SetNumber(1) pad.Draw() canv.cd() resPad = TPad("residual_%x" % (1e16 * random()), "residual", 0.01, 0.01, 0.99, 0.25) resPad.SetNumber(2) resPad.Draw() pad.cd() #~ if "iso" in variable: #~ canv.SetLogy(1) #~ pad.SetLogy(1) sameFlavour.SetMarkerStyle(20) sameFlavour.SetLineColor(1) sameFlavour.Draw("E P") sameFlavour.SetTitle(title) import ROOT histos["MuMu"].SetLineColor(ROOT.kBlack) histos["MuMu"].SetFillColor(ROOT.kWhite) prediction.SetLineColor(1) prediction.SetFillColor(baseColor - 7) prediction.Draw("SAME Hist") if predictionSrc == "EMu": predictionMuLead.SetLineColor(1) predictionMuLead.SetFillColor(baseColor - 9) predictionMuLead.Draw("SAME Hist") sameFlavour.Draw("E P SAME") histos["MuMu"].Draw("SAME Hist") pad.RedrawAxis() canv.RedrawAxis() leg = TLegend(0.75, 0.65, 1, 0.9) leg.SetFillColor(0) leg.AddEntry(sameFlavour, rooTex[combination], "P") leg.AddEntry(histos["MuMu"], "#mu#mu events", "l") leg.AddEntry(prediction, rooTex[predictionSrc + "_Prediction"], "F") if predictionSrc == "EMu": leg.AddEntry(predictionMuLead, "#mu leading", "F") leg.Draw() # print "_".join([name,variable]), # print "EE",histos["EE"].GetEntries(), "MuMu:", histos["MuMu"].GetEntries(), "EMu:", histos["EMu"].GetEntries(), # print " SF:", sameFlavour.GetEntries(), "OF:", prediction.GetEntries() # tex = TLatex() # tex.SetNDC() # tex.SetTextSize() # tex.DrawLatex(0.6, 0.7, title) residuals = RatioGraph(sameFlavour, prediction) residuals.addErrorBySize(rmueSys, rmueSys, add=False, color=kBlue - 9) residuals.draw(resPad, yMin=0.5, yMax=1.5) canv.Update() variable = variable.replace("(", "").replace(")", "") plotPath = "fig/%s.pdf" % ("_".join( [datasetName, name, combination, variable])) canv.Print(plotPath) pad.Delete() resPad.Delete() canv.Delete() # raw_input() return plotPath
def main(): BIAS_DIR = global_paths.BIASDIR + args.btagging + "/" if args.year == 'run2c': BIAS_DIR += "combined_run2_signal{}/" ## individual plots stored in run2c_masspoints ## extract pulls pulls = TGraph() for m in range(1600, 8001, 100): print print print "m = " + str(m) for pull0 in [1, 3, 5, 8, 10, 15, 20, 50]: print print "r = " + str(pull0) print "--------------------" #try: tfile = TFile.Open( BIAS_DIR.format(pull0) + "fitDiagnostics_M{mass}.root".format(mass=m), "READ") tree = tfile.Get("tree_fit_sb") ## the method proposed in the documemtation #hist = TH1D("hist", "hist", 20, -5, 5) #tree.Project("hist", "(r-1)/(0.5*(rHiErr+rLoErr))") #fit_func = TF1("gaussfit","gaus" , -5., 5.) #hist.Fit(fit_func, "E") #pulls.SetPoint(pulls.GetN(), m, fit_func.GetParameter(1)) ## get mean of gaussian fit ## Alberto's method #pull0=PULL0[m] #pull0=10. hist = TH1D("s_pulls", ";%s/#sigma_{r};Number of toys" % ("(r - " + str(pull0) + ")"), 25, -5, +5) # for i in range(tree.GetEntries()): if hist.GetEntries() >= 1000: continue tree.GetEntry(i) #print "r = {} (+{}, -{})".format(tree.r, tree.rHiErr, tree.rLoErr) ##if tree.rLoErr < 0.: continue if abs(tree.r + 1.) < 0.001: continue if abs(tree.r - 1.) < 0.001: continue if abs(tree.r - 0.) < 0.001: continue #if abs(tree.rLoErr)>8.: continue # trying to skip these values FIXME if tree.rHiErr == 0. or tree.rLoErr == 0.: continue print "r = {} (+{}, -{})".format(tree.r, tree.rHiErr, tree.rLoErr) #pull = (tree.r-pull0)/(0.5*(abs(tree.rHiErr)+abs(tree.rLoErr))) ## documentation approach #pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r-pull0 < 0. else (tree.r-pull0)/abs(tree.rLoErr) ## Alberto's sign convention pull = (tree.r - pull0) / abs( tree.rHiErr) if tree.r - pull0 > 0. else ( tree.r - pull0) / abs(tree.rLoErr) ## my own approach #pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r < 0. else (tree.r-pull0)/abs(tree.rLoErr) ## Alberto's sign convention but depending directly on the sign of r #pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r > 0. else (tree.r-pull0)/abs(tree.rLoErr) ## my own approach with an rErr dependence on r, not r-1 hist.Fill(pull) ### individual pltos for checking the fit quality c1 = TCanvas("c1", "Pulls", 600, 600) c1.cd() c1.GetPad(0).SetTopMargin(0.06) c1.GetPad(0).SetRightMargin(0.05) c1.GetPad(0).SetBottomMargin(0.15) c1.GetPad(0).SetTicks(1, 1) #print "@ m= {}: \t mean = {}".format(m, hist.GetMean()) #pulls.SetPoint(pulls.GetN(), m, hist.GetMean()) ## get actual mean of histogram fit_func = TF1("gaussfit", "gaus", -3., 3.) ##fit_func.SetParameter(1, hist.GetMean()) fit_func.SetParameter(1, 0.) ##fit_func.SetParLimits(1, -0.8, 0.8) fit_func.SetParameter(2, 1.) ##fit_func.SetParameter(0, 45.) ##fit_func.SetParLimits(0, 30., 100.) hist.Fit(fit_func, "E") hist.Draw() c1.Print("plots/bias/r_comparison/bias_fit_".format(m) + str(m) + "_r" + str(pull0) + ".png") pulls.SetPoint( pulls.GetN(), m, fit_func.GetParameter(1)) ## get fitted gaussian mean hist.Delete() c1.Delete() tfile.Close()
def main(): gStyle.SetOptStat(0) BIAS_DIR = global_paths.BIASDIR + args.btagging + "/" if args.year == 'run2c': BIAS_DIR += "combined_run2_r{}{}/" ## individual plots stored in run2c_masspoints ## extract pulls pulls = {} for signal_strength in ['0', '2sigma', '5sigma']: print print print "--------------------------------------------------" print "r = " + signal_strength print "--------------------------------------------------" pulls[signal_strength] = TGraphErrors() for m in range(1600, 8001, 100): if (signal_strength == '2sigma' and m < 2600) or (signal_strength == '5sigma' and m < 4100): ##FIXME FIXME datacard_correction = True else: datacard_correction = False print print "m = " + str(m) if datacard_correction: print "correcting signal strength in the datacard" print pull0 = int(SIGNAL_STRENGTH[signal_strength][m]) tree = TChain("tree_fit_sb") for seed in [ '123456', '234567', '345678', '456789', '567891', '678912', '789123', '891234', '912345', '123459' ]: tree.Add( BIAS_DIR.format(signal_strength, "_lowm" if datacard_correction else "") + "fitDiagnostics_M{mass}_{seed}.root".format( mass=m, seed=seed)) ##FIXME FIXME hist = TH1D("s_pulls", ";%s/#sigma_{r};Number of toys" % ("#Deltar"), 25, -5, +5) # for i in range(tree.GetEntries()): if hist.GetEntries() >= 1000: continue tree.GetEntry(i) #print "r = {} (+{}, -{})".format(tree.r, tree.rHiErr, tree.rLoErr) ##if tree.rLoErr < 0.: continue if abs(tree.r + 1.) < 0.001: continue if abs(tree.r - 1.) < 0.001: continue if abs(tree.r - 0.) < 0.001: continue if tree.rHiErr == 0. or tree.rLoErr == 0.: continue if abs(tree.r + abs(tree.rHiErr) - round(tree.r + abs(tree.rHiErr))) < 0.0001: continue if abs(tree.r - abs(tree.rLoErr) - round(tree.r - abs(tree.rLoErr))) < 0.0001: continue #print "r = {} (+{}, -{})".format(tree.r, tree.rHiErr, tree.rLoErr) pull = (tree.r - pull0) / abs( tree.rHiErr) if tree.r - pull0 > 0. else ( tree.r - pull0) / abs(tree.rLoErr) ## my own approach hist.Fill(pull) ## individual plots for checking the fit quality c1 = TCanvas("c1", "Pulls", 600, 600) c1.cd() hist.GetXaxis().SetTitleSize(0.045) hist.GetYaxis().SetTitleSize(0.045) hist.GetYaxis().SetTitleOffset(1.1) hist.GetXaxis().SetTitleOffset(1.05) hist.GetXaxis().SetLimits(-6, 6.) hist.GetYaxis().SetLimits(0, 200.) hist.SetMinimum(0.) hist.SetMaximum(190.) c1.SetTopMargin(0.05) ##print "@ m= {}: \t mean = {}".format(m, hist.GetMean()) #pulls[signal_strength].SetPoint(pulls[signal_strength].GetN(), m, hist.GetMean()) ## get actual mean of histogram fit_func = TF1("gaussfit", "gaus", -3., 3.) hist.Fit(fit_func, "E") hist.Draw() drawCMS(-1, "Simulation Preliminary", year='run2') drawMass("m_{Z'} = " + str(m) + " GeV") c1.Print("plots/bias/run2c_masspoints/r" + signal_strength + "/bias_fit_" + str(m) + "_" + args.year + ".pdf") c1.Print("plots/bias/run2c_masspoints/r" + signal_strength + "/bias_fit_" + str(m) + "_" + args.year + ".png") n = pulls[signal_strength].GetN() pulls[signal_strength].SetPoint( n, m, fit_func.GetParameter(1)) ## get fitted gaussian mean pulls[signal_strength].SetPointError( n, 0., fit_func.GetParError(1)) ## set gaussian width as error fit_func.Delete() hist.Delete() c1.Delete() #except: # print "something went wrong in m =", m ## draw pulls outfile = TFile("plots/bias/bias_study_new_" + args.year + ".root", "RECREATE") c = TCanvas("canvas", "canvas", 800, 600) leg = TLegend(0.65, 0.7, 0.95, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(0) #1001 leg.SetFillColor(0) for i, signal_strength in enumerate(['0', '2sigma', '5sigma']): pulls[signal_strength].SetMarkerStyle(2) pulls[signal_strength].SetMarkerColor(COLORS[signal_strength]) pulls[signal_strength].SetLineColor(COLORS[signal_strength]) pulls[signal_strength].SetLineWidth(2) pulls[signal_strength].SetMinimum(-0.7) pulls[signal_strength].SetMaximum(0.7) pulls[signal_strength].Draw("APL" if i == 0 else "PL") leg.AddEntry(pulls[signal_strength], LEGEND[signal_strength]) zeroline = TGraph() zeroline.SetPoint(zeroline.GetN(), 1000, 0) zeroline.SetPoint(zeroline.GetN(), 8600, 0) zeroline.SetMarkerStyle(7) zeroline.SetMarkerSize(0) zeroline.SetLineStyle(15) zeroline.SetLineColor(1) zeroline.Draw("PL") c.SetGrid() pulls['0'].SetTitle(";m_{Z'} (GeV);mean #Deltar/#sigma_{r}") pulls['0'].GetXaxis().SetTitleSize(0.045) pulls['0'].GetYaxis().SetTitleSize(0.045) pulls['0'].GetYaxis().SetTitleOffset(1.1) pulls['0'].GetXaxis().SetTitleOffset(1.05) pulls['0'].GetXaxis().SetLimits(1350., 8150.) c.SetTopMargin(0.05) leg.Draw() drawCMS(-1, "Simulation Preliminary", year='run2') c.Print("plots/bias/bias_study_new_" + args.year + ".png") c.Print("plots/bias/bias_study_new_" + args.year + ".pdf") c.Write() outfile.Close()
Canv4Plots.Write() CanvScurves.Close() Canv4Plots.Close() thresh1D.Delete() thresh2D.Delete() TDAC1D.Delete() TDAC2D.Delete() correlation.Delete() sigma1D.Delete() sigma2D.Delete() chi2_1D.Delete() chi2_2D.Delete() eyeDiagram.Delete() CanvScurves.Delete() Canv4Plots.Delete() # ===================================================================== # ----------- Analysis and suggestions of TDAC for every pixels # ===================================================================== # Define function to set TDAC value and create a .txt file file_TDAC = open( "suggested_TDACfileTuningInterpolation402.txt", "w" ) # "r" instead of "w" in order to take the original suggested_TDACfile as an input instead of create TDACfile with all 6 file_TDAC_list = []
def main(): gStyle.SetOptStat(0) BIAS_DIR = global_paths.BIASDIR+args.btagging+"/" if args.year == 'run2c': BIAS_DIR += "combined_run2/" #BIAS_DIR += "combined_run2_signal{}/" ## individual plots stored in run2c_masspoints ## extract pulls pulls = TGraphErrors() for m in range(1600,8001,100): #try: pull0=int(PULL0[m]) #pull0=10. #tfile = TFile.Open(BIAS_DIR+"fitDiagnostics_M{mass}.root".format(mass=m), "READ") #tfile = TFile.Open(BIAS_DIR.format(pull0)+"fitDiagnostics_M{mass}.root".format(mass=m), "READ") #tree = tfile.Get("tree_fit_sb") tree = TChain("tree_fit_sb") for seed in ['123456', '234567', '345678', '456789', '567891', '678912', '789123', '891234', '912345', '123459']: tree.Add(BIAS_DIR+"fitDiagnostics_M{mass}_{seed}.root".format(mass=m, seed=seed)) ## the method proposed in the documemtation #hist = TH1D("hist", "hist", 20, -5, 5) #tree.Project("hist", "(r-1)/(0.5*(rHiErr+rLoErr))") #fit_func = TF1("gaussfit","gaus" , -5., 5.) #hist.Fit(fit_func, "E") #pulls.SetPoint(pulls.GetN(), m, fit_func.GetParameter(1)) ## get mean of gaussian fit ## Alberto's method #hist = TH1D("s_pulls", ";%s/#sigma_{r};Number of toys" % ("(r - "+str(pull0)+")"), 25, -5, +5) # hist = TH1D("s_pulls", ";%s/#sigma_{r};Number of toys" % ("#Deltar"), 25, -5, +5) # for i in range(tree.GetEntries()): if hist.GetEntries() >= 1000: continue tree.GetEntry(i) #print "r = {} (+{}, -{})".format(tree.r, tree.rHiErr, tree.rLoErr) ##if tree.rLoErr < 0.: continue if abs(tree.r+1.) < 0.001: continue if abs(tree.r-1.) < 0.001: continue if abs(tree.r-0.) < 0.001: continue #if abs(tree.rLoErr)>8.: continue # trying to skip these values FIXME if tree.rHiErr==0. or tree.rLoErr==0.: continue #print "r = {} (+{}, -{})".format(tree.r, tree.rHiErr, tree.rLoErr) #pull = (tree.r-pull0)/(0.5*(abs(tree.rHiErr)+abs(tree.rLoErr))) ## documentation approach #pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r-pull0 < 0. else (tree.r-pull0)/abs(tree.rLoErr) ## Alberto's sign convention pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r-pull0 > 0. else (tree.r-pull0)/abs(tree.rLoErr) ## my own approach #pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r < 0. else (tree.r-pull0)/abs(tree.rLoErr) ## Alberto's sign convention but depending directly on the sign of r #pull = (tree.r-pull0)/abs(tree.rHiErr) if tree.r > 0. else (tree.r-pull0)/abs(tree.rLoErr) ## my own approach with an rErr dependence on r, not r-1 hist.Fill(pull) ## individual plots for checking the fit quality c1 = TCanvas("c1", "Pulls", 600, 600) c1.cd() #c1.GetPad(0).SetTopMargin(0.06) #c1.GetPad(0).SetRightMargin(0.05) #c1.GetPad(0).SetBottomMargin(0.15) #c1.GetPad(0).SetTicks(1, 1) hist.GetXaxis().SetTitleSize(0.045) hist.GetYaxis().SetTitleSize(0.045) hist.GetYaxis().SetTitleOffset(1.1) hist.GetXaxis().SetTitleOffset(1.05) hist.GetXaxis().SetLimits(-5, 5.) hist.GetYaxis().SetLimits(0, 200.) hist.SetMinimum(0.) hist.SetMaximum(190.) c1.SetTopMargin(0.05) ##print "@ m= {}: \t mean = {}".format(m, hist.GetMean()) #pulls.SetPoint(pulls.GetN(), m, hist.GetMean()) ## get actual mean of histogram fit_func = TF1("gaussfit","gaus" , -3., 3.) ###fit_func.SetParameter(1, hist.GetMean()) #fit_func.SetParameter(1, 0.) ###fit_func.SetParLimits(1, -0.8, 0.8) #fit_func.SetParameter(2, 1.) ###fit_func.SetParameter(0, 45.) ###fit_func.SetParLimits(0, 30., 100.) hist.Fit(fit_func, "E") hist.Draw() drawCMS(-1, "Simulation Preliminary", year='run2') drawMass("m_{Z'} = "+str(m)+" GeV") c1.Print("plots/bias/run2c_masspoints/bias_fit_"+str(m)+"_"+args.year+".pdf") c1.Print("plots/bias/run2c_masspoints/bias_fit_"+str(m)+"_"+args.year+".png") n = pulls.GetN() pulls.SetPoint(n, m, fit_func.GetParameter(1)) ## get fitted gaussian mean pulls.SetPointError(n, 0., fit_func.GetParError(1)) ## set gaussian width as error hist.Delete() c1.Delete() #tfile.Close() #except: # print "something went wrong in m =", m ## draw pulls c = TCanvas("canvas", "canvas", 800, 600) pulls.SetTitle(";m_{Z'} (GeV);mean #Deltar/#sigma_{r}") pulls.SetMarkerStyle(2) pulls.SetMarkerColor(2) pulls.SetLineColor(2) pulls.SetLineWidth(2) #pulls.GetYaxis().SetNdivisions(1020) pulls.SetMinimum(-0.5) pulls.SetMaximum(0.5) pulls.Draw("APL") zeroline = TGraph() zeroline.SetPoint(zeroline.GetN(), 1000, 0) zeroline.SetPoint(zeroline.GetN(), 8600, 0) zeroline.SetMarkerStyle(7) zeroline.SetMarkerSize(0) zeroline.SetLineStyle(15) zeroline.SetLineColor(1) zeroline.Draw("PL") c.SetGrid() pulls.GetXaxis().SetTitleSize(0.045) pulls.GetYaxis().SetTitleSize(0.045) pulls.GetYaxis().SetTitleOffset(1.1) pulls.GetXaxis().SetTitleOffset(1.05) pulls.GetXaxis().SetLimits(1350., 8150.) c.SetTopMargin(0.05) drawCMS(-1, "Simulation Preliminary", year='run2') c.Print("plots/bias/bias_study_"+args.year+".png") c.Print("plots/bias/bias_study_"+args.year+".pdf")
c_mt1.cd() histo[0].Draw("") histo[0].SetLabelSize(0.07, "XY") histo[0].GetXaxis().SetTitleOffset(3.) histo[0].GetYaxis().SetTitleOffset(3.) c_mt1.SaveAs("./plots/" + listofalgorithms[xi] + "_m" + listofnames[i] + "_pseudoexp_mt.pdf") c_pull1.cd() histo[2].Draw("") histo[2].SetLabelSize(0.07, "XY") histo[2].GetXaxis().SetTitleOffset(3.) histo[2].GetYaxis().SetTitleOffset(3.) c_pull1.SaveAs("./plots/" + listofalgorithms[xi] + "_m" + listofnames[i] + "_pseudoexp_pull.pdf") c.Delete() c.Close() del histo del histo_t del histo_tup del c del c_mt1 del c_pull1 c2=TCanvas("total", "c2", 2000, 1000) c2.Divide(2,1,0.01,0.01) # fit graph1.Fit("pol1")