def run_unfolder(itoy = 0, outdir = opts.dir, tau = opts.tau): styles = { 'scan_overlay' : { 'markerstyle':[0, 29], 'linecolor':[1,1], 'markercolor':[1,2], 'drawstyle':['ALP', 'P'], 'markersize':[0,3] }, 'data_overlay' : { 'linestyle' : [1,0], 'markerstyle':[0,21], 'linecolor' : [2,1], 'markercolor':[2,1], 'drawstyle' : ['hist', 'p'], 'legendstyle' : ['l', 'p'] }, 'dots' : { 'markerstyle' : 20, 'markersize' : 2, 'linestyle' : 0, 'drawstyle' : 'P' }, 'line' : { 'linestyle':1, 'markerstyle':0 }, } plotter = BasePlotter( outdir, defaults = { 'clone' : False, 'show_title' : True, } ) #canvas = plotting.Canvas(name='adsf', title='asdf') if "toy" in opts.fit_file: data_file_basedir = 'toy_' + str(itoy) data_file_dir = data_file_basedir + '/' + opts.var else: data_file_dir = opts.var xaxislabel = set_pretty_label(opts.var) scale = 1. if opts.no_area_constraint: area_constraint='None' else: area_constraint='Area' myunfolding = URUnfolding(regmode = opts.reg_mode, constraint = area_constraint) ## Migration matrix preprocessing ## remove oflow bins var_dir = getattr(resp_file, opts.var) migration_matrix = var_dir.migration_matrix for bin in migration_matrix: if bin.overflow: bin.value = 0 bin.error = 0 myunfolding.matrix = migration_matrix thruth_unscaled = var_dir.thruth_unscaled reco_unscaled = var_dir.reco_unscaled project_reco = 'X' if myunfolding.orientation == 'Vertical' else 'Y' project_gen = 'Y' if myunfolding.orientation == 'Vertical' else 'X' reco_project = rootpy.asrootpy( getattr(migration_matrix, 'Projection%s' % project_reco)() ) gen_project = rootpy.asrootpy( getattr(migration_matrix, 'Projection%s' % project_gen)() ) if gen_project.Integral() < thruth_unscaled.Integral(): eff_correction = ROOT.TGraphAsymmErrors(gen_project, thruth_unscaled) elif gen_project.Integral() == thruth_unscaled.Integral(): eff_correction = None else: log.warning( 'Efficiency correction: The visible part of the migration matrix' ' has a larger integral than the full one! (%.3f vs. %.3f).\n' 'It might be a rounding error, but please check!'\ % (reco_project.Integral(), reco_unscaled.Integral()) ) eff_correction = None if reco_project.Integral() < reco_unscaled.Integral(): purity_correction = ROOT.TGraphAsymmErrors(reco_project, reco_unscaled) elif reco_project.Integral() == reco_unscaled.Integral(): purity_correction = None else: log.warning( 'Purity correction: The visible part of the migration matrix' ' has a larger integral than the full one! (%.3f vs. %.3f).\n' 'It might be a rounding error, but please check!'\ % (reco_project.Integral(), reco_unscaled.Integral()) ) purity_correction = None #flush graphs into histograms (easier to handle) eff_hist = gen_project.Clone() eff_hist.reset() eff_hist.name = 'eff_hist' if eff_correction: for idx in range(eff_correction.GetN()): eff_hist[idx+1].value = eff_correction.GetY()[idx] eff_hist[idx+1].error = max( eff_correction.GetEYhigh()[idx], eff_correction.GetEYlow()[idx] ) else: for b in eff_hist: b.value = 1. b.error = 0. purity_hist = reco_project.Clone() purity_hist.reset() purity_hist.name = 'purity_hist' if purity_correction: for idx in range(purity_correction.GetN()): bin.value = purity_correction.GetY()[idx] bin.error = max( purity_correction.GetEYhigh()[idx], purity_correction.GetEYlow()[idx] ) else: for bin in purity_hist: bin.value = 1. bin.error = 0. #Get measured histogram measured = None if opts.use_reco_truth: log.warning("Using the MC reco distribution for the unfolding!") measured = getattr(resp_file, opts.var).reco_distribution else: measured = getattr(data_file, data_file_dir).tt_right measured_no_correction = measured.Clone() measured_no_correction.name = 'measured_no_correction' measured.name = 'measured' measured.multiply(purity_hist) myunfolding.measured = measured #get gen-level distribution gen_distro = getattr(resp_file, opts.var).true_distribution.Clone() full_true = gen_distro.Clone() full_true.name = 'complete_true_distro' gen_distro.multiply(eff_hist) gen_distro.name = 'true_distribution' myunfolding.truth = gen_distro if opts.cov_matrix != 'none': if 'toy' in opts.fit_file: input_cov_matrix = make_cov_matrix( getattr(data_file, data_file_basedir).correlation_matrix, getattr(data_file, data_file_dir).tt_right ) input_corr_matrix = make_corr_matrix( getattr(data_file, data_file_basedir).correlation_matrix, getattr(data_file, data_file_dir).tt_right ) else: input_cov_matrix = make_cov_matrix( data_file.correlation_matrix, getattr(data_file, data_file_dir).tt_right ) input_corr_matrix = make_corr_matrix( data_file.correlation_matrix, getattr(data_file, data_file_dir).tt_right ) input_cov_matrix.name = 'input_cov_matrix' input_corr_matrix.name = 'input_corr_matrix' myunfolding.cov_matrix = input_cov_matrix myunfolding.InitUnfolder() hdata = myunfolding.measured # Duplicate. Remove! #plot covariance matrix plotter.pad.cd() input_corr_matrix.SetStats(False) input_corr_matrix.Draw('colz') plotter.pad.SetLogz(True) plotter.save('correlation_matrix.png') #optimize best_taus = {} if tau >= 0: best_taus['External'] = tau else: t_min, t_max = eval(opts.tau_range) best_l, l_curve, graph_x, graph_y = myunfolding.DoScanLcurve(100, t_min, t_max) best_taus['L_curve'] = best_l l_curve.SetName('lcurve') l_curve.name = 'lcurve' graph_x.name = 'l_scan_x' graph_y.name = 'l_scan_y' l_tau = math.log10(best_l) points = [(graph_x.GetX()[i], graph_x.GetY()[i], graph_y.GetY()[i]) for i in xrange(graph_x.GetN())] best = [(x,y) for i, x, y in points if l_tau == i] graph_best = plotting.Graph(1) graph_best.SetPoint(0, *best[0]) plotter.reset() plotter.overlay( [l_curve, graph_best], **styles['scan_overlay'] ) plotter.canvas.name = 'L_curve' info = plotter.make_text_box('#tau = %.5f' % best_l, 'NE') #ROOT.TPaveText(0.65,1-canvas.GetTopMargin(),1-canvas.GetRightMargin(),0.999, "brNDC") info.Draw() canvas.Update() plotter.set_subdir('L_curve') plotter.save() modes = ['RhoMax', 'RhoSquareAvg', 'RhoAvg'] for mode in modes: plotter.set_subdir(mode) best_tau, tau_curve, index_best = myunfolding.DoScanTau(100, t_min, t_max, mode) best_taus[mode] = best_tau tau_curve.SetName('%s_scan' % mode) tau_curve.SetMarkerStyle(1) points = [(tau_curve.GetX()[i], tau_curve.GetY()[i]) for i in xrange(tau_curve.GetN())] best = [points[index_best]] graph_best = plotting.Graph(1) graph_best.SetPoint(0, *best[0]) plotter.overlay( [tau_curve, graph_best], **styles['scan_overlay'] ) plotter.canvas.name = 'c'+tau_curve.GetName() info = plotter.make_text_box('#tau = %.5f' % best_tau, 'NE') #ROOT.TPaveText(0.65,1-canvas.GetTopMargin(),1-canvas.GetRightMargin(),0.999, "brNDC") info.Draw() plotter.save('Tau_curve') #force running without regularization best_taus['NoReg'] = 0 for name, best_tau in best_taus.iteritems(): log.info('best tau option for %s: %.3f' % (name, best_tau)) if opts.runHandmade: #hand-made tau scan plotter.set_subdir('Handmade') unc_scan, bias_scan = myunfolding.scan_tau( 200, 10**-6, 50, os.path.join(outdir, 'Handmade', 'scan_info.root')) bias_scan.name = 'Handmade' bias_scan.title = 'Avg. Bias - Handmade' plotter.plot(bias_scan, logx=True, logy=True, **styles['dots']) plotter.save('bias_scan') unc_scan.name = 'Handmade' unc_scan.title = 'Avg. Unc. - Handmade' plotter.plot(unc_scan, logx=True, logy=True, **styles['dots']) plotter.save('unc_scan') bias_points = [(bias_scan.GetX()[i], bias_scan.GetY()[i]) for i in xrange(bias_scan.GetN())] unc_points = [(unc_scan.GetX()[i], unc_scan.GetY()[i]) for i in xrange(unc_scan.GetN())] fom_scan = plotting.Graph(unc_scan.GetN()) for idx, info in enumerate(zip(bias_points, unc_points)): binfo, uinfo = info tau, bias = binfo _, unc = uinfo fom_scan.SetPoint(idx, tau, quad(bias, unc)) fom_scan.name = 'Handmade' fom_scan.title = 'Figure of merit - Handmade' plotter.plot(fom_scan, logx=True, logy=True, **styles['dots']) plotter.save('fom_scan') to_save = [] outfile = rootpy.io.root_open(os.path.join(outdir, opts.out),'recreate') for name, best_tau in best_taus.iteritems(): plotter.set_subdir(name) method_dir = outfile.mkdir(name) myunfolding.tau = best_tau hdata_unfolded = myunfolding.unfolded #apply phase space efficiency corrections hdata_unfolded_ps_corrected = hdata_unfolded.Clone() hdata_unfolded_ps_corrected.Divide(eff_hist) hdata_refolded = myunfolding.refolded #apply purity corrections hdata_refolded_wpurity = hdata_refolded.Clone() error_matrix = myunfolding.ematrix_total hcorrelations = myunfolding.rhoI_total hbias = myunfolding.bias #canvas = overlay(myunfolding.truth, hdata_unfolded) myunfolding.truth.xaxis.title = xaxislabel hdata_unfolded.xaxis.title = xaxislabel n_neg_bins = 0 for ibin in range(1,hdata_unfolded.GetNbinsX()+1): if hdata_unfolded.GetBinContent(ibin) < 0: n_neg_bins = n_neg_bins + 1 hn_neg_bins = plotting.Hist( 2,-1, 1, name = 'nneg_bins', title = 'Negative bins in ' + hdata_unfolded.GetName()+ ';Bin sign; N_{bins}' ) hn_neg_bins.SetBinContent(1,n_neg_bins) hn_neg_bins.SetBinContent(2,hdata_unfolded.GetNbinsX()-n_neg_bins) plotter.plot( hn_neg_bins, writeTo='unfolding_bins_sign', **styles['line'] ) leg = LegendDefinition( title=name, labels=['Truth','Unfolded'], position='ne' ) sumofpulls = 0 sumofratios = 0 for ibin in range(1,myunfolding.truth.GetNbinsX()+1): binContent1 = myunfolding.truth.GetBinContent(ibin) binContent2 = hdata_unfolded.GetBinContent(ibin) binError1 = myunfolding.truth.GetBinError(ibin) binError2 = hdata_unfolded.GetBinError(ibin) error = sqrt(binError1*binError1 + binError2*binError2) if error != 0: pull = (binContent2-binContent1)/error else: pull = 9999 if binContent1 != 0: ratio = binContent2/binContent1 sumofpulls = sumofpulls + pull sumofratios = sumofratios + ratio sumofpulls = sumofpulls / myunfolding.truth.GetNbinsX() sumofratios = sumofratios / myunfolding.truth.GetNbinsX() hsum_of_pulls = plotting.Hist( 1, 0, 1, name = 'sum_of_pulls_' + hdata_unfolded.GetName(), title = 'Sum of pulls wrt truth for ' + hdata_unfolded.GetName()+ ';None; #Sigma(pulls) / N_{bins}' ) hsum_of_pulls[1].value = sumofpulls plotter.plot(hsum_of_pulls, writeTo='unfolding_sum_of_pulls', **styles['line']) hsum_of_ratios = plotting.Hist( 1, 0, 1, name = 'sum_of_ratios_' + hdata_unfolded.GetName(), title = 'Sum of ratios wrt truth for ' + hdata_unfolded.GetName()+ ';None; #Sigma(ratios) / N_{bins}' ) hsum_of_ratios[1].value = sumofratios plotter.plot(hsum_of_ratios, writeTo='unfolding_sum_of_ratios', **styles['line']) plotter.overlay_and_compare( [myunfolding.truth], hdata_unfolded, legend_def=leg, writeTo='unfolding_pull', **styles['data_overlay'] ) plotter.overlay_and_compare( [myunfolding.truth], hdata_unfolded, legend_def=leg, method='ratio', writeTo='unfolding_ratio', **styles['data_overlay'] ) plotter.overlay_and_compare( [full_true], hdata_unfolded_ps_corrected, legend_def=leg, writeTo='unfolding_pull', **styles['data_overlay'] ) plotter.overlay_and_compare( [full_true], hdata_unfolded_ps_corrected, legend_def=leg, method='ratio', writeTo='unfolding_ratio', **styles['data_overlay'] ) nbins = myunfolding.measured.GetNbinsX() input_distro = getattr(resp_file, opts.var).prefit_distribution leg = LegendDefinition(title=name, position='ne') myunfolding.measured.xaxis.title = xaxislabel hdata_refolded.xaxis.title = xaxislabel myunfolding.measured.drawstyle = 'e1' style = {'linestyle':[1, 0], 'markerstyle':[20, 20], 'markercolor':[2,4], 'linecolor':[2,4], 'drawstyle' : ['hist', 'e1'], 'legendstyle' : ['l', 'p'], 'title' : ['Refolded', 'Reco'] } plotter.overlay_and_compare( [hdata_refolded], myunfolding.measured, legend_def=leg, writeTo='refolded_pull', **style ) plotter.overlay_and_compare( [hdata_refolded], myunfolding.measured, legend_def=leg, method='ratio', writeTo='refolded_ratio', **style ) style = {'linestyle':[1,0,0], 'markerstyle':[20,21,21], 'markercolor':[2,4,1], 'linecolor':[2,4,1], 'drawstyle' : ['hist', 'e1', 'e1'], 'legendstyle' : ['l', 'p', 'p'], 'title' : ['Refolded', 'Reco', 'Input'] } measured_no_correction.drawstyle = 'e1' plotter.overlay_and_compare( [hdata_refolded_wpurity, measured_no_correction], input_distro, legend_def=leg, writeTo='refolded_wpurity_pull', **style ) plotter.overlay_and_compare( [hdata_refolded_wpurity, measured_no_correction], input_distro, legend_def=leg, method='ratio', writeTo='refolded_wpurity_ratio', **style ) method_dir.WriteTObject(hdata_unfolded, 'hdata_unfolded') method_dir.WriteTObject(hdata_unfolded_ps_corrected, 'hdata_unfolded_ps_corrected') method_dir.WriteTObject(hdata_refolded, 'hdata_refolded') method_dir.WriteTObject(hdata_refolded_wpurity, 'hdata_refolded_wpurity') method_dir.WriteTObject(error_matrix, 'error_matrix') method_dir.WriteTObject(hbias, 'bias') method_dir.WriteTObject(hn_neg_bins, 'hn_neg_bins') method_dir.WriteTObject(hsum_of_pulls, 'hsum_of_pulls') method_dir.WriteTObject(hsum_of_ratios, 'hsum_of_ratios') htruth = myunfolding.truth hmatrix = myunfolding.matrix hmeasured = myunfolding.measured #with rootpy.io.root_open(os.path.join(outdir, opts.out),'recreate') as outfile: outfile.cd() to_save.extend([ measured_no_correction, eff_hist, purity_hist, full_true, myunfolding.truth, ## 4 myunfolding.measured, ## 5 myunfolding.matrix,]) ## 6 if opts.tau < 0: to_save.extend([ l_curve, ## 9 tau_curve, ## 10 graph_x, graph_y ]) if opts.cov_matrix != 'none': to_save.extend([input_cov_matrix]) to_save.extend([input_corr_matrix]) for i, j in enumerate(to_save): log.debug('Saving %s as %s' % (j.name, j.GetName())) j.Write() getattr(resp_file, opts.var).reco_distribution.Write() getattr(resp_file, opts.var).prefit_distribution.Write() json = ROOT.TText(0., 0., prettyjson.dumps(best_taus)) outfile.WriteTObject(json, 'best_taus') myunfolding.write_to(outfile, 'urunfolder') outfile.Close()
if sample in signals: histo.Scale(sig_yields[cat_name][sample]) if sample not in sample_sums: sample_sums[sample] = histo.Clone() else: sample_sums[sample] += histo samples.append(histo) samples.sort(key=ordering) stack = plotter.create_stack(*samples, sort=False) legend = LegendDefinition(position='NE') plotter.overlay_and_compare( [stack], data, writeTo=cat_name, legend_def = legend, xtitle='discriminant', ytitle='Events', method='ratio' ) samples = [j for i, j in sample_sums.iteritems() if i <> 'data_obs' if i <> 'postfit S+B'] data = sample_sums['data_obs'] samples.sort(key=ordering) plotter.overlay_and_compare( [plotter.create_stack(*samples, sort=False)], data, writeTo=base, legend_def = LegendDefinition(position='NE'), xtitle='discriminant', ytitle='Events', method='ratio'
asitot.markerstyle = 20 asitot.markercolor = 'red' f1 = plotter.parse_formula('height*TMath::Poisson(x, mean)', 'height[1], mean[%f]' % (tot), [0, 100000]) f1.linecolor = 'red' f1.linewidth = 2 peak = f1(tot) f1 = plotter.parse_formula('height*TMath::Poisson(x, mean)', 'height[%f], mean[%f]' % (height / peak, tot), [0, 100000]) canlog = all(i > 0 for i in asimov.itervalues()) #jmap[savename] = asimov asimov = asimov.items() asimov.sort(key=sorting) havg = flush(avgs, linewidth=2, linecolor='red') hmed = flush(meds, linewidth=2, linecolor='blue') hasi = flush(asimov, linewidth=2, linecolor='black') plotter.overlay_and_compare([havg, hmed], hasi, method='ratio') ##if canlog: ## plotter.pad.SetLogy(True) plotter.save(savename) plotter.overlay([total, asitot, f1]) plotter.save('tot' + savename) #with open('%s/asimovs.json' % plotter.outputdir, 'w') as json: # json.write(prettyjson.dumps(jmap))
def unfolding_toy_diagnostics(indir, variable): plotter = BasePlotter(defaults={ 'clone': False, 'name_canvas': True, 'show_title': True, 'save': { 'png': True, 'pdf': False } }, ) styles = { 'dots': { 'linestyle': 0, 'markerstyle': 21, 'markercolor': 1 }, 'compare': { 'linesstyle': [1, 0], 'markerstyle': [0, 21], 'markercolor': [2, 1], 'linecolor': [2, 1], 'drawstyle': ['hist', 'pe'], 'legendstyle': ['l', 'p'] } } xaxislabel = set_pretty_label(variable) true_distribution = None curdir = os.getcwd() os.chdir(indir) toydirs = get_immediate_subdirectories(".") methods = [] pulls_lists = {} pull_means_lists = {} pull_mean_errors_lists = {} pull_sums_lists = {} pull_sigmas_lists = {} pull_sigma_errors_lists = {} deltas_lists = {} delta_means_lists = {} delta_mean_errors_lists = {} delta_sigmas_lists = {} delta_sigma_errors_lists = {} ratio_sums_lists = {} nneg_bins_lists = {} unfoldeds_lists = {} unfolded_sigmas_lists = {} taus_lists = {} histos_created = False lists_created = False idir = 0 true_distro = None #loop over toys for directory in toydirs: if not directory.startswith('toy_'): continue os.chdir(directory) log.debug('Inspecting toy %s' % directory) idir = idir + 1 i = 0 if not os.path.isfile("result_unfolding.root"): raise ValueError('root file not found in %s' % os.getcwd()) with io.root_open("result_unfolding.root") as inputfile: log.debug('Iteration %s over the file' % i) i = i + 1 if not methods: keys = [i.name for i in inputfile.keys()] for key in keys: if hasattr(getattr(inputfile, key), "hdata_unfolded"): methods.append(key) unfolded_hists = [ inputfile.get('%s/hdata_unfolded' % i) for i in methods ] unfolded_wps_hists = [ inputfile.get('%s/hdata_unfolded_ps_corrected' % i) for i in methods ] for unf, unfps, method in zip(unfolded_hists, unfolded_wps_hists, methods): unf.name = method unfps.name = method if true_distro is None: true_distribution = inputfile.true_distribution ROOT.TH1.AddDirectory(False) true_distro = true_distribution.Clone() taus = prettyjson.loads(inputfile.best_taus.GetTitle()) if len(taus_lists) == 0: taus_lists = dict((i, []) for i in taus) for i, t in taus.iteritems(): taus_lists[i].append(t) for histo in unfolded_hists: #create pull/delta containers during first iteration name = histo.name nbins = histo.nbins() log.debug("name = %s, n bins = %s" % (name, nbins)) if not lists_created: for ibin in range(1, nbins + 1): outname = "pull_" + name + "_bin" + str(ibin) pulls_lists[outname] = [] outname = "delta_" + name + "_bin" + str(ibin) deltas_lists[outname] = [] outname = "unfolded_" + name + "_bin" + str(ibin) unfoldeds_lists[outname] = [] unfolded_sigmas_lists[outname] = [] outname = "pull_" + name pull_means_lists[outname] = {} pull_mean_errors_lists[outname] = {} pull_sigmas_lists[outname] = {} pull_sigma_errors_lists[outname] = {} outname = "delta_" + name delta_means_lists[outname] = {} delta_mean_errors_lists[outname] = {} delta_sigmas_lists[outname] = {} delta_sigma_errors_lists[outname] = {} for ibin in range(1, nbins + 1): outname = "pull_" + name + "_bin" + str(ibin) unfolded_bin_content = histo.GetBinContent(ibin) unfolded_bin_error = histo.GetBinError(ibin) true_bin_content = true_distro.GetBinContent(ibin) true_bin_error = true_distro.GetBinError(ibin) total_bin_error = math.sqrt(unfolded_bin_error**2) #??? if (total_bin_error != 0): pull = (unfolded_bin_content - true_bin_content) / total_bin_error else: pull = 9999 log.debug( 'unfolded bin content %s +/- %s, true bin content %s, pull %s' % (unfolded_bin_content, unfolded_bin_error, true_bin_content, pull)) pulls_lists[outname].append(pull) outname = "delta_" + name + "_bin" + str(ibin) delta = unfolded_bin_content - true_bin_content log.debug( 'unfolded bin content %s +/- %s, true bin content %s, delta %s' % (unfolded_bin_content, unfolded_bin_error, true_bin_content, delta)) deltas_lists[outname].append(delta) outname = "unfolded_" + name + "_bin" + str(ibin) unfoldeds_lists[outname].append(unfolded_bin_content) unfolded_sigmas_lists[outname].append(unfolded_bin_error) nneg_bins_hists = [ i for i in inputfile.keys() if i.GetName().startswith("nneg_bins") ] nneg_bins_hists = [asrootpy(i.ReadObj()) for i in nneg_bins_hists] for histo in nneg_bins_hists: #create pull/delta containers during first iteration name = histo.name nbins = histo.nbins() log.debug("name = %s, n bins = %s" % (name, nbins)) if not lists_created: outname = name nneg_bins_lists[outname] = [] outname = name nneg_bins_lists[outname].append(histo.GetBinContent(1)) pull_sums_hists = [ i for i in inputfile.keys() if i.GetName().startswith("sum_of_pulls") ] pull_sums_hists = [asrootpy(i.ReadObj()) for i in pull_sums_hists] for histo in pull_sums_hists: #create pull/delta containers during first iteration name = histo.name nbins = histo.nbins() log.debug("name = %s, n bins = %s" % (name, nbins)) if not lists_created: outname = name pull_sums_lists[outname] = [] outname = name pull_sums_lists[outname].append(histo.GetBinContent(1)) ratio_sums_hists = [ i for i in inputfile.keys() if i.GetName().startswith("sum_of_ratios") ] ratio_sums_hists = [ asrootpy(i.ReadObj()) for i in ratio_sums_hists ] for histo in ratio_sums_hists: #create ratio/delta containers during first iteration name = histo.name nbins = histo.nbins() log.debug("name = %s, n bins = %s" % (name, nbins)) if not lists_created: outname = name ratio_sums_lists[outname] = [] outname = name ratio_sums_lists[outname].append(histo.GetBinContent(1)) #after the first iteration on the file all the lists are created lists_created = True os.chdir("..") #create histograms #histo containers taus = {} for name, vals in taus_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if val_min == val_max: if tau_nbins % 2: #if odd val_min, val_max = val_min - 0.01, val_min + 0.01 else: brange = 0.02 bwidth = brange / tau_nbins val_min, val_max = val_min - 0.01 + bwidth / 2., val_min + 0.01 + bwidth / 2. title = '#tau choice - %s ;#tau;N_{toys}' % (name) histo = Hist(tau_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) taus[name] = histo pulls = {} for name, vals in pulls_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max abs_max = max(abs(val_min), abs(val_max)) if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: _, method, binno = tuple(name.split('_')) title = 'Pulls - %s - %s ;Pull;N_{toys}' % (binno, method) histo = Hist(pull_nbins, -abs_max, abs_max, name=name, title=title) for val in vals: histo.Fill(val) pulls[name] = histo deltas = {} for name, vals in deltas_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: _, method, binno = tuple(name.split('_')) title = 'Deltas - %s - %s ;Delta;N_{toys}' % (binno, method) histo = Hist(delta_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) deltas[name] = histo unfoldeds = {} for name, vals in unfoldeds_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: _, method, binno = tuple(name.split('_')) title = 'Unfoldeds - %s - %s ;Unfolded;N_{toys}' % (binno, method) histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) unfoldeds[name] = histo nneg_bins = {} for name, vals, in nneg_bins_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0 if val_min > 0 else val_min - 1 val_max = max(vals) val_max = 0 if val_max < 0 else val_max + 1 if 'L_curve' in name: method = 'L_curve' else: set_trace() _, method, _ = tuple(name.split('_')) title = 'N of negative bins - %s ;N. neg bins;N_{toys}' % method histo = Hist(int(val_max - val_min + 1), val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) nneg_bins[name] = histo pull_sums = {} for name, vals in pull_sums_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' else: set_trace() _, _, _, _, _, method = tuple(name.split('_')) title = 'Pull sums - %s ;#Sigma(pull)/N_{bins};N_{toys}' % method histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) pull_sums[name] = histo ratio_sums = {} for name, vals in ratio_sums_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: set_trace() _, _, _, _, _, method = tuple(name.split('_')) title = 'Ratio sums - %s;#Sigma(ratio)/N_{bins};N_{toys}' % method histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) ratio_sums[name] = histo unfolded_sigmas = {} for name, vals in unfolded_sigmas_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: _, method, binno = tuple(name.split('_')) title = 'Unfolded uncertainties - %s - %s ;Uncertainty;N_{toys}' % ( binno, method) histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) unfolded_sigmas[name] = histo for name, histo in pulls.iteritems(): log.debug("name is %s and object type is %s" % (name, type(histo))) histo.Fit("gaus", 'Q') if not histo.GetFunction("gaus"): log.warning("Function not found for histogram %s" % name) continue mean = histo.GetFunction("gaus").GetParameter(1) meanError = histo.GetFunction("gaus").GetParError(1) sigma = histo.GetFunction("gaus").GetParameter(2) sigmaError = histo.GetFunction("gaus").GetParError(2) general_name, idx = tuple(name.split('_bin')) idx = int(idx) pull_means_lists[general_name][idx] = mean pull_mean_errors_lists[general_name][idx] = meanError pull_sigmas_lists[general_name][idx] = sigma pull_sigma_errors_lists[general_name][idx] = sigmaError for name, histo in deltas.iteritems(): log.debug("name is %s and object type is %s" % (name, type(histo))) histo.Fit("gaus", 'Q') if not histo.GetFunction("gaus"): log.warning("Function not found for histogram %s" % name) continue mean = histo.GetFunction("gaus").GetParameter(1) meanError = histo.GetFunction("gaus").GetParError(1) sigma = histo.GetFunction("gaus").GetParameter(2) sigmaError = histo.GetFunction("gaus").GetParError(2) general_name, idx = tuple(name.split('_bin')) idx = int(idx) delta_means_lists[general_name][idx] = mean delta_mean_errors_lists[general_name][idx] = meanError delta_sigmas_lists[general_name][idx] = sigma delta_sigma_errors_lists[general_name][idx] = sigmaError outfile = rootpy.io.File("unfolding_diagnostics.root", "RECREATE") outfile.cd() pull_means = {} pull_sigmas = {} pull_means_summary = {} pull_sigmas_summary = {} delta_means = {} delta_sigmas = {} delta_means_summary = {} delta_sigmas_summary = {} for outname, pmeans in pull_means_lists.iteritems(): outname_mean = outname + "_mean" outtitle = "Pull means - " + outname + ";Pull mean; N_{toys}" pull_mean_min = min(pmeans.values()) pull_mean_max = max(pmeans.values()) pull_mean_newmin = pull_mean_min - (pull_mean_max - pull_mean_min) * 0.5 pull_mean_newmax = pull_mean_max + (pull_mean_max - pull_mean_min) * 0.5 pull_means[outname] = plotting.Hist(pull_mean_nbins, pull_mean_newmin, pull_mean_newmax, name=outname_mean, title=outtitle) outname_mean_summary = outname + "_mean_summary" outtitle_mean_summary = "Pull mean summary - " + outname histocloned = true_distro.Clone(outname_mean_summary) histocloned.Reset() histocloned.xaxis.title = xaxislabel histocloned.yaxis.title = 'Pull mean' histocloned.title = outtitle_mean_summary pull_means_summary[outname] = histocloned for idx, pmean in pmeans.iteritems(): pull_means[outname].Fill(pmean) histocloned[idx].value = pmean histocloned[idx].error = pull_mean_errors_lists[outname][idx] histocloned.yaxis.SetRangeUser(min(pmeans.values()), max(pmeans.values())) for outname, psigmas in pull_sigmas_lists.iteritems(): outname_sigma = outname + "_sigma" outtitle_sigma = "Pull #sigma's - " + outname + ";Pull #sigma; N_{toys}" pull_sigma_min = min(psigmas.values()) pull_sigma_max = max(psigmas.values()) pull_sigma_newmin = pull_sigma_min - (pull_sigma_max - pull_sigma_min) * 0.5 pull_sigma_newmax = pull_sigma_max + (pull_sigma_max - pull_sigma_min) * 0.5 pull_sigmas[outname] = plotting.Hist(pull_sigma_nbins, pull_sigma_newmin, pull_sigma_newmax, name=outname_sigma, title=outtitle_sigma) outname_sigma_summary = outname + "_sigma_summary" outtitle_sigma_summary = "Pull #sigma summary - " + outname histocloned = true_distro.Clone(outname_sigma_summary) histocloned.Reset() histocloned.xaxis.title = xaxislabel histocloned.yaxis.title = 'Pull #sigma' histocloned.title = outtitle_sigma_summary pull_sigmas_summary[outname] = histocloned for idx, psigma in psigmas.iteritems(): pull_sigmas[outname].Fill(psigma) histocloned[idx].value = psigma histocloned[idx].error = pull_sigma_errors_lists[outname][idx] histocloned.yaxis.SetRangeUser(min(psigmas.values()), max(psigmas.values())) for outname, dmeans in delta_means_lists.iteritems(): outname_mean = outname + "_mean" outtitle = "Delta means - " + outname + ";Delta mean; N_{toys}" delta_mean_min = min(dmeans.values()) delta_mean_max = max(dmeans.values()) delta_mean_newmin = delta_mean_min - (delta_mean_max - delta_mean_min) * 0.5 delta_mean_newmax = delta_mean_max + (delta_mean_max - delta_mean_min) * 0.5 delta_means[outname] = plotting.Hist(delta_mean_nbins, delta_mean_newmin, delta_mean_newmax, name=outname_mean, title=outtitle) outname_mean_summary = outname + "_mean_summary" outtitle_mean_summary = "Delta mean summary - " + outname histocloned = true_distro.Clone(outname_mean_summary) histocloned.Reset() histocloned.xaxis.title = xaxislabel histocloned.yaxis.title = 'Delta mean' histocloned.title = outtitle_mean_summary delta_means_summary[outname] = histocloned for idx, dmean in dmeans.iteritems(): delta_means[outname].Fill(dmean) histocloned[idx].value = dmean histocloned[idx].error = delta_mean_errors_lists[outname][idx] histocloned.yaxis.SetRangeUser(min(dmeans.values()), max(dmeans.values())) for outname, dsigmas in delta_sigmas_lists.iteritems(): outname_sigma = outname + "_sigma" outtitle_sigma = "Delta #sigma's - " + outname + ";Delta #sigma; N_{toys}" delta_sigma_min = min(dsigmas.values()) delta_sigma_max = max(dsigmas.values()) delta_sigma_newmin = delta_sigma_min - (delta_sigma_max - delta_sigma_min) * 0.5 delta_sigma_newmax = delta_sigma_max + (delta_sigma_max - delta_sigma_min) * 0.5 delta_sigmas[outname] = plotting.Hist(delta_sigma_nbins, delta_sigma_newmin, delta_sigma_newmax, name=outname_sigma, title=outtitle_sigma) outname_sigma_summary = outname + "_sigma_summary" outtitle_sigma_summary = "Delta #sigma summary - " + outname histocloned = true_distro.Clone(outname_sigma_summary) histocloned.Reset() histocloned.xaxis.title = xaxislabel histocloned.yaxis.title = 'Delta #sigma' histocloned.title = outtitle_sigma_summary delta_sigmas_summary[outname] = histocloned for idx, dsigma in dsigmas.iteritems(): delta_sigmas[outname].Fill(dsigma) histocloned[idx].value = dsigma histocloned[idx].error = delta_sigma_errors_lists[outname][idx] histocloned.yaxis.SetRangeUser(min(dsigmas.values()), max(dsigmas.values())) unfolded_summary = {} unfolded_average = {} unfolded_envelope = {} for name, histo in unfoldeds.iteritems(): log.debug("name is %s and object type is %s" % (name, type(histo))) histo.Fit("gaus", 'Q') if not histo.GetFunction("gaus"): log.warning("Function not found for histogram %s" % name) continue mean = histo.GetFunction("gaus").GetParameter(1) meanError = histo.GetFunction("gaus").GetParError(1) sigma = histo.GetFunction("gaus").GetParameter(2) sigmaError = histo.GetFunction("gaus").GetParError(2) general_name, idx = tuple(name.split('_bin')) idx = int(idx) if general_name not in unfolded_summary: histo = true_distro.Clone("%s_unfolded_summary" % general_name) outtitle_unfolded_summary = "Unfolded summary - " + general_name histo.Reset() histo.xaxis.title = xaxislabel histo.yaxis.title = 'N_{events}' histo.title = outtitle_unfolded_summary unfolded_summary[general_name] = histo unfolded_envelope[general_name] = histo.Clone( "%s_unfolded_envelope" % general_name) unfolded_average[general_name] = histo.Clone( "%s_unfolded_average" % general_name) unfolded_summary[general_name][idx].value = mean unfolded_summary[general_name][idx].error = meanError unfolded_envelope[general_name][idx].value = mean unfolded_envelope[general_name][idx].error = sigma unfolded_average[general_name][idx].value = mean unfolded_average[general_name][idx].error = \ unfolded_sigmas['%s_bin%i' % (general_name, idx)].GetMean() plotter.set_subdir('taus') for name, histo in taus.iteritems(): #canvas = plotter.create_and_write_canvas_single(0, 21, 1, False, False, histo, write=False) plotter.canvas.cd() histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) info = plotter.make_text_box( 'mode #tau = %.5f' % histo[histo.GetMaximumBin()].x.center, position=(plotter.pad.GetLeftMargin(), plotter.pad.GetTopMargin(), 0.3, 0.025)) info.Draw() plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('pulls') for name, histo in pulls.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in pull_means.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.Write() plotter.save() for name, histo in pull_sigmas.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.Write() plotter.save() plotter.set_subdir('pull_summaries') for name, histo in pull_means_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) #histo.SetStats(True) line = ROOT.TLine(histo.GetBinLowEdge(1), 0, histo.GetBinLowEdge(histo.GetNbinsX() + 1), 0) line.Draw("same") plotter.save() histo.Write() plotter.canvas.Write() for name, histo in pull_sigmas_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) #histo.SetStats(True) line = ROOT.TLine(histo.GetBinLowEdge(1), 1, histo.GetBinLowEdge(histo.GetNbinsX() + 1), 1) line.Draw("same") plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('deltas') for name, histo in deltas.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in delta_means.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.Write() plotter.save() for name, histo in delta_sigmas.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.Write() plotter.save() plotter.set_subdir('delta_summaries') for name, histo in delta_means_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) #histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in delta_sigmas_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) #histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('unfolding_unc') for name, histo in unfolded_sigmas.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('unfolded') for name, histo in unfoldeds.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('unfolded_summaries') for name, histo in unfolded_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in unfolded_summary.iteritems(): leg = LegendDefinition("Unfolding comparison", 'NE', labels=['Truth', 'Unfolded']) plotter.overlay_and_compare([true_distro], histo, legend_def=leg, **styles['compare']) plotter.canvas.name = 'Pull_' + name plotter.save() plotter.canvas.Write() plotter.overlay_and_compare([true_distro], histo, legend_def=leg, method='ratio', **styles['compare']) plotter.canvas.name = 'Ratio_' + name plotter.save() plotter.canvas.Write() plotter.set_subdir('unfolded_average') for name, histo in unfolded_average.iteritems(): leg = LegendDefinition("Unfolding comparison", 'NE', labels=['Truth', 'Unfolded']) #set_trace() plotter.overlay_and_compare([true_distro], histo, legend_def=leg, **styles['compare']) plotter.canvas.name = 'Pull_' + name plotter.save() plotter.canvas.Write() plotter.overlay_and_compare([true_distro], histo, legend_def=leg, method='ratio', **styles['compare']) plotter.canvas.name = 'Ratio_' + name plotter.save() plotter.canvas.Write() plotter.set_subdir('unfolded_envelope') for name, histo in unfolded_envelope.iteritems(): leg = LegendDefinition("Unfolding comparison", 'NE', labels=['Truth', 'Unfolded']) plotter.overlay_and_compare([true_distro], histo, legend_def=leg, **styles['compare']) plotter.canvas.name = 'Pull_' + name plotter.save() plotter.canvas.Write() plotter.overlay_and_compare([true_distro], histo, legend_def=leg, method='ratio', **styles['compare']) plotter.canvas.name = 'Ratio_' + name plotter.save() plotter.canvas.Write() plotter.set_subdir('figures_of_merit') for name, histo in nneg_bins.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in pull_sums.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in ratio_sums.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() outfile.close() os.chdir(curdir)
sample_names = [i.name for i in cat_dir.keys() if not i.name.startswith('total')] samples = [fix_binning(cat_dir.Get(i), data) for i in sample_names] for i, j in zip(sample_names, samples): if i not in sample_sums: sample_sums[i] = j.Clone() else: sample_sums[i] += j samples.sort(key=ordering) stack = plotter.create_stack(*samples, sort=False) legend = LegendDefinition(position='NE') plotter.overlay_and_compare( [stack, hsum], data, writeTo=cat_name, legend_def = legend, xtitle='discriminant', ytitle='Events', method='ratio' ) samples = [j for i, j in sample_sums.iteritems() if i <> 'data_obs' if i <> 'postfit S+B'] samples.sort(key=ordering) data = sample_sums['data_obs'] hsum = sample_sums['postfit S+B'] plotter.overlay_and_compare( [plotter.create_stack(*samples, sort=False), hsum], data, writeTo=base, legend_def = LegendDefinition(position='NE'), xtitle='discriminant', ytitle='Events',
line = [cat_name, '%.1f' % data.Integral()] + [ '%.1f' % (cat_dir.Get(i).Integral() if i in available_samples else 0.0) for i in sample_names ] table.add_line(*line) samples = [ fix_binning(cat_dir.Get(i), data) for i in available_samples ] samples.sort(key=ordering) stack = plotter.create_stack(*samples, sort=False) legend = LegendDefinition(position='NE') plotter.overlay_and_compare([stack, hsum], data, writeTo=cat_name, legend_def=legend, xtitle='#lambda_{M}', ytitle='Events', method='datamc') table.add_separator() with open('%s/yields.raw_txt' % out_dir, 'w') as tab: tab.write('%s\n' % table) if pfit == 'postfit': pars = rootpy.asrootpy(mlfit_file.fit_s.floatParsFinal()) tab.write(print_var_line('charmSF', pars)) if 'lightSF' in pars: tab.write(print_var_line('lightSF', pars))