def plot_one_mg(run_number,alpha):
    (x_madgraph,y_madgraph)=library.MadGraph(run_number)
    a=library.create_pspline(x_madgraph,y_madgraph,alpha)
    (x_madgraph,y_madgraph,yd3_madgraph,madgraph_pspline)=a
    pltb=library.makePrettyPlots(x_madgraph, y_madgraph, yd3_madgraph,
                    title="MadGraph run"+str(run_number)+", alpha:"+str(alpha)
                    +", chi:"+str(library.chisquare(y_madgraph,yd3_madgraph)),
                    ymax = 2e5)
    return pltb
def alpha_spectrum(start,stop,list_of_alpha):
    number_of_events=library.number_of_files_in_folder()
    print("number of runs:", number_of_events)
    alpha=0.5
    factor=2
    operator=''
    for iteration in range(start,stop+1):
        alpha=0.1
        factor=2
        list_of_alpha.append(find_alpha(library.MadGraph(iteration),
                                        alpha,factor,operator))
    return list_of_alpha
def plot_all_mg(number_of_events,alpha):
    number_of_events=library.number_of_files_in_folder()
    for iteration in range(1,number_of_events+1):
            (x_madgraph,y_madgraph)=library.MadGraph(iteration)
            a=library.create_pspline(x_madgraph,y_madgraph,alpha)
            (x_madgraph,y_madgraph,yd3_madgraph,madgraph_pspline)=a
            pltb=library.makePrettyPlots(x_madgraph, y_madgraph, yd3_madgraph,
                            title="MadGraph run"+str(iteration)
                            +", alpha:"+str(alpha)+", chi:"
                            +str(library.chisquare(y_madgraph,yd3_madgraph)),
                            ymax = 2e5) 
            pltb.show()
def average_alpha():
    number_of_events=library.number_of_files_in_folder()
    #number_of_events=15
    print("number of runs:", number_of_events)
    list_of_chi=[]
    #alpha=500.625
    alpha=1#1.078125#20:0.875
    mean=0
    factor=1
    operator=''
    while True:
        list_of_chi=[]
        for iteration in range(1,number_of_events+1):
            (x_madgraph,y_madgraph)=library.MadGraph(iteration)
            a=library.create_pspline(x_madgraph,y_madgraph,alpha)
            (x_madgraph,y_madgraph,yd3_madgraph,madgraph_pspline)=a
            list_of_chi.append(library.chisquare(y_madgraph,yd3_madgraph))

        mean=sum(list_of_chi)/number_of_events
        print('Average Chi: ',mean)
        if mean<0.999:
            if operator=="SUBTRACTING":
                factor=factor/2
            operator="ADDING"
            alpha +=0.5*factor
        elif mean>1.001:
            if operator=="ADDING":
                factor=factor/2
            operator="SUBTRACTING"
            alpha =alpha-0.5*factor
        else:
            print('-----------------------------------------------------------')
            print("Best alpha is:",alpha,"\nIt gives a average chi of: ",mean)
            print('-----------------------------------------------------------')
            break
        print("Current alpha: ", alpha)

    atlas_x,atlas_y=library.atlas()
    (x,y,yd3,atlas_pspline)=library.create_pspline(atlas_x,atlas_y,alpha=alpha)
    chi_atlas=library.chisquare(y,yd3)
    
    print("chi atlas: ",chi_atlas)
    print('-----------------------------------------------------------')
    #plta=library.makePrettyPlots(x, y, yd3, title="ATLAS, alpha:"+str(alpha)
    #                             +", chi:"+str(chi_atlas), ymax = 2e5)
    return alpha,list_of_chi
def test():
    alpha = 0.8187500000000001
    atlas_x, atlas_y = library.atlas()
    (x, y, yd3, atlas_pspline) = library.create_pspline(atlas_x,
                                                        atlas_y,
                                                        alpha=alpha)
    chi_atlas = library.chisquare(y, yd3)
    print("chi atlas: ", chi_atlas)
    print('-----------------------------------------------------------')
    plta = library.makePrettyPlots(x,
                                   y,
                                   yd3,
                                   title="ATLAS data fitted with " +
                                   r"$\alpha=$" + str(round(alpha, 2)) + "\n" +
                                   r'$\chi^2=$' + str(round(chi_atlas, 2)))
    list_of_chi_final = []
    iteration = 18
    (x_madgraph, y_madgraph) = library.MadGraph(iteration)
    a = library.create_pspline(x_madgraph, y_madgraph, alpha)
    (x_madgraph, y_madgraph, yd3_madgraph, madgraph_pspline) = a
    chi_madgraph = library.chisquare(y_madgraph, yd3_madgraph)
    plta.show()