def draw_branch(name, hist_stats, cut=""): h_D = TH1D("D" + name, "#mu from D", *hist_stats) h_B = TH1D("B" + name, "#mu from B", *hist_stats) h_D.SetLineColor(2) h_D.SetLineWidth(2) h_B.SetLineWidth(2) trees = {"B": "B_tree", "D": "D_tree"} for tree in trees: trees[tree] = os.path.join(dir_name, trees[tree]) trees[tree] = root_file.Get(trees[tree]) trees[tree].Project(tree + name, name, cut) can = TCanvas("can", "can") can.cd() h_D.Draw() h_B.Draw("SAME") can.SetFillColor(10) can.SetLogy() legend = can.BuildLegend(0.7, 0.8, 0.9, 0.9) legend.SetFillColor(10) h_D.GetXaxis().SetTitle(x_axis_title) h_D.GetYaxis().SetTitle(y_axis_title) h_D.GetXaxis().SetDecimals() h_D.GetYaxis().SetDecimals() h_D.GetXaxis().SetTitleSize(size) h_D.GetYaxis().SetTitleSize(size) h_D.GetYaxis().SetTitleOffset(1.2) h_D.GetXaxis().SetLabelSize(size) h_D.GetYaxis().SetLabelSize(size) h_D.SetTitle(name) h_D.SetStats(0) can.SaveAs("BD" + name + ".gif")
def plot_calibrations(group_data, voltage, cal_pars_list, stacked=False): group_name = str(group_data[0].run_number) run = group_data[2] name = str(run.run_number) hists_and_lines = [] for cal_pars in cal_pars_list: pars = parameters.gain_pars[cal_pars] run.calibrate_energies(voltage, pars) hist = make_hist(name, run.calibrated_energies, 0, 3500) K_line = TLine(1460.820, 0, 1460.820, 1000000) K_line.SetLineColor(632) Tl_line = TLine(2614.511, 0, 2614.511, 1000000) Tl_line.SetLineColor(632) hists_and_lines.append([hist, K_line, Tl_line]) if stacked: print('Plotting stacked calibrated histograms.') run_canvas = TCanvas() run_canvas.SetLogy() colors = [808, 397, 436, 424, 628, 852, 800, 863, 403, 797] color_index = 0 hs = THStack('hs', 'Group %s Histograms' % group_name) for entry in hists_and_lines: title = str(run.run_number) entry[0].SetLineColor(colors[color_index]) color_index += 1 hs.Add(entry[0]) hs.Draw('nostack') K_line = TLine(1460.820, 0, 1460.820, 1000000) K_line.SetLineColor(632) K_line.Draw() Tl_line = TLine(2614.511, 0, 2614.511, 1000000) Tl_line.SetLineColor(632) Tl_line.Draw() run_canvas.BuildLegend() path = 'output/group_' + str(group_data[0].run_number) + '/' run_canvas.Print((path + str(group_data[0].run_number) + '_stacked_calibrated_histogram.pdf[')) run_canvas.Print((path + str(group_data[0].run_number) + '_stacked_calibrated_histogram.pdf')) run_canvas.Print((path + str(group_data[0].run_number) + '_stacked_calibrated_histogram.pdf]')) else: print('Plotting calibrated energy histogram.') run_canvas = TCanvas("run_canvas", "run canvas") run_canvas.Divide(3, int(len(cal_pars_list) / 2)) canvas_index = 1 for entry in hists_and_lines: pad = run_canvas.cd(canvas_index) pad.SetLogy() entry[0].Draw() entry[1].Draw() entry[2].Draw() pad.Update() canvas_index += 1 path = 'output/group_' + group_name + '/' run_canvas.Print((path + name + '_calibrated_histograms.pdf[')) run_canvas.Print((path + name + '_calibrated_histograms.pdf')) run_canvas.Print((path + name + '_calibrated_histograms.pdf]'))
def plotunc(typ, name, htruth, hcen, hdown, hup, lgh=''): global cana cana = TCanvas('cana') htruth.Draw() MIN = 1000000. MAX = 0. for b in range(1, htruth.GetNbinsX() + 1): bmin = min(htruth.GetBinContent(b), hcen.GetBinContent(b), hdown.GetBinContent(b), hup.GetBinContent(b)) bmax = max(htruth.GetBinContent(b), hcen.GetBinContent(b), hdown.GetBinContent(b), hup.GetBinContent(b)) MIN = min(bmin, MIN) MAX = max(bmax, MAX) htruth.GetYaxis().SetRangeUser(MIN * .8, MAX * 1.2) htruth.SetLineColor(ROOT.kGreen - 2) htruth.SetLineWidth(6) hcen.SetMarkerStyle(20) hcen.SetMarkerSize(2) hcen.SetLineColor(ROOT.kBlack) hcen.Draw('same') hup.Draw('samehist') hdown.Draw('samehist') lg = cana.BuildLegend(0.7, 0.7, 0.9, 0.94) lg.SetHeader(lgh) lg.SetBorderSize(0) lg.SetFillStyle(0) cana.SaveAs('PLOTS/' + typ + '_unfolded_' + name + '.pdf') cana.SaveAs('PLOTS/' + typ + '_unfolded_' + name + '.png')
def DrawNonResHistogram(h_, opt_, fn_, log_, N_, plotLabels_): c_tmp = TCanvas('c_tmp', 'c_tmp', 1300, 800) if (log_): gPad.SetLogy() # set bin labels # frame = c_tmp.DrawFrame(1.4,0.001, 4.1, 10) # frame.SetDirectory(0) # frame.GetXaxis().SetLimits(-0.5,N_-0.5) # frame.GetXaxis().CenterLabels(True) # for i in range(N_): # frame.GetXaxis().SetBinLabel(int(1000.*(2*i+1)/(2*N_)),plotLabels_[i]) # if(N_ > 3): frame.GetXaxis().SetLabelSize(0.07) # else: frame.GetXaxis().SetLabelSize(0.12) # frame.LabelsOption("h","X") # frame.Draw('axis') # print"frame:",frame h_.Draw(opt_) gPad.Update() mg_hist = h_.GetHistogram() # mg_hist.Rebin(N_) # print"nbins:",mg_hist.GetNbinsX() N_xbins = mg_hist.GetNbinsX() # N_rebin = int(N_xbins) / 4 N_rebin = int(N_xbins) / 100 # N_rebin = 25 mg_hist.Rebin(N_rebin) # mg_hist.SetBarOffset(100) # want 4 total # rebin means divide total by this. 4 = NXbins / Rebin -> Rebin = NXbins / 4 h_.Draw(opt_) # h_.GetXaxis().CenterLabels(True) # h_.SetBarOffset(-0.5) if (N_ > 3): h_.GetXaxis().SetLabelSize(0.05) else: h_.GetXaxis().SetLabelSize(0.12) for i in range(N_): h_.GetXaxis().LabelsOption("h") # h_.GetXaxis().LabelsOption("d") # bin = h_.GetXaxis().FindBin(i+1) bin = h_.GetXaxis().FindBin(i) #39 62 85 # print"bin:",bin # h_.GetXaxis().SetBinLabel(bin,plotLabels_[i]) h_.GetXaxis().SetBinLabel(bin, plotLabels_[i]) # print"frame:",frame # frame.Draw('axissame') # frame.Draw("sameaxis") # c_tmp.BuildLegend() # c_tmp.BuildLegend(0.65,0.52,0.95,0.82) # c_tmp.BuildLegend(0.75,0.62,0.95,0.84) # c_tmp.BuildLegend(0.75,0.42,0.95,0.65) c_tmp.BuildLegend(0.75, 0.57, 0.95, 0.8) # c_tmp.BuildLegend(0.83, 0.83, 0.99, 0.99) # c_tmp.BuildLegend(0.5, 0.3, 0.8, 0.8) # c_tmp.BuildLegend(0.6, 0.1, 0.9, 0.6) c_tmp.SaveAs(fn_)
class HowManyGraph: def __init__(self, trees, cutting_on, start, end, step, other_cuts, title, x_title, y_title): self.names = [name for name in trees] files = [open(name, 'w') for name in self.names] for cut, n1, n2 in HowManyEvents(trees, cutting_on, start, end, step, other_cuts, title, x_title, y_title): for n, file in zip((n1, n2), files): string = str(cut) + '\t\t' + str(n) + '\n' file.write(string) for file in files: file.close() self.can = TCanvas("graph", "graph") self.title = title self.x_title = x_title self.y_title = y_title self.gr_0 = TGraph(self.names[0]) self.gr_1 = TGraph(self.names[1]) self.set_graph_style() def set_graph_style(self): self.can.SetFillColor(10) self.gr_0.Draw("AP") self.gr_1.Draw("P") legend = self.can.BuildLegend(0.7, 0.8, 0.9, 0.9) legend.SetFillColor(10) legend.Paint() self.gr_0.SetMarkerStyle(21) self.gr_1.SetMarkerStyle(23) self.gr_1.SetMarkerColor(3) self.gr_0.SetMarkerColor(9) self.set_graph_title(*self.names) size = 0.04 self.gr_0.SetTitle(self.title) gStyle.SetTitleFillColor(0) self.gr_0.GetXaxis().SetTitle(self.x_title) self.gr_0.GetYaxis().SetTitle(self.y_title) self.gr_0.GetXaxis().SetDecimals() self.gr_0.GetYaxis().SetDecimals() self.gr_0.GetXaxis().SetTitleSize(size) self.gr_0.GetYaxis().SetTitleSize(size) self.gr_0.GetYaxis().SetTitleOffset(1.2) self.gr_0.GetXaxis().SetLabelSize(size) self.gr_0.GetYaxis().SetLabelSize(size) def set_graph_title(self, title_0, title_1): self.gr_0.SetTitle(title_0) self.gr_1.SetTitle(title_1) def SetLogy(self): self.can.SetLogy() def save_canvas(self, file_name): self.can.SaveAs(file_name)
def plot(Polarization): Rootfiles = [] for pol in Polarization: #print pol for plotstring in Plots: print plotstring Rootfiles.append( TFile(Outpath + "TwoPhotonThrust_" + pol + "._" + plotstring + ".root")) for plotstring in Plots: print plotstring canvas1 = TCanvas("c1", "c1") firsthist = True for i, rootfile in enumerate(Rootfiles): #print i exec('plot = rootfile.' + ptype + '_' + plotstring) if i + 1 >= 5: plot.SetLineColor(i + 2) else: plot.SetLineColor(i + 1) plot.SetLineWidth(2) plot.SetLineStyle(1) #if ptype == 'S': plot.SetLineStyle(1) #else: plot.SetLineStyle(2) plot.SetTitle(Pol[Polarization[i]]) plot.Draw("same") #plot.SetLogy() if firsthist: plot.SetStats(False) plot.SetXTitle("Thrust") plot.GetXaxis().CenterTitle() plot.SetYTitle("Events") plot.GetYaxis().CenterTitle() plot.GetXaxis().SetRangeUser(-3, 4) plot.GetYaxis().SetRangeUser(0, 10000) firsthist = False #STOP rootfiles loop gStyle.SetOptTitle(0) #turn off default title title = TPaveText(0.25, 0.9, 0.75, 1.0, "brNDC") title.AddText(Plotnames[plotstring] + ' Thrust Value') title.Draw("same") canvas1.SetGrid(1, 0) canvas1.BuildLegend() canvas1.SaveAs(Outpath + ptype + '_' + plotstring + '_' + 'aalowpt' + ".png") canvas1.SaveAs(Outpath + ptype + '_' + plotstring + '_' + 'aalowpt' + ".ps")
def study_cal_eff(): # Import data and MC files: f_data = TFile.Open("../trees_5gev_e/data_nn50.root", 'read') t_data = f_data.data f_mc = TFile.Open("../trees_5gev_e/lucas.root", 'read') t_mc = f_mc.lumical t_mc.AddFriend("lumical_noise=lumical", "../trees_5gev_e/lucas_geocuts_noise_cal.root") t_mc.AddFriend("lumical_eff=lumical", "../trees_5gev_e/lucas_geocuts_noise_cal_eff.root") c = TCanvas() hs = THStack("hs", "title") t_data.Draw("energy*(layer > 1)>>h_data50(200, 0, 50)", "energy*(layer > 1) > 0") h_data50 = gROOT.FindObject("h_data50") h_data50.SetTitle("Data") h_data50.SetLineWidth(2) h_data50.SetLineColor(1) hs.Add(h_data50, "histo") t_mc.Draw("cal_energy_new/0.0885>>h_mc(200, 0, 50)", "cal_energy_new/0.0885 > 0", "", 100000) h_mc = gROOT.FindObject("h_mc") h_mc.SetTitle("MC smearing and cal eff") h_mc.SetLineWidth(2) h_mc.SetLineColor(3) h_mc.Scale(t_data.GetEntries() / 100000) #t_mc.GetEntries()) hs.Add(h_mc, "histo") t_mc.Draw("cal_energy_smeared7/0.0885>>h_mc7(200, 0, 50)", "cal_energy_smeared7/0.0885 > 0", "", 100000) h_mc7 = gROOT.FindObject("h_mc7") h_mc7.SetTitle("MC smearing=0.7*#sigma") h_mc7.SetLineWidth(2) h_mc7.SetLineColor(2) h_mc7.Scale(t_data.GetEntries() / 100000) #t_mc.GetEntries()) hs.Add(h_mc7, "histo") print(h_mc7.GetEntries()) hs.Draw("nostack") hs.SetTitle( "Deposited energy in the pads of calorimeter;E, [MIP]; N_{hits}") c.SetGridx() c.SetGridy() c.BuildLegend() c.Update() input("wait")
def Draw_Histogram(h_,opt_,fn_,log_): c_tmp = TCanvas('c_tmp','c_tmp',1300,800) h_.Draw("apl") if(log_): gPad.SetLogy() h_.Draw(opt_) # c_tmp.BuildLegend(0.75,0.62,0.95,0.84) # c_tmp.BuildLegend(0.75,0.42,0.95,0.65) c_tmp.BuildLegend(0.75,0.57,0.95,0.8) # c_tmp.BuildLegend(0.83, 0.83, 0.99, 0.99) # c_tmp.BuildLegend(0.5, 0.3, 0.8, 0.8) # c_tmp.BuildLegend(0.6, 0.1, 0.9, 0.6) c_tmp.SaveAs(fn_) return
def study_trigger(): # Import data and MC files: f_data = TFile.Open("../trees_5gev_e/.root", 'read') t_data = f_data.data f_mc = TFile.Open("../trees_5gev_e/lucas.root", 'read') t_mc = f_mc.lumical t_mc.AddFriend("lumical_tr_clusters=lumical", "../trees_5gev_e/.root") c = TCanvas() t_data.Draw("tr1_n_clusters>>h_data(6, 0, 6)") h_data = gROOT.FindObject("h_data") h_data.SetTitle("Data") h_data.SetLineWidth(2) h_data.SetLineColor(1) t_mc.Draw("tr1_n_hits>>h_mc(6, 0, 6)") h_mc = gROOT.FindObject("h_mc") h_mc.SetTitle("MC") h_mc.SetLineWidth(2) h_mc.SetLineColor(2) h_mc.Scale(t_data.GetEntries() / t_mc.GetEntries()) t_mc.Draw("tr1_n_clusters>>h_mc2(6, 0, 6)", "n_triggers == 3") h_mc2 = gROOT.FindObject("h_mc2") h_mc2.SetTitle("MC w/ trigger") h_mc2.SetLineWidth(2) h_mc2.SetLineColor(4) h_mc2.Scale(t_data.GetEntries() / t_mc.GetEntries()) hs = THStack("hs", "title") hs.Add(h_data, "histo") hs.Add(h_mc, "histo") hs.Add(h_mc2, "histo") hs.Draw("nostack") hs.SetTitle( "Number of clusters in the 1st tracker;N_{particles}; N_{events}") c.SetGridx() c.SetGridy() c.BuildLegend() c.Update() input("wait")
def Draw_Histogram(h_, opt_, fn_, log_): c_tmp = TCanvas('c_tmp', 'c_tmp', 1300, 800) h_.Draw("apl") if (log_): #h_.SetMinimum(0.0001) print "histogram minimum:", h_.GetMinimum() h_.SetMinimum(1.) gPad.SetLogy() h_.SetMinimum(1.) print "histogram minumum after:", h_GetMinimum() h_.Draw(opt_) # c_tmp.BuildLegend(0.75,0.62,0.95,0.84) # c_tmp.BuildLegend(0.75,0.42,0.95,0.65) c_tmp.BuildLegend(0.75, 0.57, 0.95, 0.8) # c_tmp.BuildLegend(0.83, 0.83, 0.99, 0.99) # c_tmp.BuildLegend(0.5, 0.3, 0.8, 0.8) # c_tmp.BuildLegend(0.6, 0.1, 0.9, 0.6) c_tmp.SaveAs(fn_) return
#leg = TLegend() canvas = TCanvas() ymax = max(h1.GetMaximum(), h2.GetMaximum()) h1.SetMaximum(ymax * 1.2) h2.SetLineColor(kRed) gStyle.SetOptStat(0) #leg.AddEntry(h1, args.branch1+"("+args.cut1+")", "l") #leg.AddEntry(h2, args.branch2+"("+args.cut2+")", "l") h1.Draw(args.drawoption1) option2 = "same" if args.drawoption2 == "" else "same " + args.drawoption2 h2.Draw(option2) #leg.Draw("same") canvas.BuildLegend() if args.title is None: t = args.input.split("/")[-1] t = t.replace(".root", "") t = t.replace("mvaVars_", "") t = t.replace("ntuple_", "") h1.SetTitle(t) else: h1.SetTitle(args.title) canvas.SaveAs(args.output)
for histo in histos_chib2.values(): histo.SetLineColor(ROOT.kRed) histo.SetLineWidth(2) histo.SetMarkerColor(ROOT.kRed) histo.SetMarkerStyle(21) canvas=TCanvas("canvas","canvas") canvas.Print(outputFile_name+".ps[") #apre file .ps for key in histos_chib1.keys(): hs = THStack('hs', titles[key]) hs.Add(histos_chib1[key]) hs.Add(histos_chib2[key]) hs.Draw('nostack') leg = canvas.BuildLegend(.65,.93,.95,.7) leg.SetFillStyle(0) leg.SetBorderSize(0) leg.Draw("SAME") canvas.Update() canvas.Print(outputFile_name+'.ps') canvas.Print(key+'.png') <<<<<<< HEAD h2D_chib1.Draw('colz') canvas.Update() canvas.Print(outputFile_name+'.ps') h2D_chib2.Draw('colz') canvas.Update() canvas.Print(outputFile_name+'.ps') canvas.Print(outputFile_name+".ps]") #chiude file .ps =======
def makePlots(): inputFile = TFile('CutsPlots.root','READ') histos_data = dict() histos_MC = dict() keys = ['Y1S_nsigma', 'probFit1S', 'photon_pt', 'photon_eta', 'dimuon_pt', 'dimuon_rapidity', 'dz', 'invm1S'] for key in keys: histos_data[key] = inputFile.Get(key+'_data') histos_MC[key] = inputFile.Get(key+'_MC') titles = dict( Y1S_nsigma = 'Y1S_nsigma;Number of theoric standad deviation from #Upsilon(1S);', probFit1S = 'probFit1S;fit probability based on #chi^{2} over number dof;', photon_pt = 'photon p_{T};p_{T} [GeV];', photon_eta = 'photon_eta;#eta;', dimuon_pt = '#Upsilon p_{T};p_{T} [GeV];', dimuon_rapidity = '#Upsilon rapidity;y;', dz = '|dz|;|dz| [cm];', invm1S = 'Q-Value;m_{#mu#mu#gamma} - m_{#mu#mu} + m_{#Upsilon}^{PDG};' ) logY = dict( Y1S_nsigma = 0, probFit1S = 1, photon_pt = 0, photon_eta = 0, dimuon_pt = 0, dimuon_rapidity = 0, dz = 0, invm1S = 0 ) for histo in histos_data.values() + histos_MC.values(): if(histo.Integral() != 0): histo.Scale(1./histo.Integral()) for histo in histos_data.values(): histo.SetLineColor(ROOT.kGreen+2) histo.SetLineWidth(2) # histo.SetMarkerColor(ROOT.kGreen+2) # histo.SetMarkerStyle(21) for histo in histos_MC.values(): histo.SetLineColor(ROOT.kBlue) histo.SetLineWidth(2) # histo.SetMarkerColor(ROOT.kBlue) # histo.SetMarkerStyle(20) canvas=TCanvas("canvas","canvas") canvas.Print(outputFile_name+"[") #apre file .ps for key in keys: canvas.SetLogy(logY[key]) hs = THStack('hs', titles[key]) hs.Add(histos_data[key]) hs.Add(histos_MC[key]) hs.Draw('nostack') leg=canvas.BuildLegend(.8,.95,.92,.8); leg.SetFillColor(0); leg.Draw("SAME"); canvas.Update() canvas.Print(outputFile_name) canvas.Print('CutsPlots/'+key+'.png') canvas.Print(outputFile_name+"]") #chiude file .ps inputFile.Close()
h1.SetLineColor(ROOT.kBlue) h2.SetLineColor(ROOT.kRed) h1.SetTitle(file1Name.replace(".root","")) h2.SetTitle(file2Name.replace(".root","")) #h1.Draw() #h2.Draw("same") hs.Draw() T = TText() T.SetTextFont(42) T.SetTextAlign(21) T.DrawTextNDC(.5,.95,var) c1.BuildLegend(0.66, 0.87, 0.93, 0.77, "") c1.SetTitle(var) #c.cd() #Tl = ROOT.TLatex(0.5,0.9,var) #Tl.SetTextSize(0.04) #Tl.SetTextAlign(13) #Tl.Draw() pdfName=var+"_"+file1Name.replace(".root","")+"_vs_"+file2Name.replace(".root","") c1.SaveAs("plots/"+pdfName+".pdf") #/lstore/cms/cbeiraod/Stop4Body/puWeights #/lstore/cms/dbastos/Stop4Body/nTuples_v2018-04-03/ #puWeights_stop675.json.root #python plotStep0.py -f /lstore/cms/dbastos/Stop4Body/nTuples_v2018-04-03/puWeights_stop675.json.root -k /lstore/cms/cbeiraod/Stop4Body/puWeights/puWeights_stop675.json.root -p sample_T2DegStop_675_595_nTrueInt #python plotStep0.py -y -f puWeights_Wjets.json.root -p process_WJets_puWeight
gr2.GetYaxis().SetNdivisions(505) gr2.GetYaxis().SetLabelOffset(1.4) gr2.GetXaxis().SetTitleOffset(4.0) gr2.SetLineColor(3) gr2.SetLineWidth(5) gr2.SetMarkerStyle(21) gr3 = TGraph(n, MH3name, PS_M2_list) gr3.SetTitle("Medium2 (FJetCSV > 0.89)") gr3.GetYaxis().SetNdivisions(505) gr3.GetYaxis().SetLabelOffset(1.4) gr3.GetXaxis().SetTitleOffset(4.0) gr3.SetLineColor(4) gr3.SetLineWidth(5) gr3.SetMarkerStyle(22) mg = TMultiGraph("mg", "") mg.SetTitle("QCD Background; MH3 (GeV); Punzi Significance") mg.GetYaxis().SetNdivisions(505) mg.GetYaxis().SetLabelOffset(1.4) mg.GetXaxis().SetTitleOffset(4.0) mg.Add(gr1, "ALP") mg.Add(gr2, "ALP") mg.Add(gr3, "ALP") mg.Draw("ALP") c1.BuildLegend() c1.cd() c1.Update() c1.SaveAs("PS_test.pdf")
histCumul1_fast.Write() histCumul2_fast.Write() histCumul0_fast_hit.SetDrawOption("COL") histCumul1_fast_hit.SetDrawOption("COL") histCumul2_fast_hit.SetDrawOption("COL") histCumul0_fast_hit.Write() histCumul1_fast_hit.Write() histCumul2_fast_hit.Write() c1 = TCanvas('c1','Time Histograms',1400,1400) #c1.SetLogy() c1.SetGrid() hist1D0.Draw('hist') hist1D1.Draw('SameHist') hist1D2.Draw('SameHist') c1.BuildLegend() c1.SaveAs(sys.argv[2]+'timeHist_all_asics_0x%i.jpg'%(j)) c2 = TCanvas('c2','Number of Hits VS BL',1400,1400) c2.SetGrid() mg = TMultiGraph() mg.SetTitle('numberOfHits_all_asics_trim70x%i;BL;hits'%(j)) mg.Add(g0) mg.Add(g1) mg.Add(g2) mg.Draw("AL*") gPad.Modified() #mg.GetXaxis().SetLimits(0,40) #mg.SetMinimum(0.) #mg.SetMaximum(11.) leg = TLegend(.5,.1,.9,.2) leg.SetFillColor(0)
def makePlots(): # gROOT.SetStyle('Plain') # white background # gStyle.SetOptStat(222211) ROOT.gROOT.ProcessLine('.L ../tdrstyle.C') ROOT.gROOT.Reset() ROOT.gROOT.ProcessLine('setTDRStyle()') #Set CMS TDR style inputFile = TFile(output_name + '.root', 'READ') histos_SR1 = dict() histos_SR2 = dict() histos_LSB = dict() histos_RSB = dict() histos_MC = dict() keys = [ 'photon_pt', 'photon_eta', 'dimuon_pt', 'dimuon_rapidity', 'chib_pt' ] for key in keys: histos_SR1[key] = inputFile.Get(key + '_SR1') histos_SR2[key] = inputFile.Get(key + '_SR2') histos_LSB[key] = inputFile.Get(key + '_LSB') histos_RSB[key] = inputFile.Get(key + '_RSB') histos_MC[key] = inputFile.Get(key + '_MC') titles = dict( photon_pt='photon E_{T};E_{T}(#gamma) [GeV];Entries / 100 MeV', photon_eta='photon_eta;#eta(#gamma);Entries / 0.05', dimuon_pt='#Upsilon p_{T};p_{T}(#Upsilon) [GeV];Entries / GeV', dimuon_rapidity='#Upsilon rapidity(#Upsilon);y(#Upsilon);Entries / 0.1', chib_pt='#chi_{b} p_{T};p_{T}(#chi_{b}) [GeV];Entries / GeV', ) titles_chib = dict( photon_pt='photon E_{T};E_{T}(#gamma) [GeV];A.U.', photon_eta='photon_eta;#eta(#gamma);A.U.', dimuon_pt='#Upsilon p_{T};p_{T}(#Upsilon) [GeV];A.U.', dimuon_rapidity='#Upsilon rapidity(#Upsilon);y(#Upsilon);A.U.', chib_pt='#chi_{b} p_{T};p_{T}(#chi_{b}) [GeV];A.U.', ) logY = dict( photon_pt=0, photon_eta=0, dimuon_pt=0, dimuon_rapidity=0, chib_pt=0, ) maxY = dict( photon_pt=750, photon_eta=350, dimuon_pt=800, dimuon_rapidity=400, chib_pt=800, ) integralData1 = histos_SR1[keys[0]].Integral() integralData2 = histos_SR2[keys[0]].Integral() integralData = integralData1 + integralData2 for histo in histos_LSB.values() + histos_RSB.values() + histos_MC.values( ): if (histo.Integral() != 0): histo.Scale(1. / histo.Integral()) histo.Rebin(3) for histo in histos_SR1.values() + histos_SR2.values(): histo.Rebin(3) fBG = 0.48 #0.28 fLSB = 0.5 histos_BG = dict() for key in keys: histos_BG[key] = histos_RSB[key].Clone() histos_BG[key].SetNameTitle(key + '_BG', 'bgd from side-bands') histos_BG[key].Scale(1 - fLSB) histos_BG[key].Add(histos_LSB[key], fLSB) histos_Signal1 = dict() histos_Signal2 = dict() histos_Signal = dict() histos_SR = dict() for key in keys: histos_Signal1[key] = histos_SR1[key].Clone() histos_Signal1[key].SetNameTitle(key + '_signal1', 'Signal1') histos_Signal1[key].Add(histos_BG[key], -1 * integralData1 * fBG) histos_Signal2[key] = histos_SR2[key].Clone() histos_Signal2[key].SetNameTitle(key + '_signal2', 'Signal2') histos_Signal2[key].Add(histos_BG[key], -1 * integralData2 * fBG) histos_Signal[key] = histos_Signal1[key].Clone() histos_Signal[key].Add(histos_Signal2[key]) histos_Signal[key].SetNameTitle(key + '_signal', 'data bkg-subtracted') histos_SR[key] = histos_SR1[key].Clone() histos_SR[key].Add(histos_SR2[key]) histos_SR[key].SetNameTitle(key + '_SR', 'data') for histo in histos_Signal.values() + histos_BG.values(): if (histo.Integral() != 0): histo.Scale(1. / histo.Integral()) # Normalize SR to their original value, MC to be comparable with signal background-subtracted and background such that Signal+background = SR for histo in histos_MC.values(): histo.Scale(integralData * (1 - fBG)) for histo in histos_BG.values(): histo.Scale(integralData * fBG) for histo in histos_LSB.values(): histo.Scale(integralData * fBG * fLSB) for histo in histos_RSB.values(): histo.Scale(integralData * fBG * (1 - fLSB)) chi2Prob = dict() kolmogorovProb = dict() h_ratio = dict() for key in histos_Signal.keys(): h_ratio[key] = histos_SR[key].Clone() histo_sum_MC_BG = histos_MC[key].Clone() histo_sum_MC_BG.Add(histos_BG[key]) h_ratio[key].Divide(histo_sum_MC_BG) h_ratio[key].SetNameTitle(key + '_ratio', titles[key]) h_ratio[key].SetMarkerStyle(20) h_ratio[key].SetMarkerSize(1) h_ratio[key].GetYaxis().SetLabelSize(0.18) h_ratio[key].GetYaxis().SetTitle('DATA/MC') h_ratio[key].GetYaxis().SetTitleSize(0.22) h_ratio[key].GetYaxis().SetTitleOffset(0.2) h_ratio[key].GetYaxis().SetNdivisions(005) h_ratio[key].GetXaxis().SetLabelSize(0.16) h_ratio[key].GetXaxis().SetTitleSize(0.16) h_ratio[key].SetMaximum(1.5) h_ratio[key].SetMinimum(0.667) chi2Prob[key] = histos_SR[key].Chi2Test( histo_sum_MC_BG) #, 'UW')#, 'NORM') kolmogorovProb[key] = histos_SR[key].KolmogorovTest(histo_sum_MC_BG) for histo in histos_SR.values(): histo.SetLineColor(ROOT.kBlack) histo.SetLineWidth(2) histo.SetMarkerColor(ROOT.kBlack) histo.SetMarkerStyle(20) for histo in histos_SR1.values() + histos_Signal1.values(): histo.SetLineColor(ROOT.kBlue) histo.SetLineWidth(2) histo.SetMarkerColor(ROOT.kBlue) histo.SetMarkerStyle(22) for histo in histos_SR2.values() + histos_Signal2.values(): histo.SetLineColor(ROOT.kRed) histo.SetLineWidth(2) histo.SetMarkerColor(ROOT.kRed) histo.SetMarkerStyle(23) for histo in histos_Signal.values(): histo.SetLineColor(ROOT.kBlack) histo.SetLineWidth(2) # histo.SetMarkerColor(ROOT.kBlack) # histo.SetMarkerStyle(21) for histo in histos_BG.values(): histo.SetLineColor(ROOT.kRed) histo.SetLineWidth(2) histo.SetFillColor(ROOT.kRed) # histo.SetMarkerColor(ROOT.kRed) # histo.SetMarkerStyle(22) # for histo in histos_RSB.values(): # histo.SetLineColor(ROOT.kViolet) # histo.SetLineWidth(2) for histo in histos_MC.values(): histo.SetLineColor(ROOT.kBlue) histo.SetLineWidth(2) histo.SetFillColor(ROOT.kBlue) # histo.SetMarkerColor(ROOT.kBlue) # histo.SetMarkerStyle(20) outputFile = TFile(output_name + '_plots.root', 'RECREATE') for histo in histos_Signal.values() + histos_MC.values( ) + histos_BG.values() + histos_SR.values(): histo.Write() outputFile.Close() canvas = TCanvas("canvas", "canvas") canvas.Print(outputFile_name + "[") #apre file .ps for key in keys: pad1 = TPad("pad1", "The pad 80% of the height", 0.0, 0.2, 1.0, 1.0) pad1.cd() pad1.SetLogy(logY[key]) hs = THStack('hs', titles[key]) #hs.Add(histos_SR[key]) #hs.Add(histos_Signal[key]) hs.Add(histos_BG[key]) hs.Add(histos_MC[key]) #hs.Add(histos_LSB[key]) #hs.Add(histos_RSB[key]) #maxHisto = max(hs.GetMaximum(), histos_SR[key].GetMaximum()) hs.SetMaximum(maxY[key]) #maxHisto) hs.Draw('HIST') histos_SR[key].Draw('same') leg = pad1.BuildLegend(.65, .93, .95, .7) leg.SetFillStyle(0) leg.SetBorderSize(0) leg.Draw("SAME") pad2 = TPad("pad2", "The pad 20% of the height", 0.0, 0.01, 1.0, 0.2) pad2.cd() h_ratio[key].Draw() xmin = h_ratio[key].GetBinLowEdge(1) xmax = h_ratio[key].GetBinLowEdge( h_ratio[key].GetNbinsX()) + h_ratio[key].GetBinWidth( h_ratio[key].GetNbinsX()) line0 = TLine(xmin, 1, xmax, 1) line0.SetLineColor(ROOT.kBlue) line0.SetLineWidth(2) line0.Draw('same') canvas.cd() pad1.Draw() pad2.Draw() canvas.Update() canvas.Print(outputFile_name) canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '.png') canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '.pdf') canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '.root') # Make comparisison between chib1 and chib2 histos_SR1[key].Scale(1. / histos_SR1[key].Integral()) histos_SR2[key].Scale(1. / histos_SR2[key].Integral()) histos_Signal1[key].Scale(1. / histos_Signal1[key].Integral()) histos_Signal2[key].Scale(1. / histos_Signal2[key].Integral()) # histos_SR1[key].Rebin(3) # histos_SR2[key].Rebin(3) # histos_Signal1[key].Rebin(3) # histos_Signal2[key].Rebin(3) hsc = THStack('hsc', titles_chib[key]) hsc.Add(histos_Signal1[key]) hsc.Add(histos_Signal2[key]) #hsc.SetMaximum(maxY[key]) hsc.Draw('nostack') legc = canvas.BuildLegend(.65, .93, .95, .7) legc.SetFillStyle(0) legc.SetBorderSize(0) legc.Draw("SAME") print 'kolmogorov prob SR ' + key + ' ' + str( histos_SR1[key].KolmogorovTest(histos_SR2[key])) print 'kolmogorov prob Signal ' + key + ' ' + str( histos_Signal1[key].KolmogorovTest(histos_Signal2[key])) cfr = 2 if key not in ['dimuon_pt', 'chib_pt'] else 5 kolmogorovProb_chi = round( histos_Signal1[key].KolmogorovTest(histos_Signal2[key]), cfr) pvtxt = TPaveText(.2, .0, 0.5, 0.1, "NDC") pvtxt.SetFillStyle(0) pvtxt.SetBorderSize(0) pvtxt.SetTextSize(0.04) pvtxt.AddText('Kolmogorov prob = ' + str(kolmogorovProb_chi)) pvtxt.Draw() canvas.Update() canvas.Print(outputFile_name) canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '_comparison' + '.png') canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '_comparison' + '.pdf') canvas.Print('plots/' + key + '_' + mc_ptSpectrum + '_comparison' + '.root') canvas.Clear() canvas.Print(outputFile_name + "]") #chiude file .ps for key in keys: print '\n' print key + ' Kolmogorov probability: ' + str(chi2Prob[key]) print key + ' chiSqure probability: ' + str(kolmogorovProb[key]) #print 'Kolmogorov probability with sidebands: '+str(histos_MC['dimuon_pt'].KolmogorovTest(histos_LSB['dimuon_pt'])) # inputFile.Close()
fname.append('LG1_SE5') fname.append('LG1_SE5_2') fname.append('LG1_SE5_3') fname.append('LG1_SE5_4') fname.append('LG1_SE5_NM') fname.append('PIN1_SE5') fname.append('PIN1_SE5_NM') mg = TMultiGraph("mg", "") for iFile in fname: g = TGraph(iFile + ".csv", "%lf,%*lf,%lf") g.SetMarkerStyle(8) g.SetMarkerSize(0.8) mg.Add(g) print g, iFile c = TCanvas("c", "canvas", 800, 600) c.cd() gPad.SetLogy() mg.SetTitle(name + ";Bias Voltage [V];Measured Current [A]" ) # set the graph title, x-axis title and y-axis title mg.Draw("APL PMC PLC") # draw all the graphs using automatic coloring mg.GetHistogram().GetYaxis().SetRangeUser(1E-10, 0.001) # y-axis range #mg.GetHistogram().GetXaxis().SetRangeUser(0,150); # x-axis range c.BuildLegend(0.4, 0.8 - 0.05 * len(fname), 0.6, 0.8, "", "PL") # generate the legend and define its position and style c.Print(name + ".pdf")
h_fracdifference.SetFillColor(3) h_score.SetFillColor(4) h_maximum.SetFillColor(5) h_percentage.SetFillColor(6) h_average.SetFillColor(7) h_day.SetFillColor(8) h_players.SetFillColor(9) c_forgov = TCanvas("forgov", "Canvas for Gov. <3", 1000, 1000) h_fracdifference.GetXaxis().SetTitle("This is the x axis") h_fracdifference.GetYaxis().SetTitle("This is the y axis") h_fracdifference.Draw() text = TPaveText(0.1, 0.7, 0.3, 0.9) text.AddText("There is no z axis") text.Draw() c_forgov.BuildLegend() c_forgov.Print("canvasforgov.png") s_file = TFile("SporcleAnalysis.root", "RECREATE") h_difference.Write("Difference") h_fracdifference.Write("FractionalDifference") h_score.Write("Score") h_maximum.Write("Maximum") h_percentage.Write("Percentage") h_average.Write("Average") h_day.Write("Day") h_players.Write("Players") s_file.Close()
def study_smearing(): # Import data and MC files: f_data = TFile.Open("../trees_5gev_e/data_nn50.root", 'read') t_data = f_data.data t_data.AddFriend("data10=data", "../trees_5gev_e/data_nn10.root") t_data.AddFriend("data20=data", "../trees_5gev_e/data_nn20.root") t_data.AddFriend("data90=data", "../trees_5gev_e/data_nn90.root") t_data.AddFriend("data99=data", "../trees_5gev_e/data_nn99.root") f_mc = TFile.Open("../trees_5gev_e/lucas.root", 'read') t_mc = f_mc.lumical t_mc.AddFriend("lumical_noise=lumical", "../trees_5gev_e/lucas_geocuts_noise_test.root") c = TCanvas() hs = THStack("hs", "title") t_data.Draw("Sum$(data10.energy*(data10.layer == 0))>>h_data10(200, 0, 3)", "Sum$(data10.energy*(data10.layer == 0)) > 0") h_data10 = gROOT.FindObject("h_data10") h_data10.SetTitle("Data nn>0.1") h_data10.SetLineWidth(2) h_data10.SetLineColor(3) hs.Add(h_data10, "histo") t_data.Draw("Sum$(data20.energy*(data20.layer == 0))>>h_data20(200, 0, 3)", "Sum$(data20.energy*(data20.layer == 0)) > 0") h_data20 = gROOT.FindObject("h_data20") h_data20.SetTitle("Data nn>0.2") h_data20.SetLineWidth(2) h_data20.SetLineColor(4) hs.Add(h_data20, "histo") t_data.Draw("Sum$(energy*(layer == 0))>>h_data50(200, 0, 3)", "Sum$(energy*(layer == 0)) > 0") h_data50 = gROOT.FindObject("h_data50") h_data50.SetTitle("Data nn>0.5") h_data50.SetLineWidth(2) h_data50.SetLineColor(1) hs.Add(h_data50, "histo") t_data.Draw("Sum$(data90.energy*(data90.layer == 0))>>h_data90(200, 0, 3)", "Sum$(data90.energy*(data90.layer == 0)) > 0") h_data90 = gROOT.FindObject("h_data90") h_data90.SetTitle("Data nn>0.90") h_data90.SetLineWidth(2) h_data90.SetLineColor(5) hs.Add(h_data90, "histo") t_data.Draw("Sum$(data99.energy*(data99.layer == 0))>>h_data99(200, 0, 3)", "Sum$(data99.energy*(data99.layer == 0)) > 0") h_data99 = gROOT.FindObject("h_data99") h_data99.SetTitle("Data nn>0.99") h_data99.SetLineWidth(2) h_data99.SetLineColor(6) hs.Add(h_data99, "histo") # t_mc.Draw("Sum$(tr1_energy/0.0885)>>h_mc(200, 0, 3)", "Sum$(tr1_energy/0.0885) > 0") # h_mc = gROOT.FindObject("h_mc") # h_mc.SetTitle("MC no smearing") # h_mc.SetLineWidth(2) # h_mc.SetLineColor(2) # h_mc.Scale(t_data.GetEntries()/t_mc.GetEntries()) # hs.Add(h_mc, "histo") # t_mc.Draw("Sum$(tr1_energy_smeared5/0.0885)>>h_mc5(200, 0, 3)", "Sum$(tr1_energy_smeared5/0.0885) > 0") # h_mc5 = gROOT.FindObject("h_mc5") # h_mc5.SetLineWidth(2) # h_mc5.SetLineColor(3) # h_mc5.SetMarkerStyle(0) # h_mc5.Scale(t_data.GetEntries()/t_mc.GetEntries()) # hs.Add(h_mc5, "histo") # t_mc.Draw("Sum$(tr1_energy_smeared6/0.0885)>>h_mc6(200, 0, 3)", "Sum$(tr1_energy_smeared6/0.0885) > 0") # h_mc6 = gROOT.FindObject("h_mc6") # h_mc6.SetTitle("MC smearing #sigma=0.6*stdNoise") # h_mc6.SetLineWidth(2) # h_mc6.SetLineColor(2) # h_mc6.Scale(t_data.GetEntries()/t_mc.GetEntries()) # hs.Add(h_mc6, "histo") t_mc.Draw("Sum$(tr1_energy_smeared7/0.0885)>>h_mc7(200, 0, 3)", "Sum$(tr1_energy_smeared7/0.0885) > 0") h_mc7 = gROOT.FindObject("h_mc7") h_mc7.SetTitle("MC smearing #sigma=0.7*stdNoise") h_mc7.SetLineWidth(2) h_mc7.SetLineColor(2) h_mc7.Scale(t_data.GetEntries() / t_mc.GetEntries()) hs.Add(h_mc7, "histo") # t_mc.Draw("Sum$((tr1_energy_smeared10)/0.0885)>>h_mc10(200, 0, 3)", "Sum$((tr1_energy_smeared10)/0.0885) > 0") # h_mc10 = gROOT.FindObject("h_mc10") # h_mc10.SetTitle("MC smearing #sigma=stdNoise") # h_mc10.SetLineWidth(2) # h_mc10.SetLineColor(18) # h_mc10.Scale(t_data.GetEntries()/t_mc.GetEntries()) # hs.Add(h_mc10, "histo") hs.Draw("nostack") hs.SetTitle("Deposited energy in the 1st tracker;E, [MIP]; N_{events}") c.SetGridx() c.SetGridy() c.BuildLegend() c.Update() input("wait")
dir_name = "PatAnalyzerSkeleton" """open tree""" tree = "ass_muons" tree = os.path.join(dir_name, tree) tree = root_file.Get(tree) """Transverse momentum""" hist_stats = 100, 0, 50 B_pt_hist = TH1D("B_pt", "B", *hist_stats) D_pt_hist = TH1D("D_pt", "D", *hist_stats) B_pt_hist.SetStats(0) D_pt_hist.SetLineColor(2) tree.Project("B_pt", "pt", "abs(mother_pdgid) == 521") tree.Project("D_pt", "pt", "abs(mother_pdgid) == 411") [ set_histogram(x, """p_{t} #[]{GeV/c}""", """entries / 0.5 #[]{GeV/c}""") for x in [B_pt_hist, D_pt_hist] ] title = "p_{t} distribution" pt_can = TCanvas("p_{t} distribution", title) pt_can.cd() pt_can.SetLogy() B_pt_hist.Draw() D_pt_hist.Draw("SAME") legend = pt_can.BuildLegend(0.7, 0.8, 0.9, 0.9) legend.SetFillColor(10) B_pt_hist.SetTitle(title) raw_input()
class PainterScientist: def __init__(self, sci1, sci2, title, x_title, y_title): self.sci1, self.sci2 = sci1, sci2 self.can = TCanvas("graph", "graph") self.title = title self.x_title = x_title self.y_title = y_title self.build_graphs() def build_graphs(self): scientists = [self.sci1, self.sci2] names = ['tmp_global', 'tmp_inner'] for name, scientist in zip(names, scientists): with open(name, 'w') as file: for step, n_sigma, err in scientist: list = [step, n_sigma, 0, err] list = [str(x) for x in list] output_line = '\t'.join(list) output_line += '\n' file.write(output_line) unique_name = "can" + str(random.randint(0, 1000)) self.can = TCanvas(unique_name, "graph") self.gr_global = TGraphErrors(names[0]) self.gr_inner = TGraphErrors(names[1]) titles = ("global track", "inner track") self.set_graph_title(*titles) self.set_graph_style() def SetLogy(self): self.can.SetLogy() def set_graph_style(self): self.can.SetFillColor(10) self.gr_global.Draw("AP") self.gr_inner.Draw("P") legend = self.can.BuildLegend(0.7, 0.8, 0.9, 0.9) legend.SetFillColor(10) legend.Paint() self.gr_global.SetMarkerStyle(20) self.gr_inner.SetMarkerStyle(22) self.gr_inner.SetMarkerColor(2) self.gr_global.SetMarkerColor(4) size = 0.04 self.gr_global.SetTitle(self.title) gStyle.SetTitleFillColor(0) self.gr_global.GetXaxis().SetTitle(self.x_title) self.gr_global.GetYaxis().SetTitle(self.y_title) self.gr_global.GetXaxis().SetDecimals() self.gr_global.GetYaxis().SetDecimals() self.gr_global.GetXaxis().SetTitleSize(size) self.gr_global.GetYaxis().SetTitleSize(size) self.gr_global.GetYaxis().SetTitleOffset(1.2) self.gr_global.GetXaxis().SetLabelSize(size) self.gr_global.GetYaxis().SetLabelSize(size) def set_graph_title(self, title_global, title_inner): self.gr_global.SetTitle(title_global) self.gr_inner.SetTitle(title_inner) def save_canvas(self, file_name): self.can.SaveAs(file_name)
gr2.GetYaxis().SetLabelOffset(1.4) gr2.GetXaxis().SetTitleOffset(4.0) gr2.SetLineColor(2) gr2.SetLineWidth(5) gr2.SetMarkerStyle(21) gr2.SetMarkerColor(2) mg = TMultiGraph("mg", "") mg.SetTitle("Punzi Significance; MH3 (GeV); Punzi Significance") mg.GetYaxis().SetNdivisions(505) mg.GetYaxis().SetLabelOffset(1.4) mg.GetXaxis().SetTitleOffset(4.0) mg.Add(gr1, "ALP") mg.Add(gr2, "ALP") mg.Draw("ALP") c1.BuildLegend() c1.cd() c1.Update() c1.SaveAs("vetos_PS.pdf") #Draw Signal Efficiency c2 = TCanvas("c2", "c2", 900, 700) #width-height c2.SetLeftMargin(0.15) gPad.Modified() gr3 = TGraph(n, MH3name, effL) gr3.SetTitle("Loose") gr3.GetYaxis().SetNdivisions(505) gr3.GetYaxis().SetLabelOffset(1.4)
tree.Project("B_d_lin_inner", "d_lin_inner", "abs(mother_pdgid) == 521") tree.Project("D_d_lin_inner", "d_lin_inner", "abs(mother_pdgid) == 411") [set_histogram(x, """d_{lin} #[]{cm}""", """entries / 0.005 #[]{cm}""") for x in [B_d_lin_inner_hist, D_d_lin_inner_hist]] title = "d_{lin} distribution, inner track" d_lin_inner_can = TCanvas("distribution", title) d_lin_inner_can.cd() d_lin_inner_can.SetLogy() set_canvas(d_lin_inner_can) B_d_lin_inner_hist.Draw() D_d_lin_inner_hist.Draw("SAME") legend = d_lin_inner_can.BuildLegend(0.7, 0.8, 0.9, 0.9) legend.SetFillColor(10) B_d_lin_inner_hist.SetTitle(title) d_lin_inner_can.SaveAs("d_lin_inner.eps") """inner circ""" B_d_circ_inner_hist = TH1D("B_d_circ_inner", "B", *hist_stats) D_d_circ_inner_hist = TH1D("D_d_circ_inner", "D", *hist_stats) B_d_circ_inner_hist.SetStats(0) D_d_circ_inner_hist.SetLineColor(2) tree.Project("B_d_circ_inner", "d_circ_inner", "abs(mother_pdgid) == 521") tree.Project("D_d_circ_inner", "d_circ_inner", "abs(mother_pdgid) == 411") [set_histogram(x, """d_{circ} #[]{cm}""",
print(glob_lin_circ_hist.GetBinContent(0)) print(glob_lin_circ_hist.GetBinContent(glob_lin_circ_hist.GetNbinsX() + 1)) title = "distribution of d_{lin} - d_{circ}" lin_circ_can = TCanvas("lin_circ_can", title) lin_circ_can.cd() set_canvas(lin_circ_can) lin_circ_can.SetLogy() set_histogram(in_lin_circ_hist, """d_{lin} - d_{circ} #[]{cm}""", """entries / 1e-10 #[]{cm}""") set_histogram(glob_lin_circ_hist, "", "") in_lin_circ_hist.Draw() in_lin_circ_hist.SetStats(0) glob_lin_circ_hist.SetLineColor(2) glob_lin_circ_hist.Draw("SAME") legend = lin_circ_can.BuildLegend(0.7, 0.8, 0.9, 0.9) legend.SetFillColor(10) in_lin_circ_hist.SetTitle(title) in_mean = in_lin_circ_hist.GetMean() glob_mean = glob_lin_circ_hist.GetMean() glob_RMS = glob_lin_circ_hist.GetRMS() in_RMS = in_lin_circ_hist.GetRMS() print("global mean, RMS", glob_mean, glob_RMS) print("inner mean, RMS", in_mean, in_RMS) #lin_circ_can.SaveAs("lin_circ2.eps") #global_diff_can = TCanvas("glob_diff_can", "glob_diff_can") #x_axis = "fabs(d_lin_global - d_circ_global)" #y_axis = "sqrt(ref_x_global*ref_x_global + ref_y_global*ref_y_global)" #global_diff_hist = TH2D("glob_diff", "glob_diff", 2000, 0, 1e-4, 1000, 0, # 500)
canvas.Print(outputFile_name+"[") #apre file .ps for key in keys: h_chib1[key].SetLineColor(ROOT.kBlue) h_chib1[key].SetMarkerColor(ROOT.kBlue) h_chib1[key].SetMarkerStyle(22) h_chib2[key].SetLineColor(ROOT.kRed) h_chib2[key].SetMarkerColor(ROOT.kRed) h_chib2[key].SetMarkerStyle(23) hs = THStack('hs', key+axisLabels[key]) hs.Add(h_chib1[key]) hs.Add(h_chib2[key]) hs.SetMinimum(estremiGrafico[key][0]) hs.SetMaximum(estremiGrafico[key][-1]) hs.Draw('nostack') leg=canvas.BuildLegend(.8,.95,.92,.8) line1 = TLine(ptBins[0],getattr(Chib1_params_all, key),ptBins[-1],getattr(Chib1_params_all, key)) line2 = TLine(ptBins[0],getattr(Chib2_params_all, key),ptBins[-1],getattr(Chib2_params_all, key)) line1.SetLineColor(ROOT.kBlue) line2.SetLineColor(ROOT.kRed) line1.SetLineStyle(2) line2.SetLineStyle(2) print line1.GetTitle() line1.Draw('same') line2.Draw('same') leg.SetFillColor(0) leg.Draw("SAME") canvas.Update() canvas.Print(outputFile_name) canvas.Print('plots/'+key+'.pdf') canvas.Print('plots/'+key+'.root')
c = TCanvas() c.SetLogy() h1.SetXTitle("P_{T}(#phi)") h1.SetYTitle("#events") h1.Scale(1 / h1.Integral()) h1.SetXTitle("P_{T}") h1.SetYTitle("#events") h1.SetLineColor(2) h1.Draw() h2.Scale(1 / h2.Integral()) h2.SetLineColor(3) h2.Draw("same") h3.Scale(1 / h3.Integral()) h3.SetLineColor(4) h3.Draw("same") h4.Scale(1 / h4.Integral()) h4.SetLineColor(6) h4.Draw("same") h5.Scale(1 / h5.Integral()) h5.SetLineColor(1) h5.Draw("same") c.BuildLegend(0.2, 0.7, 0.48, 0.9, "M_{#chi}=1") c.SaveAs("pT(phi).png")
class KolmogorovResults: """compare d_global with d_inner""" def __init__(self, trees, cutting_on, start, end, step, hist_stats, other_cuts, title, x_title, y_title): self.kd_global = KolmogorovDistribution(trees, "d_lin_global", cutting_on, start, end, step, hist_stats, other_cuts) self.kd_inner = KolmogorovDistribution(trees, "d_lin_inner", cutting_on, start, end, step, hist_stats, other_cuts) self.can = TCanvas("graph", "graph") self.title = title self.x_title = x_title self.y_title = y_title self.build_graphs() def build_graphs(self): kds = [self.kd_global, self.kd_inner] names = ['tmp_global', 'tmp_inner'] for name, kd in zip(names, kds): with open(name, 'w') as file: for step, n_sigma in kd: output_line = '\t'.join([str(x) for x in [step, n_sigma]]) output_line += '\n' file.write(output_line) unique_name = "can" + str(random.randint(0, 1000)) self.can = TCanvas(unique_name, "graph") self.gr_global = TGraph(names[0]) self.gr_inner = TGraph(names[1]) titles = ("global track", "inner track") self.set_graph_title(*titles) self.set_graph_style() def SetLogy(self): self.can.SetLogy() def set_graph_style(self): self.can.SetFillColor(10) self.gr_global.Draw("AP") self.gr_inner.Draw("P") legend = self.can.BuildLegend(0.7, 0.8, 0.9, 0.9) legend.SetFillColor(10) legend.Paint() self.gr_global.SetMarkerStyle(20) self.gr_inner.SetMarkerStyle(22) self.gr_inner.SetMarkerColor(2) self.gr_global.SetMarkerColor(4) size = 0.04 self.gr_global.SetTitle(self.title) gStyle.SetTitleFillColor(0) self.gr_global.GetXaxis().SetTitle(self.x_title) self.gr_global.GetYaxis().SetTitle(self.y_title) self.gr_global.GetXaxis().SetDecimals() self.gr_global.GetYaxis().SetDecimals() self.gr_global.GetXaxis().SetTitleSize(size) self.gr_global.GetYaxis().SetTitleSize(size) self.gr_global.GetYaxis().SetTitleOffset(1.2) self.gr_global.GetXaxis().SetLabelSize(size) self.gr_global.GetYaxis().SetLabelSize(size) def set_graph_title(self, title_global, title_inner): self.gr_global.SetTitle(title_global) self.gr_inner.SetTitle(title_inner) def save_canvas(self, file_name): self.can.SaveAs(file_name)
f1 = TFile.Open('basic_kin_m300.root') h1 = f1.Get(s + '_nmssm') h1.SetXTitle("p_{T}(b) [GeV]") h1.SetYTitle("Events") h1.SetTitle('NMSSM') h1.SetLineColor(600) h1.Draw('hist') c.Update() h2 = f1.Get(s + '_radion') h2.SetXTitle("p_{T}(b) [GeV]") h2.SetYTitle("Event") h2.SetTitle('NMSSM') h2.SetLineColor(418) h2.Draw('hist&same') c.Update() h3 = f1.Get(s + '_graviton') h3.SetXTitle("p_{T}(b) [GeV]") h3.SetYTitle("Events") h3.SetTitle('graviton') h3.SetLineColor(625) h3.Draw('hist&same') c.Update() l = c.BuildLegend(0.75, 0.4, 0.9, 0.5) c.Update() f = TFile.Open('plots.root', 'UPDATE') c.Write()
def plot_hist(group_data, stacked=False, fitted=False): group_name = str(group_data[0].run_number) group_size = len(group_data) if stacked: print('Plotting stacked histogram.') run_canvas = TCanvas() run_canvas.SetLogy() gStyle.SetPalette(87) if group_name == '2946': TColor.InvertPalette() hs = THStack('hs', 'Group %s Histograms' % group_name) for run in group_data: tab = ' ' title = str(run.run_number) + tab + 'Average temperature: ' + str( round(run.temperature_average, 2)) hist = make_hist(title, run.energies, 0, 270000) hs.Add(hist) hs.Draw('nostack PLC') run_canvas.BuildLegend() path = 'output/group_' + str(group_data[0].run_number) + '/' run_canvas.Print( (path + str(group_data[0].run_number) + '_stacked_histogram.pdf[')) run_canvas.Print( (path + str(group_data[0].run_number) + '_stacked_histogram.pdf')) run_canvas.Print( (path + str(group_data[0].run_number) + '_stacked_histogram.pdf]')) else: hists_and_fits = [] hist_range = [20000, 60000, 150000, 350000, 550000] for i in range(len(group_data)): run = group_data[i] hist = make_hist(str(run.run_number), run.energies, 0, hist_range[i]) if fitted: K_key = 'K_' + str(run.run_number) K_pars = parameters.fit_pars[K_key] K_fit = find_peak_fit('K', K_pars[0], K_pars[1], K_pars[2], K_pars[3], K_pars[4], K_pars[5], K_pars[6]) Tl_key = 'Tl_' + str(run.run_number) Tl_pars = parameters.fit_pars[Tl_key] Tl_fit = find_peak_fit('Tl', Tl_pars[0], Tl_pars[1], Tl_pars[2], Tl_pars[3], Tl_pars[4], Tl_pars[5], Tl_pars[6]) hists_and_fits.append([hist, K_fit, Tl_fit]) write_run_output(group_name, run, K_fit, Tl_fit) else: hists_and_fits.append([hist]) run_canvas = TCanvas("run_canvas", "run canvas") run_canvas.Divide(3, int(group_size / 2)) file_name = '' canvas_index = 1 for entry in hists_and_fits: pad = run_canvas.cd(canvas_index) pad.SetLogy() run = entry[0] if len(entry) == 3: print('Plotting fitted histogram.') file_name = '_fitted_histograms.pdf' fit1 = entry[1] fit2 = entry[2] run.Fit(fit1, 'LR') run.Fit(fit2, 'LR+') else: print('Plotting histogram.') file_name = '_histograms.pdf' run.Draw() canvas_index += 1 path = 'output/group_' + group_name + '/' run_canvas.Print((path + group_name + file_name + '[')) run_canvas.Print((path + group_name + file_name)) run_canvas.Print((path + group_name + file_name + ']'))