Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Irf tool #2315

Closed
wants to merge 39 commits into from
Closed

Irf tool #2315

wants to merge 39 commits into from

Conversation

Tobychev
Copy link
Contributor

Here is a first draft of an tool to generate irf-files from a set of DL2 files.

It is primarily a port of the protopipe functionality, but I've tried to also incorporate changes from lstpipe and pyirf example scripts. In practice I think most of the differences is about the handling of offset binning. For expediency I followed the protopipe example on that.

@Tobychev Tobychev marked this pull request as draft April 21, 2023 11:47
ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
description = "Tool to create IRF files in GAD format"

def make_derived_columns(self, events, spectrum, target_spectrum):
events["pointing_az"] = 0 * u.deg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be obtained nowadays from the observation block information (and is loaded if you pass load_observation_info to TableLoader.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the event weighting, we might also want to have the option to join the simulation config as well, as in principle the simulated spectrum can change between runs (even if currently it does not), for example if extra sims were run to fill in statistics at high-energies only. That feature should be a separate PR though, and may not be necessary if in the IRF tool we use the simulated particle distribution historgrams

ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
ctapipe/tools/make_irf.py Outdated Show resolved Hide resolved
ctapipe/tools/make_irf.py Outdated Show resolved Hide resolved
setup.cfg Show resolved Hide resolved
ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
@Tobychev Tobychev marked this pull request as ready for review May 5, 2023 11:20
ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
ctapipe/tools/make_irf.py Outdated Show resolved Hide resolved
ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
@maxnoe maxnoe added this to the v0.20.0 milestone May 8, 2023
ctapipe/tools/make_irf.py Show resolved Hide resolved
)

# scale relative sensitivity by Crab flux to get the flux sensitivity
for s in (sens2, self.sensitivity):
Copy link
Contributor

@kosack kosack Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you also scale sens2, which is never used or returned. What is the difference between self.sensitivity computed above, and sens2, which is computed during the optimization? Are they the same thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a legacy of whatever script it I converted into the tool, I've just added saving sens2 to the output to check if there is any difference at all. For example, it might be that historically there was some difference but over time pyirf was refactored to use the same sensitivity function everywhere and now there is no difference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which script are you referring to?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current example script in pyirf only computes sensitivity once using optimize_gh_cuts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxnoe Probably this protopipe script. Is this the blessed script in pyIRF? The "Event display" in the name always makes me hesitant to conclude it is the appropriate script for ctapipe files.

ctapipe/tools/make_irf.py Outdated Show resolved Hide resolved
self.signal[self.signal["selected"]], bins=self.reco_energy_bins
)

background_hist = estimate_background(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between this background hist and the one generated in finish()? One uses estimate_background and one uses background_2d. Are these the same computations? I worry that things are computed multiple times, and it's not clear which ones are in the output vs used in the sensitivity

Copy link
Contributor

@kosack kosack Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I don't find pyirf.sensitivity.estimate_background in the PyIRF documentation, so could it be incorrect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That function is what pyIRF uses internally so probably this is a problem with the docs.

To be honest I'm not sure what the difference is. It looks like estimate_background only does one offset bin but also rescales from the size of the Off region to the On region. Meanwhile background_2d calculates background rates in each offset bin, but does not rescale to the On region size...

Copy link
Member

@maxnoe maxnoe Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pyirf.sensitivity.estimate_background

this function estimates the number of background counts (n_off) for the given observation time and given on region size in the binning used for the sensitivity computation.

pyirf.irf.background.background_2d is the background IRF component as defined by GADF.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I don't find pyirf.sensitivity.estimate_background in the PyIRF documentation, so could it be incorrect?

It was missing in the module __all__, added now here:
cta-observatory/pyirf#256

ctapipe/tools/make_irf.py Outdated Show resolved Hide resolved
ctapipe/tools/make_irf.py Outdated Show resolved Hide resolved
@kosack kosack modified the milestones: v0.20.0, v0.21.0 Sep 8, 2023
self.fov_offset_bins,
)
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For debugging and benchmarking, it might be nice to add a few more historgrams in the output files (all vs energy and offset):

  • N_on and N_off counts (maybe even N_excess and Significance), which can help to see where limiting factors of background and minimum counts are taken into account.
  • background rate per particle species (same as the background2d already there, but for electrons and protons separately)
  • Effective area for protons and electrons separately

ctapipe/tools/make_irf.py Outdated Show resolved Hide resolved
@kosack
Copy link
Contributor

kosack commented Sep 8, 2023

It occurs to me that it would be very useful to add a unit test that reads the generated IRFs using gammapy. You would have to add gammapy the test requirements in setup.cfg, but that is fine - it's not a ctapipe dependency, but still could be a test dependency. That way you test explicitly that the output is readable, which currently it is not due to a mismatch in the HDU bins for background.

ctapipe/irf/irf_classes.py Outdated Show resolved Hide resolved
@maxnoe maxnoe marked this pull request as draft February 23, 2024 10:24
@maxnoe
Copy link
Member

maxnoe commented Feb 23, 2024

Follow up here #2473

@maxnoe maxnoe closed this Feb 23, 2024
@maxnoe maxnoe removed this from the v0.21.0 milestone Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants