def test_create_histogram_table(): '''Test create histogram table''' from pyirf.binning import create_histogram_table events = QTable({ 'reco_energy': [1, 1, 10, 100, 100, 100] * u.TeV, }) bins = [0.5, 5, 50, 500] * u.TeV # test without weights hist = create_histogram_table(events, bins, key='reco_energy') assert np.all(hist['n'] == [2, 1, 3]) assert np.all(hist['n_weighted'] == [2, 1, 3]) # test with weights events['weight'] = [0.5, 2, 2.5, 0.5, 0.2, 0.3] hist = create_histogram_table(events, bins, key='reco_energy') assert np.all(hist['n'] == [2, 1, 3]) assert np.all(hist['n_weighted'] == [2.5, 2.5, 1.0]) # test with particle_types types = np.array(['gamma', 'electron', 'gamma', 'electron', 'gamma', 'proton']) events['particle_type'] = types hist = create_histogram_table(events, bins, key='reco_energy') assert np.all(hist['n'] == [2, 1, 3]) assert np.all(hist['n_weighted'] == [2.5, 2.5, 1.0]) assert np.all(hist['n_gamma'] == [1, 1, 1]) assert np.all(hist['n_electron'] == [1, 0, 1]) assert np.all(hist['n_proton'] == [0, 0, 1]) assert np.allclose(hist['n_gamma_weighted'], [0.5, 2.5, 0.2]) assert np.allclose(hist['n_electron_weighted'], [2, 0, 0.5]) assert np.allclose(hist['n_proton_weighted'], [0, 0, 0.3]) # test with empty table empty = events[:0] hist = create_histogram_table(empty, bins, key='reco_energy') zeros = np.zeros(3) assert np.all(hist['n'] == zeros) assert np.all(hist['n_weighted'] == zeros)
def main(): # Read arguments parser = argparse.ArgumentParser(description='Make performance files') parser.add_argument('--config_file', type=str, required=True, help='') mode_group = parser.add_mutually_exclusive_group() mode_group.add_argument('--wave', dest="mode", action='store_const', const="wave", default="tail", help="if set, use wavelet cleaning") mode_group.add_argument('--tail', dest="mode", action='store_const', const="tail", help="if set, use tail cleaning (default)") args = parser.parse_args() # Read configuration file cfg = load_config(args.config_file) # Create output directory if necessary outdir = os.path.join( cfg['general']['outdir'], 'performance_protopipe_{}_CTA{}_{}_Zd{}_{}_Time{:.2f}{}'.format( cfg['general']['prod'], cfg['general']['site'], cfg['general']['array'], cfg['general']['zenith'], cfg['general']['azimuth'], cfg['analysis']['obs_time']['value'], cfg['analysis']['obs_time']['unit']), ) indir = cfg['general']['indir'] template_input_file = cfg['general']['template_input_file'] T_OBS = cfg['analysis']['obs_time']['value'] * u.Unit( cfg['analysis']['obs_time']['unit']) # scaling between on and off region. # Make off region 5 times larger than on region for better # background statistics ALPHA = cfg['analysis']['alpha'] # Radius to use for calculating bg rate MAX_BG_RADIUS = cfg['analysis']['max_bg_radius'] * u.deg particles = { "gamma": { "file": os.path.join(indir, template_input_file.format(args.mode, "gamma")), "target_spectrum": CRAB_HEGRA, "run_header": cfg['particle_information']['gamma'] }, "proton": { "file": os.path.join(indir, template_input_file.format(args.mode, "proton")), "target_spectrum": IRFDOC_PROTON_SPECTRUM, "run_header": cfg['particle_information']['proton'] }, "electron": { "file": os.path.join(indir, template_input_file.format(args.mode, "electron")), "target_spectrum": IRFDOC_ELECTRON_SPECTRUM, "run_header": cfg['particle_information']['electron'] }, } logging.basicConfig(level=logging.INFO) logging.getLogger("pyirf").setLevel(logging.DEBUG) for particle_type, p in particles.items(): log.info(f"Simulated {particle_type.title()} Events:") p["events"], p["simulation_info"] = read_DL2_pyirf( p["file"], p["run_header"]) # Multiplicity cut p["events"] = p["events"][ p["events"]["multiplicity"] >= cfg['analysis'] ['cut_on_multiplicity']].copy() p["simulated_spectrum"] = PowerLaw.from_simulation( p["simulation_info"], T_OBS) # Weight events p["events"]["weight"] = calculate_event_weights( p["events"]["true_energy"], p["target_spectrum"], p["simulated_spectrum"]) for prefix in ('true', 'reco'): k = f"{prefix}_source_fov_offset" p["events"][k] = calculate_source_fov_offset(p["events"], prefix=prefix) # calculate theta / distance between reco and assuemd source positoin # we handle only ON observations here, so the assumed source pos # is the pointing position p["events"]["theta"] = calculate_theta( p["events"], assumed_source_az=p["events"]["pointing_az"], assumed_source_alt=p["events"]["pointing_alt"], ) log.info(p["simulation_info"]) log.info("") gammas = particles["gamma"]["events"] # background table composed of both electrons and protons background = table.vstack( [particles["proton"]["events"], particles["electron"]["events"]]) MAX_GH_CUT_EFFICIENCY = 0.8 GH_CUT_EFFICIENCY_STEP = 0.01 # gh cut used for first calculation of the binned theta cuts INITIAL_GH_CUT_EFFICENCY = 0.4 INITIAL_GH_CUT = np.quantile(gammas['gh_score'], (1 - INITIAL_GH_CUT_EFFICENCY)) log.info( f"Using fixed G/H cut of {INITIAL_GH_CUT} to calculate theta cuts") # event display uses much finer bins for the theta cut than # for the sensitivity theta_bins = add_overflow_bins( create_bins_per_decade( 10**(-1.9) * u.TeV, 10**2.3005 * u.TeV, 50, )) # theta cut is 68 percent containmente of the gammas # for now with a fixed global, unoptimized score cut mask_theta_cuts = gammas["gh_score"] >= INITIAL_GH_CUT theta_cuts = calculate_percentile_cut( gammas["theta"][mask_theta_cuts], gammas["reco_energy"][mask_theta_cuts], bins=theta_bins, min_value=0.05 * u.deg, fill_value=0.32 * u.deg, max_value=0.32 * u.deg, percentile=68, ) # same bins as event display uses sensitivity_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, bins_per_decade=5)) log.info("Optimizing G/H separation cut for best sensitivity") gh_cut_efficiencies = np.arange( GH_CUT_EFFICIENCY_STEP, MAX_GH_CUT_EFFICIENCY + GH_CUT_EFFICIENCY_STEP / 2, GH_CUT_EFFICIENCY_STEP) sensitivity_step_2, gh_cuts = optimize_gh_cut( gammas, background, reco_energy_bins=sensitivity_bins, gh_cut_efficiencies=gh_cut_efficiencies, op=operator.ge, theta_cuts=theta_cuts, alpha=ALPHA, background_radius=MAX_BG_RADIUS, ) # now that we have the optimized gh cuts, we recalculate the theta # cut as 68 percent containment on the events surviving these cuts. log.info('Recalculating theta cut for optimized GH Cuts') for tab in (gammas, background): tab["selected_gh"] = evaluate_binned_cut(tab["gh_score"], tab["reco_energy"], gh_cuts, operator.ge) theta_cuts_opt = calculate_percentile_cut( gammas[gammas['selected_gh']]["theta"], gammas[gammas['selected_gh']]["reco_energy"], theta_bins, percentile=68, fill_value=0.32 * u.deg, max_value=0.32 * u.deg, min_value=0.05 * u.deg, ) gammas["selected_theta"] = evaluate_binned_cut(gammas["theta"], gammas["reco_energy"], theta_cuts_opt, operator.le) gammas["selected"] = gammas["selected_theta"] & gammas["selected_gh"] # calculate sensitivity signal_hist = create_histogram_table(gammas[gammas["selected"]], bins=sensitivity_bins) background_hist = estimate_background( background[background["selected_gh"]], reco_energy_bins=sensitivity_bins, theta_cuts=theta_cuts_opt, alpha=ALPHA, background_radius=MAX_BG_RADIUS, ) sensitivity = calculate_sensitivity(signal_hist, background_hist, alpha=ALPHA) # scale relative sensitivity by Crab flux to get the flux sensitivity spectrum = particles['gamma']['target_spectrum'] for s in (sensitivity_step_2, sensitivity): s["flux_sensitivity"] = (s["relative_sensitivity"] * spectrum(s['reco_energy_center'])) log.info('Calculating IRFs') hdus = [ fits.PrimaryHDU(), fits.BinTableHDU(sensitivity, name="SENSITIVITY"), fits.BinTableHDU(sensitivity_step_2, name="SENSITIVITY_STEP_2"), fits.BinTableHDU(theta_cuts, name="THETA_CUTS"), fits.BinTableHDU(theta_cuts_opt, name="THETA_CUTS_OPT"), fits.BinTableHDU(gh_cuts, name="GH_CUTS"), ] masks = { "": gammas["selected"], "_NO_CUTS": slice(None), "_ONLY_GH": gammas["selected_gh"], "_ONLY_THETA": gammas["selected_theta"], } # binnings for the irfs true_energy_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, 10)) reco_energy_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, 5)) fov_offset_bins = [0, 0.5] * u.deg source_offset_bins = np.arange(0, 1 + 1e-4, 1e-3) * u.deg energy_migration_bins = np.geomspace(0.2, 5, 200) for label, mask in masks.items(): effective_area = effective_area_per_energy( gammas[mask], particles["gamma"]["simulation_info"], true_energy_bins=true_energy_bins, ) hdus.append( create_aeff2d_hdu( effective_area[..., np.newaxis], # +1 dimension for FOV offset true_energy_bins, fov_offset_bins, extname="EFFECTIVE_AREA" + label, )) edisp = energy_dispersion( gammas[mask], true_energy_bins=true_energy_bins, fov_offset_bins=fov_offset_bins, migration_bins=energy_migration_bins, ) hdus.append( create_energy_dispersion_hdu( edisp, true_energy_bins=true_energy_bins, migration_bins=energy_migration_bins, fov_offset_bins=fov_offset_bins, extname="ENERGY_DISPERSION" + label, )) # Here we use reconstructed energy instead of true energy for the sake of # current pipelines comparisons bias_resolution = energy_bias_resolution(gammas[gammas["selected"]], reco_energy_bins, energy_type="reco") # Here we use reconstructed energy instead of true energy for the sake of # current pipelines comparisons ang_res = angular_resolution(gammas[gammas["selected_gh"]], reco_energy_bins, energy_type="reco") psf = psf_table( gammas[gammas["selected_gh"]], true_energy_bins, fov_offset_bins=fov_offset_bins, source_offset_bins=source_offset_bins, ) background_rate = background_2d( background[background['selected_gh']], reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg, t_obs=T_OBS, ) hdus.append( create_background_2d_hdu( background_rate, reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg, )) hdus.append( create_psf_table_hdu( psf, true_energy_bins, source_offset_bins, fov_offset_bins, )) hdus.append( create_rad_max_hdu(theta_cuts_opt["cut"][:, np.newaxis], theta_bins, fov_offset_bins)) hdus.append(fits.BinTableHDU(ang_res, name="ANGULAR_RESOLUTION")) hdus.append( fits.BinTableHDU(bias_resolution, name="ENERGY_BIAS_RESOLUTION")) log.info('Writing outputfile') fits.HDUList(hdus).writeto(outdir + '.fits.gz', overwrite=True)
def main(): log = logging.getLogger("lstchain MC DL2 to IRF - sensitivity curves") parser = argparse.ArgumentParser(description="MC DL2 to IRF") # Required arguments parser.add_argument( "--gamma-dl2", "-g", type=str, dest="gamma_file", help="Path to the dl2 gamma file" ) parser.add_argument( "--proton-dl2", "-p", type=str, dest="proton_file", help="Path to the dl2 proton file", ) parser.add_argument( "--electron-dl2", "-e", type=str, dest="electron_file", help="Path to the dl2 electron file", ) parser.add_argument( "--outfile", "-o", action="store", type=str, dest="outfile", help="Path where to save IRF FITS file", default="sensitivity.fits.gz", ) parser.add_argument( "--source_alt", action="store", type=float, dest="source_alt", help="Source altitude (optional). If not provided, it will be guessed from the gammas true altitude", default=None ) parser.add_argument( "--source_az", action="store", type=float, dest="source_az", help="Source azimuth (optional). If not provided, it will be guessed from the gammas true altitude", default=None ) # Optional arguments # parser.add_argument('--config', '-c', action='store', type=Path, # dest='config_file', # help='Path to a configuration file. If none is given, a standard configuration is applied', # default=None # ) args = parser.parse_args() logging.basicConfig(level=logging.INFO) logging.getLogger("pyirf").setLevel(logging.DEBUG) particles = { "gamma": {"file": args.gamma_file, "target_spectrum": CRAB_HEGRA}, "proton": {"file": args.proton_file, "target_spectrum": IRFDOC_PROTON_SPECTRUM}, "electron": { "file": args.electron_file, "target_spectrum": IRFDOC_ELECTRON_SPECTRUM, }, } for particle_type, p in particles.items(): log.info("Simulated Events: {}".format(particle_type.title())) p["events"], p["simulation_info"] = read_mc_dl2_to_QTable(p["file"]) # p['events'] = filter_events(p['events'], filters) print("=====", particle_type, "=====") # p["events"]["particle_type"] = particle_type p["simulated_spectrum"] = PowerLaw.from_simulation(p["simulation_info"], T_OBS) p["events"]["weight"] = calculate_event_weights( p["events"]["true_energy"], p["target_spectrum"], p["simulated_spectrum"] ) for prefix in ("true", "reco"): k = f"{prefix}_source_fov_offset" p["events"][k] = calculate_source_fov_offset(p["events"], prefix=prefix) gammas = particles["gamma"]["events"] # background table composed of both electrons and protons background = table.vstack( [particles["proton"]["events"], particles["electron"]["events"]] ) if args.source_alt is None or args.source_az is None: source_alt, source_az = determine_source_position(gammas) else: source_alt, source_az = args.source_alt, args.source_az for particle_type, p in particles.items(): # calculate theta / distance between reco and assumed source position # we handle only ON observations here, so the assumed source pos is the pointing position p["events"]["theta"] = calculate_theta(p["events"], assumed_source_az=source_az, assumed_source_alt=source_alt) log.info(p["simulation_info"]) log.info("") INITIAL_GH_CUT = np.quantile(gammas["gh_score"], (1 - INITIAL_GH_CUT_EFFICENCY)) log.info("Using fixed G/H cut of {} to calculate theta cuts".format(INITIAL_GH_CUT)) # event display uses much finer bins for the theta cut than # for the sensitivity theta_bins = add_overflow_bins( create_bins_per_decade(MIN_ENERGY, MAX_ENERGY, N_BIN_PER_DECADE) ) # theta cut is 68 percent containment of the gammas # for now with a fixed global, unoptimized score cut mask_theta_cuts = gammas["gh_score"] >= INITIAL_GH_CUT theta_cuts = calculate_percentile_cut( gammas["theta"][mask_theta_cuts], gammas["reco_energy"][mask_theta_cuts], bins=theta_bins, min_value=MIN_THETA_CUT, fill_value=MAX_THETA_CUT, max_value=MAX_THETA_CUT, percentile=68, ) # same number of bins per decade than official CTA IRFs sensitivity_bins = add_overflow_bins( create_bins_per_decade(MIN_ENERGY, MAX_ENERGY, bins_per_decade=N_BIN_PER_DECADE) ) log.info("Optimizing G/H separation cut for best sensitivity") gh_cut_efficiencies = np.arange( GH_CUT_EFFICIENCY_STEP, MAX_GH_CUT_EFFICIENCY + GH_CUT_EFFICIENCY_STEP / 2, GH_CUT_EFFICIENCY_STEP, ) sensitivity_step_2, gh_cuts = optimize_gh_cut( gammas, background, reco_energy_bins=sensitivity_bins, gh_cut_efficiencies=gh_cut_efficiencies, op=operator.ge, theta_cuts=theta_cuts, alpha=ALPHA, background_radius=MAX_BG_RADIUS, ) # now that we have the optimized gh cuts, we recalculate the theta # cut as 68 percent containment on the events surviving these cuts. log.info("Recalculating theta cut for optimized GH Cuts") for tab in (gammas, background): tab["selected_gh"] = evaluate_binned_cut( tab["gh_score"], tab["reco_energy"], gh_cuts, operator.ge ) theta_cuts_opt = calculate_percentile_cut( gammas[gammas["selected_gh"]]["theta"], gammas[gammas["selected_gh"]]["reco_energy"], theta_bins, percentile=68, fill_value=MAX_THETA_CUT, max_value=MAX_THETA_CUT, min_value=MIN_THETA_CUT, ) gammas["selected_theta"] = evaluate_binned_cut( gammas["theta"], gammas["reco_energy"], theta_cuts_opt, operator.le ) gammas["selected"] = gammas["selected_theta"] & gammas["selected_gh"] # calculate sensitivity signal_hist = create_histogram_table( gammas[gammas["selected"]], bins=sensitivity_bins ) background_hist = estimate_background( background[background["selected_gh"]], reco_energy_bins=sensitivity_bins, theta_cuts=theta_cuts_opt, alpha=ALPHA, background_radius=MAX_BG_RADIUS, ) sensitivity = calculate_sensitivity(signal_hist, background_hist, alpha=ALPHA) # scale relative sensitivity by Crab flux to get the flux sensitivity spectrum = particles["gamma"]["target_spectrum"] for s in (sensitivity_step_2, sensitivity): s["flux_sensitivity"] = s["relative_sensitivity"] * spectrum( s["reco_energy_center"] ) log.info("Calculating IRFs") hdus = [ fits.PrimaryHDU(), fits.BinTableHDU(sensitivity, name="SENSITIVITY"), fits.BinTableHDU(sensitivity_step_2, name="SENSITIVITY_STEP_2"), fits.BinTableHDU(theta_cuts, name="THETA_CUTS"), fits.BinTableHDU(theta_cuts_opt, name="THETA_CUTS_OPT"), fits.BinTableHDU(gh_cuts, name="GH_CUTS"), ] masks = { "": gammas["selected"], "_NO_CUTS": slice(None), "_ONLY_GH": gammas["selected_gh"], "_ONLY_THETA": gammas["selected_theta"], } # binnings for the irfs true_energy_bins = add_overflow_bins( create_bins_per_decade(MIN_ENERGY, MAX_ENERGY, N_BIN_PER_DECADE) ) reco_energy_bins = add_overflow_bins( create_bins_per_decade(MIN_ENERGY, MAX_ENERGY, N_BIN_PER_DECADE) ) fov_offset_bins = [0, 0.6] * u.deg source_offset_bins = np.arange(0, 1 + 1e-4, 1e-3) * u.deg energy_migration_bins = np.geomspace(0.2, 5, 200) for label, mask in masks.items(): effective_area = effective_area_per_energy( gammas[mask], particles["gamma"]["simulation_info"], true_energy_bins=true_energy_bins, ) hdus.append( create_aeff2d_hdu( effective_area[..., np.newaxis], # add one dimension for FOV offset true_energy_bins, fov_offset_bins, extname="EFFECTIVE_AREA" + label, ) ) edisp = energy_dispersion( gammas[mask], true_energy_bins=true_energy_bins, fov_offset_bins=fov_offset_bins, migration_bins=energy_migration_bins, ) hdus.append( create_energy_dispersion_hdu( edisp, true_energy_bins=true_energy_bins, migration_bins=energy_migration_bins, fov_offset_bins=fov_offset_bins, extname="ENERGY_DISPERSION" + label, ) ) bias_resolution = energy_bias_resolution( gammas[gammas["selected"]], true_energy_bins, resolution_function=energy_resolution_absolute_68, ) ang_res = angular_resolution(gammas[gammas["selected_gh"]], true_energy_bins) psf = psf_table( gammas[gammas["selected_gh"]], true_energy_bins, fov_offset_bins=fov_offset_bins, source_offset_bins=source_offset_bins, ) background_rate = background_2d( background[background["selected_gh"]], reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg, t_obs=T_OBS, ) hdus.append( create_background_2d_hdu( background_rate, reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg ) ) hdus.append( create_psf_table_hdu(psf, true_energy_bins, source_offset_bins, fov_offset_bins) ) hdus.append( create_rad_max_hdu( theta_cuts_opt["cut"][:, np.newaxis], theta_bins, fov_offset_bins ) ) hdus.append(fits.BinTableHDU(ang_res, name="ANGULAR_RESOLUTION")) hdus.append(fits.BinTableHDU(bias_resolution, name="ENERGY_BIAS_RESOLUTION")) log.info("Writing output file") Path(args.outfile).parent.mkdir(exist_ok=True) fits.HDUList(hdus).writeto(args.outfile, overwrite=True)
def main(output, data, source, cuts_file, theta2_cut, threshold, n_offs, n_jobs): outdir = output.split('/')[0] src = SkyCoord.from_name(source) if n_jobs == -1: n_jobs = cpu_count() with Pool(n_jobs) as pool: results = np.array(pool.starmap(calculation.read_run_calculate_thetas, [(run, columns, threshold, src, n_offs) for run in data]), dtype=object) df_selected = pd.concat(results[:, 0], ignore_index=True) ontime = np.sum(results[:, 1]) theta = np.concatenate(results[:, 2]) df_selected5 = pd.concat(results[:, 3], ignore_index=True) theta_off = np.concatenate(results[:, 4]) # use pyirf cuts gh_cuts = table.QTable.read(cuts_file, hdu='GH_CUTS') theta_cuts_opt = table.QTable.read(cuts_file, hdu='THETA_CUTS_OPT') with Pool(n_jobs) as pool: results = np.array(pool.starmap(calculation.read_run_calculate_thetas, [(run, columns, gh_cuts, src, n_offs) for run in data]), dtype=object) df_pyirf = pd.concat(results[:, 0], ignore_index=True) theta_pyirf = np.concatenate(results[:, 2]) df_pyirf5 = pd.concat(results[:, 3], ignore_index=True) theta_off_pyirf = np.concatenate(results[:, 4]) n_on = np.count_nonzero( evaluate_binned_cut( theta_pyirf, df_pyirf.gamma_energy_prediction.to_numpy() * u.TeV, theta_cuts_opt, operator.le)) n_off = np.count_nonzero( evaluate_binned_cut( theta_off_pyirf, df_pyirf5.gamma_energy_prediction.to_numpy() * u.TeV, theta_cuts_opt, operator.le)) li_ma = li_ma_significance(n_on, n_off, 1 / n_offs) n_exc_mean = n_on - (1 / n_offs) * n_off n_exc_std = np.sqrt(n_on + (1 / n_offs)**2 * n_off) ############################################################################################################## # plots ############################################################################################################## figures = [] figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(theta.deg**2, theta_off.deg**2, 1 / n_offs, theta2_cut, threshold, source, ontime=ontime, ax=ax) ax.set_title('Theta calculated in ICRS using astropy') figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) plotting.theta2(theta_pyirf.deg**2, theta_off_pyirf.deg**2, 1 / n_offs, theta2_cut, r'\mathrm{energy-dependent}', source, ontime=ontime, ax=ax) ax.set_title(r'Energy-dependent $t_\gamma$ optimised using pyirf') # plot using pyirf theta cuts figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) ax.hist(theta_pyirf.deg**2, bins=100, range=[0, 1], histtype='step', color='r', label='ON') ax.hist(theta_off_pyirf.deg**2, bins=100, range=[0, 1], histtype='stepfilled', color='tab:blue', alpha=0.5, label='OFF', weights=np.full_like(theta_off_pyirf.deg**2, 1 / n_offs)) txt = rf'''Source: {source}, $t_\mathrm{{obs}} = {ontime.to_value(u.hour):.2f} \mathrm{{h}}$ $N_\mathrm{{on}} = {n_on},\, N_\mathrm{{off}} = {n_off},\, \alpha = {1/n_offs:.2f}$ $N_\mathrm{{exc}} = {n_exc_mean:.0f} \pm {n_exc_std:.0f},\, S_\mathrm{{Li&Ma}} = {li_ma:.2f}$ ''' ax.text(0.5, 0.95, txt, transform=ax.transAxes, va='top', ha='center') ax.set_xlabel(r'$\theta^2 \,\, / \,\, \mathrm{deg}^2$') ax.set_xlim(0, 1) ax.legend() ax.set_title( r'Energy-dependent $t_\gamma$ and $\theta_\mathrm{max}^2$ optimised using pyirf' ) ############################################################################################################## # sensitivity ############################################################################################################## sensitivity_bins = add_overflow_bins( create_bins_per_decade(10**-1.8 * u.TeV, 10**2.41 * u.TeV, bins_per_decade=5)) # gh_cuts and theta_cuts_opt in line 113f gammas = plotting.to_astropy_table( df_pyirf, # df_pyirf has pyirf gh cuts already applied column_map=COLUMN_MAP, unit_map=UNIT_MAP, theta=theta_pyirf, t_obs=ontime) background = plotting.to_astropy_table(df_pyirf5, column_map=COLUMN_MAP, unit_map=UNIT_MAP, theta=theta_off_pyirf, t_obs=ontime) gammas["selected_theta"] = evaluate_binned_cut(gammas["theta"], gammas["reco_energy"], theta_cuts_opt, operator.le) background["selected_theta"] = evaluate_binned_cut( background["theta"], background["reco_energy"], theta_cuts_opt, operator.le) # calculate sensitivity signal_hist = create_histogram_table(gammas[gammas["selected_theta"]], bins=sensitivity_bins) background_hist = estimate_background( background[background["selected_theta"]], reco_energy_bins=sensitivity_bins, theta_cuts=theta_cuts_opt, alpha=1 / n_offs, background_radius=MAX_BG_RADIUS, ) sensitivity = calculate_sensitivity(signal_hist, background_hist, alpha=1 / n_offs) # scale relative sensitivity by Crab flux to get the flux sensitivity spectrum = CRAB_HEGRA sensitivity["flux_sensitivity"] = ( sensitivity["relative_sensitivity"] * spectrum(sensitivity['reco_energy_center'])) ############################################################################################################## # sensitivity using unoptimised cuts ############################################################################################################## gammas_unop = plotting.to_astropy_table( df_selected, # df_selected has gammaness > threshold already applied column_map=COLUMN_MAP, unit_map=UNIT_MAP, theta=theta, t_obs=ontime) background_unop = plotting.to_astropy_table(df_selected5, column_map=COLUMN_MAP, unit_map=UNIT_MAP, theta=theta_off, t_obs=ontime) gammas_unop["selected_theta"] = gammas_unop["theta"].to_value( u.deg) <= np.sqrt(0.03) background_unop["selected_theta"] = background_unop["theta"].to_value( u.deg) <= np.sqrt(0.03) theta_cut_unop = theta_cuts_opt theta_cut_unop['cut'] = np.sqrt(0.03) * u.deg # calculate sensitivity signal_hist_unop = create_histogram_table( gammas_unop[gammas_unop["selected_theta"]], bins=sensitivity_bins) background_hist_unop = estimate_background( background_unop[background_unop["selected_theta"]], reco_energy_bins=sensitivity_bins, theta_cuts=theta_cut_unop, alpha=1 / n_offs, background_radius=MAX_BG_RADIUS, ) sensitivity_unop = calculate_sensitivity(signal_hist_unop, background_hist_unop, alpha=1 / n_offs) # scale relative sensitivity by Crab flux to get the flux sensitivity sensitivity_unop["flux_sensitivity"] = ( sensitivity_unop["relative_sensitivity"] * spectrum(sensitivity_unop['reco_energy_center'])) # write fits file and create plot hdus = [ fits.PrimaryHDU(), fits.BinTableHDU(sensitivity, name="SENSITIVITY"), fits.BinTableHDU(sensitivity_unop, name="SENSITIVITY_UNOP") ] fits.HDUList(hdus).writeto(f'{outdir}/sensitivity_{source}.fits.gz', overwrite=True) figures.append(plt.figure()) ax = figures[-1].add_subplot(1, 1, 1) for s, label in zip([sensitivity, sensitivity_unop], [ 'pyirf optimised cuts', rf'$\theta^2 < {theta2_cut}$ and gh_score$> {threshold}$' ]): plotting.plot_sensitivity(s, label=label, ax=ax) # plot Magic sensitivity for reference magic = table.QTable.read('notebooks/magic_sensitivity_2014.ecsv') plotting.plot_sensitivity(magic, label='MAGIC 2014', ax=ax, magic=True) ax.set_title( f'Minimal Flux Satisfying Requirements for 50 hours \n(based on {ontime.to_value(u.hour):.2f}h of {source} observations)' ) # save plots with PdfPages(output) as pdf: for fig in figures: fig.tight_layout() pdf.savefig(fig)
def main(): logging.basicConfig(level=logging.INFO) logging.getLogger("pyirf").setLevel(logging.DEBUG) for k, p in particles.items(): log.info(f"Simulated {k.title()} Events:") p["events"], p["simulation_info"] = read_eventdisplay_fits(p["file"]) p["simulated_spectrum"] = PowerLaw.from_simulation( p["simulation_info"], T_OBS) p["events"]["weight"] = calculate_event_weights( p["events"]["true_energy"], p["target_spectrum"], p["simulated_spectrum"]) p["events"]["source_fov_offset"] = calculate_source_fov_offset( p["events"]) # calculate theta / distance between reco and assuemd source positoin # we handle only ON observations here, so the assumed source pos # is the pointing position p["events"]["theta"] = calculate_theta( p["events"], assumed_source_az=p["events"]["pointing_az"], assumed_source_alt=p["events"]["pointing_alt"], ) log.info(p["simulation_info"]) log.info("") gammas = particles["gamma"]["events"] # background table composed of both electrons and protons background = table.vstack( [particles["proton"]["events"], particles["electron"]["events"]]) log.info( f"Using fixed G/H cut of {INITIAL_GH_CUT} to calculate theta cuts") # event display uses much finer bins for the theta cut than # for the sensitivity theta_bins = add_overflow_bins( create_bins_per_decade( 10**(-1.9) * u.TeV, 10**2.3005 * u.TeV, 50, )) # theta cut is 68 percent containmente of the gammas # for now with a fixed global, unoptimized score cut mask_theta_cuts = gammas["gh_score"] >= INITIAL_GH_CUT theta_cuts = calculate_percentile_cut( gammas["theta"][mask_theta_cuts], gammas["reco_energy"][mask_theta_cuts], bins=theta_bins, min_value=0.05 * u.deg, fill_value=np.nan * u.deg, percentile=68, ) # evaluate the theta cut gammas["selected_theta"] = evaluate_binned_cut(gammas["theta"], gammas["reco_energy"], theta_cuts, operator.le) # we make the background region larger by a factor of ALPHA, # so the radius by sqrt(ALPHA) to get better statistics for the background theta_cuts_bg = get_bg_cuts(theta_cuts, ALPHA) background["selected_theta"] = evaluate_binned_cut( background["theta"], background["reco_energy"], theta_cuts_bg, operator.le) # same bins as event display uses sensitivity_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, bins_per_decade=5)) log.info("Optimizing G/H separation cut for best sensitivity") sensitivity_step_2, gh_cuts = optimize_gh_cut( gammas[gammas["selected_theta"]], background[background["selected_theta"]], bins=sensitivity_bins, cut_values=np.arange(-1.0, 1.005, 0.05), op=operator.ge, alpha=ALPHA, ) # now that we have the optimized gh cuts, we recalculate the theta # cut as 68 percent containment on the events surviving these cuts. for tab in (gammas, background): tab["selected_gh"] = evaluate_binned_cut(tab["gh_score"], tab["reco_energy"], gh_cuts, operator.ge) theta_cuts_opt = calculate_percentile_cut( gammas["theta"], gammas["reco_energy"], theta_bins, fill_value=np.nan * u.deg, percentile=68, min_value=0.05 * u.deg, ) theta_cuts_opt_bg = get_bg_cuts(theta_cuts_opt, ALPHA) for tab, cuts in zip([gammas, background], [theta_cuts_opt, theta_cuts_opt_bg]): tab["selected_theta"] = evaluate_binned_cut(tab["theta"], tab["reco_energy"], cuts, operator.le) tab["selected"] = tab["selected_theta"] & tab["selected_gh"] signal_hist = create_histogram_table(gammas[gammas["selected"]], bins=sensitivity_bins) background_hist = create_histogram_table( background[background["selected"]], bins=sensitivity_bins) sensitivity = calculate_sensitivity(signal_hist, background_hist, alpha=ALPHA) # scale relative sensitivity by Crab flux to get the flux sensitivity for s in (sensitivity_step_2, sensitivity): s["flux_sensitivity"] = s["relative_sensitivity"] * CRAB_HEGRA( s["reco_energy_center"]) # write OGADF output file hdus = [ fits.PrimaryHDU(), fits.BinTableHDU(sensitivity, name="SENSITIVITY"), fits.BinTableHDU(sensitivity_step_2, name="SENSITIVITY_STEP_2"), fits.BinTableHDU(theta_cuts, name="THETA_CUTS"), fits.BinTableHDU(theta_cuts_opt, name="THETA_CUTS_OPT"), fits.BinTableHDU(gh_cuts, name="GH_CUTS"), ] masks = { "": gammas["selected"], "_NO_CUTS": slice(None), "_ONLY_GH": gammas["selected_gh"], "_ONLY_THETA": gammas["selected_theta"], } # binnings for the irfs true_energy_bins = add_overflow_bins( create_bins_per_decade( 10**-1.9 * u.TeV, 10**2.31 * u.TeV, 10, )) reco_energy_bins = add_overflow_bins( create_bins_per_decade( 10**-1.9 * u.TeV, 10**2.31 * u.TeV, 10, )) fov_offset_bins = [0, 0.5] * u.deg source_offset_bins = np.arange(0, 1 + 1e-4, 1e-3) * u.deg energy_migration_bins = np.geomspace(0.2, 5, 200) for label, mask in masks.items(): effective_area = point_like_effective_area( gammas[mask], particles["gamma"]["simulation_info"], true_energy_bins=true_energy_bins, ) hdus.append( create_aeff2d_hdu( effective_area[..., np.newaxis], # add one dimension for FOV offset true_energy_bins, fov_offset_bins, extname="EFFECTIVE_AREA" + label, )) edisp = energy_dispersion( gammas[mask], true_energy_bins=true_energy_bins, fov_offset_bins=fov_offset_bins, migration_bins=energy_migration_bins, ) hdus.append( create_energy_dispersion_hdu( edisp, true_energy_bins=true_energy_bins, migration_bins=energy_migration_bins, fov_offset_bins=fov_offset_bins, extname="ENERGY_DISPERSION" + label, )) bias_resolution = energy_bias_resolution( gammas[gammas["selected"]], true_energy_bins, ) ang_res = angular_resolution( gammas[gammas["selected_gh"]], true_energy_bins, ) psf = psf_table( gammas[gammas["selected_gh"]], true_energy_bins, fov_offset_bins=fov_offset_bins, source_offset_bins=source_offset_bins, ) background_rate = background_2d( background[background['selected_gh']], reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg, t_obs=T_OBS, ) hdus.append( create_background_2d_hdu( background_rate, reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg, )) hdus.append( create_psf_table_hdu( psf, true_energy_bins, source_offset_bins, fov_offset_bins, )) hdus.append( create_rad_max_hdu(theta_bins, fov_offset_bins, rad_max=theta_cuts_opt["cut"][:, np.newaxis])) hdus.append(fits.BinTableHDU(ang_res, name="ANGULAR_RESOLUTION")) hdus.append( fits.BinTableHDU(bias_resolution, name="ENERGY_BIAS_RESOLUTION")) fits.HDUList(hdus).writeto("pyirf_eventdisplay.fits.gz", overwrite=True)
def main(): logging.basicConfig(level=logging.INFO) logging.getLogger("pyirf").setLevel(logging.DEBUG) for particle_type, p in particles.items(): log.info(f"Simulated {particle_type.title()} Events:") p["events"], p["simulation_info"] = read_eventdisplay_fits(p["file"]) p["events"]["particle_type"] = particle_type p["simulated_spectrum"] = PowerLaw.from_simulation( p["simulation_info"], T_OBS) p["events"]["weight"] = calculate_event_weights( p["events"]["true_energy"], p["target_spectrum"], p["simulated_spectrum"]) for prefix in ('true', 'reco'): k = f"{prefix}_source_fov_offset" p["events"][k] = calculate_source_fov_offset(p["events"], prefix=prefix) # calculate theta / distance between reco and assuemd source positoin # we handle only ON observations here, so the assumed source pos # is the pointing position p["events"]["theta"] = calculate_theta( p["events"], assumed_source_az=p["events"]["pointing_az"], assumed_source_alt=p["events"]["pointing_alt"], ) log.info(p["simulation_info"]) log.info("") gammas = particles["gamma"]["events"] # background table composed of both electrons and protons background = table.vstack( [particles["proton"]["events"], particles["electron"]["events"]]) INITIAL_GH_CUT = np.quantile(gammas['gh_score'], (1 - INITIAL_GH_CUT_EFFICENCY)) log.info( f"Using fixed G/H cut of {INITIAL_GH_CUT} to calculate theta cuts") # event display uses much finer bins for the theta cut than # for the sensitivity theta_bins = add_overflow_bins( create_bins_per_decade( 10**(-1.9) * u.TeV, 10**2.3005 * u.TeV, 50, )) # theta cut is 68 percent containmente of the gammas # for now with a fixed global, unoptimized score cut mask_theta_cuts = gammas["gh_score"] >= INITIAL_GH_CUT theta_cuts = calculate_percentile_cut( gammas["theta"][mask_theta_cuts], gammas["reco_energy"][mask_theta_cuts], bins=theta_bins, min_value=0.05 * u.deg, fill_value=0.32 * u.deg, max_value=0.32 * u.deg, percentile=68, ) # same bins as event display uses sensitivity_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, bins_per_decade=5)) log.info("Optimizing G/H separation cut for best sensitivity") gh_cut_efficiencies = np.arange( GH_CUT_EFFICIENCY_STEP, MAX_GH_CUT_EFFICIENCY + GH_CUT_EFFICIENCY_STEP / 2, GH_CUT_EFFICIENCY_STEP) sensitivity_step_2, gh_cuts = optimize_gh_cut( gammas, background, reco_energy_bins=sensitivity_bins, gh_cut_efficiencies=gh_cut_efficiencies, op=operator.ge, theta_cuts=theta_cuts, alpha=ALPHA, background_radius=MAX_BG_RADIUS, ) # now that we have the optimized gh cuts, we recalculate the theta # cut as 68 percent containment on the events surviving these cuts. log.info('Recalculating theta cut for optimized GH Cuts') for tab in (gammas, background): tab["selected_gh"] = evaluate_binned_cut(tab["gh_score"], tab["reco_energy"], gh_cuts, operator.ge) theta_cuts_opt = calculate_percentile_cut( gammas[gammas['selected_gh']]["theta"], gammas[gammas['selected_gh']]["reco_energy"], theta_bins, percentile=68, fill_value=0.32 * u.deg, max_value=0.32 * u.deg, min_value=0.05 * u.deg, ) gammas["selected_theta"] = evaluate_binned_cut(gammas["theta"], gammas["reco_energy"], theta_cuts_opt, operator.le) gammas["selected"] = gammas["selected_theta"] & gammas["selected_gh"] # calculate sensitivity signal_hist = create_histogram_table(gammas[gammas["selected"]], bins=sensitivity_bins) background_hist = estimate_background( background[background["selected_gh"]], reco_energy_bins=sensitivity_bins, theta_cuts=theta_cuts_opt, alpha=ALPHA, background_radius=MAX_BG_RADIUS, ) sensitivity = calculate_sensitivity(signal_hist, background_hist, alpha=ALPHA) # scale relative sensitivity by Crab flux to get the flux sensitivity spectrum = particles['gamma']['target_spectrum'] for s in (sensitivity_step_2, sensitivity): s["flux_sensitivity"] = (s["relative_sensitivity"] * spectrum(s['reco_energy_center'])) log.info('Calculating IRFs') hdus = [ fits.PrimaryHDU(), fits.BinTableHDU(sensitivity, name="SENSITIVITY"), fits.BinTableHDU(sensitivity_step_2, name="SENSITIVITY_STEP_2"), fits.BinTableHDU(theta_cuts, name="THETA_CUTS"), fits.BinTableHDU(theta_cuts_opt, name="THETA_CUTS_OPT"), fits.BinTableHDU(gh_cuts, name="GH_CUTS"), ] masks = { "": gammas["selected"], "_NO_CUTS": slice(None), "_ONLY_GH": gammas["selected_gh"], "_ONLY_THETA": gammas["selected_theta"], } # binnings for the irfs true_energy_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, 10)) reco_energy_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, 5)) fov_offset_bins = [0, 0.5] * u.deg source_offset_bins = np.arange(0, 1 + 1e-4, 1e-3) * u.deg energy_migration_bins = np.geomspace(0.2, 5, 200) for label, mask in masks.items(): effective_area = effective_area_per_energy( gammas[mask], particles["gamma"]["simulation_info"], true_energy_bins=true_energy_bins, ) hdus.append( create_aeff2d_hdu( effective_area[..., np.newaxis], # add one dimension for FOV offset true_energy_bins, fov_offset_bins, extname="EFFECTIVE_AREA" + label, )) edisp = energy_dispersion( gammas[mask], true_energy_bins=true_energy_bins, fov_offset_bins=fov_offset_bins, migration_bins=energy_migration_bins, ) hdus.append( create_energy_dispersion_hdu( edisp, true_energy_bins=true_energy_bins, migration_bins=energy_migration_bins, fov_offset_bins=fov_offset_bins, extname="ENERGY_DISPERSION" + label, )) bias_resolution = energy_bias_resolution(gammas[gammas["selected"]], reco_energy_bins, energy_type="reco") ang_res = angular_resolution(gammas[gammas["selected_gh"]], reco_energy_bins, energy_type="reco") psf = psf_table( gammas[gammas["selected_gh"]], true_energy_bins, fov_offset_bins=fov_offset_bins, source_offset_bins=source_offset_bins, ) background_rate = background_2d( background[background['selected_gh']], reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg, t_obs=T_OBS, ) hdus.append( create_background_2d_hdu( background_rate, reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg, )) hdus.append( create_psf_table_hdu( psf, true_energy_bins, source_offset_bins, fov_offset_bins, )) hdus.append( create_rad_max_hdu(theta_cuts_opt["cut"][:, np.newaxis], theta_bins, fov_offset_bins)) hdus.append(fits.BinTableHDU(ang_res, name="ANGULAR_RESOLUTION")) hdus.append( fits.BinTableHDU(bias_resolution, name="ENERGY_BIAS_RESOLUTION")) log.info('Writing outputfile') fits.HDUList(hdus).writeto("pyirf_eventdisplay.fits.gz", overwrite=True)