def multiclass_graph(mt_dec,probas,name,axis_range=[0.,250.]) :
    #create a graph and save it as root file
    
    dec_modes = [0.0,1.0,10.0]

    print np.shape(mt_dec)
    print np.shape(probas)    
    
    num_of_classes = len(probas[0])
        
    all_input = np.transpose(np.vstack((mt_dec[:,0],mt_dec[:,1], probas[:,0], probas[:,1], probas[:,2], probas[:,3], probas[:,4], probas[:,5])))
    
    
    for j in dec_modes :    
        x = np.array(filter(lambda x: x[1] == j, all_input))[:,0]
        
        for k in xrange(0,num_of_classes,1) :
            y = np.array(filter(lambda x: x[1] == j, all_input))[:,k+2]        
            graph = Graph(len(x),"g1")
            #create a root file and fill it with the graph P(W+jet) vs mt
            root_open("plots/ROOTfiles/G_"+name+GetClass(k+1)+'_dec_'+str(j)+".root", 'recreate')
            fill_graph(graph,np.column_stack((x,y)))
            graph.Write()
            #create Canvas and save the plot as png
            c = Canvas()
            graph.SetTitle(name+GetClass(k+1)+'_dec_'+str(j))
            graph.SetMarkerSize(0.3)
            graph.GetXaxis().SetRangeUser(axis_range[0],axis_range[1])
            graph.Draw("AP")
            c.SaveAs("plots/G_"+name+GetClass(k+1)+'_dec_'+str(j)+".png")
Example #2
0
def efficiency_bdt(pass_function, function_inputs, xs):
    pass_results = pass_function(function_inputs)
    xs_train = xs.reshape(-1, 1)
    clf = GradientBoostingClassifier()
    clf.fit(xs_train, pass_results)
    graph = Graph(100)
    xs_graph = np.linspace(np.amin(xs), np.amax(xs), num=100)
    probas = clf.predict_proba(xs_graph.reshape(-1, 1))[:, [1]]
    print probas
    fill_graph(graph, np.column_stack((xs_graph, probas)))
    print np.column_stack((xs_graph, probas))
    return graph
Example #3
0
def efficiency_bdt(pass_function, function_inputs, xs):
    pass_results = pass_function(function_inputs)
    xs_train = xs.reshape(-1,1)
    clf = GradientBoostingClassifier()
    clf.fit(xs_train, pass_results)
    graph = Graph(100)
    xs_graph = np.linspace(np.amin(xs), np.amax(xs), num=100)
    probas = clf.predict_proba(xs_graph.reshape(-1,1))[:, [1]]
    print probas
    fill_graph(graph, np.column_stack((xs_graph, probas)))
    print np.column_stack((xs_graph, probas))
    return graph
def ClassProba(path) :
    inputs_test = pickle.load( open( path+"/"+"inputs_test.pck", "rb" ) )
    Wjet_proba = pickle.load( open( path+"/"+"class_proba.pck", "rb" ) )[:,0]

    c = Canvas()
    graph = Graph(len(inputs_test),"WjetProba")
    print len(inputs_test)
    print len(Wjet_proba)


    root_open("WjetProba.root", 'recreate')

    fill_graph(graph,np.column_stack((inputs_test,Wjet_proba)))    
    
    graph.SetMarkerSize(0.3)
    graph.GetXaxis().SetRangeUser(0.,250.)
    graph.Draw("AP")
  
    c.SaveAs(path+"/"+"WjetProba_mt.png")
Example #5
0
def test_fill_graph():
    n_samples = 1000
    data2D = RNG.randn(n_samples, 2)
    data3D = RNG.randn(n_samples, 3)

    graph = TGraph()
    rnp.fill_graph(graph, data2D)

    graph2d = TGraph2D()
    rnp.fill_graph(graph2d, data3D)

    # array not 2-d
    for g in (graph, graph2d):
        assert_raises(ValueError, rnp.fill_graph, g, RNG.randn(10))

    # length of second axis does not match dimensionality of histogram
    for g in (graph, graph2d):
        assert_raises(ValueError, rnp.fill_graph, g, RNG.randn(10, 4))

    # wrong type
    h = list()
    a = RNG.randn(10)
    assert_raises(TypeError, rnp.fill_graph, h, a)
Example #6
0
def test_fill_graph():
    np.random.seed(0)
    data2D = np.random.randn(1E6, 2)
    data3D = np.random.randn(1E4, 3)

    graph = TGraph()
    rnp.fill_graph(graph, data2D)

    graph2d = TGraph2D()
    rnp.fill_graph(graph2d, data3D)

    # array not 2-d
    for g in (graph, graph2d):
        assert_raises(ValueError, rnp.fill_graph, g, np.random.randn(1E4))

    # length of second axis does not match dimensionality of histogram
    for g in (graph, graph2d):
        assert_raises(ValueError, rnp.fill_graph, g, np.random.randn(1E4, 4))

    # wrong type
    h = list()
    a = np.random.randn(100)
    assert_raises(TypeError, rnp.fill_graph, h, a)
Example #7
0
def test_fill_graph():
    n_samples = 1000
    data2D = RNG.randn(n_samples, 2)
    data3D = RNG.randn(n_samples, 3)

    graph = TGraph()
    rnp.fill_graph(graph, data2D)

    graph2d = TGraph2D()
    rnp.fill_graph(graph2d, data3D)

    # array not 2-d
    for g in (graph, graph2d):
        assert_raises(ValueError, rnp.fill_graph, g, RNG.randn(10))

    # length of second axis does not match dimensionality of histogram
    for g in (graph, graph2d):
        assert_raises(ValueError, rnp.fill_graph, g, RNG.randn(10, 4))

    # wrong type
    h = list()
    a = RNG.randn(10)
    assert_raises(TypeError, rnp.fill_graph, h, a)
Example #8
0
def find_best_working_point(effs, signal_efficiencies, background_efficiencies, signal_probabilities, background_probabilities):
    # Compute ratios of background and signal probabilities and truncate the array
    # to match the gradient array size
    probability_ratios = background_probabilities/signal_probabilities
    probability_ratios = probability_ratios[1:]
    # Compute efficiency gradients
    effs_diff = np.ediff1d(effs)
    signal_efficiencies_diff = np.ediff1d(signal_efficiencies)
    background_efficiencies_diff = np.ediff1d(background_efficiencies)
    signal_efficiencies_diff = np.divide(signal_efficiencies_diff, effs_diff)
    background_efficiencies_diff = np.divide(background_efficiencies_diff, effs_diff)
    # Apply averaging over 3 or 2 values in order to smooth the gradients
    signal_efficiencies_diff_3 = np.convolve(signal_efficiencies_diff, np.repeat(1.0, 3.)/3., 'valid')
    signal_efficiencies_diff_2 = np.convolve(signal_efficiencies_diff, np.repeat(1.0, 2.)/2., 'valid')
    signal_efficiencies_diff = np.append(signal_efficiencies_diff_3, [signal_efficiencies_diff_2[-1],signal_efficiencies_diff[-1]])
    background_efficiencies_diff_3 = np.convolve(background_efficiencies_diff, np.repeat(1.0, 3.)/3., 'valid')
    background_efficiencies_diff_2 = np.convolve(background_efficiencies_diff, np.repeat(1.0, 2.)/2., 'valid')
    background_efficiencies_diff = np.append(background_efficiencies_diff_3, [background_efficiencies_diff_2[-1],background_efficiencies_diff[-1]])
    # Apply the signal and background probabilities in order to weight the efficiency gradients
    # If it is more likely to have background than signal in this bin, then the background efficiency gradient
    # will be increased accordingly
    background_efficiencies_diff = background_efficiencies_diff * probability_ratios
    # Interpolate and find points where background efficiency gradient > signal efficiency gradient (with some tolerance)
    interp_x = np.linspace(np.amin(effs[1:]), np.amax(effs[1:]), 1000)
    interp_signal = np.interp(interp_x, effs[1:], signal_efficiencies_diff)
    interp_background = np.interp(interp_x, effs[1:], background_efficiencies_diff)
    optimal_points = np.argwhere(np.greater(interp_background-0.05, interp_signal)).ravel() # Use a tolerance of 0.02 in case of fluctuations
    if len(optimal_points)==0:
        print 'WARNING: no working point found where the efficiency gradient is larger for background than for signal'
    # Find optimal point with smallest efficiency
    ## Compute spacing between points, and select those with an efficiency separation > 2%
    optimal_discontinuities = np.argwhere(np.ediff1d(interp_x[optimal_points])>0.02).ravel()
    ## Select the point with the largest efficiency
    optimal_index = np.amax(optimal_discontinuities)+1 if len(optimal_discontinuities)>0 else 0
    optimal_point = interp_x[optimal_points[optimal_index]]
    # Create graphs
    signal_efficiencies_diff_graph = Graph(len(effs)-1)
    background_efficiencies_diff_graph = Graph(len(effs)-1)
    optimal_points_graph = Graph(len(optimal_points))
    fill_graph(signal_efficiencies_diff_graph, np.column_stack((effs[1:], signal_efficiencies_diff)))
    fill_graph(background_efficiencies_diff_graph, np.column_stack((effs[1:], background_efficiencies_diff)))
    fill_graph(optimal_points_graph, np.column_stack((interp_x[optimal_points], interp_signal[optimal_points])))
    signal_efficiencies_diff_graph.SetName('efficiencies_signal')
    background_efficiencies_diff_graph.SetName('efficiencies_background')
    optimal_points_graph.SetName('signal_background_optimal_points')
    return signal_efficiencies_diff_graph, background_efficiencies_diff_graph, optimal_points_graph, optimal_point
Example #9
0
def find_best_working_point(effs, signal_efficiencies,
                            background_efficiencies):
    # Compute efficiency gradients
    effs_diff = np.ediff1d(effs)
    signal_efficiencies_diff = np.ediff1d(signal_efficiencies)
    background_efficiencies_diff = np.ediff1d(background_efficiencies)
    signal_efficiencies_diff = np.divide(signal_efficiencies_diff, effs_diff)
    background_efficiencies_diff = np.divide(background_efficiencies_diff,
                                             effs_diff)
    # Interpolate and find points where background efficiency gradient > signal efficiency gradient (with some tolerance)
    interp_x = np.linspace(np.amin(effs[1:]), np.amax(effs[1:]), 1000)
    interp_signal = np.interp(interp_x, effs[1:], signal_efficiencies_diff)
    interp_background = np.interp(interp_x, effs[1:],
                                  background_efficiencies_diff)
    optimal_points = np.argwhere(
        np.greater(interp_background - 0.05, interp_signal)).ravel(
        )  # Use a tolerance of 0.02 in case of fluctuations
    if len(optimal_points) == 0:
        print 'WARNING: no working point found where the efficiency gradient is larger for background than for signal'
    # Find optimal point with smallest efficiency
    ## Compute spacing between points, and select those with an efficiency separation > 2%
    optimal_discontinuities = np.argwhere(
        np.ediff1d(interp_x[optimal_points]) > 0.02).ravel()
    ## Select the point with the largest efficiency
    optimal_index = np.amax(optimal_discontinuities) + 1 if len(
        optimal_discontinuities) > 0 else 0
    optimal_point = interp_x[optimal_points[optimal_index]]
    # Create graphs
    signal_efficiencies_diff_graph = Graph(len(effs) - 1)
    background_efficiencies_diff_graph = Graph(len(effs) - 1)
    optimal_points_graph = Graph(len(optimal_points))
    fill_graph(signal_efficiencies_diff_graph,
               np.column_stack((effs[1:], signal_efficiencies_diff)))
    fill_graph(background_efficiencies_diff_graph,
               np.column_stack((effs[1:], background_efficiencies_diff)))
    fill_graph(
        optimal_points_graph,
        np.column_stack(
            (interp_x[optimal_points], interp_signal[optimal_points])))
    signal_efficiencies_diff_graph.SetName('efficiencies_signal')
    background_efficiencies_diff_graph.SetName('efficiencies_background')
    optimal_points_graph.SetName('signal_background_optimal_points')
    return signal_efficiencies_diff_graph, background_efficiencies_diff_graph, optimal_points_graph, optimal_point
Example #10
0
def remake_arrays(input_arr_):

    w_r_bins = 0.01

    # need z-binning corresponding to 1 roc
    w_z_bins = 52  # # of pixels in a roc

    # need phi-binning corresponding to 1 roc (maybe 2?)
    w_phi_bins = 80

    n_z_bins = int(3328 / w_z_bins)  # 3328 is number of pixels in a ladder row

    n_phi_bins = int(
        960 / w_phi_bins
    )  # 1440 is number of pixels around phi for all ladders, 960 for inner ladders

    inner_array = np.array(
        [row for row in input_arr_ if not np.all(row == None)])
    cleaned_array = np.array(
        [[x if x is not None else [0, np.nan, np.nan, np.nan] for x in row]
         for row in inner_array])
    r_min = np.nanmin(cleaned_array[:, :, 1])
    r_max = np.nanmax(cleaned_array[:, :, 1])

    # separate pixels into groups corresponding to rocs in phi and z
    array_by_rocs = np.array([
        cleaned_array[j * w_phi_bins:(j + 1) * w_phi_bins,
                      i * w_z_bins:(i + 1) * w_z_bins] for i in range(n_z_bins)
        for j in range(n_phi_bins)
    ])

    #roc_index = [0, 1]
    roc_index = range(0, n_z_bins * n_phi_bins)

    # fig, axs = plt.subplots(8, 8, sharex=False, sharey=False, figsize=(160, 160), tight_layout=True) #all rocs and modules
    # fig, axs = plt.subplots(12, 2, sharex=True, sharey=True, figsize=(20, 20), tight_layout=False) # fraction of rocs and modules
    # fig, axs = plt.subplots(1, sharex=True, sharey=True, figsize=(20, 20), tight_layout=True) # fraction of rocs and modules
    #fig = plt.figure() # fraction of rocs and modules
    #axs = fig.add_subplot(111, projection='3d') # fraction of rocs and modules

    # minus - 0-383
    # plus - 384-767
    occ = []
    x_array = []
    y_array = []
    z_array = []
    z_err_array = []
    phi_array = []
    phi_err_array = []
    r_array = []
    r_err_array = []

    z_hl = []
    z_err_hl = []
    phi_hl = []
    r_hl = []
    r_err_hl = []
    occ_hl = []

    occ_ring = []
    r_ring = []
    phi_ring = []
    z_ring = []

    n_ladders = 12
    n_rings = 64

    for x in range(n_ladders):
        occ_hl.append([])
        r_hl.append([])
        r_err_hl.append([])
        phi_hl.append([])
        z_hl.append([])
        z_err_hl.append([])

    for x in range(n_rings):
        occ_ring.append([])
        r_ring.append([])
        phi_ring.append([])
        z_ring.append([])

    i_ladder = 0
    i_ring = 0

    # section off rocs into roc ladders (12 'ladders'), each true ladder is split in half 6 * 2 = 12
    for roc in roc_index:

        occ_tmp = np.concatenate(array_by_rocs[roc, :, :, 0])
        r = np.concatenate(array_by_rocs[roc, :, :, 1])
        phi = np.concatenate(array_by_rocs[roc, :, :, 2])
        z = np.concatenate(array_by_rocs[roc, :, :, 3])
        z_avg = np.nanmean(z)

        x = r[~np.isnan(z)] * np.cos(phi[~np.isnan(z)])
        y = r[~np.isnan(z)] * np.sin(phi[~np.isnan(z)])
        r = r[~np.isnan(z)]
        phi = phi[~np.isnan(z)]
        occ_tmp = occ_tmp[~np.isnan(z)]
        z = z[~np.isnan(z)]

        r = np.sqrt(x**2 + y**2 + z**2)

        occ.append(np.sum(occ_tmp))
        x_array.append(np.average(x, weights=occ_tmp))
        y_array.append(np.average(y, weights=occ_tmp))
        z_array.append(np.average(z, weights=occ_tmp))
        z_err_array.append(np.std(z))
        phi_array.append(np.average(phi, weights=occ_tmp))
        phi_err_array.append(np.average(phi, weights=occ_tmp))
        r_array.append(np.average(r, weights=occ_tmp))
        r_err_array.append(np.std(r))

        occ_hl[i_ladder].append(np.sum(occ_tmp))
        r_hl[i_ladder].append(np.average(r, weights=occ_tmp))
        r_err_hl[i_ladder].append(np.std(r))
        phi_hl[i_ladder].append(np.average(phi, weights=occ_tmp))
        z_hl[i_ladder].append(np.average(z, weights=occ_tmp))
        z_err_hl[i_ladder].append(np.std(z))

        occ_ring[i_ring].append(np.sum(occ_tmp))
        r_ring[i_ring].append(np.average(r, weights=occ_tmp))
        phi_ring[i_ring].append(np.average(phi, weights=occ_tmp))
        z_ring[i_ring].append(np.average(z, weights=occ_tmp))

        i_ladder += 1
        if i_ladder == n_ladders:
            i_ring += 1
            i_ladder = 0

    occ = np.array(occ)
    x_array = np.array(x_array)
    y_array = np.array(y_array)
    z_array = np.array(z_array)
    z_err_array = np.array(z_err_array)
    phi_array = np.array(phi_array)
    phi_err_array = np.array(phi_err_array)
    r_array = np.array(r_array)
    r_err_array = np.array(r_err_array)

    phi_sort = np.argsort(phi_array)
    z_sort = np.argsort(z_array)

    occ = occ[z_sort]
    x_array = x_array[z_sort]
    y_array = y_array[z_sort]
    z_array = z_array[z_sort]
    z_err_array = z_err_array[z_sort]
    phi_array = phi_array[z_sort]
    phi_err_array = phi_err_array[z_sort]
    r_array = r_array[z_sort]
    r_err_array = r_err_array[z_sort]

    # removing rocs
    remove_z = (z_array > -12.5) * (z_array < 12.5)
    remove_z += (z_array < -12.5) + (z_array > 12.5)
    remove_z *= (z_array > -25) * (z_array < 25)
    remove_blips = (z_array < -21) + (z_array > -20)
    remove_blips *= (z_array < -14.5) + (z_array > -13.5)
    remove_blips *= (z_array < -7.5) + (z_array > -6.5)
    remove_blips *= (z_array < 5.75) + (z_array > 6.5)
    remove_blips *= (z_array < 12.5) + (z_array > 13.5)
    remove_blips *= (z_array < 19) + (z_array > 20)

    remove_hl = (phi_array < -2.3) + (phi_array > -2)
    remove_hl *= (phi_array < -1.3) + (phi_array > -1)
    remove_hl *= (phi_array < -0.25) + (phi_array > 0)
    remove_hl *= (phi_array < 0.8) + (phi_array > 1.1)
    remove_hl *= (phi_array < 1.8) + (phi_array > 2.2)
    remove_hl *= (phi_array < 2.4) + (phi_array > 2.7)

    remove_hl_low = (phi_array > -2.3) * (phi_array < -2)
    remove_hl_low += (phi_array > -1.3) * (phi_array < -1)
    remove_hl_low += (phi_array > -0.25) * (phi_array < 0)
    remove_hl_low += (phi_array > 0.8) * (phi_array < 1.1)
    remove_hl_low += (phi_array > 1.8) * (phi_array < 2.2)
    remove_hl_low += (phi_array > 2.4) * (phi_array < 2.7)

    #occ = occ[remove_z*remove_blips*remove_hl_low]
    #x_array = x_array[remove_z*remove_blips*remove_hl_low]
    #y_array = y_array[remove_z*remove_blips*remove_hl_low]
    #z_array = z_array[remove_z*remove_blips*remove_hl_low]
    #phi_array = phi_array[remove_z*remove_blips*remove_hl_low]
    #r_array = r_array[remove_z*remove_blips*remove_hl_low]
    #occ = occ[remove_z*remove_blips*remove_hl]
    #x_array = x_array[remove_z*remove_blips*remove_hl]
    #y_array = y_array[remove_z*remove_blips*remove_hl]
    #z_array = z_array[remove_z*remove_blips*remove_hl]
    #phi_array = phi_array[remove_z*remove_blips*remove_hl]
    #r_array = r_array[remove_z*remove_blips*remove_hl]

    #occ = occ[remove_z*remove_blips]
    #x_array = x_array[remove_z*remove_blips]
    #y_array = y_array[remove_z*remove_blips]
    #z_array = z_array[remove_z*remove_blips]
    #phi_array = phi_array[remove_z*remove_blips]
    #r_array = r_array[remove_z*remove_blips]
    occ = occ[remove_z * remove_blips]
    x_array = x_array[remove_z * remove_blips]
    y_array = y_array[remove_z * remove_blips]
    z_array = z_array[remove_z * remove_blips]
    phi_array = phi_array[remove_z * remove_blips]
    r_array = r_array[remove_z * remove_blips]
    r_err_array = r_err_array[remove_z * remove_blips]
    z_err_array = z_err_array[remove_z * remove_blips]
    phi_err_array = phi_err_array[remove_z * remove_blips]

    phi_sort = np.argsort(phi_array)
    z_sort = np.argsort(z_array)
    r_sort = np.argsort(r_array)

    occ_r = occ[r_sort]
    x_r_array = x_array[r_sort]
    y_r_array = y_array[r_sort]
    z_r_array = z_array[r_sort]
    z_err_r_array = z_err_array[r_sort]
    phi_r_array = phi_array[r_sort]
    r_r_array = r_array[r_sort]
    r_err_r_array = r_err_array[r_sort]

    occ_z = occ[z_sort]
    x_z_array = x_array[z_sort]
    y_z_array = y_array[z_sort]
    z_z_array = z_array[z_sort]
    z_err_z_array = z_err_array[z_sort]
    phi_z_array = phi_array[z_sort]
    r_z_array = r_array[z_sort]
    r_err_z_array = r_err_array[z_sort]

    occ_phi = occ[phi_sort]
    x_phi_array = x_array[phi_sort]
    y_phi_array = y_array[phi_sort]
    z_phi_array = z_array[phi_sort]
    z_err_phi_array = z_err_array[phi_sort]
    phi_phi_array = phi_array[phi_sort]
    phi_err_phi_array = phi_err_array[phi_sort]
    r_phi_array = r_array[phi_sort]
    r_err_phi_array = r_err_array[phi_sort]

    ####### begin condensing for projections ###############
    z_condense = []
    z_err_condense = []
    occ_z_condense = []
    n_half_ladders = 12
    for iz, z in enumerate(z_z_array):
        if iz % n_half_ladders == 0:
            z_section = iz // n_half_ladders
            z_condense.append(
                np.average(z_z_array[iz:n_half_ladders * (z_section + 1)],
                           weights=occ_z[iz:n_half_ladders * (z_section + 1)]))
            z_err_condense.append(
                np.sqrt(
                    np.sum(z_err_z_array[iz:n_half_ladders *
                                         (z_section + 1)]**2)))
            occ_z_condense.append(
                np.sum(occ_z[iz:n_half_ladders * (z_section + 1)]))
        else:
            continue
    phi_condense = []
    phi_err_condense = []
    occ_phi_condense = []
    n_z_sections = 64
    for iphi, phi in enumerate(phi_phi_array):
        if iphi % n_z_sections == 0:
            phi_section = iphi // n_z_sections
            phi_condense.append(
                np.average(
                    phi_phi_array[iphi:n_z_sections * (phi_section + 1)],
                    weights=occ_phi[iphi:n_z_sections * (phi_section + 1)]))
            phi_err_condense.append(
                np.sqrt(
                    np.sum(phi_err_phi_array[iphi:n_z_sections *
                                             (phi_section + 1)]**2)))
            occ_phi_condense.append(
                np.sum(occ_phi[iphi:n_z_sections * (phi_section + 1)]))
        else:
            continue
    r_condense = []
    r_err_condense = []
    occ_r_condense = []
    for ir, r in enumerate(r_r_array):
        if ir % n_half_ladders == 0:
            r_section = ir // n_half_ladders
            r_condense.append(
                np.average(r_r_array[ir:n_half_ladders * (r_section + 1)],
                           weights=occ_r[ir:n_half_ladders * (r_section + 1)]))
            r_err_condense.append(
                np.sqrt(
                    np.sum(r_err_r_array[ir:n_half_ladders *
                                         (r_section + 1)]**2)))
            occ_r_condense.append(
                np.sum(occ_r[ir:n_half_ladders * (r_section + 1)]))
        else:
            continue
    r_condense_comb = []
    r_err_condense_comb = []
    occ_r_condense_comb = []
    for ir, r in enumerate(r_condense):
        if ir % 2 == 0:
            r_condense_comb.append((r + r_condense[ir + 1]) / 2)
            r_err_condense_comb.append(
                np.sqrt(r_err_condense[ir]**2 + r_err_condense[ir + 1]**2))
            occ_r_condense_comb.append(
                np.mean([occ_r_condense[ir], occ_r_condense[ir + 1]]))
        else:
            continue
    ########### end condensing for projections ################

    gr_r_hl = []
    gr_z_hl = []
    phi_avg_hl = np.array([np.mean(phi) for phi in phi_hl])
    avg_phi_sort = np.argsort(phi_avg_hl)
    r_hl = np.array(r_hl)
    r_err_hl = np.array(r_err_hl)
    z_hl = np.array(z_hl)
    z_err_hl = np.array(z_err_hl)
    occ_hl = np.array(occ_hl)
    r_hl = r_hl[avg_phi_sort]
    r_err_hl = r_err_hl[avg_phi_sort]
    z_hl = z_hl[avg_phi_sort]
    z_err_hl = z_err_hl[avg_phi_sort]
    occ_hl = occ_hl[avg_phi_sort]

    for hl in range(n_ladders):
        r_hl[hl] = np.array(r_hl[hl])
        r_err_hl[hl] = np.array(r_err_hl[hl])
        z_hl[hl] = np.array(z_hl[hl])
        z_err_hl[hl] = np.array(z_err_hl[hl])
        occ_z_hl = np.array(occ_hl[hl])
        occ_r_hl = np.array(occ_hl[hl])

        remove_z_hl = (z_hl[hl] > -12.5) * (z_hl[hl] < 12.5)
        remove_z_hl += (z_hl[hl] < -12.5) + (z_hl[hl] > 12.5)
        remove_blips_hl = (z_hl[hl] < -21) + (z_hl[hl] > -20)
        remove_blips_hl *= (z_hl[hl] < -14.5) + (z_hl[hl] > -13.5)
        remove_blips_hl *= (z_hl[hl] < -7.5) + (z_hl[hl] > -6.5)
        remove_blips_hl *= (z_hl[hl] < 5.75) + (z_hl[hl] > 6.5)
        remove_blips_hl *= (z_hl[hl] < 12.5) + (z_hl[hl] > 13.5)
        remove_blips_hl *= (z_hl[hl] < 19) + (z_hl[hl] > 20)
        z_new_hl = z_hl[hl][remove_z_hl * remove_blips_hl]
        r_new_hl = r_hl[hl][remove_z_hl * remove_blips_hl]
        r_err_new_hl = r_err_hl[hl][remove_z_hl * remove_blips_hl]
        z_err_new_hl = z_err_hl[hl][remove_z_hl * remove_blips_hl]
        occ_z_hl = occ_z_hl[remove_z_hl * remove_blips_hl]
        occ_r_hl = occ_r_hl[remove_z_hl * remove_blips_hl]

        r_sort = np.argsort(r_new_hl)
        z_sort = np.argsort(z_new_hl)
        r_new_hl = r_new_hl[r_sort]
        r_err_new_hl = r_new_hl[r_sort]
        occ_r_hl = occ_r_hl[r_sort]

        r_condense_hl = []
        r_err_condense_hl = []
        occ_r_condense_hl = []
        for ir, r in enumerate(r_new_hl):
            if ir % 2 == 0:
                r_condense_hl.append((r + r_new_hl[ir + 1]) / 2)
                r_err_condense_hl.append(
                    np.sqrt(r_err_new_hl[ir]**2 + r_new_hl[ir + 1]**2))
                occ_r_condense_hl.append(
                    np.mean([occ_r_hl[ir], occ_r_hl[ir + 1]]))
            else:
                continue

        z_new_hl = z_new_hl[z_sort]
        z_err_new_hl = z_err_new_hl[z_sort]
        occ_z_hl = occ_z_hl[z_sort]

        gr_r_hl.append(rt.TGraph())
        rnp.fill_graph(gr_r_hl[hl],
                       np.swapaxes([r_condense_hl, occ_r_condense_hl], 0, 1))
        gr_r_hl[hl].SetName("gr_r_occ_hl_" + str(hl))

        gr_z_hl.append(rt.TGraph())
        rnp.fill_graph(gr_z_hl[hl], np.swapaxes([z_new_hl, occ_z_hl], 0, 1))
        gr_z_hl[hl].SetName("gr_z_occ_hl_" + str(hl))

    gr_phi_ring = []
    z_avg_ring = np.array([np.mean(z) for z in z_ring])
    avg_z_sort = np.argsort(z_avg_ring)
    occ_ring = np.array(occ_ring)
    phi_ring = np.array(phi_ring)
    phi_ring = phi_ring[avg_z_sort]
    occ_ring = occ_ring[avg_z_sort]

    for ring in range(n_rings):
        phi_ring[ring] = np.array(phi_ring[ring])
        occ_phi_ring = np.array(occ_ring[ring])
        phi_sort = np.argsort(phi_ring[ring])
        phi_ring[ring] = phi_ring[ring][phi_sort]
        occ_phi_ring = occ_phi_ring[phi_sort]

        gr_phi_ring.append(rt.TGraph())
        rnp.fill_graph(gr_phi_ring[ring],
                       np.swapaxes([phi_ring[ring], occ_phi_ring], 0, 1))
        gr_phi_ring[ring].SetName("gr_phi_occ_ring_" + str(ring))

    gr_phi = rt.TGraph()
    rnp.fill_graph(gr_phi, np.swapaxes([phi_condense, occ_phi_condense], 0, 1))
    gr_phi.SetName("gr_phi_occ")

    gr_z = rt.TGraph()
    rnp.fill_graph(gr_z, np.swapaxes([z_condense, occ_z_condense], 0, 1))
    gr_z.SetName("gr_z_occ")

    gr_r3d = rt.TGraph()
    rnp.fill_graph(gr_r3d, np.swapaxes([r_condense, occ_r_condense], 0, 1))
    gr_r3d.SetName("gr_r_occ")
    gr_r3d = rt.TGraph()
    rnp.fill_graph(gr_r3d,
                   np.swapaxes([r_condense_comb, occ_r_condense_comb], 0, 1))
    gr_r3d.SetName("gr_r_occ_pm_comb")

    #file_out = rt.TFile("output_0_charge_l200_nosmear.root", "RECREATE")
    #file_out = rt.TFile("output_0p1_neg0p08_charge_l200_nosmear.root", "RECREATE")
    #file_out = rt.TFile("output_neg0p1_0p2_charge_l200_nosmear.root", "RECREATE")
    #file_out = rt.TFile("output_0p2_0p19_charge_l200_nosmear.root", "RECREATE")
    file_out = rt.TFile("output_neg0p3_neg0p32_charge_l200_nosmear.root",
                        "RECREATE")
    #file_out = rt.TFile("output_0_charge_ge200_nosmear.root", "RECREATE")
    #file_out = rt.TFile("output_0p1_neg0p08_charge_ge200_nosmear.root", "RECREATE")
    #file_out = rt.TFile("output_neg0p1_0p2_charge_ge200_nosmear.root", "RECREATE")
    #file_out = rt.TFile("output_0p2_0p19_charge_ge200_nosmear.root", "RECREATE")
    #file_out = rt.TFile("output_neg0p3_neg0p32_charge_ge200_nosmear.root", "RECREATE")

    gr_phi.Write()
    gr_z.Write()
    gr_r3d.Write()
    for hl in range(n_ladders):
        gr_r_hl[hl].Write()
        gr_z_hl[hl].Write()
    for ring in range(n_rings):
        gr_phi_ring[ring].Write()
    file_out.Close()
def plot_likelihood(plcInterval_1sd,
                    Yat_Xmax_1sd,
                    min_1sd,
                    max_1sd,
                    Yat_Xmax_2sd,
                    min_2sd,
                    max_2sd,
                    x_min=0.8,
                    x_max=1.2,
                    x_SM=1.0,
                    NPoints=20,
                    xlabel='R'):
    plccanvas = rt.TCanvas('fig_PL', 'PL', 800, 600)
    plcplot = rt.RooStats.LikelihoodIntervalPlot(plcInterval_1sd)
    plccanvas.cd()
    pad = setup_pad()
    pad.Draw()
    pad.cd()

    plcplot.SetRange(x_min, x_max)
    plcplot.SetMaximum(10)
    plcplot.SetLineColor(0)
    plcplot.SetNPoints(NPoints)
    plcplot.Draw("tf1")
    #plcplot.Draw()

    pad.cd()
    '''
    Yline_cutoff = rt.TLine(x_min,Yat_Xmax,x_max,Yat_Xmax);
    Yline_min = rt.TLine(min_2sd,0.,min_2sd,Yat_Xmax);
    Yline_max = rt.TLine(max_2sd,0.,max_2sd,Yat_Xmax);

    Yline_cutoff.SetLineColor(8)
    Yline_min.SetLineColor(8)
    Yline_max.SetLineColor(8)

    Yline_cutoff.Draw("same");
    Yline_min.Draw("same");
    Yline_max.Draw("same");
    '''

    hist = plcplot.GetPlottedObject()
    frame = rt.TGraph(hist)
    frame.GetYaxis().SetTitle('Profile of -log(L/L_{min})')
    frame.GetXaxis().SetTitle(xlabel)
    frame.GetYaxis().SetTitleOffset(0.9)
    frame.GetYaxis().SetTitleFont(42)
    frame.GetYaxis().SetTitleSize(0.04)
    frame.GetYaxis().SetLabelSize(0.04)
    frame.GetYaxis().SetLabelFont(42)
    frame.GetXaxis().SetTitleOffset(0.9)
    frame.GetXaxis().SetTitleFont(42)
    frame.GetXaxis().SetTitleSize(0.04)
    frame.GetXaxis().SetLabelSize(0.04)
    frame.GetXaxis().SetLabelFont(42)
    frame.SetMinimum(0)
    frame.Draw()

    pad.Update()

    # plot 95% limit
    hist_2sd = hist.Clone()
    y_2sd, edge_2sd = root_numpy.hist2array(hist_2sd, return_edges=True)
    edge_2sd = np.array(edge_2sd).flatten()
    x_2sd = np.convolve(edge_2sd, np.ones(2), 'valid') / 2.0
    tofill_2sd = [(x, y) for x, y in zip(x_2sd, y_2sd)
                  if min_2sd < x < max_2sd]
    tg_2sd = rt.TGraph()
    root_numpy.fill_graph(tg_2sd, tofill_2sd)
    tg_2sd.SetPoint(tg_2sd.GetN(), min_2sd, Yat_Xmax_2sd)
    tg_2sd.SetPoint(tg_2sd.GetN(), min_2sd, 0)
    tg_2sd.SetPoint(tg_2sd.GetN(), max_2sd, 0)
    tg_2sd.SetPoint(tg_2sd.GetN(), max_2sd, Yat_Xmax_2sd)
    tg_2sd.Sort()
    tg_2sd.SetFillColor(16)
    tg_2sd.SetFillStyle(1001)
    tg_2sd.Draw("F2 SAME")

    pad.Update()

    # plot 68.3% limit
    hist_1sd = hist.Clone()
    y_1sd, edge_1sd = root_numpy.hist2array(hist_1sd, return_edges=True)
    edge_1sd = np.array(edge_1sd).flatten()
    x_1sd = np.convolve(edge_1sd, np.ones(2), 'valid') / 2.0
    tofill_1sd = [(x, y) for x, y in zip(x_1sd, y_1sd)
                  if min_1sd < x < max_1sd]
    tg_1sd = rt.TGraph()
    root_numpy.fill_graph(tg_1sd, tofill_1sd)
    tg_1sd.SetPoint(tg_1sd.GetN(), min_1sd, Yat_Xmax_1sd)
    tg_1sd.SetPoint(tg_1sd.GetN(), min_1sd, 0)
    tg_1sd.SetPoint(tg_1sd.GetN(), max_1sd, 0)
    tg_1sd.SetPoint(tg_1sd.GetN(), max_1sd, Yat_Xmax_1sd)
    tg_1sd.Sort()
    tg_1sd.SetFillColor(14)
    tg_1sd.SetFillStyle(1001)
    tg_1sd.Draw("F2 SAME")

    pad.Update()

    frame.Draw("same")
    pad.Update()

    uymax = rt.gPad.GetUymax()
    uymin = rt.gPad.GetUymin()
    if x_SM is not None:
        pad.cd()
        l = rt.TLine(x_SM, uymin, x_SM, uymax)
        l.SetLineColor(2)
        l.SetLineWidth(2)
        l.Draw("same")

    pad.cd()
    CMS_lumi(False)
    plccanvas.cd()
    plccanvas.Update()

    # save canvases
    plccanvas.Draw()
    plccanvas.SaveAs('.pdf')
    return plccanvas
def fillgraph(graph, arrx, arry):
    arrxy = combinevectors(arrx, arry)
    root_numpy.fill_graph(graph, arrxy)
Example #13
0
def _roc_graph(data, classes, prelim=False):
    # pylint: disable=too-many-locals
    pos, neg = classes

    canvas = ROOT.TCanvas('c_{}vs{}.format(pos, neg)', '', 0, 0, 800, 600)
    canvas.SetLogx()

    graphs = ROOT.TMultiGraph()
    leg = ROOT.TLegend(0.63, 0.7, 0.9, 0.88)

    for layer in data:
        if pos == 3:
            pos_sel = data[layer]['Output_number_true'] >= pos
        else:
            pos_sel = data[layer]['Output_number_true'] == pos

        if neg == 3:
            neg_sel = data[layer]['Output_number_true'] >= neg
        else:
            neg_sel = data[layer]['Output_number_true'] == neg

        isel = np.where(
            np.logical_or(
                pos_sel,
                neg_sel,
            )
        )[0]

        fpr, tpr, _ = sklearn.metrics.roc_curve(
            data[layer]['Output_number_true'][isel],
            data[layer]['Output_number'][isel][:, pos - 1],
            pos_label=pos
        )
        auc = sklearn.metrics.auc(fpr, tpr)

        graph = ROOT.TGraph(fpr.size)
        _set_graph_style(graph, layer)
        root_numpy.fill_graph(graph, np.column_stack((fpr, tpr)))
        graphs.Add(graph)
        leg.AddEntry(graph, '{}, AUC: {:.2f}'.format(layer, auc), 'L')

    graphs.SetTitle(
        ';Pr(Estimated: {pos}-particle{ps} | True: {neg}-particle{ns});Pr(Estimated: {pos}-particle{ps} | True: {pos}-particle{ps})'.format(  # noqa
            pos=pos,
            neg=neg,
            ps=('' if pos == 1 else 's'),
            ns=('' if neg == 1 else 's')
        )
    )
    graphs.SetMaximum(1.5)
    graphs.Draw('AL')

    line = ROOT.TF1("line", "x", 0, 1)
    line.SetLineStyle(3)
    line.SetLineColor(ROOT.kGray + 2)
    line.Draw("same")
    leg.AddEntry(line, 'Random, AUC: 0.50', 'L')

    leg.SetTextSize(leg.GetTextSize() * 0.65)
    leg.SetBorderSize(0)
    leg.Draw()

    figures.draw_atlas_label(prelim)
    txt = ROOT.TLatex()
    txt.SetNDC()
    txt.SetTextSize(txt.GetTextSize() * 0.75)
    txt.DrawLatex(0.2, 0.82, 'PYTHIA8 dijet, 1.8 < p_{T}^{jet} < 2.5 TeV')

    canvas.SaveAs('ROC_{}vs{}.pdf'.format(pos, neg))
Example #14
0
 zdist=np.zeros([6,90])
 abins=np.arange(0,90)
 abins_rad=np.radians(abins)
 for k in range(5,6):
   print(pfiles[k])
   name='{0}_sn.out'.format(pfiles[k])
   f=open(name,'r')
   j=0
   for line in f:
     theta[k,j]=np.float(line.split()[2])
     j+=1
   theta[k,:]=np.degrees(np.arccos(theta[k,:]))
   angraph=ROOT.TGraph()
   ang_hist,bin=np.histogram(theta[k,:],bins=abins,density=True)
   angT=np.transpose(np.array([abins_rad[:-1],ang_hist]))
   rnp.fill_graph(angraph,angT)
   if pfiles[k]=='muon':
     afun=ROOT.TF1('angular','[0]*sin(x)*TMath::Power(cos(x),[1])',0,np.pi/2.0)
   else:
     afun=ROOT.TF1('angular','[0]*sin(x)*(1.0+[1]*TMath::Power(cos(x),[2]))',0,np.pi/2.0)
   afun.SetParLimits(2,0.5,4.0)
   angraph.Fit(afun,'R')
   p=afun.GetParameters()
   e=afun.GetParError(0)
   if pfiles[k]=='muon':
     zdist[k,:]=p[0]*np.sin(abins_rad)*(np.power(np.cos(abins_rad),p[1]))
     print(p[0],p[1])
   else:
     zdist[k,:]=p[0]*np.sin(abins_rad)*(1.0+p[1]*np.power(np.cos(abins_rad),p[2]))
     print(p[0],p[1],p[2])
   fig,ax=plt.subplots(nrows=1,ncols=1,sharex=False,sharey=False)
Example #15
0
    def save(self):

        # Save the toy-model source distributions in custom NumPy-oriented format.
        if self.sourceMode == "Mixture":

            self.sourceKinematicsDistribution.save(
                f"{self.directory}/numpy/sourceKinematicsDistribution")
            self.sourceTimeDistribution.save(
                f"{self.directory}/numpy/sourceTimeDistribution")
            np.savez(f"{self.directory}/numpy/sourceCorrelation",
                     sourceCorrelation=self.sourceCorrelation)

        # Save the source distributions in ROOT format.
        else:

            # Open a ROOT file to contain the source information.
            sourceFile = root.TFile(f"{self.directory}/source.root",
                                    "RECREATE")

            if self.sourceMode == "Histogram1D":

                # Write the kinematics and injection time histograms.
                self.sourceKinematicsHistogram.Write()
                self.sourceTimeHistogram.Write()

                # Write the correlation polynomial coefficients as entries in a single-branched TTree.
                rnp.array2tree(np.array(self.sourceCorrelation,
                                        dtype=[("sourceCorrelation",
                                                np.float32)]),
                               name="sourceCorrelation").Write()

            else:

                # Write the joint kinematics and injection time histogram.
                self.sourceJointHistogram.Write()

            sourceFile.Close()

        # Save simulation histograms in NumPy formats.
        self.frequencies.save(f"{self.directory}/numpy/frequencies")
        self.profile.save(f"{self.directory}/numpy/profile")
        self.joint.save(f"{self.directory}/numpy/joint")
        self.signal.save(f"{self.directory}/numpy/signal")

        # Prepare the truth-level radial TGraph.
        radial = root.TGraph()
        radial.SetName("radial")
        rnp.fill_graph(
            radial,
            np.stack((utilities.frequencyToRadius(
                self.frequencies.xCenters), self.frequencies.heights),
                     axis=-1))

        # Save simulation histograms in ROOT format.
        rootFile = root.TFile(f"{self.directory}/simulation.root", "RECREATE")
        self.frequencies.toRoot("frequencies",
                                ";Cyclotron Frequency (kHz);Entries").Write()
        self.profile.toRoot("profile", ";Injection Time (ns);Entries").Write()
        self.joint.toRoot(
            "joint", ";Injection Time (ns);Cyclotron Frequency (kHz)").Write()
        self.signal.toRoot("signal",
                           ";Time (us);Arbitrary Units",
                           xRescale=1E-3).Write()
        radial.Write()
        rootFile.Close()
Example #16
0
def find_best_working_point(effs, signal_efficiencies, background_efficiencies,
                            signal_probabilities, background_probabilities):
    # Compute ratios of background and signal probabilities and truncate the array
    # to match the gradient array size
    probability_ratios = background_probabilities / signal_probabilities
    probability_ratios = probability_ratios[1:]
    # Compute efficiency gradients
    effs_diff = np.ediff1d(effs)
    signal_efficiencies_diff = np.ediff1d(signal_efficiencies)
    background_efficiencies_diff = np.ediff1d(background_efficiencies)
    signal_efficiencies_diff = np.divide(signal_efficiencies_diff, effs_diff)
    background_efficiencies_diff = np.divide(background_efficiencies_diff,
                                             effs_diff)
    # Apply averaging over 3 or 2 values in order to smooth the gradients
    signal_efficiencies_diff_3 = np.convolve(signal_efficiencies_diff,
                                             np.repeat(1.0, 3.) / 3., 'valid')
    signal_efficiencies_diff_2 = np.convolve(signal_efficiencies_diff,
                                             np.repeat(1.0, 2.) / 2., 'valid')
    signal_efficiencies_diff = np.append(
        signal_efficiencies_diff_3,
        [signal_efficiencies_diff_2[-1], signal_efficiencies_diff[-1]])
    background_efficiencies_diff_3 = np.convolve(background_efficiencies_diff,
                                                 np.repeat(1.0, 3.) / 3.,
                                                 'valid')
    background_efficiencies_diff_2 = np.convolve(background_efficiencies_diff,
                                                 np.repeat(1.0, 2.) / 2.,
                                                 'valid')
    background_efficiencies_diff = np.append(
        background_efficiencies_diff_3,
        [background_efficiencies_diff_2[-1], background_efficiencies_diff[-1]])
    # Apply the signal and background probabilities in order to weight the efficiency gradients
    # If it is more likely to have background than signal in this bin, then the background efficiency gradient
    # will be increased accordingly
    background_efficiencies_diff = background_efficiencies_diff * probability_ratios
    # Interpolate and find points where background efficiency gradient > signal efficiency gradient (with some tolerance)
    interp_x = np.linspace(np.amin(effs[1:]), np.amax(effs[1:]), 1000)
    interp_signal = np.interp(interp_x, effs[1:], signal_efficiencies_diff)
    interp_background = np.interp(interp_x, effs[1:],
                                  background_efficiencies_diff)
    optimal_points = np.argwhere(
        np.greater(interp_background - 0.05, interp_signal)).ravel(
        )  # Use a tolerance of 0.02 in case of fluctuations
    if len(optimal_points) == 0:
        print 'WARNING: no working point found where the efficiency gradient is larger for background than for signal'
    # Find optimal point with smallest efficiency
    ## Compute spacing between points, and select those with an efficiency separation > 2%
    optimal_discontinuities = np.argwhere(
        np.ediff1d(interp_x[optimal_points]) > 0.02).ravel()
    ## Select the point with the largest efficiency
    optimal_index = np.amax(optimal_discontinuities) + 1 if len(
        optimal_discontinuities) > 0 else 0
    optimal_point = interp_x[optimal_points[optimal_index]]
    # Create graphs
    signal_efficiencies_diff_graph = Graph(len(effs) - 1)
    background_efficiencies_diff_graph = Graph(len(effs) - 1)
    optimal_points_graph = Graph(len(optimal_points))
    fill_graph(signal_efficiencies_diff_graph,
               np.column_stack((effs[1:], signal_efficiencies_diff)))
    fill_graph(background_efficiencies_diff_graph,
               np.column_stack((effs[1:], background_efficiencies_diff)))
    fill_graph(
        optimal_points_graph,
        np.column_stack(
            (interp_x[optimal_points], interp_signal[optimal_points])))
    signal_efficiencies_diff_graph.SetName('efficiencies_signal')
    background_efficiencies_diff_graph.SetName('efficiencies_background')
    optimal_points_graph.SetName('signal_background_optimal_points')
    return signal_efficiencies_diff_graph, background_efficiencies_diff_graph, optimal_points_graph, optimal_point