Esempio n. 1
0
 def __init__(self, react='sidis'):
     if react == 'sidis':
         self.reaction = 'sidis'
         self.tabs = conf['sidis tabs']
     elif react == 'sidisEIC':
         self.reaction = 'sidisEIC'
         self.tabs = conf['sidisEIC tabs']
     elif react == 'sidisSoLID':
         self.reaction = 'sidisSoLID'
         self.tabs = conf['sidisSoLID tabs']
     self.dis_stfuncs = DIS_STFUNCS()
     self.setup()
Esempio n. 2
0
 def __init__(self):
     self.reaction = 'sidis'
     self.tabs = conf['sidis tabs']
     self.dis_stfuncs = DIS_STFUNCS()
     self.setup()
Esempio n. 3
0
class RESIDUALS(_RESIDUALS):
    def __init__(self):
        self.reaction = 'sidis'
        self.tabs = conf['sidis tabs']
        self.dis_stfuncs = DIS_STFUNCS()
        self.setup()

    def _get_theory(self, entry):
        k, i = entry
        x = self.tabs[k]['x'][i]
        y = self.tabs[k]['y'][i]
        z = self.tabs[k]['z'][i]
        Q2 = self.tabs[k]['Q2'][i]
        pT = self.tabs[k]['pT'][i]
        exp = self.tabs[k]['value'][i]
        tar = self.tabs[k]['target'][i]
        had = self.tabs[k]['hadron'][i]
        obs = self.tabs[k]['obs'][i].strip()
        col = self.tabs[k]['col'][i].strip().upper()

        if tar == 'proton': tar = 'p'
        elif tar == 'neutron': tar = 'n'
        elif tar == 'deuteron': tar = 'd'

        if obs == 'FUU':

            thy = upol.get_FUU(x, z, Q2, pT, tar, had)

        elif obs == 'M':

            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            F2 = self.dis_stfuncs.get_F2(x, Q2, tar)
            thy = FUU / F2
            if col == 'HERMES': thy *= 2 * np.pi * pT

        elif obs == 'AUTcollins':

            # convention factor
            coeff = 1.
            if col == 'HERMES': coeff = 1  # hermes is sin(phi_s+phi_h)
            elif col == 'COMPASS': coeff = -1  # compass is sin(phi_s+phi_h+pi)

            # add depolarization factor
            if col == 'HERMES': coeff *= 2 * (1 - y) / (1 + (1 - y)**2)

            FUT = collins.get_FUT(x, z, Q2, pT, tar, had)
            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            thy = coeff * FUT / FUU

        elif obs == 'AUTsivers':

            # convention factor
            coeff = 1.

            FUT = sivers.get_FUT(x, z, Q2, pT, tar, had)
            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            thy = coeff * FUT / FUU

        elif obs == 'AUUcos2':

            epsilon = (1 - y) / (1 - y + 0.5 * y**2)
            coeff = 1.
            if col == 'HERMES':
                coeff = epsilon  # add depolarization factor for HERMES
            if col == 'COMPASS':
                coeff = 1.
            if col == 'JLAB':
                coeff = epsilon  # add depolarization factor for CLAS

            FUUcos2 = boermulders.get_FUU(x, z, Q2, pT, tar, had)
            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            thy = coeff * FUUcos2 / FUU

        elif obs == 'AUTsinphiS':  # This is for collinear!

            if tar == 'p':
                pT = None
                FUTsinphiS = AUTsinphiS.get_FX(x, z, Q2, pT, 'p', had)
                FUU = upol.get_FUU(x, z, Q2, pT, 'p', had)

            coeff = 1.
            if col == 'HERMES':
                # add depolarization factor for HERMES
                coeff = np.sqrt(1.0 - y) * (2 - y) / (1 - y + 0.5 * y**2)
            if col == 'COMPASS':
                coeff = 1.

            thy = coeff * FUTsinphiS / FUU

        else:
            print 'ERR: exp=%d obs=%s and target=%s not implemented' % (k, obs,
                                                                        tar)
            sys.exit()

        return thy

    def gen_report(self, verb=1, level=1):
        """
        verb = 0: Do not print on screen. Only return list of strings
        verv = 1: print on screen the report
        level= 0: only the total chi2s
        level= 1: include point by point
        """

        L = []

        L.append('reaction: %s' % self.reaction)

        L.append('%7s %10s %10s %10s %10s %5s %10s %10s %10s %10s' %
                 ('idx', 'tar', 'had', 'col', 'obs', 'npts', 'chi2',
                  'chi2/npts', 'rchi2', 'nchi2'))
        for k in self.tabs:
            #print k,len(self.tabs[k]['value'])
            if self.tabs[k]['value'].size == 0:
                continue
            res = self._get_residuals(k)
            rres = self._get_rres(k)
            nres = self._get_nres(k)

            chi2 = np.sum(res**2)
            rchi2 = np.sum(rres**2)
            nchi2 = nres**2
            tar = self.tabs[k]['target'][0]
            col = self.tabs[k]['col'][0].split()[0]
            obs = self.tabs[k]['obs'][0].split()[0]
            had = self.tabs[k]['hadron'][0].split()[0]
            npts = res.size
            if npts > 0:
                L.append(
                    '%7d %10s %10s %10s %10s %5d %10.2f %10.2f  %10.2f %10.2f'
                    % (k, tar, had, col, obs, npts, chi2, chi2 / npts, rchi2,
                       nchi2))
            elif npts == 0:
                L.append(
                    '%7d %10s %10s %10s %10s %5d %10.2f %10.2f %10.2f %10.2f' %
                    (k, tar, had, col, obs, npts, chi2, 0.0, rchi2, nchi2))

        if level == 1:
            L.append('-' * 100)

            msg = 'idx=%7d,  '
            msg += 'col=%7s,  '
            msg += 'tar=%7s,  '
            msg += 'had=%7s,  '
            msg += 'obs=%7s,  '
            if 'dependence' in self.tabs[k]:
                msg += 'dep=%7s,  '
            if 'Dependence' in self.tabs[k]:
                msg += 'dep=%7s,  '
            msg += 'x=%10.3e,  '
            msg += 'z=%10.3e,  '
            msg += 'pT=%10.3e,  '
            msg += 'Q2=%10.3e,  '
            msg += 'yh=%10.3e,  '
            msg += 'yp=%10.3e,  '
            msg += 'dy=%10.3e,  '
            msg += 'exp=%10.3e,  '
            msg += 'alpha=%10.3e,  '
            msg += 'thy=%10.3e,  '
            if 'dthy' in self.tabs[k]:
                msg += 'dthy=%10.3e,  '
            msg += 'shift=%10.3e,  '
            msg += 'chi2=%10.3f  '

            for k in self.tabs:
                if len(self.tabs[k]['value']) == 0:
                    continue
                for i in range(len(self.tabs[k]['value'])):
                    row = [k]
                    row.append(self.tabs[k]['col'][i])
                    row.append(self.tabs[k]['target'][i])
                    row.append(self.tabs[k]['hadron'][i])
                    row.append(self.tabs[k]['obs'][i])
                    if 'dependence' in self.tabs[k]:
                        row.append(self.tabs[k]['dependence'][i].strip())
                    if 'Dependence' in self.tabs[k]:
                        row.append(self.tabs[k]['Dependence'][i].strip())
                    row.append(self.tabs[k]['x'][i])
                    row.append(self.tabs[k]['z'][i])
                    row.append(self.tabs[k]['pT'][i])
                    row.append(self.tabs[k]['Q2'][i])
                    row.append(self.tabs[k]['yh'][i])
                    row.append(self.tabs[k]['yp'][i])
                    row.append(self.tabs[k]['dy'][i])
                    row.append(self.tabs[k]['value'][i])
                    row.append(self.tabs[k]['alpha'][i])
                    row.append(self.tabs[k]['thy'][i])
                    if 'dthy' in self.tabs[k]:
                        row.append(self.tabs[k]['dthy'][i])
                    row.append(self.tabs[k]['shift'][i])
                    # row.append(self.tabs[k]['residuals'][i])
                    # row.append(self.tabs[k]['r-residuals'][i])
                    res = self.tabs[k]['residuals'][i]
                    if res < 0:
                        chi2 = -res**2
                    else:
                        chi2 = res**2
                    row.append(chi2)
                    row = tuple(row)
                    L.append(msg % row)

        if verb == 0:
            return L
        elif verb == 1:
            for l in L:
                print l
            return L
Esempio n. 4
0
class RESIDUALS(_RESIDUALS):
    def __init__(self):
        self.reaction = 'sidis'
        self.tabs = conf['sidis tabs']
        self.dis_stfuncs = DIS_STFUNCS()
        self.setup()

    def _get_theory(self, entry):
        k, i = entry
        x = self.tabs[k]['x'][i]
        y = self.tabs[k]['y'][i]
        z = self.tabs[k]['z'][i]
        Q2 = self.tabs[k]['Q2'][i]
        pT = self.tabs[k]['pT'][i]
        exp = self.tabs[k]['value'][i]
        tar = self.tabs[k]['target'][i]
        had = self.tabs[k]['hadron'][i]
        obs = self.tabs[k]['obs'][i].strip()
        col = self.tabs[k]['col'][i].strip().upper()

        if tar == 'proton': tar = 'p'
        elif tar == 'neutron': tar = 'n'
        elif tar == 'deuteron': tar = 'd'

        if obs == 'FUU':

            thy = upol.get_FUU(x, z, Q2, pT, tar, had)

        elif obs == 'M':

            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            F2 = self.dis_stfuncs.get_F2(x, Q2, tar)
            thy = FUU / F2
            if col == 'HERMES': thy *= 2 * np.pi * pT

        elif obs == 'AUTcollins':

            # convention factor
            coeff = 1.
            if col == 'HERMES': coeff = 1  # hermes is sin(phi_s+phi_h)
            elif col == 'COMPASS': coeff = -1  # compass is sin(phi_s+phi_h+pi)

            # add depolarization factor
            if col == 'HERMES': coeff *= 2 * (1 - y) / (1 + (1 - y)**2)

            FUT = collins.get_FUT(x, z, Q2, pT, tar, had)
            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            thy = coeff * FUT / FUU

        elif obs == 'AUTsivers':

            # convention factor
            coeff = 1.

            FUT = sivers.get_FUT(x, z, Q2, pT, tar, had)
            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            thy = coeff * FUT / FUU

        elif obs == 'AUUcos2':
            ''' The data from JLab and HERMES do not require any integrations,
                but require a y-based coefficient. The COMPASS data requires an
                integration over y, and does not require the coefficient.    '''
            def yield_thy(accelerator, should_integrate):

                # depending on input parameters, either selects the coefficient
                # method or the integration method of producing a residual value.
                # root_s and w2_min also fluctuate based on accelerator.

                # set known values based on the source of AUUcos2 data
                if accelerator == 'COMPASS':
                    ROOT_S = 17.3
                    W2_MIN = 25
                    RANGE_MIN = 0.2
                    RANGE_MAX = 0.9

                #TODO: define accelerator constants for JLab and HERMES data
                elif accelerator == 'JLAB':
                    ROOT_S = 7.25
                    W2_MIN = 4
                    RANGE_MIN = 0.2
                    RANGE_MAX = 0.85

                elif accelerator == 'HERMES':
                    ROOT_S = 3.42
                    W2_MIN = 10
                    RANGE_MIN = 0.2
                    RANGE_MAX = 0.85

                if should_integrate:

                    def fast_integrate(f, low, hi):
                        f = np.vectorize(f)
                        return integrate.fixed_quad(f, low, hi, n=10)

                    M2 = conf['aux'].M**2
                    Q2_MIN = 1
                    Q2_MAX = 1000

                    yA = max(  # lower bound of integration
                        RANGE_MIN, Q2_MAX / (x * ((ROOT_S**2) - M2)),
                        (W2_MIN - M2) / ((1 - x) * ((ROOT_S**2) - M2)))
                    yB = min(  # upper bound of integration
                        Q2_MAX / (x * ((ROOT_S**2) - M2)), RANGE_MAX)

                    # any value dependent on y must be a function so that its value
                    # may be recalculated during the integration process
                    Q_compass = lambda y: np.sqrt(((ROOT_S**2) - M2) * x * y)
                    FUU = lambda y: upol.get_FUU(x, z,
                                                 Q_compass(y)**2, pT, tar, had)
                    FUUcos2 = lambda y: (boermulders.get_FUU(
                        x, z,
                        Q_compass(y)**2, pT, tar, had) + cahn.get_cahn(
                            x, z,
                            Q_compass(y)**2, pT, tar, had))

                    # integrate over y for the numerator and denominator of AUUcos2
                    FUUcos2_integral = fast_integrate(
                        lambda y: (1 / (Q_compass(y)**4)) * FUUcos2(y), yA,
                        yB)[0]
                    FUU_integral = fast_integrate(
                        lambda y: (1 / (Q_compass(y)**4)) * FUU(y), yA, yB)[0]

                    theory = FUUcos2_integral / FUU_integral

                else:  # use coefficient method

                    coeff = (1 - y) / (1 - y + 0.5 * y**2)
                    FUUcos2 = (boermulders.get_FUU(x, z, Q2, pT, tar, had) +
                               cahn.get_cahn(x, z, Q2, pT, tar, had))
                    FUU = upol.get_FUU(x, z, Q2, pT, tar, had)

                    theory = coeff * FUUcos2 / FUU_integral

                return theory

            thy = yield_thy(col, should_integrate=True)

        elif obs == 'AUTsinphiS':  # This is for collinear!

            if tar == 'p':
                pT = None
                FUTsinphiS = AUTsinphiS.get_FX(x, z, Q2, pT, 'p', had)
                FUU = upol.get_FUU(x, z, Q2, pT, 'p', had)

            coeff = 1.
            if col == 'HERMES':
                # add depolarization factor for HERMES
                coeff = np.sqrt(1.0 - y) * (2 - y) / (1 - y + 0.5 * y**2)
            if col == 'COMPASS':
                coeff = 1.

            thy = coeff * FUTsinphiS / FUU

        else:
            print 'ERR: exp=%d obs=%s and target=%s not implemented' % (k, obs,
                                                                        tar)
            sys.exit()

        return thy

    def gen_report(self, verb=1, level=1):
        """
        verb = 0: Do not print on screen. Only return list of strings
        verv = 1: print on screen the report
        level= 0: only the total chi2s
        level= 1: include point by point
        """

        L = []

        L.append('reaction: %s' % self.reaction)

        L.append('%7s %10s %10s %10s %10s %5s %10s %10s %10s %10s' %
                 ('idx', 'tar', 'had', 'col', 'obs', 'npts', 'chi2',
                  'chi2/npts', 'rchi2', 'nchi2'))
        for k in self.tabs:
            #print k,len(self.tabs[k]['value'])
            if self.tabs[k]['value'].size == 0:
                continue
            res = self._get_residuals(k)
            rres = self._get_rres(k)
            nres = self._get_nres(k)

            chi2 = np.sum(res**2)
            rchi2 = np.sum(rres**2)
            nchi2 = nres**2
            tar = self.tabs[k]['target'][0]
            col = self.tabs[k]['col'][0].split()[0]
            obs = self.tabs[k]['obs'][0].split()[0]
            had = self.tabs[k]['hadron'][0].split()[0]
            npts = res.size
            if npts > 0:
                L.append(
                    '%7d %10s %10s %10s %10s %5d %10.2f %10.2f  %10.2f %10.2f'
                    % (k, tar, had, col, obs, npts, chi2, chi2 / npts, rchi2,
                       nchi2))
            elif npts == 0:
                L.append(
                    '%7d %10s %10s %10s %10s %5d %10.2f %10.2f %10.2f %10.2f' %
                    (k, tar, had, col, obs, npts, chi2, 0.0, rchi2, nchi2))

        if level == 1:
            L.append('-' * 100)

            msg = 'idx=%7d,  '
            msg += 'col=%7s,  '
            msg += 'tar=%7s,  '
            msg += 'had=%7s,  '
            msg += 'obs=%7s,  '
            if 'dependence' in self.tabs[k]:
                msg += 'dep=%7s,  '
            if 'Dependence' in self.tabs[k]:
                msg += 'dep=%7s,  '
            msg += 'x=%10.3e,  '
            msg += 'z=%10.3e,  '
            msg += 'pT=%10.3e,  '
            msg += 'Q2=%10.3e,  '
            msg += 'yh=%10.3e,  '
            msg += 'yp=%10.3e,  '
            msg += 'dy=%10.3e,  '
            msg += 'exp=%10.3e,  '
            msg += 'alpha=%10.3e,  '
            msg += 'thy=%10.3e,  '
            if 'dthy' in self.tabs[k]:
                msg += 'dthy=%10.3e,  '
            msg += 'shift=%10.3e,  '
            msg += 'chi2=%10.3f  '

            for k in self.tabs:
                if len(self.tabs[k]['value']) == 0:
                    continue
                for i in range(len(self.tabs[k]['value'])):
                    row = [k]
                    row.append(self.tabs[k]['col'][i])
                    row.append(self.tabs[k]['target'][i])
                    row.append(self.tabs[k]['hadron'][i])
                    row.append(self.tabs[k]['obs'][i])
                    if 'dependence' in self.tabs[k]:
                        row.append(self.tabs[k]['dependence'][i].strip())
                    if 'Dependence' in self.tabs[k]:
                        row.append(self.tabs[k]['Dependence'][i].strip())
                    row.append(self.tabs[k]['x'][i])
                    row.append(self.tabs[k]['z'][i])
                    row.append(self.tabs[k]['pT'][i])
                    row.append(self.tabs[k]['Q2'][i])
                    row.append(self.tabs[k]['yh'][i])
                    row.append(self.tabs[k]['yp'][i])
                    row.append(self.tabs[k]['dy'][i])
                    row.append(self.tabs[k]['value'][i])
                    row.append(self.tabs[k]['alpha'][i])
                    row.append(self.tabs[k]['thy'][i])
                    if 'dthy' in self.tabs[k]:
                        row.append(self.tabs[k]['dthy'][i])
                    row.append(self.tabs[k]['shift'][i])
                    # row.append(self.tabs[k]['residuals'][i])
                    # row.append(self.tabs[k]['r-residuals'][i])
                    res = self.tabs[k]['residuals'][i]
                    if res < 0:
                        chi2 = -res**2
                    else:
                        chi2 = res**2
                    row.append(chi2)
                    row = tuple(row)
                    L.append(msg % row)

        if verb == 0:
            return L
        elif verb == 1:
            for l in L:
                print l
            return L
Esempio n. 5
0
class RESIDUALS(_RESIDUALS):
    def __init__(self, react='sidis'):
        if react == 'sidis':
            self.reaction = 'sidis'
            self.tabs = conf['sidis tabs']
        elif react == 'sidisEIC':
            self.reaction = 'sidisEIC'
            self.tabs = conf['sidisEIC tabs']
        elif react == 'sidisSoLID':
            self.reaction = 'sidisSoLID'
            self.tabs = conf['sidisSoLID tabs']
        self.dis_stfuncs = DIS_STFUNCS()
        self.setup()

    def _get_theory(self, entry):
        k, i = entry
        x = self.tabs[k]['x'][i]
        try:
            y = self.tabs[k]['y'][i]
        except (ValueError, KeyError):
            y = None
        z = self.tabs[k]['z'][i]
        try:
            Q2 = self.tabs[k]['Q2'][i]
        except ValueError:
            Q2 = None
        pT = self.tabs[k]['pT'][i]
        exp = self.tabs[k]['value'][i]
        tar = self.tabs[k]['target'][i]
        had = self.tabs[k]['hadron'][i]
        obs = self.tabs[k]['obs'][i].strip()
        col = self.tabs[k]['col'][i].strip().upper()
        try:
            F2 = self.tabs[k]['F2'][i]
        except KeyError:
            F2 = None

        M = conf['aux'].M
        M2 = conf['aux'].M**2
        Mpi2 = conf['aux'].Mpi**2

        if tar == 'proton': tar = 'p'
        elif tar == 'neutron': tar = 'n'
        elif tar == 'deuteron': tar = 'd'

        if obs == 'FUU':

            thy = upol.get_FUU(x, z, Q2, pT, tar, had)

        elif obs == 'M':

            if had == 'h+': had = 'pi+'
            if had == 'h-': had = 'pi-'

            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            if F2 == None: F2 = self.dis_stfuncs.get_F2(x, Q2, tar)
            thy = FUU / F2
            if col == 'HERMES' or col == 'hermes': thy = 2 * np.pi * pT * thy
            if col == 'COMPASS' or col == 'compass': thy = np.pi * thy

        elif obs == 'AUTcollins':

            # convention factor
            coeff = 1.
            if col == 'HERMES': coeff = 1  # hermes is sin(phi_s+phi_h)
            elif col == 'COMPASS': coeff = -1  # compass is sin(phi_s+phi_h+pi)

            # add depolarization factor
            if col == 'HERMES': coeff *= 2 * (1 - y) / (1 + (1 - y)**2)

            FUT = collins.get_FUT(x, z, Q2, pT, tar, had)
            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            thy = coeff * FUT / FUU

        elif obs == 'AUTsivers':

            # convention factor
            coeff = 1.

            FUT = sivers.get_FUT(x, z, Q2, pT, tar, had)
            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            thy = coeff * FUT / FUU

        elif obs == 'AUUcos2':

            if had == 'h+': had = 'pi+'
            if had == 'h-': had = 'pi-'
            ''' The data from JLab and HERMES do not require any integrations,
                but require a y-based coefficient. The COMPASS data requires an
                integration over y, and does not require the coefficient.    '''
            def yield_thy(accelerator, should_integrate, ny=10):

                # depending on input parameters, either selects the coefficient
                # method or the integration method of producing a residual value.
                # root_s and W2_min also fluctuate based on accelerator.

                # set known values based on the source of AUUcos2 data
                if accelerator == 'COMPASS':
                    ROOT_S = 17.3
                    W2_MIN = 25.0
                    RANGE_MIN = 0.2
                    RANGE_MAX = 0.9

                elif accelerator == 'CLAS':  #JLab col in the data files
                    ROOT_S = 3.42
                    W2_MIN = 4.0
                    RANGE_MIN = 0.2
                    RANGE_MAX = 0.85

                elif accelerator == 'HERMES':
                    ROOT_S = 7.25
                    W2_MIN = 10
                    RANGE_MIN = 0.2
                    RANGE_MAX = 0.85

                if should_integrate:

                    def fast_integrate(f, low, hi, ny):
                        f = np.vectorize(f)
                        return integrate.fixed_quad(f, low, hi, n=ny)

                    Q2_MIN = 1
                    Q2_MAX = 1000

                    yA = max(  # lower bound of integration
                        RANGE_MIN, Q2_MIN / (x * ((ROOT_S**2) - M2)),
                        (W2_MIN - M2) / ((1 - x) * ((ROOT_S**2) - M2)))
                    yB = min(  # upper bound of integration
                        Q2_MAX / (x * ((ROOT_S**2) - M2)), RANGE_MAX)

                    # any value dependent on y must be a function so that its value
                    # may be recalculated during the integration process
                    Q = lambda y: np.sqrt(((ROOT_S**2) - M2) * x * y)

                    if accelerator == 'HERMES':
                        FUU = lambda y: (1. - y + 0.5 * y**2) * upol.get_FUU(
                            x, z,
                            Q(y)**2, pT, tar, had)
                        FUUcos2 = lambda y: (1. - y) * (boermulders.get_FUU(
                            x, z,
                            Q(y)**2, pT, tar, had) + cahn.get_cahn(
                                x, z,
                                Q(y)**2, pT, tar, had))

                    elif accelerator == 'COMPASS':
                        FUU = lambda y: upol.get_FUU(x, z,
                                                     Q(y)**2, pT, tar, had)
                        FUUcos2 = lambda y: (boermulders.get_FUU(
                            x, z,
                            Q(y)**2, pT, tar, had) + cahn.get_cahn(
                                x, z,
                                Q(y)**2, pT, tar, had))

                    elif accelerator == 'CLAS':
                        # CLAS accelerators measure H4 / (H2 + H1), which can be related
                        # to the AUUcos2 asymmetry.
                        gamma = lambda y: (2 * M * x) / Q(y)
                        kappa = lambda y: 1 / (1 + gamma(x, y)**2)
                        zeta = lambda y: 1 - y - (0.25 * (gamma(x, y)**2) *
                                                  (y**2))
                        epsilon = lambda y: 1 / (1 + (
                            (y**2) / (2 * kappa(x, y) * zeta(x, y))))
                        ppa_over_Eh = lambda y: np.sqrt(1 - (pT**2 + Mpi**2) *
                                                        (2. * M * x /
                                                         (z * Q(y)**2))**2)

                        FUU = lambda y: ppa_over_Eh * (kappa / epsilon) * (
                            1 + gamma**2 /
                            (2. * x)) * upol.get_FUU(x, z,
                                                     Q(y)**2, pT, tar, had)
                        FUUcos2 = lambda y: (ppa_over_Eh / 2.) * (
                            1 + gamma**2 / (2. * x)) * (boermulders.get_FUU(
                                x, z,
                                Q(y)**2, pT, tar, had) + cahn.get_cahn(
                                    x, z,
                                    Q(y)**2, pT, tar, had))

                    # integrate over y for the numerator and denominator of AUUcos2
                    FUUcos2_integral = fast_integrate(
                        lambda y: (1. / (Q(y)**4)) * FUUcos2(y), yA, yB, ny)[0]
                    FUU_integral = fast_integrate(
                        lambda y: (1. / (Q(y)**4)) * FUU(y), yA, yB, ny)[0]

                    theory = FUUcos2_integral / FUU_integral

                else:  # no integration

                    if accelerator == 'HERMES':
                        coeff = (1 - y) / (1 - y + 0.5 * y**2)
                    elif accelerator == 'COMPASS':
                        coeff = 1.
                    elif accelerator == 'CLAS':
                        gamma = (2. * M * x) / np.sqrt(Q2)
                        kappa = 1. / (1. + gamma**2.)
                        zeta = 1. - y - (0.25 * (gamma**2.) * (y**2.))
                        epsilon = 1. / (1. + ((y**2.) / (2. * kappa * zeta)))

                        coeff = epsilon / (2. * kappa)

                    FUUcos2 = (boermulders.get_FUU(x, z, Q2, pT, tar, had) +
                               cahn.get_cahn(x, z, Q2, pT, tar, had))
                    FUU = upol.get_FUU(x, z, Q2, pT, tar, had)

                    theory = coeff * FUUcos2 / FUU

                return theory

            if col == 'COMPASS':
                thy = yield_thy(col, should_integrate=True, ny=10)
            elif col == 'HERMES':
                thy = yield_thy(col, should_integrate=False, ny=10)
            elif col == 'CLAS':
                thy = yield_thy(col, should_integrate=False, ny=10)

        elif obs == 'AUTsinphiS':  # This is for collinear!

            if tar == 'p':
                pT = None
                FUTsinphiS = AUTsinphiS.get_FX(x, z, Q2, pT, 'p', had)
                FUU = upol.get_FUU(x, z, Q2, pT, 'p', had)

            coeff = 1.
            if col == 'HERMES':
                # add depolarization factor for HERMES
                coeff = np.sqrt(1.0 - y) * (2 - y) / (1 - y + 0.5 * y**2)
            if col == 'COMPASS':
                coeff = 1.

            thy = coeff * FUTsinphiS / FUU

        else:
            print('ERR: exp=%d obs=%s and target=%s not implemented' %
                  (k, obs, tar))
            sys.exit()

        return thy

    def gen_report(self, verb=1, level=1):
        """
        verb = 0: Do not print on screen. Only return list of strings
        verv = 1: print on screen the report
        level= 0: only the total chi2s
        level= 1: include point by point
        """

        L = []

        L.append('reaction: %s' % self.reaction)

        L.append('%7s %10s %10s %10s %10s %5s %10s %10s %10s %10s' %
                 ('idx', 'tar', 'had', 'col', 'obs', 'npts', 'chi2',
                  'chi2/npts', 'rchi2', 'nchi2'))
        for k in self.tabs:
            #print k,len(self.tabs[k]['value'])
            if self.tabs[k]['value'].size == 0:
                continue
            res = self._get_residuals(k)
            rres = self._get_rres(k)
            nres = self._get_nres(k)

            chi2 = np.sum(res**2)
            rchi2 = np.sum(rres**2)
            nchi2 = nres**2
            tar = self.tabs[k]['target'][0]
            col = self.tabs[k]['col'][0].split()[0]
            obs = self.tabs[k]['obs'][0].split()[0]
            had = self.tabs[k]['hadron'][0].split()[0]
            npts = res.size
            if npts > 0:
                L.append(
                    '%7d %10s %10s %10s %10s %5d %10.2f %10.2f  %10.2f %10.2f'
                    % (k, tar, had, col, obs, npts, chi2, chi2 / npts, rchi2,
                       nchi2))
            elif npts == 0:
                L.append(
                    '%7d %10s %10s %10s %10s %5d %10.2f %10.2f %10.2f %10.2f' %
                    (k, tar, had, col, obs, npts, chi2, 0.0, rchi2, nchi2))

        if level == 1:
            L.append('-' * 100)

            msg = 'idx=%7d,  '
            msg += 'col=%7s,  '
            msg += 'tar=%7s,  '
            msg += 'had=%7s,  '
            msg += 'obs=%7s,  '
            if 'dependence' in self.tabs[k]:
                msg += 'dep=%7s,  '
            if 'Dependence' in self.tabs[k]:
                msg += 'dep=%7s,  '
            msg += 'x=%10.3e,  '
            msg += 'z=%10.3e,  '
            msg += 'pT=%10.3e,  '
            msg += 'Q2=%10.3e,  '
            msg += 'yh=%10.3e,  '
            msg += 'yp=%10.3e,  '
            msg += 'dy=%10.3e,  '
            msg += 'exp=%10.3e,  '
            msg += 'alpha=%10.3e,  '
            msg += 'thy=%10.3e,  '
            if 'dthy' in self.tabs[k]:
                msg += 'dthy=%10.3e,  '
            msg += 'shift=%10.3e,  '
            msg += 'chi2=%10.3f  '

            for k in self.tabs:
                if len(self.tabs[k]['value']) == 0:
                    continue
                for i in range(len(self.tabs[k]['value'])):
                    row = [k]
                    row.append(self.tabs[k]['col'][i])
                    row.append(self.tabs[k]['target'][i])
                    row.append(self.tabs[k]['hadron'][i])
                    row.append(self.tabs[k]['obs'][i])
                    if 'dependence' in self.tabs[k]:
                        row.append(self.tabs[k]['dependence'][i].strip())
                    if 'Dependence' in self.tabs[k]:
                        row.append(self.tabs[k]['Dependence'][i].strip())
                    row.append(self.tabs[k]['x'][i])
                    row.append(self.tabs[k]['z'][i])
                    row.append(self.tabs[k]['pT'][i])
                    row.append(self.tabs[k]['Q2'][i])
                    row.append(self.tabs[k]['yh'][i])
                    row.append(self.tabs[k]['yp'][i])
                    row.append(self.tabs[k]['dy'][i])
                    row.append(self.tabs[k]['value'][i])
                    row.append(self.tabs[k]['alpha'][i])
                    row.append(self.tabs[k]['thy'][i])
                    if 'dthy' in self.tabs[k]:
                        row.append(self.tabs[k]['dthy'][i])
                    row.append(self.tabs[k]['shift'][i])
                    # row.append(self.tabs[k]['residuals'][i])
                    # row.append(self.tabs[k]['r-residuals'][i])
                    res = self.tabs[k]['residuals'][i]
                    if res < 0:
                        chi2 = -res**2
                    else:
                        chi2 = res**2
                    row.append(chi2)
                    row = tuple(row)
                    L.append(msg % row)

        if verb == 0:
            return L
        elif verb == 1:
            for l in L:
                print(l)
            return L
Esempio n. 6
0
class RESIDUALS(_RESIDUALS):
    def __init__(self):
        self.reaction = 'sidis'
        self.tabs = conf['sidis tabs']
        self.dis_stfuncs = DIS_STFUNCS()
        self.setup()

    def _get_theory(self, entry):
        k, i = entry
        x = self.tabs[k]['x'][i]
        y = self.tabs[k]['y'][i]
        z = self.tabs[k]['z'][i]
        Q2 = self.tabs[k]['Q2'][i]
        pT = self.tabs[k]['pT'][i]
        exp = self.tabs[k]['value'][i]
        tar = self.tabs[k]['target'][i]
        had = self.tabs[k]['hadron'][i]
        obs = self.tabs[k]['obs'][i].strip()
        col = self.tabs[k]['col'][i].strip().upper()
        M = conf['aux'].M
        S = 299.29
        W2_min = 25
        Q2_min = 1
        Q2_max = 1000

        if tar == 'proton': tar = 'p'
        elif tar == 'neutron': tar = 'n'
        elif tar == 'deuteron': tar = 'd'

        if obs == 'FUU':

            thy = upol.get_FUU(x, z, Q2, pT, tar, had)

        elif obs == 'M':

            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            F2 = self.dis_stfuncs.get_F2(x, Q2, tar)
            thy = FUU / F2
            if col == 'HERMES': thy *= 2 * np.pi * pT

        elif obs == 'AUTcollins':

            # convention factor
            coeff = 1.
            if col == 'HERMES': coeff = 1  # hermes is sin(phi_s+phi_h)
            elif col == 'COMPASS': coeff = -1  # compass is sin(phi_s+phi_h+pi)

            # add depolarization factor
            if col == 'HERMES': coeff *= 2 * (1 - y) / (1 + (1 - y)**2)

            FUT = collins.get_FUT(x, z, Q2, pT, tar, had)
            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            thy = coeff * FUT / FUU

        elif obs == 'AUTsivers':

            # convention factor
            coeff = 1.

            FUT = sivers.get_FUT(x, z, Q2, pT, tar, had)
            FUU = upol.get_FUU(x, z, Q2, pT, tar, had)
            thy = coeff * FUT / FUU

        elif obs == 'AUUcos2':

            # data from Hermes and Jlab do not need integration
            if col == ['COMPASS', 'JLAB']:

                coeff = (1 - y) / (1 - y + 0.5 * y**2)
                FUUcos2 = boermulders.get_FUU(x, z, Q2, pT, tar,
                                              had) + cahn.get_cahn(
                                                  x, z, Q2, pT, tar, had)
                FUU = upol.get_FUU(x, z, Q2, pT, tar, had)

                thy = coeff * FUUcos2 / FUU


# data from compass will be integrated over y
            elif col == 'HERMES':

                def integrand(f, low, hi):
                    f = np.vectorize(f)
                    return fixed_quad(f, low, hi, n=10)

                yLOW = max(0.2, Q2_max / (x * (S - M**2)),
                           (W2_min - M**2) / ((1 - x) * (S - M**2)))
                yUP = min(Q2_max / (x * (S - M**2)), 0.9)
                Q_XY = lambda y: np.sqrt((S - M**2) * x * y)

                FUUcos2 = lambda y: boermulders.get_FUU(
                    x, z,
                    Q_XY(y)**2, pT, tar, had) + cahn.get_cahn(
                        x, z,
                        Q_XY(y)**2, pT, tar, had)
                FUU = lambda y: upol.get_FUU(x, z, Q_XY(y)**2, pT, tar, had)

                #inegrate over y for top and bottom of AUUcos2 compass
                FUUcos2_int = integrand(
                    lambda y: (1 / (Q_XY(y)**4)) * FUUcos2(y), yLOW, yUP)[0]
                FUU_int = integrand(lambda y: (1 / (Q_XY(y)**4)) * FUU(y),
                                    yLOW, yUP)[0]
                thy = FUUcos2_int / FUU_int

        elif obs == 'AUTsinphiS':  # This is for collinear!

            if tar == 'p':
                pT = None
                FUTsinphiS = AUTsinphiS.get_FX(x, z, Q2, pT, 'p', had)
                FUU = upol.get_FUU(x, z, Q2, pT, 'p', had)

            coeff = 1.
            if col == 'HERMES':
                # add depolarization factor for HERMES
                coeff = np.sqrt(1.0 - y) * (2 - y) / (1 - y + 0.5 * y**2)
            if col == 'COMPASS':
                coeff = 1.

            thy = coeff * FUTsinphiS / FUU

        else:
            print 'ERR: exp=%d obs=%s and target=%s not implemented' % (k, obs,
                                                                        tar)
            sys.exit()

        return thy

    def gen_report(self, verb=1, level=1):
        """
        verb = 0: Do not print on screen. Only return list of strings
        verb = 1: print on screen the report
        level= 0: only the total chi2s
        level= 1: include point by point
        """

        L = []

        L.append('reaction: %s' % self.reaction)

        L.append('%7s %10s %10s %10s %10s %5s %10s %10s %10s %10s' %
                 ('idx', 'tar', 'had', 'col', 'obs', 'npts', 'chi2',
                  'chi2/npts', 'rchi2', 'nchi2'))
        for k in self.tabs:
            #print k,len(self.tabs[k]['value'])
            if self.tabs[k]['value'].size == 0:
                continue
            res = self._get_residuals(k)
            rres = self._get_rres(k)
            nres = self._get_nres(k)

            chi2 = np.sum(res**2)
            rchi2 = np.sum(rres**2)
            nchi2 = nres**2
            tar = self.tabs[k]['target'][0]
            col = self.tabs[k]['col'][0].split()[0]
            obs = self.tabs[k]['obs'][0].split()[0]
            had = self.tabs[k]['hadron'][0].split()[0]
            npts = res.size
            if npts > 0:
                L.append(
                    '%7d %10s %10s %10s %10s %5d %10.2f %10.2f  %10.2f %10.2f'
                    % (k, tar, had, col, obs, npts, chi2, chi2 / npts, rchi2,
                       nchi2))
            elif npts == 0:
                L.append(
                    '%7d %10s %10s %10s %10s %5d %10.2f %10.2f %10.2f %10.2f' %
                    (k, tar, had, col, obs, npts, chi2, 0.0, rchi2, nchi2))

        if level == 1:
            L.append('-' * 100)

            msg = 'idx=%7d,  '
            msg += 'col=%7s,  '
            msg += 'tar=%7s,  '
            msg += 'had=%7s,  '
            msg += 'obs=%7s,  '
            if 'dependence' in self.tabs[k]:
                msg += 'dep=%7s,  '
            if 'Dependence' in self.tabs[k]:
                msg += 'dep=%7s,  '
            msg += 'x=%10.3e,  '
            msg += 'z=%10.3e,  '
            msg += 'pT=%10.3e,  '
            msg += 'Q2=%10.3e,  '
            msg += 'yh=%10.3e,  '
            msg += 'yp=%10.3e,  '
            msg += 'dy=%10.3e,  '
            msg += 'exp=%10.3e,  '
            msg += 'alpha=%10.3e,  '
            msg += 'thy=%10.3e,  '
            if 'dthy' in self.tabs[k]:
                msg += 'dthy=%10.3e,  '
            msg += 'shift=%10.3e,  '
            msg += 'chi2=%10.3f  '

            for k in self.tabs:
                if len(self.tabs[k]['value']) == 0:
                    continue
                for i in range(len(self.tabs[k]['value'])):
                    row = [k]
                    row.append(self.tabs[k]['col'][i])
                    row.append(self.tabs[k]['target'][i])
                    row.append(self.tabs[k]['hadron'][i])
                    row.append(self.tabs[k]['obs'][i])
                    if 'dependence' in self.tabs[k]:
                        row.append(self.tabs[k]['dependence'][i].strip())
                    if 'Dependence' in self.tabs[k]:
                        row.append(self.tabs[k]['Dependence'][i].strip())
                    row.append(self.tabs[k]['x'][i])
                    row.append(self.tabs[k]['z'][i])
                    row.append(self.tabs[k]['pT'][i])
                    row.append(self.tabs[k]['Q2'][i])
                    row.append(self.tabs[k]['yh'][i])
                    row.append(self.tabs[k]['yp'][i])
                    row.append(self.tabs[k]['dy'][i])
                    row.append(self.tabs[k]['value'][i])
                    row.append(self.tabs[k]['alpha'][i])
                    row.append(self.tabs[k]['thy'][i])
                    if 'dthy' in self.tabs[k]:
                        row.append(self.tabs[k]['dthy'][i])
                    row.append(self.tabs[k]['shift'][i])
                    # row.append(self.tabs[k]['residuals'][i])
                    # row.append(self.tabs[k]['r-residuals'][i])
                    res = self.tabs[k]['residuals'][i]
                    if res < 0:
                        chi2 = -res**2
                    else:
                        chi2 = res**2
                    row.append(chi2)
                    row = tuple(row)
                    L.append(msg % row)

        if verb == 0:
            return L
        elif verb == 1:
            for l in L:
                print l
            return L