Beispiel #1
0
	def save_card(self, name, bbb=True):
		if not self.card:
			raise RuntimeError('There is no card to save!')
		self.card.clamp_negative_bins('.*','ZJets')
		for sys_name, info in self.systematics.iteritems():
			if 'value' not in info: continue
			for category in info['categories']:
				for sample in info['samples']:
					plotter.card.add_systematic(
						sys_name, 
						info['type'], 
						category, 
						sample, 
						info['value']
						)

		if bbb:
			mcsamples = [i for i in self.card_names if i != 'data_obs']
			for catname in self.card.categories.keys():
				category = self.card.categories[catname]
				#inflate bin errors of tt for each sample
				hists = [category[i] for i in mcsamples]
				for bins in zip(*hists):
					if bins[0].overflow: continue
					unc = quad.quad(*[i.error for i in bins[1:]])
					idx = bins[0].idx
					hup = category['TT'].Clone()
					hup[idx].value += unc
					hdw = category['TT'].Clone()
					hdw[idx].value -= unc
					category['TT_CMS_httbar_%s_MCstatBin%dUp'   % (catname, idx)] = hup
					category['TT_CMS_httbar_%s_MCstatBin%dDown' % (catname, idx)] = hdw
		self.card.save(name, self.outputdir)
Beispiel #2
0
def covers(realvar, val, sys=None):
    delta = realvar.value - val
    err_idx = 1 if delta >= 0 else 0
    delta = abs(delta)
    if hasattr(realvar.error, '__getitem__'):  #two sided error
        err = abs(realvar.error[err_idx])
    else:  #one sided error
        err = realvar.error
    if sys is not None:
        err = quad.quad(err, realvar.value * sys)
    return delta < err
Beispiel #3
0
    def cut_flow(self):
        BasePlotter.set_canvas_style(self.canvas)
        BasePlotter.set_canvas_style(self.pad)
        lab_f1, _ = self.dual_pad_format()
        self.label_factor = lab_f1
        views_to_flow = filter(lambda x: 'ttJets' not in x and 'QCD' not in x,
                               self.mc_samples)
        views_to_flow.append(self.ttbar_to_use)
        qcd_samples = [i for i in self.views if 'QCD' in i]
        samples = []

        for vtf in views_to_flow:
            histo = self.get_view(vtf).Get('cut_flow')
            print vtf, len(histo)
            self.keep.append(histo)
            samples.append(histo)

        #QCD may not have all the bins filled, needs special care
        qcd_histo = histo.Clone()
        qcd_histo.Reset()
        for sample in qcd_samples:
            qcd_flow = self.get_view(sample).Get('cut_flow')
            qcd_histo = qcd_histo.decorate(**qcd_flow.decorators)
            qcd_histo.title = qcd_flow.title
            for sbin, qbin in zip(qcd_histo, qcd_flow):
                sbin.value += qbin.value
                sbin.error = quad.quad(sbin.error, qbin.error)
        samples.append(qcd_histo)
        self.keep.append(qcd_histo)
        samples.sort(key=lambda x: x[-2].value)
        stack = plotting.HistStack()
        self.keep.append(stack)
        for i in samples:
            stack.Add(i)

        self.style_histo(stack)
        self.style_histo(histo, **histo.decorators)

        histo.Draw()  #set the proper axis labels
        histo.yaxis.title = 'Events'
        data = self.get_view('data').Get('cut_flow')
        smin = min(stack.min(), data.min(), 1.2)
        smax = max(stack.max(), data.max())
        histo.yaxis.range_user = smin * 0.8, smax * 1.2
        stack.Draw('same')
        data.Draw('same')
        self.keep.append(data)
        self.add_legend([stack, data], False, entries=len(views_to_flow) + 1)
        self.pad.SetLogy()
        self.add_ratio_plot(data, stack, ratio_range=0.4)
        self.lower_pad.SetLogy(False)
Beispiel #4
0
	def make_preselection_plot(self, *args, **kwargs):
		systematics = None
		if 'sys_effs' in kwargs:
			systematics = kwargs['sys_effs']
			del kwargs['sys_effs']
		mc_default = self.mc_samples
		self.mc_samples = [
			'QCD*',
			'EWK',
			'single*',			
			'ttJets_preselection'
			]
		self.plot_mc_vs_data(*args, **kwargs)
		if systematics is not None:
			data = [i for i in self.keep if isinstance(i, ROOT.TH1)][0]
			mc_stack = [i for i in self.keep if isinstance(i, ROOT.THStack)][0]
			stack_sum = sum(mc_stack.hists)
			self.reset()
			dirname = args[0].split('/')[0]
			path = args[0]
			args = list(args)
			args[0] = path.replace(dirname, '%s_up' % systematics)
			kwargs['nodata'] = True
			self.plot_mc_vs_data(*args, **kwargs)
			stack_up = [i for i in self.keep if isinstance(i, ROOT.THStack)][0]
			self.reset()
			s_up = sum(stack_up.hists)
			for ibin, jbin in zip(stack_sum, s_up):
				ibin.error = quad.quad(ibin.error, abs(ibin.value - jbin.value))
			stack_sum.fillcolor = 'black'
			stack_sum.fillstyle = 3013
			stack_sum.title = 'uncertainty'
			stack_sum.drawstyle = 'pe2'
			stack_sum.markerstyle = 0
			plotter.overlay_and_compare(
				[mc_stack, stack_sum], data,
				xtitle = kwargs.get('xaxis',''),
				ytitle='Events', ignore_style=True,				
				method='datamc'
				)
			# Add legend
			self.pad.cd()
			self.add_legend(
				[mc_stack, stack_sum, data], kwargs.get('leftside', True), 
				entries=len(mc_stack.hists)+2
				)

		self.mc_samples = mc_default
Beispiel #5
0
		def quad_envelope(view, var, idrange):
			central = view.Get(vartemplate % (var, idrange[0]))
			sqsum = central.Clone()
			sqsum.Reset()
			for pos, idx in enumerate(idrange[1:]):
				if pos % 2: continue
				h1 = get_and_scale(view, var, idx)
				h2 = get_and_scale(view, var, idrange[pos+1])
				for sbin, cbin, h1bin, h2bin in zip(sqsum, central, h1, h2):
					d1 = abs(cbin.value-h1bin.value)
					d2 = abs(cbin.value-h2bin.value)					
					sbin.value = quad.quad(sbin.value, max(d1,d2))

			for sbin, cbin in zip(sqsum, central):				
				cbin.error = sbin.value
			return to_up_down(central)
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()
Beispiel #7
0
    def newRebin2D(histogram, bin_arrayx, bin_arrayy):
        'Rebin 2D histo with irregular bin size'

        #old binning
        oldbinx = [float(histogram.GetXaxis().GetBinLowEdge(1))]
        oldbiny = [float(histogram.GetYaxis().GetBinLowEdge(1))]
        oldbinx.extend(float(histogram.GetXaxis().GetBinUpEdge(x)) for x in xrange(1, histogram.GetNbinsX()+1))
        oldbiny.extend(float(histogram.GetYaxis().GetBinUpEdge(y)) for y in xrange(1, histogram.GetNbinsY()+1))
        
        #if new binninf is just one number and int, use it to rebin rather than as edges
        if len(bin_arrayx) == 1 and isinstance(bin_arrayx[0], int):
            nrebin = bin_arrayx[0]
            bin_arrayx = [j for i, j in enumerate(oldbinx) if i % nrebin == 0]
        if len(bin_arrayy) == 1 and isinstance(bin_arrayy[0], int):
            nrebin = bin_arrayy[0]
            bin_arrayy = [j for i, j in enumerate(oldbiny) if i % nrebin == 0]

        #create a clone with proper binning
        # from pdb import set_trace; set_trace()
        new_histo = plotting.Hist2D(
            bin_arrayx,
            bin_arrayy,
            #name = histogram.name,
            title = histogram.title,
            **histogram.decorators
        )
        new_histo.xaxis.title = histogram.xaxis.title
        new_histo.yaxis.title = histogram.yaxis.title

        #check that new bins don't overlap on old edges
        for x in bin_arrayx:
            if x==0:
                if not any( abs((oldx)) < 10**-8 for oldx in oldbinx ):
                    raise Exception('New bin edge in x axis %s does not match any old bin edge, operation not permitted' % x)
            else:
                if not any( abs((oldx / x)-1.) < 10**-8 for oldx in oldbinx ):
                    raise Exception('New bin edge in x axis %s does not match any old bin edge, operation not permitted' % x)
        for y in bin_arrayy:
            if y ==0:
                if not any( abs((oldy) )< 10**-8 for oldy in oldbiny ):
                    raise Exception('New bin edge in y axis %s does not match any old bin edge, operation not permitted' % y)
            else:
                if not any( abs((oldy / y)-1.) < 10**-8 for oldy in oldbiny ):
                    raise Exception('New bin edge in y axis %s does not match any old bin edge, operation not permitted' % y)
        
        #fill the new histogram
        for x in xrange(0, histogram.GetNbinsX()+2 ):
            for y in xrange(0, histogram.GetNbinsY()+2 ):
                new_bin_x = new_histo.GetXaxis().FindFixBin(
                    histogram.GetXaxis().GetBinCenter(x)
                    )
                new_bin_y = new_histo.GetYaxis().FindFixBin(
                    histogram.GetYaxis().GetBinCenter(y)
                    )
                new_histo.SetBinContent(
                    new_bin_x, new_bin_y,
                    histogram.GetBinContent(x,y) + 
                    new_histo.GetBinContent(new_bin_x, new_bin_y)
                    )
                new_histo.SetBinError(
                    new_bin_x, new_bin_y,
                    quad(
                        histogram.GetBinError(x,y),
                        new_histo.GetBinError(new_bin_x, new_bin_y)
                        )
                    )
                #new_histo.Fill(
                #    histogram.GetXaxis().GetBinCenter(x), 
                #    histogram.GetYaxis().GetBinCenter(y), 
                #    histogram.GetBinContent(x,y)
                #    )

        new_histo.SetEntries( histogram.GetEntries() )
        return new_histo
Beispiel #8
0
mc_effs = prettyjson.loads(
    open('plots/%s/ctageff/mc_effs.json' % jobid).read())

#set_trace()
nevts = json_ufloat(
    summary, 'nevts')  #ufloat(summary['nevts'    ], summary['nevts_err'])
lcfrac = json_ufloat(
    summary,
    'leadCFrac')  #ufloat(summary['leadCFrac'], summary['leadCFrac_err'])
scfrac = json_ufloat(
    summary,
    'subCFrac')  #ufloat(summary['subCFrac' ], summary['subCFrac_err'])
llfrac = 1 - lcfrac - scfrac  #all-light fraction

factor = lambda leff, ceff, cfrac: ceff * cfrac + leff * (1 - cfrac)
distance = lambda x, y: (x.n - y.n) / quad(x.s)
#line = lambda name, val, est: '%20s%20s%20s%20s' % (name, val, est, distance(val, est))
line = lambda name, val, est, est2, est3: (
    '%20s' * 2 + '%20.2f' * 3) % (name, val, est.n, est2.n, est3.n
                                  )  #distance(val, est), distance(val, est2))
header = ('%20s' * 5) % ('region', 'mc evts', 'from closure', 'lead/sublead',
                         'avg eff')  #, 'delta 2 (sigma)')
header_size = 6 * 20

for wp, info in wpoints.iteritems():
    table = tables.Table(
        'region:%20s',
        'mc evts:%20s',
        ## 'from closure:%20.2f',
        'lead/sublead:%20.2f',
        ## 'avg eff:%20.2f',
Beispiel #9
0
         )

   biases.SetPoint(idx, tau, sum(bias)/len(bias))
   uncs.SetPoint(idx, tau, sum(avg_uncs)/len(avg_uncs))


canvas = plotter.create_and_write_graph_canvas('unc_scan',[0],[1],True,True,[uncs],write=False)
uncs.yaxis.SetMoreLogLabels(True)
canvas.SaveAs('%s/unc_scan.png' % unf_dir)

canvas = plotter.create_and_write_graph_canvas('bias_scan',[0],[1],True,True,[biases],write=False)
biases.yaxis.SetMoreLogLabels(True)
canvas.SaveAs('%s/bias_scan.png' % unf_dir)

bias_points = [(biases.GetX()[i], biases.GetY()[i])
               for i in xrange(biases.GetN())]
unc_points = [(uncs.GetX()[i], uncs.GetY()[i])
               for i in xrange(uncs.GetN())]
fom_scan = plotting.Graph(uncs.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'
canvas = plotter.create_and_write_graph_canvas('fom_scan',[0],[1],True,True,[fom_scan],write=False)
fom_scan.yaxis.SetMoreLogLabels(True)
canvas.SaveAs('%s/fom_scan.png' % unf_dir)

Beispiel #10
0
	def pdf_unc_histo(self, view, var, pdf, central):
		nnpdf = range(9, 109)
		## ct10 = range(112, 165)
		## mmht = range(167, 218)
		## allpdf = [0]+range(10, 218)
		vartemplate = '%s_mcws_pdf_%d'
		
		def to_up_down(central):
			up, down = central.Clone(), central.Clone()
			up.Reset()
			down.Reset()
			for ubin, dbin, cbin in zip(up, down, central):
				ubin.value = cbin.value + cbin.error
				dbin.value = cbin.value - cbin.error
			return up, down

		def get_and_scale(view, var, idx):
			scale = 1./self.tt_lhe_weights['%d' % idx]
			value = view.Get(vartemplate % (var, idx))
			value.Scale(scale)			
			return value

		def full_envelope(view, var, idrange):
			envelope = Envelope()
			for idx in idrange:
				envelope.add(
					get_and_scale(view, var, idx)
					)
			return to_up_down(envelope.one_sigma)

		def rms_envelope(view, var, idrange):
			central = view.Get(vartemplate % (var, idrange[0]))
			sqsum = central.Clone()
			sqsum.Reset()
			for idx in idrange[1:]:
				histo = get_and_scale(view, var, idx)
				for sbin, cbin, hbin in zip(sqsum, central, histo):
					sbin.value += (cbin.value-hbin.value)**2

			nreplicas = len(idrange)-1
			for sbin, cbin in zip(sqsum, central):
				err = math.sqrt(sbin.value/nreplicas)
				cbin.error = err
			return to_up_down(central)
					
		def quad_envelope(view, var, idrange):
			central = view.Get(vartemplate % (var, idrange[0]))
			sqsum = central.Clone()
			sqsum.Reset()
			for pos, idx in enumerate(idrange[1:]):
				if pos % 2: continue
				h1 = get_and_scale(view, var, idx)
				h2 = get_and_scale(view, var, idrange[pos+1])
				for sbin, cbin, h1bin, h2bin in zip(sqsum, central, h1, h2):
					d1 = abs(cbin.value-h1bin.value)
					d2 = abs(cbin.value-h2bin.value)					
					sbin.value = quad.quad(sbin.value, max(d1,d2))

			for sbin, cbin in zip(sqsum, central):				
				cbin.error = sbin.value
			return to_up_down(central)

		if pdf == 'nnpdf':
			up, down = full_envelope(view, var, nnpdf)
			alpha_up = get_and_scale(view, var, 110)
			alpha_dw = get_and_scale(view, var, 109)
			ret_up = central.Clone()
			ret_dw = central.Clone()
			for idx in range(len(central)):
				pdfu_delta = up[idx].value - central[idx].value
				lphu_delta = alpha_up[idx].value - central[idx].value
				ret_up[idx].value += quad.quad(pdfu_delta, lphu_delta)
				pdfd_delta = down[idx].value - central[idx].value
				lphd_delta = alpha_dw[idx].value - central[idx].value
				ret_dw[idx].value -= quad.quad(pdfd_delta, lphd_delta)
			return ret_up, ret_dw
		else:
			raise RuntimeError('invalid pdf type')
Beispiel #11
0
data = template_hist.Clone()
data.title = 'data_obs'

for categories in groups.itervalues():
   #check that all categories have identical bin index
   assert(len(set(binning[i]['idx'] for i in categories)) <= 1)
   bin_idx = binning[categories[0]]['idx']+1
   for sample in samples:
      val = sum(
         norms['%s/%s' % (i, sample)].value 
         for i in categories
         if '%s/%s' % (i, sample) in norms
         )
      err = quad(*[
            norms['%s/%s' % (i, sample)].error
            for i in categories
            if '%s/%s' % (i, sample) in norms
            ])
      log.debug("Setting value of %s bin %i to %.2f +/- %.2f" % (sample, bin_idx, val, err))
      fit_histos[sample][bin_idx].value = val
      fit_histos[sample][bin_idx].error = err
   #WARNING! this will break if the jet categories have 
   #different disciminator binning!
   data_of_cat = sum(
      datacard_file.Get('%s/data_obs' % i) 
      for i in categories      
      )
   integral_err = ROOT.Double()
   data[bin_idx].value = data_of_cat.IntegralAndError(1, data_of_cat.nbins(), integral_err)
   data[bin_idx].error = integral_err