Skip to content

Commit

Permalink
Changed selection of pointing nodes for interpolation.
Browse files Browse the repository at this point in the history
Instead of closest angular distance we now take the closest nodes in alt_tel
on the same side of the culmination (same sign of sin_az_tel)
  • Loading branch information
moralejo committed Dec 2, 2024
1 parent 80c0ffc commit b0afdac
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lstchain/reco/dl1_to_dl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import joblib
import numpy as np
import pandas as pd
from astropy.coordinates import SkyCoord, Angle, angular_separation
from astropy.coordinates import SkyCoord, Angle
from astropy.time import Time
from pathlib import Path
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
Expand Down Expand Up @@ -62,11 +62,12 @@ def add_zd_interpolation_info(dl2table, training_zd_deg, training_az_deg):
Parameters:
-----------
dl2table: pandas dataframe. Four columns will be added: alt0, alt1, w0, w1
alt0 and alt1 are the alt_tel (in rad) values (telescope elevation) for
the closest and second-closest training MC pointing for each event in the
table. The values w0 and w1 are the corresponding weights that, multiplied
by the RF predictions at those two pointings, provide the interpolated
result for each event's pointing
alt0 and alt1 are the alt_tel values (telescope elevation, in radians) of
the closest and second-closest training MC pointings (closest in elevation,
on the same side of culmination) for each event in the table. The values
w0 and w1 are the corresponding weights that, multiplied by the RF
predictions at those two pointings, provide the interpolated result for
each event's pointing
training_zd_deg: array containing the zenith distances (in deg) for the
MC training nodes
Expand Down Expand Up @@ -95,11 +96,15 @@ def add_zd_interpolation_info(dl2table, training_zd_deg, training_az_deg):
len(training_alt_rad)).reshape(len(training_alt_rad),
len(dl2table)).T

angular_distance = angular_separation(training_az_rad, training_alt_rad,
tiled_az, tiled_alt)
# indices ordered in angular distance to each event:
sorted_indices = np.argsort(angular_distance, axis=1)

delta_alt = np.abs(training_alt_rad - tiled_alt)

Check warning on line 99 in lstchain/reco/dl1_to_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/reco/dl1_to_dl2.py#L99

Added line #L99 was not covered by tests
# mask to select training nodes only on the same side of the source
# culmination as the event:
same_side_of_culmination = np.sign(np.sin(training_az_rad) *

Check warning on line 102 in lstchain/reco/dl1_to_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/reco/dl1_to_dl2.py#L102

Added line #L102 was not covered by tests
np.sin(tiled_az)) > 0
# Just fill a large value for pointings on the other side of culmination:
delta_alt = np.where(same_side_of_culmination, delta_alt, np.pi/2)

Check warning on line 105 in lstchain/reco/dl1_to_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/reco/dl1_to_dl2.py#L105

Added line #L105 was not covered by tests
# indices ordered according to distance in telescope elevation
sorted_indices = np.argsort(delta_alt, axis=1)
closest_alt = training_alt_rad[sorted_indices[:, 0]]
second_closest_alt = training_alt_rad[sorted_indices[:, 1]]

Check warning on line 109 in lstchain/reco/dl1_to_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/reco/dl1_to_dl2.py#L107-L109

Added lines #L107 - L109 were not covered by tests

Expand All @@ -108,10 +113,10 @@ def add_zd_interpolation_info(dl2table, training_zd_deg, training_az_deg):
cos_tel_zd = np.cos(np.pi / 2 - alt_tel)

Check warning on line 113 in lstchain/reco/dl1_to_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/reco/dl1_to_dl2.py#L111-L113

Added lines #L111 - L113 were not covered by tests

# Compute the weights that multiplied times the RF predictions at the
# closest (0) and 2nd-closest (1) nodes result in the interpolated value.
# Take care of cases in which the two closest nodes happen to have the
# same zenith (or very close)! (if so, both nodes are set to have equal
# weight in the interpolation)
# closest (0) and 2nd-closest (1) nodes (in alt_tel) result in the
# interpolated value. Take care of cases in which the two closest nodes
# happen to have the same zenith (or very close)! (if so, both nodes are
# set to have equal weight in the interpolation)
w1 = np.where(np.isclose(closest_alt, second_closest_alt, atol=1e-4, rtol=0),

Check warning on line 120 in lstchain/reco/dl1_to_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/reco/dl1_to_dl2.py#L120

Added line #L120 was not covered by tests
0.5, (cos_tel_zd - c0) / (c1 - c0))
w0 = 1 - w1

Check warning on line 122 in lstchain/reco/dl1_to_dl2.py

View check run for this annotation

Codecov / codecov/patch

lstchain/reco/dl1_to_dl2.py#L122

Added line #L122 was not covered by tests
Expand Down

0 comments on commit b0afdac

Please sign in to comment.