Beispiel #1
0
def plot_cmssw_charge_occupancy_variations(REGION, useFullDist=True):
    '''
	Data highOcc vs SCD vs SCD Po(N)
	'''
    make_folder_if_not_exists('plots/InputChargePoissonMatching/')
    OCC = REGION_DETAILS[REGION]['Occ_Data']

    hs_to_plot = []
    rs_to_plot = []
    occupancies_to_test = []
    colors = ['blue', 'red', 'darkgreen', 'magenta']

    if useFullDist:
        f_hist = 'input/landau_scd_290118_orig.root'
        occupancies_to_test = [OCC * 10, OCC * 20, OCC * 50, OCC * 100]
    else:
        f_hist = 'input/landau_scd_290118_cut.root'
        occupancies_to_test = [OCC * 5, OCC * 10, OCC * 20, OCC * 50]

    with root_open(f_hist) as f:
        h = asrootpy(f.Get(REGION).Clone())
        h.SetDirectory(0)
        l_edges = list(h.xedges())

    # with root_open('input/landau_lowPUTracks_290118_orig.root') as f:
    # 	h_data_PU0 = asrootpy(f.Get(REGION).Clone())
    # 	h_data_PU0.SetDirectory(0)
    # 	h_data_PU0.Scale(1 /h_data_PU0.integral(xbin1=21,xbin2=200))
    # 	hs_to_plot.append({
    # 		'hist'		: h_data_PU0,
    # 		'label' 	: 'lowPU Strips from Tracks',
    # 		# 'color'		: '0.5',
    # 		'color'		: 'grey',
    # 		'type' 		: 'Data'
    # 	})

    # with root_open('input/landau_lowPUClusters_290118_orig.root') as f:
    # 	h_data_lowOcc = asrootpy(f.Get(REGION).Clone())
    # 	h_data_lowOcc.SetDirectory(0)
    # 	h_data_lowOcc.Scale(1 /h_data_lowOcc.integral(xbin1=21,xbin2=200))
    # 	hs_to_plot.append({
    # 		'hist'		: h_data_lowOcc,
    # 		'label' 	: 'lowOcc Strips from Clusters',
    # 		# 'color'		: '0',
    # 		'color'		: 'brown',
    # 		'type' 		: 'Data'
    # 	})

    with root_open(
            'input/landau_clusterData_010218_VFPFix_True_orig.root') as f:
        h_data_highOcc = asrootpy(f.Get(REGION).Clone())
        h_data_highOcc.SetDirectory(0)
        h_data_highOcc.Scale(1 / h_data_highOcc.integral(xbin1=21, xbin2=200))
        hs_to_plot.append({
            'hist': h_data_highOcc,
            'label': 'highOcc Strips from Clusters',
            # 'color'		: '0',
            'color': 'black',
            'type': 'Data'
        })

    hist_scd = Hist(l_edges)

    h_tests = []
    for occ, col in zip(occupancies_to_test, colors):
        h_tmp = {
            'occ': occ,
            'hist': Hist(l_edges),
            'color': col,
        }
        h_tests.append(h_tmp)

    i = 0
    while i < 250000:
        v = return_strip_charge_from_Poisson(h,
                                             OCC,
                                             add_noise=False,
                                             add_truncation=True)
        hist_scd.Fill(v)
        for h_test in h_tests:
            v_occ = return_strip_charge_from_Poisson(h,
                                                     h_test['occ'],
                                                     add_noise=False,
                                                     add_truncation=True,
                                                     cut_charge=True)
            if not v_occ < 10000: h_test['hist'].Fill(v_occ)
        i = i + 1

    # Normalising between 10000-100000
    hist_scd.Scale(1 / hist_scd.integral(xbin1=21, xbin2=200))
    for h_test in h_tests:
        h_test['hist'].Scale(1 / h_test['hist'].integral(xbin1=21, xbin2=200))

    scd = ''
    if useFullDist:
        scd = 'SCD'
    else:
        scd = 'Cut SCD'

    r_scd = h_data_highOcc.Clone()
    r_scd.SetDirectory(0)
    r_scd.Divide(hist_scd)
    hs_to_plot.append({
        'hist':
        hist_scd,
        'label':
        '{SCD} sampled Po({OCC})'.format(SCD=scd, OCC=OCC),
        'color':
        'black',
        'type':
        'SCD'
    })
    rs_to_plot.append({
        'hist': r_scd,
        'label': '',
        'color': 'black',
        'type': 'Ratio'
    })
    for h_test in h_tests:
        r = h_data_highOcc.Clone()
        r.SetDirectory(0)
        r.Divide(h_test['hist'])
        hs_to_plot.append({
            'hist':
            h_test['hist'],
            'label':
            '{SCD} sampled Po({OCC})'.format(SCD=scd, OCC=h_test['occ']),
            'color':
            h_test['color'],
            'line':
            'solid',
            'type':
            'CMSSW'
        })
        rs_to_plot.append({
            'hist': r,
            'label': '',
            'color': h_test['color'],
            'type': 'Ratio'
        })

    fig = plt.figure()
    fig.suptitle(
        "Charge Deposition from Simulation using Sampling from {SCD} distribution"
        .format(SCD=scd),
        fontsize=14,
        fontweight='bold')

    gs = gridspec.GridSpec(2,
                           1,
                           height_ratios=[5, 1],
                           wspace=0.025,
                           hspace=0.025)
    ax = plt.subplot(gs[0])
    plt.title(REGION.replace("_", " ") + " normalised 10,000-70,000",
              loc='right')

    for h_info in hs_to_plot:
        if 'Data' in h_info['type']:
            rplt.hist(
                h_info['hist'],
                label=h_info['label'],
                color=h_info['color'],
                alpha=0.35,
                fill=True,
                zorder=0,
            )
        if 'SCD' in h_info['type']:
            rplt.errorbar(
                h_info['hist'],
                label=h_info['label'],
                markerfacecolor=h_info['color'],
                markersize=3,
                xerr=False,
                yerr=False,
                elinewidth=0,
                emptybins=False,
                zorder=len(hs_to_plot),
            )
        if 'CMSSW' in h_info['type']:
            rplt.step(h_info['hist'],
                      label=h_info['label'],
                      color=h_info['color'],
                      linestyle=h_info['line'])

    ax.set_ylim([0.0001, 1.])
    # ax.set_xlim([10000,70000])
    ax.set_xlim([0.1, 70000])
    ax.set_yscale("log", nonposy='clip')
    ax.set_ylabel('N')
    # ax.set_xlabel('Charge (e)')
    plt.setp(ax.get_xticklabels(), visible=False)

    leg = ax.legend(loc='upper right', numpoints=1, prop={'size': 10}, ncol=2)
    leg.draw_frame(False)

    ax_ratio = plt.subplot(gs[1])
    ax_ratio.axhline(1, color='black')
    for r_info in rs_to_plot:
        rplt.errorbar(
            r_info['hist'],
            label=r_info['label'],
            markerfacecolor=r_info['color'],
            markersize=6,
            markeredgewidth=0,
            xerr=False,
            yerr=False,
            elinewidth=0,
            emptybins=False,
            axes=ax_ratio,
        )

    ax_ratio.set_ylim([0, 2])
    ax_ratio.set_xlim([0.1, 70000])
    # ax_ratio.set_xlim([10000,70000])
    ax_ratio.set_xlabel('Charge deposited on APV in a bx(e) ')
    ax_ratio.set_ylabel(
        r'$\frac{\mathrm{Data\ High\ Occ}}{\mathrm{SCD\ Sampling}}$')

    gs.tight_layout(fig, rect=[0, 0.03, 1, 0.95])
    filename = 'CMSSWSimChargeFrom{SCD}_'.format(
        SCD=scd.replace(" ", "")) + REGION
    fig.savefig('plots/InputChargePoissonMatching/' + filename + '.pdf',
                bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()
Beispiel #2
0
def test_init_from_hist():
    h = Hist(100, -10, 10)
    h.FillRandom('gaus')
    g = Graph(h)
        in_file = root_open(f, "READ")
        out_file_path = f + ".hists"
        if args.output_folder != '':
            ensure_dir(args.output_folder)
            out_file_path = os.path.join(args.output_folder,
                                         os.path.basename(f) + ".hists")
        if (args.doNotOverwrite and os.path.isfile(out_file_path)): continue
        out_file = root_open(out_file_path, "RECREATE")
        tree = in_file.get(args.treename)
        if (args.hists != None and len(args.hists) > 0):
            out_file.cd()
            for hist in args.hists:
                in_file.Get(hist).Write()
        if args.copy_mbj_cutflow:
            cf = in_file.Get('cut_flow')
            cf2 = Hist(1, 0, 1, name='cutflow')
            cf2.Sumw2()
            cf2.SetEntries(cf.GetBinContent(1))
            cf2.SetBinContent(1, cf.GetBinContent(2))
            cf2.SetBinError(
                1,
                np.sqrt(cf.GetBinContent(1)) * cf.GetBinContent(2) /
                cf.GetBinContent(1))
            allDir = os.path.join(args.outdir, 'all')
            out_file.mkdir(allDir, recurse=True)
            try:
                out_file.cd(allDir)
            except:
                pass
            cf2.Write()
Beispiel #4
0
def test_poisson_errors():
    h = Hist(20, -3, 3)
    h.FillRandom('gaus')
    g = h.poisson_errors()
Beispiel #5
0
def test_width():
    h = Hist([1, 2, 4, 8])
    assert_equal(list(h.xwidth()), [1, 2, 4])
    assert_equal(list(h.xwidth(overflow=True)),
                 [float('inf'), 1, 2, 4, float('inf')])
'''
Created on 8 Nov 2012

@author: kreczko
'''
# @BROKEN
from dps.utils.Fitting import CurveFit
import numpy as np
from rootpy.plotting import Hist
import rootpy.plotting.root2matplotlib as rplt
import matplotlib.pyplot as plt

# create normal distributions
mu1, sigma1 = 100, 15
x1 = mu1 + sigma1 * np.random.randn(10000)

h1 = Hist(100, 40, 200)
map(h1.Fill, x1)

hist_fit, bin_centres = CurveFit.fit(h1, 'gaus', [10000,mu1,sigma1])
fig = plt.figure(figsize=(16, 10), dpi=100, facecolor='white')
rplt.hist(h1, label=r'data', alpha = 0.7)
plt.plot(bin_centres, hist_fit, label='Fitted data')
plt.legend()
plt.savefig('plots/CurveFit.png')
Beispiel #7
0
def test_slice_assign_bad():
    hist = Hist(10, 0, 1)
    hist[:] = range(len(hist) + 1)
#!/usr/bin/env python
import numpy as np
from rootpy.plotting import Hist, HistStack
import rootpy.root2matplotlib as rplt
import matplotlib.pyplot as plt

# create normal distributions
mu1, mu2, sigma = 100, 140, 15
x1 = mu1 + sigma * np.random.randn(10000)
x2 = mu2 + sigma * np.random.randn(10000)

# create histograms
h1 = Hist(100, 40, 160)
h2 = Hist(100, 40, 160)

# fill the histograms with our distributions
map(h1.Fill, x1)
map(h2.Fill, x2)

# normalize the histograms
h1 /= h1.Integral()
h2 /= h2.Integral()

# set visual attributes
h1.SetFillStyle('solid')
h1.SetFillColor('green')
h1.SetLineColor('green')

h2.SetFillStyle('solid')
h2.SetFillColor('red')
h2.SetLineColor('red')
Beispiel #9
0
print delimeter
print ' - time length:     ' + str(
    sci_trigger_r_obj.gps_time_length) + ' seconds'
print ' - bin width:       ' + str(args.bw) + ' seconds'
print ' - number of bins:  ' + str(nbins)
print delimeter

ROOT.gStyle.SetOptStat(0)

gps_time_span = '%d:%d => %d:%d' % (
    int(sci_trigger_r_obj.start_week), int(sci_trigger_r_obj.start_second),
    int(sci_trigger_r_obj.stop_week), int(sci_trigger_r_obj.stop_second))
trigger_hist = Hist(nbins,
                    0,
                    sci_trigger_r_obj.gps_time_length,
                    type='D',
                    name='trigger_hist',
                    title='trigger: { ' + gps_time_span + ' }')
modules_hist = [
    Hist(nbins,
         0,
         sci_trigger_r_obj.gps_time_length,
         type='D',
         name='module_hist_' + str(i),
         title='module CT_' + str(i) + ': { ' + gps_time_span + ' }')
    for i in xrange(1, 26)
]
trigger_hist.SetDirectory(None)
trigger_hist.SetMinimum(0)
trigger_hist.color = 'red'
trigger_hist.markerstyle = 3
Beispiel #10
0
matplotlib.use('Agg')


# rootpy fod matplotlib
import rootpy.plotting.root2matplotlib as rplt
import matplotlib.pyplot as plt

#pyroot
import ROOT

## --file name, Tree name
file_name = "ntuple.root"
tree_name = "mytree"

## --Hist define
h1 = Hist(200, 82, 100)

## --open file and read tree
root_file = ROOT.TFile.Open(file_name,'read')
tree = root_file.Get(tree_name)

## --start eve. loop
for Nevent, event in enumerate(tree):
        print(Nevent, event.signal)
        h1.Fill(event.signal)

## --close file
root_file.Close()


## --set parametres for plotting
Beispiel #11
0
def main():
    print("Number of arguments: ", len(sys.argv), "arguments.")
    filename = sys.argv[1]
    print("Input file: {}".format(filename))

    jetBinBorders = [5, 10, 20, 30, 40, 60, 80, 100, 150, 500]
    jetPtBins = [(a, b) for a, b in zip(jetBinBorders, jetBinBorders[1:])]
    JetPtCenter = [7.5, 15, 25, 35, 50, 70, 90, 125, 325]
    JetPtError = [2.5, 5, 5, 5, 10, 10, 10, 25, 175]
    Njets = len(jetBinBorders) - 1
    Njets = 9
    f = root_open(filename, "read")
    if len(sys.argv) > 2:
        filename_test = sys.argv[2]
        f_test = root_open(filename_test)
        print("Open {} for test data".format(filename_test))
    else:
        f_test = root_open(filename)
    if len(sys.argv) < 4:
        f_data = None
    else:
        filename_data = sys.argv[3]
        f_data = root_open(filename_data)
        if len(sys.argv) > 4:
            if sys.argv[4] == "MB":
                do_mb = True
                do_triggered = False
            elif sys.argv[4] == "Triggered":
                do_mb = False
                do_triggered = True
            else:
                do_mb = False
                do_triggered = False

    nR = 3
    iFinder = 0
    iMCFinder = iFinder + nR * 2

    base_folder = "AliJJetJtTask/AliJJetJtHistManager"
    base_folder_trig = "AliJJetJtTask_kEMCEJE/AliJJetJtHistManager"
    base_folder_MC = "AliJJetJtTask/AliJJetJtMCHistManager"

    if f_data:
        numberJetsDataMB = get_numbers(f_data, base_folder, "JetPtBin",
                                       iFinder, Njets)

        numberJetsDataTriggered = get_numbers(f_data, base_folder_trig,
                                              "JetPtBin", iFinder, Njets)

        hTrackJtDataMB = get_hists(f_data, base_folder, "JetConeJtWeightBin",
                                   iFinder, Njets)
        hTrackJtDataTriggered = get_hists(f_data, base_folder_trig,
                                          "JetConeJtWeightBin", iFinder, Njets)

        hTrackJt2DDataMB = get_hists(f_data, base_folder, "JtWeight2D",
                                     iFinder)
        hTrackJt2DDataTriggered = get_hists(f_data, base_folder_trig,
                                            "JtWeight2D", iFinder)

        hJetPtDataMBCoarse = Hist(jetBinBorders)
        for n, i in zip(numberJetsDataMB, range(1, Njets + 1)):
            hJetPtDataMBCoarse.SetBinContent(i, n)

        hJetPtDataTriggeredCoarse = Hist(jetBinBorders)
        for n, i in zip(numberJetsDataTriggered, range(1, Njets + 1)):
            hJetPtDataTriggeredCoarse.SetBinContent(i, n)

        hJetPtDataMB = get_hists(f_data, base_folder, "JetPt", iFinder)
        hJetPtDataTriggered = get_hists(f_data, base_folder_trig, "JetPt",
                                        iFinder)

        # Get Background distributions
        hBgJtDataMB = get_hists(f_data, base_folder, "BgJtWeightBin", iFinder,
                                Njets)
        hBgJtDataTriggered = get_hists(f_data, base_folder_trig,
                                       "BgJtWeightBin", iFinder, Njets)

        # Get number of background jets
        hBgNumbersDataMB = get_numbers(f_data, base_folder, "BgTrkNumberBin",
                                       iFinder, Njets)

        hBgNumbersDataTriggered = get_numbers(f_data, base_folder_trig,
                                              "BgTrkNumberBin", iFinder, Njets)

    numberJetsMeas = get_numbers(f_test, base_folder, "JetPtBin", iFinder,
                                 Njets)

    numberJetsTrue = get_numbers(f_test, base_folder, "JetPtBin", iMCFinder,
                                 Njets)

    hTrackJtMeas = get_hists(f_test, base_folder, "JetConeJtWeightBin",
                             iFinder, Njets)
    hTrackJtTrue = get_hists(f_test, base_folder, "JetConeJtWeightBin",
                             iMCFinder, Njets)

    numberJetsMeasTrain = get_numbers(f, base_folder, "JetPtBin", iFinder,
                                      Njets)

    hTrackJtCorr2D = get_hists(f, base_folder_MC, "TrackJtCorr2D", iFinder,
                               Njets)
    hTrackJtMeas2D = get_hists(f_test, base_folder, "JtWeight2D", iFinder)
    hTrackJtTrue2D = get_hists(f_test, base_folder, "JtWeight2D", iMCFinder)
    hTrackJtMisses2D = get_hists(f, base_folder_MC, "TrackJtMisses2D", iFinder)
    hTrackJtFakes2D = get_hists(f, base_folder, "JetConeJtUnfBg2D", iFinder)
    hTrackJtBg = get_hists(f, base_folder, "BgJtWeightBin", iFinder, Njets)

    hTrackJtBgNumber = get_numbers(f, base_folder, "BgTrkNumberBin", iFinder,
                                   Njets)
    # Get number of background jets

    hTrackMatchSuccess = get_hists(f, base_folder_MC, "TrackMatchSuccess",
                                   iFinder, Njets)

    LogBinsJt = [
        hTrackJtMeas2D.GetXaxis().GetBinLowEdge(iBin)
        for iBin in range(1,
                          hTrackJtMeas2D.GetNbinsX() + 2)
    ]

    for h in hTrackJtCorr2D:
        print("{}".format(h.GetTitle()))
    hJetPtMeas = get_hists(f_test, base_folder, "JetPt", iFinder)
    hJetPtMeasCoarse = Hist(jetBinBorders)
    for n, i in zip(numberJetsMeas, range(1, Njets + 1)):
        hJetPtMeasCoarse.SetBinContent(i, n)

    hJetPtTrue = get_hists(f_test, base_folder, "JetPt", iMCFinder)
    hJetPtTrueCoarse = Hist(jetBinBorders)
    for n, i in zip(numberJetsTrue, range(1, Njets + 1)):
        hJetPtTrueCoarse.SetBinContent(i, n)
    LogBinsPt = [
        hJetPtTrue.GetXaxis().GetBinLowEdge(iBin)
        for iBin in range(1,
                          hJetPtTrue.GetNbinsX() + 2)
    ]
    hJetPtResponse = get_hists(f, base_folder_MC, "JetPtCorr", iFinder)
    hJetPtResponseCoarse = get_hists(f, base_folder_MC, "JetPtCorrCoarse",
                                     iFinder)

    if False:
        TrackJtUnfolder = JtUnfolder.JtUnfolder(
            "TrackJtUnfolder",
            jetBinBorders=jetBinBorders,
            Njets=Njets,
            Iterations=5,
            Data=True,
        )
        TrackJtUnfolder.setTrackMatch(hTrackMatchSuccess)
        # TrackJtUnfolder.drawTrackMatch("TrackMatch",'single')
        TrackJtUnfolder.setPtBins(LogBinsPt)
        TrackJtUnfolder.setJtBins(LogBinsJt)
        TrackJtUnfolder.setJtMeas2D(hTrackJtMeas2D)
        TrackJtUnfolder.setJtTestMeas2D(hTrackJtMeas2D)
        TrackJtUnfolder.setJtTrue2D(hTrackJtTrue2D)
        TrackJtUnfolder.setJtTestTrue2D(hTrackJtTrue2D)
        TrackJtUnfolder.setMisses2D(hTrackJtMisses2D)
        TrackJtUnfolder.setFakes2D(hTrackJtFakes2D)
        TrackJtUnfolder.setJetPtMeas(hJetPtMeas)
        TrackJtUnfolder.setJetPtTrue(hJetPtTrue)
        TrackJtUnfolder.setJetPtMeasCoarse(hJetPtMeasCoarse)
        TrackJtUnfolder.setJetPtTrueCoarse(hJetPtTrueCoarse)
        TrackJtUnfolder.setJetPtResponse(
            createResponseInverse(hJetPtMeas, hJetPtResponse))
        TrackJtUnfolder.setJetPtResponseCoarse(
            createResponseInverse(hJetPtMeasCoarse, hJetPtResponseCoarse))
        TrackJtUnfolder.setNumberJetsMeas(numberJetsMeas)
        TrackJtUnfolder.setNumberJetsTrue(numberJetsTrue)
        TrackJtUnfolder.setNumberJetsTestMeas(numberJetsMeas)
        TrackJtUnfolder.setNumberJetsTestTrue(numberJetsTrue)
        TrackJtUnfolder.setNumberJetsMeasTrain(sum(numberJetsMeasTrain))
        TrackJtUnfolder.set2Dresponse(hTrackJtCorr2D)
        TrackJtUnfolder.setJtBackground(hTrackJtBg)
        TrackJtUnfolder.setJtBackgroundNumbers(hTrackJtBgNumber)
        TrackJtUnfolder.unfold()
        TrackJtUnfolder.write_files("dataUnfolded.root")
        # TrackJtUnfolder.plotResponse()
        # TrackJtUnfolder.plotJetPt()
        # TrackJtUnfolder.plotJt("PythiaTest", Rebin=4)
        return

    if f_data:
        split_file = splitext(split(filename_data)[1])
        suffix = "_unfolded"
        if do_mb:
            suffix += "_MB"
        if do_triggered:
            suffix += "_triggered"
        output_file = "output/" + split_file[0] + suffix + split_file[1]

        if not os.path.exists('output'):
            os.makedirs('output')

        if do_mb:
            MBDataJtUnfolder = JtUnfolder.JtUnfolder(
                "MBDataUnfolder",
                jetBinBorders=jetBinBorders,
                Njets=Njets,
                Data=True,
                Iterations=5,
            )
            MBDataJtUnfolder.setPtBins(LogBinsPt)
            MBDataJtUnfolder.setJtBins(LogBinsJt)
            MBDataJtUnfolder.setJtMeas2D(hTrackJt2DDataMB)
            MBDataJtUnfolder.setJetPtMeas(hJetPtDataMB)
            MBDataJtUnfolder.setJetPtMeasCoarse(hJetPtDataMBCoarse)
            MBDataJtUnfolder.setNumberJetsMeas(numberJetsDataMB)
            MBDataJtUnfolder.setJtBackground(hBgJtDataMB)
            MBDataJtUnfolder.setJtBackgroundNumbers(hBgNumbersDataMB)

            MBDataJtUnfolder.setJtTrue2D(hTrackJtTrue2D)
            MBDataJtUnfolder.setJtTestTrue2D(hTrackJtTrue2D)
            MBDataJtUnfolder.setFakes2D(hTrackJtFakes2D)
            MBDataJtUnfolder.setMisses2D(hTrackJtMisses2D)
            MBDataJtUnfolder.setJetPtResponse(
                createResponseInverse(hJetPtMeas, hJetPtResponse))
            MBDataJtUnfolder.setJetPtResponseCoarse(
                createResponseInverse(hJetPtMeasCoarse, hJetPtResponseCoarse))
            MBDataJtUnfolder.setNumberJetsMeasTrain(sum(numberJetsMeasTrain))
            MBDataJtUnfolder.set2Dresponse(hTrackJtCorr2D)
            MBDataJtUnfolder.unfold()
            MBDataJtUnfolder.write_files(output_file)
            # MBDataJtUnfolder.plotJt("MBDataUnfolded", Rebin=4)

        if do_triggered:
            TriggeredDataJtUnfolder = JtUnfolder.JtUnfolder(
                "TriggeredDataUnfolder",
                jetBinBorders=jetBinBorders,
                Njets=Njets,
                Data=True,
                Iterations=5,
            )
            TriggeredDataJtUnfolder.setPtBins(LogBinsPt)
            TriggeredDataJtUnfolder.setJtBins(LogBinsJt)
            TriggeredDataJtUnfolder.setJtMeas2D(hTrackJt2DDataTriggered)
            TriggeredDataJtUnfolder.setJetPtMeas(hJetPtDataTriggered)
            TriggeredDataJtUnfolder.setJetPtMeasCoarse(
                hJetPtDataTriggeredCoarse)
            TriggeredDataJtUnfolder.setNumberJetsMeas(numberJetsDataTriggered)
            TriggeredDataJtUnfolder.setJtBackground(hBgJtDataTriggered)
            TriggeredDataJtUnfolder.setJtBackgroundNumbers(
                hBgNumbersDataTriggered)

            TriggeredDataJtUnfolder.setJtTrue2D(hTrackJtTrue2D)
            TriggeredDataJtUnfolder.setJtTestTrue2D(hTrackJtTrue2D)
            TriggeredDataJtUnfolder.setFakes2D(hTrackJtFakes2D)
            TriggeredDataJtUnfolder.setMisses2D(hTrackJtMisses2D)
            TriggeredDataJtUnfolder.setJetPtResponse(
                createResponseInverse(hJetPtMeas, hJetPtResponse))
            TriggeredDataJtUnfolder.setJetPtResponseCoarse(
                createResponseInverse(hJetPtMeasCoarse, hJetPtResponseCoarse))
            TriggeredDataJtUnfolder.setNumberJetsMeasTrain(
                sum(numberJetsMeasTrain))
            TriggeredDataJtUnfolder.set2Dresponse(hTrackJtCorr2D)
            TriggeredDataJtUnfolder.unfold()
            TriggeredDataJtUnfolder.write_files(output_file)
def extract_JetBTag(f_tt, f_qcd, f_wjet):

    # get trees from files
    t_tt = f_tt.Get("Delphes")
    t_qcd = f_qcd.Get("Delphes")
    t_wjet = f_wjet.Get("Delphes")

    # get number of entries
    tt_n_entries = t_tt.GetEntries()
    qcd_n_entries = t_qcd.GetEntries()
    wjet_n_entries = t_wjet.GetEntries()

    # define leaves
    var_tt = "Jet.BTag"
    var_qcd = "Jet.BTag"
    var_wjet = "Jet.BTag"

    leaf_tt = t_tt.GetLeaf(var_tt)
    leaf_qcd = t_qcd.GetLeaf(var_qcd)
    leaf_wjet = t_wjet.GetLeaf(var_wjet)

    # create the histograms
    loose_tt = Hist(NBINS, NLO, NHI, title='loose_tt', legendstyle='L')
    medium_tt = Hist(NBINS, NLO, NHI, title='medium_tt', legendstyle='L')
    tight_tt = Hist(NBINS, NLO, NHI, title='tight_tt', legendstyle='L')

    loose_qcd = Hist(NBINS, NLO, NHI, title='loose_qcd', legendstyle='L')
    medium_qcd = Hist(NBINS, NLO, NHI, title='medium_qcd', legendstyle='L')
    tight_qcd = Hist(NBINS, NLO, NHI, title='tight_qcd', legendstyle='L')

    loose_wjet = Hist(NBINS, NLO, NHI, title='loose_wjet', legendstyle='L')
    medium_wjet = Hist(NBINS, NLO, NHI, title='medium_wjet', legendstyle='L')
    tight_wjet = Hist(NBINS, NLO, NHI, title='tight_wjet', legendstyle='L')

    # FILLING THE TREE
    fill_JetBTag_tree(tt_n_entries, t_tt, leaf_tt, loose_tt, medium_tt,
                      tight_tt)
    fill_JetBTag_tree(qcd_n_entries, t_qcd, leaf_qcd, loose_qcd, medium_qcd,
                      tight_qcd)
    fill_JetBTag_tree(wjet_n_entries, t_wjet, leaf_wjet, loose_wjet,
                      medium_wjet, tight_wjet)

    #set line colors
    loose_tt.SetLineColor('blue')
    medium_tt.SetLineColor('green')
    tight_tt.SetLineColor('red')

    loose_qcd.SetLineColor('blue')
    medium_qcd.SetLineColor('green')
    tight_qcd.SetLineColor('red')

    loose_wjet.SetLineColor('blue')
    medium_wjet.SetLineColor('green')
    tight_wjet.SetLineColor('red')

    #begin drawing stuff
    c1 = Canvas()
    tight_tt.SetStats(0)
    tight_tt.Draw('HIST')
    medium_tt.Draw('HIST SAME')
    loose_tt.Draw('HIST SAME')

    #make legend
    l1 = Legend([tight_tt, medium_tt, loose_tt], textfont=42, textsize=.03)
    l1.Draw()

    #save as pdf
    c1.SaveAs("../plots/JetBTag_plots/btag_tt.pdf")

    c2 = Canvas()
    tight_qcd.SetStats(0)
    tight_qcd.Draw('HIST')
    medium_qcd.Draw('HIST SAME')
    loose_qcd.Draw('HIST SAME')

    #make legend
    l2 = Legend([tight_qcd, medium_qcd, loose_qcd], textfont=42, textsize=.03)
    l2.Draw()

    #save as pdf
    c2.SaveAs("../plots/JetBTag_plots/btag_qcd.pdf")

    c3 = Canvas()
    tight_wjet.SetStats(0)
    tight_wjet.Draw('HIST')
    medium_wjet.Draw('HIST SAME')
    loose_wjet.Draw('HIST SAME')

    #make legend
    l3 = Legend([tight_wjet, medium_wjet, loose_wjet],
                textfont=42,
                textsize=.03)
    l3.Draw()

    #save as pdf
    c3.SaveAs("../plots/JetBTag_plots/btag_wjet.pdf")

    wait(True)
Beispiel #13
0
#!/usr/bin/env python
"""
============================
Working with ROOT histograms
============================

This example demonstrates how to create and work with ROOT histogram in rootpy.
"""
print __doc__
from rootpy.plotting import Hist, Hist2D, Hist3D, HistStack, Legend, Canvas
from rootpy.interactive import wait
import random

# create a simple 1D histogram with 10 constant-width bins between 0 and 1
h_simple = Hist(10, 0, 1)
print h_simple.name

# If the name is not specified, a UUID is used so that ROOT never complains
# about two histograms having the same name.
# Alternatively you can specify the name (and the title or any other style
# attributes) in the constructor:
h_simple = Hist(10,
                -4,
                12,
                name='my hist',
                title='Some Data',
                drawstyle='hist',
                legendstyle='F',
                fillstyle='/')

# fill the histogram
Beispiel #14
0
 def __init__(self, name, bins, threshold):
     self._pass = Hist(bins, name=name + '_pass')
     self._total = Hist(bins, name=name + '_total')
     self._dist = Hist(bins, name=name + '_dist')
     self._threshold = threshold
     self._efficiency = None
    def ln_likelihood_full_matching(self,
                                    w_value,
                                    alpha,
                                    zeta,
                                    beta,
                                    gamma,
                                    delta,
                                    kappa,
                                    eta,
                                    lamb,
                                    g1_value,
                                    extraction_efficiency,
                                    gas_gain_value,
                                    gas_gain_width,
                                    spe_res,
                                    s1_acc_par0,
                                    s1_acc_par1,
                                    s2_acc_par0,
                                    s2_acc_par1,
                                    scale_par,
                                    d_gpu_local_info,
                                    draw_fit=False):

        # -----------------------------------------------
        # -----------------------------------------------
        # determine prior likelihood and variables
        # -----------------------------------------------
        # -----------------------------------------------

        prior_ln_likelihood = 0
        matching_ln_likelihood = 0

        # get w-value prior lieklihood
        prior_ln_likelihood += self.get_ln_prior_w_value(w_value)

        # priors of detector variables
        prior_ln_likelihood += self.get_ln_prior_g1(g1_value)
        prior_ln_likelihood += self.get_ln_prior_extraction_efficiency(
            extraction_efficiency)
        prior_ln_likelihood += self.get_ln_prior_gas_gain_value(gas_gain_value)
        prior_ln_likelihood += self.get_ln_prior_gas_gain_width(gas_gain_width)
        prior_ln_likelihood += self.get_ln_prior_spe_res(spe_res)

        prior_ln_likelihood += self.get_ln_prior_s1_acc_pars(
            s1_acc_par0, s1_acc_par1)
        prior_ln_likelihood += self.get_ln_prior_s2_acc_pars(
            s2_acc_par0, s2_acc_par1)

        # get priors from lindhard parameters
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(alpha)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(beta)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(gamma)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(kappa)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(eta)
        prior_ln_likelihood += self.get_ln_prior_par_greater_than_zero(lamb)

        prior_ln_likelihood += self.get_ln_prior_zeta(zeta)
        prior_ln_likelihood += self.get_ln_prior_delta(delta)

        # if prior is -inf then don't bother with MC
        #print 'removed prior infinity catch temporarily'
        if not np.isfinite(prior_ln_likelihood) and not draw_fit:
            return -np.inf

        # -----------------------------------------------
        # -----------------------------------------------
        # run MC
        # -----------------------------------------------
        # -----------------------------------------------

        num_trials = np.asarray(self.num_mc_events, dtype=np.int32)
        num_repetitions = np.asarray(d_gpu_local_info['num_repetitions'],
                                     dtype=np.int32)
        mean_field = np.asarray(self.d_data_parameters['mean_field'],
                                dtype=np.float32)

        w_value = np.asarray(w_value, dtype=np.float32)
        alpha = np.asarray(alpha, dtype=np.float32)
        zeta = np.asarray(zeta, dtype=np.float32)
        beta = np.asarray(beta, dtype=np.float32)
        gamma = np.asarray(gamma, dtype=np.float32)
        delta = np.asarray(delta, dtype=np.float32)
        kappa = np.asarray(kappa, dtype=np.float32)
        eta = np.asarray(eta, dtype=np.float32)
        lamb = np.asarray(lamb, dtype=np.float32)

        g1_value = np.asarray(g1_value, dtype=np.float32)
        extraction_efficiency = np.asarray(extraction_efficiency,
                                           dtype=np.float32)
        gas_gain_value = np.asarray(gas_gain_value, dtype=np.float32)
        gas_gain_width = np.asarray(gas_gain_width, dtype=np.float32)
        spe_res = np.asarray(spe_res, dtype=np.float32)

        s1_acc_par0 = np.asarray(s1_acc_par0, dtype=np.float32)
        s1_acc_par1 = np.asarray(s1_acc_par1, dtype=np.float32)

        s2_acc_par0 = np.asarray(s2_acc_par0, dtype=np.float32)
        s2_acc_par1 = np.asarray(s2_acc_par1, dtype=np.float32)

        # for histogram binning
        num_bins_s1 = np.asarray(self.s1_settings[0], dtype=np.int32)
        num_bins_s2 = np.asarray(self.s2_settings[0], dtype=np.int32)

        a_hist_2d = np.zeros(self.s1_settings[0] * self.s2_settings[0],
                             dtype=np.int32)

        #print d_gpu_local_info['d_gpu_energy'][degree_setting]

        l_gpu_args = (d_gpu_local_info['rng_states'], drv.In(num_trials),
                      drv.In(num_repetitions), drv.In(mean_field),
                      d_gpu_local_info['gpu_energies'], drv.In(w_value),
                      drv.In(alpha), drv.In(zeta), drv.In(beta), drv.In(gamma),
                      drv.In(delta), drv.In(kappa), drv.In(eta), drv.In(lamb),
                      drv.In(g1_value), drv.In(extraction_efficiency),
                      drv.In(gas_gain_value), drv.In(gas_gain_width),
                      drv.In(spe_res), drv.In(s1_acc_par0),
                      drv.In(s1_acc_par1), drv.In(s2_acc_par0),
                      drv.In(s2_acc_par1), drv.In(num_bins_s1),
                      d_gpu_local_info['gpu_bin_edges_s1'],
                      drv.In(num_bins_s2),
                      d_gpu_local_info['gpu_bin_edges_s2'],
                      drv.InOut(a_hist_2d))

        #print '\n\n\nBefore call...'
        #print d_gpu_local_info
        d_gpu_local_info['function_to_call'](*l_gpu_args, **self.d_gpu_scale)
        #print 'After call...\n\n\n'

        a_s1_s2_mc = np.reshape(a_hist_2d,
                                (self.s2_settings[0], self.s1_settings[0])).T

        #print list(a_s1_s2_mc)

        sum_mc = np.sum(a_s1_s2_mc, dtype=np.float32)
        if sum_mc == 0:
            #print 'sum mc == 0'
            return -np.inf

        # this forces our scale to be close to 1 (difference comes from acceptance)
        a_s1_s2_mc = np.multiply(
            a_s1_s2_mc,
            float(scale_par) * len(self.a_data_s1) /
            float(self.num_mc_events * self.num_repetitions))

        #'ml'
        if draw_fit:

            f, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)

            ax1.set_xlabel('S1 [PE]')
            ax1.set_ylabel('log(S2/S1)')
            ax2.set_xlabel('S1 [PE]')
            ax2.set_ylabel('log(S2/S1)')

            s1_s2_data_plot = np.rot90(self.d_coincidence_data_information[
                self.l_cathode_settings_in_use[0]][degree_setting]
                                       ['a_log_s2_s1'])
            s1_s2_data_plot = np.flipud(s1_s2_data_plot)
            ax1.pcolormesh(self.a_s1_bin_edges, self.a_log_bin_edges,
                           s1_s2_data_plot)

            s1_s2_mc_plot = np.rot90(a_s1_s2_mc)
            s1_s2_mc_plot = np.flipud(s1_s2_mc_plot)
            #print self.l_s1_settings
            #print self.l_log_settings
            #print self.d_coincidence_data_information[self.l_cathode_settings_in_use[0]][self.l_degree_settings_in_use[0]]['a_log_s2_s1'].shape
            #print a_s1_s2_mc.shape
            #print s1_s2_data_plot.shape
            #print s1_s2_mc_plot.shape
            ax2.pcolormesh(self.a_s1_bin_edges, self.a_log_bin_edges,
                           s1_s2_mc_plot)
            #plt.colorbar()

            c1 = Canvas(1400, 400)
            c1.Divide(2)

            h_s1_data = Hist(*self.l_s1_settings, name='hS1_draw_data')
            root_numpy.array2hist(
                self.d_coincidence_data_information[
                    self.l_cathode_settings_in_use[0]][degree_setting]
                ['a_log_s2_s1'].sum(axis=1), h_s1_data)

            hS1MC = Hist(*self.l_s1_settings, name='hS1_draw_mc')
            root_numpy.array2hist(a_s1_s2_mc.sum(axis=1), hS1MC)

            s1_scale_factor = h_s1_data.Integral() / float(hS1MC.Integral())

            g_s1_data = neriX_analysis.convert_hist_to_graph_with_poisson_errors(
                h_s1_data)
            g_s1_mc = neriX_analysis.convert_hist_to_graph_with_poisson_errors(
                hS1MC, scale=s1_scale_factor)

            g_s1_mc.SetFillColor(root.kBlue)
            g_s1_mc.SetMarkerColor(root.kBlue)
            g_s1_mc.SetLineColor(root.kBlue)
            g_s1_mc.SetFillStyle(3005)

            g_s1_data.SetTitle('S1 Comparison')
            g_s1_data.GetXaxis().SetTitle('S1 [PE]')
            g_s1_data.GetYaxis().SetTitle('Counts')

            g_s1_data.SetLineColor(root.kRed)
            g_s1_data.SetMarkerSize(0)
            g_s1_data.GetXaxis().SetRangeUser(self.l_s1_settings[1],
                                              self.l_s1_settings[2])
            g_s1_data.GetYaxis().SetRangeUser(
                0, 1.2 * max(h_s1_data.GetMaximum(), hS1MC.GetMaximum()))

            c1.cd(1)
            g_s1_data.Draw('ap')
            g_s1_mc.Draw('same')
            g_s1_mc_band = g_s1_mc.Clone()
            g_s1_mc_band.Draw('3 same')

            h_s2_data = Hist(*self.l_log_settings, name='h_s2_draw_data')
            root_numpy.array2hist(
                self.d_coincidence_data_information[
                    self.l_cathode_settings_in_use[0]][degree_setting]
                ['a_log_s2_s1'].sum(axis=0), h_s2_data)

            h_s2_mc = Hist(*self.l_log_settings, name='h_s2_draw_mc')
            root_numpy.array2hist(a_s1_s2_mc.sum(axis=0), h_s2_mc)

            s2_scale_factor = h_s2_data.Integral() / float(h_s2_mc.Integral())

            g_s2_data = neriX_analysis.convert_hist_to_graph_with_poisson_errors(
                h_s2_data)
            g_s2_mc = neriX_analysis.convert_hist_to_graph_with_poisson_errors(
                h_s2_mc, scale=s2_scale_factor)

            g_s2_mc.SetFillColor(root.kBlue)
            g_s2_mc.SetMarkerColor(root.kBlue)
            g_s2_mc.SetLineColor(root.kBlue)
            g_s2_mc.SetFillStyle(3005)

            g_s2_data.SetTitle('Log(S2/S1) Comparison')
            g_s2_data.GetXaxis().SetTitle('Log(S2/S1)')
            g_s2_data.GetYaxis().SetTitle('Counts')

            g_s2_data.SetLineColor(root.kRed)
            g_s2_data.SetMarkerSize(0)
            g_s2_data.GetXaxis().SetRangeUser(self.l_log_settings[1],
                                              self.l_log_settings[2])
            g_s2_data.GetYaxis().SetRangeUser(
                0, 1.2 * max(h_s2_data.GetMaximum(), h_s2_mc.GetMaximum()))

            c1.cd(2)
            g_s2_data.Draw('ap')
            g_s2_mc.Draw('same')
            g_s2_mc_band = g_s2_mc.Clone()
            g_s2_mc_band.Draw('3 same')

            c1.Update()

            neriX_analysis.save_plot(['temp_results'],
                                     c1,
                                     '%d_deg_1d_hists' % (degree_setting),
                                     batch_mode=True)
            f.savefig('./temp_results/%d_deg_2d_hist.png' % (degree_setting))

        flat_s1_s2_data = np.asarray(self.a_data_hist_s1_s2.flatten(),
                                     dtype=np.float32)
        flat_s1_s2_mc = np.asarray(a_s1_s2_mc.flatten(), dtype=np.float32)
        logLikelihoodMatching = c_log_likelihood(
            flat_s1_s2_data, flat_s1_s2_mc, len(flat_s1_s2_data),
            int(self.num_mc_events * self.num_repetitions), 0.95)

        #print prior_ln_likelihood
        #print logLikelihoodMatching
        #print max(flat_s1_s2_data)
        #print max(flat_s1_s2_mc)
        #print '\n\n'

        if np.isnan(logLikelihoodMatching):
            return -np.inf
        else:
            matching_ln_likelihood += logLikelihoodMatching

        if self.b_suppress_likelihood:
            matching_ln_likelihood /= self.ll_suppression_factor

        total_ln_likelihood = prior_ln_likelihood + matching_ln_likelihood
        #print total_ln_likelihood

        if np.isnan(total_ln_likelihood):
            return -np.inf
        else:
            return total_ln_likelihood
def plot_pull(pulls,
              centre_of_mass,
              channel,
              variable,
              k_value,
              tau_value,
              output_folder,
              output_formats,
              bin_index=None,
              n_bins=1):
    min_x, max_x = min(pulls), max(pulls)
    abs_max = int(max(abs(min_x), max_x))
    if abs_max < 1:
        abs_max = 1
    n_x_bins = 2 * abs_max * 10  # bin width = 0.1
    #     print(n_x_bins, -abs_max, abs_max)
    h_pull = Hist(n_x_bins, -abs_max, abs_max)
    filling = h_pull.Fill
    stats = 0

    for pull_index, pull in enumerate(pulls):
        if not bin_index is None:
            matches_bin = (pull_index - bin_index) % (n_bins) == 0
            if pull_index < n_bins:  # first set correction
                matches_bin = pull_index == bin_index
            if not matches_bin:
                continue
        filling(pull)
        stats += 1


#    printstats
#    h_list = hist_to_value_error_tuplelist(h_pull)
#    printh_list
#    printlen(hist_data), min(hist_data), max(hist_data)
    fr = None
    if bin_index is None:
        fr = plot_h_pull(h_pull,
                         centre_of_mass,
                         channel,
                         variable,
                         k_value,
                         tau_value,
                         output_folder,
                         output_formats,
                         stats=stats,
                         name='pull_from_files_all_bins_stats_%d' % stats)
    else:
        fr = plot_h_pull(h_pull,
                         centre_of_mass,
                         channel,
                         variable,
                         k_value,
                         tau_value,
                         output_folder,
                         output_formats,
                         stats=stats,
                         name='pull_from_files_bin_%d_stats_%d' %
                         (bin_index, stats))

    return fr
Beispiel #17
0
print "Creating test tree in chaintest1.root"
f = root_open("chaintest1.root", "recreate")

tree = Tree("test")
branches = {'x': 'F', 'y': 'F', 'z': 'F', 'i': 'I'}
tree.create_branches(branches)

for i in xrange(10000):
    tree.x = gauss(.5, 1.)
    tree.y = gauss(.3, 2.)
    tree.z = gauss(13., 42.)
    tree.i = i
    tree.fill()

# Make a histogram of x when y > 1
hist1 = Hist(100, -10, 10, name='hist1')
tree.Draw('x', 'y > 1', hist=hist1)
hist1.SetDirectory(0)  # memory resident
print "The first tree has %f entries where y > 1" % hist1.Integral()

tree.write()
f.close()

print "Creating test tree in chaintest2.root"
f = root_open("chaintest2.root", "recreate")

tree = Tree("test")
tree.create_branches(branches)

for i in xrange(10000):
    tree.x = gauss(.5, 1.)
def plot_difference(difference, centre_of_mass, channel, variable, k_value,
                    tau_value, output_folder, output_formats):
    stats = len(difference)
    values, errors = [], []
    add_value = values.append
    add_error = errors.append
    for value, error in difference:
        add_value(value)
        add_error(error)
    min_x, max_x = min(values), max(values)
    abs_max = int(max(abs(min_x), max_x))
    #    n_x_bins = 2 * abs_max * 10
    h_values = Hist(100, -abs_max, abs_max)
    fill_value = h_values.Fill
    for value in values:
        fill_value(value)

    plt.figure(figsize=(16, 16), dpi=200, facecolor='white')
    axes = plt.axes()
    h_values.SetMarkerSize(CMS.data_marker_size)
    rplt.errorbar(h_values, xerr=True, emptybins=True, axes=axes)

    channel_label = latex_labels.channel_latex[channel]
    var_label = latex_labels.variables_latex[variable]
    title_template = 'SVD unfolding performance for unfolding of {variable}\n'
    title_template += '$\sqrt{{s}}$ = {com} TeV, {channel}, {value}'
    title = title_template.format(variable=var_label,
                                  com=centre_of_mass,
                                  channel=channel_label,
                                  value=get_value_title(k_value, tau_value))

    plt.xlabel('$\mathrm{unfolded} - \mathrm{true}$', CMS.x_axis_title)
    plt.ylabel('number of toy experiments', CMS.y_axis_title)
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)
    plt.title(title, CMS.title)
    plt.tight_layout()

    for save in output_formats:
        plt.savefig(output_folder + 'difference_stats_' + str(stats) + '.' +
                    save)

    min_x, max_x = min(errors), max(errors)
    h_errors = Hist(1000, min_x, max_x)
    fill_value = h_errors.Fill
    for error in errors:
        fill_value(error)

    plt.figure(figsize=(16, 16), dpi=200, facecolor='white')
    axes = plt.axes()
    h_errors.SetMarkerSize(CMS.data_marker_size)
    rplt.errorbar(h_errors, xerr=True, emptybins=True, axes=axes)

    plt.xlabel('$\sigma(\mathrm{unfolded} - \mathrm{true})$', CMS.x_axis_title)
    plt.ylabel('number of toy experiments', CMS.y_axis_title)
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)
    plt.title(title_template, CMS.title)

    plt.tight_layout()

    for save in output_formats:
        plt.savefig(output_folder + 'difference_errors_stats_' + str(stats) +
                    '.' + save)
Beispiel #19
0
def test_uniform():
    hist = Hist(10, 0, 1)
    assert_true(hist.uniform())
    hist = Hist2D(10, 0, 1, [1, 10, 100])
    assert_false(hist.uniform())
    assert_true(hist.uniform(axis=0))
Beispiel #20
0
  except ValueError:
    print("\t\tThis exists. Try re-running with -f, --force to overwrite the existing directory or specify a different output directory")
    sys.exit(1)

  # guess we're all ok, so cd and continue
  print("\tCd'ing into {0}".format(args.output))
  out_file.cd(args.output)

  # get the tree if exists
  tree = getattr(out_file, args.tree_name, None)

  # no entries, just write empty histograms to file
  if tree is None or tree.get_entries() == 0:
    print("\tNo entries in ntuple, writing empty histograms and finishing.")
    for histName, st3 in boundaries.iteritems():
      h = Hist(st3[2], st3[0], st3[1], name=histName)
      h.write()
  else:
    differences = []
    #c = ROOT.TCanvas("canvas", "canvas", 500, 500)
    for subercuts in combinations(supercuts, len(supercuts)-1):
      # hold the differences and create a text file with them later for reference
      # use integers to denote them
      differences.append([x for x in supercuts if x not in subercuts][0])

      # get the selection we apply to draw it
      selection = utils.cuts_to_selection(subercuts)
      # get the branch we need to draw
      selection_string = differences[-1]['selections']

      print("\tLooking at selection: {0}".format(selection_string))
Beispiel #21
0
def test_init_from_graph():
    hist = Hist(10, 0, 1)
    hist.FillRandom('gaus')
    graph = Graph(hist)
    hist2 = Hist(graph)
    def start(self):
        self._bs_branchnames = [
            "BToPhiMuMu_chi2",
            "BToPhiMuMu_cos2D",
            "BToPhiMuMu_eta",
            "BToPhiMuMu_etaphi_fullfit",
            "BToPhiMuMu_fit_cos2D",
            "BToPhiMuMu_fit_eta",
            "BToPhiMuMu_fit_mass",
            "BToPhiMuMu_fit_massErr",
            "BToPhiMuMu_fit_phi",
            "BToPhiMuMu_fit_pt",
            "BToPhiMuMu_l_xy",
            "BToPhiMuMu_l_xy_unc",
            "BToPhiMuMu_lep1eta_fullfit",
            "BToPhiMuMu_lep1phi_fullfit",
            "BToPhiMuMu_lep1pt_fullfit",
            "BToPhiMuMu_lep2eta_fullfit",
            "BToPhiMuMu_lep2phi_fullfit",
            "BToPhiMuMu_lep2pt_fullfit",
            "BToPhiMuMu_mass",
            "BToPhiMuMu_max_dr",
            "BToPhiMuMu_min_dr",
            "BToPhiMuMu_mll_fullfit",
            "BToPhiMuMu_mll_llfit",
            "BToPhiMuMu_mll_raw",
            "BToPhiMuMu_mphi_fullfit",
            "BToPhiMuMu_phi",
            "BToPhiMuMu_phiphi_fullfit",
            "BToPhiMuMu_pt",
            "BToPhiMuMu_ptphi_fullfit",
            "BToPhiMuMu_svprob",
            "BToPhiMuMu_trk1eta_fullfit",
            "BToPhiMuMu_trk1phi_fullfit",
            "BToPhiMuMu_trk1pt_fullfit",
            "BToPhiMuMu_trk2eta_fullfit",
            "BToPhiMuMu_trk2phi_fullfit",
            "BToPhiMuMu_trk2pt_fullfit",
            "BToPhiMuMu_charge",
            "BToPhiMuMu_l1_idx",
            "BToPhiMuMu_l2_idx",
            "BToPhiMuMu_pdgId",
            "BToPhiMuMu_phi_idx",
            "BToPhiMuMu_trk1_idx",
            "BToPhiMuMu_trk2_idx",
        ]
        self._event_branchnames = [
            #"nPhi",
            #"Phi_eta",
            #"Phi_fitted_eta",
            #"Phi_fitted_mass",
            #"Phi_fitted_phi",
            #"Phi_fitted_pt",
            #"Phi_mass",
            #"Phi_phi",
            #"Phi_pt",
            #"Phi_svprob",
            #"Phi_trk_deltaR",
            #"Phi_charge",
            #"Phi_pdgId",
            #"Phi_trk1_idx",
            #"Phi_trk2_idx",
            "nMuon",
            "nBToPhiMuMu",
            "HLT_Mu7_IP4",
            "HLT_Mu8_IP6",
            "HLT_Mu8_IP5",
            "HLT_Mu8_IP3",
            "HLT_Mu8p5_IP3p5",
            "HLT_Mu9_IP6",
            "HLT_Mu9_IP5",
            "HLT_Mu9_IP4",
            "HLT_Mu10p5_IP3p5",
            "HLT_Mu12_IP6",
            #"L1_SingleMu7er1p5",
            #"L1_SingleMu8er1p5",
            #"L1_SingleMu9er1p5",
            #"L1_SingleMu10er1p5",
            #"L1_SingleMu12er1p5",
            #"L1_SingleMu22",
            #"nTrigObj",
            #"TrigObj_pt",
            #"TrigObj_eta",
            #"TrigObj_phi",
            #"TrigObj_l1pt",
            #"TrigObj_l1pt_2",
            #"TrigObj_l2pt",
            #"TrigObj_id",
            #"TrigObj_l1iso",
            #"TrigObj_l1charge",
            #"TrigObj_filterBits",
            #"nTriggerMuon",
            #"TriggerMuon_eta",
            #"TriggerMuon_mass",
            #"TriggerMuon_phi",
            #"TriggerMuon_pt",
            #"TriggerMuon_vx",
            #"TriggerMuon_vy",
            #"TriggerMuon_vz",
            #"TriggerMuon_charge",
            #"TriggerMuon_pdgId",
            #"TriggerMuon_trgMuonIndex",
            "event",
        ]

        self._muon_branchnames = [
            "Muon_dxy",
            "Muon_dxyErr",
            "Muon_dz",
            "Muon_dzErr",
            "Muon_eta",
            "Muon_ip3d",
            "Muon_mass",
            "Muon_pfRelIso03_all",
            "Muon_pfRelIso03_chg",
            "Muon_pfRelIso04_all",
            "Muon_phi",
            "Muon_pt",
            "Muon_ptErr",
            "Muon_segmentComp",
            "Muon_sip3d",
            "Muon_vx",
            "Muon_vy",
            "Muon_vz",
            "Muon_charge",
            "Muon_isTriggering",
            "Muon_nStations",
            "Muon_pdgId",
            "Muon_tightCharge",
            "Muon_highPtId",
            "Muon_inTimeMuon",
            "Muon_isGlobal",
            "Muon_isPFcand",
            "Muon_isTracker",
            "Muon_mediumId",
            "Muon_mediumPromptId",
            "Muon_miniIsoId",
            "Muon_multiIsoId",
            "Muon_mvaId",
            "Muon_pfIsoId",
            "Muon_softId",
            "Muon_softMvaId",
            "Muon_tightId",
            "Muon_tkIsoId",
            "Muon_triggerIdLoose",
        ]

        #if self._isMC:
        #  self._input_branches.extend(['GenPart_pdgId', 'GenPart_genPartIdxMother'])
        self._mu_histograms = {}
        self._mu_histograms["nMuon"] = Hist(11,
                                            -0.5,
                                            10.5,
                                            name="nMuon",
                                            title="",
                                            type="F")
        self._mu_histograms["nMuon_isTrig"] = Hist(11,
                                                   -0.5,
                                                   10.5,
                                                   name="nMuon_isTrig",
                                                   title="",
                                                   type="F")
        self._mu_histograms["Muon_pt"] = Hist(100,
                                              0.0,
                                              100.0,
                                              name="Muon_pt",
                                              title="",
                                              type="F")
        self._mu_histograms["Muon_pt_isTrig"] = Hist(100,
                                                     0.0,
                                                     100.0,
                                                     name="Muon_pt_isTrig",
                                                     title="",
                                                     type="F")

        self._histograms = {}
        for tag_type in ["tag", "probe"]:
            self._histograms[tag_type] = {}
            for trig in ["trigpass", "triginclusive"]:
                self._histograms[tag_type][trig] = {}
                self._histograms[tag_type][trig]["BToPhiMuMu_chi2"] = Hist(
                    100,
                    0.0,
                    100.0,
                    name="{}_{}_BToPhiMuMu_chi2".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_eta"] = Hist(
                    50,
                    -5.0,
                    5.0,
                    name="{}_{}_BToPhiMuMu_eta".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_fit_cos2D"] = Hist(
                        100,
                        -1.,
                        1.,
                        name="{}_{}_BToPhiMuMu_fit_cos2D".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_fit_eta"] = Hist(
                    50,
                    -5.0,
                    5.0,
                    name="{}_{}_BToPhiMuMu_fit_eta".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_fit_mass"] = Hist(
                    100,
                    BS_MASS * 0.9,
                    BS_MASS * 1.1,
                    name="{}_{}_BToPhiMuMu_fit_mass".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_fit_phi"] = Hist(
                    50,
                    -2.0 * math.pi,
                    2.0 * math.pi,
                    name="{}_{}_BToPhiMuMu_fit_phi".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_fit_pt"] = Hist(
                    100,
                    0.0,
                    100.0,
                    name="{}_{}_BToPhiMuMu_fit_pt".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_l_xy"] = Hist(
                    50,
                    -1.0,
                    4.0,
                    name="{}_{}_BToPhiMuMu_l_xy".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_l_xy_sig"] = Hist(
                    50,
                    -1.0,
                    4.0,
                    name="{}_{}_BToPhiMuMu_l_xy_sig".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_lep1eta_fullfit"] = Hist(
                        50,
                        -5.0,
                        5.0,
                        name="{}_{}_BToPhiMuMu_lep1eta_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_lep1phi_fullfit"] = Hist(
                        50,
                        -2.0 * math.pi,
                        2.0 * math.pi,
                        name="{}_{}_BToPhiMuMu_lep1phi_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_lep1pt_fullfit"] = Hist(
                        100,
                        0.0,
                        100.0,
                        name="{}_{}_BToPhiMuMu_lep1pt_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_lep2eta_fullfit"] = Hist(
                        50,
                        -5.0,
                        5.0,
                        name="{}_{}_BToPhiMuMu_lep2eta_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_lep2phi_fullfit"] = Hist(
                        50,
                        -2.0 * math.pi,
                        2.0 * math.pi,
                        name="{}_{}_BToPhiMuMu_lep2phi_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_lep2pt_fullfit"] = Hist(
                        100,
                        0.0,
                        100.0,
                        name="{}_{}_BToPhiMuMu_lep2pt_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_mass"] = Hist(
                    500,
                    BS_MASS * 0.9,
                    BS_MASS * 1.1,
                    name="{}_{}_BToPhiMuMu_mass".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_mll_fullfit"] = Hist(
                        500,
                        JPSI_1S_MASS * 0.9,
                        JPSI_1S_MASS * 1.1,
                        name="{}_{}_BToPhiMuMu_mll_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_mll_llfit"] = Hist(
                        500,
                        JPSI_1S_MASS * 0.9,
                        JPSI_1S_MASS * 1.1,
                        name="{}_{}_BToPhiMuMu_mll_llfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_mll_raw"] = Hist(
                    500,
                    JPSI_1S_MASS * 0.9,
                    JPSI_1S_MASS * 1.1,
                    name="{}_{}_BToPhiMuMu_mll_raw".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_mphi_fullfit"] = Hist(
                        500,
                        PHI_1020_MASS * 0.9,
                        PHI_1020_MASS * 1.1,
                        name="{}_{}_BToPhiMuMu_mphi_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_phi"] = Hist(
                    50,
                    -2.0 * math.pi,
                    2.0 * math.pi,
                    name="{}_{}_BToPhiMuMu_phi".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_etaphi_fullfit"] = Hist(
                        50,
                        -5.0,
                        5.0,
                        name="{}_{}_BToPhiMuMu_etaphi_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_phiphi_fullfit"] = Hist(
                        50,
                        -2.0 * math.pi,
                        2.0 * math.pi,
                        name="{}_{}_BToPhiMuMu_phiphi_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_pt"] = Hist(
                    100,
                    0.0,
                    100.0,
                    name="{}_{}_BToPhiMuMu_pt".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig][
                    "BToPhiMuMu_ptphi_fullfit"] = Hist(
                        100,
                        0.0,
                        100.0,
                        name="{}_{}_BToPhiMuMu_ptphi_fullfit".format(
                            tag_type, trig),
                        title="",
                        type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_svprob"] = Hist(
                    100,
                    -1.0,
                    1.0,
                    name="{}_{}_BToPhiMuMu_svprob".format(tag_type, trig),
                    title="",
                    type="F")
                self._histograms[tag_type][trig]["BToPhiMuMu_charge"] = Hist(
                    3,
                    -1.5,
                    1.5,
                    name="{}_{}_BToPhiMuMu_charge".format(tag_type, trig),
                    title="",
                    type="F")

        self._cutflow_names = []
        self._cutflow_names.append("Inclusive")
        self._cutflow_names.append(self._trigger)

        self._cutflow_names.append("Tag")
        self._cutflow_names.append("Tag SV")
        self._cutflow_names.append("Tag mu-K")
        self._cutflow_names.append("Tag Jpsi")
        self._cutflow_names.append("Tag phi")

        self._cutflow_names.append("Probe")
        self._cutflow_names.append("Probe SV")
        self._cutflow_names.append("Probe mu-K")
        self._cutflow_names.append("Probe Jpsi")
        self._cutflow_names.append("Probe phi")

        self._cutflow_counts = {}
        for name in self._cutflow_names:
            self._cutflow_counts[name] = 0
Beispiel #23
0
def test_init_edge_repeated():
    # bin edges must not be repeated
    Hist([10, 10, 30])
Beispiel #24
0
try:
    print f.a.b.c.d.e.f
except AttributeError, e:
    print e

for thing in f.walk():
    print thing

f.close()

# supports with statements
with root_open('data.root', 'update') as f:
    print f

    # write some histograms
    h1 = Hist(100, 0, 100, name='h1', type='I')
    # variable bin widths
    h2 = Hist2D((0, 3, 5, 20, 50), (10, 20, 30, 40, 1000), name='h2')

    h1.Write()
    h2.Write()
# file is automatically closed after with statement

# retrieve the histograms previously saved
with root_open('data.root') as f:

    h1 = f.h1
    # or h1 = f.Get('h1')
    h2 = f.h2
    # or h2 = f.Get('h2')
Beispiel #25
0
def run_landaus(title, folder_path, d_mip_variables, data_charge_inputs):
    '''
	Plot any landaus
	'''
    print "- - - - - - - - - - - - - - - - - - -"
    print "Plotting Data Simulation Comparisons"
    print "- - - - - - - - - - - - - - - - - - -"
    ########################################################################################################################
    ### Fill ROOTPY Hists
    ########################################################################################################################
    charge_deposited_hist = Hist(data_charge_inputs['edges'])
    for v in d_mip_variables['q_Deposited_e']:
        charge_deposited_hist.Fill(v)
    charge_deposited_hist.Scale(1 / charge_deposited_hist.Integral())

    unweighted_charge_deposited_hist = Hist(data_charge_inputs['edges'])
    for v in d_mip_variables['q_ClusterDeposited_e']:
        unweighted_charge_deposited_hist.Fill(v)
    unweighted_charge_deposited_hist.Scale(
        1 / unweighted_charge_deposited_hist.Integral())

    charge_read_hist = Hist(data_charge_inputs['edges'])
    for v in d_mip_variables['q_Read_e']:
        charge_read_hist.Fill(v)
    charge_read_hist.Scale(1 / charge_read_hist.Integral())

    charge_weight_hist = Hist(data_charge_inputs['edges_stripClusterFraction'])
    for v in d_mip_variables['q_Weight_e']:
        charge_weight_hist.Fill(v)
    charge_weight_hist.Scale(1 / charge_weight_hist.Integral())

    ########################################################################################################################
    ### Charge Weighting
    ########################################################################################################################
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    rplt.hist(
        data_charge_inputs['hist_stripClusterFraction_normed'],
        histtype='step',
        facecolor='red',
        edgecolor='red',
        fill=True,
        alpha=0.5,
        label='Strip-Cluster Distribution',
    )
    rplt.hist(
        charge_weight_hist,
        histtype='step',
        facecolor='blue',
        edgecolor='blue',
        fill=True,
        alpha=0.5,
        label='Scaling Distribution applied to Simulated Charge Deposition ',
    )
    ax.set_ylim([0, 0.1])
    ax.set_xlabel('Charge [e]')
    ax.set_ylabel('N')
    fig.suptitle('Strip-Cluster Charge Distribution',
                 fontsize=14,
                 fontweight='bold')
    plt.title(title, loc='right')
    leg = plt.legend(loc='best')
    leg.draw_frame(False)
    fig.savefig(folder_path + 'MC_Data_ChargeWeighting_Norm.pdf',
                bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()

    # ########################################################################################################################
    # ### Data STRIP vs Cluster Charge
    # ########################################################################################################################
    # fig = plt.figure()
    # ax = fig.add_subplot(1, 1, 1)
    # rplt.hist(
    # 	data_charge_inputs['hist_clusterCharge'],
    # 	histtype='step',
    # 	facecolor='orange',
    # 	edgecolor='orange',
    # 	fill = True,
    # 	alpha=0.5,
    # 	label='Cluster Charge Distribution in Data',
    # )
    # rplt.hist(
    # 	data_charge_inputs['hist_stripCharge'],
    # 	histtype='step',
    # 	facecolor='red',
    # 	edgecolor='red',
    # 	fill = True,
    # 	alpha=0.5,
    # 	label='Strip Charge Distribution in Data',
    # )
    # # ax.set_ylim([0,0.15])
    # ax.set_xlabel('Charge [e]')
    # ax.set_ylabel('N')
    # fig.suptitle('Strip v Cluster Charge Deposition in Data', fontsize=14, fontweight='bold')
    # plt.title(title, loc='right')
    # leg = plt.legend(loc='best')
    # leg.draw_frame(False)
    # fig.savefig(folder_path+'Data_StripCluster.pdf', bbox_inches='tight')
    # fig.clf()
    # plt.close()
    # gc.collect()

    ########################################################################################################################
    ### Data STRIP vs Cluster Charge
    ########################################################################################################################
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    rplt.hist(
        data_charge_inputs['hist_clusterCharge_normed'],
        histtype='step',
        facecolor='orange',
        edgecolor='orange',
        fill=True,
        alpha=0.5,
        label='Cluster Charge Distribution in Data',
    )
    rplt.hist(
        data_charge_inputs['hist_stripCharge_normed'],
        histtype='step',
        facecolor='red',
        edgecolor='red',
        fill=True,
        alpha=0.5,
        label='Strip Charge Distribution in Data',
    )
    ax.set_ylim([0, 0.15])
    ax.set_xlabel('Charge [e]')
    ax.set_ylabel('N')
    fig.suptitle('Strip v Cluster Charge in Data',
                 fontsize=14,
                 fontweight='bold')
    plt.title(title, loc='right')
    leg = plt.legend(loc='best')
    leg.draw_frame(False)
    fig.savefig(folder_path + 'Data_StripCluster_Norm.pdf',
                bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()

    ########################################################################################################################
    ### Simulation STRIP vs Cluster Charge
    ########################################################################################################################
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    rplt.hist(
        unweighted_charge_deposited_hist,
        histtype='step',
        facecolor='green',
        edgecolor='green',
        fill=True,
        alpha=0.5,
        label='Cluster Charge Distribution in Simulation',
    )
    rplt.hist(
        charge_deposited_hist,
        histtype='step',
        facecolor='blue',
        edgecolor='blue',
        fill=True,
        alpha=0.5,
        label='Strip Charge Distribution in Simulation',
    )
    ax.set_ylim([0, 0.1])
    ax.set_xlabel('Charge [e]')
    ax.set_ylabel('N')
    fig.suptitle('Strip v Cluster Charge Deposition in Simulation',
                 fontsize=14,
                 fontweight='bold')
    plt.title(title, loc='right')
    leg = plt.legend(loc='best')
    leg.draw_frame(False)
    fig.savefig(folder_path + 'MC_ChargeDeposition_Norm.pdf',
                bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()

    ########################################################################################################################
    ### Simulation STRIP vs Cluster Charge. Input vs Output Distirbutions
    ########################################################################################################################
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    rplt.hist(
        charge_deposited_hist,
        histtype='step',
        facecolor='blue',
        edgecolor='blue',
        fill=True,
        alpha=0.5,
        label='Strip Charge Distribution in Simulation',
    )
    rplt.hist(
        charge_read_hist,
        histtype='step',
        facecolor='purple',
        edgecolor='purple',
        fill=True,
        alpha=0.5,
        label='Strip Charge Distribution in Simulation from APV Gain',
    )
    ax.set_ylim([0, 0.1])
    ax.set_xlabel('Charge [e]')
    ax.set_ylabel('N')
    fig.suptitle(
        'Simulated Charge Deposition Comparison with Readout from Gain',
        fontsize=14,
        fontweight='bold')
    plt.title(title, loc='right')
    leg = plt.legend(loc='best')
    leg.draw_frame(False)
    fig.savefig(folder_path + 'MC_ChargeDepositionFromGain_Norm.pdf',
                bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()

    ########################################################################################################################
    ### Strip Charge Deposition From Data vs MC
    ########################################################################################################################
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    rplt.hist(
        charge_read_hist,
        histtype='step',
        facecolor='blue',
        edgecolor='blue',
        fill=True,
        alpha=0.5,
        label='Strip Charge Distribution in Simulation',
    )
    rplt.hist(
        data_charge_inputs['hist_stripCharge_normed'],
        histtype='step',
        facecolor='red',
        edgecolor='red',
        fill=True,
        alpha=0.5,
        label='Strip Charge Distribution in Data',
    )
    # plt.axvline(x=7500, linewidth=1, color='r', label = 'Min Threshold')
    # plt.axvline(x=1500, linewidth=1, color='g', label = 'Min Threshold')
    # plt.axvline(x=3000, linewidth=1, color='b', label = 'Min Threshold')
    # plt.axvline(x=2000, linewidth=1, color='orange', label = 'Min Threshold')

    ax.set_ylim([0, 0.1])
    ax.set_xlabel('Charge [e]')
    ax.set_ylabel('N')
    fig.suptitle('Strip Charge Deposition Data vs Simulation',
                 fontsize=14,
                 fontweight='bold')
    plt.title(title, loc='right')
    leg = plt.legend(loc='best')
    leg.draw_frame(False)
    fig.savefig(folder_path + 'MC_Data_StripChargeDeposition_Norm.pdf',
                bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()

    ########################################################################################################################
    ### Cluster Charge Deposition From Data vs MC
    ########################################################################################################################
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    rplt.hist(
        data_charge_inputs['hist_clusterCharge_normed'],
        histtype='step',
        facecolor='orange',
        edgecolor='orange',
        fill=True,
        alpha=0.5,
        label='Cluster Charge Distribution in Data',
    )
    rplt.hist(
        unweighted_charge_deposited_hist,
        histtype='step',
        facecolor='green',
        edgecolor='green',
        fill=True,
        alpha=0.5,
        label='Cluster Charge Distribution in Simulation',
    )
    ax.set_ylim([0, 0.1])
    ax.set_xlabel('Charge [e]')
    ax.set_ylabel('N')
    fig.suptitle('Cluster Charge Deposition Data vs Simulation',
                 fontsize=14,
                 fontweight='bold')
    plt.title(title, loc='right')
    leg = plt.legend(loc='best')
    leg.draw_frame(False)
    fig.savefig(folder_path + 'MC_Data_ClusterChargeDeposition_Norm.pdf',
                bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()

    # ########################################################################################################################
    # ### Cluster Charge Vs Charge Splitting
    # ########################################################################################################################
    # fig = plt.figure()
    # ax = fig.add_subplot(1, 1, 1)
    # plt.scatter(
    # 	d_mip_variables['q_ClusterDeposited_e'],
    # 	d_mip_variables['q_Weight_e'],
    # )
    # ax.set_xlim([0,8000000])
    # ax.set_ylim([0,1])
    # ax.set_xlabel('Cluster Charge [e]')
    # ax.set_ylabel('Strip Charge [e]')

    # # ax.set_ylabel('N')
    # fig.suptitle('Cluster Charge vs Strip Charge [Simulation]', fontsize=14, fontweight='bold')
    # plt.title(title, loc='right')
    # # leg = plt.legend(loc='best')
    # # leg.draw_frame(False)
    # fig.savefig(folder_path+'TEST.pdf', bbox_inches='tight')
    # fig.clf()
    # plt.close()
    # gc.collect()

    ########################################################################################################################
    ### All Charge Distributions
    ########################################################################################################################
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    rplt.hist(
        data_charge_inputs['hist_clusterCharge_normed'],
        histtype='step',
        facecolor='orange',
        edgecolor='orange',
        fill=True,
        alpha=0.5,
        label='Cluster Charge Distribution in Data',
    )
    rplt.hist(
        data_charge_inputs['hist_stripCharge_normed'],
        histtype='step',
        facecolor='red',
        edgecolor='red',
        fill=True,
        alpha=0.5,
        label='Strip Charge Distribution in Data',
    )
    rplt.hist(
        unweighted_charge_deposited_hist,
        histtype='step',
        facecolor='blue',
        edgecolor='blue',
        fill=True,
        alpha=0.5,
        label='Cluster Charge Distribution in Simulation',
    )
    rplt.hist(
        charge_read_hist,
        histtype='step',
        facecolor='purple',
        edgecolor='purple',
        fill=True,
        alpha=0.5,
        label='Strip Charge Distribution in Simulation from APV Gain',
    )

    ax.set_ylim([0, 0.05])
    ax.set_xlabel('Charge [e]')
    ax.set_ylabel('N')
    fig.suptitle('Strip and Cluster Distributions in Simulation and Data',
                 fontsize=14,
                 fontweight='bold')
    plt.title(title, loc='right')
    leg = plt.legend(loc='best')
    leg.draw_frame(False)
    fig.savefig(folder_path + 'MC_Data_ChargeComparison_Norm.pdf',
                bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()

    return
Beispiel #26
0
def makeDiscr(train_discr_dict, discr_dict, outfile, xtitle="discriminator"):
    c = ROOT.TCanvas("c", "c", 800, 500)
    ROOT.gStyle.SetOptStat(0)
    ROOT.gPad.SetMargin(0.15, 0.1, 0.2, 0.1)
    #ROOT.gPad.SetLogy(1)
    #ROOT.gPad.SetGrid(1,1)
    ROOT.gStyle.SetGridColor(17)
    l = TLegend(0.17, 0.75, 0.88, 0.88)
    l.SetTextSize(0.055)
    l.SetBorderSize(0)
    l.SetFillStyle(0)
    l.SetNColumns(2)

    colors = [2, 1, 4, ROOT.kCyan + 2]
    counter = 0
    for leg, discr in train_discr_dict.iteritems():
        a = Hist(30, 0, 1)
        #fill_hist_with_ndarray(a, discr)
        a.fill_array(discr)
        a.SetLineColor(colors[counter])
        a.SetLineWidth(2)
        a.GetXaxis().SetTitle(xtitle)
        a.GetXaxis().SetLabelSize(0.05)
        a.GetXaxis().SetTitleSize(0.06)
        a.GetXaxis().SetTitleOffset(1.45)
        a.GetYaxis().SetTitle("a.u.")
        a.GetYaxis().SetTickSize(0)
        a.GetYaxis().SetLabelSize(0)
        a.GetYaxis().SetTitleSize(0.06)
        a.GetYaxis().SetTitleOffset(0.9)
        a.Scale(1. / a.Integral())
        #a.GetYaxis().SetRangeUser(0.00001,100)
        a.GetYaxis().SetRangeUser(0, 0.9)
        if counter == 0: a.draw("hist")
        else: a.draw("same hist")
        l.AddEntry(a, leg, "l")
        counter += 1

    counter = 0
    for leg, discr in discr_dict.iteritems():
        a = Hist(30, 0, 1)
        #fill_hist_with_ndarray(a, discr)
        a.fill_array(discr)
        a.SetLineColor(colors[counter])
        a.SetMarkerColor(colors[counter])
        a.SetMarkerStyle(34)
        a.SetMarkerSize(1.8)
        a.SetLineWidth(2)
        a.GetXaxis().SetTitle(xtitle)
        a.GetXaxis().SetLabelSize(0.05)
        a.GetXaxis().SetTitleSize(0.06)
        a.GetXaxis().SetTitleOffset(1.45)
        a.GetYaxis().SetTitle("a.u.")
        a.GetYaxis().SetTickSize(0)
        a.GetYaxis().SetLabelSize(0)
        a.GetYaxis().SetTitleSize(0.06)
        a.GetYaxis().SetTitleOffset(0.9)
        a.Scale(1. / a.Integral())
        #a.GetYaxis().SetRangeUser(0.00001,100)
        a.GetYaxis().SetRangeUser(0, 0.4)
        a.draw("same p X0")
        l.AddEntry(a, leg, "p")
        counter += 1

    # counter = 0


#     for leg,discr in train_discr_dict.iteritems():
#         d = Hist(30, 0, 1)
#         d.fill_array(discr)
#         d.SetLineColor(colors[counter])
#         d.SetLineWidth(2)
#         l.AddEntry(d,leg,"l")
#
#         b = Hist(30, 0, 1)
#         d.fill_array(discr_dict[leg.split(" ")[0] + " test"])
#         b.SetLineColor(colors[counter])
#         b.SetMarkerColor(colors[counter])
#         b.SetMarkerStyle(34)
#         b.SetMarkerSize(1.8)
#         b.SetLineWidth(2)
#         l.AddEntry(b,leg,"p")
#         counter += 1

    l.Draw("same")

    c.SaveAs(outfile)
Beispiel #27
0
def test_divide():
    Graph.divide(Graph(Hist(10, 0, 1).FillRandom('gaus')),
                 Hist(10, 0, 1).FillRandom('gaus'), 'pois')
        try:
            out_file.cd(innerDir)
        except:
            pass
        # get list of things to draw
        for toDraw in config['draw']:
            histName = toDraw['name']

            histDimension = len(toDraw['draw'].split(':'))
            print "\tmaking {4}D histogram {0}\n\t{1} bins from {2} to {3}".format(
                toDraw['name'], toDraw['nbins'], toDraw['min'], toDraw['max'],
                histDimension)

            if histDimension == 1:
                h = Hist(toDraw['nbins'],
                         toDraw['min'],
                         toDraw['max'],
                         name=histName)
            elif histDimension == 2:
                h = Hist2D(toDraw['nbins'][0],
                           toDraw['min'][0],
                           toDraw['max'][0],
                           toDraw['nbins'][1],
                           toDraw['min'][1],
                           toDraw['max'][1],
                           name=histName)
            elif histDimension == 3:
                h = Hist2D(toDraw['nbins'][0],
                           toDraw['min'][0],
                           toDraw['max'][0],
                           toDraw['nbins'][1],
                           toDraw['min'][1],
Beispiel #29
0
    '''
    return poisson(b, n)/b

def generate_n_poisson_weights_for_average(n, n_events_in_bins = [100, 120]):
    mean = average(n_events_in_bins)
    return generate_n_poisson_weights(n, mean)

if __name__ == '__main__':
    from array import array
    from rootpy.plotting import Hist
    import rootpy.plotting.root2matplotlib as rplt
    import matplotlib.pyplot as plt
    from matplotlib.ticker import AutoMinorLocator
    
    bins = array('d', [0, 25, 45, 70, 100, 1000])
    h_data = Hist(bins.tolist())
    h_data.SetBinContent(1, 2146)
    h_data.SetBinError(1, 145)
    h_data.SetBinContent(2, 3399)
    h_data.SetBinError(2, 254)
    h_data.SetBinContent(3, 3723)
    h_data.SetBinError(3, 69)
    h_data.SetBinContent(4, 2256)
    h_data.SetBinError(4, 53)
    h_data.SetBinContent(5, 1722)
    h_data.SetBinError(5, 91)
    h_data.SetTitle('input distribution')
    h_new = generate_toy_MC_from_distribution(h_data)
    h_new.SetTitle('toy MC')
    fig = plt.figure(figsize=(16, 10), dpi=100, facecolor='white')
    axes = plt.axes([0.15, 0.15, 0.8, 0.8])
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)