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 fit_atlas(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('-----------------------------------------------------------')
    return library.makePrettyPlots(x, y, yd3, title="ATLAS, alpha:"
                                   +str(alpha)+", chi:"+str(chi_atlas))
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 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 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()
def find_alpha(tuuple,alpha,factor,operator):
    while True:
        x,y=tuuple
        (x,y,yd3,pspline)=library.create_pspline(x,y,alpha)
        this_chi=(library.chisquare(y,yd3))
        #print('Run: ',iteration,' Chi: ',this_chi,' alpha: ',alpha)
        if 0.999<this_chi<1.001:
            break
        elif this_chi<0.999:
            if operator=="SUBTRACTING":
                factor=factor/2
            operator="ADDING"
            alpha +=0.5*factor
        elif this_chi>1.001:
            if operator=="ADDING":
                factor=factor/2
            operator="SUBTRACTING"
            alpha =alpha-0.5*factor
            if alpha<0:
                while alpha<0:
                    alpha=alpha+1.5
                    factor=factor/2
    return alpha