예제 #1
0
        #'''
        name = "nominal_%s_njets%d" % (s,j)
        if s=='ttbar':
            params_dict[name] = {'fix':True,'start_val':nominal[j][i],'limits':(0,nd)}
        else:
            params_dict[name] = {'fix':True,'start_val':nominal[j][i],'limits':(0,nd)}

        name = "uncert_%s_njets%d" % (s,j)
        if s=='ttbar':
            params_dict[name] = {'fix':True,'start_val':uncert[j][i],'limits':(0,nd)}
        else:
            params_dict[name] = {'fix':True,'start_val':uncert[j][i],'limits':(0,nd)}
        #'''


params_names,kwd = fitutils.dict2kwd(params_dict)
kwd['errordef'] = 0.5 # For maximum likelihood method
#kwd['print_level'] = 0

data_and_pdfs = [data,templates]

f = fitutils.Minuit_FCN([data_and_pdfs],params_dict,emlf_normalized_minuit)

m = minuit.Minuit(f,**kwd)
#m.tol = 100

m.migrad(ncall=10000)
print "RUNNING HESSE"
m.hesse()

#m.minos()
def main():

    ############################################################################
    # Parse the command lines.
    ############################################################################
    parser = argparse.ArgumentParser()
    parser.add_argument('--fit', dest='fit', type=int,\
            default=0, help='Which fit to perform (0,1,2)')
    parser.add_argument('--verbose', dest='verbose', action='store_true',\
            default=False, help='Verbose output')
    parser.add_argument('--dataset', dest='dataset', type=str,\
            default='nicole', help='Dataset to use in fitting. Nicoles simulated (nicole) or Juans pulser (juan)')
    parser.add_argument('--batch', dest='batch', action='store_true',\
            default=False, help='Run in batch mode (exit on completion).')

    args = parser.parse_args()

    ############################################################################

    #tag = 'pulser_onelognormal'
    #tag = 'pulser'
    #tag = 'pulser_zoomed_in'
    #tag = 'pulser_simulated_Nicole'
    #tag = 'pulser_simulated_Nicole_zoomed_in'
    #tag = 'pulser_simulated_Nicole_onelognormal'
    #tag = 'pulser_simulated_Nicole_no_fit'

    tag = "risetime_determination_%s" % (args.dataset)


    outfilename = "risetime_parameters_%s.py" % (tag)
    outfile = open(outfilename,'w')
    outfile.write("def risetime_parameters():\n\n")

    '''
    if args.help:
        parser.print_help()
        exit(-1)
    '''

    ############################################################################
    # Read in the data
    ############################################################################
    
    infile_name = 'data/pulser_data_325ns.dat' # SIMULATED DATA FROM NICOLE
    if args.dataset=='nicole':
        infile_name = 'data/pulser_data_325ns.dat' # SIMULATED DATA FROM NICOLE
    elif args.dataset=='juan':
        infile_name = 'data/pulser_data.dat' # FROM JUAN, 8/2/13, manually scanned pulser runs.

    tdays,energies,rise_times = get_3yr_cogent_data(infile_name,first_event=first_event,calibration=0)
    print (tdays)
    print (energies)
    print (rise_times)

    print (energies)
    if args.verbose:
        print_data(energies,tdays,rise_times)

    data = [energies.copy(),tdays.copy(),rise_times.copy()]
    print(("data before range cuts: ",len(data[0]),len(data[1]),len(data[2])))


    ############################################################################
    # Declare the ranges.
    ############################################################################
    ranges,subranges,nbins = parameters.fitting_parameters(args.fit)
    
    bin_widths = np.ones(len(ranges))
    for i,n,r in zip(list(range(len(nbins))),nbins,ranges):
        bin_widths[i] = (r[1]-r[0])/n

    # Cut events out that fall outside the range.
    data = cut_events_outside_range(data,ranges)
    data = cut_events_outside_subrange(data,subranges[1],data_index=1)

    if args.verbose:
        print_data(energies,tdays)

    print(("data after  range cuts: ",len(data[0]),len(data[1])))

    nevents = float(len(data[0]))

    '''
    plt.figure()
    plt.plot(energies,rise_times,'o',markersize=1.5)
    plt.yscale('log')
    plt.ylim(0.1,10)

    plt.figure()
    plt.plot(tdays,rise_times,'o',markersize=1.5)
    plt.yscale('log')
    plt.ylim(0.1,10)
    '''

    ############################################################################
    # Plot the data
    ############################################################################
    ############################################################################
    # Look at the rise-time information.
    ############################################################################

    # Will use this later when trying to figure out the energy dependence of 
    # the log-normal parameters.
    # define our (line) fitting function
    expfunc = lambda p, x: p[1]*np.exp(-p[0]*x) + p[2]
    errfunc = lambda p, x, y, err: (y - expfunc(p, x)) / err

    ############################################################################
    # Starting values for fits. 
    ############################################################################
    # For the data (two lognormals)
    #starting_params = [-0.6,0.6,0.2*nevents,  0.1,0.8,0.8*nevents]
    # For the pulser fast rise times (two lognormals)
    starting_params = [-0.6,0.5,0.6*nevents,  0.5,0.8,0.4*nevents]

    fit_parameters = []
    fit_errors = []
    fit_mnerrors = []
    nevs = []
    axrt = []

    elo = 0.0
    ehi = 1.0
    eoffset = 0.5

    ewidth = 0.15
    estep = 0.15

    #ewidth = 0.200
    #estep = 0.050

    expts = []

    figcount = 0
    for i in range(0,24):
        
        j = i
        if j%6==0:
            figrt = plt.figure(figsize=(12,6),dpi=100)
        axrt.append(figrt.add_subplot(2,3, i%6 + 1))

        #figrt = plt.figure(figsize=(6,4),dpi=100)
        #axrt.append(figrt.add_subplot(1,1,1))

        data_to_fit = []
        #h,xpts,ypts,xpts_err,ypts_err = lch.hist_err(data[1],bins=nbins[1],range=ranges[1],axes=ax1)

        if i>=0:
            elo = i*estep + eoffset
            ehi = elo + ewidth
            index0 = data[0]>=elo
            index1 = data[0]< ehi
            print((elo,ehi))
            index = index0*index1
            data_to_fit = data[2][index]

        if len(data_to_fit)>0:
            lch.hist_err(data_to_fit,bins=nbins[2],range=ranges[2],axes=axrt[j])
            plt.ylim(0)
            plt.xlim(ranges[2][0],ranges[2][1])
            name = "Energy: %0.2f-%0.2f (keVee)" % (elo,ehi)
            plt.text(0.20,0.75,name,transform=axrt[j].transAxes)
            print ("=======-------- E BIN ----------===========")
            print (name)

        nevents = len(data_to_fit)
        print(("Nevents for this fit: ",nevents))
        #starting_params = [-0.6,0.6,0.2*nevents,  0.6,0.55,0.8*nevents]
        # For pulser fits
        #starting_params = [-0.1,0.8,0.2*nevents,  0.6,0.55,0.8*nevents]
        '''
        if i==0:
            starting_params = [-0.6,0.6,0.2*nevents,  0.6,0.55,0.8*nevents]
        '''
        '''
        if elo>=1.0 and elo<1.2:    
            starting_params = [0.1,0.2,0.3*nevents,  0.2,3.0,0.7*nevents]
        '''
        ############################################################################
        # Declare the fit parameters
        ############################################################################
        params_dict = {}
        params_dict['flag'] = {'fix':True,'start_val':args.fit} 
        params_dict['var_rt'] = {'fix':True,'start_val':0,'limits':(ranges[2][0],ranges[2][1])}
        #params_dict['fast_logn_mean'] = {'fix':False,'start_val':0.005,'limits':(-2,2),'error':0.1}
        #params_dict['fast_logn_sigma'] = {'fix':False,'start_val':0.5,'limits':(0.01,5),'error':0.1}
        #params_dict['fast_num'] = {'fix':False,'start_val':0.2*nevents,'limits':(0.0,1.5*nevents),'error':0.1}
        #params_dict['slow_logn_mean'] = {'fix':False,'start_val':0.5,'limits':(-2,2),'error':0.1}
        #params_dict['slow_logn_sigma'] = {'fix':False,'start_val':1.0,'limits':(0.01,5),'error':0.1}
        #params_dict['slow_num'] = {'fix':False,'start_val':0.8*nevents,'limits':(0.0,1.5*nevents),'error':0.1}

        #starting_params = [1.0,1.2,0.6*nevents,  0.1,0.8,0.4*nevents]

        # Worked for 1.0-1.25
        #params_dict['fast_logn_mean'] = {'fix':False,'start_val':1.000,'limits':(-2,2),'error':0.1}
        #params_dict['fast_logn_sigma'] = {'fix':False,'start_val':1.2,'limits':(0.01,5),'error':0.1}
        #params_dict['fast_num'] = {'fix':False,'start_val':0.6*nevents,'limits':(0.0,1.5*nevents),'error':0.1}
        #params_dict['slow_logn_mean'] = {'fix':False,'start_val':0.1,'limits':(-2,2),'error':0.1}
        #params_dict['slow_logn_sigma'] = {'fix':False,'start_val':0.8,'limits':(0.01,5),'error':0.1}
        #params_dict['slow_num'] = {'fix':False,'start_val':0.4*nevents,'limits':(0.0,1.5*nevents),'error':0.1}

        params_dict['fast_logn_mean'] = {'fix':False,'start_val':starting_params[0],'limits':(-2,2),'error':0.01}
        params_dict['fast_logn_sigma'] = {'fix':False,'start_val':starting_params[1],'limits':(0.05,30),'error':0.01}
        params_dict['fast_num'] = {'fix':False,'start_val':nevents,'limits':(0.0,1.5*nevents),'error':0.01}

        #params_dict['slow_logn_mean'] = {'fix':False,'start_val':starting_params[3],'limits':(-2,2),'error':0.01}
        #params_dict['slow_logn_sigma'] = {'fix':False,'start_val':starting_params[4],'limits':(0.05,30),'error':0.01}
        #params_dict['slow_num'] = {'fix':False,'start_val':starting_params[5],'limits':(0.0,1.5*nevents),'error':0.01}

        # For the pulser fits
        #params_dict['slow_logn_mean'] = {'fix':True,'start_val':0.000,'limits':(-0.002,0.002),'error':0.000001}
        #params_dict['slow_logn_sigma'] = {'fix':True,'start_val':1.000,'limits':(0.9005,1.002),'error':0.000001}
        #params_dict['slow_num'] = {'fix':True,'start_val':0.001,'limits':(0.0,0.002),'error':0.000001}

        # float them
        params_dict['slow_logn_mean'] = {'fix':False,'start_val':starting_params[3],'limits':(-2,2),'error':0.01}
        params_dict['slow_logn_sigma'] = {'fix':False,'start_val':starting_params[4],'limits':(0.05,30),'error':0.01}
        params_dict['slow_num'] = {'fix':False,'start_val':starting_params[5],'limits':(0.0,1.5*nevents),'error':0.01}
        # To try one lognormal
        #params_dict['slow_logn_mean'] = {'fix':True,'start_val':starting_params[3],'limits':(-2,2),'error':0.01}
        #params_dict['slow_logn_sigma'] = {'fix':True,'start_val':starting_params[4],'limits':(0.05,30),'error':0.01}
        #params_dict['slow_num'] = {'fix':True,'start_val':0.1,'limits':(0.0,1.5*nevents),'error':0.01}

        # Above some value, lock down the second log normal, as the distribution is pretty well 
        # fit with just one log-normal. 
        elomax = 2.8
        if args.dataset=='nicole':
            elomax = 2.8
        elif args.dataset=='juan':
            elomax = 2.2

        if elo>=elomax:
            params_dict['slow_logn_mean'] = {'fix':True,'start_val':0.0,'limits':(-2,2),'error':0.01}
            params_dict['slow_logn_sigma'] = {'fix':True,'start_val':1.0,'limits':(0.05,30),'error':0.01}
            params_dict['slow_num'] = {'fix':True,'start_val':1,'limits':(0.0,1.5*nevents),'error':0.01}

        '''
        if i==0:
            None
            # From Nicole's simulation.
            #params_dict['fast_logn_mean'] = {'fix':True,'start_val':-0.10,'limits':(-2,2),'error':0.01}
            # From Juan
            #params_dict['fast_logn_mean'] = {'fix':True,'start_val':-0.60,'limits':(-2,2),'error':0.01}
            #params_dict['slow_logn_sigma'] = {'fix':True,'start_val':0.50,'limits':(0.05,30),'error':0.01}
        '''

        # Try fixing the slow sigma
        #params_dict['slow_logn_sigma'] = {'fix':True,'start_val':0.52,'limits':(-2,2),'error':0.01}

        #figrt.subplots_adjust(left=0.07, bottom=0.15, right=0.95, wspace=0.2, hspace=None,top=0.85)
        #figrt.subplots_adjust(left=0.05, right=0.98)
        #figrt.subplots_adjust(left=0.15, right=0.98,bottom=0.15)
        figrt.subplots_adjust(left=0.07, right=0.98,bottom=0.10)
        #plt.show()
        #exit()

        ############################################################################
        # Fit
        ############################################################################

        if i>=0 and len(data_to_fit)>0:
            params_names,kwd = fitutils.dict2kwd(params_dict)
        
            #print data_to_fit
            f = fitutils.Minuit_FCN([[data_to_fit]],params_dict,emlf)

            # For maximum likelihood method.
            kwd['errordef'] = 0.5
            kwd['print_level'] = 0
            #print kwd

            m = minuit.Minuit(f,**kwd)

            m.print_param()

            m.migrad()
            #m.hesse()
            m.minos()

            print ("Finished fit!!\n")

            values = m.values # Dictionary
            errors = m.errors # Dictionary
            mnerrors = m.get_merrors()
            print ("MNERRORS: ")
            print (mnerrors)
            fit_parameters.append(values)
            fit_errors.append(errors)
            fit_mnerrors.append(mnerrors)
            nevs.append(len(data_to_fit))

            xpts = np.linspace(ranges[2][0],ranges[2][1],1000)
            tot_ypts = np.zeros(len(xpts))

            ypts  = pdfs.lognormal(xpts,values['fast_logn_mean'],values['fast_logn_sigma'],ranges[2][0],ranges[2][1])
            y,plot = plot_pdf(xpts,ypts,bin_width=bin_widths[2],scale=values['fast_num'],fmt='r--',linewidth=2,axes=axrt[j])
            tot_ypts += y

            ypts  = pdfs.lognormal(xpts,values['slow_logn_mean'],values['slow_logn_sigma'],ranges[2][0],ranges[2][1])
            y,plot = plot_pdf(xpts,ypts,bin_width=bin_widths[2],scale=values['slow_num'],fmt='r:',linewidth=2,axes=axrt[j])
            tot_ypts += y

            axrt[j].plot(xpts,tot_ypts,'r',linewidth=2)

            axrt[j].set_ylabel(r'Events')
            axrt[j].set_xlabel(r'Rise time ($\mu$s)')
            axrt[j].set_xlim(0,5.0)

            '''
            name = "Plots/rt_slice_%d.png" % (figcount)
            if j%6==5:
                plt.savefig(name)
                figcount += 1
            '''

            #'''
            if math.isnan(values['fast_logn_mean']) == False:
                starting_params = [ \
                values['fast_logn_mean'], \
                values['fast_logn_sigma'], \
                values['fast_num'], \
                values['slow_logn_mean'], \
                values['slow_logn_sigma'],
                values['slow_num'] \
                ]
            #'''

            expts.append((ehi+elo)/2.0)

        if j%6==5:
            name = "Plots/rt_slice_%s_%d.png" % (tag,j/6)
            plt.savefig(name)

    print (fit_parameters)
    print (nevs)
    
    ypts = [[],[],[],[],[],[]]
    yerr = [[],[],[],[],[],[]]
    yerrlo = [[],[],[],[],[],[]]
    yerrhi = [[],[],[],[],[],[]]
    npts = []

    if len(expts)>0:
        #for i,fp,fe,n in zip(range(len(nevs)),fit_parameters,fit_errors,nevs):
        for i,fp,fe,n in zip(list(range(len(nevs))),fit_parameters,fit_mnerrors,nevs):
            print ("----------")
            #ypts[0].append(fp['fast_logn_mean'])
            #ypts[1].append(fp['fast_logn_sigma'])
            #ypts[2].append(fp['fast_num'])
            #ypts[3].append(fp['slow_logn_mean'])
            #ypts[4].append(fp['slow_logn_sigma'])
            #ypts[5].append(fp['slow_num'])

            pars = ['fast_logn_mean','fast_logn_sigma','fast_num',\
                    'slow_logn_mean','slow_logn_sigma','slow_num']

            for i,p in enumerate(pars):
                if p in fe:
                #if fe.has_key(p):
                    ypts[i].append(fp[p])
                    yerrlo[i].append(abs(fe[p]['lower']))
                    yerrhi[i].append(abs(fe[p]['upper']))
                else:
                    ypts[i].append(0.0)
                    yerrlo[i].append(0.0)
                    yerrhi[i].append(0.0)


            npts.append(n)

        for i in range(len(ypts)):
            ypts[i] = np.array(ypts[i])
            yerrlo[i] = np.array(yerrlo[i])
            yerrhi[i] = np.array(yerrhi[i])

        colors = ['r','b']
        labels = ['narrow','wide']

        ########################################################################
        # Use all or some of the points in the fit.
        ########################################################################
        index = np.arange(1,16)
        if args.dataset=='nicole':
            index = np.arange(1,16)
        elif args.dataset=='juan':
            index = np.arange(0,9)

        
        #xp = np.linspace(min(expts),max(expts),100)
        xp = np.linspace(min(expts),expts[17],100)
        if args.dataset=='nicole':
            xp = np.linspace(min(expts),expts[17],100)
        elif args.dataset=='juan':
            xp = np.linspace(min(expts),expts[8],100)

        expts = np.array(expts)

        fvals2 = plt.figure(figsize=(13,4),dpi=100)

        yfitpts = []

        for k in range(0,3):
            # Some of the broad rise times are set to 0.
            #index0s = ypts[3+k]!=0
            #index0s = np.ones(len(ypts[3+k])).astype(bool)
            index0s = np.ones(16).astype(bool)

            fvals2.add_subplot(1,3,k+1)

            tempypts = ypts[0+k]-ypts[3+k]
            # Fractional error
            tempyerrlo = np.sqrt((yerrlo[0+k])**2 + (yerrlo[3+k])**2)
            tempyerrhi = np.sqrt((yerrhi[0+k])**2 + (yerrhi[3+k])**2)
            if k>1:
                tempypts = ypts[0+k][index0s]/ypts[3+k][index0s]
                tempyerrlo = np.sqrt((yerrlo[0+k][index0s]/ypts[3+k][index0s])**2 + (yerrlo[3+k][index0s]*(ypts[0+k][index0s]/(ypts[3+k][index0s]**2)))**2)
                tempyerrhi = np.sqrt((yerrhi[0+k][index0s]/ypts[3+k][index0s])**2 + (yerrhi[3+k][index0s]*(ypts[0+k][index0s]/(ypts[3+k][index0s]**2)))**2)

            plt.errorbar(expts[index0s],tempypts[index0s],xerr=0.01,yerr=[tempyerrlo[index0s],tempyerrhi[index0s]],\
                    fmt='o',ecolor='k',mec='k',mfc='m',label='Ratio')

            if k==0:
                plt.ylabel(r'$\Delta \mu$')
            elif k==1:
                plt.ylabel(r'$\Delta \sigma$')
            elif k==2:
                plt.ylabel(r'# wide/# narrow')

            plt.xlim(0.5,3.5)
            plt.xlabel('Energy (keVee)')


            ########################################################################
            # Fit to exponentials.
            ########################################################################
            # Choose appropriate starting values, depending on the dataset.
            pinit = [1,1,1]
            if args.dataset=='nicole':
                if k==0:
                    pinit = [1.0, 1.0, -1.2]
                elif k==1:
                    #pinit = [1.0, -1.0, -0.5]
                    pinit = [-3.0,0.0015,-0.4]
                elif k==2:
                    pinit = [-2.0, 1.0, 2.0]
            elif args.dataset=='juan':
                if k==0:
                    pinit = [1.0, 1.0, -1.2]
                elif k==1:
                    #pinit = [1.0, -1.0, -0.5]
                    pinit = [-3.0,0.0015,-0.4]
                elif k==2:
                    pinit = [-2.0, 1.0, 2.0]
            
            out = leastsq(errfunc, pinit, args=(expts[index], tempypts[index], (tempyerrlo[index]+tempyerrhi[index])/2.0), full_output=1)
            z = out[0]
            zcov = out[1]
            #print "Differences and ratios: %d [%f,%f,%f]" % (k,z[0],z[1],z[2])
            variable = None
            if (k==0):
                variable = "fast_mean_rel_k"
            if (k==1):
                variable = "fast_sigma_rel_k"
            elif (k==2):
                variable = "fast_num_rel_k"
            output = "\t%s = [%f,%f,%f]\n" % (variable,z[0],z[1],z[2])
            print (output)
            outfile.write(output)
            #print "zcov: ",zcov
            '''
            if zcov is not None:
                print "Differences and ratios: %d [%f,%f,%f]" % (k,np.sqrt(zcov[0][0]),np.sqrt(zcov[1][1]),np.sqrt(zcov[2][2]))
            '''
            yfitpts = expfunc(z,xp)
            #print zcov
            plt.plot(xp,yfitpts,'-',color='m')

        fvals2.subplots_adjust(left=0.10, right=0.98,bottom=0.15,wspace=0.25,hspace=0.25)
        name = 'Plots/rt_summary_%s_1.png' % (tag)
        plt.savefig(name)

        outfile.write("\n")

        ########################################################################
        # Try to fit the individual distributions.
        ########################################################################
        yfitpts = []
        for i in range(0,6):
            yfitpts.append(np.zeros(len(xp)))

        fvals = plt.figure(figsize=(13,4),dpi=100)
        for k in range(0,3):
            fvals.add_subplot(1,3,k+1)
            for ik in range(0,2):
                nindex = k+3*ik
                #print "HERERERERE"
                #print ypts[nindex]
                #print ypts[nindex][ypts[nindex]!=0]
                print((len(yerrlo[nindex][ypts[nindex]!=0])))
                print((len(yerrhi[nindex][ypts[nindex]!=0])))
                plt.errorbar(expts[ypts[nindex]!=0],ypts[nindex][ypts[nindex]!=0],xerr=0.01,yerr=[yerrlo[nindex][ypts[nindex]!=0],yerrhi[nindex][ypts[nindex]!=0]],\
                        fmt='o',ecolor='k',mec='k',mfc=colors[ik],label=labels[ik])

                #'''
                # Use part of the data
                #index0 = np.arange(0,3)
                #index1 = np.arange(7,len(expts))
                #index = np.append(index0,index1)

                # Use all or some of the points
                index = np.arange(1,16)
                if args.dataset=='nicole':
                    index = np.arange(1,15)
                elif args.dataset=='juan':
                    index = np.arange(0,7)

                ########################################################################
                # Fit to exponentials.
                ########################################################################
                pinit = [1,1,1]
                if ik==0 and k==0:
                    pinit = [1.0, 1.0, -1.2]
                elif ik==0 and k==1:
                    pinit = [4.0, 2.0, 0.0]
                elif ik==0 and k==2:
                    pinit = [2.0, 2000.0, 300.0]
                elif ik==1:
                    pinit = [3.0, 1.5, 0.5]
                
                out = leastsq(errfunc, pinit, args=(expts[index], ypts[nindex][index], (yerrlo[nindex][index]+yerrhi[nindex][index])/2.0), full_output=1)
                z = out[0]
                zcov = out[1]
                variable = None
                if (k==0):
                    variable = "fast_mean0_k"
                if (k==1):
                    variable = "fast_sigma0_k"
                elif (k==2):
                    variable = "fast_num0_k"
                #print "Data points: %d %d [%f,%f,%f]" % (k,ik,z[0],z[1],z[2])
                if (ik==0):
                    output = "\t%s = [%f,%f,%f]\n" % (variable,z[0],z[1],z[2])
                    outfile.write(output)
                    print (output)
                #print "Data points: %d %d [%f,%f,%f]" % (k,ik,np.sqrt(zcov[0][0]),np.sqrt(zcov[1][1]),np.sqrt(zcov[2][2]))
                yfitpts[nindex] = expfunc(z,xp)
                #print zcov
                plt.plot(xp,yfitpts[nindex],'-',color=colors[ik])
                #'''

            if k==0:
                plt.ylim(-1.5,1.5)
            elif k==1:
                plt.ylim(0,1.5)
            plt.xlabel('Energy (keVee)')
            if k==0:
                plt.ylabel(r'Lognormal $\mu$')
            elif k==1:
                plt.ylabel(r'Lognormal $\sigma$')
            elif k==2:
                plt.ylabel(r'Number of events')
            plt.legend()

        #fval
        '''
        fvals.add_subplot(2,3,4)
        plt.plot(xp,yfitpts[3]-yfitpts[0],'-',color='m')

        fvals.add_subplot(2,3,5)
        plt.plot(xp,yfitpts[4]-yfitpts[1],'-',color='m')

        fvals.add_subplot(2,3,6)
        plt.plot(xp,yfitpts[5]/yfitpts[2],'-',color='m')
        '''

        fvals.subplots_adjust(left=0.10, right=0.98,bottom=0.15,wspace=0.25,hspace=0.25)
        name = 'Plots/rt_summary_%s_0.png' % (tag)
        plt.savefig(name)

        np.savetxt('rt_parameters.txt',[expts,ypts[0],ypts[1],ypts[2],ypts[3],ypts[4],ypts[5],npts])
        #'''

    #print "Sum ypts[5]: ",sum(ypts[5])

    outfile.write("\n\treturn fast_mean_rel_k,fast_sigma_rel_k,fast_num_rel_k,fast_mean0_k,fast_sigma0_k,fast_num0_k\n")

    if not args.batch:
        plt.show()
예제 #3
0
        #'''
        name = "nominal_%s_njets%d" % (s,j)
        if s=='ttbar':
            params_dict[name] = {'fix':True,'start_val':nominal[j][i],'limits':(0,nd)}
        else:
            params_dict[name] = {'fix':True,'start_val':nominal[j][i],'limits':(0,nd)}

        name = "uncert_%s_njets%d" % (s,j)
        if s=='ttbar':
            params_dict[name] = {'fix':True,'start_val':uncert[j][i],'limits':(0,nd)}
        else:
            params_dict[name] = {'fix':True,'start_val':uncert[j][i],'limits':(0,nd)}
        #'''


params_names,kwd = fitutils.dict2kwd(params_dict)
kwd['errordef'] = 0.5 # For maximum likelihood method
#kwd['print_level'] = 1

data_and_pdfs = [data,templates]

f = fitutils.Minuit_FCN([data_and_pdfs],params_dict,emlf_normalized_minuit)

m = minuit.Minuit(f,**kwd)
#m.tol = 100

m.migrad(ncall=10000)
print "RUNNING HESSE"
m.hesse()

#m.minos()
def main():

    ############################################################################
    # Parse the command lines.
    ############################################################################
    parser = argparse.ArgumentParser()
    parser.add_argument('--fit', dest='fit', type=int,\
            default=0, help='Which fit to perform (0,1,2)')
    parser.add_argument('--verbose', dest='verbose', action='store_true',\
            default=False, help='Verbose output')
    parser.add_argument('--rt-parameters', dest='rt_parameters', type=str,\
            default='risetime_parameters_risetime_determination_nicole.py', help='File of rise-time parameters determined from wavelet or pulser data.')
    parser.add_argument('--batch', dest='batch', action='store_true',\
            default=False, help='Run in batch mode (exit on completion).')

    args = parser.parse_args()

    #tag = 'data_constrained_with_pulser_mean20_sigma20_slowsigfloat'
    #tag = 'data_constrained_with_pulser'
    #tag = 'data_constrained_with_simulated_Nicole'

    ############################################################################

    '''
    if args.help:
        parser.print_help()
        exit(-1)
    '''

    # Read in the parameters from the file passed in on the commandline
    rt_parameters_filename = args.rt_parameters.rstrip('.py')
    tag = rt_parameters_filename
    print(("Rise-time parameters_filename: %s" % (rt_parameters_filename)))
    rt_parameters_file = __import__(rt_parameters_filename)
    risetime_parameters = getattr(rt_parameters_file,'risetime_parameters')

    fast_mean_rel_k,fast_sigma_rel_k,fast_num_rel_k,fast_mean0_k,fast_sigma0_k,fast_num0_k = risetime_parameters()

    ############################################################################
    # Read in the data
    ############################################################################
    infile_name = 'data/LE.txt'
    #infile_name = 'data/HE.txt'
    #infile_name = 'data/pulser_data.dat'
    tdays,energies,rise_times = get_3yr_cogent_data(infile_name,first_event=first_event,calibration=0)
    print (tdays)
    print (energies)
    print (rise_times)

    print (energies)
    if args.verbose:
        print_data(energies,tdays,rise_times)

    #data = [energies.copy(),tdays.copy()]
    #print "data before range cuts: ",len(data[0]),len(data[1])

    # 3yr data
    data = [energies.copy(),tdays.copy(),rise_times.copy()]
    print(("data before range cuts: ",len(data[0]),len(data[1]),len(data[2])))
    #exit()


    ############################################################################
    # Declare the ranges.
    ############################################################################
    ranges,subranges,nbins = parameters.fitting_parameters(args.fit)
    
    bin_widths = np.ones(len(ranges))
    for i,n,r in zip(list(range(len(nbins))),nbins,ranges):
        bin_widths[i] = (r[1]-r[0])/n

    # Cut events out that fall outside the range.
    data = cut_events_outside_range(data,ranges)
    data = cut_events_outside_subrange(data,subranges[1],data_index=1)

    if args.verbose:
        print_data(energies,tdays)

    print(("data after  range cuts: ",len(data[0]),len(data[1])))

    nevents = float(len(data[0]))

    plt.figure()
    plt.plot(energies,rise_times,'o',markersize=1.5)
    plt.yscale('log')
    plt.ylim(0.1,10)

    plt.figure()
    plt.plot(tdays,rise_times,'o',markersize=1.5)
    plt.yscale('log')
    plt.ylim(0.1,10)

    ############################################################################
    # Plot the data
    ############################################################################
    ############################################################################
    # Look at the rise-time information.
    ############################################################################

    # For the data (two lognormals)
    #starting_params = [-0.6,0.6,0.2*nevents,  0.1,0.8,0.8*nevents]
    # For the pulser fast rise times (two lognormals)
    starting_params = [-0.6,0.5,0.6*nevents,  0.5,0.8,0.4*nevents]


    fit_parameters = []
    fit_errors = []
    fit_mnerrors = []
    nevs = []
    axrt = []

    elo = 0.0
    ehi = 1.0
    eoffset = 0.5

    ewidth = 0.100
    estep = 0.100

    #ewidth = 0.150
    #estep = 0.150 

    #ewidth = 0.200
    #estep = 0.050

    expts = []

    figcount = 0
    maxrange = int((ranges[0][1]-ranges[0][0])/ewidth)
    #for i in range(48,-1,-1):
    for i in range(0,maxrange):
        #j = 48-i
        j = i
        if j%6==0:
            figrt = plt.figure(figsize=(12,6),dpi=100)
        axrt.append(figrt.add_subplot(2,3, i%6 + 1))

        #figrt = plt.figure(figsize=(6,4),dpi=100)
        #axrt.append(figrt.add_subplot(1,1,1))

        data_to_fit = []
        #h,xpts,ypts,xpts_err,ypts_err = lch.hist_err(data[1],bins=nbins[1],range=ranges[1],axes=ax1)

        if i>=0:
            elo = i*estep + eoffset
            ehi = elo + ewidth
            index0 = data[0]>=elo
            index1 = data[0]< ehi
            print((elo,ehi))
            index = index0*index1
            data_to_fit = data[2][index]

        if len(data_to_fit)>0:
            lch.hist_err(data_to_fit,bins=nbins[2],range=ranges[2],axes=axrt[j])
            plt.ylim(0)
            plt.xlim(ranges[2][0],ranges[2][1])
            name = "%0.2f-%0.2f" % (elo,ehi)
            plt.text(0.75,0.75,name,transform=axrt[j].transAxes)
            print ("=======-------- E BIN ----------===========")
            print (name)

        emid = (elo+ehi)/2.0
        #print "HERE ------------------------------- emid: ",emid

        # The entries for the narrow peak parameters.
        fast_mean0 = expfunc(fast_mean0_k,emid)
        fast_sigma0 = expfunc(fast_sigma0_k,emid)
        fast_num0 = expfunc(fast_num0_k,emid)

        # USE THIS FOR THE GAUSSIAN CONSTRAINT
        fast_mean0_optimal = fast_mean0
        fast_mean0_uncert = 0.30*fast_mean0

        fast_sigma0_optimal = fast_sigma0
        fast_sigma0_uncert = 0.30*fast_sigma0

        # The entries for the relationship between the broad and narrow peak.
        fast_mean_rel = expfunc(fast_mean_rel_k,emid)
        fast_sigma_rel = expfunc(fast_sigma_rel_k,emid)
        fast_logn_num_rel = expfunc(fast_num_rel_k,emid)

        fast_mean1 = fast_mean0 - fast_mean_rel
        fast_sigma1 = fast_sigma0 - fast_sigma_rel
        fast_num1 = fast_num0 / fast_logn_num_rel

        fast_logn_frac0 = fast_num0/(fast_num0+fast_num1)

        nevents = len(data_to_fit)
        print(("Nevents for this fit: ",nevents))
        #starting_params = [-0.1,0.8,0.2*nevents,  0.6,0.52,0.8*nevents]

        print((starting_params[4]))
        #exit()
        
        ############################################################################
        # Declare the fit parameters
        ############################################################################
        params_dict = {}
        params_dict['flag'] = {'fix':True,'start_val':args.fit} 
        params_dict['var_rt'] = {'fix':True,'start_val':0,'limits':(ranges[2][0],ranges[2][1])}

        params_dict['emid'] = {'fix':True,'start_val':emid,'limits':(ranges[0][0],ranges[0][1])}

        params_dict['fast_logn_mean0'] = {'fix':False,'start_val':fast_mean0,'limits':(-2,2),'error':0.01}
        params_dict['fast_logn_sigma0'] = {'fix':False,'start_val':fast_sigma0,'limits':(0.05,30),'error':0.01}
        params_dict['fast_logn_frac0'] = {'fix':True,'start_val':fast_logn_frac0,'limits':(0.0001,1.0),'error':0.01}

        #params_dict['fast_num'] = {'fix':False,'start_val':0.5*nevents,'limits':(0.0,1.5*nevents),'error':0.01}
        params_dict['fast_num'] = {'fix':False,'start_val':starting_params[2],'limits':(0.0,1.5*nevents),'error':0.01}

        params_dict['fast_logn_sigma0_optimal'] = {'fix':True,'start_val':fast_sigma0_optimal}
        params_dict['fast_logn_sigma0_uncert'] = {'fix':True,'start_val':fast_sigma0_uncert}

        params_dict['fast_logn_mean0_optimal'] = {'fix':True,'start_val':fast_mean0_optimal}
        params_dict['fast_logn_mean0_uncert'] = {'fix':True,'start_val':fast_mean0_uncert}

        #params_dict['fast_logn_mean1'] = {'fix':False,'start_val':starting_params[0],'limits':(-2,2),'error':0.01}
        #params_dict['fast_logn_sigma1'] = {'fix':False,'start_val':starting_params[1],'limits':(0.05,30),'error':0.01}
        #params_dict['fast_num1'] = {'fix':False,'start_val':nevents,'limits':(0.0,1.5*nevents),'error':0.01}

        # float them
        params_dict['slow_logn_mean'] = {'fix':False,'start_val':starting_params[3],'limits':(-2,2),'error':0.01}
        params_dict['slow_logn_sigma'] = {'fix':False,'start_val':starting_params[4],'limits':(0.05,30),'error':0.01}
        params_dict['slow_num'] = {'fix':False,'start_val':starting_params[5],'limits':(0.0,1.5*nevents),'error':0.01}

        # Fixed values
        params_dict['fast_mean_rel_k_0'] = {'fix':True,'start_val':fast_mean_rel_k[0],'limits':(-1e6,1e6),'error':0.01}
        params_dict['fast_mean_rel_k_1'] = {'fix':True,'start_val':fast_mean_rel_k[1],'limits':(-1e6,1e6),'error':0.01}
        params_dict['fast_mean_rel_k_2'] = {'fix':True,'start_val':fast_mean_rel_k[2],'limits':(-1e6,1e6),'error':0.01}

        params_dict['fast_sigma_rel_k_0'] = {'fix':True,'start_val':fast_sigma_rel_k[0],'limits':(-1e6,1e6),'error':0.01}
        params_dict['fast_sigma_rel_k_1'] = {'fix':True,'start_val':fast_sigma_rel_k[1],'limits':(-1e6,1e6),'error':0.01}
        params_dict['fast_sigma_rel_k_2'] = {'fix':True,'start_val':fast_sigma_rel_k[2],'limits':(-1e6,1e6),'error':0.01}

        params_dict['fast_num_rel_k_0'] = {'fix':True,'start_val':fast_num_rel_k[0],'limits':(-1e6,1e6),'error':0.01}
        params_dict['fast_num_rel_k_1'] = {'fix':True,'start_val':fast_num_rel_k[1],'limits':(-1e6,1e6),'error':0.01}
        params_dict['fast_num_rel_k_2'] = {'fix':True,'start_val':fast_num_rel_k[2],'limits':(-1e6,1e6),'error':0.01}

        '''
        if i==7:
            params_dict['slow_logn_mean'] = {'fix':True,'start_val':0.57,'limits':(-2,2),'error':0.01}
        elif i==8:
            params_dict['slow_logn_mean'] = {'fix':True,'start_val':0.55,'limits':(-2,2),'error':0.01}
        '''

        # Above some value, lock this down.
        '''
        if elo>=2.2:
            params_dict['slow_logn_mean'] = {'fix':True,'start_val':0.0,'limits':(-2,2),'error':0.01}
            params_dict['slow_logn_sigma'] = {'fix':True,'start_val':1.0,'limits':(0.05,30),'error':0.01}
            params_dict['slow_num'] = {'fix':True,'start_val':1,'limits':(0.0,1.5*nevents),'error':0.01}
        '''

        '''
        if i==0:
            None
            # From Nicole's simulation.
            #params_dict['fast_logn_mean'] = {'fix':True,'start_val':-0.10,'limits':(-2,2),'error':0.01}
            # From Juan
            #params_dict['fast_logn_mean'] = {'fix':True,'start_val':-0.60,'limits':(-2,2),'error':0.01}
            #params_dict['slow_logn_sigma'] = {'fix':True,'start_val':0.50,'limits':(0.05,30),'error':0.01}
        '''

        # Try fixing the slow sigma
        #params_dict['slow_logn_sigma'] = {'fix':True,'start_val':0.60,'limits':(-2,2),'error':0.01}

        #figrt.subplots_adjust(left=0.07, bottom=0.15, right=0.95, wspace=0.2, hspace=None,top=0.85)
        #figrt.subplots_adjust(left=0.05, right=0.98)
        #figrt.subplots_adjust(left=0.15, right=0.98,bottom=0.15)
        figrt.subplots_adjust(left=0.07, right=0.98,bottom=0.10)
        #plt.show()
        #exit()

        ############################################################################
        # Fit
        ############################################################################

        if i>=0 and len(data_to_fit)>0:
            params_names,kwd = fitutils.dict2kwd(params_dict)
        
            #print data_to_fit
            f = fitutils.Minuit_FCN([[data_to_fit]],params_dict,emlf)

            # For maximum likelihood method.
            kwd['errordef'] = 0.5
            kwd['print_level'] = 0
            #print kwd

            m = minuit.Minuit(f,**kwd)

            m.print_param()

            m.migrad()
            #m.hesse()
            m.minos()

            print ("Finished fit!!\n")

            values = m.values # Dictionary
            errors = m.errors # Dictionary
            mnerrors = m.get_merrors()
            print ("MNERRORS: ")
            print (mnerrors)
            fit_parameters.append(values)
            fit_errors.append(errors)
            fit_mnerrors.append(mnerrors)
            nevs.append(len(data_to_fit))

            xpts = np.linspace(ranges[2][0],ranges[2][1],1000)
            tot_ypts = np.zeros(len(xpts))

            # The entries for the relationship between the broad and narrow peak.
            fast_logn_mean_rel = expfunc(fast_mean_rel_k,values['emid'])
            fast_logn_sigma_rel = expfunc(fast_sigma_rel_k,values['emid'])
            fast_logn_num_rel = expfunc(fast_num_rel_k,values['emid'])

            fast_logn_mean1 = values['fast_logn_mean0'] - fast_logn_mean_rel
            fast_logn_sigma1 = values['fast_logn_sigma0'] - fast_logn_sigma_rel

            tot_ypts_fast = np.zeros(len(xpts))

            ypts  = pdfs.lognormal(xpts,values['fast_logn_mean0'],values['fast_logn_sigma0'],ranges[2][0],ranges[2][1])
            y,plot = plot_pdf(xpts,ypts,bin_width=bin_widths[2],scale=values['fast_logn_frac0']*values['fast_num'],fmt='r--',linewidth=2,axes=axrt[j])
            tot_ypts += y
            tot_ypts_fast += y
            # Only plot the second narrow bump above some value.
            if emid<=3.9: 
                ypts  = pdfs.lognormal(xpts,fast_logn_mean1,fast_logn_sigma1,ranges[2][0],ranges[2][1])
                y,plot = plot_pdf(xpts,ypts,bin_width=bin_widths[2],scale=(1.0-values['fast_logn_frac0'])*values['fast_num'],fmt='r--',linewidth=2,axes=axrt[j])
                tot_ypts += y
                tot_ypts_fast += y

            ypts  = pdfs.lognormal(xpts,values['slow_logn_mean'],values['slow_logn_sigma'],ranges[2][0],ranges[2][1])
            y,plot = plot_pdf(xpts,ypts,bin_width=bin_widths[2],scale=values['slow_num'],fmt='b-',linewidth=2,axes=axrt[j])
            tot_ypts += y

            axrt[j].plot(xpts,tot_ypts_fast,'r-',linewidth=2)

            axrt[j].plot(xpts,tot_ypts,'m',linewidth=2)
            axrt[j].set_ylabel(r'Events')
            axrt[j].set_xlabel(r'Rise time ($\mu$s)')
            axrt[j].set_xlim(0,5.0)
            if j%6==5:
                name = "Plots/rt_slice_%s_%d.png" % (tag,figcount)
                plt.savefig(name)
                figcount += 1

            #'''
            if math.isnan(values['fast_logn_mean0']) == False:
                starting_params = [ \
                values['fast_logn_mean0'], \
                values['fast_logn_sigma0'], \
                values['fast_num'], \
                values['slow_logn_mean'], \
                values['slow_logn_sigma'],
                values['slow_num'] \
                ]
            #'''

            expts.append((ehi+elo)/2.0)

    #plt.show()
    #exit()
    print (fit_parameters)
    print (nevs)
    
    ypts = [[],[],[],[],[],[]]
    yerr = [[],[],[],[],[],[]]
    yerrlo = [[],[],[],[],[],[]]
    yerrhi = [[],[],[],[],[],[]]
    npts = []

    if len(expts)>0:
        #for i,fp,fe,n in zip(xrange(len(nevs)),fit_parameters,fit_errors,nevs):
        for i,fp,fe,n in zip(list(range(len(nevs))),fit_parameters,fit_mnerrors,nevs):
            print ("----------")
            #ypts[0].append(fp['fast_logn_mean'])
            #ypts[1].append(fp['fast_logn_sigma'])
            #ypts[2].append(fp['fast_num'])
            #ypts[3].append(fp['slow_logn_mean'])
            #ypts[4].append(fp['slow_logn_sigma'])
            #ypts[5].append(fp['slow_num'])

            pars = ['fast_logn_mean0','fast_logn_sigma0','fast_num',\
                    'slow_logn_mean','slow_logn_sigma','slow_num']

            for i,p in enumerate(pars):
                #print "key ",p
                #if fe.has_key(p):
                if p in fe:
                    ypts[i].append(fp[p])
                    #print "val: ",fp[p]
                    yerrlo[i].append(abs(fe[p]['lower']))
                    yerrhi[i].append(abs(fe[p]['upper']))
                elif p=='slow_logn_sigma':
                    ypts[i].append(starting_params[4])
                    yerrlo[i].append(0.0)
                    yerrhi[i].append(0.0)
                else:
                    ypts[i].append(0.0)
                    yerrlo[i].append(0.0)
                    yerrhi[i].append(0.0)


            npts.append(n)

        for i in range(len(ypts)):
            ypts[i] = np.array(ypts[i])
            yerrlo[i] = np.array(yerrlo[i])
            yerrhi[i] = np.array(yerrhi[i])

        colors = ['r','b']
        labels = ['fast','slow']

        # Use all or some of the points
        nfitpts = len(expts)

        
        #xp = np.linspace(min(expts),max(expts),100)
        print ("herherherkehre")
        print (nfitpts)
        print (expts)
        xp = np.linspace(min(expts),expts[nfitpts-1],100)
        expts = np.array(expts)

        yfitpts = []

        '''
        fvals2 = plt.figure(figsize=(13,6),dpi=100)

        for k in range(0,3):
            # Some of the broad rise times are set to 0.
            #index0s = ypts[3+k]!=0
            index0s = np.ones(len(ypts[3+k])).astype(bool)
            #index0s = np.ones(nfitpts).astype(bool)

            fvals2.add_subplot(2,3,k+1)

            tempypts = ypts[0+k]-ypts[3+k]
            # Fractional error
            tempyerrlo = np.sqrt((yerrlo[0+k])**2 + (yerrlo[3+k])**2)
            tempyerrhi = np.sqrt((yerrhi[0+k])**2 + (yerrhi[3+k])**2)
            if k>1:
                tempypts = ypts[0+k][index0s]/ypts[3+k][index0s]
                tempyerrlo = np.sqrt((yerrlo[0+k][index0s]/ypts[3+k][index0s])**2 + (yerrlo[3+k][index0s]*(ypts[0+k][index0s]/(ypts[3+k][index0s]**2)))**2)
                tempyerrhi = np.sqrt((yerrhi[0+k][index0s]/ypts[3+k][index0s])**2 + (yerrhi[3+k][index0s]*(ypts[0+k][index0s]/(ypts[3+k][index0s]**2)))**2)

            print expts
            print index0s
            print expts[index0s]
            print tempypts[index0s]
            print tempyerrlo[index0s]
            print tempyerrhi[index0s]
            plt.errorbar(expts[index0s],tempypts[index0s],xerr=0.01,yerr=[tempyerrlo[index0s],tempyerrhi[index0s]],\
                    fmt='o',ecolor='k',mec='k',mfc='m',label='Ratio')

            plt.xlim(0.5,3.5)


            ########################################################################
            # Fit to exponentials.
            ########################################################################
            pinit = [1,1,1]
            if k==0:
                pinit = [1.0, 1.0, -1.2]
            elif k==1:
                pinit = [1.0, -1.0, -0.5]
            elif k==2:
                pinit = [-2.0, 1.0, 2.0]
            
            index = np.arange(0,len(tempypts))
            print "WHHHHYYYYYY"
            print expts
            print index
            print expts[index]
            print tempypts[index]
            print tempyerrlo[index]
            print tempyerrhi[index]
            if sum(tempypts[index]) > 0:
                out = leastsq(errfunc, pinit, args=(expts[index], tempypts[index], (tempyerrlo[index]+tempyerrhi[index])/2.0), full_output=1)
                z = out[0]
                zcov = out[1]
                print "Differences and ratios: %d [%f,%f,%f]" % (k,z[0],z[1],z[2])
                #print "zcov: ",zcov
                if zcov is not None:
                    print "Differences and ratios: %d [%f,%f,%f]" % (k,np.sqrt(zcov[0][0]),np.sqrt(zcov[1][1]),np.sqrt(zcov[2][2]))
                yfitpts = expfunc(z,xp)
                #print zcov
                plt.plot(xp,yfitpts,'-',color='m')


        '''

        ########################################################################
        # Try to fit the individual distributions.
        ########################################################################

        # To hold the means and widths for the fast and slow rise-time distributions.
        means = []
        sigmas = []

        yfitpts = []
        for i in range(0,6):
            yfitpts.append(np.zeros(len(xp)))

        fvals = plt.figure(figsize=(13,4),dpi=100)
        for k in range(0,3):
            fvals.add_subplot(1,3,k+1)
            for ik in range(0,2):
                nindex = k+3*ik
                plt.errorbar(expts,ypts[nindex],xerr=0.01,yerr=[yerrlo[nindex],yerrhi[nindex]],\
                        fmt='o',ecolor='k',mec='k',mfc=colors[ik],label=labels[ik])

                #'''
                # Use part of the data
                #index0 = np.arange(0,3)
                #index1 = np.arange(7,len(expts))
                #index = np.append(index0,index1)

                # Use all or some of the points
                index = np.arange(0,len(expts))
                #index = np.arange(0,20)
                if ik>0:
                    index = np.arange(0,len(expts))
                #print index
                index = index[index!=7]
                index = index[index!=8]
                index = index[index!=17]
                index = index[index!=18]
                index = index[index!=19]

                ########################################################################
                # Fit to exponentials.
                ########################################################################
                pinit = [1,1,1]
                if ik==0 and k==0:
                    pinit = [1.0, 1.0, -1.2]
                elif ik==0 and k==1:
                    pinit = [4.0, 2.0, 0.0]
                elif ik==0 and k==2:
                    pinit = [2.0, 2000.0, 300.0]
                elif ik==1 and k==0:
                    pinit = [3.0, 1.5, 0.5]
                elif ik==1 and k==1:
                    pinit = [0.5, -0.1] # For linear ft
                    #pinit = [0.0001, 0.0001, starting_params[4]] # For exponential
                
                #print "before fit: ",ypts[nindex][index],yerrlo[nindex][index],yerrhi[nindex][index]
                print(("Fitting data!!!!!! --------------- %d %d" % (k,ik)))
                #print "before fit: ",ypts[nindex][index]
                if abs(sum(ypts[nindex][index])) > 0 and k<2:
                    print ("FITTING -----------")
                    #print expts[index]
                    #print ypts[nindex][index]
                    if k==1 and ik==1: ########## FIT WITH LINEAR TERM FOR SLOW SIGMA
                        out = leastsq(errfunc1, pinit, args=(expts[index], ypts[nindex][index], (yerrlo[nindex][index]+yerrhi[nindex][index])/2.0), full_output=1)
                        z = out[0]
                        zcov = out[1]
                        print(("Data points: %d %d [%f,%f]" % (k,ik,z[0],z[1])))
                        sigmas.append([z[0],z[1]])
                        '''
                        if zcov is not None:
                            print "Data points: %d %d [%f,%f]" % (k,ik,np.sqrt(zcov[0][0]),np.sqrt(zcov[1][1]))
                        '''
                        yfitpts[nindex] = expfunc1(z,xp)
                        #print zcov
                        #print plt.gca()
                        plt.plot(xp,yfitpts[nindex],'-',color=colors[ik])
                    else:
                        out = leastsq(errfunc, pinit, args=(expts[index], ypts[nindex][index], (yerrlo[nindex][index]+yerrhi[nindex][index])/2.0), full_output=1)
                        z = out[0]
                        zcov = out[1]
                        print(("Data points: %d %d [%f,%f,%f]" % (k,ik,z[0],z[1],z[2])))
                        if k==1 and ik==0:
                            sigmas.append([z[0],z[1],z[2]])
                        elif k==0:
                            means.append([z[0],z[1],z[2]])

                        '''
                        if zcov is not None:
                            print "Data points: %d %d [%f,%f,%f]" % (k,ik,np.sqrt(zcov[0][0]),np.sqrt(zcov[1][1]),np.sqrt(zcov[2][2]))
                        '''
                        yfitpts[nindex] = expfunc(z,xp)
                        #print zcov
                        #print plt.gca()
                        plt.plot(xp,yfitpts[nindex],'-',color=colors[ik])

            if k==0:
                plt.ylim(-1.5,1.5)
            elif k==1:
                plt.ylim(0.0,1.5)
            plt.xlabel('Energy (keVee)')
            if k==0:
                plt.ylabel(r'Lognormal $\mu$')
            elif k==1:
                plt.ylabel(r'Lognormal $\sigma$')
            elif k==2:
                plt.ylabel(r'Number of events')
            plt.legend()

        #fval
        '''
        fvals.add_subplot(2,3,4)
        plt.plot(xp,yfitpts[3]-yfitpts[0],'-',color='m')

        fvals.add_subplot(2,3,5)
        plt.plot(xp,yfitpts[4]-yfitpts[1],'-',color='m')

        fvals.add_subplot(2,3,6)
        plt.plot(xp,yfitpts[5]/yfitpts[2],'-',color='m')
        '''

        fvals.subplots_adjust(left=0.10, right=0.98,bottom=0.15,wspace=0.25,hspace=0.25)
        name = 'Plots/rt_summary_%s_0.png' % (tag)
        plt.savefig(name)

        np.savetxt('rt_parameters.txt',[expts,ypts[0],ypts[1],ypts[2],ypts[3],ypts[4],ypts[5],npts])

        #'''

    #print "Sum ypts[5]: ",sum(ypts[5])
    print (means)
    print (sigmas)
    print((fast_mean_rel_k,fast_sigma_rel_k,fast_num_rel_k))

    outfilename = "risetime_parameters_from_data_%s.py" % (tag)
    outfile = open(outfilename,'w')
    outfile.write("def risetime_parameters():\n\n")

    output = "\n\t%s = [%f,%f,%f]\n" % ("fast_mean_rel_k",fast_mean_rel_k[0],fast_mean_rel_k[1],fast_mean_rel_k[2])
    outfile.write(output)
    output = "\t%s = [%f,%f,%f]\n" % ("fast_sigma_rel_k",fast_sigma_rel_k[0],fast_sigma_rel_k[1],fast_sigma_rel_k[2])
    outfile.write(output)
    output = "\t%s = [%f,%f,%f]\n" % ("fast_num_rel_k",fast_num_rel_k[0],fast_num_rel_k[1],fast_num_rel_k[2])
    outfile.write(output)
    output = "\n\t%s = [%f,%f,%f]\n" % ("mu0",means[0][0],means[0][1],means[0][2])
    outfile.write(output)
    output = "\t%s = [%f,%f,%f]\n" % ("sigma0",sigmas[0][0],sigmas[0][1],sigmas[0][2])
    outfile.write(output)
    output = "\n\t%s = [%f,%f,%f]\n" % ("mu1",means[1][0],means[1][1],means[1][2])
    outfile.write(output)
    output = "\t%s = [%f,%f] # This has only two parameters.\n" % ("sigma1",sigmas[1][0],sigmas[1][1])
    outfile.write(output)
    output = "\n\treturn fast_mean_rel_k,fast_sigma_rel_k,fast_num_rel_k,mu0,sigma0,mu1,sigma1\n"
    outfile.write(output)

        




    if not args.batch:
        plt.show()