Пример #1
0
def rate(shape, emin, emax, param, cone, area):
    """
    Calculates the rate of events for a power-law distribution,
    in a given energy range, collection area and solid angle

    Parameters
    ----------
    shape: `string` weighted spectrum shape
    emin:  `float`  minimum energy
    emax:  `float`  maximum energy
    param: `dict` with weighted spectral parameters
    cone:  `float`  angle [deg] for the solid angle cone
    area:  `float`  collection area [cm**2]

    if shape is 'PowerLaw':
    param should include 'f0','e0','alpha'
    dFdE  = f0 * np.power(E / e0, alpha)

    if shape is 'LogParabola':
    param should include 'f0','e0','alpha','beta'
    dFdE  = f0 * np.power(E / e0, alpha + beta * np.log10(E/e0))

    Returns
    -------
    rate: `float` rate of events
    """

    if (cone == 0):
        omega = 1
    else:
        omega = 2 * np.pi * (1 - np.cos(cone)) * u.sr

    if (shape == "PowerLaw"):
        if (len(param) != 3):
            print("param included {} parameters, not 3".format(len(param)))
            print("param should include 'f0', 'e0', 'alpha'")
            sys.exit(1)

        for key in ['f0', 'e0', 'alpha']:
            if (key not in param.keys()):
                print("{} is missing in param".format(key))
                print("param should include 'f0', 'e0', 'alpha'")
                sys.exit(1)

        integral = param['f0'] * int_diff_sp(emin, emax, param['alpha'],
                                             param['e0'])

    elif (shape == "LogParabola"):
        if (len(param) != 4):
            print("param included {} parameters, not 4".format(len(param)))
            print("param should include 'f0', 'e0', 'alpha', 'beta'")
            sys.exit(1)

        for key in ['f0', 'e0', 'alpha', 'beta']:
            if (key not in param.keys()):
                print("{} is missing in param".format(key))
                print("param should include 'f0', 'e0', 'alpha', 'beta'")
                sys.exit(1)

        log_parabola = LogParabolaSpectralModel.from_log10(
            amplitude=param['f0'],
            reference=param['e0'],
            alpha=-1 * param['alpha'],
            beta=-1 * param['beta'])
        integral = log_parabola.integral(emin, emax)

    rate = area * omega * integral

    return rate
Пример #2
0
     name="norm-logpar",
     model=LogParabolaNormSpectralModel(
         alpha=2.3 * u.Unit(""),
         norm=4 * u.Unit(""),
         reference=1 * u.TeV,
         beta=0.5 * u.Unit(""),
     ),
     val_at_2TeV=u.Quantity(0.6387956571420305, ""),
     integral_1_10TeV=u.Quantity(2.255689748270628, "TeV"),
     eflux_1_10TeV=u.Quantity(3.9586515834989267, "TeV2"),
 ),
 dict(
     name="logpar10",
     model=LogParabolaSpectralModel.from_log10(
         alpha=2.3 * u.Unit(""),
         amplitude=4 / u.cm ** 2 / u.s / u.TeV,
         reference=1 * u.TeV,
         beta=1.151292546497023 * u.Unit(""),
     ),
     val_at_2TeV=u.Quantity(0.6387956571420305, "cm-2 s-1 TeV-1"),
     integral_1_10TeV=u.Quantity(2.255689748270628, "cm-2 s-1"),
     eflux_1_10TeV=u.Quantity(3.9586515834989267, "TeV cm-2 s-1"),
     e_peak=0.74082 * u.TeV,
 ),
 dict(
     name="constant",
     model=ConstantSpectralModel(const=4 / u.cm ** 2 / u.s / u.TeV),
     val_at_2TeV=u.Quantity(4, "cm-2 s-1 TeV-1"),
     integral_1_10TeV=u.Quantity(35.9999999999999, "cm-2 s-1"),
     eflux_1_10TeV=u.Quantity(198.00000000000006, "TeV cm-2 s-1"),
 ),
 dict(
Пример #3
0
def weight(shape, emin, emax, sim_sp_idx, rate, nev, w_param):
    """
    Calculates the weight of events to transform a power-law distribution
    with spectral index sim_sp_idx to a power-law distribution with 
    spectral index w_sp_idx

    Parameters
    ----------
    shape:      `string` estimated spectrum shape
    emin:       `float` minimum energy
    emax:       `float` maximum energy
    sim_sp_idx: `float` simulated spectral index of the power-law distribution
    rate:       `float` rate of simulated events
    nev:        `int`   number of simulated events 
    w_param:    `dict` with weighted spectral parameters

    if shape is 'PowerLaw':
    w_param should include 'f0','e0','alpha'
    dFdE  = f0 * np.power(E / e0, alpha)

    if shape is 'LogParabola':
    w_param should include 'f0','e0','alpha','beta'
    dFdE  = f0 * np.power(E / e0, alpha + beta * np.log10(E/e0))

    Returns
    -------
    weight: `float` rate of events
    """

    sim_integral = nev / int_diff_sp(emin, emax, sim_sp_idx, w_param['e0'])

    if (shape == "PowerLaw"):
        if (len(w_param) != 3):
            print("param included {} parameters, not 3".format(len(w_param)))
            print("param should include 'f0', 'e0', 'alpha'")
            sys.exit(1)

        for key in ['f0', 'e0', 'alpha']:
            if (key not in w_param.keys()):
                print("{} is missing in param".format(key))
                print("param should include 'f0', 'e0', 'alpha'")
                sys.exit(1)

        norm_sim = sim_integral * int_diff_sp(emin, emax, w_param['alpha'],
                                              w_param['e0'])

    elif (shape == "LogParabola"):
        if (len(w_param) != 4):
            print("param included {} parameters, not 4".format(len(w_param)))
            print("param should include 'f0', 'e0', 'alpha', 'beta'")
            sys.exit(1)

        for key in ['f0', 'e0', 'alpha', 'beta']:
            if (key not in w_param.keys()):
                print("{} is missing in param".format(key))
                print("param should include 'f0', 'e0', 'alpha', 'beta'")
                sys.exit(1)

        log_parabola = LogParabolaSpectralModel.from_log10(
            amplitude=sim_integral / (u.s * u.cm * u.cm),
            reference=w_param['e0'],
            alpha=-1 * w_param['alpha'],
            beta=-1 * w_param['beta'])

        norm_sim = log_parabola.integral(emin, emax) * (u.s * u.cm * u.cm)

    weight = rate / norm_sim

    return weight