示例#1
0
def gen_statistical_uncertainties(wdir, dirname, iset, iF2, iFL, iF3, data,
                                  lum):
    """
    ATTENTION: sigtot is in 1/GeV^2
    lum must be in GeV^2
    """
    import lhapdf
    stf = lhapdf.mkPDF(dirname, iset)

    global par
    par = AUX()

    for bin in data:
        N = bin['sigtot'] * lum
        ps = (bin['xmax'] - bin['xmin']) * (bin['Q2max'] - bin['Q2min'])
        bin['<value>'] = N / ps / lum
        bin['stat_u'] = np.sqrt(N) / ps / lum

        bin['xmid'] = 0.5 * (bin['xmax'] + bin['xmin'])
        bin['Q2mid'] = 0.5 * (bin['Q2max'] + bin['Q2min'])
        x = bin['<x>']
        Q2 = bin['<Q2>']
        F2 = stf.xfxQ2(iF2, x, Q2)
        FL = stf.xfxQ2(iFL, x, Q2)
        F3 = 0
        rs = bin['rs']
        sign = bin['sign']
        bin['obs'] = 'dsig/dxdQ2'
        bin['value'] = get_sigma_dxdQ2(rs, x, Q2, F2, FL, F3, sign)
        bin['x'] = x
        bin['Q2'] = Q2
        bin['y'] = Q2 / x / (rs**2 - par.M2)

    return data
示例#2
0
def get_red_sigtot(wdir, dirname, iset, iF2, iFL, iF3, data, current):
    import lhapdf

    global par
    par = AUX()
    stf = lhapdf.mkPDF(dirname, iset)

    for bin in data:
        xmin = bin['xmin']
        xmax = bin['xmax']
        q2min = bin['Q2min']
        q2max = bin['Q2max']
        sign = bin['sign']
        rs = bin['rs']
        bin['sigtot'] = red_integrate(stf, sign, rs, xmin, xmax, q2min, q2max,
                                      iF2, iFL, iF3, 0, current)
        bin['<x>'] = integrate(stf, sign, rs, xmin, xmax, q2min, q2max, iF2,
                               iFL, iF3, 1) / bin['sigtot']
        bin['<Q2>'] = integrate(stf, sign, rs, xmin, xmax, q2min, q2max, iF2,
                                iFL, iF3, 2) / bin['sigtot']

    return data
示例#3
0
        F = conf['aux'].p2n(F)
        return _get_FX(x, z, Q2, pT, target, hadron, F, D)

    elif target == 'd':

        return 0.5 * (get_FX(x, z, Q2, pT, 'p', hadron) +
                      get_FX(x, z, Q2, pT, 'n', hadron))


if __name__ == '__main__':

    from qcdlib.ff1 import FF as FF1
    from qcdlib.pdf1 import PDF as PDF1

    conf['aux'] = AUX()
    conf['Htildepi'] = FF1('pi')
    conf['Htildek'] = FF1('k')
    conf['transversity'] = PDF1()

    x = 0.25
    z = 0.5
    Q2 = 2.4
    pT = 0.3

    print get_FX(x, z, Q2, pT, 'p', 'pi+')
    print get_FX(x, z, Q2, pT, 'p', 'pi-')
    print get_FX(x, z, Q2, pT, 'p', 'pi0')

    print get_FX(x, z, Q2, pT, 'n', 'pi+')
    print get_FX(x, z, Q2, pT, 'n', 'pi-')
示例#4
0
def all_errors(wdir, tar, est, value, lum):

    conf['aux'] = AUX()
    data = pd.read_excel('%s/sim/all-%s-%s.xlsx' % (wdir, tar, est),
                         index=False)
    data = data.to_dict(orient='list')
    target = data['target'][0]
    l = len(data['value'])
    X = np.array(data['X'])
    Q2 = np.array(data['Q2'])
    Xup = np.array(data['Xup'])
    Xdo = np.array(data['Xdo'])
    Q2up = np.array(data['Q2up'])
    Q2do = np.array(data['Q2do'])
    dx = Xup - Xdo
    dQ2 = Q2up - Q2do
    bins = dx * dQ2

    pole = data['pole'][0]
    polh = data['polh'][0]

    RS = data['RS'][0]
    S = RS**2

    M2 = conf['aux'].M2

    if tar == 'd': M2 = 4 * M2

    #--luminosity
    lum = convert_lum(lum)

    GF = conf['aux'].GF

    #--get structure functions
    resman = RESMAN(parallel=False, datasets=False)
    parman = resman.parman
    resman.setup_idis()
    resman.setup_pidis()
    pidis = resman.pidis_thy
    idis = resman.idis_thy
    pidis.data[tar]['g1'] = np.zeros(pidis.X.size)
    pidis.data[tar]['g2'] = np.zeros(pidis.X.size)
    idis.data[tar]['F2'] = np.zeros(idis.X.size)
    idis.data[tar]['FL'] = np.zeros(idis.X.size)
    idis = resman.idis_thy
    pidis = resman.pidis_thy

    idis._update()
    pidis._update()

    g1 = lambda x, q2: pidis.get_stf(x, q2, stf='g1', tar=tar)
    g2 = lambda x, q2: pidis.get_stf(x, q2, stf='g2', tar=tar)
    F2 = lambda x, q2: idis.get_stf(x, q2, stf='F2', tar=tar)
    FL = lambda x, q2: idis.get_stf(x, q2, stf='FL', tar=tar)

    rho2 = lambda x, q2: 1 + 4 * x**2 * M2 / q2
    gamma2 = lambda x, q2: 4 * x**2 * M2 / q2
    F1 = lambda x, q2: (gamma2(x, q2) * F2(x, q2) - FL(x, q2)) / (2 * x)

    y = lambda x, q2: (q2 / 2 / x) / ((S - M2) / 2)

    alpha = lambda q2: conf['eweak'].get_alpha(q2)

    C = lambda x, q2: 4 * np.pi * alpha(q2)**2 * S / q2

    T1 = lambda x, q2: x * F1(x, q2)
    T2 = lambda x, q2: (1 - y(x, q2) - y(x, q2)**2 / 4 * gamma2(x, q2)) * F2(
        x, q2) / y(x, q2)

    T3 = lambda x, q2: x * (2 - y(x, q2) - y(x, q2)**2 / 2 * gamma2(x, q2)
                            ) * g1(x, q2)
    T4 = lambda x, q2: x * y(x, q2) * gamma2(x, q2) * g2(x, q2)

    _ddsigR = lambda x, q2: C(x, q2) * (T1(x, q2) + T2(x, q2) - T3(x, q2) + T4(
        x, q2))
    _ddsigL = lambda x, q2: C(x, q2) * (T1(x, q2) + T2(x, q2) + T3(x, q2) - T4(
        x, q2))

    #--integrate over bin
    z1, w1 = np.polynomial.legendre.leggauss(3)
    z2, w2 = np.polynomial.legendre.leggauss(3)

    ddsigR = np.zeros((len(X), len(z1), len(z2)))
    ddsigL = np.zeros((len(X), len(z1), len(z2)))
    for i in range(len(X)):
        _x = 0.5 * ((Xup[i] - Xdo[i]) * z1 + Xup[i] + Xdo[i])
        _q2 = 0.5 * ((Q2up[i] - Q2do[i]) * z2 + Q2up[i] + Q2do[i])
        xjac = 0.5 * (Xup[i] - Xdo[i])
        q2jac = 0.5 * (Q2up[i] - Q2do[i])
        for j in range(len(_x)):
            for k in range(len(_q2)):
                ddsigR[i][j][k] = _ddsigR(_x[j], _q2[k]) * xjac * q2jac
                ddsigL[i][j][k] = _ddsigL(_x[j], _q2[k]) * xjac * q2jac

    #--integrate over Q2
    dsigR = np.sum(w2 * ddsigR, axis=2)
    dsigL = np.sum(w2 * ddsigL, axis=2)

    #--integrate over X
    sigR = np.sum(w1 * dsigR, axis=1)
    sigL = np.sum(w1 * dsigL, axis=1)

    NR = lum * sigR
    NL = lum * sigL

    #--theoretical asymmetry
    A = np.array(value)

    #--absolute uncertainty (not divided by sigma)
    stat2 = (1 + A**2) / (NR + NL)

    stat = np.sqrt(stat2)

    #--statistical uncertainties
    data['stat_u'] = stat

    Y = np.array(data['Q2']) / 2 / np.array(data['X']) / ((S - M2) / 2)

    #--add systemic uncertainties beyond polarization
    #--optimistic: 1% on lum, 1% on Pe, 2% on Ph, no further errors
    if est == 'opt':
        data['lum_u'] = A * 0.01
        data['pole_u'] = A * 0.01
        data['polh_u'] = A * 0.02
        data['syst_u'] = A * 0.0

    #--moderate: do no have yet
    elif est == 'mod':
        return
        #data['syst_u'] = np.zeros(l)
        #for i in range(l):
        #    if Y[i] <= 0.01: data['syst_u'][i] = Am[i]*0.020
        #    if Y[i] >  0.01: data['syst_u'][i] = Am[i]*0.025
    #--pessimistic: 2% on lum, 2% on Pe, 4% on Ph, no further errors
    elif est == 'pes':
        data['lum_u'] = A * 0.02
        data['pole_u'] = A * 0.02
        data['polh_u'] = A * 0.04
        data['syst_u'] = A * 0.0

    else:
        print('est must be opt, mod, or pes')
        return

    return data['stat_u'], data['pole_u'], data['polh_u'], data['lum_u'], data[
        'syst_u']
示例#5
0
def chi2_histogram(wdir, reaction, dataset, dpi):
    conf['datasets'] = {}
    if reaction.lower() == 'jet':
        from obslib.jets.reader import READER
        conf['datasets']['jet'] = {}
        conf['datasets']['jet']['xlsx'] = {}
        conf['datasets']['jet']['xlsx'][
            dataset] = 'jets/expdata/%s.xlsx' % dataset
        ## please notice the inconsistence of names, 'jet' and 'jets'
        ## so please do not remove this part
    elif reaction.lower() == 'pjet':
        from obslib.pjets.reader import READER
        conf['datasets']['pjet'] = {}
        conf['datasets']['pjet']['xlsx'] = {}
        conf['datasets']['pjet']['xlsx'][
            dataset] = 'pjets/expdata/%s.xlsx' % dataset
        ## please notice the inconsistence of names, 'pjet' and 'pjets'
        ## so please do not remove this part
    elif reaction.lower() == 'idis':
        from obslib.idis.reader import READER
    elif reaction.lower() == 'sidis':
        from obslib.sidis.reader import READER
    elif reaction.lower() == 'pidis':
        from obslib.pidis.reader import READER
    elif reaction.lower() == 'psidis':
        from obslib.psidis.reader import READER
    elif reaction.lower() == 'dy':
        from obslib.dy.reader import READER
    elif reaction.lower() == 'sia':
        from obslib.sia.reader import READER

    conf['aux'] = AUX()
    if reaction not in conf['datasets']:
        conf['datasets'][reaction] = {}
        conf['datasets'][reaction]['xlsx'] = {}
        conf['datasets'][reaction]['xlsx'][dataset] = '%s/expdata/%s.xlsx' % (
            reaction, dataset)
    data_table = READER().load_data_sets(reaction)

    ## plot histogram of relative 'chi2' for 'dataset' in 'reaction'
    load_config('%s/input.py' % wdir)
    istep = core.get_istep()
    collaboration = data_table[dataset]['col'][0]
    print('\nplotting relative chi2 histogram for %d in %s from %s' %
          (dataset, reaction, wdir))
    replicas = core.get_replicas(wdir)
    relative_chi2 = []
    for replica in replicas:
        # print replica['chi2'][reaction][dataset]
        relative_chi2.append(replica['chi2'][reaction][dataset]['chi2'] /
                             float(replica['chi2'][reaction][dataset]['npts']))

    n_rows, n_columns = 1, 1
    figure, ax = py.subplots(n_rows, n_columns)

    ax.hist(relative_chi2,
            len(relative_chi2),
            histtype='bar',
            color='b',
            label=r'$\mathrm{%s,~%s}$' %
            (reaction.upper(), collaboration.upper()))
    ax.legend(fontsize=20, loc='upper right')
    # ax.set_ylabel(r'\boldmath$\mathrm{n}$', size = 24)
    # ax.yaxis.set_label_coords(-0.17, 2.0)

    ax.set_xlabel(r'\boldmath$\chi^2/\mathrm{n}$', size=24)
    # ax.xaxis.set_label_coords(0.90, -0.12)

    py.tight_layout()
    py.savefig('%s/gallery/chi2-histogram-%s-%s-%d.png' %
               (wdir, reaction, collaboration, istep),
               dpi=dpi)
示例#6
0
def pvdis_errors(wdir, idxs, tar, lum='100:fb-1'):

    for idx in idxs:
        conf['aux'] = AUX()
        conf['eweak'] = EWEAK()
        data = pd.read_excel('%s/sim/%s%s.xlsx' % (wdir, idx, tar),
                             index=False)
        data = data.to_dict(orient='list')
        target = data['target'][0]
        l = len(data['value'])
        X = np.array(data['X'])
        Q2 = np.array(data['Q2'])
        Xup = np.array(data['Xup'])
        Xdo = np.array(data['Xdo'])
        Q2up = np.array(data['Q2up'])
        Q2do = np.array(data['Q2do'])

        RS = data['RS'][0]
        S = RS**2

        M2 = conf['aux'].M2

        if target == 'd': M2 = 4 * M2

        #--luminosity
        lum = convert_lum(lum)

        dx = Xup - Xdo
        dQ2 = Q2up - Q2do

        bins = dx * dQ2

        GF = conf['aux'].GF

        if target == 'p': tabname = 'JAM4EIC_p'
        if target == 'd': tabname = 'JAM4EIC_d'

        stf = lhapdf.mkPDF(tabname, 0)

        #--integrate over bin
        #--get structure functions
        #F2g  = lambda x,q2: stf.xfxQ2(900,x,q2)
        #FLg  = lambda x,q2: stf.xfxQ2(901,x,q2)
        #F2gZ = lambda x,q2: stf.xfxQ2(902,x,q2)
        #FLgZ = lambda x,q2: stf.xfxQ2(903,x,q2)
        #F3gZ = lambda x,q2: stf.xfxQ2(904,x,q2)

        #rho2 = lambda x,q2: 1 + 4*x**2*M2/q2

        #y=lambda x,q2: (q2/2/x)/((S-M2)/2)

        #YP = lambda x,q2: y(x,q2)**2*(rho2(x,q2)+1)/2 - 2*y(x,q2) +2
        #YM = lambda x,q2: 1-(1-y(x,q2))**2

        #sin2w = lambda q2: conf['eweak'].get_sin2w(q2)
        #alpha = lambda q2: conf['eweak'].get_alpha(q2)

        #gA = -0.5
        #gV = lambda q2: -0.5 + 2*sin2w(q2)

        #C  = lambda q2: GF*q2/(2*np.sqrt(2)*np.pi*alpha(q2))

        #C1 = lambda x,q2: 2*np.pi*alpha(q2)**2/(x*y(x,q2)*q2)

        #T1g  = lambda x,q2: YP(x,q2)*F2g(x,q2)  - y(x,q2)**2*FLg(x,q2)
        #T1gZ = lambda x,q2: YP(x,q2)*F2gZ(x,q2) - y(x,q2)**2*FLgZ(x,q2)

        #T2 = lambda x,q2: x*YM(x,q2)*F3gZ(x,q2)

        #_ddsigR = lambda x,q2: C1(x,q2)*(T1g(x,q2) + C(q2)*(gV(q2)-gA)*(T1gZ(x,q2) - T2(x,q2)))
        #_ddsigL = lambda x,q2: C1(x,q2)*(T1g(x,q2) + C(q2)*(gV(q2)+gA)*(T1gZ(x,q2) + T2(x,q2)))

        #z1,w1 = np.polynomial.legendre.leggauss(5)
        #z2,w2 = np.polynomial.legendre.leggauss(5)

        #ddsigR = np.zeros((len(X),len(z1),len(z2)))
        #ddsigL = np.zeros((len(X),len(z1),len(z2)))
        #for i in range(len(X)):
        #    _x   = 0.5*((Xup[i] -Xdo[i])*z1  + Xup[i]  + Xdo[i])
        #    _q2  = 0.5*((Q2up[i]-Q2do[i])*z2 + Q2up[i] + Q2do[i])
        #    xjac  = 0.5*(Xup[i]-Xdo[i])
        #    q2jac = 0.5*(Q2up[i]-Q2do[i])
        #    for j in range(len(_x)):
        #        for k in range(len(_q2)):
        #            ddsigR[i][j][k] = _ddsigR(_x[j],_q2[k])*xjac*q2jac
        #            ddsigL[i][j][k] = _ddsigL(_x[j],_q2[k])*xjac*q2jac

        ##--integrate over Q2
        #dsigR = np.sum(w2*ddsigR,axis=2)
        #dsigL = np.sum(w2*ddsigL,axis=2)

        ##--integrate over X
        #sigR = np.sum(w1*dsigR,axis=1)
        #sigL = np.sum(w1*dsigL,axis=1)

        #--multiply by bin
        #--get structure functions
        F2g = np.array([stf.xfxQ2(900, X[i], Q2[i]) for i in range(l)])
        FLg = np.array([stf.xfxQ2(901, X[i], Q2[i]) for i in range(l)])
        F2gZ = np.array([stf.xfxQ2(902, X[i], Q2[i]) for i in range(l)])
        FLgZ = np.array([stf.xfxQ2(903, X[i], Q2[i]) for i in range(l)])
        F3gZ = np.array([stf.xfxQ2(904, X[i], Q2[i]) for i in range(l)])

        rho2 = 1 + 4 * X**2 * M2 / Q2

        y = (Q2 / 2 / X) / ((S - M2) / 2)

        YP = y**2 * (rho2 + 1) / 2 - 2 * y + 2
        YM = 1 - (1 - y)**2

        sin2w = np.array([conf['eweak'].get_sin2w(q2) for q2 in Q2])
        alpha = np.array([conf['eweak'].get_alpha(q2) for q2 in Q2])

        gA = -0.5
        gV = -0.5 + 2 * sin2w

        C = GF * Q2 / (2 * np.sqrt(2) * np.pi * alpha)

        C1 = 2 * np.pi * alpha**2 / (X * y * Q2) * bins

        T1g = YP * F2g - y**2 * FLg
        T1gZ = YP * F2gZ - y**2 * FLgZ

        T2 = X * YM * F3gZ

        sigR = C1 * (T1g + C * (gV - gA) * (T1gZ - T2))
        sigL = C1 * (T1g + C * (gV + gA) * (T1gZ + T2))

        NR = lum * sigR
        NL = lum * sigL

        C = 4 / (NR + NL)**4

        T1 = NL**2 * NR
        T2 = NR**2 * NL

        stat2 = C * (T1 + T2)

        stat = np.sqrt(stat2)

        #--add syst errors and save new excel sheet
        data['stat_u'] = stat

        print stat
        df = pd.DataFrame(data)
        df.to_excel('%s/sim/%s%s.xlsx' % (wdir, idx, tar), index=False)