Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions fact_plots/scripts/plot_data_mc_compare.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fact.io import read_h5py
from fact.analysis.statistics import calc_proton_obstime, calc_weight_change_index
from fact.analysis.statistics import calc_proton_obstime, calc_gamma_obstime, calc_weight_change_index
import astropy.units as u
import numpy as np
import click
Expand All @@ -10,6 +10,11 @@
from tqdm import tqdm
from fnmatch import fnmatch

dl3_ids = dict(
observations=['event_num','run_id','night'],
gammas=['event_num','run_id','corsika_run_header_run_number','corsika_event_header_event_number'],
protons=['event_num','run_id','corsika_run_header_run_number','corsika_event_header_event_number'],
)

def wrap_angle(angle):
angle = np.asanyarray(angle).copy()
Expand Down Expand Up @@ -94,10 +99,18 @@ def main(config, outputfile):

# get columns available in all datasets and calculate weights
weights = OrderedDict()
dl3 = OrderedDict()
for i, dataset in enumerate(config['datasets']):
l = dataset['label']
df = read_h5py(dataset['path'], key='events', last=1)

if 'prediction' in config.keys():
if 'dl3_path' in dataset.keys():
dl3[l] = read_h5py(dataset['dl3_path'], key='events')
dl3[l] = dl3[l].query(f"gamma_prediction >= {config['prediction']['threshold']}")
dl3[l] = dl3[l].query(f"theta_deg <= {np.sqrt(config['prediction']['theta'])}")


if i == 0:
common_columns = set(df.columns)
else:
Expand All @@ -121,6 +134,21 @@ def main(config, outputfile):
)
weights[l] = 1 / (ontime.to_value(u.hour) * sample_fraction)

elif dataset['kind'] == 'gammas':

norm = dataset['phi_0']
norm = u.Quantity(norm['value'], norm['unit'])
sample_fraction = dataset.get('sample_fraction', 1.0)
ontime = calc_gamma_obstime(
n_events=float(dataset['n_showers']),
spectral_index=dataset['spectral_index'],
max_impact=dataset['max_impact'] * u.m,
e_min=float(dataset['e_min']) * u.GeV,
e_max=float(dataset['e_max']) * u.GeV,
flux_normalization=norm,
)
weights[l] = 1 / (ontime.to_value(u.hour) * sample_fraction)

# select columns
columns = config.get('include_columns')
if columns is not None:
Expand Down Expand Up @@ -157,7 +185,7 @@ def excluded(column):
dfs = OrderedDict()
for dataset in config['datasets']:
l = dataset['label']
dfs[l] = read_h5py(dataset['path'], key='events', columns=[column])
dfs[l] = read_h5py(dataset['path'], key='events', columns=[column]+dl3_ids[dataset['kind']])
dfs[l]['weight'] = weights[l]

if dataset['kind'] == 'protons':
Expand All @@ -178,11 +206,13 @@ def excluded(column):
).to_value(u.dimensionless_unscaled)

dfs[l]['weight'] *= index_weights[l]
if l in dl3.keys():
dfs[l] = dl3[l].merge(dfs[l], on=dl3_ids[dataset['kind']])


if i == 0:
for l, df in dfs.items():
print(f'{l: <15}', f'{df["weight"].sum() / 3600:.1f} Events/s')
print(f'{l: <15}', f'{df["weight"].sum() / 60:.1f} Events/h ')

ax_hist.cla()
try:
Expand Down