Exemplo n.º 1
0
def energy_results(dl2_data, points_outfile=None, plot_outfile=None):
    """
    Plot energy resolution, energy bias and energy migration matrix in the same figure

    Parameters
    ----------
    dl2_data: `pandas.DataFrame`
        dl2 MC gamma data - must include the columns `mc_energy` and `reco_energy`
    points_outfile: None or str
        if specified, save the resolution and bias in hdf5 format
    plot_outfile: None or str
        if specified, save the figure

    Returns
    -------
    fig, axes: `matplotlib.pyplot.figure`, `matplotlib.pyplot.axes`
    """
    fig, axes = plt.subplots(2, 2, figsize=(12, 8))

    ctaplot.plot_energy_resolution(dl2_data.mc_energy.values * u.TeV,
                                   dl2_data.reco_energy.values * u.TeV,
                                   ax=axes[0, 0], bias_correction=False)
    ctaplot.plot_energy_resolution_cta_requirement('north', ax=axes[0, 0], color='black')

    ctaplot.plot_energy_bias(dl2_data.mc_energy.values * u.TeV, dl2_data.reco_energy.values * u.TeV, ax=axes[1, 0])
    ctaplot.plot_migration_matrix(dl2_data.mc_energy.apply(np.log10),
                                  dl2_data.reco_energy.apply(np.log10),
                                  ax=axes[0, 1],
                                  colorbar=True,
                                  xy_line=True,
                                  hist2d_args=dict(norm=matplotlib.colors.LogNorm()),
                                  line_args=dict(color='black'),
                                  )
    axes[0, 0].legend()
    axes[0, 1].set_xlabel('log(mc energy/[TeV])')
    axes[0, 1].set_ylabel('log(reco energy/[TeV])')
    axes[0, 0].set_title("")
    axes[0, 0].label_outer()
    axes[1, 0].set_title("")
    axes[1, 0].set_ylabel("Energy bias")
    for ax in axes.ravel():
        ax.grid(True, which='both')
    axes[1, 1].remove()

    fig.tight_layout()

    if points_outfile:
        e_bins, e_res = ctaplot.energy_resolution_per_energy(dl2_data.mc_energy.values * u.TeV,
                                                             dl2_data.reco_energy.values * u.TeV)
        e_bins, e_bias = ctaplot.energy_bias(dl2_data.mc_energy.values * u.TeV, dl2_data.reco_energy.values * u.TeV)
        write_energy_resolutions(points_outfile, e_bins, e_res, e_bias)

    if plot_outfile:
        fig.savefig(plot_outfile)

    return fig, axes
Exemplo n.º 2
0
def plot_energy_resolution_hdf(gamma_filename, ax=None, **kwargs):
    """
    Plot angular resolution from an IRF file

    Parameters
    ----------
    irf_filename
    ax
    kwargs

    Returns
    -------

    """
    data = pd.read_hdf(gamma_filename)

    ax = ctaplot.plot_angular_resolution_cta_performance('north',
                                                         color='black',
                                                         label='CTA North',
                                                         ax=ax)
    ax = ctaplot.plot_energy_resolution(data.mc_energy,
                                        data.reco_energy,
                                        ax=ax,
                                        **kwargs)
    ax.grid(which='both')
    ax.set_title('Energy resoluton', fontsize=18)
    ax.legend()
    return ax
Exemplo n.º 3
0
def plot_energy_resolution(dl2_data, ax=None, bias_correction=False, cta_req_north=False, **kwargs):
    """
    Plot the energy resolution from a pandas dataframe of DL2 data.
    See `~ctaplot.plot_energy_resolution` for doc.

    Parameters
    ----------
    dl2_data: `pandas.DataFrame`
        Reconstructed MC events at DL2+ level.
    ax: `matplotlib.pyplot.axes` or None
    bias_correction: `bool`
        correct for systematic bias
    cta_req_north: `bool`
        if True, includes CTA requirement curve
    kwargs: args for `matplotlib.pyplot.plot`

    Returns
    -------
    ax: `matplotlib.pyplot.axes`
    """

    ax = ctaplot.plot_energy_resolution(dl2_data.mc_energy.values * u.TeV,
                                        dl2_data.reco_energy.values * u.TeV,
                                        ax=ax,
                                        bias_correction=bias_correction,
                                        **kwargs,
                                        )
    ax.grid(which='both')
    if cta_req_north:
        ax = ctaplot.plot_energy_resolution_cta_requirement('north', ax=ax, color='black')
    return ax
Exemplo n.º 4
0
def show_energy_resolution(predicted_mc_energy, true_mc_energy, 
                           percentile=68.27, confidence_level=0.95, bias_correction=False,
                           label="this method", include_requirement=[], 
                           xlim=None, ylim=None, fmt=None, ax=None):
    """
    Show the energy resolution for a model's predictions.
    """
    
    # Create new figure
    if ax is None:
        plt.figure(figsize=(6,6))
        ax = plt.gca()
    fmt = fmt or "o"
    ax = ctaplot.plot_energy_resolution(
        true_mc_energy, predicted_mc_energy, percentile=percentile, 
        confidence_level=confidence_level, bias_correction=bias_correction, 
        fmt=fmt, label=label, ax=ax
    )
    if xlim is not None:
        ax.set_xlim(xlim)
    if ylim is not None:
        ax.set_ylim(ylim)
    ax.legend()

    try:
        for include in include_requirement:
            ax = ctaplot.plot_energy_resolution_cta_requirement(include, ax)
    except:
        print("Unable to display cta requirements.")
    return ax
Exemplo n.º 5
0
    def plot_energy_resolution(self, ax=None):
        if self.get_loaded():
            self.ax_ene_res = ctaplot.plot_energy_resolution(
                self.gamma_data.mc_energy,
                self.gamma_data.reco_energy,
                bias_correction=self.bias_correction,
                ax=ax,
                label=self.name,
                color=self.color)
            if self.reco_gamma_data is not None and self.classif_resolution:
                self.ax_ene_res = ctaplot.plot_energy_resolution(
                    self.reco_gamma_data.mc_energy,
                    self.reco_gamma_data.reco_energy,
                    bias_correction=self.bias_correction,
                    ax=ax,
                    label=self.name + '_reco',
                    color=self.color,
                    **post_classification_opt)

            self.set_plotted(True)
Exemplo n.º 6
0
    def plot_metrics(self,
                     max_events: int = None,
                     min_valid_observations=2,
                     energy_regressor: EnergyModel = None,
                     plot_charges: bool = False,
                     save_to: str = None,
                     save_hillas: str = None,
                     save_plots: str = None):
        import ctaplot

        reco = self.reconstruct_all(
            max_events,
            min_valid_observations=min_valid_observations,
            energy_regressor=energy_regressor)

        preds = list(reco.values())

        if save_plots is not None:
            reco_alt = np.array(
                [pred['pred_alt'] / (1 * u.rad) for pred in preds])
            reco_az = np.array(
                [pred['pred_az'] / (1 * u.rad) for pred in preds])

            alt = np.array([pred['alt'] / (1 * u.rad) for pred in preds])
            az = np.array([pred['az'] / (1 * u.rad) for pred in preds])
            if energy_regressor is not None:
                energy = np.array([pred['energy'] for pred in preds])
            mc_energy = np.array([pred['mc_energy'] for pred in preds])

            _, (ax1, ax2) = plt.subplots(1, 2)
            ctaplot.plot_angular_resolution_per_energy(reco_alt,
                                                       reco_az,
                                                       alt,
                                                       az,
                                                       mc_energy,
                                                       ax=ax1)
            if energy_regressor is not None:
                ctaplot.plot_energy_resolution(mc_energy, energy, ax=ax2)

            plt.savefig(save_plots)
Exemplo n.º 7
0
        
        text = f"{len(hipecta_d[p]['events'])} events in hipecta file and {len(hipe)} good events\n"
        text += f"common filters:\n{common_filters}\n"
        text += f"{len(hipe)} events in hipecta after common filters\n"
        text += f"{len(lst)} events in lstchain after common filters\n"
        text += f"{len(hipe_sel)} events in hipecta after specific filters\n"
        text += f"{len(lst_sel)} events in lstchain after specific filters\n"
        
        firstPage = plt.figure(figsize=(11.69,8.27))
        firstPage.clf()
        firstPage.text(0.2,0.5, text, transform=firstPage.transFigure, size=11, ha="center")
        pp.savefig()
        plt.close()
        
        plt.figure(figsize=(15,10))
        ctaplot.plot_energy_resolution(hipe_sel.mc_energy, hipe_sel.reco_energy, label='hipecta')
        ctaplot.plot_energy_resolution(lst_sel.mc_energy, lst_sel.reco_energy, label='lstchain')
        plt.legend(fontsize=15)
        plt.title('energy resolution')
        plt.grid()
        plt.ylim(0, 1)
        plt.tight_layout()
        pp.savefig()

        plt.figure(figsize=(15,10))
        ctaplot.plot_energy_bias(hipe_sel.mc_energy, hipe_sel.reco_energy, label='hipecta')
        ctaplot.plot_energy_bias(lst_sel.mc_energy, lst_sel.reco_energy, label='lstchain')
        plt.legend(fontsize=15)
        plt.title('energy bias')
        plt.grid()
        plt.ylim(-1, 1)
def main():
    ntelescopes_gamma = 4
    ntelescopes_protons = 4
    n_bins_energy = 20  #  Number of energy bins
    n_bins_gammaness = 11  #  Number of gammaness bins
    n_bins_theta2 = 10  #  Number of theta2 bins
    obstime = 50 * 3600 * u.s
    noff = 5

    energy, best_sens, result, units, gcut, tcut = find_best_cuts_sensitivity(
        args.dl1file_gammas, args.dl1file_protons, args.dl2_file_g_cuts,
        args.dl2_file_p_cuts, ntelescopes_gamma, ntelescopes_protons,
        n_bins_energy, n_bins_gammaness, n_bins_theta2, noff, obstime)

    # For testing using fixed cuts
    # gcut = np.ones(eb) * 0.8
    # tcut = np.ones(eb) * 0.01

    energy, best_sens, result, units, dl2 = sensitivity(
        args.dl1file_gammas, args.dl1file_protons, args.dl2_file_g_sens,
        args.dl2_file_p_sens, 1, 1, 20, gcut, tcut * (u.deg**2), noff, obstime)

    dl2.to_hdf('test_sens.h5', key='data')
    result.to_hdf('test_sens.h5', key='results')

    tab = Table.from_pandas(result)

    for i, key in enumerate(tab.columns.keys()):
        tab[key].unit = units[i]
        if key == 'sensitivity':
            continue
        tab[key].format = '8f'

    egeom = np.sqrt(energy[1:] * energy[:-1])

    plt.plot(egeom[:-1], tab['hadron_rate'], label='Hadron rate', marker='o')
    plt.plot(egeom[:-1], tab['gamma_rate'], label='Gamma rate', marker='o')
    plt.legend()
    plt.xscale('log')
    plt.xlabel('Energy (GeV)')
    plt.ylabel('events / min')
    plt.show()

    gammas_mc = dl2[dl2.mc_type == 0]
    protons_mc = dl2[dl2.mc_type == 101]

    sns.distplot(gammas_mc.gammaness, label='gammas')
    sns.distplot(protons_mc.gammaness, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    sns.distplot(gammas_mc.mc_energy, label='gammas')
    sns.distplot(protons_mc.mc_energy, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()

    sns.distplot(gammas_mc.reco_energy.apply(np.log10), label='gammas')
    sns.distplot(protons_mc.reco_energy.apply(np.log10), label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    ctaplot.plot_theta2(gammas_mc.reco_alt,
                        gammas_mc.reco_az,
                        gammas_mc.mc_alt,
                        gammas_mc.mc_az,
                        range=(0, 1),
                        bins=100)
    plt.show()
    plt.figure(figsize=(12, 8))
    ctaplot.plot_angular_res_per_energy(
        gammas_mc.reco_alt,
        gammas_mc.reco_az,
        gammas_mc.mc_alt,
        gammas_mc.mc_az,
        10**(gammas_mc.reco_energy - 3),
    )

    ctaplot.plot_angular_res_cta_requirements('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.show()

    plt.figure(figsize=(12, 8))
    ctaplot.plot_energy_resolution(gammas_mc.mc_energy, gammas_mc.reco_energy)
    ctaplot.plot_energy_resolution_cta_requirements('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.show()

    ctaplot.plot_energy_resolution(gammas_mc.mc_energy, gammas_mc.reco_energy)
    ctaplot.plot_energy_bias(10**(gammas_mc.mc_energy - 3),
                             10**(gammas_mc.reco_energy - 3))
    plt.show()

    gamma_ps_simu_info = read_simu_info_merged_hdf5(args.dl1file_gammas)
    emin = gamma_ps_simu_info.energy_range_min.value
    emax = gamma_ps_simu_info.energy_range_max.value
    total_number_of_events = gamma_ps_simu_info.num_showers * gamma_ps_simu_info.shower_reuse
    spectral_index = gamma_ps_simu_info.spectral_index
    area = (gamma_ps_simu_info.max_scatter_range.value -
            gamma_ps_simu_info.min_scatter_range.value)**2 * np.pi
    ctaplot.plot_effective_area_per_energy_power_law(
        emin,
        emax,
        total_number_of_events,
        spectral_index,
        10**(gammas_mc.reco_energy - 3)[gammas_mc.tel_id == 1],
        area,
        label='selected gammas',
        linestyle='--')

    ctaplot.plot_effective_area_cta_requirements('north', color='black')
    plt.legend()
    plt.tight_layout()
    plt.show()
Exemplo n.º 9
0
plt.figure(figsize=(12, 8))
ctaplot.plot_angular_res_per_energy(
    gammas_mc.reco_alt,
    gammas_mc.reco_az,
    gammas_mc.mc_alt,
    gammas_mc.mc_az,
    10**(gammas_mc.reco_energy - 3),
)

ctaplot.plot_angular_res_cta_requirements('north', color='black')

plt.legend()
plt.tight_layout()
plt.show()
plt.figure(figsize=(12, 8))
ctaplot.plot_energy_resolution(10**(gammas_mc.mc_energy - 3),
                               10**(gammas_mc.reco_energy - 3))

ctaplot.plot_energy_resolution_cta_requirements('north', color='black')

plt.legend()
plt.tight_layout()
plt.show()

ctaplot.plot_energy_bias(10**(gammas_mc.mc_energy - 3),
                         10**(gammas_mc.reco_energy - 3))
plt.show()

gamma_ps_simu_info = read_simu_info_merged_hdf5(args.dl1file_gammas)
emin = gamma_ps_simu_info.energy_range_min.value
emax = gamma_ps_simu_info.energy_range_max.value
total_number_of_events = gamma_ps_simu_info.num_showers * gamma_ps_simu_info.shower_reuse
Exemplo n.º 10
0
def main():

    nfiles_gammas = 0.5  # 100*0.5 #Pointlike gammas
    nfiles_protons = 0.5  # 5000*0.8*0.5

    eb = 20  # Number of energy bins
    gb = 11  # Number of gammaness bins
    tb = 10  # Number of theta2 bins
    obstime = 50 * 3600 * u.s
    noff = 5

    E, best_sens, result, units, gcut, tcut = sensitivity.find_best_cuts_sens(
        args.dl1file_gammas, args.dl1file_protons, args.dl2_file_g_cuts,
        args.dl2_file_p_cuts, nfiles_gammas, nfiles_protons, eb, gb, tb, noff,
        obstime)
    E, best_sens, result, units, dl2 = sensitivity.sens(
        args.dl1file_gammas, args.dl1file_protons, args.dl2_file_g_sens,
        args.dl2_file_p_sens, nfiles_gammas, nfiles_protons, eb, gcut,
        tcut * (u.deg**2), noff, obstime)
    # plt.show()
    plot_utils.sens_plot(eb, E, best_sens)
    plt.show()

    tab = Table.from_pandas(result)

    for i, key in enumerate(tab.columns.keys()):
        tab[key].unit = units[i]
        if key == 'sensitivity':
            continue
        tab[key].format = '8f'

    print(tab)
    emed = np.sqrt(E[1:] * E[:-1])

    plt.plot(emed[:-1], tab['hadron_rate'], label='Hadron rate', marker='o')
    plt.plot(emed[:-1], tab['gamma_rate'], label='Gamma rate', marker='o')
    plt.legend()
    plt.xscale('log')
    plt.xlabel('Energy (GeV)')
    plt.ylabel('events / min')
    plt.show()

    gammas_mc = dl2[dl2.mc_type == 0]
    protons_mc = dl2[dl2.mc_type == 101]
    good_gammas = dl2

    sns.distplot(gammas_mc.gammaness, label='gammas')
    sns.distplot(protons_mc.gammaness, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    sns.distplot(gammas_mc.mc_energy, label='gammas')
    sns.distplot(protons_mc.mc_energy, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    sns.distplot(gammas_mc.log_reco_energy, label='gammas')
    sns.distplot(protons_mc.log_reco_energy, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    ctaplot.plot_theta2(gammas_mc.reco_alt,
                        gammas_mc.reco_az,
                        gammas_mc.mc_alt,
                        gammas_mc.mc_az,
                        range=(0, 1),
                        bins=100)
    plt.show()
    plt.figure(figsize=(12, 8))
    ctaplot.plot_angular_res_per_energy(
        gammas_mc.reco_alt,
        gammas_mc.reco_az,
        gammas_mc.mc_alt,
        gammas_mc.mc_az,
        gammas_mc.reco_energy,
    )

    ctaplot.plot_angular_res_cta_requirements('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.figure(figsize=(12, 8))
    ctaplot.plot_energy_resolution(10**(gammas_mc.mc_energy - 3),
                                   10**(gammas_mc.reco_energy - 3))

    ctaplot.plot_energy_resolution_cta_requirements('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.show()

    ctaplot.plot_energy_bias(10**(gammas_mc.mc_energy - 3),
                             10**(gammas_mc.reco_energy - 3))
    plt.show()

    gamma_ps_simu_info = read_simu_info_merged_hdf5(args.dl1file_gammas)
    emin = gamma_ps_simu_info.energy_range_min.value
    emax = gamma_ps_simu_info.energy_range_max.value
    total_number_of_events = gamma_ps_simu_info.num_showers * gamma_ps_simu_info.shower_reuse
    spectral_index = gamma_ps_simu_info.spectral_index
    area = (gamma_ps_simu_info.max_scatter_range.value -
            gamma_ps_simu_info.min_scatter_range.value)**2 * np.pi
    ctaplot.plot_effective_area_per_energy_power_law(
        emin,
        emax,
        total_number_of_events,
        spectral_index,
        gammas_mc.reco_energy[gammas_mc.tel_id == 1],
        area,
        label='selected gammas',
        linestyle='--',
    )

    ctaplot.plot_effective_area_cta_requirements('north', color='black')
    plt.legend()
    plt.tight_layout()
    plt.show()
Exemplo n.º 11
0
def main():
    ntelescopes_gamma = 1
    ntelescopes_protons = 1
    n_bins_energy = 20  #  Number of energy bins
    obstime = 50 * 3600 * u.s
    noff = 5
    geff_gammaness = 0.8  #Gamma efficincy of gammaness cut
    geff_theta2 = 0.68
    #Gamma efficiency of theta2 cut

    # Calculate the sensitivity
    '''
    energy,sensitivity,result,events, gcut, tcut = sensitivity_gamma_efficiency(args.dl2_file_g,
                                                                                         args.dl2_file_p,
                                                                                         ntelescopes_gamma,
                                                                                         ntelescopes_protons,
                                                                                         n_bins_energy,
                                                                                         geff_gammaness,
                                                                                         geff_theta2,
                                                                                         noff,
                                                                                         obstime)


    '''

    mc_energy, mc_sensitivity, mc_result, mc_events, gcut, tcut = sensitivity_gamma_efficiency_real_protons(
        args.dl2_file_g, args.dl2_file_p, ntelescopes_gamma, n_bins_energy,
        geff_gammaness, geff_theta2, noff, obstime)

    # Saves the results
    #   mc_events.to_hdf(args.output_path+'/mc_sensitivity.h5', key='data', mode='w')
    mc_result.to_hdf(args.output_path + '/mc_sensitivity.h5', key='results')

    print("\nOptimal gammaness cuts:", gcut)
    print("Optimal theta2 cuts: {} \n".format(tcut))

    energy, sensitivity, result, events, gcut, tcut = sensitivity_gamma_efficiency_real_data(
        args.dl2_file_on, args.dl2_file_p, gcut, tcut, n_bins_energy,
        mc_energy, geff_gammaness, geff_theta2, noff, obstime)
    print("\nOptimal gammaness cuts:", gcut)
    print("Optimal theta2 cuts: {} \n".format(tcut))

    #events[events.mc_type==0].alt_tel = events[events.mc_type==0].mc_alt
    #events[events.mc_type==0].az_tel = events[events.mc_type==0].mc_az

    if not os.path.exists(args.output_path):
        os.makedirs(args.output_path)

    # Saves the results


#    events.to_hdf(args.output_path+'/sensitivity.h5', key='data', mode='w')
    result.to_hdf(args.output_path + '/sensitivity.h5', key='results')

    # Plots

    #Sensitivity
    ax = plt.axes()
    plot_utils.format_axes_sensitivity(ax)
    plot_utils.plot_MAGIC_sensitivity(ax, color='C0')
    plot_utils.plot_Crab_SED(ax, 100, 50, 5e4,
                             label="100% Crab")  #Energy in GeV
    plot_utils.plot_Crab_SED(ax, 10, 50, 5e4, linestyle='--',
                             label="10% Crab")  #Energy in GeV
    plot_utils.plot_Crab_SED(ax, 1, 50, 5e4, linestyle=':',
                             label="1% Crab")  #Energy in GeV
    plot_utils.plot_sensitivity(energy,
                                sensitivity,
                                ax,
                                color='orange',
                                label="Sensitivity real data")
    plot_utils.plot_sensitivity(energy,
                                mc_sensitivity,
                                ax,
                                color='green',
                                label="Sensitivity MC gammas")
    plt.legend(prop={'size': 12})
    plt.savefig(args.output_path + "/sensitivity.png")
    plt.show()

    #Rates

    egeom = np.sqrt(energy[1:] * energy[:-1])
    plt.plot(egeom, result['proton_rate'], label='Proton rate', marker='o')
    plt.plot(egeom, result['gamma_rate'], label='Gamma rate', marker='o')
    plt.legend()
    plt.grid()
    plt.xscale('log')
    plt.yscale('log')
    plt.xlabel('Energy (TeV)')
    plt.ylabel('events / min')
    plt.savefig(args.output_path + "/rates.png")
    plt.show()

    #Gammaness
    gammas_mc = pd.read_hdf(args.dl2_file_g, key=dl2_params_lstcam_key)
    protons_mc = pd.read_hdf(args.dl2_file_p, key=dl2_params_lstcam_key)
    sns.distplot(gammas_mc.gammaness, label='gammas')
    sns.distplot(protons_mc.gammaness, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.savefig(args.output_path + "/distplot_gammaness.png")
    plt.show()
    '''
    #True Energy
    sns.distplot(gammas_mc.mc_energy, label='gammas');
    sns.distplot(protons_mc.mc_energy, label='protons');
    plt.legend()
    plt.tight_layout()
    plt.savefig(args.output_path+"/distplot_mc_energy.png")
    plt.show()

    #Reconstructed Energy
    sns.distplot(gammas_mc.reco_energy.apply(np.log10), label='gammas')
    sns.distplot(protons_mc.reco_energy.apply(np.log10), label='protons')
    plt.legend()
    plt.tight_layout()
    plt.savefig(args.output_path+"/distplot_energy_apply.png")
    plt.show()
    '''

    #Theta2
    ctaplot.plot_theta2(events.reco_alt,
                        events.reco_az,
                        events.alt_tel,
                        events.az_tel,
                        range=(0, 1),
                        bins=100)
    plt.savefig(args.output_path + "/theta2.png")
    plt.show()

    #Angular resolution
    ctaplot.plot_angular_resolution_per_energy(events.reco_alt, events.reco_az,
                                               events.alt_tel, events.az_tel,
                                               events.reco_energy)
    ctaplot.plot_angular_resolution_cta_requirement('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.savefig(args.output_path + "/angular_resolution.png")
    plt.show()

    #Energy resolution

    ctaplot.plot_energy_resolution(events[events.mc_type == 0].mc_energy,
                                   events[events.mc_type == 0].reco_energy)
    ctaplot.plot_energy_resolution_cta_requirement('north', color='black')
    plt.legend()
    plt.tight_layout()
    plt.savefig(args.output_path + "/effective_area.png")
    plt.show()

    #Energy bias

    ctaplot.plot_energy_bias(events[events.mc_type == 0].mc_energy,
                             events[events.mc_type == 0].reco_energy)
    plt.savefig(args.output_path + "/energy_bias.png")
    plt.show()

    #Effective Area

    gamma_ps_simu_info = read_simu_info_merged_hdf5(args.dl2_file_g)
    emin = gamma_ps_simu_info.energy_range_min.value
    emax = gamma_ps_simu_info.energy_range_max.value
    total_number_of_events = gamma_ps_simu_info.num_showers * gamma_ps_simu_info.shower_reuse * ntelescopes_gamma
    spectral_index = gamma_ps_simu_info.spectral_index
    area = (gamma_ps_simu_info.max_scatter_range.value -
            gamma_ps_simu_info.min_scatter_range.value)**2 * np.pi
    ctaplot.plot_effective_area_per_energy_power_law(emin,
                                                     emax,
                                                     total_number_of_events,
                                                     spectral_index,
                                                     events.reco_energy,
                                                     area,
                                                     label='selected gammas',
                                                     linestyle='--')

    ctaplot.plot_effective_area_cta_requirement('north', color='black')
    plt.ylim([2 * 10**3, 10**6])
    plt.legend()
    plt.tight_layout()
    plt.savefig(args.output_path + "/effective_area.png")
    plt.show()
Exemplo n.º 12
0
                                                    ax=ax,
                                                    label='selected gammas')
    ax.legend()
    ax.set_ylim(0, 0.5)
    ax.grid(which='both')
    fig.savefig(os.path.join(args.outdir, 'angular_resolution.png'),
                fmt='png',
                dpi=200)

    fig, ax = plt.subplots(figsize=(10, 7))
    ax = ctaplot.plot_energy_resolution_cta_performance('north',
                                                        color='black',
                                                        ax=ax)
    ax = ctaplot.plot_energy_resolution(
        gamma.mc_energy,
        gamma.reco_energy,
        label='all gammas',
        ax=ax,
    )
    ax = ctaplot.plot_energy_resolution(
        selected_gamma.mc_energy,
        selected_gamma.reco_energy,
        label='selected gammas',
        ax=ax,
    )
    ax.legend()
    ax.set_ylim(0, 0.5)
    ax.grid(which='both')
    fig.savefig(os.path.join(args.outdir, 'energy_resolution.png'),
                fmt='png',
                dpi=200)
def main():
    ntelescopes_gamma = 4
    ntelescopes_protons = 1
    n_bins_energy = 20  #  Number of energy bins
    n_bins_gammaness = 10  #  Number of gammaness bins
    n_bins_theta2 = 10  #  Number of theta2 bins
    obstime = 50 * 3600 * u.s
    noff = 5

    # Finds the best cuts for the computation of the sensitivity
    '''energy, best_sens, result, units, gcut, tcut = find_best_cuts_sensitivity(args.dl1file_gammas,
                                                                              args.dl1file_protons,
                                                                              args.dl2_file_g_sens,
                                                                              args.dl2_file_p_sens,
                                                                              ntelescopes_gamma, ntelescopes_protons,
                                                                              n_bins_energy, n_bins_gammaness,
                                                                              n_bins_theta2, noff,
                                                                              obstime)
    '''
    #For testing using fixed cuts
    gcut = np.ones(n_bins_energy) * 0.8
    tcut = np.ones(n_bins_energy) * 0.01

    print("\nApplying optimal gammaness cuts:", gcut)
    print("Applying optimal theta2 cuts: {} \n".format(tcut))

    # Computes the sensitivity
    energy, best_sens, result, units, dl2 = sensitivity(
        args.dl1file_gammas, args.dl1file_protons,
        args.dl2_file_g_cuts, args.dl2_file_p_cuts, 1, 1, n_bins_energy, gcut,
        tcut * (u.deg**2), noff, obstime)

    egeom = np.sqrt(energy[1:] * energy[:-1])
    dFdE, par = crab_hegra(egeom)
    sensitivity_flux = best_sens / 100 * (dFdE * egeom * egeom).to(
        u.erg / (u.cm**2 * u.s))

    # Saves the results
    dl2.to_hdf('test_sens.h5', key='data')
    result.to_hdf('test_sens.h5', key='results')

    tab = Table.from_pandas(result)

    for i, key in enumerate(tab.columns.keys()):
        tab[key].unit = units[i]
        if key == 'sensitivity':
            continue
        tab[key].format = '8f'

    # Plots

    plt.figure(figsize=(12, 8))
    plt.plot(egeom[:-1], tab['hadron_rate'], label='Hadron rate', marker='o')
    plt.plot(egeom[:-1], tab['gamma_rate'], label='Gamma rate', marker='o')
    plt.legend()
    plt.xscale('log')
    plt.xlabel('Energy (TeV)')
    plt.ylabel('events / min')
    plt.show()
    plt.savefig("rates.png")

    plt.figure(figsize=(12, 8))
    gammas_mc = dl2[dl2.mc_type == 0]
    protons_mc = dl2[dl2.mc_type == 101]
    sns.distplot(gammas_mc.gammaness, label='gammas')
    sns.distplot(protons_mc.gammaness, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.savefig("distplot_gammaness.png")

    plt.figure(figsize=(12, 8))
    sns.distplot(gammas_mc.mc_energy, label='gammas')
    sns.distplot(protons_mc.mc_energy, label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.savefig("distplot_mc_energy.png")

    plt.figure(figsize=(12, 8))
    sns.distplot(gammas_mc.reco_energy.apply(np.log10), label='gammas')
    sns.distplot(protons_mc.reco_energy.apply(np.log10), label='protons')
    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.savefig("distplot_energy_apply.png")

    plt.figure(figsize=(12, 8))
    ctaplot.plot_theta2(gammas_mc.reco_alt,
                        gammas_mc.reco_az,
                        gammas_mc.mc_alt,
                        gammas_mc.mc_az,
                        range=(0, 1),
                        bins=100)
    plt.show()
    plt.savefig("theta2.png")

    plt.figure(figsize=(12, 8))
    ctaplot.plot_angular_resolution_per_energy(gammas_mc.reco_alt,
                                               gammas_mc.reco_az,
                                               gammas_mc.mc_alt,
                                               gammas_mc.mc_az,
                                               gammas_mc.reco_energy)
    ctaplot.plot_angular_resolution_cta_requirement('north', color='black')

    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.savefig("angular_resolution.png")

    plt.figure(figsize=(12, 8))
    ctaplot.plot_energy_resolution(gammas_mc.mc_energy, gammas_mc.reco_energy)
    ctaplot.plot_energy_resolution_cta_requirement('north', color='black')
    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.savefig("effective_area.png")

    plt.figure(figsize=(12, 8))
    ctaplot.plot_energy_bias(gammas_mc.mc_energy, gammas_mc.reco_energy)
    plt.show()
    plt.savefig("energy_bias.png")

    plt.figure(figsize=(12, 8))
    gamma_ps_simu_info = read_simu_info_merged_hdf5(args.dl1file_gammas)
    emin = gamma_ps_simu_info.energy_range_min.value
    emax = gamma_ps_simu_info.energy_range_max.value
    total_number_of_events = gamma_ps_simu_info.num_showers * gamma_ps_simu_info.shower_reuse
    spectral_index = gamma_ps_simu_info.spectral_index
    area = (gamma_ps_simu_info.max_scatter_range.value -
            gamma_ps_simu_info.min_scatter_range.value)**2 * np.pi
    ctaplot.plot_effective_area_per_energy_power_law(
        emin,
        emax,
        total_number_of_events,
        spectral_index,
        gammas_mc.reco_energy[gammas_mc.tel_id == 1],
        area,
        label='selected gammas',
        linestyle='--')

    ctaplot.plot_effective_area_cta_requirement('north', color='black')
    plt.ylim([2 * 10**3, 10**6])
    plt.legend()
    plt.tight_layout()
    plt.show()
    plt.savefig("effective_area.png")

    plt.figure(figsize=(12, 8))
    plt.plot(energy[0:len(sensitivity_flux)],
             sensitivity_flux,
             '-',
             color='red',
             markersize=0,
             label='LST mono')
    plt.xscale('log')
    plt.yscale('log')

    plt.ylabel('$\mathsf{E^2 F \; [erg \, cm^{-2} s^{-1}]}$', fontsize=16)
    plt.xlabel('E [TeV]')
    plt.xlim([10**-2, 100])
    plt.ylim([10**-14, 10**-9])
    plt.tight_layout()
    plt.savefig('sensitivity.png')

    plt.figure(figsize=(12, 8))
    ctaplot.plot_energy_resolution(gammas_mc.mc_energy,
                                   gammas_mc.reco_energy,
                                   percentile=68.27,
                                   confidence_level=0.95,
                                   bias_correction=False)
    ctaplot.plot_energy_resolution_cta_requirement('north', color='black')
    plt.xscale('log')
    plt.ylabel('\u0394 E/E 68\%')
    plt.xlabel('E [TeV]')
    plt.xlim([10**-2, 100])
    plt.ylim([0.08, 0.48])
    plt.tight_layout()

    plt.savefig('energy_resolution.png', dpi=100)